diff --git a/.gitignore b/.gitignore index c8b2911d9..c6ea0f0c4 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ coverage *.log .env + +jest-junit.xml diff --git a/src/dto/rpc/DogeRpcSuite.ts b/src/dto/rpc/DogeRpcSuite.ts index 5f5d8e62a..eb908ec2d 100644 --- a/src/dto/rpc/DogeRpcSuite.ts +++ b/src/dto/rpc/DogeRpcSuite.ts @@ -2,46 +2,10 @@ import { JsonRpcResponse } from '../JsonRpcResponse.dto' import { AbstractRpcInterface } from './AbstractJsonRpcInterface' +import { UtxoBasedCommonRpcInterface } from './UtxoBasedRpcSuite' export interface DogeRpcSuite extends DogeRpcInterface, AbstractRpcInterface {} -export interface DogeRpcInterface { - // blockchain methods - getBestBlockHash(): Promise> +export interface DogeRpcInterface extends UtxoBasedCommonRpcInterface{ getBlock(hashOrHeight: string, verbose?: boolean): Promise> - getBlockChainInfo(): Promise> - getBlockCount(): Promise> - getBlockHash(height: number): Promise> - getBlockHeader(hash: string, verbose?: boolean): Promise> - getBlockStats(hash: string): Promise> - getChainTips(): Promise> - getDifficulty(): Promise> - getMempoolAncestors(txId: string, verbose?: boolean): Promise> - getMempoolDescendants(txId: string, verbose?: boolean): Promise> - getMempoolEntry(txId: string): Promise> - getMempoolInfo(): Promise> - getRawMemPool(verbose?: boolean): Promise> - getTxOut(txId: string, index: number, includeMempool?: boolean): Promise> - getTxOutProof(txIds: string[], blockhash?: string): Promise> - verifyTxOutProof(proof: string): Promise> - - // raw transactions methods - createRawTransaction( - inputs: any[], - outputs: any, - locktime?: number, - replaceable?: boolean, - ): Promise> - decodeRawTransaction(hexstring: string): Promise> - decodeScript(hexstring: string): Promise> - getRawTransaction(txId: string, verbose?: boolean): Promise> - sendRawTransaction(hexstring: string): Promise> - - // utility methods - estimateSmartFee( - blocks: number, - estimateMode?: 'UNSET' | 'ECONOMICAL' | 'CONSERVATIVE', - ): Promise> - validateAddress(address: string): Promise> - verifyMessage(address: string, signature: string, message: string): Promise> } diff --git a/src/dto/rpc/UtxoBasedRpcSuite.ts b/src/dto/rpc/UtxoBasedRpcSuite.ts index 48e6872d8..86be8437c 100644 --- a/src/dto/rpc/UtxoBasedRpcSuite.ts +++ b/src/dto/rpc/UtxoBasedRpcSuite.ts @@ -5,10 +5,9 @@ import { AbstractRpcInterface } from './AbstractJsonRpcInterface' export interface UtxoBasedRpcSuite extends UtxoBasedRpcInterface, AbstractRpcInterface {} -export interface UtxoBasedRpcInterface { +export interface UtxoBasedCommonRpcInterface { // blockchain methods getBestBlockHash(): Promise> - getBlock(hashOrHeight: string, verbose?: 0 | 1 | 2): Promise> getBlockChainInfo(): Promise> getBlockCount(): Promise> getBlockHash(height: number): Promise> @@ -46,6 +45,10 @@ export interface UtxoBasedRpcInterface { verifyMessage(address: string, signature: string, message: string): Promise> } +export interface UtxoBasedRpcInterface extends UtxoBasedCommonRpcInterface{ + getBlock(hashOrHeight: string, verbose?: 0 | 1 | 2): Promise> +} + export interface UtxoBasedRpcInterfaceEstimateFee extends UtxoBasedRpcInterface { estimateFee(): Promise> } diff --git a/src/service/rpc/utxo/AbstractDogeRpc.ts b/src/service/rpc/utxo/AbstractCommonUtxoRpc.ts similarity index 93% rename from src/service/rpc/utxo/AbstractDogeRpc.ts rename to src/service/rpc/utxo/AbstractCommonUtxoRpc.ts index 363de527d..28e920840 100644 --- a/src/service/rpc/utxo/AbstractDogeRpc.ts +++ b/src/service/rpc/utxo/AbstractCommonUtxoRpc.ts @@ -1,7 +1,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ -import { DogeRpcInterface, JsonRpcResponse } from '../../../dto' +import { JsonRpcResponse, UtxoBasedCommonRpcInterface } from '../../../dto' -export abstract class AbstractDogeRpc implements DogeRpcInterface { +export abstract class AbstractCommonUtxoRpc implements UtxoBasedCommonRpcInterface { protected abstract rpcCall(method: string, params?: unknown[]): Promise async createRawTransaction( @@ -40,10 +40,6 @@ export abstract class AbstractDogeRpc implements DogeRpcInterface { return this.rpcCall>('getbestblockhash') } - async getBlock(hashOrHeight: string, verbose = true): Promise> { - return this.rpcCall>('getblock', [hashOrHeight, verbose]) - } - async getBlockChainInfo(): Promise> { return this.rpcCall>('getblockchaininfo') } diff --git a/src/service/rpc/utxo/AbstractUtxoRpc.ts b/src/service/rpc/utxo/AbstractUtxoRpc.ts index e697c320a..9158132ab 100644 --- a/src/service/rpc/utxo/AbstractUtxoRpc.ts +++ b/src/service/rpc/utxo/AbstractUtxoRpc.ts @@ -1,130 +1,11 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { JsonRpcResponse, UtxoBasedRpcInterface } from '../../../dto' +import { AbstractCommonUtxoRpc } from './AbstractCommonUtxoRpc' -export abstract class AbstractUtxoRpc implements UtxoBasedRpcInterface { +export abstract class AbstractUtxoRpc extends AbstractCommonUtxoRpc implements UtxoBasedRpcInterface { protected abstract rpcCall(method: string, params?: unknown[]): Promise - async createRawTransaction( - inputs: any[], - outputs: any, - locktime: number, - replaceable: boolean, - ): Promise> { - const params: unknown[] = [inputs, outputs] - if (locktime) { - params.push(locktime) - } - if (replaceable) { - params.push(replaceable) - } - return this.rpcCall>('createrawtransaction', params) - } - - async decodeRawTransaction(hexstring: string): Promise> { - return this.rpcCall>('decoderawtransaction', [hexstring]) - } - - async decodeScript(hexstring: string): Promise> { - return this.rpcCall>('decodescript', [hexstring]) - } - - async estimateSmartFee(blocks: number, estimateMode?: string): Promise> { - const params: unknown[] = [blocks] - if (estimateMode) { - params.push(estimateMode) - } - return this.rpcCall>('estimatesmartfee', params) - } - - async getBestBlockHash(): Promise> { - return this.rpcCall>('getbestblockhash') - } - async getBlock(hashOrHeight: string, verbose: 0 | 1 | 2 = 1): Promise> { return this.rpcCall>('getblock', [hashOrHeight, verbose]) } - - async getBlockChainInfo(): Promise> { - return this.rpcCall>('getblockchaininfo') - } - - async getBlockCount(): Promise> { - return this.rpcCall>('getblockcount') - } - - async getBlockHash(height: number): Promise> { - return this.rpcCall>('getblockhash', [height]) - } - - async getBlockHeader(hash: string, verbose = true): Promise> { - return this.rpcCall>('getblockheader', [hash, verbose]) - } - - async getBlockStats(hash: string): Promise> { - return this.rpcCall>('getblockstats', [hash]) - } - - async getChainTips(): Promise> { - return this.rpcCall>('getchaintips') - } - - async getDifficulty(): Promise> { - return this.rpcCall>('getdifficulty') - } - - async getMempoolAncestors(txId: string, verbose = false): Promise> { - return this.rpcCall>('getmempoolancestors', [txId, verbose]) - } - - async getMempoolDescendants(txId: string, verbose = false): Promise> { - return this.rpcCall>('getmempooldescendants', [txId, verbose]) - } - - async getMempoolEntry(txId: string): Promise> { - return this.rpcCall>('getmempoolentry', [txId]) - } - - async getMempoolInfo(): Promise> { - return this.rpcCall>('getmempoolinfo') - } - - async getRawMemPool(verbose = false): Promise> { - return this.rpcCall>('getrawmempool', [verbose]) - } - - async getRawTransaction(txId: string, verbose = false): Promise> { - return this.rpcCall>('getrawtransaction', [txId, verbose]) - } - - async getTxOut(txId: string, index: number, includeMempool = true): Promise> { - return this.rpcCall>('gettxout', [txId, index, includeMempool]) - } - - async getTxOutProof(txIds: string[], blockhash?: string): Promise> { - const params: unknown[] = [txIds] - if (blockhash) { - params.push(blockhash) - } - return this.rpcCall>('gettxoutproof', params) - } - - async sendRawTransaction(hexstring: string): Promise> { - return this.rpcCall>('sendrawtransaction', [hexstring]) - } - - async validateAddress(address: string): Promise> { - return this.rpcCall>('validateaddress', [address]) - } - - async verifyMessage( - address: string, - signature: string, - message: string, - ): Promise> { - return this.rpcCall>('verifymessage', [address, signature, message]) - } - - async verifyTxOutProof(proof: string): Promise> { - return this.rpcCall>('verifytxoutproof', [proof]) - } } diff --git a/src/service/rpc/utxo/DogeLoadBalancedRpc.ts b/src/service/rpc/utxo/DogeLoadBalancedRpc.ts index c5ab20bb6..911ab3b19 100644 --- a/src/service/rpc/utxo/DogeLoadBalancedRpc.ts +++ b/src/service/rpc/utxo/DogeLoadBalancedRpc.ts @@ -2,9 +2,9 @@ import { Container, Service } from 'typedi' import { JsonRpcCall, JsonRpcResponse, DogeRpcSuite } from '../../../dto' import { Utils } from '../../../util' -import { AbstractDogeRpc } from './AbstractDogeRpc' // Need to import like this to keep browser working import { LoadBalancer } from '../generic/LoadBalancer' +import { AbstractCommonUtxoRpc } from './AbstractCommonUtxoRpc' @Service({ @@ -13,7 +13,7 @@ import { LoadBalancer } from '../generic/LoadBalancer' }, transient: true, }) -export class DogeLoadBalancedRpc extends AbstractDogeRpc implements DogeRpcSuite { +export class DogeLoadBalancedRpc extends AbstractCommonUtxoRpc implements DogeRpcSuite { protected readonly loadBalancer: LoadBalancer constructor(id: string) { @@ -41,4 +41,8 @@ export class DogeLoadBalancedRpc extends AbstractDogeRpc implements DogeRpcSuite getRpcNodeUrl(): string { return this.loadBalancer.getActiveNormalUrlWithFallback().url } + + async getBlock(hashOrHeight: string, verbose = true): Promise> { + return this.rpcCall>('getblock', [hashOrHeight, verbose]) + } } diff --git a/src/service/rpc/utxo/DogeRpc.ts b/src/service/rpc/utxo/DogeRpc.ts deleted file mode 100644 index 326914fc6..000000000 --- a/src/service/rpc/utxo/DogeRpc.ts +++ /dev/null @@ -1,42 +0,0 @@ -/* eslint-disable @typescript-eslint/no-explicit-any */ -import { Container, Service } from 'typedi' -import { JsonRpcCall, JsonRpcResponse, DogeRpcSuite } from '../../../dto' -import { Utils } from '../../../util' -import { GenericRpc } from '../generic' -import { AbstractDogeRpc } from './AbstractDogeRpc' - -@Service({ - factory: (data: { id: string }) => { - return new DogeRpc(data.id) - }, - transient: true, -}) -export class DogeRpc extends AbstractDogeRpc implements DogeRpcSuite { - public readonly genericRpc: GenericRpc - - constructor(id: string) { - super() - this.genericRpc = Container.of(id).get(GenericRpc) - } - - protected async rpcCall(method: string, params?: unknown[]): Promise { - const preparedCall = Utils.prepareRpcCall(method, params) - return (await this.genericRpc.rawRpcCall(preparedCall)) as T - } - - async rawBatchRpcCall(body: JsonRpcCall[]): Promise[] | JsonRpcResponse> { - return this.genericRpc.rawBatchRpcCall(body) - } - - async rawRpcCall(body: JsonRpcCall): Promise { - return (await this.genericRpc.rawRpcCall(body)) as T - } - - destroy(): void { - // do nothing - } - - getRpcNodeUrl(): string { - return this.genericRpc.getRpcNodeUrl() - } -} diff --git a/src/service/rpc/utxo/index.ts b/src/service/rpc/utxo/index.ts index a331bff39..c78cf7ad7 100644 --- a/src/service/rpc/utxo/index.ts +++ b/src/service/rpc/utxo/index.ts @@ -1,3 +1,2 @@ export * from './AbstractUtxoRpc' export * from './UtxoRpc' -export * from './DogeRpc'