Skip to content

Commit

Permalink
feat: message sending + event parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
EvolveArt committed May 16, 2024
1 parent 5767e3c commit a16c57d
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 10 deletions.
24 changes: 24 additions & 0 deletions rust/Cargo.lock

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

13 changes: 10 additions & 3 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,22 @@ crate-type = ["cdylib", "rlib"]
starknet = "0.10.0"

[dev_dependencies]
starknet = "0.10.0"

# starknet
cainome = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.2.9", features = [
"abigen-rs",
] }
starknet = "0.10.0"

# eth
ethers = "2.0.7"

# testing
futures = "0.3.30"
rstest = "0.18.2"
serde = { version = "1.0.162", default-features = false, features = ["derive"] }
serde_json = "1.0.96"
futures = "0.3.30"
tokio = { version = "1", features = ["full"] }

# utilities
thiserror = { version = "1.0.37" }
Expand All @@ -52,7 +60,6 @@ eyre = { version = "0.6.8" }
# crypto
k256 = { version = "0.13.1", default-features = false, features = ["ecdsa"] }

rstest = "0.18.2"

[build-dependencies]
ethers = "2.0.7"
Expand Down
7 changes: 2 additions & 5 deletions rust/tests/constants.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
pub const DOMAIN_EVM: u32 = 1;

pub const DOMAIN_OSMO: u32 = 2;
pub const PREFIX_OSMO: &str = "osmo";

pub const DOMAIN_NTRN: u32 = 3;
pub const PREFIX_NTRN: &str = "neutron";
pub const DOMAIN_STRK: u32 = 2;
pub const PREFIX_STRK: &str = "strk";
1 change: 1 addition & 0 deletions rust/tests/contracts/strk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod setup;
mod types;
mod utils;

pub use bind::*;
pub use deploy::*;
pub use setup::{setup_env, Env};
pub use types::StarknetAccount;
Expand Down
2 changes: 1 addition & 1 deletion rust/tests/contracts/strk/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl Env {
}
}

pub async fn setup_env(hrp: &str, domain: u32, validators: &[TestValidators]) -> eyre::Result<Env> {
pub async fn setup_env(domain: u32, validators: &[TestValidators]) -> eyre::Result<Env> {
let owner = get_dev_account(0);
let deployer = get_dev_account(1);
let tester = get_dev_account(2);
Expand Down
2 changes: 1 addition & 1 deletion rust/tests/contracts/strk/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use starknet::{

use super::{types::Codes, StarknetAccount};

const BUILD_PATH_PREFIX: &str = "../target/dev/hyperlane_starknet_";
const BUILD_PATH_PREFIX: &str = "../contracts/target/dev/hyperlane_starknet_";

const KATANA_RPC_URL: &str = "http://localhost:5050";

Expand Down
14 changes: 14 additions & 0 deletions rust/tests/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::strk::mailbox::Dispatch as DispatchEvent;
use starknet::core::{types::Event, utils::get_selector_from_name};

pub fn parse_dispatch_from_res(events: &[Event]) -> DispatchEvent {
let key = get_selector_from_name("Dispatch").unwrap(); // safe to unwrap
let found = events.iter().find(|v| v.keys.contains(&key)).unwrap();

DispatchEvent {
sender: cainome::cairo_serde::ContractAddress(found.data[0]),
destination_domain: found.data[1].try_into().unwrap(),
recipient_address: cainome::cairo_serde::ContractAddress(found.data[2]),
message: (found.data[3], found.data[4]).try_into().unwrap(),
}
}
85 changes: 85 additions & 0 deletions rust/tests/mailbox.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,88 @@
use contracts::strk::mailbox::{mailbox, Bytes};
use ethers::{
prelude::parse_log, providers::Middleware, signers::Signer, types::TransactionReceipt,
};
use event::parse_dispatch_from_res;
use starknet::{
accounts::{Account, ConnectedAccount},
core::types::{FieldElement, MaybePendingTransactionReceipt},
providers::{AnyProvider, Provider},
};

use crate::{
constants::{DOMAIN_EVM, DOMAIN_STRK},
contracts::{eth, strk},
validator::TestValidators,
};

mod constants;
mod contracts;
mod event;
mod validator;

async fn send_msg_strk_to_evm<M, S>(
from: &strk::Env,
to: &eth::Env<M, S>,
) -> eyre::Result<TransactionReceipt>
where
M: Middleware + 'static,
S: Signer + 'static,
{
let mut receiver = [0u8; 32];
receiver[12..].copy_from_slice(&to.core.msg_receiver.address().0);
let _sender = from.acc_tester.address();
let msg_body = b"hello world";

// dispatch
let mailbox_contract = mailbox::new(from.core.mailbox, &from.acc_tester);
let dispatch_res = mailbox_contract
.dispatch(
&DOMAIN_EVM,
&cainome::cairo_serde::ContractAddress(FieldElement::from_bytes_be(&receiver).unwrap()),
&Bytes {
size: msg_body.len() as u32,
data: msg_body.iter().map(|b| *b as u128).collect(),
},
&None,
&None,
)
.send()
.await?;
let strk_provider: &AnyProvider = from.acc_owner.provider();
let dispatch_receipt = strk_provider
.get_transaction_receipt(dispatch_res.transaction_hash)
.await?;

let dispatch = match dispatch_receipt {
MaybePendingTransactionReceipt::PendingReceipt(_) => {
return Err(eyre::eyre!("Transaction is pending"))
}
MaybePendingTransactionReceipt::Receipt(receipt) => match receipt {
starknet::core::types::TransactionReceipt::Invoke(receipt) => {
parse_dispatch_from_res(&receipt.events)
}
_ => return Err(eyre::eyre!("Unexpected receipt type, check the hash")),
},
};

let process_tx = to.core.mailbox.process(
vec![].into(),
dispatch.message.to_bytes_be().as_slice().to_vec().into(),
);
let process_tx_res = process_tx.send().await?.await?.unwrap();

Ok(process_tx_res)
}

#[tokio::test]
async fn test_mailbox_cw_to_evm() -> eyre::Result<()> {
// init starknet env
let strk = strk::setup_env(DOMAIN_STRK, &[TestValidators::new(DOMAIN_EVM, 5, 3)]).await?;

// init eth env
let anvil = eth::setup_env(DOMAIN_EVM).await?;

let _ = send_msg_strk_to_evm(&strk, &anvil).await?;

Ok(())
}

0 comments on commit a16c57d

Please sign in to comment.