Skip to content

Commit

Permalink
ALL-7216 - Add electrs & iota, rename rostrum
Browse files Browse the repository at this point in the history
  • Loading branch information
Hathoriel committed Jun 3, 2024
1 parent c3b1b94 commit 8462e3b
Show file tree
Hide file tree
Showing 18 changed files with 309 additions and 140 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## [4.2.30] - 2024.5.31

### Added

- Support for Electrs calls and Iota testnet, renamed bch rostrum

## [4.2.29] - 2024.5.27

### Added
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.2.29",
"version": "4.2.30",
"description": "Tatum JS SDK",
"author": "Tatum",
"repository": "https://github.com/tatumio/tatum-js",
Expand Down
4 changes: 3 additions & 1 deletion src/connector/tatum.connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ export class TatumConnector {
basePath,
}: GetUrl<PARAMS>) {
const config = Container.of(this.id).get(CONFIG)
const url = new URL(path || '', basePath || this.getBaseUrl())
const base = basePath || this.getBaseUrl()

const url = new URL(path && path?.length > 1 ? `${base}${path}` : base)

if (params) {
Object.keys(params)
Expand Down
25 changes: 22 additions & 3 deletions src/dto/Network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export enum Network {
PALM = 'palm-mainnet',
POLYGON = 'polygon-mainnet',
POLKADOT = 'dot-mainnet',
ROSTRUM = 'rostrum-mainnet',
ROSTRUM = 'bch-mainnet-rostrum',
RSK = 'rsk-mainnet',
SOLANA = 'solana-mainnet',
STELLAR = 'stellar-mainnet',
Expand All @@ -54,6 +54,8 @@ export enum Network {
XRP = 'ripple-mainnet',
ZCASH = 'zcash-mainnet',
ZILLIQA = 'zilliqa-mainnet',
BITCOIN_ELECTRS = 'bitcoin-mainnet-electrs',


// Testnets
ALGORAND_ALGOD_TESTNET = 'algorand-testnet-algod',
Expand Down Expand Up @@ -104,6 +106,7 @@ export enum Network {
XRP_TESTNET = 'ripple-testnet',
ZCASH_TESTNET = 'zcash-testnet',
ZILLIQA_TESTNET = 'zilliqa-testnet',
IOTA_TESTNET = 'iota-testnet',
}

export const EVM_BASED_NETWORKS = [
Expand Down Expand Up @@ -253,8 +256,10 @@ export const CARDANO_NETWORKS = [
]
export const STELLAR_LOAD_BALANCER_NETWORKS = [Network.STELLAR]
export const KADENA_LOAD_BALANCER_NETWORKS = [Network.KADENA, Network.KADENA_TESTNET]
export const ROSTRUM_LOAD_BALANCER_NETWORKS = [Network.ROSTRUM]
export const ROSTRUM_LOAD_BALANCER_NETWORKS = [Network.ROSTRUM, Network.BITCOIN_ELECTRS]
export const IOTA_LOAD_BALANCER_NETWORKS = [Network.IOTA]
export const BITCOIN_ELECTRS_NETWORKS = [Network.BITCOIN_ELECTRS]
export const IOTA_NETWORKS = [Network.IOTA, Network.IOTA_TESTNET]

export const LOAD_BALANCER_NETWORKS = [
...UTXO_LOAD_BALANCER_NETWORKS,
Expand All @@ -274,6 +279,7 @@ export const LOAD_BALANCER_NETWORKS = [
...KADENA_LOAD_BALANCER_NETWORKS,
...ROSTRUM_LOAD_BALANCER_NETWORKS,
...IOTA_LOAD_BALANCER_NETWORKS,
...BITCOIN_ELECTRS_NETWORKS,
]

export const EVM_ARCHIVE_NON_ARCHIVE_LOAD_BALANCER_NETWORKS = [
Expand Down Expand Up @@ -364,7 +370,11 @@ export const isStellarLoadBalancerNetwork = (network: Network) =>
export const isStellarNetwork = (network: Network) =>
[Network.STELLAR, Network.STELLAR_TESTNET].includes(network)

export const isIotaNetwork = (network: Network) => IOTA_LOAD_BALANCER_NETWORKS.includes(network)
export const isIotaLoadBalancerNetwork = (network: Network) => IOTA_LOAD_BALANCER_NETWORKS.includes(network)

export const isIotaNetwork = (network: Network) => IOTA_NETWORKS.includes(network)

export const isElectrsNetwork = (network: Network) => BITCOIN_ELECTRS_NETWORKS.includes(network)

export const isSameGetBlockNetwork = (network: Network) =>
isUtxoBasedNetwork(network) ||
Expand Down Expand Up @@ -918,4 +928,13 @@ export const NETWORK_METADATA: Record<Network, NetworkMetadata> = {
testnet: false,
defaultMainnet: true,
},
[Network.IOTA_TESTNET]: {
currency: Currency.IOTA,
testnet: true,
defaultTestnet: true,
},
[Network.BITCOIN_ELECTRS]: {
currency: Currency.BTC,
testnet: false,
},
}
22 changes: 22 additions & 0 deletions src/e2e/rpc/other/tatum.rpc.electrs.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Network, BitcoinElectrs, TatumSDK } from '../../../service'
import { e2eUtil } from '../../e2e.util'

const getElectrsRpc = async () => await TatumSDK.init<BitcoinElectrs>(e2eUtil.initConfig(Network.BITCOIN_ELECTRS))

describe('Electrs', () => {

it('blockchain.headers.subscribe', async () => {
const electrs = await getElectrsRpc()
const result = await electrs.rpc.blockchainHeadersSubscribe()
await electrs.destroy()
expect(result.result?.hex).toBeDefined()
expect(result.result?.height).toBeDefined()
})

it('server.banner', async () => {
const electrs = await getElectrsRpc()
const result = await electrs.rpc.serverBanner()
await electrs.destroy()
expect(result.result).toBeDefined()
})
})
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { Iota, Network, TatumSDK } from '../../../service'
import { e2eUtil } from '../../e2e.util'

const getIotaRpc = async () => await TatumSDK.init<Iota>(e2eUtil.initConfig(Network.IOTA))
const getIotaRpc = async (testnet?: boolean) => await TatumSDK.init<Iota>(e2eUtil.initConfig(testnet ? Network.IOTA_TESTNET : Network.IOTA))

describe('Iota', () => {
describe('mainnet', () => {
describe.each([true, false])('Iota', (testnet) => {
describe(`${testnet ? Network.IOTA_TESTNET : Network.IOTA}`, () => {
it('getNodeInfo', async () => {
const tatum = await getIotaRpc()
const tatum = await getIotaRpc(testnet)
const info = await tatum.rpc.getNodeInfo()
await tatum.destroy()
expect(info).toBeDefined()
})

it('getTips', async () => {
const tatum = await getIotaRpc()
const tatum = await getIotaRpc(testnet)
const tips = await tatum.rpc.getTips()
await tatum.destroy()
expect(tips).toBeDefined()
})

it('getReceipts', async () => {
const tatum = await getIotaRpc()
const tatum = await getIotaRpc(testnet)
const receipts = await tatum.rpc.getAllReceipts()
await tatum.destroy()
expect(receipts).toBeDefined()
Expand Down
24 changes: 18 additions & 6 deletions src/service/rpc/evm/AbstractTronRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
FreezeAccountOptions,
GetCanWithdrawUnfreezeAmountOptions,
JsonRpcCall,
JsonRpcResponse,
JsonRpcResponse, QueryParams,
TransferAssetIssueByAccountOptions,
TriggerConstantContractOptions,
TriggerSmartContractOptions,
Expand All @@ -30,10 +30,12 @@ import {
import { PostI } from '../../../dto/PostI'
import { Utils } from '../../../util'
import { AbstractEvmRpc } from './AbstractEvmRpc'
import { GetI } from '../../../dto/GetI'

@Service()
export abstract class AbstractTronRpc extends AbstractEvmRpc implements TronRpcSuite {
protected abstract post<T>(post: PostI): Promise<T>
protected abstract get<T>(get: GetI): Promise<T>
abstract destroy(): void
abstract getRpcNodeUrl(): string

Expand Down Expand Up @@ -61,6 +63,16 @@ export abstract class AbstractTronRpc extends AbstractEvmRpc implements TronRpcS
return this.post(post)
}

private async sendGet<T>({ path, queryParams }: { path: string; queryParams?: QueryParams }): Promise<T> {
return this.get({
path: Utils.addQueryParams({
basePath: path,
strategy: Utils.camelToDashCase,
queryParams: queryParams,
}),
})
}

accountPermissionUpdate(
ownerAddress: string,
actives: TronPermission[],
Expand Down Expand Up @@ -351,7 +363,7 @@ export abstract class AbstractTronRpc extends AbstractEvmRpc implements TronRpcS
}

getBurnTRX(): Promise<any> {
return this.sendPost({
return this.sendGet({
path: '/wallet/getburntrx',
})
}
Expand All @@ -378,7 +390,7 @@ export abstract class AbstractTronRpc extends AbstractEvmRpc implements TronRpcS
}

getChainParameters(): Promise<any> {
return this.sendPost({
return this.sendGet({
path: '/wallet/getchainparameters',
})
}
Expand Down Expand Up @@ -426,13 +438,13 @@ export abstract class AbstractTronRpc extends AbstractEvmRpc implements TronRpcS
}

getEnergyPrices(): Promise<TronPrices> {
return this.sendPost({
return this.sendGet({
path: '/wallet/getenergyprices',
})
}

getNodeInfo(): Promise<any> {
return this.sendPost({
return this.sendGet({
path: '/wallet/getnodeinfo',
})
}
Expand Down Expand Up @@ -472,7 +484,7 @@ export abstract class AbstractTronRpc extends AbstractEvmRpc implements TronRpcS
}

listNodes(): Promise<any> {
return this.sendPost({
return this.sendGet({
path: '/wallet/listnodes',
})
}
Expand Down
12 changes: 9 additions & 3 deletions src/service/rpc/evm/TronLoadBalancerRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { PostI } from '../../../dto/PostI'
import { Logger } from '../../../service/logger/logger.types'
import { LOGGER, Utils } from '../../../util'
// Need to import like this to keep browser working
import { LoadBalancer } from '../generic/LoadBalancer'
import { AbstractTronRpc } from './AbstractTronRpc'
import { GetI } from '../../../dto/GetI'
import { TronLoadBalancer } from '../generic/LoadBalancer'

@Service({
factory: (data: { id: string }) => {
Expand All @@ -15,12 +16,12 @@ import { AbstractTronRpc } from './AbstractTronRpc'
transient: true,
})
export class TronLoadBalancerRpc extends AbstractTronRpc implements EvmBasedRpcSuite {
protected readonly loadBalancer: LoadBalancer
protected readonly loadBalancer: TronLoadBalancer
protected readonly logger: Logger

constructor(id: string) {
super()
this.loadBalancer = Container.of(id).get(LoadBalancer)
this.loadBalancer = Container.of(id).get(TronLoadBalancer)
this.logger = Container.of(id).get(LOGGER)
}

Expand All @@ -29,6 +30,7 @@ export class TronLoadBalancerRpc extends AbstractTronRpc implements EvmBasedRpcS
return (await this.loadBalancer.rawRpcCall(preparedCall)) as T
}


async rawRpcCall(body: JsonRpcCall): Promise<JsonRpcResponse<any>> {
return this.loadBalancer.rawRpcCall(body)
}
Expand All @@ -45,6 +47,10 @@ export class TronLoadBalancerRpc extends AbstractTronRpc implements EvmBasedRpcS
return this.loadBalancer.post(post)
}

protected get<T>(get: GetI): Promise<T> {
return this.loadBalancer.get(get)
}

getRpcNodeUrl(): string {
return this.loadBalancer.getActiveNormalUrlWithFallback().url
}
Expand Down
7 changes: 7 additions & 0 deletions src/service/rpc/evm/TronRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { CONFIG, Constant, LOGGER, Utils } from '../../../util'
import { TatumConfig } from '../../tatum'
import { GenericRpc } from '../generic/GenericRpc'
import { AbstractTronRpc } from './AbstractTronRpc'
import { GetI } from '../../../dto/GetI'

@Service({
factory: (data: { id: string }) => {
Expand Down Expand Up @@ -49,6 +50,12 @@ export class TronRpc extends AbstractTronRpc {
})
}

protected get<T>(get: GetI): Promise<T> {
return this.connector.get({
basePath: `${Constant.TRON_SHASTA_BASE_URL.BASE}${get.path}`,
})
}

destroy(): void {
// do nothing
}
Expand Down
Loading

0 comments on commit 8462e3b

Please sign in to comment.