/* eslint-disable no-console */ /* eslint-disable no-unused-vars */ /** * Commands specific to ajix commands */ import { pinFileToIpfs } from '../helpers/ipfs-helper'; import { createHelixTransaction, generateHelixAddress, getBundlePayload, getBalance, getInclutionStateTilConfirmation, } from '../helpers/helix-helpers'; import { recordCount, addNewRecord, getRecord } from '../helpers/mongo-helper'; /** * Function to publish new lab report to ipfs and helix network * @param helix helix compose wrapper * @param pinata pinata ipfs provider * @param filePath file path to the file that have to be uploaded * @param helixSeed seed with which transaction is being signed * @param address address of the receiver */ // eslint-disable-next-line import/prefer-default-export export async function publishNewRecord( helix:any, pinata:any, filePath:string, helixSeed:string, address:string, ) { console.log('Starting to publish new record'); const ipfs = await pinFileToIpfs(pinata, filePath); const count = await recordCount(); const previousAddr = count === 0 ? address : await generateHelixAddress(helixSeed, count - 1); const currentAddr = await generateHelixAddress(helixSeed, count); const nextAddr = await generateHelixAddress(helixSeed, count + 1); const balance = await getBalance(helix, [currentAddr]); const txPayload = JSON.stringify({ recordIpfsHash: ipfs.IpfsHash, recordNo: count, previousAddr, nextAddr, }); const inputs = [{ address: currentAddr, keyIndex: count, balance: balance.balances[0], security: 2, }]; console.log('Started to publish record to helixnetwork'); const tx = await createHelixTransaction(helix, helixSeed, address, 1, txPayload, inputs, 'ajix', nextAddr); console.log('Transaction Published'); console.log(tx); console.log('You can see the transaction at ', `https://explorer.helixmain.net/tx/${tx[0].hash}`); console.log('Waiting for transaction confirmation. Sit tight ....'); await getInclutionStateTilConfirmation(helix, [tx[0].hash]); console.log('Transaction confirmed'); await addNewRecord(`ajix#${count}`, ipfs.IpfsHash, tx[0].hash); const record = { recordID: `ajix#${count}`, ipfsHash: ipfs.IpfsHash, helixTxHash: tx[0].hash, }; console.log('Payload to be added to QR Code'); console.log(record); process.exit(0); } /** * Extract payload from valid helix hash * @param helix helix compose wrapper * @param txHash valid txHash with the records */ export async function extractPayloadFromHelixTx( helix:any, txHash: string, ) { const result = await getBundlePayload(helix, txHash); console.log(result); } /** * Gives back an address which needs to be funded * @param seed The current seed which is being used * @return {string} address */ export async function giveLatestAddress(seed: string) { const count = await recordCount(); const address = await generateHelixAddress(seed, count); console.log('You can send funds to this address: ', address); } /** * Fetches report information from the database * @param reportId id of the report to fetch */ export async function fetchRecordById(reportId: string) { const record = await getRecord(reportId); if (record === 'null') { console.log(record); } else { console.log('No record found !!!'); } } /** * Gives back the balance for the current account balance * @param helix helix compose wrapper * @param seed current seed for the cli */ export async function getFundBalance(helix:any, seed:string) { const count = await recordCount(); const address = await generateHelixAddress(seed, count); const balance = await getBalance(helix, [address]); console.log(`Current Fund balance is ${balance.balances[0]} hlx`); }