blockchain - 我怎样才能让巧克力蛋糕中的所有 ERC20 地址在前端使用
问题描述
我想使用 brownie 创建像 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);
}
推荐阅读
- python-3.x - Python 3.7.3,shutil 导入 copy2 代码无法正常工作。怎么修?
- java - 无法在 Nativescript 和外部设备 SDK 25、Android 7.1.2 中使用 SQLite
- c++ - 访问模板类的非模板库的静态数据
- java - 在模块中使用第三方库未在 bintray 上上传
- spring - Spring Boot 无法从 application.yml 正确加载配置文件
- php - 更改 null laravel 上的请求值
- php - 获取具有相同父项的子类别中的子类别列表
- android - 尝试包含标准库中的 android 布局的数据绑定错误
- sql - How to ignore the null values in the first date?
- node.js - How read data by Dialogflow on Firebase