Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to verify transactions from witness_actions.js #155

Merged
merged 3 commits into from
Jul 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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