From 90143879f327bc98135445acab1f922f6dfa3578 Mon Sep 17 00:00:00 2001 From: Yo <56731761+yomarion@users.noreply.github.com> Date: Tue, 26 Sep 2023 15:38:42 +0200 Subject: [PATCH] chore: utils to refund and not panic (#23) --- deploy.sh | 6 +-- tests/sim/conversion_proxy.rs | 68 +++++++++++++++++++++----- tests/sim/fungible_conversion_proxy.rs | 4 +- tests/sim/fungible_proxy.rs | 56 ++++++++++----------- tests/sim/utils.rs | 56 +++++++++++---------- 5 files changed, 118 insertions(+), 72 deletions(-) diff --git a/deploy.sh b/deploy.sh index ead65ee..fd7dd5c 100755 --- a/deploy.sh +++ b/deploy.sh @@ -60,13 +60,13 @@ printf "Deploying %s on NEAR_ENV=%s with ACCOUNT_ID=%s (patch=%s)\n\n" "$contrac if [ "$contract_name" = "fungible_proxy" ]; then set -x - near deploy -f --wasmFile ./target/wasm32-unknown-unknown/release/$contract_name.wasm \ + NEAR_ENV=$NEAR_ENV near deploy -f --wasmFile ./target/wasm32-unknown-unknown/release/$contract_name.wasm \ --accountId $ACCOUNT_ID else if [ "$contract_name" = "conversion_proxy" ]; then if [ "$NEAR_ENV" = "mainnet" ]; then - feed_parser="switchboard-v2.mainnet"; + feed_parser="switchboard-v2.near"; feed_address="C3p8SSWQS8j1nx7HrzBBphX5jZcS1EY28EJ5iwjzSix2"; else feed_parser="switchboard-v2.testnet"; @@ -84,7 +84,7 @@ else --initArgs $initArgs"; fi set -x - near deploy -f --wasmFile ./target/wasm32-unknown-unknown/release/$contract_name.wasm \ + NEAR_ENV=$NEAR_ENV near deploy -f --wasmFile ./target/wasm32-unknown-unknown/release/$contract_name.wasm \ --accountId $ACCOUNT_ID \ $initParams fi diff --git a/tests/sim/conversion_proxy.rs b/tests/sim/conversion_proxy.rs index d16de79..f56e0f1 100644 --- a/tests/sim/conversion_proxy.rs +++ b/tests/sim/conversion_proxy.rs @@ -150,7 +150,7 @@ fn test_transfer_with_invalid_reference_length() { ), deposit = transfer_amount ); - assert_one_promise_error(result.clone(), "Incorrect payment reference length"); + result.assert_one_promise_error("Incorrect payment reference length"); // Check Alice balance assert_eq!( @@ -181,10 +181,7 @@ fn test_transfer_with_wrong_currency() { ), deposit = transfer_amount ); - assert_one_promise_error( - result, - "Only payments denominated in USD are implemented for now", - ); + result.assert_one_promise_error("Only payments denominated in USD are implemented for now"); } #[test] @@ -210,9 +207,7 @@ fn test_transfer_with_low_deposit() { ), deposit = transfer_amount ); - result.assert_success(); - assert_eq!(result.logs().len(), 1, "Wrong number of logs"); - assert!(result.logs()[0].contains("Deposit too small for payment")); + result.assert_success_one_log("Deposit too small for payment"); assert_eq!( alice.account().unwrap().amount, @@ -232,6 +227,59 @@ fn test_transfer_with_low_deposit() { ); } +#[test] +fn test_transfer_with_wrong_feed_address() { + let (alice, bob, builder, proxy, root) = init(); + + let result = call!(alice, proxy.get_encoded_feed_address()); + result.assert_success(); + + let result = call!( + root, + proxy.set_feed_address(&"7igqhpGQ8xPpyjQ4gMHhXRvtZcrKSGJkdKDJYBiPQgcb".to_string()) + ); + result.assert_success(); + + let initial_alice_balance = alice.account().unwrap().amount; + let initial_bob_balance = bob.account().unwrap().amount; + let initial_contract_balance = proxy.account().unwrap().amount; + let transfer_amount = to_yocto("100000"); + let payment_address = bob.account_id().try_into().unwrap(); + let fee_address = builder.account_id().try_into().unwrap(); + + let result = call!( + alice, + proxy.transfer_with_reference( + PAYMENT_REF.into(), + payment_address, + U128::from(2000000), + USD.into(), + fee_address, + U128::from(0), + U64::from(0) + ), + deposit = transfer_amount + ); + result.assert_success_one_log("ERR_FAILED_ORACLE_FETCH"); + + assert_eq!( + alice.account().unwrap().amount, + initial_alice_balance, + "Alice should not spend NEAR on a wrong feed address payment.", + ); + + assert_eq!( + proxy.account().unwrap().amount, + initial_contract_balance, + "Contract's balance should be unchanged" + ); + assert_eq!( + builder.account().unwrap().amount, + initial_bob_balance, + "Builder's balance should be unchanged" + ); +} + #[test] fn test_transfer_zero_usd() { let (alice, bob, builder, proxy, _) = init(); @@ -295,9 +343,7 @@ fn test_outdated_rate() { ), deposit = transfer_amount ); - result.assert_success(); - assert_eq!(result.logs().len(), 1, "Wrong number of logs"); - assert!(result.logs()[0].contains("Conversion rate too old")); + result.assert_success_one_log("Conversion rate too old"); assert_eq!( initial_proxy_balance, diff --git a/tests/sim/fungible_conversion_proxy.rs b/tests/sim/fungible_conversion_proxy.rs index 6d9b9a6..4fbeb57 100644 --- a/tests/sim/fungible_conversion_proxy.rs +++ b/tests/sim/fungible_conversion_proxy.rs @@ -238,7 +238,7 @@ fn test_transfer_not_enough() { ft_contract.user_account, proxy.ft_on_transfer(alice.account_id(), send_amt.0.to_string(), msg) ); - assert_one_promise_error(result, "Deposit too small") + result.assert_one_promise_error("Deposit too small"); } #[test] @@ -410,5 +410,5 @@ fn test_outdated_rate() { ft_contract.user_account, proxy.ft_on_transfer(alice.account_id(), send_amt.0.to_string(), msg) ); - assert_one_promise_error(result, "Conversion rate too old"); + result.assert_one_promise_error("Conversion rate too old"); } diff --git a/tests/sim/fungible_proxy.rs b/tests/sim/fungible_proxy.rs index 4fb1a07..28b13fb 100644 --- a/tests/sim/fungible_proxy.rs +++ b/tests/sim/fungible_proxy.rs @@ -144,18 +144,17 @@ fn test_transfer() { ft_contract.user_account, proxy.ft_on_transfer(alice.account_id(), send_amt.0.to_string(), args.into()) ); - result.assert_success(); - assert_eq!(result.logs().len(), 1, "Wrong number of logs"); - let expected_log = json!({ - "amount": "498000000", // 500 USDC.e - 2 USDC.e fee - "token_address": "mockedft", - "fee_address": "builder", - "fee_amount": "2000000", - "payment_reference": "abc7c8bb1234fd11", - "to": "bob", - }) - .to_string(); - assert_eq!(result.logs()[0], expected_log); + result.assert_success_one_log( + &json!({ + "amount": "498000000", // 500 USDC.e - 2 USDC.e fee + "token_address": "mockedft", + "fee_address": "builder", + "fee_amount": "2000000", + "payment_reference": "abc7c8bb1234fd11", + "to": "bob", + }) + .to_string(), + ); // The mocked fungible token does not handle change let change = result.unwrap_json::().parse::().unwrap(); @@ -184,7 +183,7 @@ fn transfer_less_than_fee_amount() { ft_contract.user_account, proxy.ft_on_transfer(alice.account_id(), send_amt.0.to_string(), args.into()) ); - assert_one_promise_error(result, "amount smaller than fee_amount") + result.assert_one_promise_error("amount smaller than fee_amount"); } #[test] @@ -210,9 +209,7 @@ fn test_transfer_receiver_send_failed() { ft_contract.user_account, proxy.ft_on_transfer(alice.account_id(), send_amt.0.to_string(), args.into()) ); - result.assert_success(); - assert_eq!(result.logs().len(), 1, "Wrong number of logs"); - assert_eq!(result.logs()[0], "Transfer failed to bob or builder. Returning attached amount of 500000000 of token mockedft to alice"); + result.assert_success_one_log("Transfer failed to bob or builder. Returning attached amount of 500000000 of token mockedft to alice"); // The mocked fungible token does not handle change let change = result.unwrap_json::().parse::().unwrap(); @@ -250,9 +247,7 @@ fn test_transfer_fee_receiver_send_failed() { ft_contract.user_account, proxy.ft_on_transfer(alice.account_id(), send_amt.0.to_string(), args.into()) ); - result.assert_success(); - assert_eq!(result.logs().len(), 1, "Wrong number of logs"); - assert_eq!(result.logs()[0], "Transfer failed to bob or builder. Returning attached amount of 500000000 of token mockedft to alice"); + result.assert_success_one_log("Transfer failed to bob or builder. Returning attached amount of 500000000 of token mockedft to alice"); // The mocked fungible token does not handle change let change = result.unwrap_json::().parse::().unwrap(); @@ -284,18 +279,17 @@ fn test_transfer_zero_usd() { ft_contract.user_account, proxy.ft_on_transfer(alice.account_id(), send_amt.0.to_string(), args.into()) ); - result.assert_success(); - assert_eq!(result.logs().len(), 1, "Wrong number of logs"); - let expected_log = json!({ - "amount": "0", - "token_address": "mockedft", - "fee_address": "builder", - "fee_amount": "0", - "payment_reference": "abc7c8bb1234fd12", - "to": "bob", - }) - .to_string(); - assert_eq!(result.logs()[0], expected_log); + result.assert_success_one_log( + &json!({ + "amount": "0", + "token_address": "mockedft", + "fee_address": "builder", + "fee_amount": "0", + "payment_reference": "abc7c8bb1234fd12", + "to": "bob", + }) + .to_string(), + ); assert_unchanged_balance(alice, alice_balance_before, &ft_contract, "Alice"); assert_unchanged_balance(bob, bob_balance_before, &ft_contract, "Bob"); diff --git a/tests/sim/utils.rs b/tests/sim/utils.rs index 9cf8336..ac1ce79 100644 --- a/tests/sim/utils.rs +++ b/tests/sim/utils.rs @@ -66,32 +66,38 @@ pub fn assert_received( ); } -pub fn assert_one_promise_error(promise_result: ExecutionResult, expected_error_message: &str) { - assert!( - !promise_result.is_ok(), - "Promise succeeded, expected to fail." - ); - assert_eq!( - promise_result.promise_errors().len(), - 1, - "Expected 1 error, got {}", - promise_result.promise_errors().len() - ); +pub trait ExecutionResultAssertion { + fn assert_one_promise_error(&self, expected_error: &str); + fn assert_success_one_log(&self, expected_log: &str); +} - if let ExecutionStatus::Failure(execution_error) = &promise_result - .promise_errors() - .remove(0) - .unwrap() - .outcome() - .status - { - assert!( - execution_error.to_string().contains(expected_error_message), - "Expected error containing: '{}'. Got: '{}'", - expected_error_message, - execution_error.to_string() +impl ExecutionResultAssertion for ExecutionResult { + fn assert_one_promise_error(&self, expected_error: &str) { + assert!(!self.is_ok(), "Promise succeeded, expected to fail."); + assert_eq!( + self.promise_errors().len(), + 1, + "Expected 1 error, got {}", + self.promise_errors().len() ); - } else { - unreachable!(); + + if let ExecutionStatus::Failure(execution_error) = + &self.promise_errors().remove(0).unwrap().outcome().status + { + assert!( + execution_error.to_string().contains(expected_error), + "Expected error containing: '{}'. Got: '{}'", + expected_error, + execution_error.to_string() + ); + } else { + unreachable!(); + } + } + + fn assert_success_one_log(&self, expected_log: &str) { + self.assert_success(); + assert_eq!(self.logs().len(), 1, "Wrong number of logs"); + assert!(self.logs()[0].contains(&expected_log)); } }