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:
parent
c8eb5c43d6
commit
fbefb3aced
|
@ -59,6 +59,7 @@ test/node_modules: test/package-lock.json
|
||||||
unit-test:
|
unit-test:
|
||||||
cargo test -p wormhole-bridge-terra-2
|
cargo test -p wormhole-bridge-terra-2
|
||||||
cargo test -p token-bridge-terra-2
|
cargo test -p token-bridge-terra-2
|
||||||
|
cargo test -p cw20-wrapped-2
|
||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
## Run unit and integration tests
|
## Run unit and integration tests
|
||||||
|
|
|
@ -315,7 +315,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn can_mint_by_minter() {
|
fn can_mint_by_minter() {
|
||||||
let mut deps = mock_dependencies(&[]);
|
let mut deps = mock_dependencies();
|
||||||
let minter = HumanAddr::from("minter");
|
let minter = HumanAddr::from("minter");
|
||||||
let recipient = HumanAddr::from("recipient");
|
let recipient = HumanAddr::from("recipient");
|
||||||
let amount = Uint128::new(222_222_222);
|
let amount = Uint128::new(222_222_222);
|
||||||
|
@ -324,7 +324,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn others_cannot_mint() {
|
fn others_cannot_mint() {
|
||||||
let mut deps = mock_dependencies(&[]);
|
let mut deps = mock_dependencies();
|
||||||
let minter = HumanAddr::from("minter");
|
let minter = HumanAddr::from("minter");
|
||||||
let recipient = HumanAddr::from("recipient");
|
let recipient = HumanAddr::from("recipient");
|
||||||
do_init(deps.as_mut(), &minter);
|
do_init(deps.as_mut(), &minter);
|
||||||
|
@ -347,7 +347,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn transfer_balance_success() {
|
fn transfer_balance_success() {
|
||||||
let mut deps = mock_dependencies(&[]);
|
let mut deps = mock_dependencies();
|
||||||
let minter = HumanAddr::from("minter");
|
let minter = HumanAddr::from("minter");
|
||||||
let owner = HumanAddr::from("owner");
|
let owner = HumanAddr::from("owner");
|
||||||
let amount_initial = Uint128::new(222_222_222);
|
let amount_initial = Uint128::new(222_222_222);
|
||||||
|
@ -371,7 +371,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn transfer_balance_not_enough() {
|
fn transfer_balance_not_enough() {
|
||||||
let mut deps = mock_dependencies(&[]);
|
let mut deps = mock_dependencies();
|
||||||
let minter = HumanAddr::from("minter");
|
let minter = HumanAddr::from("minter");
|
||||||
let owner = HumanAddr::from("owner");
|
let owner = HumanAddr::from("owner");
|
||||||
let amount_initial = Uint128::new(222_221);
|
let amount_initial = Uint128::new(222_221);
|
||||||
|
|
|
@ -922,7 +922,9 @@ fn handle_complete_transfer_token(
|
||||||
let (not_supported_amount, mut amount) = transfer_info.amount;
|
let (not_supported_amount, mut amount) = transfer_info.amount;
|
||||||
let (not_supported_fee, mut fee) = transfer_info.fee;
|
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
|
// Check high 128 bit of amount value to be empty
|
||||||
if not_supported_amount != 0 || not_supported_fee != 0 {
|
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_amount, mut amount) = transfer_info.amount;
|
||||||
let (not_supported_fee, mut fee) = transfer_info.fee;
|
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
|
// Check high 128 bit of amount value to be empty
|
||||||
if not_supported_amount != 0 || not_supported_fee != 0 {
|
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);
|
let multiplier = 10u128.pow((max(decimals, 8u8) - 8u8) as u32);
|
||||||
|
|
||||||
// chop off dust
|
// chop off dust
|
||||||
amount = Uint128::new(
|
amount = amount
|
||||||
amount
|
.u128()
|
||||||
.u128()
|
.checked_rem(multiplier)
|
||||||
.checked_sub(amount.u128().checked_rem(multiplier).unwrap())
|
.and_then(|rem| amount.u128().checked_sub(rem))
|
||||||
.unwrap(),
|
.map(Uint128::new)
|
||||||
);
|
.ok_or(StdError::generic_err("Insufficient funds"))?;
|
||||||
|
|
||||||
fee = Uint128::new(
|
fee = fee
|
||||||
fee.u128()
|
.u128()
|
||||||
.checked_sub(fee.u128().checked_rem(multiplier).unwrap())
|
.checked_rem(multiplier)
|
||||||
.unwrap(),
|
.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
|
// This is a regular asset, transfer its balance
|
||||||
submessages.push(SubMsg::reply_on_success(
|
submessages.push(SubMsg::reply_on_success(
|
||||||
|
|
|
@ -97,11 +97,11 @@ pub fn add_liquidity(
|
||||||
let share_amount = if accs.from_mint.decimals > accs.to_mint.decimals {
|
let share_amount = if accs.from_mint.decimals > accs.to_mint.decimals {
|
||||||
data.amount
|
data.amount
|
||||||
.checked_mul(10u64.pow((accs.from_mint.decimals - accs.to_mint.decimals) as u32))
|
.checked_mul(10u64.pow((accs.from_mint.decimals - accs.to_mint.decimals) as u32))
|
||||||
.unwrap()
|
.ok_or(SolitaireError::InsufficientFunds)?
|
||||||
} else {
|
} else {
|
||||||
data.amount
|
data.amount
|
||||||
.checked_div(10u64.pow((accs.to_mint.decimals - accs.from_mint.decimals) as u32))
|
.checked_div(10u64.pow((accs.to_mint.decimals - accs.from_mint.decimals) as u32))
|
||||||
.unwrap()
|
.ok_or(SolitaireError::InsufficientFunds)?
|
||||||
};
|
};
|
||||||
|
|
||||||
// Mint LP shares
|
// Mint LP shares
|
||||||
|
|
|
@ -99,11 +99,11 @@ pub fn migrate_tokens(
|
||||||
let out_amount = if accs.from_mint.decimals > accs.to_mint.decimals {
|
let out_amount = if accs.from_mint.decimals > accs.to_mint.decimals {
|
||||||
data.amount
|
data.amount
|
||||||
.checked_div(10u64.pow((accs.from_mint.decimals - accs.to_mint.decimals) as u32))
|
.checked_div(10u64.pow((accs.from_mint.decimals - accs.to_mint.decimals) as u32))
|
||||||
.unwrap()
|
.ok_or(SolitaireError::InsufficientFunds)?
|
||||||
} else {
|
} else {
|
||||||
data.amount
|
data.amount
|
||||||
.checked_mul(10u64.pow((accs.to_mint.decimals - accs.from_mint.decimals) as u32))
|
.checked_mul(10u64.pow((accs.to_mint.decimals - accs.from_mint.decimals) as u32))
|
||||||
.unwrap()
|
.ok_or(SolitaireError::InsufficientFunds)?
|
||||||
};
|
};
|
||||||
|
|
||||||
// Transfer out-tokens to user
|
// Transfer out-tokens to user
|
||||||
|
|
|
@ -84,11 +84,11 @@ pub fn remove_liquidity(
|
||||||
let out_amount = if accs.from_mint.decimals > accs.to_mint.decimals {
|
let out_amount = if accs.from_mint.decimals > accs.to_mint.decimals {
|
||||||
data.amount
|
data.amount
|
||||||
.checked_div(10u64.pow((accs.from_mint.decimals - accs.to_mint.decimals) as u32))
|
.checked_div(10u64.pow((accs.from_mint.decimals - accs.to_mint.decimals) as u32))
|
||||||
.unwrap()
|
.ok_or(SolitaireError::InsufficientFunds)?
|
||||||
} else {
|
} else {
|
||||||
data.amount
|
data.amount
|
||||||
.checked_mul(10u64.pow((accs.to_mint.decimals - accs.from_mint.decimals) as u32))
|
.checked_mul(10u64.pow((accs.to_mint.decimals - accs.from_mint.decimals) as u32))
|
||||||
.unwrap()
|
.ok_or(SolitaireError::InsufficientFunds)?
|
||||||
};
|
};
|
||||||
|
|
||||||
// Transfer removed liquidity to LP
|
// Transfer removed liquidity to LP
|
||||||
|
|
|
@ -129,6 +129,10 @@ pub fn complete_native(
|
||||||
fee *= 10u64.pow((accs.mint.decimals - 8) as u32);
|
fee *= 10u64.pow((accs.mint.decimals - 8) as u32);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let token_amount = amount
|
||||||
|
.checked_sub(fee)
|
||||||
|
.ok_or(SolitaireError::InsufficientFunds)?;
|
||||||
|
|
||||||
// Transfer tokens
|
// Transfer tokens
|
||||||
let transfer_ix = spl_token::instruction::transfer(
|
let transfer_ix = spl_token::instruction::transfer(
|
||||||
&spl_token::id(),
|
&spl_token::id(),
|
||||||
|
@ -136,7 +140,7 @@ pub fn complete_native(
|
||||||
accs.to.info().key,
|
accs.to.info().key,
|
||||||
accs.custody_signer.key,
|
accs.custody_signer.key,
|
||||||
&[],
|
&[],
|
||||||
amount.checked_sub(fee).unwrap(),
|
token_amount,
|
||||||
)?;
|
)?;
|
||||||
invoke_seeded(&transfer_ix, ctx, &accs.custody_signer, None)?;
|
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)?;
|
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
|
// Mint tokens
|
||||||
let mint_ix = spl_token::instruction::mint_to(
|
let mint_ix = spl_token::instruction::mint_to(
|
||||||
&spl_token::id(),
|
&spl_token::id(),
|
||||||
|
@ -245,11 +256,7 @@ pub fn complete_wrapped(
|
||||||
accs.to.info().key,
|
accs.to.info().key,
|
||||||
accs.mint_authority.key,
|
accs.mint_authority.key,
|
||||||
&[],
|
&[],
|
||||||
accs.vaa
|
token_amount,
|
||||||
.amount
|
|
||||||
.as_u64()
|
|
||||||
.checked_sub(accs.vaa.fee.as_u64())
|
|
||||||
.unwrap(),
|
|
||||||
)?;
|
)?;
|
||||||
invoke_seeded(&mint_ix, ctx, &accs.mint_authority, None)?;
|
invoke_seeded(&mint_ix, ctx, &accs.mint_authority, None)?;
|
||||||
|
|
||||||
|
|
|
@ -50,6 +50,9 @@ pub enum SolitaireError {
|
||||||
UnknownInstruction(u8),
|
UnknownInstruction(u8),
|
||||||
|
|
||||||
Custom(u64),
|
Custom(u64),
|
||||||
|
|
||||||
|
/// User does not have sufficient funds for the tx
|
||||||
|
InsufficientFunds,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<ProgramError> for SolitaireError {
|
impl From<ProgramError> for SolitaireError {
|
||||||
|
|
Loading…
Reference in New Issue