# LibAggregator

The purpose of this library is to provide the core functionality for aggregating swap transactions and managing related operations within the Aggregator contract. The library has three structs.

### SwapInArgs

<table><thead><tr><th width="269.6666666666667">Field</th><th width="250">Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">swapArgs
</code></pre></td><td>bytes memory</td><td></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">dataTransferInProtocol
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">DataTransferInProtocol
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct DataTransferInProtocol {
    uint16 networkId;
    DataTransferType dataTransferType;
    bytes payload;
}
</code></pre></td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">transactionValidation
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">TransactionValidation
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct TransactionValidation {
    bytes32 fromAssetAddress;
    bytes32 toAssetAddress;
    bytes32 toAddress;
    uint256 amountOutMin;
    uint256 swapOutGasFee;
}
</code></pre></td></tr></tbody></table>

### SwapOutArgs

<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">swapArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes memory
</code></pre></td><td></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">dataTransferOutArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">DataTransferOutArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct DataTransferOutArgs {
    DataTransferType dataTransferType;
    bytes payload;
}
</code></pre></td></tr></tbody></table>

### SwapOutVariables

<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">fromAssetAddress
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>The address of the asset being swapped out.</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 address of the asset to which the swap is being performed.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">toAddress
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>The address to which the swapped asset will be transferred.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">transactionToAddress
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>The address to which any transaction fee associated with the swap will be sent.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">bridgeAmount
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256
</code></pre></td><td>The amount of the asset being swapped out.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">amountIn
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256
</code></pre></td><td>The amount of the asset being swapped out.</td></tr></tbody></table>

### updateWeth()

The purpose of this function is to allow the contract owner to update the address of the WETH token used in the aggregator 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">weth
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>Address of the Wrapped Ether (WETH) contract.</td></tr></tbody></table>

### updateMagpieRouterAddress()

This function is responsible for updating the `magpieRouterAddress` variable within the contract's storage, allowing changes to be made to the address associated with the Magpie Router. Only 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">magpieRouterAddress
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>The address of the magpie router.</td></tr></tbody></table>

### updateNetworkId()

The purpose of this function is to allow the contract owner to update the network ID used in the aggregator contract. Only 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">networkId
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint16
</code></pre></td><td>The network ID associated with the application.</td></tr></tbody></table>

### addMagpieAggregatorAddresses()

The purpose of this function is to allow the contract owner to add Magpie aggregator addresses for multiple network IDs in the aggregator contract.

**Input**

<table><thead><tr><th width="311.3333333333333">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>The network ID associated with the application.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">magpieAggregatorAddresses
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes32[]
</code></pre></td><td>The Magpie Diamond contract addresses for each of the networkIDs</td></tr></tbody></table>

### swapIn()

This function allows for swapping assets into the contract using a bridge-in transaction. It facilitates interoperability between different networks and allows users to transfer assets from one network to another through 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">swapInArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">SwapInArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct SwapInArgs {
    SwapArgs swapArgs;
    BridgeArgs bridgeArgs;
    DataTransferInProtocol dataTransferInProtocol;
    TransactionValidation transactionValidation;
}
</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 received after swapping</td></tr></tbody></table>

### swapOut()

This function allows for swapping out assets from the contract using a bridge-out transaction. It facilitates interoperability between different networks and allows users to transfer assets from the contract to another network through the bridge-out mechanism.

**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">swapOutArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">SwapOutArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">struct SwapOutArgs {
    SwapArgs swapArgs;
    BridgeArgs bridgeArgs;
    DataTransferOutArgs dataTransferOutArgs;
}
</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 received after swapping</td></tr></tbody></table>

### getDeposit()

The purpose of this function is to allow external callers to retrieve the deposit amount for a specific asset in the aggregator 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">assetAddress
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>Address of the asset that will be deposited</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">s.deposits[assetAddress]
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256
</code></pre></td><td>The deposited amount</td></tr></tbody></table>

### withdraw()

This function provides a way for users to withdraw their deposited assets from the contract, ensuring that only the rightful owner can withdraw their funds.

**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">assetAddress
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>The asset that will be withdrawn</td></tr></tbody></table>

### getDepositByUser()

The purpose of this function is to allow external callers to retrieve the deposit amount for a specific asset deposited by a specific user in the aggregator 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">assetAddress
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>Address of the asset that will be deposited</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">senderAddress
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>Address of the user who has deposited the asset</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">s.depositsByUser[assetAddress][senderAddress]
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256
</code></pre></td><td>Deposited amount</td></tr></tbody></table>

### isTransferKeyUsed()

The purpose of this function is to allow external callers to check if a specific transfer key has been used in the aggregator 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">networkId
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint16
</code></pre></td><td>networkId of the current chain in Magpie protocol, it is different from the actual networkId<br>ethereum: 1<br>polygon: 2<br>bsc: 3<br>avalanche: 4<br>arbitrum: 5 <br>optimism: 6</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">senderAddress
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes32
</code></pre></td><td>The address who initiated the transfer.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">coreSequence
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint64
</code></pre></td><td>The magpie sequence for the current swap.</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">s.usedTransferKeys[networkId][senderAddress][swapSequence]
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bool
</code></pre></td><td>Flag used to identify if the transfer key has been used for swapping or not.</td></tr></tbody></table>

### bridgeIn():

This function is responsible for executing a delegatecall to a specific facet in the Diamond contract, passing the `bridgeInArgs` data as the input. The specific functionality and behavior of the called facet would depend on the implementation details defined in the facet contract associated with the given selector.

**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">bridgeInArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">BridgeInArgs
</code></pre></td><td><pre><code>struct BridgeInArgs {
    uint16 recipientNetworkId;
    BridgeArgs bridgeArgs;
    uint256 amount;
    address toAssetAddress;
    TransferKey transferKey;
}
</code></pre></td></tr></tbody></table>

### bridgeOut():

his function is responsible for executing a delegatecall to a specific facet in the Diamond contract, passing the `bridgeOutArgs` data as the input. The specific functionality and behavior of the called facet would depend on the implementation details defined in the facet contract associated with the given selector. The function expects the delegatecall to return a single `uint256` value, which is then returned by the function itself.

**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">bridgeOutArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">BridgeOutArgs
</code></pre></td><td><pre><code>struct BridgeOutArgs {
    BridgeArgs bridgeArgs;
    Transaction transaction;
    TransferKey transferKey;
}
</code></pre></td></tr></tbody></table>

### dataTransferIn():

This function is responsible for executing a delegatecall to a specific facet in the Diamond contract, passing the `dataTransferInArgs` data as the input. The specific functionality and behavior of the called facet would depend on the implementation details defined in the facet contract associated with the given selector.

**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">dataTransferInArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">DataTransferInArgs
</code></pre></td><td><pre><code>struct DataTransferInArgs {
    DataTransferInProtocol protocol;
    TransferKey transferKey;
    bytes payload;
}
</code></pre></td></tr></tbody></table>

### dataTransferOut():

**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">dataTransferOutArgs
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">DataTransferOutArgs
</code></pre></td><td><pre><code>struct DataTransferOutArgs {
    DataTransferType dataTransferType;
    bytes payload;
}
</code></pre></td></tr></tbody></table>

**Output**:

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td>transferKey</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><tr><td>payload</td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>A string of bytes that contains transactional data.</td></tr></tbody></table>

### Events:

```solidity
event UpdateWeth(address indexed sender, address weth);
```

```solidity
event UpdateMagpieRouterAddress(address indexed sender, address weth);
```

```solidity
event UpdateNetworkId(address indexed sender, uint16 networkId);
```

```solidity
event AddMagpieAggregatorAddresses(
        address indexed sender,
        uint16[] networkIds,
        bytes32[] magpieAggregatorAddresses
    );
```

```solidity
event SwapIn(
        address indexed fromAddress,
        bytes32 indexed toAddress,
        address fromAssetAddress,
        address toAssetAddress,
        uint256 amountIn,
        uint256 amountOut,
        TransferKey transferKey,
        Transaction transaction
    );
```

```solidity
event SwapOut(
        address indexed fromAddress,
        address indexed toAddress,
        address fromAssetAddress,
        address toAssetAddress,
        uint256 amountIn,
        uint256 amountOut,
        TransferKey transferKey,
        Transaction transaction
    );
```

```solidity
 event Withdraw(address indexed sender, address indexed assetAddress, uint256 amount);
```


---

# 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/aggregator/libaggregator.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.
