Skip to content

Commit

Permalink
Adding getClient method to ChainDefinition
Browse files Browse the repository at this point in the history
This call will return the appropriate client for the given chain.
  • Loading branch information
aaroncox committed Aug 28, 2023
1 parent cf9f0eb commit f6fbfa3
Show file tree
Hide file tree
Showing 8 changed files with 806 additions and 51 deletions.
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ lib: ${SRC_FILES} package.json tsconfig.json node_modules rollup.config.js

.PHONY: test
test: node_modules
@TS_NODE_PROJECT='./test/tsconfig.json' \
@TS_NODE_PROJECT='./test/tsconfig.json' MOCK_DIR='./test/data' \
${BIN}/mocha ${MOCHA_OPTS} ${TEST_FILES} --no-timeout --grep '$(grep)'

test/watch: node_modules
@TS_NODE_PROJECT='./test/tsconfig.json' \
${BIN}/mocha --watch ${MOCHA_OPTS} ${TEST_FILES} --no-timeout --grep '$(grep)'

build/coverage: ${SRC_FILES} ${TEST_FILES} node_modules
@TS_NODE_PROJECT='./test/tsconfig.json' \
@TS_NODE_PROJECT='./test/tsconfig.json' MOCK_DIR='./test/data' \
${BIN}/nyc ${NYC_OPTS} --reporter=html \
${BIN}/mocha ${MOCHA_OPTS} -R nyan ${TEST_FILES}

Expand All @@ -28,7 +28,7 @@ coverage: build/coverage

.PHONY: ci-test
ci-test: node_modules
@TS_NODE_PROJECT='./test/tsconfig.json' \
@TS_NODE_PROJECT='./test/tsconfig.json' MOCK_DIR='./test/data' \
${BIN}/nyc ${NYC_OPTS} --reporter=text \
${BIN}/mocha ${MOCHA_OPTS} -R list ${TEST_FILES}

Expand Down
2 changes: 1 addition & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import typescript from '@rollup/plugin-typescript'
import cleanup from 'rollup-plugin-cleanup'
import pkg from './package.json'

const external = Object.keys(pkg.peerDependencies)
const external = [...Object.keys(pkg.dependencies), ...Object.keys(pkg.peerDependencies)]

/** @type {import('rollup').RollupOptions} */
export default [
Expand Down
75 changes: 66 additions & 9 deletions src/common/chains.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import {Checksum256, Checksum256Type, Struct} from '@wharfkit/antelope'
import {APIClientOptions, Checksum256, Checksum256Type, Struct} from '@wharfkit/antelope'

import {APIClient as LeapAPIClient} from '@wharfkit/apiclient-leap'
import {APIClient as TelosAPIClient} from '@wharfkit/apiclient-leap'
import {APIClient as WAXAPIClient} from '@wharfkit/apiclient-wax'

import {ExplorerDefinition} from './explorer'
import {Logo} from './logo'
Expand All @@ -9,7 +13,7 @@ import type {ExplorerDefinitionType, LogoType} from './types'
* The information required to interact with a given chain.
*/
@Struct.type('chain_definition')
export class ChainDefinition extends Struct {
export class ChainDefinition<ChainIndice extends ChainIndices = 'Antelope'> extends Struct {
/**
* The chain ID.
*/
Expand All @@ -30,12 +34,12 @@ export class ChainDefinition extends Struct {
*/
@Struct.field(ExplorerDefinition, {optional: true}) declare explorer?: ExplorerDefinitionType

static from(data) {
static from<ChainIndice extends ChainIndices = 'Antelope'>(data) {
return super.from({
...data,
explorer: data.explorer ? ExplorerDefinition.from(data.explorer) : undefined,
logo: data.logo ? Logo.from(data.logo) : undefined,
}) as ChainDefinition
}) as ChainDefinition<ChainIndice>
}

get name() {
Expand All @@ -59,12 +63,64 @@ export class ChainDefinition extends Struct {
}
return undefined
}

public getClient(options?: APIClientOptions): ClientType<ChainIndice> {
// Create options that default to URL defined in ChainDefinition
const opts = {
url: this.url,
...options,
}
// Determine if we have a custom client for this chain
const indice = chainIdsToIndices.get(String(this.id))
if (indice) {
const client = ChainClients[indice]
if (client) {
// Return defined client
return new client(opts) as ClientType<ChainIndice>
}
}
// Return generic APIClient when unknown blockchain
return new LeapAPIClient(opts) as ClientType<ChainIndice>
}
}

// Type exports based on ChainClients
export type ClientType<T> =
// Override WAX
T extends 'WAX'
? WAXAPIClient
: // Override WAX Testnet
T extends 'WAXTestnet'
? WAXAPIClient
: // Override Telos
T extends 'Telos'
? TelosAPIClient
: // Override Telos Testnet
T extends 'TelosTestnet'
? TelosAPIClient
: // Default to Leap
LeapAPIClient

export type ChainClientsTypes<I extends string> = {
[K in I]: typeof LeapAPIClient
}

/**
* A mapping of specific chains to their APIClients
*/
export const ChainClients: Partial<ChainClientsTypes<ChainIndices>> = {
Antelope: LeapAPIClient,
Telos: TelosAPIClient,
TelosTestnet: TelosAPIClient,
WAX: WAXAPIClient,
WAXTestnet: WAXAPIClient,
}

/**
* A list of string-based chain names to assist autocompletion
*/
export type ChainIndices =
| 'Antelope'
| 'EOS'
| 'FIO'
| 'FIOTestnet'
Expand All @@ -84,6 +140,7 @@ export type ChainIndices =
* List of human readable chain names based on the ChainIndices type.
*/
export const ChainNames: Record<ChainIndices, string> = {
Antelope: 'Unknown Antelope Chain',
EOS: 'EOS',
FIO: 'FIO',
FIOTestnet: 'FIO (Testnet)',
Expand All @@ -103,7 +160,7 @@ export const ChainNames: Record<ChainIndices, string> = {
/**
* An exported list of ChainDefinition entries for select chains.
*/
export const Chains: Record<ChainIndices, ChainDefinition> = {
export const Chains = {
EOS: ChainDefinition.from({
id: 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906',
url: 'https://eos.greymass.com',
Expand Down Expand Up @@ -160,27 +217,27 @@ export const Chains: Record<ChainIndices, ChainDefinition> = {
id: '71ee83bcf52142d61019d95f9cc5427ba6a0d7ff8accd9e2088ae2abeaf3d3dd',
url: 'https://proton-testnet.greymass.com',
}),
Telos: ChainDefinition.from({
Telos: ChainDefinition.from<'Telos'>({
id: '4667b205c6838ef70ff7988f6e8257e8be0e1284a2f59699054a018f743b1d11',
url: 'https://telos.greymass.com',
explorer: {
prefix: 'https://explorer.telos.net/transaction/',
suffix: '',
},
}),
TelosTestnet: ChainDefinition.from({
TelosTestnet: ChainDefinition.from<'TelosTestnet'>({
id: '1eaa0824707c8c16bd25145493bf062aecddfeb56c736f6ba6397f3195f33c9f',
url: 'https://telos.greymass.com',
}),
WAX: ChainDefinition.from({
WAX: ChainDefinition.from<'WAX'>({
id: '1064487b3cd1a897ce03ae5b6a865651747e2e152090f99c1d19d44e01aea5a4',
url: 'https://wax.greymass.com',
explorer: {
prefix: 'https://waxblock.io/transaction/',
suffix: '',
},
}),
WAXTestnet: ChainDefinition.from({
WAXTestnet: ChainDefinition.from<'WAXTestnet'>({
id: 'f16b1833c747c43682f4386fca9cbb327929334a762755ebec17f6f23c9b8a12',
url: 'https://waxtestnet.greymass.com',
}),
Expand Down
32 changes: 32 additions & 0 deletions test/data/1068183311566483d75b86a6da056815f2387184.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"request": {
"path": "https://eos.greymass.com/v1/chain/get_info",
"params": {
"method": "POST"
}
},
"status": 200,
"json": {
"server_version": "43873e82",
"chain_id": "aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906",
"head_block_num": 328129279,
"last_irreversible_block_num": 328128954,
"last_irreversible_block_id": "138ed9ba7819e0b797b9ef24915e2d96a94ad4f22b578d3b983a43143deb2ea3",
"head_block_id": "138edaff7955888f12a7f5d2d288ff49ccbc42ee4f96d57d1ddf29b9678e77ce",
"head_block_time": "2023-08-28T20:00:18.000",
"head_block_producer": "atticlabeosb",
"virtual_block_cpu_limit": 200000,
"virtual_block_net_limit": 1048576000,
"block_cpu_limit": 200000,
"block_net_limit": 1048576,
"server_version_string": "v3.2.3-hotfix",
"fork_db_head_block_num": 328129279,
"fork_db_head_block_id": "138edaff7955888f12a7f5d2d288ff49ccbc42ee4f96d57d1ddf29b9678e77ce",
"server_full_version_string": "v3.2.3-hotfix-43873e822160be5e5f364fd867e3c7d27162cd0c-dirty",
"total_cpu_weight": "383375678612944",
"total_net_weight": "96156749298177",
"earliest_available_block_num": 327956125,
"last_irreversible_block_time": "2023-08-28T19:57:35.500"
},
"text": "{\"server_version\":\"43873e82\",\"chain_id\":\"aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906\",\"head_block_num\":328129279,\"last_irreversible_block_num\":328128954,\"last_irreversible_block_id\":\"138ed9ba7819e0b797b9ef24915e2d96a94ad4f22b578d3b983a43143deb2ea3\",\"head_block_id\":\"138edaff7955888f12a7f5d2d288ff49ccbc42ee4f96d57d1ddf29b9678e77ce\",\"head_block_time\":\"2023-08-28T20:00:18.000\",\"head_block_producer\":\"atticlabeosb\",\"virtual_block_cpu_limit\":200000,\"virtual_block_net_limit\":1048576000,\"block_cpu_limit\":200000,\"block_net_limit\":1048576,\"server_version_string\":\"v3.2.3-hotfix\",\"fork_db_head_block_num\":328129279,\"fork_db_head_block_id\":\"138edaff7955888f12a7f5d2d288ff49ccbc42ee4f96d57d1ddf29b9678e77ce\",\"server_full_version_string\":\"v3.2.3-hotfix-43873e822160be5e5f364fd867e3c7d27162cd0c-dirty\",\"total_cpu_weight\":\"383375678612944\",\"total_net_weight\":\"96156749298177\",\"earliest_available_block_num\":327956125,\"last_irreversible_block_time\":\"2023-08-28T19:57:35.500\"}"
}
Loading

0 comments on commit f6fbfa3

Please sign in to comment.