Skip to content

Commit

Permalink
fix: outdated rate should refund deposit (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
yomarion authored Sep 25, 2023
1 parent d413e91 commit b5e9ff6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
17 changes: 10 additions & 7 deletions conversion_proxy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,16 @@ impl ConversionProxy {
);
}
// Check rate validity
assert!(
u64::from(max_rate_timespan) == 0
|| rate.round_open_timestamp
>= env::block_timestamp() - u64::from(max_rate_timespan),
"Conversion rate too old (Last updated: {})",
rate.round_open_timestamp,
);
if u64::from(max_rate_timespan) != 0
&& rate.round_open_timestamp < env::block_timestamp() - u64::from(max_rate_timespan)
{
return self.refund_then_log(
payer,
"Conversion rate too old (Last updated: ".to_string()
+ &rate.round_open_timestamp.to_string()
+ &")",
);
}
let conversion_rate = 0_u128
.checked_add_signed(rate.result.mantissa)
.expect("The conversion rate should be positive");
Expand Down
21 changes: 18 additions & 3 deletions tests/sim/conversion_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn test_transfer() {
let expected_spent = to_yocto("12001") * 1000 / 1234;
assert!(
yocto_almost_eq(spent_amount, expected_spent),
"Alice should spend 12'000 + 1 USD worth of NEAR. \nSpent: {spent_amount}. \nExpected: {expected_spent}.",
"\nSpent: {spent_amount} \nExpected: {expected_spent} : Alice should have spent 12'000 + 1 USD worth of NEAR.",
);

assert!(bob.account().unwrap().amount > initial_bob_balance);
Expand Down Expand Up @@ -275,6 +275,8 @@ fn test_transfer_zero_usd() {
#[test]
fn test_outdated_rate() {
let (alice, bob, builder, proxy, _) = init();
let initial_alice_balance = alice.account().unwrap().amount;
let initial_proxy_balance = proxy.account().unwrap().amount;
let transfer_amount = to_yocto("100");
let payment_address = bob.account_id().try_into().unwrap();
let fee_address = builder.account_id().try_into().unwrap();
Expand All @@ -284,7 +286,7 @@ fn test_outdated_rate() {
proxy.transfer_with_reference(
PAYMENT_REF.into(),
payment_address,
U128::from(0),
U128::from(120000),
USD.into(),
fee_address,
U128::from(0),
Expand All @@ -293,5 +295,18 @@ fn test_outdated_rate() {
),
deposit = transfer_amount
);
assert_one_promise_error(result, "Conversion rate too old");
result.assert_success();
assert_eq!(result.logs().len(), 1, "Wrong number of logs");
assert!(result.logs()[0].contains("Conversion rate too old"));

assert_eq!(
initial_proxy_balance,
proxy.account().unwrap().amount,
"Contract's balance should be unchanged"
);
assert_eq!(
initial_alice_balance,
alice.account().unwrap().amount,
"Alice should not spend NEAR on an outdated rate payment.",
);
}

0 comments on commit b5e9ff6

Please sign in to comment.