0

我想使用 brownie 创建像 uniswap 这样的应用程序,并做出反应如何访问我的项目的所有令牌地址和 abi 并在前端使用它。我怎样才能以最佳优化的方式实现这一点?

4

1 回答 1

0

你想要做的是从像 uniswap 这样的代币中获取信息

uniswap没有保存所有现有的代币,这是不可能的事情

每次你在 uniswap 上写一个代币的地址时,它都会向智能合约发出请求,调用现有的函数,这要归功于ERC-20 标准

被调用的函数是

totalSupply() // to get the total supply

decimals() // to get the number of decimals

name() // to get the name of the token (e.g. Bitcoin)

symbol() // to get the symbol of the token (e.g. BTC)

要获取此数据,您必须通过 web3 进行调用,这将返回您请求的数据

// initialize web3
const Web3 = require("web3");

// save only the ABI of the standard, so you can re-use them for all the tokens
// in this ABI you can find only the function totalSupply ()
const ABI = [
     {
         "type": "function",
         "name": "totalSupply",
         "inputs": [],
         "outputs": [{"name": "", "type": "uint256"}],
         "stateMutability": "view",
         "payable": false,
         "constant": true // for backward-compatibility
     }
];

// run the JS function
async function run() {
     const web3 = new Web3(<YourNodeUrl>);
// create the web3 contract
     const contract = new web3.eth.Contract(ABI, <TokenAddress>);
// call the function and get the totalSupply
     const totalSupply = await contract.methods.totalSupply().call();
     console.log(totalSupply);
}
于 2021-10-11T13:37:54.467 回答