relevant tag icon
Euler Swap
copy icon
euler-xyz
• version 1.0.0
Finance
Swap
ERC20
AMM

Euler Swap

This module allows users to swap their deposited underlying tokens on Uniswap V3 and 1inch DEXes. Under the hood, the tokens are swapped directly from the pool, thus saving gas, which would normally be spent to withdraw and deposit back the traded assets. From the user's perspective the swap will change the balances of their eTokens. Paired with deferred liquidity check (see below), the swap module allows users to put on one-click leveraged long and short positions on any collateral vs collateral asset pairs and one-click leveraged short positions on any collateral vs non-collateral pairs.

*Visit desktop site to download or deploy

Version

1.0.0

Creator

euler-xyz

Last Publish

1/16/2023
Any contract you deploy is yours.
Fully owned and controlled by your wallet.
Documentation
Source Code
swapUniExactInputSingle() :
This function is used to perform a Uniswap swap with exact input single parameters. It takes in parameters such as the underlying tokens to be swapped, the amount of tokens to be swapped, the subaccount IDs of the sender and receiver, and the fee to be paid. It then sets the amount of tokens to be withdrawn and approves the underlying token to be used in the swap. It then calls the exactInputSingle function of the Uniswap Router contract to perform the swap, passing in the parameters specified. Finally, it calls the finalizeSwap function to complete the swap. (autogenerated documentation)
swapUniExactInput() :
This function allows a user to swap one ERC20 token for another using Uniswap. It takes in a set of parameters, including the addresses of the two tokens to be swapped, the amount of the input token, the subaccount IDs of the input and output tokens, and the deadline for the swap. It then calls the Uniswap Router's exactInput function, which returns the amount of the output token that will be received in exchange for the input token. Finally, it calls the finalizeSwap function to complete the swap. (autogenerated documentation)
swapUniExactOutputSingle() :
This function is used to perform a swap between two ERC20 tokens using the Uniswap V2 exchange. It takes in a set of parameters, including the underlying tokens to be swapped, the amount of the output token to be received, and the subaccount IDs of the sender and receiver. It then initializes a SwapCache memory object with the given parameters, sets the amountOut field of the SwapCache object to the given amountOut parameter, and calls the doSwapUniExactOutputSingle function to perform the swap. Finally, it calls the finalizeSwap function to complete the swap. (autogenerated documentation)
swapUniExactOutput() :
This function is used to perform a swap of two ERC20 tokens using the Uniswap V2 exchange. It takes in a SwapUniExactOutputParams struct containing the path of the tokens to be swapped, the amount of the output token to be received, and the subaccount IDs of the input and output tokens. It then decodes the path to get the underlying input and output tokens, initializes a SwapCache struct with the relevant information, and calls the doSwapUniExactOutput function to execute the swap. Finally, it calls the finalizeSwap function to complete the swap. (autogenerated documentation)
swapAndRepayUniSingle() :
This function allows a user to swap one ERC20 token for another and repay a debt at the same time. It takes two parameters: a SwapUniExactOutputSingleParams memory struct containing information about the tokens to be swapped and the target debt to be repaid, and a uint targetDebt representing the amount of debt to be repaid. It first initializes a SwapCache memory struct with the provided parameters, then calculates the amount of the token to be repaid using the getRepayAmount function. Finally, it executes the swap using the doSwapUniExactOutputSingle function and finalizes the swap and repayment using the finalizeSwapAndRepay function. (autogenerated documentation)
swapAndRepayUni() :
This function is used to swap and repay a debt using Uniswap. It takes two parameters: a SwapUniExactOutputParams memory struct and a targetDebt uint. It first decodes the Uniswap path from the SwapUniExactOutputParams struct and initializes a SwapCache memory struct with the underlyingIn, underlyingOut, targetDebt, subAccountIdIn, and subAccountIdOut. It then calculates the amountOut of the swap using the getRepayAmount function and calls the doSwapUniExactOutput function to execute the swap. Finally, it calls the finalizeSwapAndRepay function to complete the swap and repay the debt. (autogenerated documentation)
swap1Inch() :
This function is used to perform a swap using the 1inch protocol. It takes in a set of parameters, including the underlying tokens to be swapped, the amount of tokens to be swapped, and the subaccount IDs of the sender and receiver. It then initializes a swap cache, sets the withdrawal amounts, and calls the 1inch protocol with the given payload. If the call is successful, it decodes the result to get the amount of tokens to be received, and then finalizes the swap. (autogenerated documentation)
initSwap(address,address) :
This function is used to initiate a swap between two different assets. It takes in the addresses of the two underlying assets, the amount of each asset to be swapped, the subaccount IDs of the two accounts involved in the swap, and the type of swap to be performed. It then emits a RequestSwap event, and loads the asset caches for the two underlying assets. Finally, it calls the balanceOf function to get the balance of each asset in the swap. (autogenerated documentation)
doSwapUniExactOutputSingle() :
This function is used to perform a swap on Uniswap using the exact output single method. It first approves the Uniswap router to transfer the maximum amount of the input token from the caller. It then calls the exactOutputSingle function on the Uniswap router, passing in the parameters for the swap. The function then sets the amount of the input and output tokens for the swap, and if the amount of the input token is less than the maximum amount, it approves the Uniswap router to transfer 0 of the input token. (autogenerated documentation)
doSwapUniExactOutput(address) :
This function is used to perform a swap on Uniswap using the exact output method. It first approves the Uniswap router to transfer the maximum amount of the input token from the caller's address. It then calls the exactOutput function on the Uniswap router, passing in the path, recipient, deadline, amountOut, and amountInMaximum parameters. The function then sets the withdraw amounts for the swap and, if the amountIn is less than the amountInMaximum, it approves the Uniswap router to transfer 0 of the input token from the caller's address. (autogenerated documentation)
setWithdrawAmounts() :
This function sets the amount of tokens to be withdrawn from a swap in the SwapCache memory. It takes in the swap and the amount to be withdrawn as parameters. It then calls the withdrawAmounts function to calculate the amount of tokens to be withdrawn and the amount of internal tokens to be received. It then sets the amount of tokens to be withdrawn in the swap and requires that the pool size is greater than or equal to the amount to be withdrawn. (autogenerated documentation)
finalizeSwap() :
This function is used to finalize a swap between two different ERC20 tokens. It first checks the balances of the two tokens involved in the swap, then processes the withdrawal of the token from the account of the sender and the deposit of the token to the account of the receiver. Finally, it checks the liquidity of the sender's account. (autogenerated documentation)
finalizeSwapAndRepay() :
This function is used to finalize a swap and repay transaction. It first checks the balances of the swap, then processes the withdraw of the asset from the accountIn, and then processes the repayment of the asset to the accountOut. Finally, it checks the liquidity of the accountIn. (autogenerated documentation)
processWithdraw(address,address) :
This function processes a withdrawal from an account. It takes in the AssetStorage storage, AssetCache memory, eTokenAddress, account address, amountInternal, and balanceIn as parameters. It then sets the assetCache poolSize to the decoded external amount of the balanceIn. It then decreases the balance of the account in the assetStorage by the amountInternal. Finally, it logs the assetCache status. (autogenerated documentation)
processDeposit(address,address) :
This function processes a deposit of an asset to an account. It takes in the AssetStorage storage, AssetCache memory, eTokenAddress, account address, and amount as parameters. It first decodes the external amount and converts it to an internal balance amount. It then increases the pool size by the decoded amount and increases the balance of the account by the internal balance amount. Finally, it checks the liquidity of the account and logs the asset status. (autogenerated documentation)
processRepay(address) :
This function is used to process a repayment of a loan. It takes in an AssetStorage storage object, an AssetCache memory object, an address of the account making the repayment, and the amount being repaid. It then calls the decreaseBorrow function to reduce the amount of the loan owed by the account, and logs the asset status in the assetCache. (autogenerated documentation)
checkBalances() :
This function checks the balances of two assets in a SwapCache memory swap. It calls the BalanceOf function to get the balance of the assetCacheIn and assetCacheOut, and then compares it to the balanceIn and balanceOut values stored in the SwapCache memory swap. If the balances match, the function returns the balanceIn value. Otherwise, it throws an error. (autogenerated documentation)
decodeUniPath(bool) :
This function decodes a uni-directional path from a byte array. It takes two parameters, the byte array and a boolean value. It requires that the byte array is at least 20 bytes plus 3 bytes plus 20 bytes long, and that the length of the byte array minus 20 is divisible by 23. It then converts the first 20 bytes of the array into an address and the last 20 bytes into an address. Finally, it returns the two addresses, either in the exact order or reversed depending on the boolean value. (autogenerated documentation)
getRepayAmount() :
This function calculates the amount of debt that needs to be repaid by a borrower. It takes in a SwapCache memory swap, which contains information about the borrower's account, the asset they are borrowing, and the eToken they are using to borrow, as well as a targetDebt, which is the amount of debt the borrower is aiming to repay. The function then calculates the current amount of debt owed by the borrower by dividing the current owed amount by the underlyingDecimalsScaler of the asset being borrowed. It then requires that the amount of debt owed is greater than the targetDebt, and returns the difference between the two as the amount of debt to be repaid. (autogenerated documentation)
toAddress() :
This function takes in two parameters, a bytes memory data and a uint start, and returns an address. It works by loading the data from the memory address given by adding the data and start parameters together, and then dividing the result by 0x1000000000000000000000000 to get the address. (autogenerated documentation)

Get Cookin'
share iconShare

copy iconDownload Source
copy iconnpx cookbookdev i euler-swap
copy icon

Last Publish

1/16/2023

Version

1.0.0

Creator

euler-xyz

Cookbook is free.
Any contract you deploy is yours.
Your contract is owned and controlled by you.