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.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:
Field | Type | Description |
---|---|---|
self | address | The asset that will be checked for a native token. |
Output:
Field | Type | Description |
---|---|---|
self == NATIVE_ASSETID | bool | Flag to identify if the asset is native or not. |
This function retrieves the balance of a specific target address (
target
) for a given asset (self
). If the asset is a native asset, it returns the Ether balance of the target address. Otherwise, it uses the ERC20 balanceOf
function to fetch the token balance.Input:
Field | Type | Description |
---|---|---|
self | address | Asset whose balance needs to be found |
target | address | Address where the asset resides. |
Output:
Field | Type | Description |
---|---|---|
self.isNative() ? target.balance : IERC20(self).balanceOf(target) | uint256 | Balance of the asset |
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:
Field | Type | Description |
---|---|---|
self | address | Asset whose balance needs to be found |
Output:
Field | Type | Description |
---|---|---|
self.isNative() ? address(this).balance : IERC20(self).balanceOf(address(this)) | uint256 | Balance of the asset |
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:
Field | Type | Description |
---|---|---|
self | address | Asset that will be transferred |
from | address | address that will send the asset |
to | address | address that will receive the transferred asset |
amount | uint256 | amount of an asset that will be transferred |
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:
Field | Type | Description |
---|---|---|
self | address | Asset that will be transferred |
recipient | address | address that will receive the transferred asset |
amount | uint256 | amount of an asset that will be transferred |
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:
Field | Type | Description |
---|---|---|
self | address | The asset that will be approved. |
spender | address | Address of a contract that will use the owners asset. |
amount | uint256 | Asset amount that can be spent. |
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:
Field | Type | Description |
---|---|---|
self | address | Address of the asset that will be deposited |
weth | address | Address of the Wrapped Ether (WETH) contract. |
amount | uint256 | Depositing amount |
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:
Field | Type | Description |
---|---|---|
self | address | The asset whose allowance will be granted to the spender. |
owner | address | Address of the owner who owns the asset. |
spender | address | Address of a contract that will use the owners allowance. |
Output:
Field | Type | Description |
---|---|---|
IERC20(self).allowance(owner, spender) | uint256 | The allowed amount |
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:
Field | Type | Description |
---|---|---|
self | address | The asset that will be withdrawn |
weth | address | Address of the Wrapped Ether (WETH) contract. |
to | address | Address that will receive withdrawn token. |
amount | uint256 | Amount that needs to be withdrawn |
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:
Field | Type | Description |
---|---|---|
self | address | The asset address whose decimals we are finding. |
Output:
Field | Type | Text |
---|---|---|
tokenDecimals | uint8 | The decimals of the asset address |
Last modified 26d ago