const HDWalletProvider = require('truffle-hdwallet-provider');
const BigNumber = require('bignumber.js');
const Web3 = require('web3');
//You must compile the smart contracts or use the official ABIs of the repository
const MocAbi = require('./contracts/moc/MoC.json');
const MoCInrateAbi = require('./contracts/moc/MoCInrate.json');
const MoCExchangeAbi = require('./contracts/moc/MoCExchange.json');
const MoCStateAbi = require('./contracts/moc/MoCState.json');
//Config params to TestNet
const endpoint = 'https://public-node.testnet.rsk.co';
//a mnemonic is 12 words instead of a single private key to sign the //transactions
const mnemonic = 'chase chair crew elbow uncle awful cover asset cradle pet loud puzzle';
const provider = new HDWalletProvider(mnemonic, endpoint);
const web3 = new Web3(provider);
//Contract addresses on testnet
const mocContractAddress = '<contract-address>';
const mocInrateAddress = '<contract-address>';
const mocExchangeAddress = '<contract-address>';
const mocStateAddress = '<contract-address>';
const gasPrice = 60000000;
const execute = async () => {
* Loads an specified contract
* @param {localhost/testnet/mainnet} contractAddress
const getContract = async (abi, contractAddress) => new web3.eth.Contract(abi, contractAddress);
* Transforms BigNumbers into
const toContract = number => new BigNumber(number).toFixed(0);
const moc = await getContract(MocAbi.abi, mocContractAddress);
throw Error('Can not find MoC contract.');
// Loading mocInrate contract. It is necessary to get fees for transaction types
const mocInrate = await getContract(MoCInrateAbi.abi, mocInrateAddress);
throw Error('Can not find MoC Inrate contract.');
// Loading mocExchange contract. It is necessary to compute commissions and vendor markup
const mocExchange = await getContract(MoCExchangeAbi.abi, mocExchangeAddress);
throw Error('Can not find MoC Exchange contract.');
// Loading mocState contract. It is necessary to compute max BPRO available to mint
const mocState = await getContract(MoCStateAbi.abi, mocStateAddress);
throw Error('Can not find MoCState contract.');
const mintBpro = async (btcAmount, vendorAccount) => {
web3.eth.getAccounts().then(console.log);
const from = '0x088f4B1313D161D83B4D8A5EB90905C263ce0DbD';
const weiAmount = web3.utils.toWei(btcAmount, 'ether');
const txTypeFeesRBTC = await mocInrate.methods.MINT_BPRO_FEES_RBTC();
const txTypeFeesMOC = await mocInrate.methods.MINT_BPRO_FEES_MOC();
amount: toContractBN(weiAmount).toString(),
txTypeFeesMOC: txTypeFeesMOC.toString(),
txTypeFeesRBTC: txTypeFeesRBTC.toString(),
} = await mocExchange.methods.calculateCommissionsWithPrices(params, { from }));
// Computes totalBtcAmount to call mintBpro. If commission is paid in RBTC, add it to value
const totalBtcAmount = toContract(btcCommission.plus(btcMarkup).plus(weiAmount));
console.log(`Calling Bpro minting with account: ${from} and amount: ${weiAmount}.`);
.mintBProVendors(weiAmount, vendorAccount)
.send({ from, value: totalBtcAmount, gasPrice }, function(error, transactionHash) {
if (error) console.log(error);
if (transactionHash) console.log('txHash: '.concat(transactionHash));
console.log('End Example');
const btcAmount = '0.00005';
const vendorAccount = '<vendor-address>';
await mintBpro(btcAmount, vendorAccount, logEnd);
.then(() => console.log('Completed'))
console.log('Error', err);