Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BE] [Practice] Make a backend service to get a users balance of an erc20 token #2

Open
2 tasks
cupOJoseph opened this issue Feb 3, 2023 · 0 comments
Open
2 tasks

Comments

@cupOJoseph
Copy link
Member

cupOJoseph commented Feb 3, 2023

Goal

Learn about generating services with redwood and getting data.

Deliverable

A simple redwood service which gets a particular balance from the blockchain (any network).

Suggested tasks

  • If you don't have one already, create a service eg. yarn rw generate service user which will create a file in api/src/services where you will place your logic.
  • Use ethers.js v5 to load a wallet token or coin balance. You will probably need to look for docs for this! Pick any network, any coin or token, and any user address. These can be hardcoded.

The service could look like this:

import { Contract, getDefaultProvider, providers, utils } from 'ethers'

function getProvider(chain) {
  const provider =
    chain === 'ETH'
      ? getDefaultProvider()
      : new providers.JsonRpcProvider(
          'https://www.ethercluster.com/etc',
          'classic'
        )
  return provider
}

const contractAbiFragment = [
    {
      name: 'balanceOf',
      type: 'function',
      inputs: [
        {
          name: '_owner',
          type: 'address',
        },
      ],
      outputs: [
        {
          name: 'balance',
          type: 'uint256',
        },
      ],
      constant: true,
      payable: false,
    },
  ]

  const provider = getProvider('ETH')
  const contract = new Contract(token, contractAbiFragment, provider)

//do stuff
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant