How to integrate anyCall V6?
anyCall contract is deployed at: 0xC10Ef9F491C9B59f936957026020C321651ac078
Chains supported: BNB Chain, Polygon,Ethereum, Optimism, Gnosis Chain, Fantom, Moonriver, IoTeX, Arbitrum, Avalanche, Harmony
Audit report: PeckShield
You can also test anyCall V6 on testnets freely now. Follow the link below for more information.
anyCall V6 Workflow
anyCall V6 added an important middleware called AnyCallExecutor. This sandbox contract will make the final execution of your destination contract. Hence any caller verification modifier should authorize AnyCallExecutor.
The anyCall protocol is made up of three main functions anyCall
, anyExec
. These two methods exist in our deployed anyCall contracts.
DAPPS need to develop and deploy a sender contract on chain A and a receiver contract on chain B. On the receiver contract, a function named anyExecute
needs to be present and it will be called.
DAPPS sender contract(Chain A) -> anyCall
(Chain A) -> SMPC Network -> anyExec
(Chain B) ->AnyCallExecutor
-> anyExecute
by DAPP receiver contract(Chain B)
DAPPS sender contract call anyCall
on the Chain A. Then SMPC network will relay anyCall event which will call AnyCallExecutor
to make the final execution of the anyExecute
function on the DAPP receiver contract. Hence a function named anyExecute
needs to be present in the DAPP receiver contract.
If the anyExec
contract execution failed on Chain B, it can call _fallback
function to send messages back to Chain A.
anyExecute
DAPP receiver contract(Chain B failed) -> anyCall(Chain B) -> SMPC Network -> anyExec(Chain A) -> anyFallback
DAPP sender contract(Chain A)
More docs on fallback can be found below:
anyFallbackAnyCallExecutor
As stated above, AnyCallExecutor
will make the final execution to your destination contract as a sandbox.
The address of this executor contract is stored in the main anycall contract. It can be accessed with an interface function like below.
The executor should then be saved in your contract constructor
anyCall Interfaces
anyCall (Called by Dapps)
function anyCall( address _to, bytes calldata _data, address _fallback, uint256 _toChainID, uint256 _flags )
Parameters
Param | Type | Description |
---|---|---|
_to | address | The target contract to interact with on |
_data | bytes calldata | The calldata supplied for the interaction with
|
_fallback | address | This is the fallback contract on the SOURCE CHAIN if the destination chain contract execution failed. If you put |
_toChainID | uint256 | The target chain id to interact with |
_flags | uint256 | How dapps are paying gas fee of tx execution: 0: Gas fee paid on destination chain. A gas balance needs to be topped up by dapps on destination chain. 2: Gas fee paid on source chain. Allow users to pay the gas fee. (The fee details will be explained further) |
anyExec (Only called by our MPC address)
function anyExec( address _to, bytes memory _data, address _fallback, string memory _appID, RequestContext memory _ctx )
Parameters
Param | Type | Description |
---|---|---|
_to | address | The target contract to interact with |
_data | bytes calldata | The calldata supplied for the interaction with target |
_fallback | address | The address to call on |
_fromChainID | uint256 | The originating chain id |
_ctx | struct | A struct containing information about the source chain anyCall request.
2. txhash: Original txhash 3. 4. 5. |
Requirements to be compatible with anyCall interfaces:
Sender Contract: Your sender contract needs to call the method
anyCall
on the official anyCall contract.Receiver Contract: A method named
anyExecute
needs to exist on your_to
contract address. This is needed becauseanyExec
(Our deployed anyCall Contract) will callanyExecute
on your receiver contract.
Code Example
Refer to this example for the correct interface to implement
This example will send a simple message from BNB chain to Polygon chain. This message is represented as a string event.
Deployed sender contract on BNB chain and receiver contract on Polygon:
Sender contract on BNB chain: https://bscscan.com/address/0xa7ce20b8254fcb2caa6c43315be470df3438a50a#code
Receiver contract on Polygon chain: https://polygonscan.com/address/0xF9D415fcDe051DE5D36D4c6faE03185759cCBe1E#code
Sender contract on BNB chain
1. Import the anyCall interface, so we can call the deployed anyCall contract.
2. Define the AnycallV6senderBNBMainnet
contract.
anycallcontractbnb
: define where the anyCall contract exists which will use the interface above to call the anyCall. This is the contract deployed by Multichain team.
owneraddress
: Simple owner address protection.
receivercontract
: The receiver contract deployed on Polygon.
An event is also defined to show what message is being sent on Polygon. This is not 100% necessary.
3. step1_initiateAnyCallSimple
This method takes a string input. Then it uses the interface and anyCall contract address defined above to call the anyCall function with the following parameters.
Address _to: This is the receiver contract address on Polygon.
Bytes calldata _data: This is the bytes data the destination
anyExecute
will take. This can be any data you want. In this case, we are using abi.encode to encode our message string. And the destination contract will process this byte data.Address _fallback: We're putting address(0) because we don't have a fallback function.
Uint256 _toChainID: The destination chain id. Polygon is 137.
uint256 _flags: 0 in our case because we want gas fees to be paid on the destination chain.
Receiver contract on Polygon
The function anyExecute
is executed with the _data
passed in from anyCall above.
Compatibility Notes:
It needs to be named anyExecute.
returns (bool success, bytes memory result)
has to be the return values to be compatible.
This function simply decodes the _data
that was previously encoded by abi.encode
back to a string and creates an event containing that message.
anyCall example workflow
Step 1 on BNB Chain:
The function step1_initiateAnyCallSimple(string calldata _msg)
is called with the msg 'hi polygon 7 with correct returns'
Txhash: https://bscscan.com/tx/0x5f4f2486153aced0f3bca0d967836624cc71525417a0e32c9a81c266425c1cae
Step 2 on Polygon Chain:
SMPC networks relay the anyCall event, then invoke anyExecute function on our Polygon receiver contract.
Txhash: https://polygonscan.com/tx/0x41dadc279612965c142c1e37262d70652a30ea44482ea9aced1afab1f0e18b35
This function emits the message from BNB chain.
Deploy Requirements
Fees: Before using the anyCall protocol, projects need to deposit some gas fee to the destination anyCall contract.
Note that the address argument of the deposit(address _account)
function should be the sender contract address on the source chain. The fee depends on the calldata size. The totalCost of one transaction = (gasUsed - gasleft()) * (tx.gasprice + _feeData.premium)
You can also pay fees on the source chain, refer to this article
Fees Paid on Source ChainWhitelisting is not required anymore for anyCall V6.
Last updated