我已经看到,getStorageKeys
或parity_listStorageKeys
函数已经从最流行的客户端停止使用。因此,开发人员只能访问getStorageAt
(我正在使用etheres.js
)。是否有一种方法可以自动从特定的智能契约中获取索引值的所有键?一种可能是先验地知道哪些值是定义和使用getStorageAt
的.然而,对于动态大小的类型,这将包括在循环中使用getStorageAt
,直到只检索到零,而且这看起来不太有效。
有什么想法吗?
发布于 2023-01-13 00:12:15
因此,在https://github.com/0xweb-org/0xweb库中,我为合同的存储阅读器添加了TypeScript代码生成。只有当源代码可用时,它才能工作,例如,合同是有效的。
npm i 0xweb -g
0xweb init
0xweb install 0x1234... --name FooContract --chain eth
它生成契约的客户端类来调用读/写方法,但它也有storage
字段,其中包含所有存储变量的getter。它也适用于结构和动态内容,如数组和映射。
contract FooContract {
uint256 a;
string b;
struct Foo {
uint256 c;
uint256 d;
}
Foo foo;
address[] list;
mapping(uint256 => address) people;
}
import { FooContract } from '@0xweb/eth/FooContract/FooContract'
import { Config } from '@dequanto/Config'
let contract = new FooContract();
let aValue = await contract.storage.a();
let bValue = await contract.storage.b();
// from struct
let cValue = await contract.storage.foo('c');
// or the first item of the array
let arrItem = await contract.storage.list(0);
// mapping
let mappingValue = await contract.storage.people(100n);
https://ethereum.stackexchange.com/questions/142841
复制相似问题