# Brief Intro to the Cross-Chain Communication

The Exsat-EVM allow communication between the EVM layer and the native Vaulta layer. This doc will cover this topic.

<figure><img src="https://3042747009-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaDUBtTPZKYj40o0zqoC8%2Fuploads%2FDqKSDhLXK1afrzT6g7xl%2Fvaulta-evm-cross-chain.png?alt=media&#x26;token=111a6a30-e293-4d5e-8cce-2b25d0da1011" alt=""><figcaption><p>Cross chain communication</p></figcaption></figure>

## Basic concepts:

### Reserved Address

Each Vaulta account has a mapped reserved EVM address. The rule is using the uint64 value of the Vaulta name as the last 8 bytes of the EVM address, and then pad the rest with 0xbb. E.g. the name “eosio.evm” is mapped to “0xbbbbbbbbbbbbbbbbbbbbbbbb5530ea015b900000”

We will use ***addr\_evm(vaulta\_acct)*** to represent the reserved address of the ***vaulta\_acct***.

### Runtime Contract

The Vaulta-EVM system is run in a way that each EVM transaction is actually part of Vaulta transactions. The EVM runtime contract will process Vaulta transactions invoking EVM transactions.

We will use ***evm\_runtime*** to represent the Vaulta account of the evm runtime contract. Thus the reserved address of the evm runtime contract will be  ***addr\_evm(evm\_runtime)***

### Bridge Balance

Each Vaulta account can call “open” action of EVM runtime to open a bridge balance. This balance can be used to hold “dusts” beyond Vaulta token precision and to hold the gas for call EVM contracts. User can deposit to this balance by sending some tokens to ***evm\_runtime*** with the vaulta name of the target balance (in string) as memo.

We will call this balance the **bridge** **balance.**

## EVM to Vaulta

Sending messages from EVM to Vaulta is done by the Bridge Massage mechanism.

### Prerequisites:

The **receiver** Vaulta account ***receiver*** registered a message channel.

```cpp
bridgereg(eosio::name receiver, eosio::name handler, const eosio::asset& min_fee)
```

A ***handler*** can be assigned to process this message. It can be the same account as the ***receiver***.

The ***min\_fee*** sets a minimum bridge fee for bridge messages in this channel. Details will be covered below.

### Sending Messages

The EVM contracts should call bridgeMsgV0(string,bool,bytes) function at ***addr\_evm(evm\_runtime)***

The three parameter for this call should be:

string receiver: the receiver vaulta name in string representation.

bool force\_atomic: whether the call is atomic or not. Currently only atomic mode is supported. So this parameter should always be true.

bytes data: The message data sent to the Vaulta side.

The method is payable. Users can set a value to pay with the call.

After detected such calls, the EVM runtime contract will generate inline actons calling onbridgemsg(const bridge\_message\_t \&message) action of the registered ***handler*** for the ***receiver***

The message received by the ***handler*** is defined as:

```cpp
struct bridge_message_v0 {
        eosio::name receiver;
        bytes sender;
        eosio::time_point timestamp;
        bytes value;
        bytes data;

        EOSLIB_SERIALIZE(bridge_message_v0, (receiver)(sender)(timestamp)(value)(data));
    };
```

Where

```cpp
eosio::name receiver; // The receiver of the message. Handlers can use this to distinguish messages from different channels if a handler is linked to multiple receivers.
bytes sender; // The EVM address of the message sender.
eosio::time_point timestamp; // timestamp for the message
bytes value; // The value paid in the bridgeMsgV0 call
bytes data; // The actual message data
```

### Fees

As mentioned above, the bridgeMsgV0 method is payable. Users can set a value to pay with the call. The value paid will go into the **receiver’s** balance in evm runtime contract. If a min\_fee is set, the evm runtime will fail the call if the requirement is not met.

### Error Handling

bridgeMsgV0 will ALWAYS succeed viewed from the EVM side. No logic should rely on the result of the call.

If anything is wrong during the message processing, the handler should consider failing the whole transaction if it does not want the EVM state to change.

## Vaulta to EVM

Vaulta to EVM communication can be done by letting the Vaulta account generate EVM transactions by code.

### Prerequisites:

The sender should call “open” at evm\_runtime to open the **bridge balance** to pay for the EVM calls.

The sender should deposit some gas token to its **bridge balance**.

### Generate EVM Transactions

Someone could call this action of the ***evm\_runtime*** to generate an EVM transaction.

```cpp
typedef std::vector<char>       bytes;

void call(eosio::name from, const bytes &to, const bytes& value, const bytes &data, uint64_t gas_limit);
eosio::name from // the eos_caller should be put here. Obviously you need the authorization from the eos_caller to proceed.
 const bytes &to // destination EVM address
 const bytes& value // payment value
 const bytes &data // calldata
 uint64_t gas_limit // gas limit

```

### Perform Read-only calls

One could also call the exec action to call the view functions of EVM contracts. Currently the caller can only get the result via call\_backs. Support for synchronously calls will be available after the next major Vaulta upgrade.

```cpp
typedef std::vector<char>       bytes;

struct exec_input {
   std::optional<bytes> context;
   std::optional<bytes> from;
   bytes                to;
   bytes                data;
   std::optional<bytes> value;
};

struct exec_callback {
   name contract;
   name action;
};

// The callback action should accept this as input
struct exec_output {
   int32_t              status;
   bytes                data;
   std::optional<bytes> context;
};

void exec(const exec_input& input, const std::optional<exec_callback>& callback);

```

### Fees

Payment value and gas fee will be deducted from the **bridge** **balance** of the vault&#x61;***\_caller***
