LibSwap

struct SwapData {
    uint16 amountsOffset;
    uint16 dataOffset;
    uint16 commandsOffset;
    uint16 commandsOffsetEnd;
    uint16 outputsLength;
    uint256 amountIn;
    address toAddress;
    address fromAssetAddress;
    address toAssetAddress;
    uint256 deadline;
    uint256 amountOutMin;
}
FieldTypeDescription
amountsOffset
uint16

This is a 16-bit unsigned integer representing the offset for the amounts section in the transaction calldata. Offsets are used to locate specific data within a block of calldata.

dataOffset
uint16

Similar to amountsOffset, this is a 16-bit unsigned integer representing the offset for another data section in the calldata. The specific nature of this data depends on the context in which SwapData is used.

commandsOffset
uint16

This is a 16-bit unsigned integer indicating the starting point of the commands section in the calldata. Commands in the context of a swap refers to specific actions.

commandsOffsetEnd
uint16

This is a 16-bit unsigned integer marking the end of the commands section in the calldata. Knowing the start and end of the commands section helps in parsing and executing them correctly.

outputsLength

uint16

Represents the length of all of the commands.

amountIn
uint256

A 256-bit unsigned integer representing the amount of the asset being provided in the swap.

toAddress
address

This is the Ethereum address to which the output of the swap (the swapped asset) will be sent.

fromAssetAddress
address

The Ethereum address of the asset being swapped from. This could be the contract address of an ERC-20 token or another type of asset.

toAssetAddress
address

The Ethereum address of the asset being swapped to. Like fromAssetAddress, this is typically the contract address of the target ERC-20 token or other asset types.

deadline
uint256

A timestamp (usually in seconds since the Unix epoch) indicating the deadline by which the swap must be completed.

amountOutMin
uint256

The minimum amount of the output asset that must be received for the swap to be considered successful.

getAmountIn():

getAmountIn is a function that iterates over a portion of the transaction calldata, extracting and summing up amounts of an asset.

Input:

FieldTypeDescription

startOffset

uint16

The location starting position

endOffset

uint16

The location ending position

positionOffset

uint16

The location of the current position

Ouput:

FieldTypeDescription

amountIn

uint256

The Amount for the current swap.

getFirstAmountIn():

the getFirstAmountIn function is a utility function used to extract the first amount of an asset involved in a swap operation directly from the calldata of a transaction.

Input:

FieldTypeDescription

swapArgsOffset

uint16

The location where the swapping data is stored.

Output:

FieldTypeDescription

amountIn

uint256

The first amount in.

getData():

getData is a function that parses and organizes data from a transaction's calldata for a token swap operation.

Input:

FieldTypeDescription

swapArgsOffset

uint16

The location where the swapping data is stored.

Output:

FieldTypeDescription

swapData

SwapData memory

struct SwapData {
    uint16 amountsOffset;
    uint16 dataOffset;
    uint16 commandsOffset;
    uint16 commandsOffsetEnd;
    uint256 amountIn;
    address toAddress;
    address fromAssetAddress;
    address toAssetAddress;
    uint256 deadline;
    uint256 amountOutMin;
}

Last updated