Each time a user swaps, the handling fee is directly transferred to the contract. And the preset users can withdraw fees from the contract.
Preset the Payee for fee withdrawl
The admin can add payee and set the corresponding withdrawal propotion in the contract; Payees can only withdraw tokens from the contract according to their preset propotions.
The payee addresses and their corresponding shares are set in the constructor function
constructor(address[] memory payees,uint256[] memory shares_,address_owner){require(payees.length == shares_.length,"payees and shares length mismatch");require(payees.length >0,"no payees");for (uint256 i =0; i < payees.length; i++) {_addPayee(payees[i], shares_[i]); }require(_owner !=address(0),"owner can not be zero address");_transferOwnership(_owner); converters[_owner] =true; }
Covert fee token to stablecoins
The contract contains a 'Convert' interface for converting tokens in the contract to stablecoins like USDT or USDC.
Convert structure is a key component for a convert transaction. It contains four fields, see below code blocks for detailed information.
structConvert {address token; //the fee token contract address that will be convertedaddress callTo; // the DEX/SWAP contract address that is used for convertingaddress approveode; //an contract address belongs DEX/SWAP that user should grant approvalbytes payload; // the actual token swap calldata for later execution}
The convertToStablecoin function is responsible for executing the transaction to swap fee tokens to stablecoin
functionconvertToStablecoin(Convert[] calldata converts) external {require(converters[msg.sender],"convert deny");require(converts.length >0);for(uint256 i =0; i < converts.length; i++){require(converts[i].callTo.code.length >0,"execute must be contract address");require(!stablecoins[converts[i].token],"not need convert");uint256 balance = Helper._getBalance(converts[i].token,address(this));bool result;if(Helper._isNative(converts[i].token)){ (result,) = converts[i].callTo.call{value:balance}(converts[i].playload); } else { SafeERC20.safeIncreaseAllowance(IERC20(converts[i].token),converts[i].approveTo,balance); (result,) = converts[i].callTo.call(converts[i].playload); }require(result,"convert fail");emitConvertTo(converts[i].token,balance); } }
Claim the converted stablecoin
All the preset payee addresses can claim their generated fees as stablecoins via release function
functionrelease(address token,address account) publicvirtual {require(shares[account] >0,"account has no shares");require(stablecoins[token],"unsuport release token");uint256 payment =releasable(token, account);require(payment !=0,"account is not due payment"); totalReleased[token] += payment;unchecked { payeeReleased[token][account] += payment; } Helper._transfer(token,account,payment);emitPaymentReleased(token, account, payment); }