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);
}
}