Hello!
I followed your docs and managed to advance quite a bit. I got stuck while trying to programmatically access the ERC20PresetMinterPauserUpgradeSafe I deployed as is, following the instructions here:
So far I only tested programmatically the sample Box contract, which I compiled and deployed. But since I am deploying an instance of ERC20PresetMinterPauserUpgradeSafe there is no reference to this at the build folder and thus the way that I was accessing the contract programmatically via loader.fromArtifact is not working anymore and I am getting this error when running a similar code that I used for the Box contract:
at Object.openSync (fs.js:462:3)
at Object.readFileSync (fs.js:364:35)
at Object.readFileSync (/home/luis/token/node_modules/jsonfile/index.js:61:22)
at loadArtifact (/home/luis/token/node_modules/@openzeppelin/contract-loader/lib/index.js:47:23)
at Web3Loader.BaseLoader.fromArtifact (/home/luis/token/node_modules/@openzeppelin/contract-loader/lib/index.js:65:18)
at main (/home/luis/token/src/index.js:14:26)
at Object.<anonymous> (/home/luis/token/src/index.js:38:1)
at Module._compile (internal/modules/cjs/loader.js:1137:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10)
at Module.load (internal/modules/cjs/loader.js:985:32)
at Function.Module._load (internal/modules/cjs/loader.js:878:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47
(node:318) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:318) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
In summary what I need is a code example of how to access this contract in this situation where I did not compile it myself.
I am running Ubuntu 20.04 LTS
Code to reproduce
const token = loader.fromArtifact('ERC20', address);
Thanks!
Luis
1 Like
Hi @Luis-Fernando-Molina,
Welcome to the community
I deployed the preset contract as an upgradeable contract using the already deployed implementation contract.
$ npx oz deploy @openzeppelin/contracts-ethereum-package/ERC20PresetMinterPauserUpgradeSafe
No contracts found to compile.
? Choose the kind of deployment upgradeable
? Pick a network rinkeby
✓ Linked dependency @openzeppelin/contracts-ethereum-package 3.0.0
All implementations are up to date
? Call a function to initialize the instance after creating it? Yes
? Select which function initialize(name: string, symbol: string)
? name: string: My Token
? symbol: string: TKN
✓ Setting everything up to create contract instances
✓ Instance created at 0x4f78cC83EF5cA802C7F3AaC9865Ad77E0732360e
To upgrade this instance run 'oz upgrade'
0x4f78cC83EF5cA802C7F3AaC9865Ad77E0732360e
To get the precompiled artifacts, I copied them from node_modules
$ cp node_modules/@openzeppelin/contracts-ethereum-package/build/contracts/*
build/contracts/
We can then interact programmatically (following instructions in the documentation: https://docs.openzeppelin.com/learn/deploying-and-interacting#interacting-programmatically)
Using the address of the proxy contract.
// src/index.js
const Web3 = require('web3');
const { setupLoader } = require('@openzeppelin/contract-loader');
const { projectId, mnemonic } = require('../secrets.json');
const HDWalletProvider = require('@truffle/hdwallet-provider');
const provider = new HDWalletProvider(mnemonic, `https://rinkeby.infura.io/v3/${projectId}`);
async function main() {
// Set up web3 object
const web3 = new Web3(provider);
// Set up a web3 contract, representing a deployed ERC20, using the contract loader
const address = '0x4f78cC83EF5cA802C7F3AaC9865Ad77E0732360e';
const loader = setupLoader({ provider: web3 }).web3;
const token = loader.fromArtifact('ERC20PresetMinterPauserUpgradeSafe', address);
// Retrieve accounts
const accounts = await web3.eth.getAccounts();
console.log(`Account: ${accounts[0]}`);
// Call the deployed token contract
const name = await token.methods.name().call();
const symbol = await token.methods.symbol().call();
const decimals = await token.methods.decimals().call();
const totalSupply = await token.methods.totalSupply().call();
console.log(`${name} (${symbol}) - Decimals:${decimals} Total Supply:${totalSupply}`);
// At termination, `provider.engine.stop()' should be called to finish the process elegantly.
provider.engine.stop();
}
main();
The output of this script is:
$ node src/index.js
Account: 0xEce6999C6c5BDA71d673090144b6d3bCD21d13d4
My Token (TKN) - Decimals:18 Total Supply:0
Hi @abcoathup !
I am happy to be part of the community. Thanks for the detailed explanation. That was all I needed. I will move forward now and see how far I can get.
Cheers!
1 Like
Hi @Luis-Fernando-Molina,
Great to hear! Feel free to ask all the questions that you need.
If you have a moment it would be great if you could introduce yourself.