Skip to content

Commit

Permalink
Merge pull request #155 from Rishi556/hive-engine
Browse files Browse the repository at this point in the history
Add ability to verify transactions from witness_actions.js
  • Loading branch information
bt-cryptomancer authored Jul 25, 2022
2 parents c2244e7 + aa71237 commit b413e82
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions witness_action.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require('dotenv').config();
const axios = require('axios');
const dhive = require('@hiveio/dhive');
const program = require('commander');
const packagejson = require('./package.json');
Expand All @@ -16,6 +17,53 @@ const {
const extRPCNodePort = Number(String(process.env.RPCNODEPORT)) || rpcNodePort;
const extP2PPort = Number(String(process.env.P2PPORT)) || p2pPort;

program
.option('-v, --verify', 'verify transaction on hive-engine', false)
.option('-e, --engineNode [url]', 'verify with given hive-engine node', 'https://api.hive-engine.com/rpc')
.parse(process.argv);

let { engineNode } = program;
const { verify } = program;

engineNode = engineNode.replace(/\/$/, '');

function awaitValidation(trxID, tries = 1) {
axios.post(`${engineNode}/blockchain`, {
jsonrpc: '2.0', id: 0, method: 'getTransactionInfo', params: { txid: trxID },
}).then((res) => {
if (res.data.result) {
const parsedLogs = JSON.parse(res.data.result.logs);
if (parsedLogs.errors) {
// eslint-disable-next-line no-console
console.log(`Failed action with reason "${parsedLogs.errors[0]}", your trx id: ${trxID}`);
} else {
// eslint-disable-next-line no-console
console.log(`Successfull, transaction id is ${trxID}`);
}
} else {
if (tries >= 4) {
// eslint-disable-next-line no-console
console.log(`Successful broadcast but could not verify, trx id: ${trxID}`);
return;
}
setTimeout(() => {
awaitValidation(trxID, tries + 1);
}, 4000);
}
}).catch((err) => {
// eslint-disable-next-line no-console
console.log(err);
if (tries >= 4) {
// eslint-disable-next-line no-console
console.log(`Successful broadcast but could not verify, trx id: ${trxID}`);
return;
}
setTimeout(() => {
awaitValidation(trxID, tries + 1);
}, 4000);
});
}

function broadcastWitnessAction(contractAction, contractPayload) {
const client = new dhive.Client(streamNodes[0]);
const transaction = {
Expand All @@ -30,8 +78,12 @@ function broadcastWitnessAction(contractAction, contractPayload) {
};

client.broadcast.json(transaction, privateSigningKey).then((res) => {
// eslint-disable-next-line no-console
console.log(`Successful, trx id: ${res.id}`);
if (verify) {
awaitValidation(res.id);
} else {
// eslint-disable-next-line no-console
console.log(`Successful, trx id: ${res.id}`);
}
}).catch((err) => {
// eslint-disable-next-line no-console
console.error('Error', err);
Expand Down

0 comments on commit b413e82

Please sign in to comment.