- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在一个场景中,钱包A 正在接收 代币B 定期从 地址C .
AddressC 只发送 TokenB,没有别的。
在 etherscan 或 bscscan 中,很容易查看 WalletA 中收到了多少 TokenB,并且“来自”字段在那里,因此您可以进行一些数学计算以获得总数。
如何使用 web3 做到这一点?我在 web3 文档中找不到任何相关的 api 调用。
我可以通过 web3.js 在 WalletA 中获得 TokenB 的总余额,但我需要 token 数量 只有从地址C发送。
谢谢。
最佳答案
根据 ERC-20标准,每次 token 转移发出 Transfer()
事件日志,包含发件人地址、收件人地址和代币数量。
您可以使用 web3js
获取过去的事件日志。一般方法web3.eth.getPastLogs() ,编码输入并解码输出。
或者您可以提供合约的 ABI JSON(在这种情况下仅使用 Transfer()
事件定义就足够了)并使用 web3js
方法 web3.eth.Contract.getPastEvents() ,它根据提供的 ABI JSON 为您编码输入并解码输出。
const Web3 = require('web3');
const web3 = new Web3('<provider_url>');
const walletA = '0x3cd751e6b0078be393132286c442345e5dc49699'; // sender
const tokenB = '0xdAC17F958D2ee523a2206206994597C13D831ec7'; // token contract address
const addressC = '0xd5895011F887A842289E47F3b5491954aC7ce0DF'; // receiver
// just the Transfer() event definition is sufficient in this case
const abiJson = [{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}];
const contract = new web3.eth.Contract(abiJson, tokenB);
const fromBlock = 10000000;
const toBlock = 13453500;
const blockCountIteration = 5000;
const run = async () => {
let totalTokensTranferred = 0;
for (let i = fromBlock; i <= (toBlock - blockCountIteration); i += blockCountIteration) {
//console.log("Requesting from block", i, "to block ", i + blockCountIteration - 1);
const pastEvents = await contract.getPastEvents('Transfer', {
'filter': {
'from': walletA,
'to': addressC,
},
'fromBlock': i,
'toBlock': i + blockCountIteration - 1,
});
}
for (let pastEvent of pastEvents) {
totalTokensTranferred += parseInt(pastEvent.returnValues.value);
}
console.log(totalTokensTranferred);
}
run();
关于web3js - 使用 Web3.js 获取从特定地址收到的 token 总量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69602206/
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: C# - How do you get total amount of RAM the computer h
使用 C#,我想获取我的计算机拥有的 RAM 总量。使用 PerformanceCounter,我可以通过设置获得可用内存的数量: counter.CategoryName = "Memory"; c
这里我要计算PF,我们每个月都要扣pf,现在我要显示pf的总金额,这是我的数据库表结构 id first_name pf_amount pf_month badge_number
有没有办法找出我的系统使用 C 的 RAM 总量?我在 Ubuntu 12.04 上。我需要用 C 编写一个应用程序,理想情况下应该在运行时查询 RAM 总量。 最佳答案 在 Linux 上,这可以从
在一个场景中,钱包A 正在接收 代币B 定期从 地址C . AddressC 只发送 TokenB,没有别的。 在 etherscan 或 bscscan 中,很容易查看 WalletA 中收到了多少
在我们的 Spring Boot Web 服务器中,我知道如何限制每个用户的 session (httpSecurity.sessionManagement().maximumSessions()),
我在 CosmosDB 中为我的集合设置了 400 RU/s。我想估计 24 小时内 RU/s 的最大总数。请告诉我,我该如何计算? 计算正确吗?24 小时内 400 * 60 * 60 * 24 =
我只想分享我为增强 docker stats 命令而制作的一个小脚本。我不确定这种方法的准确性。 我可以假设整个 Docker 部署消耗的内存总量是每个容器消耗内存的总和吗? 请分享您的修改和/或更正
我是一名优秀的程序员,十分优秀!