fix bytes -> string, flip arg order to be less confusing

Change-Id: I952b73628e56bc9449e5525bfd85e57851c20143
This commit is contained in:
Alwin Peng 2021-07-27 16:08:30 -04:00 committed by Hendrik Hofstadt
parent c8bc1b57cc
commit 2c56a916eb
4 changed files with 10 additions and 14 deletions

View File

@ -268,9 +268,7 @@ fn handle_governance_payload<S: Storage, A: Api, Q: Querier>(
data: &Vec<u8>,
) -> StdResult<HandleResponse> {
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<S: Storage, A: Api, Q: Querier>(
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

View File

@ -61,16 +61,12 @@ pub fn extend_string_to_32(s: &String) -> StdResult<Vec<u8>> {
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<u8>) -> StdResult<String> {
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())
}

View File

@ -252,8 +252,8 @@ pub fn wrapped_asset_address_read<S: Storage>(storage: &S) -> ReadonlyBucket<S,
pub struct GovernancePacket {
pub module: Vec<u8>,
pub chain: u16,
pub action: u8,
pub chain: u16,
pub payload: Vec<u8>,
}
@ -267,8 +267,8 @@ impl GovernancePacket {
Ok(GovernancePacket {
module,
chain,
action,
chain,
payload,
})
}

View File