5 web3.py 如何获取pancakeswap其他交易对路由过的兑换数量

web3.py 开发中 (当前交易对)可以用以下代码成功获取 200个coin 兑换 usd 的数量结果

contract_pancake.functions.getAmountsOut(web3.toWei(200, 'ether'),[coin_address,usd_address]).call()

但是当交易对不足的时候,或者不是最优兑换价格的时候,pancakeV2会自动路由到其他的交易对 获得最优的兑换。谁知道如何用web3 获取这种类型的兑换数量呢?

我看到pancakeV2 router里面的有这样一段代码 如何使用?

// performs chained getAmountOut calculations on any number of pairs
function getAmountsOut(address factory, uint amountIn, address[] memory path) internal view returns (uint[] memory amounts) {
    require(path.length >= 2, 'PancakeLibrary: INVALID_PATH');
    amounts = new uint[](path.length);
    amounts[0] = amountIn;
    for (uint i; i < path.length - 1; i++) {
        (uint reserveIn, uint reserveOut) = getReserves(factory, path[i], path[i + 1]);
        amounts[i + 1] = getAmountOut(amounts[i], reserveIn, reserveOut);
    }
}
请先 登录 后评论

最佳答案 2022-03-09 16:12

你第一段代码不就是使用了第二段代码么?

pancakeV2 会对比多个交易对价格,然后去最优价格,这个不是合约是处理的, 而是前端处理的。

请先 登录 后评论

其它 1 个回答

nono
请先 登录 后评论