From 2c56a916ebe23945eaa6cf0ffdfd2df93f37b9e2 Mon Sep 17 00:00:00 2001 From: Alwin Peng Date: Tue, 27 Jul 2021 16:08:30 -0400 Subject: [PATCH] fix bytes -> string, flip arg order to be less confusing Change-Id: I952b73628e56bc9449e5525bfd85e57851c20143 --- terra/contracts/token-bridge/src/contract.rs | 6 +++--- terra/contracts/wormhole/src/byte_utils.rs | 14 +++++--------- terra/contracts/wormhole/src/state.rs | 4 ++-- terra/test/integration.py | 0 4 files changed, 10 insertions(+), 14 deletions(-) create mode 100644 terra/test/integration.py diff --git a/terra/contracts/token-bridge/src/contract.rs b/terra/contracts/token-bridge/src/contract.rs index 59106b6d..d016b275 100644 --- a/terra/contracts/token-bridge/src/contract.rs +++ b/terra/contracts/token-bridge/src/contract.rs @@ -268,9 +268,7 @@ fn handle_governance_payload( data: &Vec, ) -> StdResult { let gov_packet = GovernancePacket::deserialize(&data)?; - - let module = String::from_utf8(gov_packet.module).unwrap(); - let module: String = module.chars().filter(|c| c != &'\0').collect(); + let module = get_string_from_32(&gov_packet.module)?; if module != "TokenBridge" { return Err(StdError::generic_err("this is not a valid module")); @@ -407,6 +405,8 @@ fn handle_complete_transfer( let recipient = deps.api.human_address(&target_address)?; let contract_addr = deps.api.human_address(&token_address)?; + // note -- here the amount is the amount the recipient will receive; + // amount + fee is the total sent receive_native(&mut deps.storage, &token_address, Uint128(amount + fee))?; // undo normalization to 8 decimals diff --git a/terra/contracts/wormhole/src/byte_utils.rs b/terra/contracts/wormhole/src/byte_utils.rs index b12da86e..4ac247d7 100644 --- a/terra/contracts/wormhole/src/byte_utils.rs +++ b/terra/contracts/wormhole/src/byte_utils.rs @@ -61,16 +61,12 @@ pub fn extend_string_to_32(s: &String) -> StdResult> { return Err(StdError::generic_err("string more than 32 ")); } - let mut result = vec![0; 32 - bytes.len()]; - result.extend(bytes); - Ok(result) + let result = vec![0; 32 - bytes.len()]; + Ok([bytes.to_vec(), result].concat()) } pub fn get_string_from_32(v: &Vec) -> StdResult { - let mut idx = 31usize; - while v[idx] == 0 { - idx -= 1 - } - String::from_utf8(v[..idx + 1].to_vec()) - .or_else(|_| Err(StdError::generic_err("could not parse string"))) + let s = String::from_utf8(v.clone()) + .or_else(|_| Err(StdError::generic_err("could not parse string")))?; + Ok(s.chars().filter(|c| c != &'\0').collect()) } diff --git a/terra/contracts/wormhole/src/state.rs b/terra/contracts/wormhole/src/state.rs index 65670b35..414b04b0 100644 --- a/terra/contracts/wormhole/src/state.rs +++ b/terra/contracts/wormhole/src/state.rs @@ -252,8 +252,8 @@ pub fn wrapped_asset_address_read(storage: &S) -> ReadonlyBucket, - pub chain: u16, pub action: u8, + pub chain: u16, pub payload: Vec, } @@ -267,8 +267,8 @@ impl GovernancePacket { Ok(GovernancePacket { module, - chain, action, + chain, payload, }) } diff --git a/terra/test/integration.py b/terra/test/integration.py new file mode 100644 index 00000000..e69de29b