Skip to content

Commit

Permalink
ALL-2670 Make classes type safe (#993)
Browse files Browse the repository at this point in the history
* ALL-2670 Make classes type safe

* ALL-2670 Make classes type safe - runned lint
  • Loading branch information
Hathoriel authored Oct 18, 2023
1 parent 3af3824 commit 696f280
Show file tree
Hide file tree
Showing 33 changed files with 444 additions and 324 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.8] - 2023.10.18
### Changed
- Make Blockchain Classes type safe

## [4.1.7] - 2023.10.15
### Changed
- `TatumUtils` chainId <-> `Network` mappings always return usable value or throw error for ease of use.
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.7",
"version": "4.1.8",
"description": "Tatum JS SDK",
"author": "Tatum",
"repository": "https://github.com/tatumio/tatum-js",
Expand Down
6 changes: 5 additions & 1 deletion src/dto/Network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,11 @@ export const isXrpLoadBalancerNetwork = (network: Network) => XRP_LOAD_BALANCER_
export const isNativeEvmLoadBalancerNetwork = (network: Network) =>
NATIVE_EVM_LOAD_BALANCER_NETWORKS.includes(network)

export const isSameGetBlockNetwork = (network: Network) => isUtxoBasedNetwork(network) || isEvmBasedNetwork(network) || isTronNetwork(network) || isSolanaNetwork(network)
export const isSameGetBlockNetwork = (network: Network) =>
isUtxoBasedNetwork(network) ||
isEvmBasedNetwork(network) ||
isTronNetwork(network) ||
isSolanaNetwork(network)

export enum MappedNetwork {
HORIZEN_EON = 'horizen-eon-mainnet',
Expand Down
8 changes: 4 additions & 4 deletions src/e2e/e2e.util.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
AddressBasedNotification,
AddressBasedNotificationDetail,
BaseEvmClass,
BlockBasedNotification,
BlockBasedNotificationDetail,
ContractBasedNotification,
ContractBasedNotificationDetail,
FullSdk,
Network,
} from '../service'
import { ResponseDto } from '../util'
Expand Down Expand Up @@ -68,7 +68,7 @@ export const e2eUtil = {
}
},
testAddressBasedSubscription: async (
tatum: BaseEvmClass,
tatum: FullSdk,
address: string,
func: (
addressBasedNotificationDetail: AddressBasedNotificationDetail,
Expand All @@ -93,7 +93,7 @@ export const e2eUtil = {
return data.id
},
testContractBasedSubscription: async (
tatum: BaseEvmClass,
tatum: FullSdk,
contractAddress: string,
func: (
contractBasedNotificationDetail: ContractBasedNotificationDetail,
Expand Down Expand Up @@ -122,7 +122,7 @@ export const e2eUtil = {
await tatum.destroy()
},
testBlockBasedSubscription: async (
tatum: BaseEvmClass,
tatum: FullSdk,
func: (
blockBasedNotificationDetail: BlockBasedNotificationDetail,
) => Promise<ResponseDto<BlockBasedNotification>>,
Expand Down
118 changes: 61 additions & 57 deletions src/e2e/extensions/e2e.extensions.ts
Original file line number Diff line number Diff line change
@@ -1,76 +1,80 @@
import { EVM_BASED_NETWORKS } from '../../dto'
import {
ITatumSdkContainer,
Network,
TatumConfig,
TatumSdkExtension,
TatumSdkWalletProvider,
TxId
} from "../../service";
import { EVM_BASED_NETWORKS } from "../../dto";
ITatumSdkContainer,
Network,
TatumConfig,
TatumSdkExtension,
TatumSdkWalletProvider,
TxId,
} from '../../service'

export class TestExtension extends TatumSdkExtension {
private readonly sdkConfig: TatumConfig
private readonly sdkConfig: TatumConfig

constructor(tatumSdkContainer: ITatumSdkContainer, private readonly mockTestExtension: any) {
super(tatumSdkContainer)
this.sdkConfig = this.tatumSdkContainer.getConfig()
}
constructor(tatumSdkContainer: ITatumSdkContainer, private readonly mockTestExtension: any) {
super(tatumSdkContainer)
this.sdkConfig = this.tatumSdkContainer.getConfig()
}

async sayHello(){
this.mockTestExtension.dummyMethod()
this.mockTestExtension.network(this.sdkConfig.network)
}
async sayHello() {
this.mockTestExtension.dummyMethod()
this.mockTestExtension.network(this.sdkConfig.network)
}

init(): Promise<void> {
this.mockTestExtension.init()
return Promise.resolve(undefined)
}
init(): Promise<void> {
this.mockTestExtension.init()
return Promise.resolve(undefined)
}

destroy(): Promise<void> {
this.mockTestExtension.destroy()
return Promise.resolve(undefined)
}
destroy(): Promise<void> {
this.mockTestExtension.destroy()
return Promise.resolve(undefined)
}

supportedNetworks: Network[] = EVM_BASED_NETWORKS
supportedNetworks: Network[] = EVM_BASED_NETWORKS
}

export class TestWalletProvider extends TatumSdkWalletProvider<string, string> {
private readonly sdkConfig: TatumConfig
private readonly sdkConfig: TatumConfig

constructor(tatumSdkContainer: ITatumSdkContainer, private readonly mockTestExtension?: any, someOtherConfig?: {someConfigValue: boolean}) {
super(tatumSdkContainer)
this.sdkConfig = this.tatumSdkContainer.getConfig()
console.log('someOtherConfig', someOtherConfig)
if(!mockTestExtension){
this.mockTestExtension = {
dummyMethod: jest.fn(),
init: jest.fn(),
destroy: jest.fn(),
network: jest.fn()
}
}
constructor(
tatumSdkContainer: ITatumSdkContainer,
private readonly mockTestExtension?: any,
someOtherConfig?: { someConfigValue: boolean },
) {
super(tatumSdkContainer)
this.sdkConfig = this.tatumSdkContainer.getConfig()
console.log('someOtherConfig', someOtherConfig)
if (!mockTestExtension) {
this.mockTestExtension = {
dummyMethod: jest.fn(),
init: jest.fn(),
destroy: jest.fn(),
network: jest.fn(),
}
}
}

async getWallet(){
this.mockTestExtension.network(this.sdkConfig.network)
this.mockTestExtension.dummyMethod()
return 'connected'
}
async getWallet() {
this.mockTestExtension.network(this.sdkConfig.network)
this.mockTestExtension.dummyMethod()
return 'connected'
}

init(): Promise<void> {
this.mockTestExtension.init()
return Promise.resolve(undefined)
}
init(): Promise<void> {
this.mockTestExtension.init()
return Promise.resolve(undefined)
}

destroy(): Promise<void> {
this.mockTestExtension.destroy()
return Promise.resolve(undefined)
}
destroy(): Promise<void> {
this.mockTestExtension.destroy()
return Promise.resolve(undefined)
}

signAndBroadcast(payload: string): Promise<TxId> {
this.mockTestExtension.dummyMethod()
return Promise.resolve(payload);
}
signAndBroadcast(payload: string): Promise<TxId> {
this.mockTestExtension.dummyMethod()
return Promise.resolve(payload)
}

supportedNetworks: Network[] = EVM_BASED_NETWORKS
supportedNetworks: Network[] = EVM_BASED_NETWORKS
}
20 changes: 6 additions & 14 deletions src/e2e/extensions/tatum.extensions.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Ethereum, Network, TatumSDK } from '../../service'
import { TestExtension, TestWalletProvider } from "./e2e.extensions";
import { TestExtension, TestWalletProvider } from './e2e.extensions'

const mockTestExtension = {
dummyMethod: jest.fn(),
init: jest.fn(),
destroy: jest.fn(),
network: jest.fn()
network: jest.fn(),
}

describe('Tatum Extension Ecosystem', () => {
Expand All @@ -16,9 +16,7 @@ describe('Tatum Extension Ecosystem', () => {
it('should work after being registered', async () => {
const tatum = await TatumSDK.init<Ethereum>({
network: Network.ETHEREUM_SEPOLIA,
configureExtensions:[
{type: TestExtension, config: mockTestExtension}
]
configureExtensions: [{ type: TestExtension, config: mockTestExtension }],
})

await tatum.extension(TestExtension).sayHello()
Expand All @@ -34,9 +32,7 @@ describe('Tatum Extension Ecosystem', () => {
try {
const tatum = await TatumSDK.init<Ethereum>({
network: Network.BITCOIN,
configureExtensions: [
TestExtension
]
configureExtensions: [TestExtension],
})

expect(true).toBe(false)
Expand All @@ -56,9 +52,7 @@ describe('Tatum Extension Ecosystem', () => {
it('should work after being registered', async () => {
const tatum = await TatumSDK.init<Ethereum>({
network: Network.ETHEREUM_SEPOLIA,
configureWalletProviders:[
{type: TestWalletProvider, config: mockTestExtension}
]
configureWalletProviders: [{ type: TestWalletProvider, config: mockTestExtension }],
})

const result = await tatum.walletProvider.use(TestWalletProvider).getWallet()
Expand All @@ -78,9 +72,7 @@ describe('Tatum Extension Ecosystem', () => {
it('should work after being registered without config if optional', async () => {
const tatum = await TatumSDK.init<Ethereum>({
network: Network.ETHEREUM_SEPOLIA,
configureWalletProviders:[
TestWalletProvider
]
configureWalletProviders: [TestWalletProvider],
})

const result = await tatum.walletProvider.use(TestWalletProvider).getWallet()
Expand Down
4 changes: 2 additions & 2 deletions src/e2e/rpc/evm/evm.e2e.utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { BigNumber } from 'bignumber.js'
import { Network } from '../../../dto'
import { BaseEvmClass, TatumSDK } from '../../../service'
import { BaseEvm, TatumSDK } from '../../../service'
import { RpcE2eUtils } from '../rpc.e2e.utils'

interface evmE2eI {
Expand All @@ -16,7 +16,7 @@ interface evmE2eI {
}

export const EvmE2eUtils = {
initTatum: async <T extends BaseEvmClass>(network: Network, apiKey?: string) =>
initTatum: async <T extends BaseEvm>(network: Network, apiKey?: string) =>
TatumSDK.init<T>(RpcE2eUtils.initConfig(network, apiKey)),
e2e: (evmE2eI: evmE2eI) => {
const { network, expected, data, skipEstimateGas, apiKey } = evmE2eI
Expand Down
12 changes: 6 additions & 6 deletions src/e2e/rpc/evm/tatum.rpc.ethereum.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import process from 'process'
import { BaseEvmClass, Network, RpcNodeType, TatumSDK } from '../../../service'
import { BaseEvm, Network, RpcNodeType, TatumSDK } from '../../../service'
import { EvmE2eUtils } from './evm.e2e.utils'

describe('Ethereum', () => {
Expand Down Expand Up @@ -28,7 +28,7 @@ describe('Ethereum', () => {
})

it('custom rpc provider', async () => {
const tatum = await TatumSDK.init<BaseEvmClass>({
const tatum = await TatumSDK.init<BaseEvm>({
network: Network.ETHEREUM,
rpc: {
nodes: [
Expand All @@ -50,11 +50,11 @@ describe('Ethereum', () => {
it('debug storage range at', async () => {
const tatum = await EvmE2eUtils.initTatum(Network.ETHEREUM, process.env.V4_API_KEY_MAINNET)
const { result } = await tatum.rpc.debugStorageRangeAt(
"0xc20f6b582e0c7923341cdb1299a94ea00c8a23e1ccabc532955a2a07b27121dc",
'0xc20f6b582e0c7923341cdb1299a94ea00c8a23e1ccabc532955a2a07b27121dc',
0,
"0x5799e216fb6825f21e6f20af22836303edc45df3",
"0x0000000000000000000000000000000000000000000000000000000000000000",
5
'0x5799e216fb6825f21e6f20af22836303edc45df3',
'0x0000000000000000000000000000000000000000000000000000000000000000',
5,
)
await tatum.destroy()
expect(result).toBeDefined()
Expand Down
25 changes: 16 additions & 9 deletions src/e2e/rpc/other/tatum.rpc.solana.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,9 @@ describe('Solana', () => {
describe('getInflationReward', () => {
it.skip('should return inflation reward', async () => {
const tatum = await getClient()
const { result } = await tatum.rpc.getInflationReward(['GUP3BG93X9EoDor3bDarTqv8n653u1Bkr2NbQqRqBZwF'])
const { result } = await tatum.rpc.getInflationReward([
'GUP3BG93X9EoDor3bDarTqv8n653u1Bkr2NbQqRqBZwF',
])
const item = result![0]
await tatum.destroy()
expect(item.epoch).toBeGreaterThan(0)
Expand Down Expand Up @@ -277,7 +279,9 @@ describe('Solana', () => {
it('should return account info', async () => {
const tatum = await getClient()
//binance validator
const { result } = await tatum.rpc.getMultipleAccounts(['DRpbCBMxVnDK7maPM5tGv6MvB3v1sRMC86PZ8okm21hy'])
const { result } = await tatum.rpc.getMultipleAccounts([
'DRpbCBMxVnDK7maPM5tGv6MvB3v1sRMC86PZ8okm21hy',
])
await tatum.destroy()
expect(result?.context.slot).toBeGreaterThan(0)
expect(result?.value[0]?.lamports).toBeGreaterThan(0)
Expand Down Expand Up @@ -358,13 +362,16 @@ describe('Solana', () => {
it.skip('should return account data', async () => {
const tatum = await getClient(true)

const { result } = await tatum.rpc.getProgramAccounts('FriELggez2Dy3phZeHHAdpcoEXkKQVkv6tx3zDtCVP8T', {
filters: [
{
dataSize: 165, // number of bytes
},
],
})
const { result } = await tatum.rpc.getProgramAccounts(
'FriELggez2Dy3phZeHHAdpcoEXkKQVkv6tx3zDtCVP8T',
{
filters: [
{
dataSize: 165, // number of bytes
},
],
},
)
await tatum.destroy()
expect(result).toBeTruthy()
})
Expand Down
4 changes: 2 additions & 2 deletions src/e2e/rpc/utxo/utxo.e2e.utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Network } from '../../../dto'
import { BaseUtxoClass, TatumSDK } from '../../../service'
import { BaseUtxo, TatumSDK } from '../../../service'
import { RpcE2eUtils } from '../rpc.e2e.utils'

export enum UtxoNetworkType {
Expand All @@ -16,7 +16,7 @@ interface TatumBtcUtils {

export const UtxoE2eUtils = {
initTatum: async (params: TatumBtcUtils) =>
TatumSDK.init<BaseUtxoClass>(RpcE2eUtils.initConfig(params.network, params.apiKey)),
TatumSDK.init<BaseUtxo>(RpcE2eUtils.initConfig(params.network, params.apiKey)),
e2e: (params: TatumBtcUtils) => {
const { type } = params
it('chain info', async () => {
Expand Down
4 changes: 2 additions & 2 deletions src/e2e/tatum.address.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Network } from '../dto'
import {
ApiVersion,
BaseTatumSdk,
Bitcoin,
Dogecoin,
Ethereum,
FullSdk,
HorizenEon,
Litecoin,
Solana,
Expand Down Expand Up @@ -202,7 +202,7 @@ describe.skip('Address', () => {
})

describe('getBalance CARDANO', () => {
let tatum: BaseTatumSdk
let tatum: FullSdk

beforeAll(async () => {
tatum = await TatumSDK.init({ network: Network.CARDANO_PREPROD })
Expand Down
Loading

0 comments on commit 696f280

Please sign in to comment.