From fbefb3aced46827a0aa918a3e7fe005138a28e43 Mon Sep 17 00:00:00 2001 From: kii Date: Wed, 26 Oct 2022 09:39:31 -0700 Subject: [PATCH] tob-worm-4-panics: using options rather than unwrap & tob-worm-9: cosmwasm test build (#1672) * tob-worm-4-panics: using options rather than unwrap * removed mock dependencies argument * added test to make file * use ok or else * fmt * ok * errors resolved --- cosmwasm/Makefile | 1 + .../contracts/cw20-wrapped/src/contract.rs | 8 ++--- .../contracts/token-bridge/src/contract.rs | 31 +++++++++++-------- solana/migration/src/api/add_liquidity.rs | 4 +-- solana/migration/src/api/migrate_tokens.rs | 4 +-- solana/migration/src/api/remove_liquidity.rs | 4 +-- .../program/src/api/complete_transfer.rs | 19 ++++++++---- solana/solitaire/program/src/error.rs | 3 ++ 8 files changed, 45 insertions(+), 29 deletions(-) diff --git a/cosmwasm/Makefile b/cosmwasm/Makefile index 48004937e..a08a1d949 100644 --- a/cosmwasm/Makefile +++ b/cosmwasm/Makefile @@ -59,6 +59,7 @@ test/node_modules: test/package-lock.json unit-test: cargo test -p wormhole-bridge-terra-2 cargo test -p token-bridge-terra-2 + cargo test -p cw20-wrapped-2 .PHONY: test ## Run unit and integration tests diff --git a/cosmwasm/contracts/cw20-wrapped/src/contract.rs b/cosmwasm/contracts/cw20-wrapped/src/contract.rs index d743f4aca..00c4fefcb 100644 --- a/cosmwasm/contracts/cw20-wrapped/src/contract.rs +++ b/cosmwasm/contracts/cw20-wrapped/src/contract.rs @@ -315,7 +315,7 @@ mod tests { #[test] fn can_mint_by_minter() { - let mut deps = mock_dependencies(&[]); + let mut deps = mock_dependencies(); let minter = HumanAddr::from("minter"); let recipient = HumanAddr::from("recipient"); let amount = Uint128::new(222_222_222); @@ -324,7 +324,7 @@ mod tests { #[test] fn others_cannot_mint() { - let mut deps = mock_dependencies(&[]); + let mut deps = mock_dependencies(); let minter = HumanAddr::from("minter"); let recipient = HumanAddr::from("recipient"); do_init(deps.as_mut(), &minter); @@ -347,7 +347,7 @@ mod tests { #[test] fn transfer_balance_success() { - let mut deps = mock_dependencies(&[]); + let mut deps = mock_dependencies(); let minter = HumanAddr::from("minter"); let owner = HumanAddr::from("owner"); let amount_initial = Uint128::new(222_222_222); @@ -371,7 +371,7 @@ mod tests { #[test] fn transfer_balance_not_enough() { - let mut deps = mock_dependencies(&[]); + let mut deps = mock_dependencies(); let minter = HumanAddr::from("minter"); let owner = HumanAddr::from("owner"); let amount_initial = Uint128::new(222_221); diff --git a/cosmwasm/contracts/token-bridge/src/contract.rs b/cosmwasm/contracts/token-bridge/src/contract.rs index 1bd8acdd1..0d161b998 100644 --- a/cosmwasm/contracts/token-bridge/src/contract.rs +++ b/cosmwasm/contracts/token-bridge/src/contract.rs @@ -922,7 +922,9 @@ fn handle_complete_transfer_token( let (not_supported_amount, mut amount) = transfer_info.amount; let (not_supported_fee, mut fee) = transfer_info.fee; - amount = amount.checked_sub(fee).unwrap(); + amount = amount + .checked_sub(fee) + .ok_or(StdError::generic_err("Insufficient funds"))?; // Check high 128 bit of amount value to be empty if not_supported_amount != 0 || not_supported_fee != 0 { @@ -1068,7 +1070,9 @@ fn handle_complete_transfer_token_native( let (not_supported_amount, mut amount) = transfer_info.amount; let (not_supported_fee, mut fee) = transfer_info.fee; - amount = amount.checked_sub(fee).unwrap(); + amount = amount + .checked_sub(fee) + .ok_or(StdError::generic_err("Insufficient funds"))?; // Check high 128 bit of amount value to be empty if not_supported_amount != 0 || not_supported_fee != 0 { @@ -1263,18 +1267,19 @@ fn handle_initiate_transfer_token( let multiplier = 10u128.pow((max(decimals, 8u8) - 8u8) as u32); // chop off dust - amount = Uint128::new( - amount - .u128() - .checked_sub(amount.u128().checked_rem(multiplier).unwrap()) - .unwrap(), - ); + amount = amount + .u128() + .checked_rem(multiplier) + .and_then(|rem| amount.u128().checked_sub(rem)) + .map(Uint128::new) + .ok_or(StdError::generic_err("Insufficient funds"))?; - fee = Uint128::new( - fee.u128() - .checked_sub(fee.u128().checked_rem(multiplier).unwrap()) - .unwrap(), - ); + fee = fee + .u128() + .checked_rem(multiplier) + .and_then(|rem| fee.u128().checked_sub(rem)) + .map(Uint128::new) + .ok_or(StdError::generic_err("Invalid fee"))?; // This is a regular asset, transfer its balance submessages.push(SubMsg::reply_on_success( diff --git a/solana/migration/src/api/add_liquidity.rs b/solana/migration/src/api/add_liquidity.rs index 514d8f16c..45149d204 100644 --- a/solana/migration/src/api/add_liquidity.rs +++ b/solana/migration/src/api/add_liquidity.rs @@ -97,11 +97,11 @@ pub fn add_liquidity( let share_amount = if accs.from_mint.decimals > accs.to_mint.decimals { data.amount .checked_mul(10u64.pow((accs.from_mint.decimals - accs.to_mint.decimals) as u32)) - .unwrap() + .ok_or(SolitaireError::InsufficientFunds)? } else { data.amount .checked_div(10u64.pow((accs.to_mint.decimals - accs.from_mint.decimals) as u32)) - .unwrap() + .ok_or(SolitaireError::InsufficientFunds)? }; // Mint LP shares diff --git a/solana/migration/src/api/migrate_tokens.rs b/solana/migration/src/api/migrate_tokens.rs index 0a697a2f3..9d7e7bc37 100644 --- a/solana/migration/src/api/migrate_tokens.rs +++ b/solana/migration/src/api/migrate_tokens.rs @@ -99,11 +99,11 @@ pub fn migrate_tokens( let out_amount = if accs.from_mint.decimals > accs.to_mint.decimals { data.amount .checked_div(10u64.pow((accs.from_mint.decimals - accs.to_mint.decimals) as u32)) - .unwrap() + .ok_or(SolitaireError::InsufficientFunds)? } else { data.amount .checked_mul(10u64.pow((accs.to_mint.decimals - accs.from_mint.decimals) as u32)) - .unwrap() + .ok_or(SolitaireError::InsufficientFunds)? }; // Transfer out-tokens to user diff --git a/solana/migration/src/api/remove_liquidity.rs b/solana/migration/src/api/remove_liquidity.rs index 6bc9e5f25..0647eddc2 100644 --- a/solana/migration/src/api/remove_liquidity.rs +++ b/solana/migration/src/api/remove_liquidity.rs @@ -84,11 +84,11 @@ pub fn remove_liquidity( let out_amount = if accs.from_mint.decimals > accs.to_mint.decimals { data.amount .checked_div(10u64.pow((accs.from_mint.decimals - accs.to_mint.decimals) as u32)) - .unwrap() + .ok_or(SolitaireError::InsufficientFunds)? } else { data.amount .checked_mul(10u64.pow((accs.to_mint.decimals - accs.from_mint.decimals) as u32)) - .unwrap() + .ok_or(SolitaireError::InsufficientFunds)? }; // Transfer removed liquidity to LP diff --git a/solana/modules/token_bridge/program/src/api/complete_transfer.rs b/solana/modules/token_bridge/program/src/api/complete_transfer.rs index 21dff0b12..2abf4b22a 100644 --- a/solana/modules/token_bridge/program/src/api/complete_transfer.rs +++ b/solana/modules/token_bridge/program/src/api/complete_transfer.rs @@ -129,6 +129,10 @@ pub fn complete_native( fee *= 10u64.pow((accs.mint.decimals - 8) as u32); } + let token_amount = amount + .checked_sub(fee) + .ok_or(SolitaireError::InsufficientFunds)?; + // Transfer tokens let transfer_ix = spl_token::instruction::transfer( &spl_token::id(), @@ -136,7 +140,7 @@ pub fn complete_native( accs.to.info().key, accs.custody_signer.key, &[], - amount.checked_sub(fee).unwrap(), + token_amount, )?; invoke_seeded(&transfer_ix, ctx, &accs.custody_signer, None)?; @@ -238,6 +242,13 @@ pub fn complete_wrapped( claim::consume(ctx, accs.payer.key, &mut accs.claim, &accs.vaa)?; + let token_amount: u64 = accs + .vaa + .amount + .as_u64() + .checked_sub(accs.vaa.fee.as_u64()) + .ok_or(SolitaireError::InsufficientFunds)?; + // Mint tokens let mint_ix = spl_token::instruction::mint_to( &spl_token::id(), @@ -245,11 +256,7 @@ pub fn complete_wrapped( accs.to.info().key, accs.mint_authority.key, &[], - accs.vaa - .amount - .as_u64() - .checked_sub(accs.vaa.fee.as_u64()) - .unwrap(), + token_amount, )?; invoke_seeded(&mint_ix, ctx, &accs.mint_authority, None)?; diff --git a/solana/solitaire/program/src/error.rs b/solana/solitaire/program/src/error.rs index 72e5c5442..6c431a014 100644 --- a/solana/solitaire/program/src/error.rs +++ b/solana/solitaire/program/src/error.rs @@ -50,6 +50,9 @@ pub enum SolitaireError { UnknownInstruction(u8), Custom(u64), + + /// User does not have sufficient funds for the tx + InsufficientFunds, } impl From for SolitaireError {