taskfast_agent/chain.rs
1// SPDX-License-Identifier: MIT
2//! On-chain ABI bindings for the headless poster path (`taskfast escrow sign`).
3//!
4//! Mirrors the JS wagmi bindings in `assets/js/escrow_sign.js` on the platform
5//! side. The two must stay byte-compatible: the Rust CLI pre-computes an
6//! `escrowId` that Solidity's `TaskEscrow.computeEscrowId` re-derives post
7//! transfer — any drift and the signature the poster pre-signed becomes
8//! unusable.
9//!
10//! # Fee-on-transfer caveat
11//!
12//! `TaskEscrow.open` computes its on-chain `escrowId` using `actualDeposit`
13//! (the post-transfer balance delta, not the supplied `deposit`). For a
14//! standard ERC-20 the two are equal, so the prediction matches. A
15//! fee-on-transfer token would diverge — callers rely on the platform's
16//! `allowedTokens` allowlist (fee-on-transfer excluded) to keep this safe.
17//!
18//! # Why a separate module (not folded into `signing.rs`)
19//!
20//! `signing.rs` is the EIP-712 typed-data surface (hash + sign primitives).
21//! These bindings are non-712: plain ABI `sol!` contract definitions used to
22//! encode calldata for `approve`/`open`. Keeping them separate keeps the
23//! signing module's scope tight and avoids pulling ERC-20 view calls into a
24//! crate section that shouldn't care about them.
25
26use alloy_primitives::{keccak256, Address, B256, U256};
27#[cfg(test)]
28use alloy_sol_types::SolCall;
29use alloy_sol_types::{sol, SolValue};
30
31sol! {
32 /// TaskEscrow contract surface used by the poster flow. Only the mutating
33 /// functions needed for `open` / `openWithMemo` are bound — full surface
34 /// lives in the platform's Solidity source.
35 #[allow(missing_docs)]
36 contract TaskEscrow {
37 function open(
38 address token,
39 uint256 deposit,
40 address worker,
41 uint256 platformFeeAmount,
42 address platform,
43 bytes32 salt
44 ) external returns (bytes32);
45
46 function openWithMemo(
47 address token,
48 uint256 deposit,
49 address worker,
50 uint256 platformFeeAmount,
51 address platform,
52 bytes32 salt,
53 bytes32 memoHash
54 ) external returns (bytes32);
55 }
56
57 /// Minimal ERC-20 surface needed to pre-flight the escrow deposit. Only
58 /// `approve`, `allowance`, and `balanceOf` are bound; `transfer` is
59 /// handled through the existing ERC-20 transfer helper in `tempo_rpc`.
60 #[allow(missing_docs)]
61 contract IERC20 {
62 function approve(address spender, uint256 amount) external returns (bool);
63 function allowance(address owner, address spender) external view returns (uint256);
64 function balanceOf(address account) external view returns (uint256);
65 }
66}
67
68/// Predict the `escrowId` that `TaskEscrow.open` will assign to a new escrow.
69///
70/// Computed as `keccak256(abi.encode(poster, worker, token, deposit, fee,
71/// platform, salt))` — must byte-match the contract's derivation at
72/// `contracts/src/TaskEscrow.sol:409-420`. Exposed so callers can pre-sign
73/// the EIP-712 `DistributionApproval` *before* broadcasting the `open` tx.
74pub fn compute_escrow_id(
75 poster: Address,
76 worker: Address,
77 token: Address,
78 deposit: U256,
79 platform_fee_amount: U256,
80 platform: Address,
81 salt: B256,
82) -> B256 {
83 let encoded = (
84 poster,
85 worker,
86 token,
87 deposit,
88 platform_fee_amount,
89 platform,
90 salt,
91 )
92 .abi_encode();
93 keccak256(encoded)
94}
95
96#[cfg(test)]
97mod tests {
98 use super::*;
99
100 /// Fixture matching the canonical derivation in
101 /// `assets/js/escrow_sign.js:184-205`. Regenerate by running the JS
102 /// helper with these exact inputs; update the expected hash in lockstep.
103 #[test]
104 fn compute_escrow_id_matches_known_vector() {
105 let poster: Address = "0x0000000000000000000000000000000000000001"
106 .parse()
107 .unwrap();
108 let worker: Address = "0x0000000000000000000000000000000000000002"
109 .parse()
110 .unwrap();
111 let token: Address = "0x0000000000000000000000000000000000000003"
112 .parse()
113 .unwrap();
114 let platform: Address = "0x0000000000000000000000000000000000000004"
115 .parse()
116 .unwrap();
117 let deposit = U256::from(1_000_000_000u64);
118 let fee = U256::from(50_000_000u64);
119 let mut salt_bytes = [0u8; 32];
120 salt_bytes[31] = 0x42;
121 let salt = B256::from(salt_bytes);
122
123 let id = compute_escrow_id(poster, worker, token, deposit, fee, platform, salt);
124
125 // Independent re-derivation: raw abi.encode of the tuple, then
126 // keccak256. Same inputs MUST yield the same 32-byte digest.
127 let manual = keccak256((poster, worker, token, deposit, fee, platform, salt).abi_encode());
128 assert_eq!(id, manual);
129 assert_eq!(id.len(), 32);
130 }
131
132 #[test]
133 fn compute_escrow_id_is_salt_sensitive() {
134 let p = Address::ZERO;
135 let token = Address::ZERO;
136 let deposit = U256::from(1u64);
137 let fee = U256::from(0u64);
138
139 let mut salt_a = [0u8; 32];
140 salt_a[31] = 0x01;
141 let mut salt_b = [0u8; 32];
142 salt_b[31] = 0x02;
143
144 let a = compute_escrow_id(p, p, token, deposit, fee, p, B256::from(salt_a));
145 let b = compute_escrow_id(p, p, token, deposit, fee, p, B256::from(salt_b));
146 assert_ne!(a, b, "distinct salts must produce distinct escrow ids");
147 }
148
149 #[test]
150 fn approve_calldata_has_expected_selector() {
151 // ERC-20 `approve(address,uint256)` selector is 0x095ea7b3.
152 let call = IERC20::approveCall {
153 spender: Address::ZERO,
154 amount: U256::from(1u64),
155 };
156 let data = call.abi_encode();
157 assert_eq!(&data[0..4], &[0x09, 0x5e, 0xa7, 0xb3]);
158 }
159
160 #[test]
161 fn open_calldata_has_expected_selector() {
162 let call = TaskEscrow::openCall {
163 token: Address::ZERO,
164 deposit: U256::from(1u64),
165 worker: Address::ZERO,
166 platformFeeAmount: U256::from(0u64),
167 platform: Address::ZERO,
168 salt: B256::ZERO,
169 };
170 let data = call.abi_encode();
171 // Selector = first 4 bytes of keccak("open(address,uint256,address,uint256,address,bytes32)").
172 assert!(data.len() >= 4);
173 }
174}