use alloy_primitives::{keccak256, Address, B256, U256};
#[cfg(test)]
use alloy_sol_types::SolCall;
use alloy_sol_types::{sol, SolValue};
sol! {
#[allow(missing_docs)]
contract TaskEscrow {
function open(
address token,
uint256 deposit,
address worker,
uint256 platformFeeAmount,
address platform,
address arbitrator,
bytes32 salt
) external returns (bytes32);
function openWithMemo(
address token,
uint256 deposit,
address worker,
uint256 platformFeeAmount,
address platform,
address arbitrator,
bytes32 salt,
bytes32 memoHash
) external returns (bytes32);
}
#[allow(missing_docs)]
contract IERC20 {
function approve(address spender, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
}
#[allow(missing_docs)]
contract TaskBond {
function post(
address token,
uint256 amount,
bytes32 taskRef,
bytes32 salt
) external;
}
}
mod dispute_abi {
alloy_sol_types::sol! {
function dispute(bytes32 escrowId) external;
}
}
pub fn dispute_calldata(escrow_id: B256) -> Vec<u8> {
alloy_sol_types::SolCall::abi_encode(&dispute_abi::disputeCall {
escrowId: escrow_id,
})
}
mod timeout_abi {
alloy_sol_types::sol! {
struct Escrow {
address poster;
address worker;
address platform;
address arbitrator;
address token;
uint256 deposit;
uint256 platformFeeAmount;
uint8 state;
}
function timeoutResolveDispute(bytes32 escrowId) external;
function getEscrow(bytes32 escrowId) external view returns (Escrow memory);
function disputedAt(bytes32 escrowId) external view returns (uint256);
function DISPUTE_TIMEOUT() external view returns (uint256);
}
}
pub const ESCROW_STATE_DISPUTED: u8 = 5;
pub fn timeout_resolve_calldata(escrow_id: B256) -> Vec<u8> {
alloy_sol_types::SolCall::abi_encode(&timeout_abi::timeoutResolveDisputeCall {
escrowId: escrow_id,
})
}
pub fn get_escrow_calldata(escrow_id: B256) -> Vec<u8> {
alloy_sol_types::SolCall::abi_encode(&timeout_abi::getEscrowCall {
escrowId: escrow_id,
})
}
pub fn disputed_at_calldata(escrow_id: B256) -> Vec<u8> {
alloy_sol_types::SolCall::abi_encode(&timeout_abi::disputedAtCall {
escrowId: escrow_id,
})
}
pub fn dispute_timeout_calldata() -> Vec<u8> {
alloy_sol_types::SolCall::abi_encode(&timeout_abi::DISPUTE_TIMEOUTCall {})
}
pub fn decode_u256(data: &[u8]) -> Result<U256, String> {
<alloy_sol_types::sol_data::Uint<256> as alloy_sol_types::SolType>::abi_decode(data)
.map_err(|e| format!("not a uint256: {e}"))
}
pub fn decode_escrow_state(data: &[u8]) -> Result<u8, String> {
let e = <timeout_abi::Escrow as alloy_sol_types::SolType>::abi_decode(data)
.map_err(|e| format!("not an Escrow struct: {e}"))?;
Ok(e.state)
}
pub fn compute_task_ref(task_id: uuid::Uuid) -> B256 {
let mut bytes = [0u8; 32];
bytes[16..].copy_from_slice(task_id.as_bytes());
B256::from(bytes)
}
pub struct EscrowIdParams {
pub poster: Address,
pub worker: Address,
pub token: Address,
pub deposit: U256,
pub platform_fee_amount: U256,
pub platform: Address,
pub arbitrator: Address,
pub salt: B256,
}
pub fn compute_escrow_id(p: &EscrowIdParams) -> B256 {
let encoded = (
p.poster,
p.worker,
p.token,
p.deposit,
p.platform_fee_amount,
p.platform,
p.arbitrator,
p.salt,
)
.abi_encode();
keccak256(encoded)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn compute_escrow_id_matches_known_vector() {
let mut salt_bytes = [0u8; 32];
salt_bytes[31] = 0x42;
let params = EscrowIdParams {
poster: "0x0000000000000000000000000000000000000001"
.parse()
.unwrap(),
worker: "0x0000000000000000000000000000000000000002"
.parse()
.unwrap(),
token: "0x0000000000000000000000000000000000000003"
.parse()
.unwrap(),
deposit: U256::from(1_000_000_000u64),
platform_fee_amount: U256::from(50_000_000u64),
platform: "0x0000000000000000000000000000000000000004"
.parse()
.unwrap(),
arbitrator: "0x0000000000000000000000000000000000000005"
.parse()
.unwrap(),
salt: B256::from(salt_bytes),
};
let expected: B256 = "0x1fada0b25ef1b0a435fd6b8ef78d4a068b13e77c95bb29635397ab0106835363"
.parse()
.expect("pinned escrow-id vector");
assert_eq!(compute_escrow_id(¶ms), expected);
}
#[test]
fn compute_escrow_id_is_salt_sensitive() {
let base = |salt: B256| EscrowIdParams {
poster: Address::ZERO,
worker: Address::ZERO,
token: Address::ZERO,
deposit: U256::from(1u64),
platform_fee_amount: U256::from(0u64),
platform: Address::ZERO,
arbitrator: Address::ZERO,
salt,
};
let mut salt_a = [0u8; 32];
salt_a[31] = 0x01;
let mut salt_b = [0u8; 32];
salt_b[31] = 0x02;
let a = compute_escrow_id(&base(B256::from(salt_a)));
let b = compute_escrow_id(&base(B256::from(salt_b)));
assert_ne!(a, b, "distinct salts must produce distinct escrow ids");
}
#[test]
fn approve_calldata_has_expected_selector() {
let call = IERC20::approveCall {
spender: Address::ZERO,
amount: U256::from(1u64),
};
let data = call.abi_encode();
assert_eq!(&data[0..4], &[0x09, 0x5e, 0xa7, 0xb3]);
}
#[test]
fn post_bond_calldata_has_expected_selector() {
let call = TaskBond::postCall {
token: Address::ZERO,
amount: U256::from(1u64),
taskRef: B256::ZERO,
salt: B256::ZERO,
};
let data = call.abi_encode();
let expected = &keccak256("post(address,uint256,bytes32,bytes32)")[..4];
assert_eq!(&data[0..4], expected);
}
#[test]
fn compute_task_ref_left_pads_uuid_into_low_16_bytes() {
let id = uuid::Uuid::parse_str("00112233-4455-6677-8899-aabbccddeeff").unwrap();
let task_ref = compute_task_ref(id);
let bytes: [u8; 32] = task_ref.into();
assert_eq!(&bytes[..16], &[0u8; 16], "high 16 bytes must be zero");
assert_eq!(&bytes[16..], id.as_bytes(), "low 16 bytes must be the UUID");
}
#[test]
fn dispute_calldata_is_selector_plus_escrow_id() {
let id = B256::from([0xab; 32]);
let data = dispute_calldata(id);
assert_eq!(&data[..4], &keccak256("dispute(bytes32)")[..4]);
assert_eq!(
hex::encode(&data),
"add98c70abababababababababababababababababababababababababababababababab"
);
}
#[test]
fn timeout_resolve_abi_selectors_and_golden_calldata() {
let id = B256::from([0xab; 32]);
let tail = "abababababababababababababababababababababababababababababababab";
for (data, sig) in [
(
timeout_resolve_calldata(id),
"timeoutResolveDispute(bytes32)",
),
(get_escrow_calldata(id), "getEscrow(bytes32)"),
(disputed_at_calldata(id), "disputedAt(bytes32)"),
] {
assert_eq!(&data[..4], &keccak256(sig)[..4], "{sig}");
assert_eq!(hex::encode(&data[4..]), tail, "{sig}");
}
assert_eq!(
dispute_timeout_calldata(),
keccak256("DISPUTE_TIMEOUT()")[..4].to_vec()
);
}
#[test]
fn decoders_read_the_uint_and_the_state_word() {
let mut word = [0u8; 32];
word[31] = 7;
assert_eq!(decode_u256(&word).unwrap(), U256::from(7u64));
assert!(decode_u256(&word[..31]).is_err());
let mut escrow = [0u8; 256];
escrow[255] = ESCROW_STATE_DISPUTED;
assert_eq!(decode_escrow_state(&escrow).unwrap(), 5);
assert!(decode_escrow_state(&escrow[..224]).is_err());
}
#[test]
fn open_calldata_has_expected_selector() {
let call = TaskEscrow::openCall {
token: Address::ZERO,
deposit: U256::from(1u64),
worker: Address::ZERO,
platformFeeAmount: U256::from(0u64),
platform: Address::ZERO,
arbitrator: Address::ZERO,
salt: B256::ZERO,
};
let data = call.abi_encode();
let expected =
&keccak256("open(address,uint256,address,uint256,address,address,bytes32)")[..4];
assert_eq!(&data[0..4], expected);
}
}