# GlacisRouter

**Your gateway to cross-chain activity.**

The GlacisRouter smart contract acts as the on-chain interface for all cross-chain activity with Glacis.

You can discover the source smart contract [`GlacisRouter.sol`](https://github.com/glacislabs/v1-core/blob/main/contracts/routers/GlacisRouter.sol) in this repository.

## The Route Function

The main function in GlacisRouter is route, which sends a cross-chain message. It returns a `bytes32` messageId and a `uint256` nonce:

```solidity
route(
        uint256 chainId,
        bytes32 to,
        bytes memory payload,
        address[] memory adapters,
        GlacisRouter.CrossChainGas[] memory fees,
        address refundAddress,
        bool retryable)
payable
returns(bytes32 messageId, uint256 nonce)
```

<table><thead><tr><th width="276">Parameter Name</th><th>Parameter Description</th></tr></thead><tbody><tr><td><strong>uint256</strong> chainId</td><td>The Glacis ID of the blockchain that you wish to send a message to</td></tr><tr><td><strong>address</strong> to</td><td>The address of the destination contract that you are sending a message to</td></tr><tr><td><strong>bytes</strong> <strong>memory</strong> payload</td><td>The payload of data that you wish to send to the destination contract</td></tr><tr><td><strong>address[]</strong> <strong>memory</strong> adapters</td><td>An array of adapters to send the message over, such as an <a href="https://www.axelar.network/">Axelar</a> adapter. Can be an address if you wish to use an adapter that Glacis does not use, otherwise it can be a <a href="../supported-gmps">GMP ID</a> stored as an address</td></tr><tr><td><strong>GlacisRouter.CrossChainGas[] memory</strong> fees</td><td>An array of fees that correspond to the amount provided to each adapter. This is a parallel array to the adapters parameter. The sum of the "nativeCurrencyValue" must be equal to the value sent with this function</td></tr><tr><td><strong>address</strong> refundAddress</td><td>The address to which excess native currency is sent to if an adapter deems that the user overpaid</td></tr><tr><td><strong>bool</strong> retriable</td><td>Whether to allow the message to be retried (stores owner of the message ID on-chain, slightly more gas expensive)</td></tr></tbody></table>

### CrossChainGas

To represent how adapters require gas, we have created the CrossChainGas struct, located within the [GlacisCommons](https://github.com/glacislabs/v1-core/blob/main/contracts/commons/GlacisCommons.sol) contract.&#x20;

```solidity
struct CrossChainGas {
        uint128 gasLimit;
        uint128 nativeCurrencyValue;
}
```

| Parameter Name                  | Parameter Description                                                                                 |
| ------------------------------- | ----------------------------------------------------------------------------------------------------- |
| **uint128** gasLimit            | A desired destination chain gas limit. Required by some adapters, and ignored by others               |
| **uint128** nativeCurrencyValue | The amount of source chain value to be forwarded to an adapter. Different GMPs require different fees |

## The Retry Route

There is an additional route function variant:

```solidity
routeRetry(
	uint256 chainId,
	address to,
        bytes memory payload,
        address[] memory adapters,
        GlacisRouter.CrossChainGas[] memory fees,
        address refundAddress,
        bytes32 messageId,
        uint256 nonce)
payable
returns(bytes32, uint256)
```

This route cannot be used for original cross-chain messages. Instead, it is used to retry a cross-chain message with an identical message ID and identical data (but potentially through a different GMP).  For example, an original message was sent through Axelar but is now being retried via Wormhole.

This can be helpful in cases where a message is somehow lost by a GMP's consensus mechanism or its relayers. An identical message would be sent, and whichever message arrives after the first will revert.

Two additional parameters are required with this function:

| Parameter Name    | Parameter Description                                                                |
| ----------------- | ------------------------------------------------------------------------------------ |
| bytes32 messageId | The Glacis message ID of the original message                                        |
| uint256 nonce     | The nonce of the message, which can be retrieved in the original message's event log |

{% hint style="info" %}
Only the original message sender can retry a message, no other address can do it on their behalf. The original message must have been sent with its "retryable" parameter as true. Identical data **must** be provided.
{% endhint %}

## Read-Only

These are the read-only functions in GlacisRouter, but most developers won't use them:

| Function Signature                                                                                     | Function Description                                                                                                                                                                                                                  |
| ------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| glacisAdapters(uint8) returns(address)                                                                 | Returns the address of the adapter contract for the specific GMP ID input                                                                                                                                                             |
| sourceAdapterToGMP(address) returns(uint8)                                                             | Returns the GMP ID of the adapter at the specified address. Returns 0 if no such adapter exists                                                                                                                                       |
| validateGlacisMessageId(bytes32 messageId, uint256 toChainId, address to, uint256 nonce) returns(bool) | Returns true if the combination of chain ID, address, msg.sender, and message nonce result in the same messageId **You can use the JSON-RPC method eth\_call to simulate any msg.sender if you wish to use this in your application** |
| messageSenders(bytes32)                                                                                | Returns the message sender of a specific message ID                                                                                                                                                                                   |

## Errors

It also includes the following errors, some of which you may encounter:

| Error Name                                       | Error Description                                                                                                                                |
| ------------------------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| GlacisRouter\_\_GMPNotSupported                  | Occurs when routing, and the GMP ID provided is not supported on this chain                                                                      |
| GlacisRouter\_\_RouteDoesNotExist                | Occurs when routing, and the adapter of the GMP ID does not exist                                                                                |
| GlacisRouter\_\_NotOwnerOfMessageToRetry         | Occurs when retrying a message route, when the message ID that is attempting to be retried was not originally sent by the current message sender |
| GlacisRouter\_\_OnlyAdaptersAllowed              | Occurs when trying to call a function that only a Glacis GMP Adapter can call                                                                    |
| GlacisRouter\_\_MessageInputNotIdenticalForRetry | Occurs when retrying a message route, when the message input was not identical to the original                                                   |
| GlacisRouter\_\_ClientDeniedRoute                | Occurs when a message is received, and the client contract does not accept messages from the sender address, origin chain, and/or GMP            |
| GlacisRouter\_\_ImpossibleGMPId(uint8 gmpId)     | Occurs when a message is received, and the GMP ID is not supported                                                                               |
| GlacisRouter\_\_MessageAlreadyReceivedFromGMP    | Occurs when a message is received, and the same message has already been received from that GMP                                                  |
| GlacisRouter\_\_MessageIdNotValid                | Occurs when a message is received, and the Glacis message ID contained in the header does not pass verification                                  |
| \`\`\`                                           |                                                                                                                                                  |
