# LibAsset

The `LibAsset` library provides a convenient interface for interacting with different types of assets in Solidity, whether they are native assets or ERC20 tokens.

### isNative():

This function checks if the given address (`self`) represents a native asset (Ether). It returns `true` if the address is the native asset ID (0x0).

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">self
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>The asset that will be checked for a native token.</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">self == NATIVE_ASSETID
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bool
</code></pre></td><td>Flag to identify if the asset is native or not.</td></tr></tbody></table>

### getBalance():

This function retrieves the balance of the current contract for a given asset (`self`). If the asset is a native asset, it returns the Ether balance of the contract. Otherwise, it uses the ERC20 `balanceOf` function to fetch the token balance.

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">self
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>Asset whose balance needs to be found</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">self.isNative() ? address(this).balance : IERC20(self).balanceOf(address(this))
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256
</code></pre></td><td>Balance of the asset</td></tr></tbody></table>

### getBalanceOf():

Retrieves the balance of the target address for a given asset (self).

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">self
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>Asset whose balance needs to be found.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">targetAddress
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>The address where the balance is checked from.</td></tr></tbody></table>

Output:

<table><thead><tr><th>Field</th><th>Type</th><th>Description</th></tr></thead><tbody><tr><td><p></p><p></p></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256
</code></pre></td><td>Balance of the specific asset.</td></tr></tbody></table>

### transferFrom():

This function performs a `safeTransferFrom` operation for a given asset (`self`) from one address (`from`) to another address (`to`). It uses the `SafeERC20` library to ensure safe token transfers.

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">self
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>Asset that will be transferred</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">from
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>address that will send the  asset</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">to
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>address that will receive the transferred 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>amount of an asset that will be transferred</td></tr></tbody></table>

### transfer():

This function performs a transfer of a given amount of an asset (`self`) to a recipient address (`recipient`). If the asset is a native asset, it uses `Address.sendValue` to send Ether. Otherwise, it uses the ERC20 `safeTransfer` 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">self
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>Asset that will be transferred</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">recipient
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>address that will receive the transferred 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>amount of an asset that will be transferred</td></tr></tbody></table>

### approve():

This function approves a spender address (`spender`) to spend a specified amount of an asset (`self`). It uses the `SafeERC20` library's `forceApprove` 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">self
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>The asset that will be approved.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">spender
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>Address of a contract that will use the owners 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>Asset amount that can be spent.</td></tr></tbody></table>

### deposit():

This function allows for the deposit of a specified amount of an asset (`self`). If the asset is a native asset, it checks if the received Ether amount is sufficient and then converts it to the wrapped Ether token (`weth`) using the `IWETH` interface's `deposit` function. Otherwise, it performs a `safeTransferFrom` operation to transfer the asset from the sender address to the current 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">self
</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">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><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>Depositing amount</td></tr></tbody></table>

### getAllowance():

This function retrieves the allowance amount that a spender address (`spender`) is approved to spend from an owner address (`owner`) for a given asset (`self`). It uses the ERC20 `allowance` 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">self
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>The asset whose allowance will be granted to the spender.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">owner
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>Address of the owner who owns the asset.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">spender
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>Address of a contract that will use the owners allowance.</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">IERC20(self).allowance(owner, spender)
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint256
</code></pre></td><td>The allowed amount</td></tr></tbody></table>

### Withdraw():

This function allows for the withdrawal of a specified amount of an asset (`self`) to a designated address (`to`). If the asset is a native asset, it uses the `IWETH` interface's `withdraw` function to convert the wrapped Ether token back to Ether. Then, it performs a transfer of the native asset to the specified address. If the asset is an ERC20 token, it performs a transfer of the token to the specified 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">self
</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><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><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">to
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>Address that will receive withdrawn token.</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>Amount that needs to be withdrawn</td></tr></tbody></table>

### getDecimals():

This function retrieves the decimal precision of an ERC20 token. If the asset is a native asset, it defaults to 18 decimal places. Otherwise, it uses the `decimals` function of the ERC20 token to fetch the decimal precision.

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">self
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>The asset address whose decimals we are finding.</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">tokenDecimals
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">uint8
</code></pre></td><td>The decimals of the asset address</td></tr></tbody></table>

### isSuccessful():

Determines if a call was successful.

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">target
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>Address of the target contract.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">success
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bool
</code></pre></td><td>To check if the call to the contract was successful or not.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">data
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>The data was sent while calling the target contract.</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">result
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bool
</code></pre></td><td>The success of the call.</td></tr></tbody></table>

### execute():

Executes a low level call.

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">self
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">address
</code></pre></td><td>The address of the contract to which the call is being made.</td></tr><tr><td><p></p><pre class="language-solidity"><code class="lang-solidity">params
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bytes
</code></pre></td><td>The parameters or data to be sent in the call.</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">result
</code></pre></td><td><p></p><pre class="language-solidity"><code class="lang-solidity">bool
</code></pre></td><td>The success of the call.</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/developers/deprecated-magpie-contracts/magpieaggregator-diamond-proxy/libraries/libasset.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.
