Skip to content

Commit

Permalink
ALL-3150 - add bch estimatefee method (#1001)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hathoriel authored Oct 24, 2023
1 parent c5590f5 commit eb79ee3
Show file tree
Hide file tree
Showing 11 changed files with 166 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [4.1.15] - 2023.10.24
### Added
- Added estimatefee rpc method to the Bitcoin Cash network

## [4.1.14] - 2023.10.23
### Added
- Added IPFS get file data method
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@tatumio/tatum",
"version": "4.1.14",
"version": "4.1.15",
"description": "Tatum JS SDK",
"author": "Tatum",
"repository": "https://github.com/tatumio/tatum-js",
Expand Down
15 changes: 14 additions & 1 deletion src/dto/Network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,14 @@ export const UTXO_BASED_NETWORKS = [
Network.DOGECOIN_TESTNET,
]

export const UTXO_LOAD_BALANCER_ESTIMATE_FEE_NETWORKS = [
Network.BITCOIN_CASH,
]

export const UTXO_ESTIMATE_FEE_NETWORKS = [
Network.BITCOIN_CASH_TESTNET,
]

export const DATA_API_UTXO_NETWORKS = [
Network.BITCOIN,
Network.BITCOIN_TESTNET,
Expand Down Expand Up @@ -255,6 +263,12 @@ export const isEvmBasedNetwork = (network: Network) => EVM_BASED_NETWORKS.includ

export const isUtxoBasedNetwork = (network: Network) => UTXO_BASED_NETWORKS.includes(network)

export const isUtxoLoadBalancerEstimateFeeNetwork = (network: Network) => UTXO_LOAD_BALANCER_ESTIMATE_FEE_NETWORKS.includes(network)

export const isUtxoEstimateFeeNetwork = (network: Network) => UTXO_ESTIMATE_FEE_NETWORKS.includes(network)

export const isUtxoLoadBalancerNetwork = (network: Network) => UTXO_LOAD_BALANCER_NETWORKS.includes(network)

export const isXrpNetwork = (network: Network) => [Network.XRP, Network.XRP_TESTNET].includes(network)

export const isDataApiEvmEnabledNetwork = (network: Network) => DATA_API_EVM_NETWORKS.includes(network)
Expand All @@ -271,7 +285,6 @@ export const isEosNetwork = (network: Network) => EOS_NETWORKS.includes(network)

export const isLoadBalancerNetwork = (network: Network) => LOAD_BALANCER_NETWORKS.includes(network)

export const isUtxoLoadBalancerNetwork = (network: Network) => UTXO_LOAD_BALANCER_NETWORKS.includes(network)

export const isEvmLoadBalancerNetwork = (network: Network) => EVM_LOAD_BALANCER_NETWORKS.includes(network)

Expand Down
6 changes: 6 additions & 0 deletions src/dto/rpc/UtxoBasedRpcSuite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ export interface UtxoBasedRpcInterface {
validateAddress(address: string): Promise<JsonRpcResponse<any>>
verifyMessage(address: string, signature: string, message: string): Promise<JsonRpcResponse<boolean>>
}

export interface UtxoBasedRpcInterfaceEstimateFee extends UtxoBasedRpcInterface {
estimateFee(): Promise<JsonRpcResponse<any>>
}

export interface UtxoBasedRpcSuiteEstimateFee extends UtxoBasedRpcSuite, UtxoBasedRpcInterfaceEstimateFee {}
22 changes: 22 additions & 0 deletions src/e2e/rpc/utxo/tatum.rpc.bch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { BitcoinCash, Network } from '../../../service'
import { UtxoE2eUtils, UtxoNetworkType } from './utxo.e2e.utils'

describe('Bitcoin Cash', () => {
describe('mainnet', () => {
it('estimatefee', async () => {
const tatum = await UtxoE2eUtils.initTatum<BitcoinCash>({ network: Network.BITCOIN_CASH, type: UtxoNetworkType.MAIN })
const result = await tatum.rpc.estimateFee()
await tatum.destroy()
expect(result.result).not.toBeNull()
})
})

describe('testnet', () => {
it('estimatefee', async () => {
const tatum = await UtxoE2eUtils.initTatum<BitcoinCash>({ network: Network.BITCOIN_CASH, type: UtxoNetworkType.TEST })
const result = await tatum.rpc.estimateFee()
await tatum.destroy()
expect(result.result).not.toBeNull()
})
})
})
4 changes: 2 additions & 2 deletions src/e2e/rpc/utxo/utxo.e2e.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ interface TatumBtcUtils {
}

export const UtxoE2eUtils = {
initTatum: async (params: TatumBtcUtils) =>
TatumSDK.init<BaseUtxo>(RpcE2eUtils.initConfig(params.network, params.apiKey)),
initTatum: async <T extends BaseUtxo>(params: TatumBtcUtils) =>
TatumSDK.init<T>(RpcE2eUtils.initConfig(params.network, params.apiKey)),
e2e: (params: TatumBtcUtils) => {
const { type } = params
it('chain info', async () => {
Expand Down
9 changes: 9 additions & 0 deletions src/service/rpc/utxo/AbstractUtxoRpcEstimateFee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { JsonRpcResponse, UtxoBasedRpcInterfaceEstimateFee } from '../../../dto'
import { AbstractUtxoRpc } from './AbstractUtxoRpc'

export abstract class AbstractUtxoRpcEstimateFee extends AbstractUtxoRpc implements UtxoBasedRpcInterfaceEstimateFee {
estimateFee(): Promise<JsonRpcResponse<any>> {
return this.rpcCall<JsonRpcResponse<any>>('estimatefee')
}
}
46 changes: 46 additions & 0 deletions src/service/rpc/utxo/UtxoLoadBalancerRpcEstimateFee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Container, Service } from 'typedi'
import {
JsonRpcCall,
JsonRpcResponse,
UtxoBasedRpcSuiteEstimateFee,
} from '../../../dto'
import { Utils } from '../../../util'
import { LoadBalancer } from '../generic'
import { AbstractUtxoRpcEstimateFee } from './AbstractUtxoRpcEstimateFee'

@Service({
factory: (data: { id: string }) => {
return new UtxoLoadBalancerRpcEstimateFee(data.id)
},
transient: true,
})
export class UtxoLoadBalancerRpcEstimateFee extends AbstractUtxoRpcEstimateFee implements UtxoBasedRpcSuiteEstimateFee {
protected readonly loadBalancerRpc: LoadBalancer

constructor(id: string) {
super()
this.loadBalancerRpc = Container.of(id).get(LoadBalancer)
}

protected async rpcCall<T>(method: string, params?: unknown[]): Promise<T> {
const preparedCall = Utils.prepareRpcCall(method, params)
return (await this.loadBalancerRpc.rawRpcCall(preparedCall)) as T
}

async rawRpcCall(body: JsonRpcCall): Promise<JsonRpcResponse<any>> {
return this.loadBalancerRpc.rawRpcCall(body)
}

rawBatchRpcCall(body: JsonRpcCall[]): Promise<JsonRpcResponse<any>[] | JsonRpcResponse<any>> {
return this.loadBalancerRpc.rawBatchRpcCall(body)
}

public destroy() {
this.loadBalancerRpc.destroy()
}

getRpcNodeUrl(): string {
return this.loadBalancerRpc.getActiveNormalUrlWithFallback().url
}
}
42 changes: 42 additions & 0 deletions src/service/rpc/utxo/UtxoRpcEstimateFee.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { Container, Service } from 'typedi'
import { JsonRpcCall, JsonRpcResponse, UtxoBasedRpcSuiteEstimateFee } from '../../../dto'
import { Utils } from '../../../util'
import { GenericRpc } from '../generic'
import { AbstractUtxoRpcEstimateFee } from './AbstractUtxoRpcEstimateFee'

@Service({
factory: (data: { id: string }) => {
return new UtxoRpcEstimateFee(data.id)
},
transient: true,
})
export class UtxoRpcEstimateFee extends AbstractUtxoRpcEstimateFee implements UtxoBasedRpcSuiteEstimateFee {
public readonly genericRpc: GenericRpc

constructor(id: string) {
super()
this.genericRpc = Container.of(id).get(GenericRpc)
}

protected async rpcCall<T>(method: string, params?: unknown[]): Promise<T> {
const preparedCall = Utils.prepareRpcCall(method, params)
return (await this.genericRpc.rawRpcCall(preparedCall)) as T
}

async rawBatchRpcCall(body: JsonRpcCall[]): Promise<JsonRpcResponse<any>[] | JsonRpcResponse<any>> {
return this.genericRpc.rawBatchRpcCall(body)
}

async rawRpcCall<T>(body: JsonRpcCall): Promise<T> {
return (await this.genericRpc.rawRpcCall(body)) as T
}

destroy(): void {
// do nothing
}

getRpcNodeUrl(): string {
return this.genericRpc.getRpcNodeUrl()
}
}
11 changes: 9 additions & 2 deletions src/service/tatum/tatum.utxo.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Container } from 'typedi'
import { UtxoBasedRpcSuite } from '../../dto'
import { UtxoBasedRpcSuite, UtxoBasedRpcSuiteEstimateFee } from '../../dto'
import { CONFIG, Utils } from '../../util'
import { Address } from '../address'
import { FeeUtxo } from '../fee'
Expand Down Expand Up @@ -49,5 +49,12 @@ export abstract class FullUtxo extends NotificationUtxo {
export class Bitcoin extends FullUtxo {}
export class Litecoin extends FullUtxo {}
export class Dogecoin extends FullUtxo {}
export class BitcoinCash extends NotificationUtxo {}
export class BitcoinCash extends NotificationUtxo {
rpc: UtxoBasedRpcSuiteEstimateFee

constructor(id: string) {
super(id)
this.rpc = Utils.getRpc<UtxoBasedRpcSuiteEstimateFee>(id, Container.of(id).get(CONFIG))
}
}
export class ZCash extends BaseUtxo {}
12 changes: 11 additions & 1 deletion src/util/util.shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
isSolanaNetwork,
isTronLoadBalancerNetwork,
isTronNetwork,
isUtxoBasedNetwork,
isUtxoBasedNetwork, isUtxoEstimateFeeNetwork, isUtxoLoadBalancerEstimateFeeNetwork,
isUtxoLoadBalancerNetwork,
isXrpNetwork,
JsonRpcCall,
Expand Down Expand Up @@ -77,10 +77,20 @@ import { XrpLoadBalancerRpc } from '../service/rpc/other/XrpLoadBalancerRpc'
import { UtxoLoadBalancerRpc } from '../service/rpc/utxo/UtxoLoadBalancerRpc'
import { Constant } from './constant'
import { CONFIG } from './di.tokens'
import { UtxoLoadBalancerRpcEstimateFee } from '../service/rpc/utxo/UtxoLoadBalancerRpcEstimateFee'
import { UtxoRpcEstimateFee } from '../service/rpc/utxo/UtxoRpcEstimateFee'

export const Utils = {
getRpc: <T>(id: string, config: TatumConfig): T => {
const { network } = config
if (isUtxoLoadBalancerEstimateFeeNetwork(network)) {
return Container.of(id).get(UtxoLoadBalancerRpcEstimateFee) as T
}

if (isUtxoEstimateFeeNetwork(network)) {
return Container.of(id).get(UtxoRpcEstimateFee) as T
}

if (isUtxoLoadBalancerNetwork(network)) {
return Container.of(id).get(UtxoLoadBalancerRpc) as T
}
Expand Down

0 comments on commit eb79ee3

Please sign in to comment.