如何从 Truffle 控制台与 OpenZeppelin 代理合约进行交互?

How to interact with OpenZeppelin proxy contracts from Truffle console?

我不知道如何使用 @truffle/contract 与包 @openzeppelin/truffle-upgrades[=25= 部署的合约进行交互](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)。

我做错了什么?

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');

这将 return 代理地址 (0x2FCe57853cB98fCd6491ddebc8430E970CA333b5) 的一个 truffle-contract 实例与 MetaToken ABI,这可能是您想要的。

如果您想与 Proxy 交互以升级它或更改所有者,您应该通过 upgrades plugin methods

来自 OpenZeppelin 的 Andrew B Coathup 帮助实现了这一目标。诀窍是将 ProxyAdmin 和 AdminUpgradeabilityProxy 的构建工件从 npm 包复制到松露项目中。

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