Skip to content
Snippets Groups Projects
Commit bdb9b083 authored by Bartłomiej Górnicki's avatar Bartłomiej Górnicki
Browse files

feat: add transaction status API

parent ada363fd
No related branches found
No related tags found
1 merge request!32feat: add transaction status API
{
"name": "@hiveio/dhive",
"version": "1.1.1",
"version": "1.2.0",
"description": "Hive blockchain RPC client library",
"author": "hive-network",
"license": "BSD-3-Clause",
......
......@@ -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)
}
/**
......
......@@ -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()) })
}
}
/**
* @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)
}
}
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')
})
})
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment