From bdb9b083dc7e9e4bd45b646226d1142fe5f8af88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20G=C3=B3rnicki?= <bgor91@gmail.com> Date: Wed, 16 Feb 2022 23:34:27 +0100 Subject: [PATCH] feat: add transaction status API --- package.json | 2 +- src/client.ts | 7 +++++++ src/helpers/key.ts | 6 ++---- src/helpers/transaction.ts | 43 ++++++++++++++++++++++++++++++++++++++ test/transaction.ts | 27 ++++++++++++++++++++++++ 5 files changed, 80 insertions(+), 5 deletions(-) create mode 100644 src/helpers/transaction.ts create mode 100644 test/transaction.ts diff --git a/package.json b/package.json index e945da6..327d999 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@hiveio/dhive", - "version": "1.1.1", + "version": "1.2.0", "description": "Hive blockchain RPC client library", "author": "hive-network", "license": "BSD-3-Clause", diff --git a/src/client.ts b/src/client.ts index e535c0f..4940191 100644 --- a/src/client.ts +++ b/src/client.ts @@ -43,6 +43,7 @@ import { DatabaseAPI } from './helpers/database' import { HivemindAPI } from './helpers/hivemind' import {AccountByKeyAPI} from './helpers/key' import { RCAPI } from './helpers/rc' +import {TransactionStatusAPI} from './helpers/transaction' import { copy, retryingFetch, waitForEvent } from './utils' /** @@ -214,6 +215,11 @@ export class Client { */ public readonly keys: AccountByKeyAPI + /** + * Transaction status API helper. + */ + public readonly transaction: TransactionStatusAPI + /** * Chain ID for current network. */ @@ -264,6 +270,7 @@ export class Client { this.rc = new RCAPI(this) this.hivemind = new HivemindAPI(this) this.keys = new AccountByKeyAPI(this) + this.transaction = new TransactionStatusAPI(this) } /** diff --git a/src/helpers/key.ts b/src/helpers/key.ts index 4f27779..163fa35 100644 --- a/src/helpers/key.ts +++ b/src/helpers/key.ts @@ -7,9 +7,7 @@ import {PublicKey} from '../crypto' import { Client } from './../client' export interface AccountsByKey { - accounts: [ - string[] - ] + accounts: string[][] } export class AccountByKeyAPI { @@ -26,6 +24,6 @@ export class AccountByKeyAPI { * Returns all accounts that have the key associated with their owner or active authorities. */ public async getKeyReferences(keys: (PublicKey | string)[]): Promise<AccountsByKey> { - return this.call('get_key_references', { keys: keys.map((key) => key.toString()) }) + return this.call('get_key_references', { keys: keys.map(key => key.toString()) }) } } diff --git a/src/helpers/transaction.ts b/src/helpers/transaction.ts new file mode 100644 index 0000000..5797fb7 --- /dev/null +++ b/src/helpers/transaction.ts @@ -0,0 +1,43 @@ +/** + * @file Transaction status API helpers. + * @author Bartłomiej (@engrave) Górnicki + */ + +import {Client} from './../client' + +export type TransactionStatus = + 'unknown' + | 'within_mempool' + | 'within_reversible_block' + | 'within_irreversible_block' + | 'expired_reversible' + | 'expired_irreversible' + | 'too_old' + +interface FindTransactionParams { + transaction_id: string + expiration?: string +} +export class TransactionStatusAPI { + constructor(readonly client: Client) {} + + /** + * Convenience for calling `transaction_status_api`. + */ + public call(method: string, params?: any) { + return this.client.call('transaction_status_api', method, params) + } + + /** + * Returns the status of a given transaction id + */ + public async findTransaction(transaction_id: string, expiration?: string): Promise<{ status: TransactionStatus }> { + const params: FindTransactionParams = { + transaction_id + } + if (expiration) { + params.expiration = expiration + } + return this.call('find_transaction', params) + } +} diff --git a/test/transaction.ts b/test/transaction.ts new file mode 100644 index 0000000..91dcb9d --- /dev/null +++ b/test/transaction.ts @@ -0,0 +1,27 @@ +import * as assert from 'assert' +import 'mocha' + +import { Client } from './../src' +import { agent } from './common' + +describe('transaction_status_api', function() { + this.slow(500) + this.timeout(20 * 1000) + + const client = Client.testnet({ agent }) + + describe('find_transaction', () => { + + it('should return unknown', async () => { + const {status} = await client.transaction.findTransaction('0000000000000000000000000000000000000000') + assert.deepEqual(status, 'unknown') + }) + + it('should return too_old', async () => { + const {status} = await client.transaction.findTransaction('0000000000000000000000000000000000000000', '2016-03-24T18:00:21') + assert.deepEqual(status, 'too_old') + }) + + }) + +}) -- GitLab