Skip to content

Commit

Permalink
chore: utils to refund and not panic (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
yomarion authored Sep 26, 2023
1 parent b5e9ff6 commit 9014387
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 72 deletions.
6 changes: 3 additions & 3 deletions deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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
Expand Down
68 changes: 57 additions & 11 deletions tests/sim/conversion_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down Expand Up @@ -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]
Expand All @@ -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,
Expand All @@ -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();
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tests/sim/fungible_conversion_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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");
}
56 changes: 25 additions & 31 deletions tests/sim/fungible_proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>().parse::<u128>().unwrap();
Expand Down Expand Up @@ -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]
Expand All @@ -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::<String>().parse::<u128>().unwrap();
Expand Down Expand Up @@ -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::<String>().parse::<u128>().unwrap();
Expand Down Expand Up @@ -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");
Expand Down
56 changes: 31 additions & 25 deletions tests/sim/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

0 comments on commit 9014387

Please sign in to comment.