Skip to content

Commit

Permalink
implement IpcHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptoAtwill committed Nov 29, 2024
1 parent 8e6eacc commit 9c4bfa3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
28 changes: 24 additions & 4 deletions contracts/contracts/examples/CrossMessengeCaller.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@ pragma solidity ^0.8.23;

import {FvmAddress} from "../structs/FvmAddress.sol";
import {SubnetID, IPCAddress} from "../structs/Subnet.sol";
import {IpcEnvelope, IpcMsgKind, CallMsg} from "../structs/CrossNet.sol";
import {IpcEnvelope, IpcMsgKind, CallMsg, ResultMsg} from "../structs/CrossNet.sol";
import {IGateway} from "../interfaces/IGateway.sol";
import {SubnetIDHelper} from "../lib/SubnetIDHelper.sol";
import {FvmAddressHelper} from "../lib/FvmAddressHelper.sol";
import {EMPTY_BYTES, METHOD_SEND} from "../../contracts/constants/Constants.sol";
import {EMPTY_BYTES, METHOD_SEND} from "../constants/Constants.sol";
import {IpcExchange} from "../../sdk/IpcContract.sol";

interface ISubnetGetter {
function ipcGatewayAddr() external view returns (address);
function getParent() external view returns (SubnetID memory);
}

/// This is a simple example contract to invoke cross messages between subnets from different levels
contract CrossMessengeCaller {
contract CrossMessengeCaller is IpcExchange {
event CallReceived(IPCAddress from, CallMsg msg);
event ResultReceived(IpcEnvelope original, ResultMsg result);

address public subnetActor;

constructor(address _subnetActor) {
constructor(address _subnetActor, address gateway) IpcExchange(gateway) {
subnetActor = _subnetActor;
}

Expand All @@ -29,6 +33,22 @@ contract CrossMessengeCaller {
return SubnetIDHelper.createSubnetId(parent, actor);
}

function _handleIpcCall(
IpcEnvelope memory envelope,
CallMsg memory callMsg
) internal override returns (bytes memory) {
emit CallReceived(envelope.from, callMsg);
return EMPTY_BYTES;
}

function _handleIpcResult(
IpcEnvelope storage original,
IpcEnvelope memory,
ResultMsg memory resultMsg
) internal override {
emit ResultReceived(original, resultMsg);
}

/// @dev Invoke a cross net send fund message from the current subnet to the target subnet
function invokeSendMessage(SubnetID calldata targetSubnet, address recipient, uint256 value) external {
IPCAddress memory to = IPCAddress({subnetId: targetSubnet, rawAddress: FvmAddressHelper.from(recipient)});
Expand Down
6 changes: 3 additions & 3 deletions contracts/tasks/cross-network-messenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ task('cross-network-messenger-deploy')
})

// step 2. invoke a cross network send message
// sample command: pnpm exec hardhat validator-gater-set-subnet --network calibrationnet 314159 <YOUR SUBNET ETH ROUTE>
// sample command: pnpm exec hardhat cross-network-send --network calibrationnet 314159 <YOUR SUBNET ETH ROUTE> <RECIPIENT> <VALUE>
task('cross-network-send')
.addPositionalParam('root', 'the chain id of root subnet')
.addPositionalParam('route', 'the addresses of the subnet routes, use "," to separate the addresses')
Expand All @@ -35,13 +35,13 @@ task('cross-network-send')
.setAction(async (args: TaskArguments, hre: HardhatRuntimeEnvironment) => {
await hre.run('compile')

const subnetId = { root: args.root, route: args.route.split(",") }
const subnetId = { root: args.root, route: args.route.split(',') }
console.log('sending to subnet', subnetId)

const amount = hre.ethers.utils.parseEther(args.value)
console.log('sending to address', args.recipient, 'with amount', amount)

const contracts = await Deployments.resolve(hre, 'CrossMessengeCaller')
const contract = contracts.contracts.CrossMessengeCaller
await contract.invokeSendMessage(subnetId, args.recipient, amount)
await contract.invokeSendMessage(subnetId, args.recipient, amount, { value: Number(amount) })
})
1 change: 1 addition & 0 deletions contracts/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ import './upgrade'
import './validator-gater'
import './validator-rewarder'
import './gen-selector-library'
import './cross-network-messenger'

0 comments on commit 9c4bfa3

Please sign in to comment.