-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
328 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { JsonRpcResponse } from '../JsonRpcResponse.dto' | ||
|
||
export interface Height { | ||
height: string | ||
} | ||
|
||
export interface AbciQuery { | ||
path: string, | ||
data: string | ||
height?: string | ||
prove?: boolean | ||
} | ||
|
||
export interface Validators { | ||
height?: string | ||
page?: string | ||
perPage?: string | ||
} | ||
|
||
export interface Tx { hash: string, prove?: boolean } | ||
|
||
export interface TxSearch { query: string, prove?: boolean, page?: string } | ||
|
||
export interface Broadcast { tx: string } | ||
|
||
export interface Blockchain { minHeight?: string, maxHeight?: string } | ||
|
||
export interface UnconfirmedTxs { limit?: string } | ||
|
||
export interface BnbRpcInterface { | ||
status(): Promise<JsonRpcResponse<any>> | ||
abciInfo(): Promise<JsonRpcResponse<any>> | ||
abciQuery(params: AbciQuery): Promise<JsonRpcResponse<any>> | ||
block(params?: Height): Promise<JsonRpcResponse<any>> | ||
blockResult(params?: Height): Promise<JsonRpcResponse<any>> | ||
blockchain(params?: Blockchain): Promise<JsonRpcResponse<any>> | ||
commit(params?: Height): Promise<JsonRpcResponse<any>> | ||
tx(params: Tx): Promise<JsonRpcResponse<any>> | ||
broadcastTxAsync(params: Broadcast): Promise<JsonRpcResponse<any>> | ||
broadcastTxCommit(params: Broadcast): Promise<JsonRpcResponse<any>> | ||
broadcastTxSync(params: Broadcast): Promise<JsonRpcResponse<any>> | ||
txSearch(params: TxSearch): Promise<JsonRpcResponse<any>> | ||
validators(params?: Validators): Promise<JsonRpcResponse<any>> | ||
unconfirmedTxs(params: UnconfirmedTxs): Promise<JsonRpcResponse<any>> | ||
genesis(): Promise<JsonRpcResponse<any>> | ||
health(): Promise<JsonRpcResponse<any>> | ||
netInfo(): Promise<JsonRpcResponse<any>> | ||
numUnconfirmedTxs(): Promise<JsonRpcResponse<any>> | ||
} | ||
|
||
export interface JsonBnbRpcCall { | ||
id: number | string | ||
jsonrpc: string | ||
method: string | ||
params?: Params | ||
} | ||
|
||
export interface Params { | ||
[key: string]: unknown; | ||
} | ||
|
||
export interface AbstractBnbRpcInterface { | ||
rawRpcCall(body: JsonBnbRpcCall): Promise<JsonRpcResponse<any>> | ||
destroy(): void | ||
getRpcNodeUrl(): string | ||
} | ||
|
||
export interface BnbRpcSuite extends BnbRpcInterface, AbstractBnbRpcInterface {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import * as process from 'process' | ||
import { Bnb, Network, TatumSDK } from '../../../service' | ||
import { e2eUtil } from '../../e2e.util' | ||
|
||
const getBnbRpc = async () => | ||
await TatumSDK.init<Bnb>({ | ||
network: Network.BNB, | ||
apiKey: { | ||
v4: process.env.V4_API_KEY_MAINNET | ||
}, | ||
verbose: e2eUtil.isVerbose, | ||
}) | ||
|
||
// Testnet is not available | ||
describe('Bnb', () => { | ||
describe('mainnet', () => { | ||
it('block', async () => { | ||
const tatum = await getBnbRpc() | ||
const { result } = await tatum.rpc.block() | ||
await tatum.destroy() | ||
expect(result).toBeDefined() | ||
}) | ||
|
||
it('abciInfo', async () => { | ||
const tatum = await getBnbRpc() | ||
const { result } = await tatum.rpc.abciInfo() | ||
await tatum.destroy() | ||
expect(result).toBeDefined() | ||
}) | ||
|
||
it('blockchain', async () => { | ||
const tatum = await getBnbRpc() | ||
const { result } = await tatum.rpc.blockchain() | ||
await tatum.destroy() | ||
expect(result).toBeDefined() | ||
}) | ||
|
||
it('health', async () => { | ||
const tatum = await getBnbRpc() | ||
const { result } = await tatum.rpc.health() | ||
await tatum.destroy() | ||
expect(result).toBeDefined() | ||
}) | ||
|
||
it('genesis', async () => { | ||
const tatum = await getBnbRpc() | ||
const { result } = await tatum.rpc.genesis() | ||
await tatum.destroy() | ||
expect(result).toBeDefined() | ||
}) | ||
|
||
it('validators', async () => { | ||
const tatum = await getBnbRpc() | ||
const { result } = await tatum.rpc.validators() | ||
await tatum.destroy() | ||
expect(result).toBeDefined() | ||
}) | ||
|
||
it('unconfirmedTxs', async () => { | ||
const tatum = await getBnbRpc() | ||
const { result } = await tatum.rpc.unconfirmedTxs({ limit: '1' }) | ||
await tatum.destroy() | ||
expect(result).toBeDefined() | ||
}) | ||
|
||
it('raw rpc call', async () => { | ||
const tatum = await getBnbRpc() | ||
const { result } = await tatum.rpc.rawRpcCall({ method: 'block', id: 1, jsonrpc: '2.0', params: {} }) | ||
await tatum.destroy() | ||
expect(result).toBeDefined() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { JsonRpcResponse } from '../../../dto' | ||
import { Height, AbciQuery, BnbRpcInterface, Blockchain, Tx, Broadcast, TxSearch } from '../../../dto/rpc/BnbRpcSuite' | ||
import { PostI } from '../../../dto/PostI' | ||
import { Utils } from '../../../util' | ||
|
||
export abstract class AbstractBnbRpc implements BnbRpcInterface { | ||
protected abstract post<T>(post: PostI): Promise<T> | ||
|
||
sendRpcCall<T, U>(method: string, params?: T): Promise<U> { | ||
const body = { | ||
id: 1, | ||
jsonrpc: '2.0', | ||
method, | ||
params: params ? Utils.convertObjCamelToSnake(params) : {}, | ||
} | ||
return this.post({ body, path: '' }) | ||
} | ||
|
||
status(): Promise<JsonRpcResponse<any>> { | ||
return this.sendRpcCall('status') | ||
} | ||
|
||
abciInfo(): Promise<JsonRpcResponse<any>> { | ||
return this.sendRpcCall('abci_info') | ||
} | ||
|
||
abciQuery(params: AbciQuery): Promise<JsonRpcResponse<any>> { | ||
return this.sendRpcCall('abci_query', params) | ||
} | ||
|
||
block(params?: Height): Promise<JsonRpcResponse<any>> { | ||
return this.sendRpcCall('block', params) | ||
} | ||
|
||
blockResult(params?: Height): Promise<JsonRpcResponse<any>> { | ||
return this.sendRpcCall('block_result', params) | ||
} | ||
|
||
blockchain(params?: Blockchain): Promise<JsonRpcResponse<any>> { | ||
return this.sendRpcCall('blockchain', params) | ||
} | ||
|
||
commit(params?: Height): Promise<JsonRpcResponse<any>> { | ||
return this.sendRpcCall('commit', params) | ||
} | ||
|
||
tx(params: Tx): Promise<JsonRpcResponse<any>> { | ||
return this.sendRpcCall('tx', params) | ||
} | ||
|
||
broadcastTxAsync(params: Broadcast): Promise<JsonRpcResponse<any>> { | ||
return this.sendRpcCall('broadcast_tx_async', params) | ||
} | ||
|
||
broadcastTxCommit(params: Broadcast): Promise<JsonRpcResponse<any>> { | ||
return this.sendRpcCall('broadcast_tx_commit', params) | ||
} | ||
|
||
broadcastTxSync(params: Broadcast): Promise<JsonRpcResponse<any>> { | ||
return this.sendRpcCall('broadcast_tx_sync', params) | ||
} | ||
|
||
txSearch(params: TxSearch): Promise<JsonRpcResponse<any>> { | ||
return this.sendRpcCall('tx_search', params) | ||
} | ||
|
||
validators(params: Height): Promise<JsonRpcResponse<any>> { | ||
return this.sendRpcCall('validators', params) | ||
} | ||
|
||
unconfirmedTxs(params: { limit: string }): Promise<JsonRpcResponse<any>> { | ||
return this.sendRpcCall('unconfirmed_txs', params) | ||
} | ||
|
||
genesis(): Promise<JsonRpcResponse<any>> { | ||
return this.sendRpcCall('genesis') | ||
} | ||
|
||
health(): Promise<JsonRpcResponse<any>> { | ||
return this.sendRpcCall('health') | ||
} | ||
|
||
netInfo(): Promise<JsonRpcResponse<any>> { | ||
return this.sendRpcCall('net_info') | ||
} | ||
|
||
numUnconfirmedTxs(): Promise<JsonRpcResponse<any>> { | ||
return this.sendRpcCall('num_unconfirmed_txs') | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
import { Container, Service } from 'typedi' | ||
import { LoadBalancer } from '../generic' | ||
import { AbstractBnbRpc } from './AbstractBnbRpc' | ||
import { BnbRpcSuite, JsonBnbRpcCall } from '../../../dto/rpc/BnbRpcSuite' | ||
import { JsonRpcResponse } from '../../../dto' | ||
import { PostI } from '../../../dto/PostI' | ||
|
||
@Service({ | ||
factory: (data: { id: string }) => { | ||
return new BnbLoadBalancerRpc(data.id) | ||
}, | ||
transient: true, | ||
}) | ||
export class BnbLoadBalancerRpc extends AbstractBnbRpc implements BnbRpcSuite { | ||
protected readonly loadBalancer: LoadBalancer | ||
|
||
constructor(id: string) { | ||
super() | ||
this.loadBalancer = Container.of(id).get(LoadBalancer) | ||
} | ||
|
||
public destroy() { | ||
this.loadBalancer.destroy() | ||
} | ||
|
||
getRpcNodeUrl(): string { | ||
return this.loadBalancer.getActiveNormalUrlWithFallback().url | ||
} | ||
|
||
rawRpcCall(body: JsonBnbRpcCall): Promise<JsonRpcResponse<any>> { | ||
return this.loadBalancer.post({ body, path: '' }) | ||
} | ||
|
||
protected post<T>(post: PostI): Promise<T> { | ||
return this.loadBalancer.post(post) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.