Skip to content

Commit

Permalink
faster command switching
Browse files Browse the repository at this point in the history
  • Loading branch information
Okavango committed Oct 25, 2022
1 parent c214787 commit 0cf5f64
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
61 changes: 33 additions & 28 deletions contracts/RouteProcessor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,39 @@ contract RouteProcessor {
uint position = 0; // current reading position in route
while(position < route.length) {
uint8 commandCode = uint8(route[position]);
if (commandCode == 3) { // distribute ERC20 tokens from msg.sender to an address
uint transferAmount;
(transferAmount, position) = distributeERC20Amounts(tokenIn, route, position + 1);
amountInAcc += transferAmount;
} else if (commandCode == 4) { // distribute ERC20 tokens from this router to an address
position = distributeERC20Shares(route, position + 1);

} else if (commandCode == 10) { // Sushi/Uniswap pool swap
(, position) = swapUniswapPool(route, position + 1);

} else if (commandCode == 20) {
position = bentoDepositAmountFromBento(tokenIn, route, position + 1);
} else if (commandCode == 21) {
position = swapTrident(route, position + 1);
} else if (commandCode == 23) {
position = bentoWithdrawShareFromRP(tokenIn, route, position + 1);
} else if (commandCode == 24) { // distribute Bento tokens from msg.sender to an address
uint transferAmount;
(transferAmount, position) = distributeBentoShares(tokenIn, route, position + 1);
amountInAcc += transferAmount;
} else if (commandCode == 25) {
position = distributeBentoPortions(route, position + 1);
} else if (commandCode == 26) {
position = bentoDepositAllFromBento(route, position + 1);
} else if (commandCode == 27) {
position = bentoWithdrawAllFromRP(route, position + 1);

} else revert("Unknown command code");
if (commandCode < 20) {
if (commandCode == 10) { // Sushi/Uniswap pool swap
(, position) = swapUniswapPool(route, position + 1);
} else if (commandCode == 3) { // distribute ERC20 tokens from msg.sender to an address
uint transferAmount;
(transferAmount, position) = distributeERC20Amounts(tokenIn, route, position + 1);
amountInAcc += transferAmount;
} else if (commandCode == 4) { // distribute ERC20 tokens from this router to an address
position = distributeERC20Shares(route, position + 1);
} else revert("Unknown command code");
} else {
if (commandCode < 24) {
if (commandCode == 20) {
position = bentoDepositAmountFromBento(tokenIn, route, position + 1);
} else if (commandCode == 21) {
position = swapTrident(route, position + 1);
} else if (commandCode == 23) {
position = bentoWithdrawShareFromRP(tokenIn, route, position + 1);
} else revert("Unknown command code");
} else {
if (commandCode == 24) { // distribute Bento tokens from msg.sender to an address
uint transferAmount;
(transferAmount, position) = distributeBentoShares(tokenIn, route, position + 1);
amountInAcc += transferAmount;
} else if (commandCode == 25) {
position = distributeBentoPortions(route, position + 1);
} else if (commandCode == 26) {
position = bentoDepositAllFromBento(route, position + 1);
} else if (commandCode == 27) {
position = bentoWithdrawAllFromRP(route, position + 1);
} else revert("Unknown command code");
}
}
}

require(amountInAcc == amountIn, "Wrong amountIn value");
Expand Down
4 changes: 2 additions & 2 deletions test/RouteProcessor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ async function testRouteProcessor(net: Network, amountIn: number, toToken: Token

console.log("6. Create Route ...")
console.log(` Input: ${amountIn} ${route.fromToken.name}`);
route.legs.forEach(l => {
route.legs.forEach((l, i) => {
console.log(
` ${l.tokenFrom.name} ${Math.round(l.absolutePortion*100)}%`
` ${i+1}. ${l.tokenFrom.name} ${Math.round(l.absolutePortion*100)}%`
+ ` -> [${swapper.getPoolsProviderName(l.poolAddress)}] -> ${l.tokenTo.name}`);
})
const output = Math.round(parseInt(route.amountOutBN.toString())/Math.pow(10, toToken.decimals)*100)/100
Expand Down

0 comments on commit 0cf5f64

Please sign in to comment.