In the following example we will show how to invoke redeemFreeDocVendors from Money on Chain contract. This method allows to redeem DOC outside the settlement and they are limited by user balance. Check the DOC redeemption section for more details.
We will learn how to:
Get the maximum amount of DoCs available to redeem.
Get DoC balance of an account.
Redeem DoCs.
You can find code examples into /examples dir. We will use truffle and testnet network. First we create a new node project.
mkdir example-redeem-free-doc
cd example-redeem-free-doc
npm init
Let's add the necessary dependencies to run the project.
npm install --save web3
Example
constWeb3=require('web3');//You must compile the smart contracts or use the official ABIs of the repositoryconstMoC=require('../../build/contracts/MoC.json');constMocState=require('../../build/contracts/MoCState.json');constDocToken=require('../../build/contracts/DocToken.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 gasPrice from truffle.js file * @param{String} network */constgetGasPrice= network =>truffleConfig.networks[network].gasPrice ||60000000;/** * Get a new web3 instance from truffle.js file */constgetWeb3= network => {constprovider=getDefaultProvider(network);returnnewWeb3(provider,null, { transactionConfirmationBlocks:1 });};constweb3=getWeb3('rskTestnet');constgasPrice=getGasPrice('rskTestnet');//Contract addresses on testnetconstmocAddress='<contract-address>';constmocStateAddress='<contract-address>';constdocTokenAddress='<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 MoC contractconstmoc=awaitgetContract(MoC.abi, mocAddress);if (!moc) {throwError('Can not find MoC contract.'); }// Loading mocState contract. It is necessary to compute freeDocconstmocState=awaitgetContract(MocState.abi, mocStateAddress);if (!mocState) {throwError('Can not find MoCState contract.'); }// Loading DocToken contract. It is necessary to compute user balanceconstdocToken=awaitgetContract(DocToken.abi, docTokenAddress);if (!docToken) {throwError('Can not find DocToken contract.'); }const [from] =awaitweb3.eth.getAccounts();constredeemFreeDoc=async (docAmount, vendorAccount) => {constweiAmount=web3.utils.toWei(docAmount,'ether');console.log(`Calling redeem Doc request, account: ${from}, amount: ${weiAmount}.`);moc.methods.redeemFreeDocVendors(weiAmount, vendorAccount).send({ from, gasPrice },function(error, transactionHash) {if (error) console.log(error);if (transactionHash) console.log('txHash: '.concat(transactionHash)); }).on('transactionHash',function(hash) {console.log('TxHash: '.concat(hash)); }).on('receipt',function(receipt) {console.log(receipt); }).on('error',console.error); };constdocAmount='10000';constfreeDoc=awaitmocState.methods.freeDoc().call();constuserDocBalance=awaitdocToken.methods.balanceOf(from).call();constfinalDocAmount=Math.min(freeDoc, userDocBalance);constvendorAccount='<vendor-address>';console.log('User DOC balance: ',userDocBalance.toString());console.log('=== Max Available DOC to redeem: ', finalDocAmount);// Call redeemawaitredeemFreeDoc(docAmount, vendorAccount);};execute().then(() =>console.log('Completed')).catch(err => {console.log('Error', err); });