作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不知道如何使用 @truffle/contract 与包 @openzeppelin/truffle-upgrades(ProxyAdmin 和 AdminUpgradeabilityProxy)部署的契约(Contract)进行交互。我已将我的合约部署到 Ganache:
Deploying 'MetaToken'
---------------------
> transaction hash: 0xac159783380d929e6de5eb9f2a8cd90146997d340aa6ac1dd0762a97ae3a7379
> Blocks: 0 Seconds: 0
> contract address: 0x5aab2dF75BeB13891a640B193C823bE02F11f752
> block number: 3
> block timestamp: 1601252702
> account: 0x43076f318858988550F85Ec308FfC832253f8c9E
> balance: 99.96601856
> gas used: 1464575 (0x1658ff)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.0292915 ETH
Deploying 'ProxyAdmin'
----------------------
> transaction hash: 0xd1d9d3d2272ef3611b66dbf96fe8eaa8ccbc16f595e478f544b657c244f2e33d
> Blocks: 0 Seconds: 0
> contract address: 0x34De0046a5FbA24b9aFd32687990e60517FE95F6
> block number: 4
> block timestamp: 1601252703
> account: 0x43076f318858988550F85Ec308FfC832253f8c9E
> balance: 99.94841932
> gas used: 879962 (0xd6d5a)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.01759924 ETH
Deploying 'AdminUpgradeabilityProxy'
------------------------------------
> transaction hash: 0x4e0a725865e6ce322b3479623c07e8a05c2cacdc2e5025b2f400972a5d12b43d
> Blocks: 0 Seconds: 0
> contract address: 0x2FCe57853cB98fCd6491ddebc8430E970CA333b5
> block number: 5
> block timestamp: 1601252704
> account: 0x43076f318858988550F85Ec308FfC832253f8c9E
> balance: 99.93331538
> gas used: 755197 (0xb85fd)
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.01510394 ETH
这是我试过的代码:
Web3 = require("web3")
var provider = new Web3.providers.HttpProvider("http://localhost:7545");
var contract = require("@truffle/contract");
var fs=require("fs");
var DeployJson=fs.readFileSync("/path/metatoken/node_modules/@openzeppelin/upgrades-core/artifacts/AdminUpgradeabilityProxy.json");
var DeployJs=JSON.parse(DeployJson);
var AdminUpgradeabilityProxy=contract({abi: DeployJs.abi, unlinked_binary: DeployJs.bytecode, address: "0x2FCe57853cB98fCd6491ddebc8430E970CA333b5"});
AdminUpgradeabilityProxy.setProvider(provider);
let i = await AdminUpgradeabilityProxy.deployed()
但是它抛出一个错误:
Error: Contract has not been deployed to detected network (network/artifact mismatch)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at Function.deployed (/path/metatoken/node_modules/@truffle/contract/lib/contract/constructorMethods.js:84:11)
at Object.checkNetworkArtifactMatch (/path/metatoken/node_modules/@truffle/contract/lib/utils/index.js:249:13)
网络是一样的(Ganache)。
我做错了什么?
最佳答案
来自 OpenZeppelin 的 Andrew B Coathup 帮助实现了这一目标。诀窍是将 ProxyAdmin 和 AdminUpgradeabilityProxy 的构建工件从 npm 包复制到 truffle 项目中。
cp node_modules/@openzeppelin/upgrades-core/artifacts/* build/contracts
然后 truffle 控制台将能够加载它们,因此您可以像往常一样与它们交互:
let proxyAdmin = await ProxyAdmin.at("0x254dffcd3277C0b1660F6d42EFbB754edaBAbC2B")
proxyAdmin.owner()
访问此主题以获取更多信息:
https://forum.openzeppelin.com/t/how-to-interact-with-proxy-and-proxyadmin-on-truffle-console/3964/2
关于ethereum - 如何从 Truffle 控制台与 OpenZeppelin 代理合约进行交互?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64094691/
我是一名优秀的程序员,十分优秀!