Skip to content
This repository has been archived by the owner on Jun 3, 2022. It is now read-only.

Add v0/:network/transactions/:id endpoint #244

Merged
merged 5 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
58 changes: 58 additions & 0 deletions packages/whale-api-client/__tests__/api/transaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,4 +236,62 @@ describe('transaction', () => {
expect(feeRate).toStrictEqual(0.00005000)
})
})

describe('getTransaction', () => {
siradji marked this conversation as resolved.
Show resolved Hide resolved
let txid: string
let height: number

async function setup (): Promise<void> {
const address = await container.getNewAddress()
const metadata = {
symbol: 'ETH',
name: 'ETH',
isDAT: true,
mintable: true,
tradeable: true,
collateralAddress: address
}

txid = await container.call('createtoken', [metadata])

await container.generate(1)

height = await container.call('getblockcount')
}

beforeAll(async () => {
await setup()
})

it('should get a single transaction', async () => {
await service.waitForIndexedHeight(height)

const transaction = await client.transactions.getTransaction(txid)

expect(transaction).toStrictEqual({
id: txid,
block: {
hash: expect.any(String),
height
},
txid,
hash: txid,
version: expect.any(Number),
size: expect.any(Number),
vSize: expect.any(Number),
weight: expect.any(Number),
lockTime: expect.any(Number),
vinCount: expect.any(Number),
voutCount: expect.any(Number)
})
})

it('should return undefined for invalid transaction id ', async () => {
await service.waitForIndexedHeight(height)

const transaction = await client.transactions.getTransaction('invalidtransactionId')

expect(transaction).toStrictEqual(undefined)
})
})
})
28 changes: 28 additions & 0 deletions packages/whale-api-client/src/api/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ export class Transactions {
return await this.client.requestData('POST', 'transactions', rawTx)
}

/**
* @param {string} id transaction id to query
* @return {Transaction}
*/
async getTransaction (id: string): Promise<Transaction> {
siradji marked this conversation as resolved.
Show resolved Hide resolved
return await this.client.requestData('GET', `transactions/${id}`)
}

/**
* @param {RawTxReq} rawTx to test mempool acceptance
* @throws WhaleApiException if failed mempool acceptance
Expand All @@ -36,3 +44,23 @@ export interface RawTxReq {
hex: string
maxFeeRate?: number
}

/**
* Transaction interface
*/
export interface Transaction {
id: string
block: {
hash: string
height: number
}
txid: string
hash: string
version: number
size: number
vSize: number
weight: number
lockTime: number
vinCount: number
voutCount: number
}
54 changes: 54 additions & 0 deletions src/module.api/transaction.controller.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,4 +172,58 @@ describe('transactions', () => {
expect(after).not.toStrictEqual(0.00005000)
})
})

describe('getTransaction', () => {
siradji marked this conversation as resolved.
Show resolved Hide resolved
let txid: string
let height: number

async function setup (): Promise<void> {
const address = await container.getNewAddress()
const metadata = {
symbol: 'ETH',
name: 'ETH',
isDAT: true,
mintable: true,
tradeable: true,
collateralAddress: address
}
txid = await container.call('createtoken', [metadata])

await container.generate(1)

height = await container.call('getblockcount')
}

beforeAll(async () => {
await setup()
})

it('should get a single transaction', async () => {
await waitForIndexedHeight(app, height)
siradji marked this conversation as resolved.
Show resolved Hide resolved

const transaction = await controller.get(txid)
expect(transaction).toStrictEqual({
id: txid,
block: {
hash: expect.any(String),
height
},
txid,
hash: txid,
version: expect.any(Number),
size: expect.any(Number),
vSize: expect.any(Number),
weight: expect.any(Number),
lockTime: expect.any(Number),
vinCount: expect.any(Number),
voutCount: expect.any(Number)
})
})

it('should return undefined for invalid transaction id ', async () => {
await waitForIndexedHeight(app, height)
const transaction = await controller.get('invalidtransactionId')
expect(transaction).toStrictEqual(undefined)
})
})
})
19 changes: 17 additions & 2 deletions src/module.api/transaction.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import BigNumber from 'bignumber.js'
import { Body, Controller, Get, HttpCode, ParseIntPipe, Post, Query, ValidationPipe } from '@nestjs/common'
import { Body, Controller, Get, HttpCode, ParseIntPipe, Post, Query, ValidationPipe, Param } from '@nestjs/common'
import { JsonRpcClient } from '@defichain/jellyfish-api-jsonrpc'
import { IsHexadecimal, IsNotEmpty, IsNumber, IsOptional, Min } from 'class-validator'
import { BadRequestApiException } from '@src/module.api/_core/api.error'
import { EstimateMode } from '@defichain/jellyfish-api-core/dist/category/mining'
import { TransactionMapper } from '@src/module.model/transaction'
import { Transaction } from '@whale-api-client/api/transactions'

class RawTxDto {
@IsNotEmpty()
Expand All @@ -27,7 +29,10 @@ export class TransactionsController {
*/
private readonly defaultMaxFeeRate: BigNumber = new BigNumber('0.005')

constructor (private readonly client: JsonRpcClient) {
constructor (
private readonly client: JsonRpcClient,
private readonly transactionMapper: TransactionMapper
) {
}

/**
Expand All @@ -47,6 +52,16 @@ export class TransactionsController {
return 0.00005000
}

/**
* Get a single transaction by id
*
* @return{Promise<Transaction | undefined>}
*/
@Get('/:id')
async get (@Param('id') id: string): Promise<Transaction | undefined> {
canonbrother marked this conversation as resolved.
Show resolved Hide resolved
siradji marked this conversation as resolved.
Show resolved Hide resolved
return await this.transactionMapper.get(id)
siradji marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param {RawTxDto} tx to submit to the network.
* @return {Promise<string>} hash of the transaction
Expand Down