1swap-contracts's People
1swap-contracts's Issues
Return value of calculateSwapFromBase of StableSwapRouter.sol is incorrect
To reproduct the bug, please do as following steps :
First, try to call calculateSwapFromBase with following parameters:
pool = 0x008db1Cef0958e7f87A107b58F0dede796ce7962 (BUSD meta pool address)
basePool = 0xb578a396e56388CbF398a12Dea9eb6B01b7c777f (USDC/USDT/DAI base pool address)
tokenIndexFrom = 0 (USDC index)
tokenIndexTo = 0 (BUSD index)
dx = 1000000000 (1000 USDC)
Get return value as 998604422682132896156 .
This means : due to the calculateSwapFromBase call response, 1000 USDC can be converted to about 998.6 BUSD
Second, build swapFromBase transaction with following parameters:
pool = 0x008db1Cef0958e7f87A107b58F0dede796ce7962 (BUSD meta pool address)
basePool = 0xb578a396e56388CbF398a12Dea9eb6B01b7c777f (USDC/USDT/DAI base pool address)
tokenIndexFrom = 0 (USDC index)
tokenIndexTo = 0 (BUSD index)
dx = 1000000000 (1000 USDC)
minDy = 998604422682132896156 (value returned by calculateSwapFromBase)
deadLine = 1633077775 (current time + 5 minutes)
It will always yield exception:
{'code': -32603, 'message': 'VM Exception while processing transaction: revert > slippage', 'data': '08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000a3e20736c69707061676500000000000000000000000000000000000000000000'}
It seems that the minDy value returned by calculateSwapFromBase is incorrect, which is bigger than the real amount which can be converted by swapFromBase.
Testing python code can be found below :
import eth_account
from web3 import Web3
import time
w3 = Web3(Web3.HTTPProvider('https://rpc.moonriver.moonbeam.network'))
private_key = 'MY PRIVATE KEY'
wallet_address = eth_account.Account.from_key(private_key = private_key).address
route_address = '0xf741ae23D540AfC30e967cEdc362B93dd1e7b91B'
route_abi = '[{"type":"function","stateMutability":"nonpayable","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"addLiquidity","inputs":[{"type":"address","name":"pool","internalType":"contract IStableSwap"},{"type":"address","name":"basePool","internalType":"contract IStableSwap"},{"type":"uint256[]","name":"meta_amounts","internalType":"uint256[]"},{"type":"uint256[]","name":"base_amounts","internalType":"uint256[]"},{"type":"uint256","name":"minToMint","internalType":"uint256"},{"type":"uint256","name":"deadline","internalType":"uint256"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"calculateConvert","inputs":[{"type":"address","name":"fromPool","internalType":"contract IStableSwap"},{"type":"address","name":"toPool","internalType":"contract IStableSwap"},{"type":"uint256","name":"amount","internalType":"uint256"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"availableTokenAmount","internalType":"uint256"}],"name":"calculateRemoveBaseLiquidityOneToken","inputs":[{"type":"address","name":"pool","internalType":"contract IStableSwap"},{"type":"address","name":"basePool","internalType":"contract IStableSwap"},{"type":"uint256","name":"_token_amount","internalType":"uint256"},{"type":"uint8","name":"iBase","internalType":"uint8"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256[]","name":"meta_amounts","internalType":"uint256[]"},{"type":"uint256[]","name":"base_amounts","internalType":"uint256[]"}],"name":"calculateRemoveLiquidity","inputs":[{"type":"address","name":"pool","internalType":"contract IStableSwap"},{"type":"address","name":"basePool","internalType":"contract IStableSwap"},{"type":"uint256","name":"amount","internalType":"uint256"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"calculateSwapFromBase","inputs":[{"type":"address","name":"pool","internalType":"contract IStableSwap"},{"type":"address","name":"basePool","internalType":"contract IStableSwap"},{"type":"uint8","name":"tokenIndexFrom","internalType":"uint8"},{"type":"uint8","name":"tokenIndexTo","internalType":"uint8"},{"type":"uint256","name":"dx","internalType":"uint256"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"calculateSwapToBase","inputs":[{"type":"address","name":"pool","internalType":"contract IStableSwap"},{"type":"address","name":"basePool","internalType":"contract IStableSwap"},{"type":"uint8","name":"tokenIndexFrom","internalType":"uint8"},{"type":"uint8","name":"tokenIndexTo","internalType":"uint8"},{"type":"uint256","name":"dx","internalType":"uint256"}]},{"type":"function","stateMutability":"view","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"calculateTokenAmount","inputs":[{"type":"address","name":"pool","internalType":"contract IStableSwap"},{"type":"address","name":"basePool","internalType":"contract IStableSwap"},{"type":"uint256[]","name":"meta_amounts","internalType":"uint256[]"},{"type":"uint256[]","name":"base_amounts","internalType":"uint256[]"},{"type":"bool","name":"is_deposit","internalType":"bool"}]},{"type":"function","stateMutability":"nonpayable","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"convert","inputs":[{"type":"address","name":"fromPool","internalType":"contract IStableSwap"},{"type":"address","name":"toPool","internalType":"contract IStableSwap"},{"type":"uint256","name":"amount","internalType":"uint256"},{"type":"uint256","name":"minToMint","internalType":"uint256"},{"type":"uint256","name":"deadline","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"removeBaseLiquidityOneToken","inputs":[{"type":"address","name":"pool","internalType":"contract IStableSwap"},{"type":"address","name":"basePool","internalType":"contract IStableSwap"},{"type":"uint256","name":"_token_amount","internalType":"uint256"},{"type":"uint8","name":"i","internalType":"uint8"},{"type":"uint256","name":"_min_amount","internalType":"uint256"},{"type":"uint256","name":"deadline","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[{"type":"uint256[]","name":"amounts","internalType":"uint256[]"},{"type":"uint256[]","name":"base_amounts","internalType":"uint256[]"}],"name":"removeLiquidity","inputs":[{"type":"address","name":"pool","internalType":"contract IStableSwap"},{"type":"address","name":"basePool","internalType":"contract IStableSwap"},{"type":"uint256","name":"_amount","internalType":"uint256"},{"type":"uint256[]","name":"min_amounts_meta","internalType":"uint256[]"},{"type":"uint256[]","name":"min_amounts_base","internalType":"uint256[]"},{"type":"uint256","name":"deadline","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"swapFromBase","inputs":[{"type":"address","name":"pool","internalType":"contract IStableSwap"},{"type":"address","name":"basePool","internalType":"contract IStableSwap"},{"type":"uint8","name":"tokenIndexFrom","internalType":"uint8"},{"type":"uint8","name":"tokenIndexTo","internalType":"uint8"},{"type":"uint256","name":"dx","internalType":"uint256"},{"type":"uint256","name":"minDy","internalType":"uint256"},{"type":"uint256","name":"deadline","internalType":"uint256"}]},{"type":"function","stateMutability":"nonpayable","outputs":[{"type":"uint256","name":"","internalType":"uint256"}],"name":"swapToBase","inputs":[{"type":"address","name":"pool","internalType":"contract IStableSwap"},{"type":"address","name":"basePool","internalType":"contract IStableSwap"},{"type":"uint8","name":"tokenIndexFrom","internalType":"uint8"},{"type":"uint8","name":"tokenIndexTo","internalType":"uint8"},{"type":"uint256","name":"dx","internalType":"uint256"},{"type":"uint256","name":"minDy","internalType":"uint256"},{"type":"uint256","name":"deadline","internalType":"uint256"}]}]'
route_contract = w3.eth.contract(route_address, abi = route_abi)
metapool_address = '0x008db1Cef0958e7f87A107b58F0dede796ce7962'
basepool_address = '0xb578a396e56388CbF398a12Dea9eb6B01b7c777f'
min_dy = route_contract.functions.calculateSwapFromBase(metapool_address, basepool_address, 0, 0, 1000000000).call()
print(min_dy)
try :
txn_contract = route_contract.functions.swapFromBase(metapool_address, basepool_address, 0, 0, 1000000000, min_dy, int(time.time()) + 300)
txn_dict = txn_contract.buildTransaction({'from' : wallet_address, 'nonce' : w3.eth.get_transaction_count(wallet_address)})
except Exception as e:
print(e)
pass
min_dy = route_contract.functions.calculateSwapFromBase(metapool_address, basepool_address, 0, 0, 1000000000).call()
print(min_dy)
Test Output:
998604422682132896156
{'code': -32603, 'message': 'VM Exception while processing transaction: revert > slippage', 'data': '08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000a3e20736c69707061676500000000000000000000000000000000000000000000'}
998604422682132896156
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.