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:

Output:

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:

Output:

getBalanceOf():

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

Input:

Output:

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:

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:

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:

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:

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:

Output:

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:

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:

Output:

isSuccessful():

Determines if a call was successful.

Input:

Output:

execute():

Executes a low level call.

Input:

Output:

Last updated