use std::env;
use alloy_primitives::{Address, Bytes, B256, U256};
use alloy_sol_types::SolCall;
use reqwest::Client;
use serde_json::{json, Value};
use taskfast_agent::chain::{TaskEscrow, IERC20};
use taskfast_client::TaskFastClient;
async fn rpc(client: &Client, rpc_url: &str, method: &str, params: Value) -> Value {
let resp = client
.post(rpc_url)
.json(&json!({"jsonrpc":"2.0","id":1,"method":method,"params":params}))
.send()
.await
.expect("rpc send");
let body: Value = resp.json().await.expect("rpc parse");
println!("---- {method}");
println!("{}", serde_json::to_string_pretty(&body).unwrap());
body
}
async fn eth_call_probe(
client: &Client,
rpc_url: &str,
from: Address,
to: Address,
data: &Bytes,
label: &str,
) {
println!("\n==== {label} ====");
rpc(
client,
rpc_url,
"eth_call",
json!([{
"from": format!("{from:#x}"),
"to": format!("{to:#x}"),
"data": format!("0x{}", hex::encode(data)),
}, "latest"]),
)
.await;
}
#[tokio::main]
async fn main() {
let api = env::var("TASKFAST_API").unwrap_or_else(|_| "http://localhost:4000".into());
let api_key = env::var("TASKFAST_API_KEY").expect("TASKFAST_API_KEY");
let bid_id = env::var("BID").expect("BID=<uuid>");
let tf = TaskFastClient::from_api_key(&api, &api_key).expect("construct client");
let cfg = tf
.fetch_network_config()
.await
.expect("fetch /config/network");
let rpc_url = cfg
.entry("testnet")
.expect("deployment advertises testnet")
.rpc_url
.clone();
let http = tf.http_client();
let params: Value = http
.get(format!("{api}/bids/{bid_id}/escrow/params"))
.header("X-API-Key", &api_key)
.send()
.await
.expect("api send")
.json()
.await
.expect("api parse");
println!("==== escrow_params");
println!("{}", serde_json::to_string_pretty(¶ms).unwrap());
let p = if params.get("data").is_some() {
¶ms["data"]
} else {
¶ms
};
let token: Address = p["token_address"].as_str().unwrap().parse().unwrap();
let task_escrow: Address = p["task_escrow_contract"].as_str().unwrap().parse().unwrap();
let worker: Address = p["worker_address"].as_str().unwrap().parse().unwrap();
let platform: Address = p["platform_wallet"].as_str().unwrap().parse().unwrap();
let decimals = p["decimals"].as_i64().unwrap() as u8;
let deposit = decimal_to_u256(p["amount"].as_str().unwrap(), decimals);
let fee = decimal_to_u256(p["platform_fee_amount"].as_str().unwrap(), decimals);
let memo_hash: Option<B256> = p["memo_hash"].as_str().map(|s| s.parse().unwrap());
let poster: Address = env::var("TEMPO_WALLET_ADDRESS")
.expect("TEMPO_WALLET_ADDRESS")
.parse()
.unwrap();
println!("\ncomputed: token={token:#x} task_escrow={task_escrow:#x} worker={worker:#x} platform={platform:#x}");
println!(" poster={poster:#x} deposit={deposit} fee={fee} memo_hash={memo_hash:?}");
rpc(
&http,
&rpc_url,
"eth_getCode",
json!([format!("{task_escrow:#x}"), "latest"]),
)
.await;
rpc(
&http,
&rpc_url,
"eth_getCode",
json!([format!("{token:#x}"), "latest"]),
)
.await;
let balance_data: Bytes = IERC20::balanceOfCall { account: poster }
.abi_encode()
.into();
eth_call_probe(
&http,
&rpc_url,
poster,
token,
&balance_data,
"token.balanceOf(poster)",
)
.await;
let allow_data: Bytes = IERC20::allowanceCall {
owner: poster,
spender: task_escrow,
}
.abi_encode()
.into();
eth_call_probe(
&http,
&rpc_url,
poster,
token,
&allow_data,
"token.allowance(poster, task_escrow)",
)
.await;
let salt = B256::from(rand::random::<[u8; 32]>());
let memo = memo_hash.unwrap_or_default();
let open_memo_data: Bytes = TaskEscrow::openWithMemoCall {
token,
deposit,
worker,
platformFeeAmount: fee,
platform,
salt,
memoHash: memo,
}
.abi_encode()
.into();
eth_call_probe(
&http,
&rpc_url,
poster,
task_escrow,
&open_memo_data,
"openWithMemo (live params)",
)
.await;
let open_data: Bytes = TaskEscrow::openCall {
token,
deposit,
worker,
platformFeeAmount: fee,
platform,
salt,
}
.abi_encode()
.into();
eth_call_probe(
&http,
&rpc_url,
poster,
task_escrow,
&open_data,
"open (no memo)",
)
.await;
let open_zero_fee: Bytes = TaskEscrow::openWithMemoCall {
token,
deposit,
worker,
platformFeeAmount: U256::ZERO,
platform,
salt,
memoHash: memo,
}
.abi_encode()
.into();
eth_call_probe(
&http,
&rpc_url,
poster,
task_escrow,
&open_zero_fee,
"openWithMemo (fee=0)",
)
.await;
let open_min: Bytes = TaskEscrow::openWithMemoCall {
token,
deposit: U256::from(1u64),
worker,
platformFeeAmount: U256::ZERO,
platform,
salt,
memoHash: memo,
}
.abi_encode()
.into();
eth_call_probe(
&http,
&rpc_url,
poster,
task_escrow,
&open_min,
"openWithMemo (deposit=1, fee=0)",
)
.await;
println!("\n==== debug_traceCall openWithMemo (may be unsupported) ====");
rpc(
&http,
&rpc_url,
"debug_traceCall",
json!([{
"from": format!("{poster:#x}"),
"to": format!("{task_escrow:#x}"),
"data": format!("0x{}", hex::encode(&open_memo_data)),
}, "latest", {"tracer": "callTracer"}]),
)
.await;
}
fn decimal_to_u256(s: &str, decimals: u8) -> U256 {
let (w, f) = s.split_once('.').unwrap_or((s, ""));
let max = decimals as usize;
assert!(
f.len() <= max,
"decimal_to_u256: {s} has {} fractional digits, exceeds token decimals {max}",
f.len()
);
let mut combined = String::new();
combined.push_str(w);
combined.push_str(f);
for _ in 0..max.saturating_sub(f.len()) {
combined.push('0');
}
let stripped = combined.trim_start_matches('0');
let digits = if stripped.is_empty() { "0" } else { stripped };
U256::from_str_radix(digits, 10).unwrap()
}