When a transaction is mined, smart contracts can emit events and write logs to the blockchain that the frontend can then process. Click here for more information about events.
In the following example we will show you how to find events that are emitted by Money On Chain smart contract in RSK Testnet blockchain with truffle.
Code example
constWeb3=require('web3');//You must compile the smart contracts or use the official ABIs of the repositoryconstMocExchange=require('../../build/contracts/MoCExchange.json');consttruffleConfig=require('../../truffle');/** * Get a provider from truffle.js file * @param{String} network */constgetDefaultProvider= network =>truffleConfig.networks[network].provider ||truffleConfig.networks[network].endpoint;/** * Get a new web3 instance from truffle.js file */constgetWeb3= network => {constprovider=getDefaultProvider(network);returnnewWeb3(provider,null, { transactionConfirmationBlocks:1 });};constweb3=getWeb3('rskTestnet');//Contract address on testnetconstmocExchangeAddress='<contract-address>';constexecute=async () => {web3.eth.defaultGas =2000000;/** * Loads an specified contract * @param{ContractABI} abi * @param{String} contractAddress */constgetContract=async (abi, contractAddress) =>newweb3.eth.Contract(abi, contractAddress);// Loading MoCExchange contract to get the events emitted by thisconstmocExchange=awaitgetContract(MocExchange.abi, mocExchangeAddress);if (!mocExchange) {throwError('Can not find MoCExchange contract.'); }// In this example we are getting BPro Mint events from MoCExchange contract// in the interval of blocks passed by parameterconstgetEvents= () =>Promise.resolve(mocExchange.getPastEvents('RiskProMint', { fromBlock:1000, toBlock:1010 })).then(events =>console.log(events)).catch(err =>console.log('Error getting past events ', err));awaitgetEvents();};execute().then(() =>console.log('Completed')).catch(err => {console.log('Error', err); });
See getPastEvents for parameters and event structure details.