20 从pancakeswap获取代币的交易价格,通过调用getAmountsOut()方法

因为我想每次获取一批代币的交易价格,所以我暂时想到的是在solidity中批量调用pancakeswap的getAmountsOut()方法,目的是想减少rpc调用的次数,但是本人不太熟悉solidity,下面是我的一段solidity代码,然后我用web3.js调用报错 `// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.8.2 <0.9.0;

interface Pancakeswap {

function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);

}

contract EnhanceSwap {

address constant private pancakeswapAddress = 0x10ED43C718714eb63d5aA57B78B54704E256024E;

enum TradeType {
    BUY,
    SELL
}

struct AmountOutEntity {
    address amountOutAddress;
    uint amountIn;
    address[] path;
    TradeType tradeType;
}

struct AmountOutResult {
    uint amountIn;
    uint[] amountOut;
    address amountOutAddress;
    TradeType tradeType;
}

/**
 * 批量处理多个代币合约兑换数量
 **/
function getAmountsOutForBatch(AmountOutEntity[] memory amountOutEntity) external view returns (AmountOutResult[] memory amounts){
    //用来存储多个结果
    AmountOutResult[] memory amountOutResults = new AmountOutResult[](amountOutEntity.length);

    if (amountOutEntity.length == 0) {
        return amountOutResults;
    }

    uint[] memory zeroAmountOuts;
    zeroAmountOuts[0] = 0;

    //从pancakeswap获取数据
    for(uint i = 0; i &lt; amountOutEntity.length; i++) {
        try  Pancakeswap(pancakeswapAddress).getAmountsOut(amountOutEntity[i].amountIn, amountOutEntity[i].path) returns(uint[] memory results) {
            AmountOutResult memory aor = AmountOutResult(
                amountOutEntity[i].amountIn,
                results,
                amountOutEntity[i].amountOutAddress,
                amountOutEntity[i].tradeType
            );
            amountOutResults[i] = aor;
        }catch Error(string memory) {
            AmountOutResult memory aor = AmountOutResult(
                amountOutEntity[i].amountIn,
                zeroAmountOuts,
                amountOutEntity[i].amountOutAddress,
                amountOutEntity[i].tradeType
            );
            amountOutResults[i] = aor;
        }catch {
            AmountOutResult memory aor = AmountOutResult(
                amountOutEntity[i].amountIn,
                zeroAmountOuts,
                amountOutEntity[i].amountOutAddress,
                amountOutEntity[i].tradeType
            );
            amountOutResults[i] = aor;
        }
    }
    return amountOutResults;
}

}` 麻烦大佬有空指点一下,或者提供一下别的思路,感谢~ 我的目的就是想一批批的获取代币价格

请先 登录 后评论

3 个回答

曾小仲
请先 登录 后评论
Jeack
请先 登录 后评论
i am duck - 划水
请先 登录 后评论
  • 3 关注
  • 0 收藏,3616 浏览
  • 提出于 2023-02-13 10:14