如何从 Truffle 控制台与 OpenZeppelin 代理合约进行交互?
Posted
技术标签:
【中文标题】如何从 Truffle 控制台与 OpenZeppelin 代理合约进行交互?【英文标题】:How to interact with OpenZeppelin proxy contracts from Truffle console? 【发布时间】:2021-01-13 14:15:32 【问题描述】:我不知道如何使用 @truffle/contract 与包 @openzeppelin/truffle-upgrades (ProxyAdmin 和 AdminUpgradeabilityProxy)部署的合约进行交互。我已将合约部署到 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)。
我做错了什么?
【问题讨论】:
【参考方案1】:来自truffle-contract
的deployed()
方法将查找存储在合约工件(即代码中的DeployJson)中的部署地址。仅当您通过迁移部署合同时才设置此值,最重要的是,它设置在实施合同的工件上。在这种情况下,它将是MetaToken
。
要与部署的实例交互,您需要执行以下操作:
const MetaToken = artifacts.require('MetaToken');
const instance = await MetaToken.deployed();
或者,绕过deployed()
机制并直接附加:
const MetaToken = artifacts.require('MetaToken');
const instance = await MetaToken.at('0x2FCe57853cB98fCd6491ddebc8430E970CA333b5');
这将使用 MetaToken ABI 在代理地址 (0x2FCe57853cB98fCd6491ddebc8430E970CA333b5
) 处返回一个 truffle-contract
的实例,这可能是您想要的。
如果您想与代理进行交互以对其进行升级或更改所有者,您应该通过upgrades plugin methods 进行。
【讨论】:
【参考方案2】:来自 OpenZeppelin 的 Andrew B Coathup 帮助解决了这个问题。诀窍是将 ProxyAdmin 和 AdminUpgradeabilityProxy 的构建工件从 npm 包复制到 truffle 项目中。
cp node_modules/@openzeppelin/upgrades-core/artifacts/* build/contracts
然后松露控制台将能够加载这些,所以你可以像往常一样交互它们:
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
【讨论】:
以上是关于如何从 Truffle 控制台与 OpenZeppelin 代理合约进行交互?的主要内容,如果未能解决你的问题,请参考以下文章
无法使用 Ganache GUI 连接到 Truffle 或 testrpc