Receiving NGNT

NGNT (like all ERC20 tokens) emit a Transfer event whenever NGNT is transferred from one address to another.

Web3 lets you subscribe to these events and you can determine if the recipient address is one that belongs to your application.

To interact with smart contracts using web3, there's something called the ABI. This is essentially a JSON representation of the contract. The ABI for NGNT can be found here.

The code snippet below shows how to listen on NGNT Transfer events.

const BigNumber = require('bignumber.js');
const Web3 = require('web3');
const Provider = Web3.providers.HttpProvider; // this could be a websocket provider
const web3Provider = new Web3(new Provider(<url to Ethereum client>));
const provider = web3Provider.eth

const NGNTAbi = require('../path/to/abi.json');
const NGNTContractAddress = <NGNT contract address depending on the network>
const NGNT = new provider.Contract(NGNTAbi, NGNTContractAddress);

NGNT.events.Transfer(function(error, event) { 
  const transactionHash = event.transactionHash;
  const from = event.returnValues.from; // sender address
  const to = event.returnValues.to; // recipient address
  const value = (new BigNumber(event.returnValues.value)).dividedBy(100).toNumber();
  
  // validate that recipient address belongs to application (with a db lookup)
  // proceed to give user value or update local record of address balance
});

Last updated

Was this helpful?