LibAssetV2

The LibAssetV2 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

The asset that will be checked for a native token.

Output:

FieldTypeDescription

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

Asset whose balance needs to be found

targetAddress

address

The address where the balance is checked from

Output:

FieldTypeDescription

Balance of the 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

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:

FieldTypeDescription

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:

FieldTypeDescription

The asset that will be approved.

Address of a contract that will use the owners asset.

Asset amount that can be spent.

isSuccessful():

This function is useful in scenarios where a contract interacts with another contract and needs to determine if the interaction was successful, especially when dealing with low-level calls (call, delegatecall, staticcall) that return a success flag and data.

Input:

FieldTypeDescription

Address of the target contract

bool

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

bytes memory

The data was sent while calling the target contract.

execute():

This function is useful in scenarios where a contract needs to interact with another contract and then determine if that interaction was successful, not just at the surface level (i.e., the call didn't throw an error), but also based on the content of the response.

Input:

FieldTypeDescription

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

params

bytes memory

The parameters or data to be sent in the call.

Last updated