# MagpieCCTPBridge

The MagpieCCTPBridge contract is designed for cross-chain token transfers and swaps, integrating various external libraries and interfaces. It inherits from Ownable2Step and Pausable for managing ownership and pausing functionalities. The contract uses the LibAsset library for asset operations and interacts with external interfaces like IMagpieCCTPBridge, IAxelarGateway, IAxelarGasService, and IReceiver. Key features include managing internal callers and deposits, updating critical addresses (WETH, token messenger, message transmitter, gateway, gas receiver), and performing token swaps with Magpie or user signatures. It ensures secure operations through signature verification and handles inbound and outbound swaps, bridging assets, and executing multiple function calls in a single transaction. The contract also supports incoming Ether transfers.

<table data-full-width="false"><thead><tr><th width="235">Function Name</th><th>Description (Business Logic)</th></tr></thead><tbody><tr><td><mark style="color:red;"><strong>verifySignature</strong></mark> </td><td>The <mark style="color:red;"><strong>verifySignature</strong></mark> function is a private function that verifies the signature for a swap operation. It takes <mark style="color:red;"><strong>SwapData</strong></mark> and a boolean flag <mark style="color:red;"><strong>useCaller</strong></mark> as parameters. The function constructs a message based on the swap details, including whether an affiliate is involved, and uses assembly to efficiently handle the message data. It then calls <mark style="color:red;"><strong>LibRouter.verifySignature</strong></mark> to validate the signature, ensuring the integrity and authenticity of the swap operation.</td></tr><tr><td><mark style="color:red;"><strong>swapIn</strong></mark> </td><td>The <mark style="color:red;"><strong>swapIn</strong></mark> function is a private function that executes an inbound swap operation. It increments the <mark style="color:red;"><strong>swapSequence</strong></mark> to ensure each swap has a unique sequence number and retrieves the current network ID and router address using assembly. The function verifies the signature of the swap operation through the <mark style="color:red;"><strong>verifySignature</strong></mark> function, ensuring the authenticity of the transaction. If the swap data includes a permit, the function calls <mark style="color:red;"><strong>LibRouter.permit</strong></mark> to handle the permit. It then transfers any associated fees using <mark style="color:red;"><strong>LibRouter.transferFees</strong></mark>. The function prepares the deposit data by filling an encoded data structure and computing its hash. The actual swap is performed by calling <mark style="color:red;"><strong>LibBridge.swapIn</strong></mark>, which returns the amount received from the swap. Finally, the function calls <mark style="color:red;"><strong>bridgeIn</strong></mark> to handle the bridging of assets and checks for <mark style="color:red;"><strong>reentrancy</strong></mark> by comparing the current swap sequence with the initial value. If they do not match, it reverts the transaction with a <mark style="color:red;"><strong>ReentrancyError</strong></mark>. An event <mark style="color:red;"><strong>BridgeIn</strong></mark> is emitted with the deposit data hash and nonce.</td></tr><tr><td><mark style="color:red;"><strong>bridgeIn</strong></mark> </td><td>The <mark style="color:red;"><strong>bridgeIn</strong></mark> function handles the bridging of an inbound asset transfer into the contract. It approves the token messenger to spend the specified amount of the asset and prepares the data for the bridging operation using assembly. The function calls <mark style="color:red;"><strong>tokenMessengerAddress.execute</strong></mark> to perform the bridging operation and reverts with <mark style="color:red;"><strong>BurnFailed</strong></mark> if the operation fails. It then calls <mark style="color:red;"><strong>dataIn</strong></mark> to handle the inbound data transfer and emits a <mark style="color:red;"><strong>BridgeIn</strong></mark> event with the deposit data hash and nonce.</td></tr><tr><td><mark style="color:red;"><strong>dataIn</strong></mark> </td><td>The <strong>dataIn</strong> function executes an inbound data transfer, preparing the payload with the deposit data hash, amount, and asset address. It calls <mark style="color:red;"><strong>IAxelarGasService.payNativeGasForContractCall</strong></mark> to pay the gas fee for the contract call and <mark style="color:red;"><strong>IAxelarGateway.callContract</strong></mark> to execute the contract call on the destination chain.</td></tr><tr><td><mark style="color:red;"><strong>execute</strong></mark></td><td>The <mark style="color:red;"><strong>execute</strong></mark> function handles the execution of inbound messages from the Axelar gateway. It validates the contract call using <mark style="color:red;"><strong>IAxelarGateway.validateContractCall</strong></mark> and reverts with <mark style="color:red;"><strong>NotApprovedByGateway</strong></mark> if the validation fails. The function then calls <mark style="color:red;"><strong>addDeposit</strong></mark> to add the validated deposit.</td></tr><tr><td><mark style="color:red;"><strong>addDeposit</strong></mark> </td><td>The <mark style="color:red;"><strong>addDeposit</strong></mark> function adds a validated deposit to the contract, updating the deposit mapping with the amount and asset address from the payload. It emits a <mark style="color:red;"><strong>Deposit</strong></mark> event with the deposit data hash and amount.</td></tr><tr><td><mark style="color:red;"><strong>swapOut</strong></mark> </td><td>The <mark style="color:red;"><strong>swapOut</strong></mark> function handles outbound swap operations, restricted to internal callers. It retrieves the network ID and router address using assembly and prepares the message and attestation data for the swap. The function calls <mark style="color:red;"><strong>IReceiver.receiveMessage</strong></mark> to process the message and reverts with <mark style="color:red;"><strong>MintFailed</strong></mark> if the operation fails. It then retrieves the deposit amount from the deposit mapping and performs the swap using <mark style="color:red;"><strong>LibBridge.swapOut</strong></mark>, returning the amount received from the swap.</td></tr><tr><td><mark style="color:red;"><strong>multicall</strong></mark> </td><td>The <mark style="color:red;"><strong>multicall</strong></mark> function allows the contract owner to execute multiple function calls in a single transaction. It iterates over the provided data array and calls each function using <mark style="color:red;"><strong>Address.functionDelegateCall</strong></mark>, returning the results of each call.</td></tr><tr><td><mark style="color:red;"><strong>receive</strong></mark> </td><td><mark style="color:red;"><strong>receive</strong></mark> function handles incoming Ether transfers.</td></tr></tbody></table>


---

# 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/flycn/developers/fly.trade-contracts/magpiecctpbridge.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.
