Skip to content

Commit

Permalink
feat: call contract
Browse files Browse the repository at this point in the history
  • Loading branch information
karlem committed Oct 28, 2024
1 parent 6370fc9 commit a056fe9
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 18 deletions.
7 changes: 6 additions & 1 deletion fendermint/testing/materializer/src/docker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,12 @@ impl DockerMaterializer {
// Run the command and handle errors
let output = cmd.output().context("failed to run forge create")?;

println!("OUT: {}", String::from_utf8_lossy(&output.stdout));
if output.status.success() {
println!("OUT: {}", String::from_utf8_lossy(&output.stdout));
} else {
println!("ERR: {}", String::from_utf8_lossy(&output.stderr));
bail!("forge create failed");
}

// Parse the JSON output
let json_output: ForgeCreateOutput =
Expand Down
2 changes: 1 addition & 1 deletion fendermint/testing/materializer/src/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ where
let deployed = m
.deploy_solidity_contract(deployment_config)
.await
.with_context(|| "failed to deploy custom contract")?;
.with_context(|| "failed to deploy solidity contract")?;

self.solidity_deployments
.entry(subnet_name.clone())
Expand Down
85 changes: 73 additions & 12 deletions fendermint/testing/materializer/tests/docker_tests/layer_2_plus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use anyhow::{anyhow, bail, Context};
use ethers::contract::{ContractError, ContractRevert};
use ethers::core::types as et;
use ethers::types::transaction::response;
use ethers::types::U256;
use ethers::{
core::k256::ecdsa::SigningKey,
middleware::SignerMiddleware,
Expand All @@ -26,13 +28,16 @@ use ipc_actors_abis::gateway_messenger_facet::{
};
use ipc_actors_abis::subnet_actor_getter_facet::SubnetActorGetterFacet;

use ipc_actors_abis::cross_messenger::{self, CrossMessenger, Ipcaddress};
use ipc_actors_abis::cross_messenger::{
self, CrossMessenger, FvmAddress, Ipcaddress, SubnetID as IPCSubnetID,
};

use fvm_shared::econ::TokenAmount;

use ipc_api::address::IPCAddress;
use ipc_api::cross::{IpcEnvelope, IpcMsgKind};
use ipc_api::evm;
use ipc_api::subnet_id::SubnetID;

use crate::with_testnet;

Expand All @@ -41,6 +46,31 @@ const CHECKPOINT_PERIOD: u64 = 10;
const SLEEP_SECS: u64 = 5;
const MAX_RETRIES: u32 = 5;

use fvm_shared::address::{Address, Payload};

/// Convert the ipc SubnetID type to a vec of evm addresses. It extracts all the children addresses
/// in the subnet id and turns them as a vec of evm addresses.
pub fn subnet_id_to_evm_addresses(
subnet: &SubnetID,
) -> anyhow::Result<Vec<ethers::types::Address>> {
let children = subnet.children();
children
.iter()
.map(|addr| payload_to_evm_address(addr.payload()))
.collect::<anyhow::Result<_>>()
}

/// Util function to convert Fil address payload to evm address. Only delegated address is supported.
pub fn payload_to_evm_address(payload: &Payload) -> anyhow::Result<ethers::types::Address> {
match payload {
Payload::Delegated(delegated) => {
let slice = delegated.subaddress();
Ok(ethers::types::Address::from_slice(&slice[0..20]))
}
_ => Err(anyhow!("address provided is not delegated")),
}
}

#[serial_test::serial]
#[tokio::test]
async fn test_cross_messages() {
Expand Down Expand Up @@ -89,19 +119,50 @@ async fn test_cross_messages() {
parent_provider.clone(),
);

Ipcaddress {
subnet_id: SubnetID::new(vec![0], vec![0]),
raw_address: FvmAddress::new(vec![0]),
};
let call = parent_cross_messenger.invoke_cross_message(
Ipcaddress {
subnet_id: IPCSubnetID {
root: root_network.subnet_id.root_id(),
route: Default::default(),
},
raw_address: Default::default(),
},
Ipcaddress {
subnet_id: IPCSubnetID {
root: subnet.subnet_id.root_id(),
route: subnet_id_to_evm_addresses(&subnet.subnet_id)?,
},
raw_address: Default::default(),
},
U256::from(3),
);

let from = IPCAddress::new(&root_network.subnet_id, &charlie.fvm_addr())?;
let to = IPCAddress::new(&subnet.subnet_id, &bob.fvm_addr())?;
let response = call.call().await;

parent_cross_messenger.invoke_cross_message(
from.try_into().unwrap(),
to.try_into(),
TokenAmount::from_nano(10),
);
match response {
Ok(response) => {
println!("Response: {:?}", response);
}
Err(ContractError::Revert(revert_reason)) => {
println!("Revert reason: {:?}", revert_reason);
}
Err(e) => {
// Generic error handler for other cases
println!(
"An error occurred while sending the contract message: {:?}",
e
);
}
}

// let from = IPCAddress::new(&root_network.subnet_id, &charlie.fvm_addr())?;
// let to = IPCAddress::new(&subnet.subnet_id, &bob.fvm_addr())?;

// parent_cross_messenger.invoke_cross_message(
// from.try_into().unwrap(),
// to.try_into(),
// TokenAmount::from_nano(10),
// );

let envelope = IpcEnvelope {
kind: IpcMsgKind::Call,
Expand Down
5 changes: 3 additions & 2 deletions fendermint/testing/materializer/tests/manifests/layer3.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,11 @@ subnets:
solidity_deployments:
- name: "cross_messenger"
deployer: will
# TODO Karel - allow relative paths
foundry_root: "/Users/karlem/work/ipc/contracts"
contract:
name: "CrossMessenger"
path: "contracts/contracts/materializer/CrossMessenger.sol"
path: "contracts/materializer/CrossMessenger.sol"
constructor_args:
- ""

Expand Down Expand Up @@ -86,7 +87,7 @@ subnets:
foundry_root: "/Users/karlem/work/ipc/contracts"
contract:
name: "CrossMessenger"
path: "contracts/contracts/materializer/CrossMessenger.sol"
path: "contracts/materializer/CrossMessenger.sol"
constructor_args:
- ""

Expand Down
4 changes: 2 additions & 2 deletions ipc/api/src/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use fvm_shared::address::{Address, Payload};
use fvm_shared::clock::ChainEpoch;
use fvm_shared::econ::TokenAmount;
use ipc_actors_abis::{
cross_messenger, gateway_getter_facet, gateway_manager_facet, gateway_messenger_facet,
lib_gateway, register_subnet_facet, subnet_actor_checkpointing_facet, subnet_actor_diamond,
gateway_getter_facet, gateway_manager_facet, gateway_messenger_facet, lib_gateway,
register_subnet_facet, subnet_actor_checkpointing_facet, subnet_actor_diamond,
subnet_actor_getter_facet, top_down_finality_facet, xnet_messaging_facet,
};

Expand Down

0 comments on commit a056fe9

Please sign in to comment.