EcosystemNFTsDocs

Footer

Xchange

  • Swap
  • Liquidity
  • Loans
  • Fund
  • Vote

Tokens

  • X7R
  • X7DAO
  • X7D
  • X7101-X7105

Explore

  • Dashboard
  • Guides
  • Integration
  • Whitepaper
  • Breakdowns
  • Onchains
  • FAQs

Community

  • Telegram
  • Twitter
  • Discord
  • Warpcast
  • Blog
  • Links

Permissionless Trustless Decentralized Exchange

Launch your project on Xchange with 10-1000x the initial liquidity across multiple blockchains where anyone can Swap, Borrow and Lend!

X7 Finance·Trust No One. Trust Code. Long Live DeFi.
  • Telegram
  • Twitter
  • Discordś
  • Warpcast
  • YouTube
  • Reddit
  • GitHub
  • Pioneer 1488 avatar
    Guides
    View
  • Pioneer 1488 avatar
    Integration
    View
  • Pioneer 1488 avatar
    Whitepaper
    View
  • Pioneer 1488 avatar
    Breakdowns
    View
  • Pioneer 1488 avatar
    Onchains
    View
  • Pioneer 1488 avatar
    FAQ
    View
    • X7R Token Contract
    • X7DAO Token Contract
    • X7100 Token Contract
    • X7D
    • X7DAO Discount Authority
    • X7DAO Liquidity Hub
    • X7 Ecosystem Splitter
    • X7 Liquidity Loan Terms
    • X7 Lending Pool
    • X7 Lending Pool Reserve
    • X7 Lending Discount Authority
    • X7 Liquidity Treasury
    • X7 Profit Share Splitter
    • X7R Discount Authority
    • X7R Liquidity Hub
    • X7 Token Burner
    • X7 Token Timelock
    • X7 Treasury Splitter V3
    • X7100 Discount Authority
    • X7100 Liquidity Hub
    • X7 Borrowing Maxi NFT
    • X7 Dex Maxi NFT
    • X7 Ecosystem Maxi NFT
    • X7 Liquidity Maxi NFT
    • X7 Pioneer NFT
    • X7 Magister NFT
    • Xchange Factory
    • Xchange Pair
    • X7R Token Contract
    • X7DAO Token Contract
    • X7100 Token Contract
    • X7D
    • X7DAO Discount Authority
    • X7DAO Liquidity Hub
    • X7 Ecosystem Splitter
    • X7 Liquidity Loan Terms
    • X7 Lending Pool
    • X7 Lending Pool Reserve
    • X7 Lending Discount Authority
    • X7 Profit Share Splitter
    • X7R Discount Authority
    • X7R Liquidity Hub
    • X7 Token Burner
    • X7 Token Timelock
    • X7 Treasury Splitter V3
    • X7100 Discount Authority
    • X7100 Liquidity Hub
    • X7 Borrowing Maxi
    • X7 Dex Maxi
    • X7 Ecosystem Maxi
    • X7 Liquidity Maxi NFT
    • X7 Pioneer NFT
    • X7 Magister NFT
    • Xchange Factory
    • Xchange Pair
    • X7R Token Contract
    • X7DAO Token Contract
    • X7100 Token Contract
    • X7D
    • X7DAO Discount Authority
    • X7DAO Liquidity Hub
    • X7 Ecosystem Splitter
    • X7 Liquidity Loan Terms
    • X7 Lending Pool
    • X7 Lending Pool Reserve
    • X7 Lending Discount Authority
    • X7 Profit Share Splitter
    • X7R Discount Authority
    • X7R Liquidity Hub
    • X7 Token Burner
    • X7 Token Timelock
    • X7 Treasury Splitter V3
    • X7100 Discount Authority
    • X7100 Liquidity Hub
    • X7 Borrowing Maxi
    • X7 Dex Maxi
    • X7 Ecosystem Maxi
    • X7 Liquidity Maxi NFT
    • X7 Pioneer NFT
    • X7 Magister NFT
    • Xchange Factory
    • Xchange Pair
Contracts
breakdowns

X7DAO Liquidity Hub Contract

The contract X7DAOLiquidityHub is a smart contract for managing a liquidity pool on a decentralized exchange (DEX). The contract includes several interfaces for interacting with other contracts, including IUniswapV2Router for interacting with the V2 DEX, ILiquidityHub for defining functions that can be called by other contracts to process fees, and IERC20 for interacting with ERC20 tokens.

The contract has several variables that define the behavior of the liquidity pool, such as liquidityRatioTarget, minShare, and maxShare. It also has variables for tracking the balances of various accounts, including distributeBalance, auxiliaryBalance, and treasuryBalance.

interface IERC20 { function circulatingSupply() external view returns (uint256); function balanceOf(address account) external view returns (uint256); function transfer(address to, uint256 amount) external returns (bool); function approve(address spender, uint256 amount) external returns (bool); } interface IUniswapV2Router { function WETH() external returns (address); function addLiquidityETH( address token, uint amountTokenDesired, uint amountTokenMin, uint amountETHMin, address to, uint deadline ) external payable returns (uint amountToken, uint amountETH, uint liquidity); function swapExactETHForTokensSupportingFeeOnTransferTokens( uint amountOutMin, address[] calldata path, address to, uint deadline ) external payable; function swapExactTokensForETHSupportingFeeOnTransferTokens( uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline ) external; } interface ILiquidityHub { function processFees(address) external; } interface IX7EcosystemSplitter { function takeBalance() external; } interface IWETH { function withdraw(uint) external; }

Above are the Interface functions of the contract

function setShares(uint256 distributeShare_, uint256 liquidityShare_, uint256 auxiliaryShare_, uint256 treasuryShare_) external onlyOwner { require(distributeShare + liquidityShare + auxiliaryShare + treasuryShare == 1000); require(distributeShare_ >= minShare && distributeShare_ <= maxShare); require(liquidityShare_ >= minShare && liquidityShare_ <= maxShare); require(auxiliaryShare_ >= minShare && auxiliaryShare_ <= maxShare); require(treasuryShare_ >= minShare && treasuryShare_ <= maxShare); distributeShare = distributeShare_; liquidityShare = liquidityShare_; auxiliaryShare = auxiliaryShare_; treasuryShare = treasuryShare_; emit SharesSet(distributeShare_, liquidityShare_, auxiliaryShare_, treasuryShare_); }

The setShares function is a function for adjusting the distribution of fees between different accounts. The function takes four arguments: distributeShare_, liquidityShare_, auxiliaryShare_, and treasuryShare_. These arguments represent the percentage of fees that should be allocated to the distribute account, the liquidity account, the auxiliary account, and the treasury account, respectively.

The function first checks that the total percentage of fees being allocated adds up to 1000. It then checks that each of the individual shares is within a certain range (specified by the minShare and maxShare variables). If these checks pass, the function updates the values of the distributeShare, liquidityShare, auxiliaryShare, and treasuryShare variables and emits a SharesSet event.

This function can only be called by the contract owner.

function setRouter(address router_) external onlyOwner { require(router_ != address(router)); router = IUniswapV2Router(router_); emit RouterSet(router_); }

The setRouter function is a function for setting the address of the IUniswapV2Router contract that the X7DAOLiquidityHub contract should use for interacting with the V2 DEX. The function takes a single argument, router_, which is the address of the IUniswapV2Router contract.

The function first checks that the provided address is not the same as the current router address. It then assigns the provided address to the router variable and emits a RouterSet event.

This function can only be called by the contract owner.

function setOffRampPair(address offRampPairAddress) external onlyOwner { require(offRampPair != offRampPairAddress); offRampPair = offRampPairAddress; emit OffRampPairSet(offRampPairAddress); }

The setOffRampPair function is a function for setting the address of the V2 pair that should be used for off-ramp liquidity. The function takes a single argument, offRampPairAddress, which is the address of the V2 pair.

The function first checks that the provided address is not the same as the current off-ramp pair address. It then assigns the provided address to the offRampPair variable and emits an OffRampPairSet event.

This function can only be called by the contract owner.

function setBalanceThreshold(uint256 threshold) external onlyOwner { require(!balanceThresholdFrozen); balanceThreshold = threshold; emit BalanceThresholdSet(threshold); }

The setBalanceThreshold function is a function for setting the balance threshold for the X7DAOLiquidityHub contract. The balance threshold is the minimum balance that must be reached in the contract before fees can be processed and distributed to the various accounts.

The function takes a single argument, threshold, which is the new balance threshold. The function first checks that the balanceThresholdFrozen variable is not set (which indicates that the balance threshold can be modified). It then updates the value of the balanceThreshold variable and emits a BalanceThresholdSet event.

This function can only be called by the contract owner.

function setLiquidityRatioTarget(uint256 liquidityRatioTarget_) external onlyOwner { require(liquidityRatioTarget_ != liquidityRatioTarget); require(liquidityRatioTarget_ >= minLiquidityRatioTarget && liquidityRatioTarget <= maxLiquidityRatioTarget); liquidityRatioTarget = liquidityRatioTarget_; emit LiquidityRatioTargetSet(liquidityRatioTarget_); }

The setLiquidityRatioTarget function is a function for setting the target liquidity ratio for the X7DAOLiquidityHub contract. The liquidity ratio is the ratio of liquidity tokens (tokens representing ownership of the liquidity pool) to underlying tokens in the liquidity pool.

The function takes a single argument, liquidityRatioTarget_, which is the new target liquidity ratio. The function first checks that the provided target is not the same as the current target, and that it falls within a certain range (specified by the minLiquidityRatioTarget and maxLiquidityRatioTarget variables). If these checks pass, the function updates the value of the liquidityRatioTarget variable and emits a LiquidityRatioTargetSet event.

This function can only be called by the contract owner.

function setLiquidityTokenReceiver(address liquidityTokenReceiver_) external onlyOwner { require( liquidityTokenReceiver_ != address(0) && liquidityTokenReceiver_ != address(0x000000000000000000000000000000000000dEaD) && liquidityTokenReceiver != liquidityTokenReceiver_ ); address oldLiquidityTokenReceiver = liquidityTokenReceiver; liquidityTokenReceiver = liquidityTokenReceiver_; emit LiquidityTokenReceiverSet(oldLiquidityTokenReceiver, liquidityTokenReceiver_); }

The setLiquidityTokenReceiver function is a function for setting the address of the contract that should receive any new liquidity tokens that are minted. Liquidity tokens represent ownership of the liquidity pool and can be minted when new underlying tokens are added to the pool.

The function takes a single argument, liquidityTokenReceiver_, which is the address of the contract that should receive the new liquidity tokens. The function first checks that the provided address is not the zero address, is not the "dead" address (an address with all zeros), and is not the same as the current liquidity token receiver address. If these checks pass, the function updates the value of the liquidityTokenReceiver variable and emits a LiquidityTokenReceiverSet event.

This function can only be called by the contract owner.

function setDistributionTarget(address target) external onlyOwner { require( target != address(0) && target != address(0x000000000000000000000000000000000000dEaD) && distributeTarget != payable(target) ); require(!distributeTargetFrozen); address oldTarget = address(distributeTarget); distributeTarget = payable(target); emit DistributeTargetSet(oldTarget, distributeTarget); }

The setDistributionTarget function is a function for setting the address of the contract that should receive a share of the fees collected by the X7DAOLiquidityHub contract. The function takes a single argument, target, which is the address of the contract that should receive the fees.

The function first checks that the provided address is not the zero address, is not the "dead" address (an address with all zeros), and is not the same as the current distribution target address. It also checks that the distributeTargetFrozen variable is not set (which indicates that the distribution target can be modified). If these checks pass, the function updates the value of the distributeTarget variable (which is a payable address) and emits a DistributeTargetSet event.

This function can only be called by the contract owner.

function setAuxiliaryTarget(address target) external onlyOwner { require( target != address(0) && target != address(0x000000000000000000000000000000000000dEaD) && auxiliaryTarget != payable(target) ); require(!auxiliaryTargetFrozen); address oldTarget = address(auxiliaryTarget); auxiliaryTarget = payable(target); emit AuxiliaryTargetSet(oldTarget, target); }

The setAuxiliaryTarget function is a function for setting the address of the contract that should receive a share of the fees collected by the X7DAOLiquidityHub contract. The function takes a single argument, target, which is the address of the contract that should receive the fees.

The function first checks that the provided address is not the zero address, is not the "dead" address (an address with all zeros), and is not the same as the current auxiliary target address. It also checks that the auxiliaryTargetFrozen variable is not set (which indicates that the auxiliary target can be modified). If these checks pass, the function updates the value of the auxiliaryTarget variable (which is a payable address) and emits an AuxiliaryTargetSet event.

This function can only be called by the contract owner.

function setTreasuryTarget(address target) external onlyOwner { require( target != address(0) && target != address(0x000000000000000000000000000000000000dEaD) && treasuryTarget != payable(target) ); require(!treasuryTargetFrozen); address oldTarget = address(treasuryTarget); treasuryTarget = payable(target); emit TreasuryTargetSet(oldTarget, target); }

The setTreasuryTarget function is a function for setting the address of the contract that should receive a share of the fees collected by the X7DAOLiquidityHub contract. The function takes a single argument, target, which is the address of the contract that should receive the fees.

The function first checks that the provided address is not the zero address, is not the dead address (an address with all zeros), and is not the same as the current treasury target address. It also checks that the treasuryTargetFrozen variable is not set (which indicates that the treasury target can be modified). If these checks pass, the function updates the value of the treasuryTarget variable (which is a payable address) and emits a TreasuryTargetSet event.

This function can only be called by the contract owner.

function freezeTreasuryTarget() external onlyOwner { require(!treasuryTargetFrozen); treasuryTargetFrozen = true; emit TreasuryTargetFrozen(); }

The freezeTreasuryTarget function is a function for freezing the address of the contract that receives a share of the fees collected by the X7DAOLiquidityHub contract. When the treasury target is frozen, it cannot be changed by calling the setTreasuryTarget function.

The function has no arguments. It simply sets the treasuryTargetFrozen variable to true and emits a TreasuryTargetFrozen event.

This function can only be called by the contract owner.

function freezeDistributeTarget() external onlyOwner { require(!distributeTargetFrozen); distributeTargetFrozen = true; emit DistributeTargetFrozen(); }

The freezeDistributeTarget function is a function for freezing the address of the contract that receives a share of the fees collected by the X7DAOLiquidityHub contract. When the distribution target is frozen, it cannot be changed by calling the setDistributionTarget function.

The function has no arguments. It simply sets the distributeTargetFrozen variable to true and emits a DistributeTargetFrozen event.

This function can only be called by the contract owner.

function freezeAuxiliaryTarget() external onlyOwner { require(!auxiliaryTargetFrozen); auxiliaryTargetFrozen = true; emit AuxiliaryTargetFrozen(); }

The freezeAuxiliaryTarget function is a function for freezing the address of the contract that receives a share of the fees collected by the X7DAOLiquidityHub contract. When the auxiliary target is frozen, it cannot be changed by calling the setAuxiliaryTarget function.

The function has no arguments. It simply sets the auxiliaryTargetFrozen variable to true and emits an AuxiliaryTargetFrozen event.

This function can only be called by the contract owner.

function freezeBalanceThreshold() external onlyOwner { require(!balanceThresholdFrozen); balanceThresholdFrozen = true; emit BalanceThresholdFrozen(); }

The freezeBalanceThreshold function is a function for freezing the balance threshold for the X7DAOLiquidityHub contract. The balance threshold is the minimum balance that must be reached in the contract before fees can be processed and distributed to the various accounts. When the balance threshold is frozen, it cannot be changed by calling the setBalanceThreshold function.

The function has no arguments. It simply sets the balanceThresholdFrozen variable to true and emits a BalanceThresholdFrozen event.

This function can only be called by the contract owner.

function processFees(address tokenAddress) external { uint256 startingETHBalance = address(this).balance; uint256 tokensToSwap = IERC20(tokenAddress).balanceOf(address(this)); if (tokenAddress == address(x7dao)) { tokensToSwap -= x7daoLiquidityBalance; } if (tokensToSwap > 0) { swapTokensForEth(tokenAddress, tokensToSwap); } uint256 ETHForDistribution = address(this).balance - startingETHBalance; distributeBalance += ETHForDistribution * distributeShare / 1000; auxiliaryBalance += ETHForDistribution * auxiliaryShare / 1000; treasuryBalance += ETHForDistribution * treasuryShare / 1000; liquidityBalance = address(this).balance - distributeBalance - auxiliaryBalance - treasuryBalance; if (distributeBalance >= balanceThreshold) { sendDistributeBalance(); } if (auxiliaryBalance >= balanceThreshold) { sendAuxiliaryBalance(); } if (treasuryBalance >= balanceThreshold) { sendTreasuryBalance(); } if (liquidityBalance >= balanceThreshold) { buyBackAndAddLiquidity(); } }

The processFees function is a function for processing and distributing fees collected by the X7DAOLiquidityHub contract. The function takes a single argument, tokenAddress, which is the address of the token contract that the fees were collected in.

The function begins by storing the contract's starting ETH balance in the startingETHBalance variable. It then calculates the number of tokens to swap for ETH by calling the balanceOf function of the token contract at tokenAddress, and subtracting the x7daoLiquidityBalance if the token is the x7dao token. If the number of tokens to swap is greater than zero, the function calls the swapTokensForEth function to swap the tokens for ETH.

After the tokens are swapped for ETH, the function calculates the total ETH balance for distribution by subtracting the starting ETH balance from the current contract balance. It then calculates the balances for the distribute, auxiliary, and treasury accounts by multiplying the distribution ETH balance by the corresponding share and dividing by 1000. It also calculates the remaining liquidity balance by subtracting the other balances from the total contract balance.

Finally, the function checks if any of the balances have reached or exceeded the balanceThreshold, and if so, calls the sendDistributeBalance, sendAuxiliaryBalance, `sendTreasuryBalance

function sendDistributeBalance() public { if (distributeTarget == address(0)) { return; } IX7EcosystemSplitter(distributeTarget).takeBalance(); uint256 ethToSend = distributeBalance; distributeBalance = 0; (bool success,) = distributeTarget.call{value: ethToSend}(""); if (!success) { distributeBalance = ethToSend; } }

The sendDistributeBalance function is a function for sending the balance in the distribute account of the X7DAOLiquidityHub contract to the distributeTarget address.

The function first checks if the distributeTarget address is not the zero address. If it is, the function returns without doing anything. If the distributeTarget address is not the zero address, the function calls the takeBalance function on the distributeTarget contract (which is assumed to be an instance of the IX7EcosystemSplitter interface).

Next, the function stores the current distributeBalance in the ethToSend variable and sets the distributeBalance to zero. It then calls the send function on the distributeTarget address, passing it the ethToSend value as the amount to send. If the send function call fails (i.e., returns false), the function sets the distributeBalance back to the ethToSend value.

This function is marked public so it can be called externally.

function sendTreasuryBalance() public { if (treasuryTarget == address(0)) { return; } uint256 ethToSend = treasuryBalance; treasuryBalance = 0; (bool success,) = treasuryTarget.call{value: ethToSend}(""); if (!success) { treasuryBalance = ethToSend; } }

The sendTreasuryBalance function is a function for sending the balance in the treasury account of the X7DAOLiquidityHub contract to the treasuryTarget address.

The function first checks if the treasuryTarget address is not the zero address. If it is, the function returns without doing anything. If the treasuryTarget address is not the zero address, the function stores the current treasuryBalance in the ethToSend variable and sets the treasuryBalance to zero. It then calls the send function on the treasuryTarget address, passing it the ethToSend value as the amount to send. If the send function call fails (i.e., returns false), the function sets the treasuryBalance back to the ethToSend value.

This function is marked public so it can be called externally.

function sendAuxiliaryBalance() internal { if (auxiliaryTarget == address(0)) { return; } uint256 ethToSend = auxiliaryBalance; auxiliaryBalance = 0; (bool success,) = auxiliaryTarget.call{value: ethToSend}(""); if (!success) { auxiliaryBalance = ethToSend; } }

The sendAuxiliaryBalance function is a function for sending the balance in the auxiliary account of the X7DAOLiquidityHub contract to the auxiliaryTarget address.

The function first checks if the auxiliaryTarget address is not the zero address. If it is, the function returns without doing anything. If the auxiliaryTarget address is not the zero address, the function stores the current auxiliaryBalance in the ethToSend variable and sets the auxiliaryBalance to zero. It then calls the send function on the auxiliaryTarget address, passing it the ethToSend value as the amount to send. If the send function call fails (i.e., returns false), the function sets the auxiliaryBalance back to the ethToSend value.

This function is marked internal, which means it can only be called by other functions within the same contract or by derived contracts.

function buyBackAndAddLiquidity() internal { uint256 ethForSwap; uint256 startingETHBalance = address(this).balance; if (x7dao.balanceOf(offRampPair) > x7dao.circulatingSupply() * liquidityRatioTarget / 100 ) { ethForSwap = liquidityBalance; liquidityBalance = 0; swapEthForTokens(ethForSwap); } else { ethForSwap = liquidityBalance; liquidityBalance = 0; if (x7dao.balanceOf(address(this)) > 0) { addLiquidityETH(x7dao.balanceOf(address(this)), ethForSwap); ethForSwap = ethForSwap - (startingETHBalance - address(this).balance); } if (ethForSwap > 0) { uint256 ethLeft = ethForSwap; ethForSwap = ethLeft / 2; uint256 ethForLiquidity = ethLeft - ethForSwap; swapEthForTokens(ethForSwap); addLiquidityETH(x7dao.balanceOf(address(this)), ethForLiquidity); } } x7daoLiquidityBalance = x7dao.balanceOf(address(this)); }

The buyBackAndAddLiquidity function is a function for buying back tokens and adding liquidity to the pool for the X7DAOLiquidityHub contract.

The function first checks the balance of the x7dao token on the off-ramp pair contract. If it is greater than the liquidityRatioTarget percentage of the circulating supply of the x7dao token, the function swaps all of the liquidityBalance for x7dao tokens using the swapEthForTokens function. If the balance of the x7dao token on the off-ramp pair contract is not greater than the liquidityRatioTarget percentage of the circulating supply of the x7dao token, the function does the following:

  1. Stores the current liquidityBalance in the ethForSwap variable and sets the liquidityBalance to zero.
  2. If the contract has any x7dao tokens, it adds liquidity to the pool using the addLiquidityETH function, passing it the balance of x7dao tokens that the contract holds and the remaining ethForSwap value. The ethForSwap value is updated to the difference between the starting ETH balance of the contract and its current ETH balance.
  3. If there is still some ethForSwap left (i.e., the ethForSwap value is greater than zero), the function splits it in half and uses half of it to swap for x7dao tokens using the swapEthForTokens function and the other half to add liquidity to the pool using the addLiquidityETH function.

Finally, the function updates the x7daoLiquidityBalance variable to the current balance of x7dao tokens that the contract holds.

This function is marked internal, which means that it can only be called by other functions within the same contract. This means that it is not possible to call this function directly from another contract or from an external actor.

function addLiquidityETH(uint256 tokenAmount, uint256 ethAmount) internal { x7dao.approve(address(router), tokenAmount); router.addLiquidityETH{value: ethAmount}( address(x7dao), tokenAmount, 0, 0, liquidityTokenReceiver, block.timestamp ); }

This function, addLiquidityETH, is used to add liquidity to the X7DAO liquidity hub contract.

This function takes two arguments: tokenAmount and ethAmount. The tokenAmount argument represents the amount of X7DAO tokens that should be added as liquidity, and the ethAmount argument represents the amount of Ether that should be added as liquidity.

The function first calls the approve function on the X7DAO contract, which allows the router contract to transfer the specified amount of X7DAO tokens on behalf of the liquidity hub contract. This is necessary because the addLiquidityETH function in the router contract will transfer the tokens from the liquidity hub contract to the router contract as part of the liquidity addition process.

Then, the function calls the addLiquidityETH function in the router contract, passing in the address of the X7DAO contract, the tokenAmount and ethAmount arguments, and some other parameters. This function will perform the actual liquidity addition process by creating a new liquidity pool in the exchange and adding the specified amount of X7DAO tokens and Ether as liquidity.

Finally, the function updates the x7daoLiquidityBalance variable to store the current balance of X7DAO tokens held by the liquidity hub contract. This variable is used to track the total amount of X7DAO tokens that have been added as liquidity to the exchange by the liquidity hub contract.

function swapTokensForEth(address tokenAddress, uint256 tokenAmount) internal { address[] memory path = new address[](2); path[0] = tokenAddress; path[1] = router.WETH(); IERC20(tokenAddress).approve(address(router), tokenAmount); router.swapExactTokensForETHSupportingFeeOnTransferTokens( tokenAmount, 0, path, address(this), block.timestamp ); }

The function is called swapTokensForEth and it takes in two arguments: tokenAddress, which is the address of the token that we want to swap, and tokenAmount, which is the amount of tokens we want to swap.

The function first sets up an array path which contains the addresses of the token and WETH (wrapped ETH). Then it calls the approve function on the token contract to give the router contract permission to transfer tokenAmount of tokens on behalf of the contract.

Next, the function calls the swapExactTokensForETHSupportingFeeOnTransferTokens function on the router contract, passing in the amount of tokens we want to swap, a value of 0 for the minimum amount of ETH we want to receive, the path array, the contract's own address as the recipient, and the current block timestamp as the deadline. This function will swap the tokens for an equivalent amount of ETH.

function swapEthForTokens(uint256 ethAmount) internal { address[] memory path = new address[](2); path[0] = router.WETH(); path[1] = address(x7dao); router.swapExactETHForTokensSupportingFeeOnTransferTokens{value: ethAmount}( 0, path, address(this), block.timestamp ); }

The swapTokensForEth() function is used to swap a given amount of tokens for an equivalent amount of ETH. The function does this by calling the swapExactETHForTokensSupportingFeeOnTransferTokens function on the router contract, which is an instance of the V2 Router contract. This function takes four arguments:

  1. slippage: The allowed slippage for the token to ETH exchange rate. A value of 0 means that the exchange rate must be exactly equal to the current market rate.
  2. path: An array of addresses representing the token exchange path to use for the swap. In this case, the array contains the addresses of the WETH contract and the x7DAO token contract.
  3. to: The address that will receive the tokens obtained from the swap. In this case, it is the contract's own address.
  4. deadline: The timestamp after which the function call will be rejected. This is provided by the block.timestamp variable, which returns the current block's timestamp.

The function also includes a value keyword in its function call, which specifies the amount of ETH to send along with the function call. This is the amount of ETH that will be swapped for tokens.

Finally, the function does not return any value. Instead, the tokens obtained from the swap will be transferred to the contract's own address.

function rescueWETH() external { address wethAddress = router.WETH(); IWETH(wethAddress).withdraw(IERC20(wethAddress).balanceOf(address(this))); }

The rescueWETH() function allows the contract owner to withdraw any WETH tokens that may be stuck in the contract's balance. This can happen if, for example, the contract has mistakenly received WETH tokens through a function call that did not properly transfer ownership.

The function first gets the address of the WETH contract by calling the WETH() function on the router contract. Then it calls the withdraw() function on the WETH contract, passing in the balance of WETH tokens that the contract owns as an argument. This will transfer ownership of the WETH tokens back to the contract owner's address.

Previous
← X7DAO Discount Authority
Next
X7 Ecosystem Splitter →

-

  • Pioneer 1488 avatar
    Guides
    View
  • Pioneer 1488 avatar
    Integration
    View
  • Pioneer 1488 avatar
    Whitepaper
    View
  • Pioneer 1488 avatar
    Breakdowns
    View
  • Pioneer 1488 avatar
    Onchains
    View
  • Pioneer 1488 avatar
    FAQ
    View