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:

FieldTypeDescription

self

address

The asset that will be checked for a native token.

Output:

FieldTypeDescription

self == NATIVE_ASSETID

bool

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:

FieldTypeDescription

self

address

Asset whose balance needs to be found

Output:

FieldTypeDescription

self.isNative() ? address(this).balance : IERC20(self).balanceOf(address(this))

uint256

Balance of the asset

getBalanceOf():

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

Input:

FieldTypeDescription

self

address

Asset whose balance needs to be found.

targetAddress

address

The address where the balance is checked from.

Output:

FieldTypeDescription

uint256

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:

FieldTypeDescription

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

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:

FieldTypeDescription

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

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:

FieldTypeDescription

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.

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:

FieldTypeDescription

self

address

Address of the asset that will be deposited

weth

address

Address of the Wrapped Ether (WETH) contract.

amount

uint256

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:

FieldTypeDescription

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:

FieldTypeDescription

IERC20(self).allowance(owner, spender)

uint256

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:

FieldTypeDescription

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

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:

FieldTypeDescription

self

address

The asset address whose decimals we are finding.

Output:

FieldTypeDescription

tokenDecimals

uint8

The decimals of the asset address

isSuccessful():

Determines if a call was successful.

Input:

FieldTypeDescription

target

address

Address of the target contract.

success

bool

To check if the call to the contract was successful or not.

data

bytes

The data was sent while calling the target contract.

Output:

FieldTypeDescription

result

bool

The success of the call.

execute():

Executes a low level call.

Input:

FieldTypeDescription

self

address

The address of the contract to which the call is being made.

params

bytes

The parameters or data to be sent in the call.

Output:

FieldTypeDescription

result

bool

The success of the call.

Last updated