Skip to content

Commit

Permalink
improve default getExpectedIn (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyennk92 authored Jul 28, 2021
1 parent 5e84a29 commit 0209b9e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
25 changes: 17 additions & 8 deletions contracts/SmartWalletImplementation.sol
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ contract SmartWalletImplementation is SmartWalletStorage, ISmartWalletImplementa
srcDecimal = srcDecimal - 3;
}
srcAmount = 1 * (10**srcDecimal); // Use a 0.001 as base
uint256 lastSrcAmount = 0;
uint256 destAmount = 0;
uint256 lastGoodSrcAmount = 0;
for (uint256 i = 0; i < 10; i++) {
try
ISwap(swapContract).getExpectedReturn(
Expand All @@ -172,21 +171,31 @@ contract SmartWalletImplementation is SmartWalletStorage, ISmartWalletImplementa
)
returns (uint256 newDestAmount) {
if (newDestAmount != 0) {
destAmount = newDestAmount;
(lastSrcAmount, srcAmount) = (srcAmount, (srcAmount * params.destAmount) / newDestAmount);
(lastGoodSrcAmount, srcAmount) = (
srcAmount,
(srcAmount * params.destAmount) / newDestAmount
);
continue;
}
} catch {}
// If there's an error or newDestAmount == 0, try something closer to lastSrcAmount
(lastSrcAmount, srcAmount) = (srcAmount, (srcAmount + lastSrcAmount) / 2);
// If there's an error or newDestAmount == 0, try something closer to lastGoodSrcAmount
srcAmount = (srcAmount + lastGoodSrcAmount) / 2;
}

// Precision check
uint256 destAmount = ISwap(swapContract).getExpectedReturn(
ISwap.GetExpectedReturnParams({
srcAmount: srcAmount,
tradePath: params.tradePath,
feeBps: params.feeBps,
extraArgs: params.extraArgs
})
);
uint256 diff;
if (destAmount > params.destAmount) {
diff = destAmount - params.destAmount;
diff = destAmount - params.destAmount;
} else {
diff = params.destAmount - destAmount;
diff = params.destAmount - destAmount;
}

// Telerate a 5% difference
Expand Down
2 changes: 1 addition & 1 deletion scripts/config_eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export const EthConfig: Record<string, IConfig> = {

tokens: [{symbol: 'dai', address: '0xad6d458402f60fd3bd25163575031acdce07538d'}],

wNative: '0x0a180a76e4466bf68a7f86fb029bed3cccfaaac5',
wNative: '0xc778417e063141139fce010982780140aa0cd5ab',

// Uniswap & clones
uniswap: {
Expand Down

0 comments on commit 0209b9e

Please sign in to comment.