LibRouter

The LibRouter smart contract facilitates efficient and secure swap operations by preparing and managing swap data, transferring necessary fees, and verifying permissions and signatures. The getData function extracts and organizes swap parameters from calldata, ensuring transactions are valid and not expired. The transferFees function handles the transfer of gas and affiliate fees, accommodating both native and non-native assets. The permit function grants the contract permission to use the user’s assets for the swap, covering all associated fees. The recoverSigner function ensures the integrity of signatures by recovering the signer’s address and validating it. Additionally, the getDomainSeparator function generates a domain separator for EIP-712 typed data hashing, crucial for preventing replay attacks. The verifySignature function verifies the signature for a swap operation by constructing a message hash and recovering the signer’s address, ensuring the authenticity of the transaction. Overall, the contract ensures secure and efficient swap operations through meticulous data handling and verification processes.

Function Name
Description (Business Logic)

getData

The getData function in the LibRouter library is designed to prepare and return a SwapData struct from the calldata. It begins by calculating the deadline from the calldata and checks if the transaction has expired by comparing it with the current timestamp. If the transaction is expired, it reverts with an ExpiredTransaction error. The function then proceeds to populate the SwapData struct with various parameters extracted from the calldata, including toAddress, fromAssetAddress, toAssetAddress, deadline, amountOutMin, gasFee, and amountIn. It also determines if the transaction includes a permit by checking the v value from the calldata and sets the hasPermit flag accordingly. Depending on whether the permit is present, it further checks for the presence of an affiliate and populates the affiliateAddress and affiliateFee fields if applicable. The function uses inline assembly for efficient data manipulation and storage within the SwapData struct.

transferFees

The transferFees function is responsible for transferring the necessary fees for a swap operation from the user’s account. It first checks if the fromAssetAddress is a native asset, in which case it sets the gasFee to zero. If there is a gasFee greater than zero, it transfers this fee from the user’s address to the contract’s address. Additionally, if there is an affiliateFee, the function checks if the asset is native. If it is, the fee is transferred directly to the affiliate’s address. Otherwise, the fee is transferred from the user’s address to the affiliate’s address using the transferFrom method.

permit

The permit function grants permission for the user’s asset to be used in a swap operation. It extracts the v, r, and s values, as well as the deadline from the calldata using inline assembly. These values are then used to call the permit method on the fromAssetAddress, allowing the contract to spend the specified amount of the user’s asset. The amount includes the amountIn, gasFee, and affiliateFee, ensuring that all necessary fees are covered by the permission granted.

recoverSigner

The recoverSigner function is a private, pure function that recovers the signer’s address from a hashed message and its signature components (r, s, and v). This function ensures the uniqueness of the signature by addressing potential malleability issues as outlined in the Ethereum Yellow Paper. It first checks if the s value is within the valid range, reverting with an InvalidSignature error if it is not. It also verifies that the v value is either 27 or 28, reverting with an InvalidSignature error if it is not. The function then uses the ecrecover function to obtain the signer’s address from the hash and the signature components. If the recovered address is the zero address, it reverts with an InvalidSignature error, ensuring that only valid signatures are accepted.

getDomainSeparator

The getDomainSeparator function is a private view function that generates a domain separator for EIP-712 typed data hashing. It first retrieves the current chain ID using inline assembly. The function then returns the keccak256 hash of an encoded EIP-712 domain, which includes the name, version, chain ID, and the contract’s address. The domain separator is crucial for ensuring the integrity and uniqueness of the signed data, preventing replay attacks across different domains.

verifySignature

The verifySignature function verifies the signature for a swap operation. It takes several parameters, including the SwapData struct, a pointer to the message data in memory, the length of the message data, a flag indicating whether to use the caller’s address for verification, and a slot in the internal callers storage for verification. The function first generates a domain separator using the getDomainSeparator function. It then constructs the message hash by storing various fields from the SwapData struct in memory and computing the keccak256 hash of the message data. The domain separator and message hash are combined to create the final digest. Using inline assembly, the function extracts the r, s, and v components of the signature from the calldata. If the useCaller flag is set, it recovers the signer’s address from the digest and verifies it against the internal callers storage. If the verification fails, it reverts with an InvalidSignature error. If the useCaller flag is not set, it simply recovers the signer’s address from the digest and returns it.

swapInWithUserSignature

The swapInWithUserSignature function, restricted to internal callers, facilitates swaps using a user signature.

swapInWithMagpieSignature

The swapInWithMagpieSignature function allows for token swaps using a Magpie signature, and can only be executed when the contract is not paused.

Last updated