LibAsset

This contract defines a library called LibAsset which provides utility functions for handling assets, particularly focusing on native assets like Ether. The library includes several custom error types for handling various failure scenarios such as AssetNotReceived, ApprovalFailed, TransferFromFailed, TransferFailed, PermitFailed, FailedWrap, and FailedUnwrap. The isNative function checks if a given address represents a native asset (Ether) by comparing it to a constant NATIVE_ASSETID, which is set to the zero address. The wrap function is designed to wrap a specified asset by using inline assembly to prepare a call to the asset’s contract. If the wrapping operation fails, it reverts with a FailedWrap error. Similarly, the unwrap function unwraps a specified asset, also using inline assembly to prepare the call and reverting with a FailedUnwrap error if the operation fails. Lastly, the getBalance function retrieves the balance of the current contract for a given asset by calling another internal function getBalanceOf.

Function Name
Description (Business Logic)

getBalanceOf

The getBalanceOf function retrieves the balance of a specified asset for a target address. It uses inline assembly to handle both native assets (Ether) and other ERC-20 tokens. For native assets, it directly retrieves the balance using the balance opcode. For ERC-20 tokens, it constructs a call to the balanceOf function of the token contract, passing the target address as an argument. If the call fails, it reverts with the returned data.

transferFrom

The transferFrom function performs a safe transfer of a specified asset from one address to another. It uses inline assembly to prepare a call to the transferFrom function of the asset’s contract, passing the sender’s address, the recipient’s address, and the amount to be transferred. If the transfer operation fails, it reverts with a TransferFromFailed error. This function ensures that asset transfers are handled securely and correctly.

transfer

The transfer function facilitates the transfer of a specified amount of an asset to a recipient address. If the asset is native (Ether), it uses a low-level call to transfer the amount directly. If the transfer fails, it reverts with a TransferFailed error. For non-native assets, it constructs a call to the transfer function of the asset’s contract using inline assembly. If the transfer operation fails, it also reverts with a TransferFailed error. This function ensures that both native and non-native asset transfers are handled securely.

approve

The approve function allows a spender address to spend a specified amount of an asset on behalf of the owner. It constructs a call to the approve function of the asset’s contract using inline assembly. If the approval operation fails, it attempts to reset the allowance to zero and then re-approve the specified amount. If these attempts fail, it reverts with an ApprovalFailed error. This function ensures that the approval process is robust and handles potential issues with resetting allowances.

permit

The permit function allows for the approval of a spender to spend a specified amount of an asset on behalf of the owner, using a signature. This function is useful for ERC-20 tokens that support the permit function, enabling gasless approvals. It constructs a call to the permit function of the asset’s contract using inline assembly, passing the owner’s address, spender’s address, amount, deadline, and the signature components (v, r, s). If the permit operation fails, it reverts with a PermitFailed error.

isSuccessful

The isSuccessful function checks if a call to a target contract was successful. It takes the target address, a success flag, and the returned data as inputs. If the call was successful and the returned data is empty, it checks if the target address is a contract by verifying the code length. If the returned data is not empty, it loads the result from the data using inline assembly. This function ensures that the success of a call is accurately determined, handling both contract and non-contract addresses.

execute

The execute function performs a low-level call to a target contract, handling both native and non-native assets. It uses inline assembly to construct and execute the call, passing the necessary parameters such as the target address (self), the amount of native asset to send (currentNativeAmount), and pointers to the input and output data. The function includes an internal helper function, isSuccessfulCall, which determines if the call was successful by checking the return data size and the existence of code at the target address. If the call fails, it reverts with the returned data. This function ensures that low-level calls are executed safely and correctly, providing a robust mechanism for interacting with other contracts.

Last updated