# How to interact with an ERC20 token on mainnet

**URL:** <https://forum.openzeppelin.com/t/how-to-interact-with-an-erc20-token-on-mainnet/3390>\
**Category:** Contracts\
**Tags:** erc20\
**Created:** [July 23, 2020, 10:30pm UTC](https://forum.openzeppelin.com/t/how-to-interact-with-an-erc20-token-on-mainnet/3390 "2020-07-23T22:30:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![1baga](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/1baga/32/4209_2.png) [@1baga](https://forum.openzeppelin.com/u/1baga)\
**Post date:** [July 23, 2020, 10:30pm UTC](https://forum.openzeppelin.com/t/how-to-interact-with-an-erc20-token-on-mainnet/3390/1 "2020-07-23T22:30:25Z")

</div>

Hello Admin, I know this question might sound silly. How do I interact with erc20 token on the mainnet using infura and openzeppelin in a nodejs project? Any pointers to tutorials would really help…

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [July 24, 2020, 7:46am UTC](https://forum.openzeppelin.com/t/how-to-interact-with-an-erc20-token-on-mainnet/3390/2 "2020-07-24T07:46:44Z")

</div>

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:

- [Interacting Programmatically](https://docs.openzeppelin.com/learn/deploying-and-interacting#interacting-programmatically)
- [Connecting to Public Test Networks](https://docs.openzeppelin.com/learn/connecting-to-public-test-networks)

I used them as the basis for the following quick sample. Please ask if you need further explanation:

## Setup

```auto
$ mkdir mainneterc20
$ cd mainneterc20
$ npm init -y
$ npm i @openzeppelin/contracts @truffle/hdwallet-provider web3 @openzeppelin/contract-loader

```

Copy the contract artifacts

```auto
$ 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](https://docs.openzeppelin.com/learn/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](https://forum.openzeppelin.com/t/create-an-erc20-using-truffle-without-writing-solidity/2713), [Remix](https://forum.openzeppelin.com/t/create-an-erc20-using-remix-without-writing-solidity/2908), [buidler](https://forum.openzeppelin.com/t/create-an-erc20-using-buidler-without-writing-solidity/2800) or [OpenZeppelin CLI](https://forum.openzeppelin.com/t/create-an-erc20-without-writing-solidity-using-openzeppelin-cli/2698) without writing Solidity)

```auto
// 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](https://etherscan.io/tokens)

Run the script.

```auto
$ node ./src/index.js
Compound (COMP) - Decimals:18 Total Supply:10000000000000000000000000

```

---

<div class="post-metadata">

**Author:** ![1baga](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/1baga/32/4209_2.png) [@1baga](https://forum.openzeppelin.com/u/1baga)\
**Post date:** [July 24, 2020, 7:54pm UTC](https://forum.openzeppelin.com/t/how-to-interact-with-an-erc20-token-on-mainnet/3390/3 "2020-07-24T19:54:11Z")

</div>

Will give this a try… thank you… but could you give an example of how i could send transactions?

---

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [July 28, 2020, 6:39am UTC](https://forum.openzeppelin.com/t/how-to-interact-with-an-erc20-token-on-mainnet/3390/4 "2020-07-28T06:39:32Z")

</div>

Hi @1baga,

To send transactions in a node script, please see the documentation: [https://docs.openzeppelin.com/learn/deploying-and-interacting#sending-a-transaction](https://docs.openzeppelin.com/learn/deploying-and-interacting#sending-a-transaction)

I recommend testing on a public testnet first and checking the appropriate gas price to use ([https://ethgasstation.info](https://ethgasstation.info)) as well as the gas (limit) depending on the function call.

I updated the script to set an allowance on an ERC20 token on Ropsten (note the change in the Infura URL).

```auto
// 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://ropsten.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 = '0xad6d458402f60fd3bd25163575031acdce07538d';
  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}`);

  console.log('Please wait whilst approve transaction sent')
  const tx = await token.methods.approve(accounts[1], "10000000000000000000")
   .send({ from: accounts[0], gas: 50000, gasPrice: 1e6 });

  console.log(tx);

  const allowance = await token.methods.allowance(accounts[0], accounts[1]).call();
  console.log(`Allowance set by ${accounts[0]} of ${allowance} tokens for ${accounts[1]}`)

  // At termination, `provider.engine.stop()' should be called to finish the process elegantly.
  provider.engine.stop();
}

main();

```

---

<div class="post-metadata">

**Author:** ![ssadek](https://avatars.discourse-cdn.com/v4/letter/s/e274bd/32.png) [@ssadek](https://forum.openzeppelin.com/u/ssadek)\
**Post date:** [September 9, 2022, 4:45pm UTC](https://forum.openzeppelin.com/t/how-to-interact-with-an-erc20-token-on-mainnet/3390/5 "2022-09-09T16:45:39Z")

</div>

Hello @ [abcoathup](https://forum.openzeppelin.com/u/abcoathup),

Is it possible to interact with localhost network, deploy a smart contract and interact with it using an available token from testnet for example or would you have to create/deploy a simple IERC20 token into localhost network e.g. a fake WETH token in order to play with it without forking out any of your own account´s testnet/mainnet tokens?

Thank you very much for your help!

Best,  
Sam
