Skip to content

Instantly share code, notes, and snippets.

@0xtuytuy
Last active March 8, 2022 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xtuytuy/b74011d4d0c2343179935ab88266376e to your computer and use it in GitHub Desktop.
Save 0xtuytuy/b74011d4d0c2343179935ab88266376e to your computer and use it in GitHub Desktop.
Alluo - Extract of voteExecutor.sol main function
function execute(Entry[] memory _entries) external onlyRole(DEFAULT_ADMIN_ROLE){
uint8 totalWeight;
for(uint256 i = 0; i < _entries.length; i++){
totalWeight += _entries[i].weight;
require(entryTokens.contains(_entries[i].entryToken), "There is no such entry token");
}
require(totalWeight <= 100, "Total weight more then 100");
uint256 totalBalance = getTotalBalance();
for(uint256 i = 0; i < _entries.length; i++){
Entry memory entry = _entries[i];
uint256 amount = entry.weight * totalBalance / 100;
// Stablecoins have different decimals, so we need to have one base
// (18 decimals) for calculation at each step
uint256 entryDecimalsMult = 10**(18 - ERC20(entry.entryToken).decimals());
uint256 poolDecimalsMult = 10**(18 - ERC20(entry.poolToken).decimals());
uint256 actualAmount = IERC20(entry.entryToken).balanceOf(address(this)) * entryDecimalsMult;
// if entry token not enough contact should exchange other stablecoins
if(actualAmount < amount){
uint256 amountLeft = amount - actualAmount;
uint256 maxLoop = entryTokens.length();
while(amountLeft > 0 && maxLoop != 0){
maxLoop--;
(address helpToken, uint256 helpAmount) = findBiggest(entry.entryToken);
if(amountLeft <= helpAmount){
uint256 exchangeAmountIn = amountLeft / 10**(18 - ERC20(helpToken).decimals());
uint256 exchangeAmountOut = amountLeft / entryDecimalsMult;
actualAmount += IExchange(exchangeAddress).exchange(
helpToken,
entry.entryToken,
exchangeAmountIn,
exchangeAmountOut * (100 - slippage) / 100
) * entryDecimalsMult;
amountLeft = 0;
}
else{
uint256 exchangeAmountIn = helpAmount / 10**(18 - ERC20(helpToken).decimals());
uint256 exchangeAmountOut = helpAmount / entryDecimalsMult;
actualAmount += IExchange(exchangeAddress).exchange(
helpToken,
entry.entryToken,
exchangeAmountIn,
exchangeAmountOut * (100 - slippage) / 100
) * entryDecimalsMult;
amountLeft -= helpAmount;
}
}
amount = actualAmount;
}
// final exchange before curve if needed
if(entry.entryToken != entry.poolToken){
amount = IExchange(exchangeAddress).exchange(
entry.entryToken,
entry.poolToken,
amount / entryDecimalsMult,
0
);
}
else{
amount = amount / poolDecimalsMult;
}
uint256[4] memory arrAmounts;
arrAmounts[entry.tokenIndexInCurve] = amount;
IERC20[4] memory arrTokens;
arrTokens[entry.tokenIndexInCurve] = IERC20(entry.poolToken);
// entering curve with all amount of pool tokens
IERC20(entry.poolToken).safeTransfer(strategyDeployer, amount);
[...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment