|
| 1 | +const _ = require('lodash'); |
| 2 | +const axios = require('axios'); |
| 3 | +const logger = require('../logger')('NETWORK-UTIL'); |
| 4 | +const { |
| 5 | + CURRENT_PROTOCOL_VERSION |
| 6 | +} = require('../common/constants'); |
| 7 | +const ChainUtil = require('../common/chain-util'); |
| 8 | + |
| 9 | + |
| 10 | +async function _waitUntilTxFinalize(endpoint, txHash) { |
| 11 | + while (true) { |
| 12 | + const confirmed = await sendGetRequest( |
| 13 | + endpoint, |
| 14 | + 'ain_getTransactionByHash', |
| 15 | + { hash: txHash } |
| 16 | + ) |
| 17 | + .then((resp) => { |
| 18 | + return (_.get(resp, 'data.result.result.is_finalized', false) === true); |
| 19 | + }) |
| 20 | + .catch((err) => { |
| 21 | + logger.error(`Failed to confirm transaction: ${err}`); |
| 22 | + return false; |
| 23 | + }); |
| 24 | + if (confirmed) { |
| 25 | + return true; |
| 26 | + } |
| 27 | + await ChainUtil.sleep(1000); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +async function sendTxAndWaitForFinalization(endpoint, tx, privateKey) { |
| 32 | + const res = await signAndSendTx(endpoint, tx, privateKey); |
| 33 | + if (_.get(res, 'errMsg', false) || !_.get(res, 'success', false)) { |
| 34 | + throw Error(`Failed to sign and send tx: ${res.errMsg}`); |
| 35 | + } |
| 36 | + if (!(await _waitUntilTxFinalize(endpoint, _.get(res, 'txHash', null)))) { |
| 37 | + throw Error('Transaction did not finalize in time.' + |
| 38 | + 'Try selecting a different parent_chain_poc.'); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +async function sendSignedTx(endpoint, params) { |
| 43 | + return await axios.post( |
| 44 | + endpoint, |
| 45 | + { |
| 46 | + method: 'ain_sendSignedTransaction', |
| 47 | + params, |
| 48 | + jsonrpc: '2.0', |
| 49 | + id: 0 |
| 50 | + } |
| 51 | + ).then((resp) => { |
| 52 | + const result = _.get(resp, 'data.result.result.result', {}); |
| 53 | + const success = !ChainUtil.isFailedTx(result); |
| 54 | + return { success, errMsg: result.error_message }; |
| 55 | + }).catch((err) => { |
| 56 | + logger.error(`Failed to send transaction: ${err}`); |
| 57 | + return { success: false, errMsg: err.message }; |
| 58 | + }); |
| 59 | +} |
| 60 | + |
| 61 | +// FIXME(minsulee2): this is duplicated function see: ./tools/util.js |
| 62 | +async function signAndSendTx(endpoint, tx, privateKey) { |
| 63 | + const { txHash, signedTx } = ChainUtil.signTransaction(tx, privateKey); |
| 64 | + const result = await sendSignedTx(endpoint, signedTx); |
| 65 | + return Object.assign(result, { txHash }); |
| 66 | +} |
| 67 | + |
| 68 | +function sendGetRequest(endpoint, method, params) { |
| 69 | + // NOTE(platfowner): .then() was used here to avoid some unexpected behavior of axios.post() |
| 70 | + // (see https://github.com/ainblockchain/ain-blockchain/issues/101) |
| 71 | + return axios.post( |
| 72 | + endpoint, |
| 73 | + { |
| 74 | + method, |
| 75 | + params: Object.assign(params, { protoVer: CURRENT_PROTOCOL_VERSION }), |
| 76 | + jsonrpc: '2.0', |
| 77 | + id: 0 |
| 78 | + } |
| 79 | + ).then((resp) => { |
| 80 | + return resp; |
| 81 | + }).catch((err) => { |
| 82 | + logger.error(`Failed to send get request: ${err}`); |
| 83 | + return null; |
| 84 | + }); |
| 85 | +} |
| 86 | + |
| 87 | +module.exports = { |
| 88 | + sendTxAndWaitForFinalization, |
| 89 | + sendSignedTx, |
| 90 | + signAndSendTx, |
| 91 | + sendGetRequest |
| 92 | +}; |
0 commit comments