当前位置:   article > 正文

获取PancakeSwap Price_php获取pancakeswap山寨币价格

php获取pancakeswap山寨币价格

导入

ethers.js 接口文档 — ethers.js 中文文档 — 深入浅出区块链

  1. <script>
  2. const tokens = {
  3. BUSD: '0xe9e7cea3dedca5984780bafc599bd69add087d56',
  4. UAK: '0x00e1656e45f18ec6747f5a8496fd39b50b38396d',
  5. };
  6. async function getPrice(inputCurrency, outputCurrency){
  7. const provider = new ethers.providers.JsonRpcProvider('https://bsc-dataseed.binance.org/');
  8. const contract = {
  9. factory: '0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73', // PancakeSwap V2 factory
  10. router: '0x10ED43C718714eb63d5aA57B78B54704E256024E', // PancakeSwap V2 router
  11. };
  12. const router = new ethers.Contract(
  13. contract.router,
  14. [{ "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" }],
  15. provider
  16. );
  17. const amounts = await router.getAmountsOut(ethers.utils.parseUnits('1', 18), [inputCurrency, outputCurrency]);
  18. return amounts[1].toString()/1e18;
  19. }
  20. await getPrice(tokens.AFC, tokens.BUSD).then(price => {
  21. console.log(price);
  22. });
  23. </script>

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
  

闽ICP备14008679号