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
This commit is contained in:
kii 2022-10-26 09:39:31 -07:00 committed by GitHub
parent c8eb5c43d6
commit fbefb3aced
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 45 additions and 29 deletions

View File

@ -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

View File

@ -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);

View File

@ -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(

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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)?;

View File

@ -50,6 +50,9 @@ pub enum SolitaireError {
UnknownInstruction(u8),
Custom(u64),
/// User does not have sufficient funds for the tx
InsufficientFunds,
}
impl From<ProgramError> for SolitaireError {