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:
The asset that will be checked for a native token.
Output:
Flag to identify if the asset is native or not.
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:
Asset whose balance needs to be found
Output:
Balance of the asset
getBalanceOf():
Retrieves the balance of the target address for a given asset (self).
Input:
Asset whose balance needs to be found.
The address where the balance is checked from.
Output:
Balance of the specific asset.
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:
Asset that will be transferred
address that will send the asset
address that will receive the transferred asset
amount of an asset that will be transferred
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:
Asset that will be transferred
address that will receive the transferred asset
amount of an asset that will be transferred
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:
The asset that will be approved.
Address of a contract that will use the owners asset.
Asset amount that can be spent.
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:
Address of the asset that will be deposited
Address of the Wrapped Ether (WETH) contract.
Depositing amount
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:
The asset whose allowance will be granted to the spender.
Address of the owner who owns the asset.
Address of a contract that will use the owners allowance.
Output:
The allowed amount
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:
The asset that will be withdrawn
Address of the Wrapped Ether (WETH) contract.
Address that will receive withdrawn token.
Amount that needs to be withdrawn
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:
The asset address whose decimals we are finding.
Output:
The decimals of the asset address
isSuccessful():
Determines if a call was successful.
Input:
Address of the target contract.
To check if the call to the contract was successful or not.
The data was sent while calling the target contract.
Output:
The success of the call.
execute():
Executes a low level call.
Input:
The address of the contract to which the call is being made.
The parameters or data to be sent in the call.
Output:
The success of the call.
Last updated