我想使用brownie和react创建像uniswap这样的应用程序,我如何才能访问我的项目的所有令牌地址和abi并在前端使用它。我怎样才能以最佳优化的方式实现这一点?
你要做的是获取信息从像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);
}