Skip to content

Commit

Permalink
cosmwasm: added rust tests to wormchain-ibc-receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
kakucodes committed Sep 17, 2024
1 parent 91ec4d1 commit cab790c
Show file tree
Hide file tree
Showing 7 changed files with 392 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cosmwasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions cosmwasm/contracts/wormchain-ibc-receiver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ thiserror = "1.0.31"
wormhole-bindings = "0.1.0"
wormhole-sdk = { workspace = true, features = ["schemars"] }
serde_wormhole.workspace = true

[dev-dependencies]
cw-multi-test = "0.13.2"
serde-json-wasm = "0.4"
wormhole-bindings = { version = "0.1.0", features=["fake"] }
serde = { version = "1.0.137", default-features = false, features = ["derive"] }
2 changes: 1 addition & 1 deletion cosmwasm/contracts/wormchain-ibc-receiver/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn execute(
}
}

fn submit_vaas(
pub fn submit_vaas(
mut deps: DepsMut<WormholeQuery>,
info: MessageInfo,
vaas: Vec<Binary>,
Expand Down
3 changes: 3 additions & 0 deletions cosmwasm/contracts/wormchain-ibc-receiver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ pub mod error;
pub mod ibc;
pub mod msg;
pub mod state;

#[cfg(test)]
pub mod tests;
2 changes: 2 additions & 0 deletions cosmwasm/contracts/wormchain-ibc-receiver/src/tests/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod test_utils;
pub mod tests;
66 changes: 66 additions & 0 deletions cosmwasm/contracts/wormchain-ibc-receiver/src/tests/test_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use cosmwasm_std::{Binary, Uint256};
use serde::Serialize;
use wormhole_bindings::fake::WormholeKeeper;
use wormhole_sdk::{
ibc_receiver::{Action, GovernancePacket},
token::Message,
vaa::{Body, Header, Vaa},
Address, Amount, Chain, GOVERNANCE_EMITTER,
};

pub fn create_transfer_vaa_body(i: usize) -> Body<Message> {
Body {
timestamp: i as u32,
nonce: i as u32,
emitter_chain: (i as u16).into(),
emitter_address: Address([(i as u8); 32]),
sequence: i as u64,
consistency_level: 32,
payload: Message::Transfer {
amount: Amount(Uint256::from(i as u128).to_be_bytes()),
token_address: Address([(i + 1) as u8; 32]),
token_chain: (i as u16).into(),
recipient: Address([i as u8; 32]),
recipient_chain: ((i + 2) as u16).into(),
fee: Amount([0u8; 32]),
},
}
}

pub fn create_gov_vaa_body(
i: usize,
chain_id: Chain,
channel_id: [u8; 64],
) -> Body<GovernancePacket> {
Body {
timestamp: i as u32,
nonce: i as u32,
emitter_chain: Chain::Solana,
emitter_address: GOVERNANCE_EMITTER,
sequence: i as u64,
consistency_level: 0,
payload: GovernancePacket {
chain: Chain::Wormchain,
action: Action::UpdateChannelChain {
channel_id,
chain_id,
},
},
}
}

pub fn sign_vaa_body<P: Serialize>(wh: WormholeKeeper, body: Body<P>) -> (Vaa<P>, Binary) {
let data = serde_wormhole::to_vec(&body).unwrap();
let signatures = WormholeKeeper::new().sign(&data);

let header = Header {
version: 1,
guardian_set_index: wh.guardian_set_index(),
signatures,
};

let v = (header, body).into();
let data = serde_wormhole::to_vec(&v).map(From::from).unwrap();

(v, data)
}
Loading

0 comments on commit cab790c

Please sign in to comment.