【发布时间】:2021-11-30 05:31:02
【问题描述】:
我想使用 brownie 创建像 uniswap 这样的应用程序,并反应如何访问我的项目的所有令牌地址和 abi 并在前端使用它。我怎样才能以最佳优化的方式实现这一点?
【问题讨论】:
-
所有的令牌是哪一个?所有现有的?
-
是的活动令牌
标签: blockchain ethereum brownie dapp
我想使用 brownie 创建像 uniswap 这样的应用程序,并反应如何访问我的项目的所有令牌地址和 abi 并在前端使用它。我怎样才能以最佳优化的方式实现这一点?
【问题讨论】:
标签: blockchain ethereum brownie dapp
你想要做的是从像 uniswap 这样的 Token 中获取信息
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);
}
【讨论】: