赞
踩
导入
ethers.js 接口文档 — ethers.js 中文文档 — 深入浅出区块链
- <script>
- const tokens = {
- BUSD: '0xe9e7cea3dedca5984780bafc599bd69add087d56',
- UAK: '0x00e1656e45f18ec6747f5a8496fd39b50b38396d',
- };
- async function getPrice(inputCurrency, outputCurrency){
- const provider = new ethers.providers.JsonRpcProvider('https://bsc-dataseed.binance.org/');
- const contract = {
- factory: '0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73', // PancakeSwap V2 factory
- router: '0x10ED43C718714eb63d5aA57B78B54704E256024E', // PancakeSwap V2 router
- };
-
-
- const router = new ethers.Contract(
- contract.router,
- [{ "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" }],
- provider
- );
-
- const amounts = await router.getAmountsOut(ethers.utils.parseUnits('1', 18), [inputCurrency, outputCurrency]);
- return amounts[1].toString()/1e18;
- }
-
-
- await getPrice(tokens.AFC, tokens.BUSD).then(price => {
- console.log(price);
- });
- </script>

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。