diff --git a/package.json b/package.json index e945da66c2ec477ace2c9228be3479c9509ee154..327d999bffdeed989aad9c3e0b3946899a9118db 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 e535c0f1b18a1b5837475ab721ba3421ccea016f..4940191dc67af835e7463d323288da677c965a67 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 4f27779d6e95218fd5e36a342fcdcbd64ddb0ba1..163fa359dd95302b160996470025bc8c5ddfaf7d 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 0000000000000000000000000000000000000000..5797fb77361629e986761062904d1f453fadf741 --- /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 0000000000000000000000000000000000000000..91dcb9d4c19dfcd9a062f04bd90d9140b2f0cf34 --- /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') + }) + + }) + +})