Hi @1baga,
Welcome to the community
I don’t think there are silly questions. Questions and answers build the knowledge base of the community.
I suggest having a look at the following from Learn guides:
I used them as the basis for the following quick sample. Please ask if you need further explanation:
Setup
$ mkdir mainneterc20
$ cd mainneterc20
$ npm init -y
$ npm i @openzeppelin/contracts @truffle/hdwallet-provider web3 @openzeppelin/contract-loader
Copy the contract artifacts
$ mkdir -p build/contracts/
$ cp node_modules/@openzeppelin/contracts/build/contracts/* build/contracts/
Create a secrets.json
(see the instructions in Connecting to Public Test Networks and create an Infura account if you don’t already have one)
Do not commit any secrets to a public repository as they can be used by anyone.
index.js
Create a script to interact. I only used read functions but you could also send transactions if you used a mnemonic with funds for gas.
If you are sending transactions, I suggest testing on a public testnet first. You could try with your own token. (See: Create an ERC20 using Truffle, Remix, buidler or OpenZeppelin CLI without writing Solidity)
// 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://mainnet.infura.io/v3/${projectId}`);
async function main() {
// Set up web3 object
const web3 = new Web3(provider);
const loader = setupLoader({ provider: web3 }).web3;
// Set up a web3 contract, representing a deployed ERC20, using the contract loader
const address = '0xc00e94cb662c3520282e6f5717214004a7f26888';
const token = loader.fromArtifact('ERC20', address);
// Retrieve accounts
const accounts = await web3.eth.getAccounts();
// 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();
Set the address to the token that you want to interact with. Please note a token may not implement all the functionality so you may want to use IERC20
rather than ERC20
.
For this example I used Compound. Have a look at Etherscan for other tokens to interact with: https://etherscan.io/tokens
Run the script.
$ node ./src/index.js
Compound (COMP) - Decimals:18 Total Supply:10000000000000000000000000