# How to integrate anyCall V6?

* anyCall contract is deployed at: [0xC10Ef9F491C9B59f936957026020C321651ac078](https://blockscan.com/address/0xC10Ef9F491C9B59f936957026020C321651ac078)
* Chains supported: BNB Chain, Polygon,Ethereum, Optimism, Gnosis Chain, Fantom, Moonriver, IoTeX, Arbitrum, Avalanche, Harmony
* Source code:  <https://github.com/anyswap/anyswap-v1-core/blob/master/contracts/AnyswapV6CallProxy.sol>
* Audit report: [PeckShield](https://github.com/anyswap/Anyswap-Audit/blob/master/PeckShield/PeckShield-Audit-Report-MultiChainV6-v1.0.pdf)

{% hint style="info" %}
You can also test anyCall V6 on testnets freely now. Follow the link below for more information.&#x20;
{% endhint %}

{% content-ref url="anycall-v6-testnet-environments" %}
[anycall-v6-testnet-environments](https://docs.multichain.org/developer-guide/anycall-v6/anycall-v6-testnet-environments)
{% endcontent-ref %}

### **anyCall V6 Workflow**

{% hint style="info" %}
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.**
{% endhint %}

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**.

&#x20;<mark style="color:green;">**DAPPS sender contract(Chain A)**</mark>**&#x20;->** `anyCall`(Chain A) **->** *SMPC Network* **->** `anyExec`(Chain B) **->`AnyCallExecutor` ->** `anyExecute` by <mark style="color:green;">**DAPP receiver contract(Chain B)**</mark>

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.&#x20;

&#x20;`anyExecute` <mark style="color:red;">**DAPP receiver contract(Chain B failed)**</mark>**&#x20;-> anyCall**(Chain B) **->** *SMPC Network* **-> anyExec**(Chain A) **->** `anyFallback` <mark style="color:green;">**DAPP sender contract(Chain A)**</mark>&#x20;

**More docs on fallback can be found below:**

{% content-ref url="anyfallback" %}
[anyfallback](https://docs.multichain.org/developer-guide/anycall-v6/anyfallback)
{% endcontent-ref %}

### AnyCallExecutor

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.

```
interface CallProxy{
 
    function executor() external view returns (address executor);
}
```

The executor should then be saved in your contract constructor

```
constructor(){
        anycallExecutor=CallProxy(anycallcontract).executor();
    }
```

### **anyCall Interfaces**

#### anyCall (Called by Dapps)

`function anyCall( address _to, bytes calldata _data, address _fallback, uint256 _toChainID, uint256 _flags )`

#### Parameters <a href="#request-parameters" id="request-parameters"></a>

| Param       | Type           | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| ----------- | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| \_to        | address        | The target contract to interact with on `_toChainID`                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| \_data      | bytes calldata | <p>The calldata supplied for the interaction with <code>\_to</code></p><p> </p><p><code>anyExecute</code> will be run with this <code>\_data</code> on the receiver contract you deployed.</p>                                                                                                                                                                                                                                                                                                                            |
| \_fallback  | address        | <p>This is the fallback contract on the <strong>SOURCE CHAIN</strong> if the destination chain contract execution failed. If you put <code>address(0)</code>, it means you don’t have a fallback contract.<br><br>Note that this fallback mechanism is done by the destination chain issuing another anyCall with the following parameters. So you need to design your fallback function to be compatible with the following parameters. <code>abi.encodeWithSelector(IApp.anyFallback.selector, \_to, \_data)</code></p> |
| \_toChainID | uint256        | The target chain id to interact with                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| \_flags     | uint256        | <p>How dapps are paying gas fee of tx execution: </p><p><strong>0</strong>: Gas fee paid on destination chain. A gas balance needs to be topped up by dapps on destination chain. </p><p><br><strong>2</strong>: Gas fee paid on source chain. Allow users to pay the gas fee. (The fee details will be explained further)</p>                                                                                                                                                                                            |

#### **anyExec (Only called by our MPC address)**

`function anyExec( address _to, bytes memory _data, address _fallback, string memory _appID, RequestContext memory _ctx )`

#### Parameters <a href="#request-parameters" id="request-parameters"></a>

| 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`  if the cross chain interaction fails                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| \_fromChainID | uint256        | The originating chain id                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
| \_ctx         | struct         | <p>A struct containing information about the source chain anyCall request.</p><p></p><p><code>struct RequestContext { bytes32 txhash; address from; uint256 fromChainID; uint256 nonce; uint256 flags; }</code><br><br>Data includes:<br>1. <code>txhash</code>: Original txhash</p><p>2. <code>from</code>: Original txhash</p><p> txhash: Original txhash</p><p>3. <code>fromChainID</code>: The source chain id</p><p>4. <code>nonce</code>: The nonce of anyCall, used to keep track of order of anyCall.</p><p>5. <code>flags</code>: The fee setting flag. Used to determine if fees need to be deducted on destination chain.<br></p> |

### Requirements to be compatible with anyCall interfaces:

1. **Sender Contract**: Your sender contract needs to call the method `anyCall` on the official anyCall contract.&#x20;
2. **Receiver Contract:** A method named `anyExecute` needs to exist on your `_to` contract address. This is needed because `anyExec` (Our deployed anyCall Contract) will call `anyExecute` on your receiver contract.

###

### C**ode Example**

{% hint style="info" %}
Refer to this example for the correct interface to implement
{% endhint %}

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>

#### &#x20;**Sender contract on** BNB chain

```solidity
pragma solidity ^0.8.10;

interface CallProxy{
    function anyCall(
        address _to,
        bytes calldata _data,
        address _fallback,
        uint256 _toChainID,
        uint256 _flags

    ) external;
}
  

contract AnycallV6senderBNBMainnet{

    // The Multichain anycall contract on bnb mainnet
    address private anycallcontractbnb=0xC10Ef9F491C9B59f936957026020C321651ac078
;


    address private owneraddress=0xfa7e030d2ac001c2bA147c0b147D468E4609f7CC;

    // Destination contract on Polygon
    address private receivercontract=0x3E2347a6F93eaC793C56DC508206e397eA11e83D;
    
    event NewMsg(string msg);

    function step1_initiateAnyCallSimple(string calldata _msg) external {
        emit NewMsg(_msg);
        if (msg.sender == owneraddress){
        CallProxy(anycallcontractbnb).anyCall(
            receivercontract,

            // sending the encoded bytes of the string msg and decode on the destination chain
            abi.encode(_msg),

            // 0x as fallback address because we don't have a fallback function
            address(0),

            // chainid of polygon
            137,

            // Using 0 flag to pay fee on destination chain
            0
            );
            
        }

    }
}
```

**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.&#x20;

An event is also defined to show what message is being sent on Polygon. This is not 100% necessary.

**3.** `step1_initiateAnyCallSimple` &#x20;

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.&#x20;
* **Uint256 \_toChainID**: The destination chain id. Polygon is 137.&#x20;
* **uint256 \_flags**: 0 in our case because we want gas fees to be paid on the destination chain.

#### **R**eceiver **contract on Polygon**

The function `anyExecute` is executed with the `_data` passed in from anyCall above.

**Compatibility Notes**:

1. It needs to be named anyExecute.
2. `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.&#x20;

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;

contract anycallV6receiverPolygon{
    event NewMsg(string msg);

    function anyExecute(bytes memory _data) external returns (bool success, bytes memory result){
        (string memory _msg) = abi.decode(_data, (string));  
        emit NewMsg(_msg);
        success=true;
        result='';

    }
    }
```

#### **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.

![](https://1909004250-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUdcg2zIVro9DItOfrezt%2Fuploads%2FNWOZ1XzKi2EfauB2IamW%2Fimage.png?alt=media\&token=02eefc65-9581-4b7f-87df-a5db471fb09a)

### **Deploy Requirements**

1. **Fees**: Before using the anyCall protocol, projects need to deposit some gas fee to the **destination** anyCall contract.&#x20;

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

{% content-ref url="fees-paid-on-source-chain" %}
[fees-paid-on-source-chain](https://docs.multichain.org/developer-guide/anycall-v6/fees-paid-on-source-chain)
{% endcontent-ref %}

{% hint style="info" %}
Whitelisting is not required anymore for anyCall V6.
{% endhint %}

###
