# MagpieStargateBridge

The contract serves as a bridge between Magpie and the Stargate network, allowing deposits and withdrawals of assets between the two networks. It has a modifier `onlyMagpieAggregator` that restricts access to functions only to the Magpie aggregator address specified in the `settings` struct. It has a modifier `onlyStargate` that restricts access to functions only to the Stargate router address specified in the `settings` struct.

```solidity
struct Settings {
        address aggregatorAddress;
        address routerAddress;
    }
```

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><pre><code>aggregatorAddress
</code></pre></td><td><pre><code>address
</code></pre></td><td>Magpie Aggregator Address</td></tr><tr><td><pre><code>routerAddress
</code></pre></td><td><pre><code>address
</code></pre></td><td>Stargate Router Address</td></tr></tbody></table>

```solidity
struct WithdrawArgs {
        uint16 srcChainId;
        uint256 nonce;
        address assetAddress;
        bytes srcAddress;
        TransferKey transferKey;
    }
```

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><pre><code>srcChainId
</code></pre></td><td><pre><code>uint16
</code></pre></td><td>A <code>uint16</code> value representing the source chain ID from which the withdrawal is initiated.</td></tr><tr><td><pre><code>nonce
</code></pre></td><td><pre><code>uint256
</code></pre></td><td>A <code>uint256</code> value representing a unique identifier for the withdrawal transaction.</td></tr><tr><td><pre><code>assetAddress
</code></pre></td><td><pre><code>address
</code></pre></td><td>An <code>address</code> representing the asset address that is being withdrawn.</td></tr><tr><td><pre><code>srcAddress
</code></pre></td><td><pre><code>bytes
</code></pre></td><td>A <code>bytes</code> array representing the source address on the source chain.</td></tr><tr><td><pre><code>transferKey
</code></pre></td><td><pre><code>TransferKey
</code></pre></td><td>An instance of the <code>TransferKey</code> struct from the <code>LibTransferKey</code> library, which contains the network ID, sender address, and swap sequence associated with the withdrawal.</td></tr></tbody></table>

### updateSettings():

to update the bridge settings. Only the contract owner can call this function.

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">_settings
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">Settings
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct Settings {
        address aggregatorAddress;
        address routerAddress;
    }
</code></pre></td></tr></tbody></table>

### withdraw():

to withdraw deposited funds. It checks the deposited amount based on the transfer key and asset address, clears the cached swap if the amount is zero, and transfers the amount to the aggregator address. Only the Magpie aggregator can call this function.

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">withdrawArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">WithdrawArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct WithdrawArgs {
        uint16 srcChainId;
        uint256 nonce;
        address assetAddress;
        bytes srcAddress;
        TransferKey transferKey;
    }
</code></pre></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">amountOut
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256
</code></pre></td><td>The amount that is withdrawn.</td></tr></tbody></table>

### sgReceive():

`sgReceive`  is called by the Stargate router when assets are received from another chain. It increments the deposited amount based on the transfer key, asset address, and amount.

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">transferKey.networkId
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint16
</code></pre></td><td>Network Id extracted from the TransferKey struct</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">transferKey.senderAddress
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>Sender Address extracted from the TransferKey struct</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">transferKey.swapSequence
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256
</code></pre></td><td>Swap Sequence extracted from the TransferKey struct</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">assetAddress
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>An <code>address</code> parameter representing the address of the received asset.</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>A <code>uint256</code> parameter representing the amount of the received asset.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">payload
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>A <code>bytes calldata</code> parameter containing encoded information about the transfer, including the <code>TransferKey</code>.</td></tr></tbody></table>
