xclud / web3dart

Ethereum library, written in Dart.
https://pub.dev/packages/web3dart
MIT License
170 stars 94 forks source link

How to Get price from pancakeSwap through contract call #59

Open zenshindm opened 1 year ago

zenshindm commented 1 year ago

appreciate your help , can anyone point me to the right direction for getting price from pancakeswap by calling contract ? BNB in USD i try to get BNB in USD using USDT

I try to call the pancakeswap contract then feed it with BNB and USDT contract address and i get this error Unhandled Exception: type 'String' is not a subtype of type 'BigInt' of 'data'

hope anyone can point to right resource or direction

Cheers

here is my code


final pancakeSwapContract = '0x10ED43C718714eb63d5aA57B78B54704E256024E';
  late Client httpClient;
  late Web3Client ethereumClient;
  String ethereumClientUrl = 'https://bsc-dataseed1.binance.org';
  final bnbTokenAddress = "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c";
  // hedgepay token
  final tokenAddres = '0xC75aa1Fa199EaC5adaBC832eA4522Cff6dFd521A'; 
  final privateKey = '53fc84b207d9d1a16f412b7d701ea46cf7a9b55be337a1b2be5385d5b3be6f52';
  int getAmountsOut = 0;

  @override
  void initState() {
    super.initState();
    httpClient = Client();
    ethereumClient = Web3Client(ethereumClientUrl, httpClient);
  }
  Future<DeployedContract> getContract() async {
    String abi = await rootBundle.loadString("assets/abi/pancakeSwapAbi.json");
    String pancakeSwapConAddress = "0x10ED43C718714eb63d5aA57B78B54704E256024E";

    DeployedContract contract = DeployedContract(
      ContractAbi.fromJson(abi, 'pancakeSwapAbi'),
      EthereumAddress.fromHex(pancakeSwapConAddress),
  );

    return contract;
  }

  Future<List<dynamic>> query(String functionName, List<dynamic> args) async {
    DeployedContract contract = await getContract();
    ContractFunction function = contract.function(functionName);
    List<dynamic> result = await ethereumClient.call(
        contract: contract, function: function, params: args);
    return result;
  }

  Future<String> transaction(String functionName, List<dynamic> args) async {
    EthPrivateKey credential = EthPrivateKey.fromHex(privateKey);
    DeployedContract contract = await getContract();
    ContractFunction function = contract.function(functionName);
    dynamic result = await ethereumClient.sendTransaction(
      credential,
      Transaction.callContract(
        contract: contract,
        function: function,
        parameters: args,
      ),
      fetchChainIdFromNetworkId: true,
      chainId: null,
    );

    return result;
  }

  Future<void> calcBNBPrice() async {
    String bNBTokenAddress = "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c"; //BNB
    String uSDTokenAddress  = "0x55d398326f99059fF775485246999027B3197955"; //USDT
    List<dynamic> result = await query('getAmountsOut', [bNBTokenAddress, uSDTokenAddress]); //i think here went wrong 

    getAmountsOut = int.parse(result[0].toString());
    print(getAmountsOut);
  }

and here is my abi


[
  {
    "inputs": [
      { "internalType": "uint256", "name": "amountIn", "type": "uint256" },
      { "internalType": "address[]", "name": "path", "type": "address[]" }
    ],
    "name": "getAmountsOut",
    "outputs": [
      { "internalType": "uint256[]", "name": "amounts", "type": "uint256[]" }
    ],
    "stateMutability": "view",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "decimals",
    "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
    "stateMutability": "view",
    "type": "function"
  }
]
mopilo commented 1 year ago

@zenshindm were you able to solve this?