How to interact with OpenZeppelin proxy contracts from Truffle console?

1.9k views Asked by At

I cannot figure out how to use @truffle/contract to interact with contracts deployed by package @openzeppelin/truffle-upgrades (ProxyAdmin and AdminUpgradeabilityProxy). I have deployed my contracts to 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

This is the code that I've tried:

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()

But it throws an error:

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)

Network is the same (Ganache).

What am I doing wrong?

2

There are 2 answers

0
Santiago Palladino On BEST ANSWER

The deployed() method from truffle-contract will look for the deployment address stored in the contract artifact (ie the DeployJson in your code). This value is only set if you deploy your contract via a migration, and most importantly, it's set on the artifact of the implementation contract. In this case, it would be the MetaToken.

To interact with the deployed instance, you'd need to do something like:

const MetaToken = artifacts.require('MetaToken');
const instance = await MetaToken.deployed();

Or, to bypass the deployed() mechanic and attaching directly:

const MetaToken = artifacts.require('MetaToken');
const instance = await MetaToken.at('0x2FCe57853cB98fCd6491ddebc8430E970CA333b5');

This will return an instance of a truffle-contract at the proxy address (0x2FCe57853cB98fCd6491ddebc8430E970CA333b5) with the MetaToken ABI, which is what you probably want.

If you want to interact with the Proxy to upgrade it or change the owner, you should do so through the upgrades plugin methods.

0
kivi_o On

Andrew B Coathup from OpenZeppelin helped to get this right. The trick is to copy built artifacts of ProxyAdmin and AdminUpgradeabilityProxy from npm package into the truffle project.

cp node_modules/@openzeppelin/upgrades-core/artifacts/* build/contracts

Then the truffle console will be able to load those, so you can interact them like usual:

let proxyAdmin = await ProxyAdmin.at("0x254dffcd3277C0b1660F6d42EFbB754edaBAbC2B")
proxyAdmin.owner()

Visit this topic for additional info:

https://forum.openzeppelin.com/t/how-to-interact-with-proxy-and-proxyadmin-on-truffle-console/3964/2