# LibStargate

This library is implementing a bridge for cross-chain communication using the Stargate protocol.&#x20;

The code defines three structs:

### StargateBridgeInData

contains the chain ID of the recipient on the other chain, the IDs of the source and destination pools, and the amount of fees to be paid.

```solidity
struct StargateBridgeInData {
    uint16 layerZeroRecipientChainId;
    uint256 sourcePoolId;
    uint256 destPoolId;
    uint256 fee;
    uint256 gasLimit;
}
```

<table><thead><tr><th width="292">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">layerZeroRecipientChainId
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint16
</code></pre></td><td>This field is an unsigned 16-bit integer that represents the chain ID of the recipient on Layer 0. Layer 0 typically refers to the root blockchain or the main chain.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">sourcePoolId
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256
</code></pre></td><td>This field is a <code>uint256</code> value that represents the ID or identifier of the source pool. The exact meaning and context of the pool ID may depend on the specific implementation or protocol using this struct.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">destPoolId
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256
</code></pre></td><td>This field is a <code>uint256</code> value that represents the ID or identifier of the destination pool. Similarly to the source pool ID, the meaning and context of the destination pool ID depend on the specific implementation or protocol.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">fee
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256
</code></pre></td><td>This field is a <code>uint256</code> value that represents the fee associated with the bridge operation.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">gasLimit
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256
</code></pre></td><td>The amount of gas fees one needs to execute the transaction.</td></tr></tbody></table>

### StargateBridgeOutData

contains the address of the sender's bridge contract, a nonce, and the ID of the sender's chain.

```solidity
struct StargateBridgeOutData {
    bytes srcAddress;
    uint256 nonce;
    uint16 srcChainId;
}
```

<table><thead><tr><th width="216.99999999999997">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">srcAddress
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>This field is of type <code>bytes</code> and represents the source address.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">nonce
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256
</code></pre></td><td>This field is of type <code>uint256</code> and represents a nonce value. Nonce is commonly used as a security measure to prevent replay attacks or ensure transaction ordering.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">srcChainId
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint16
</code></pre></td><td>This field is of type <code>uint16</code> and represents the source chain ID. It identifies the chain from which the bridge-out operation originates.</td></tr></tbody></table>

### ExecuteBridgeInArgs

this struct is used to encapsulate the necessary input arguments for executing a bridge operation in the context of a Stargate bridge system. The fields hold relevant information such as network ID, token details, router address, recipient address, and additional bridge-specific data

```solidity
struct ExecuteBridgeInArgs {
    address routerAddress;
    uint256 amount;
    bytes recipientAddress;
    TransferKey transferKey;
    StargateBridgeInData bridgeInData;
    IStargateRouter.lzTxObj lzTxObj;
}
```

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><pre><code>routerAddress
</code></pre></td><td><pre><code>address
</code></pre></td><td>This field is of type <code>address</code> and represents the address of the router</td></tr><tr><td><pre><code>amount
</code></pre></td><td><pre><code>uint256
</code></pre></td><td>This field is of type <code>uint256</code> and represents the amount being bridged in. It specifies the quantity of tokens or assets involved in the bridge-in operation.</td></tr><tr><td><pre><code>recipientAddress
</code></pre></td><td><pre><code>bytes
</code></pre></td><td>This field is of type <code>bytes</code> and represents the recipient address. The encoding and interpretation of the recipient address may depend on the specific requirements or format of the bridge system or protocol.</td></tr><tr><td><pre><code>transferKey
</code></pre></td><td><pre><code>TransferKey
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct TransferKey {
    uint16 networkId;
    bytes32 senderAddress;
    uint64 swapSequence;
}
</code></pre></td></tr><tr><td><pre><code>bridgeInData
</code></pre></td><td><pre><code>StargateBridgeInData
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct StargateBridgeInData {
    uint16 layerZeroRecipientChainId;
    uint256 sourcePoolId;
    uint256 destPoolId;
    uint256 fee;
}
</code></pre></td></tr><tr><td><pre><code>lzTxObj
</code></pre></td><td><pre><code>IStargateRouter.lzTxObj
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct lzTxObj {
        uint256 dstGasForCall;
        uint256 dstNativeAmount;
        bytes dstNativeAddr;
    }
</code></pre></td></tr></tbody></table>

LibStargate has the below functions:

### updateSettings

updates the stargateSettings struct in the contract's AppStorage struct.

**Input**

<table><thead><tr><th width="216.99999999999997">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">stargateSettings
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">StargateSettings
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct StargateSettings {
    address routerAddress;
}
</code></pre></td></tr></tbody></table>

### decodeBridgeOutPayload

decodes the bridgeOutPayload and returns the StargateBridgeOutData.

**Input**

<table><thead><tr><th width="216.99999999999997">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">bridgeOutPayload
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>Payload consisting of data necessary for bridging out</td></tr></tbody></table>

**Output**

<table><thead><tr><th width="216.99999999999997">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">bridgeOutData
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">StargateBridgeOutData
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct StargateBridgeOutData {
    bytes srcAddress;
    uint256 nonce;
    uint16 srcChainId;
}
</code></pre></td></tr></tbody></table>

### decodeBridgeInPayload

decodes the bridgeInPayload and returns the StargateBridgeInData.

**Input**

<table><thead><tr><th width="216.99999999999997">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">bridgeInPayload
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>Payload consisting of data necessary for bridging in.</td></tr></tbody></table>

**Output**

<table><thead><tr><th width="216.99999999999997">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">bridgeInData
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity"><strong>StargateBridgeInData
</strong></code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct StargateBridgeInData {
    uint16 layerZeroRecipientChainId;
    uint256 sourcePoolId;
    uint256 destPoolId;
    uint256 fee;
}
</code></pre></td></tr></tbody></table>

### getMinAmountLD

returns the minimum amount of tokens that can be bridged.

**Input**

<table><thead><tr><th width="216.99999999999997">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">amount
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256
</code></pre></td><td>The amount that needs to be swapped</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">bridgeInData
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">StargateBridgeInData
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct StargateBridgeInData {
    uint16 layerZeroRecipientChainId;
    uint256 sourcePoolId;
    uint256 destPoolId;
    uint256 fee;
}
</code></pre></td></tr></tbody></table>

**Output**

<table><thead><tr><th width="216.99999999999997">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">swapObj.amount
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256
</code></pre></td><td>The minimum amount out</td></tr></tbody></table>

### encodeRecipientAddress

this function is responsible for converting a `bytes32` Ethereum address into a 20-byte `bytes` representation, suitable for further processing or storage within the contract.

**Input**

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">recipientAddress
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes32
</code></pre></td><td>The recipient address in bytes32</td></tr></tbody></table>

**Output**

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">encodedRecipientAddress
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>The receipient address in bytes</td></tr></tbody></table>

### getLzTxObj

this function is responsible for creating and returning a layerZero transaction object (`lzTxObj`) based on the provided sender address.

**Input**

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">sender
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>The address that initiated the transaction.</td></tr></tbody></table>

**Output**

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">lzTxObj
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">IStargateRouter.lzTxObj
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct lzTxObj {
        uint256 dstGasForCall;
        uint256 dstNativeAmount;
        bytes dstNativeAddr;
    }
</code></pre></td></tr></tbody></table>

### bridgeIn

this function facilitates the bridging of tokens from the caller's network to a recipient network. It sets the necessary parameters, including the recipient address, bridge input data, lazy transaction object, token sequence, amount, network ID, and router address, and then calls the `executeBridgeIn` function to execute the bridge operation.

**Input**

<table><thead><tr><th width="216.99999999999997">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">recipientNetworkId
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint16
</code></pre></td><td>The network identifier of the destination chain</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">bridgeArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">BridgeArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct BridgeArgs {
    BridgeType bridgeType;
    bytes payload;
}

</code></pre></td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">amount </code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256 </code></pre></td><td>The amount that needs to be swapped</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">toAssetAddress </code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address </code></pre></td><td>The final token that needs to be received</td></tr></tbody></table>

**Output**

<table><thead><tr><th width="216.99999999999997">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">tokenSequence
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint64
</code></pre></td><td>The sequence received after a successful bridging in</td></tr></tbody></table>

### executeBridgeIn

this function is responsible for executing the bridge-in operation by calling the `swap` function of the StargateRouter contract with the necessary arguments. It handles the transaction fee, source and destination pool IDs, sender address, amount, minimum acceptable amount, lazy transaction object, recipient address, and payload.

**Input**

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">executeBridgeInArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">ExecuteBridgeInArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct ExecuteBridgeInArgs {
    address routerAddress;
    uint256 amount;
    bytes recipientAddress;
    TransferKey transferKey;
    StargateBridgeInData bridgeInData;
    IStargateRouter.lzTxObj lzTxObj;
}
</code></pre></td></tr></tbody></table>

### bridgeOut

this function is responsible for executing the bridge out operation. It decodes the payload, retrieves the sender address, and calls the `withdraw` function of the `IMagpieStargateBridge` contract to withdraw tokens based on the provided arguments. The deposited amount for the `fromAssetAddress` is reduced accordingly.

**Input**

<table><thead><tr><th width="216.99999999999997">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">bridgeOutPayload
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>Payload consisting of data necessary for briding out.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">transaction
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">Transaction
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct Transaction {
    DataTransferType dataTransferType;
    BridgeType bridgeType;
    uint16 recipientNetworkId;
    bytes32 fromAssetAddress;
    bytes32 toAssetAddress;
    bytes32 toAddress;
    bytes32 recipientAggregatorAddress;
    uint256 amountOutMin;
    uint256 swapOutGasFee;
}
</code></pre></td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">transferKey
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">TransferKey
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct TransferKey {
    uint16 networkId;
    bytes32 senderAddress;
    uint64 swapSequence;
}
</code></pre></td></tr></tbody></table>

**Output**

<table><thead><tr><th width="216.99999999999997">Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">amount
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256
</code></pre></td><td>The amount received after bridging out.</td></tr></tbody></table>

### addStargateBridgeAddresses():

It updates the Magpie Stargate bridge addresses for specific network IDs within the application storage.

**Input**

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">networkIds
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint16[]
</code></pre></td><td>An array of unsigned 16-bit integers (<code>uint16</code>) representing network identifiers.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">magpieStargateBridgeAddresses
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes32[]
</code></pre></td><td><p></p><p>An array of <code>bytes32</code> values representing Magpie Stargate Bridge addresses.<br></p></td></tr></tbody></table>

### addstargateBridgeV2Addresses():

It updates the Magpie Stargate bridge addresses for specific network IDs within the application storage.

**Input**

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">networkIds
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint16[]
</code></pre></td><td>An array of unsigned 16-bit integers (<code>uint16</code>) representing network identifiers.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">magpieStargateBridgeAddresses
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes32[]
</code></pre></td><td>An array of <code>bytes32</code> values representing Magpie Stargate Bridge addresses.</td></tr></tbody></table>

### Events:

```solidity
event UpdateStargateSettings(address indexed sender, StargateSettings stargateSettings);
```

```solidity
event AddMagpieStargateBridgeAddresses(
        address indexed sender,
        uint16[] networkIds,
        bytes32[] magpieStargateBridgeAddresses
    );
```

```solidity
event AddMagpieStargateBridgeV2Addresses(
        address indexed sender,
        uint16[] networkIds,
        bytes32[] magpieStargateBridgeAddresses
    );
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.fly.trade/developers/deprecated-magpie-contracts/magpieaggregator-diamond-proxy/bridge/libstargate.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
