use serde_json::{Value, json};
use super::*;
use crate::{
error::VerificationKind,
testing::{self, OTHER, OWNER, at, edited, fixtures},
};
fn plan_check(plan: &Value, terms: &PlanTerms) -> Result<(), VerificationError> {
let plan: FundingPlan = fixtures::model(plan);
check_funding_plan(Some(&plan), terms, EscrowEnvironment::Production, fixtures::now())
.map(|_| ())
}
fn requests(kinds: &[&str]) -> Value {
kinds.iter().map(|kind| json!({"kind": kind, "signer": OWNER, "chain_id": 999})).collect()
}
#[test]
fn a_funding_plan_lists_the_permit_first_then_the_withdrawal_and_the_payment() {
let terms = fixtures::plan_terms();
assert!(plan_check(&fixtures::funding_plan(), &terms).is_ok());
let cases: Vec<Value> = vec![
edited(fixtures::funding_plan(), |p| {
p["requests"] = requests(&["margin_move", "permit", "pay_take"]);
}),
edited(fixtures::funding_plan(), |p| p["payment_setup"] = json!("done")),
edited(fixtures::funding_plan(), |p| p["payment_setup"] = json!("pending")),
edited(fixtures::funding_plan(), |p| {
p["requests"] = requests(&["margin_move", "pay_take"]);
}),
edited(fixtures::funding_plan(), |p| {
p["deposit"] = json!("0");
p["withdrawal_amount"] = json!("0");
p["withdrawal_to"] = Value::Null;
p["requests"] = requests(&["permit", "pay_take"]);
}),
edited(fixtures::funding_plan(), |p| {
p["requests"]
.as_array_mut()
.unwrap()
.push(json!({"kind": "move", "signer": OWNER, "chain_id": 999}));
}),
edited(fixtures::funding_plan(), |p| {
p["requests"] = requests(&["permit", "margin_move", "pay_take", "pay_take"]);
}),
edited(fixtures::funding_plan(), |p| {
p["requests"] = requests(&["permit", "transfer_out", "pay_take"]);
}),
edited(fixtures::funding_plan(), |p| p["requests"][2]["signer"] = json!(OTHER)),
edited(fixtures::funding_plan(), |p| p["requests"][2]["chain_id"] = json!(137)),
];
for (index, plan) in cases.iter().enumerate() {
assert_eq!(
plan_check(plan, &terms).unwrap_err().detail,
details::FUNDING_REQUESTS,
"case {index}"
);
}
let done = edited(fixtures::funding_plan(), |p| {
p["payment_setup"] = json!("done");
p["requests"] = requests(&["margin_move", "pay_take"]);
});
assert!(plan_check(&done, &terms).is_ok());
let gmx = edited(done, |p| {
p["venue"] = json!("gmx");
p["requests"] = json!([
{"kind": "transfer_out", "signer": OWNER, "chain_id": 42161},
{"kind": "pay_take", "signer": OWNER, "chain_id": 42161},
]);
});
assert!(plan_check(&gmx, &PlanTerms { from: VenueId::Gmx, ..terms }).is_ok());
}
#[test]
fn a_funding_plan_is_the_transfers_fresh_and_withdraws_only_to_the_signer() {
let terms = fixtures::plan_terms();
let cases = [
(
edited(fixtures::funding_plan(), |p| p["venue"] = json!("polymarket")),
details::FUNDING_PLAN,
),
(edited(fixtures::funding_plan(), |p| p["owner"] = json!(OTHER)), details::FUNDING_PLAN),
(
edited(fixtures::funding_plan(), |p| p["amount"] = json!("10000001")),
details::FUNDING_PLAN,
),
(
edited(fixtures::funding_plan(), |p| p["deposit"] = json!("10000001")),
details::FUNDING_PLAN,
),
(
edited(fixtures::funding_plan(), |p| p["withdrawal_amount"] = json!("10000001")),
details::FUNDING_PLAN,
),
(
edited(fixtures::funding_plan(), |p| p["withdrawal_to"] = json!("escrow")),
details::FUNDING_WITHDRAWAL,
),
(
edited(fixtures::funding_plan(), |p| p["withdrawal_to"] = Value::Null),
details::FUNDING_WITHDRAWAL,
),
(
edited(fixtures::funding_plan(), |p| {
p["observed_at"] = json!(testing::time(-120));
}),
details::FUNDING_OBSERVED_AT,
),
(
edited(fixtures::funding_plan(), |p| p["observed_at"] = json!(testing::time(6))),
details::FUNDING_OBSERVED_AT,
),
];
for (index, (plan, detail)) in cases.iter().enumerate() {
assert_eq!(plan_check(plan, &terms).unwrap_err().detail, *detail, "case {index}");
}
assert!(
plan_check(
&edited(fixtures::funding_plan(), |p| p["observed_at"] = json!(testing::time(5))),
&terms
)
.is_ok()
);
let wallet = PlanTerms { wallet_funding: true, ..terms.clone() };
assert_eq!(
plan_check(&fixtures::funding_plan(), &wallet).unwrap_err().detail,
details::FUNDING_WITHDRAWAL
);
let from_wallet = edited(fixtures::funding_plan(), |p| {
p["withdrawal_amount"] = json!("0");
p["withdrawal_to"] = Value::Null;
p["requests"] = requests(&["permit", "pay_take"]);
});
assert!(plan_check(&from_wallet, &wallet).is_ok());
let missing = check_funding_plan(None, &terms, EscrowEnvironment::Production, fixtures::now());
assert_eq!(missing.unwrap_err().kind, VerificationKind::Changed);
let unknown = PlanTerms { from: "dydx".parse().unwrap(), ..terms };
let plan: FundingPlan = fixtures::model(&fixtures::funding_plan());
let error =
check_funding_plan(Some(&plan), &unknown, EscrowEnvironment::Production, fixtures::now());
assert_eq!(error.unwrap_err().kind, VerificationKind::Unsupported);
}
fn funds_check(funds: &Value, now: i64) -> Result<U256, VerificationError> {
let funds: WalletFunding = fixtures::model(funds);
check_wallet_funding(
&funds,
&VenueId::Hyperliquid,
OWNER,
EscrowEnvironment::Production,
at(now),
)
}
#[test]
fn wallet_reads_bind_the_payer_token_deployment_and_fresh_observation() {
assert_eq!(funds_check(&fixtures::wallet_funds(), 0).unwrap(), U256::from(5_000_000u64));
let cases = [
("venue", json!("polymarket"), details::WALLET_FUNDS),
("chain_id", json!(137), details::WALLET_FUNDS),
("escrow", json!("0x948e65c40442d935d4ee990bdbd01bdd825c4873"), details::WALLET_FUNDS),
("token", json!(OTHER), details::WALLET_FUNDS),
("decimals", json!(18), details::WALLET_FUNDS),
("address", json!(OTHER), details::WALLET_FUNDS),
("spendable_wallet", json!("5000001"), details::WALLET_FUNDS),
("spendable_available", json!("1000001"), details::WALLET_FUNDS),
("observed_at", json!(testing::time(-130)), details::WALLET_FUNDS_OBSERVED_AT),
("observed_at", json!(testing::time(16)), details::WALLET_FUNDS_OBSERVED_AT),
];
for (field, value, detail) in cases {
let funds = edited(fixtures::wallet_funds(), |f| f[field] = value.clone());
assert_eq!(funds_check(&funds, 0).unwrap_err().detail, detail, "{field}");
}
let permission =
edited(fixtures::wallet_funds(), |f| f["payment_setup"]["address"] = json!(OTHER));
assert_eq!(funds_check(&permission, 0).unwrap_err().kind, VerificationKind::Changed);
assert!(funds_check(&fixtures::wallet_funds(), 109).is_ok());
assert!(funds_check(&fixtures::wallet_funds(), 110).is_err());
let max = U256::MAX.to_string();
let huge = edited(fixtures::wallet_funds(), |f| {
for field in ["wallet", "available", "spendable_wallet", "spendable_available"] {
f[field] = json!(max);
}
});
assert_eq!(funds_check(&huge, 0).unwrap_err().detail, details::WALLET_FUNDS);
}
#[test]
fn wallet_funding_fails_closed_without_the_pinned_capability() {
let venues = fixtures::venues();
let check = |venues: &[Venue], venue: VenueId| {
check_wallet_capability(venues, &venue, EscrowEnvironment::Production)
};
assert!(check(&venues, VenueId::Hyperliquid).is_ok());
assert_eq!(check(&venues, VenueId::Lighter).unwrap_err().detail, details::WALLET_FUNDING);
let edit = |edit: fn(&mut Value)| {
fixtures::venues_with(|list| {
edit(list.iter_mut().find(|row| row["id"] == "hyperliquid").unwrap());
})
};
let missing = edit(|row| row["chain"]["wallet_funding"] = Value::Null);
assert_eq!(check(&missing, VenueId::Hyperliquid).unwrap_err().detail, details::WALLET_FUNDING);
let token = edit(|row| row["chain"]["wallet_funding"]["token"] = json!(OTHER));
assert_eq!(check(&token, VenueId::Hyperliquid).unwrap_err().detail, details::WALLET_FUNDING);
let decimals = edit(|row| row["chain"]["wallet_funding"]["decimals"] = json!(18));
assert_eq!(check(&decimals, VenueId::Hyperliquid).unwrap_err().detail, details::WALLET_FUNDING);
let disabled = edit(|row| row["enabled"] = json!(false));
assert_eq!(check(&disabled, VenueId::Hyperliquid).unwrap_err().detail, details::VENUE);
let development = fixtures::venues_in(EscrowEnvironment::Development);
assert_eq!(check(&development, VenueId::Hyperliquid).unwrap_err().detail, details::ESCROW);
}