Interacting with upgradeable ERC20 using geth

HI @beck1234,

I haven’t interacted via geth before.

I created a simple script to interact via Alchemy, though you could modify to use a local node.

First I obtained an ERC20 artifact (though you could just use the ABI) from OpenZeppelin Contracts

$ npm i @openzeppelin/contracts
$ mkdir -p build/contracts/
$ cp node_modules/@openzeppelin/contracts/build/contracts/* build/contracts/

Then I installed web3 and the OpenZeppelin Contract Loader (for convenience)

$ npm i web3
$ npm i @openzeppelin/contract-loader

index.js

Interact using ERC20 and the address of the proxy

// scripts/index.js
const Web3 = require('web3');
const { setupLoader } = require('@openzeppelin/contract-loader');

const { alchemyApiKey, mnemonic } = require('../secrets.json');

const HDWalletProvider = require('@truffle/hdwallet-provider');

const provider = new HDWalletProvider(mnemonic, `https://eth-mainnet.alchemyapi.io/v2/${alchemyApiKey}`);

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 = '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48';
  const loader = setupLoader({ provider: web3 }).web3;
  const token = loader.fromArtifact('ERC20', 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();

Run

$ node scripts/index.js
Account: 0xEce6999C6c5BDA71d673090144b6d3bCD21d13d4
USD Coin (USDC) - Decimals:6 Total Supply:4750520962337187
1 Like