Skip to content

Commit

Permalink
ALL-4895 - Add Cronos subscriptions (#1079)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hathoriel authored Mar 15, 2024
1 parent d106fe6 commit a8b0ce9
Show file tree
Hide file tree
Showing 16 changed files with 163 additions and 24 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.16] - 2024.2.15

### Added

- Added support for Cronos and Cronos testnet subscriptions.

## [4.2.15] - 2024.3.11

### 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.15",
"version": "4.2.16",
"description": "Tatum JS SDK",
"author": "Tatum",
"repository": "https://github.com/tatumio/tatum-js",
Expand Down
6 changes: 5 additions & 1 deletion src/connector/tatum.connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ export class TatumConnector {
if (isDownload) {
return await res.blob()
}
return await res.json()
const response = await res.json()
if (response?.error) {
return await this.retry(url, request, res, retry)
}
return response
}

// Retry only in case of 5xx error
Expand Down
1 change: 1 addition & 0 deletions src/dto/AddressEventNotificationChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export enum AddressEventNotificationChain {
EON = 'EON',
CHZ = 'CHZ',
FLR = 'FLR',
CRO = 'CRO',
}
2 changes: 2 additions & 0 deletions src/dto/Network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -822,11 +822,13 @@ export const NETWORK_METADATA: Record<Network, NetworkMetadata> = {
currency: Currency.CRO,
testnet: false,
chainId: 25,
defaultMainnet: true,
},
[Network.CRONOS_TESTNET]: {
currency: Currency.CRO,
testnet: true,
chainId: 338,
defaultTestnet: true,
},
[Network.KUCOIN]: {
currency: Currency.KCS,
Expand Down
1 change: 1 addition & 0 deletions src/dto/rpc/EvmBasedRpcInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ export interface ValidatorQuery extends StateQuery {

export interface EvmBeaconV1Interface {
getGenesis(): Promise<EvmBeaconResponse<any>>
getNodeVersion(): Promise<EvmBeaconResponse<any>>
getBlockHeaders(query?: { slot?: string; parentRoot?: string }): Promise<EvmBeaconResponse<any>>
getBlockHeader(query: BlockQuery): Promise<EvmBeaconResponse<any>>
getBlockRoot(query: BlockQuery): Promise<EvmBeaconResponse<any>>
Expand Down
61 changes: 61 additions & 0 deletions src/e2e/e2e.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ export const AddressEventNetworks = [
Network.XRP_TESTNET,
Network.TEZOS,
Network.FLARE,
Network.FLARE_COSTON,
Network.FLARE_COSTON_2,
Network.FLARE_SONGBIRD,
Network.CRONOS,
Network.CRONOS_TESTNET,
]

export const IncomingNativeTxNetworks = [
Expand Down Expand Up @@ -62,6 +67,12 @@ export const IncomingNativeTxNetworks = [
Network.XRP,
Network.XRP_TESTNET,
Network.TEZOS,
Network.FLARE,
Network.FLARE_COSTON,
Network.FLARE_COSTON_2,
Network.FLARE_SONGBIRD,
Network.CRONOS,
Network.CRONOS_TESTNET,
]

export const OutgoingNativeTxNetworks = [
Expand Down Expand Up @@ -89,6 +100,12 @@ export const OutgoingNativeTxNetworks = [
Network.XRP,
Network.XRP_TESTNET,
Network.TEZOS,
Network.FLARE,
Network.FLARE_COSTON,
Network.FLARE_COSTON_2,
Network.FLARE_SONGBIRD,
Network.CRONOS,
Network.CRONOS_TESTNET,
]

export const OutgoingFailedNetworks = [
Expand All @@ -104,6 +121,12 @@ export const OutgoingFailedNetworks = [
Network.KLAYTN,
Network.KLAYTN_BAOBAB,
Network.TEZOS,
Network.FLARE,
Network.FLARE_COSTON,
Network.FLARE_COSTON_2,
Network.FLARE_SONGBIRD,
Network.CRONOS,
Network.CRONOS_TESTNET,
]

export const PaidFeeNetworks = [
Expand All @@ -123,6 +146,12 @@ export const PaidFeeNetworks = [
Network.XRP,
Network.XRP_TESTNET,
Network.TEZOS,
Network.FLARE,
Network.FLARE_COSTON,
Network.FLARE_COSTON_2,
Network.FLARE_SONGBIRD,
Network.CRONOS,
Network.CRONOS_TESTNET,
]

export const FungibleTxNetworks = [
Expand All @@ -142,6 +171,12 @@ export const FungibleTxNetworks = [
Network.SOLANA,
Network.SOLANA_DEVNET,
Network.TEZOS,
Network.FLARE,
Network.FLARE_COSTON,
Network.FLARE_COSTON_2,
Network.FLARE_SONGBIRD,
Network.CRONOS,
Network.CRONOS_TESTNET,
]

export const NftNetworks = [
Expand All @@ -161,6 +196,12 @@ export const NftNetworks = [
Network.SOLANA,
Network.SOLANA_DEVNET,
Network.TEZOS,
Network.FLARE,
Network.FLARE_COSTON,
Network.FLARE_COSTON_2,
Network.FLARE_SONGBIRD,
Network.CRONOS,
Network.CRONOS_TESTNET,
]

export const MultitokenNetworks = [
Expand All @@ -175,6 +216,12 @@ export const MultitokenNetworks = [
Network.BINANCE_SMART_CHAIN_TESTNET,
Network.KLAYTN,
Network.KLAYTN_BAOBAB,
Network.FLARE,
Network.FLARE_COSTON,
Network.FLARE_COSTON_2,
Network.FLARE_SONGBIRD,
Network.CRONOS,
Network.CRONOS_TESTNET,
]

export const FailedTxPerBlockNetworks = [
Expand All @@ -194,6 +241,12 @@ export const FailedTxPerBlockNetworks = [
Network.SOLANA,
Network.SOLANA_DEVNET,
Network.TEZOS,
Network.FLARE,
Network.FLARE_COSTON,
Network.FLARE_COSTON_2,
Network.FLARE_SONGBIRD,
Network.CRONOS,
Network.CRONOS_TESTNET,
]

export const ContractAddressLogEventNetworks = [
Expand All @@ -208,6 +261,10 @@ export const ContractAddressLogEventNetworks = [
Network.BINANCE_SMART_CHAIN_TESTNET,
Network.KLAYTN,
Network.KLAYTN_BAOBAB,
Network.FLARE,
Network.FLARE_COSTON,
Network.FLARE_COSTON_2,
Network.FLARE_SONGBIRD,
]

export const InternalTxNetworks = [
Expand All @@ -217,4 +274,8 @@ export const InternalTxNetworks = [
Network.CELO,
Network.CELO_ALFAJORES,
Network.TEZOS,
Network.FLARE,
Network.FLARE_COSTON,
Network.FLARE_COSTON_2,
Network.FLARE_SONGBIRD,
]
22 changes: 21 additions & 1 deletion src/e2e/e2e.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ContractBasedNotificationDetail,
FullSdk,
Network,
NotificationSubscription,
} from '../service'
import { ResponseDto } from '../util'
import { NetworkUtils } from '../util/network.utils'
Expand All @@ -32,11 +33,13 @@ export const e2eUtil = {
case Network.FLARE_COSTON:
case Network.FLARE_COSTON_2:
case Network.FLARE_SONGBIRD:
case Network.CRONOS:
case Network.CRONOS_TESTNET:
return '0xdb4C3b4350EE869F2D0a2F43ce0292865E2Aa149'
case Network.CELO_ALFAJORES:
return '0xdf083B077F1FD890fC71feCaBbd3F68F94cD21Bf'
case Network.POLYGON_MUMBAI:
return '0xd608d48c6ddB41254d73ec0619FE31B254Fb5ab8'
return '0xcf3c930601111c216fc0232d32cf5c2a86f107da'
case Network.KLAYTN_BAOBAB:
return '0xdc7Dfb8Aa86D41b7e39441711Fe1669f2d843C06'
case Network.BINANCE_SMART_CHAIN_TESTNET:
Expand Down Expand Up @@ -91,6 +94,7 @@ export const e2eUtil = {
addressBasedNotificationDetail: AddressBasedNotificationDetail,
) => Promise<ResponseDto<AddressBasedNotification>>,
) => {
await e2eUtil.flushSubscriptions(tatum)
const url = 'https://webhook.site/'
const { data, error } = await func({
url,
Expand All @@ -116,6 +120,7 @@ export const e2eUtil = {
contractBasedNotificationDetail: ContractBasedNotificationDetail,
) => Promise<ResponseDto<ContractBasedNotification>>,
) => {
await e2eUtil.flushSubscriptions(tatum)
const url = 'https://webhook.site/'
const event = '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
const { data, error } = await func({
Expand Down Expand Up @@ -144,6 +149,7 @@ export const e2eUtil = {
blockBasedNotificationDetail: BlockBasedNotificationDetail,
) => Promise<ResponseDto<BlockBasedNotification>>,
) => {
await e2eUtil.flushSubscriptions(tatum)
const url = 'https://webhook.site/'

const { data, error } = await func({
Expand All @@ -157,4 +163,18 @@ export const e2eUtil = {
},
},
isVerbose: process.env.E2E_VERBOSE === 'true',
flushSubscriptions: async (tatum: FullSdk) => {
try {
const notifications = await tatum.notification.getAll()

if (notifications?.data?.length > 0) {
for (const notification of notifications.data as NotificationSubscription[]) {
await tatum.notification.unsubscribe(notification.id)
}
}
} catch (e) {
console.error('Error flushing subscriptions')
console.error(e)
}
},
}
7 changes: 7 additions & 0 deletions src/e2e/rpc/evm/eth/tatum.rpc.beacon.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ describe('Beacon', () => {
const networks = [Network.ETHEREUM_HOLESKY, Network.ETHEREUM_SEPOLIA, Network.ETHEREUM]

describe.each(networks)('%s', (network) => {
it('should get node version', async () => {
const tatum = await EvmE2eUtils.initTatum<Ethereum>(network, process.env.V4_API_KEY_TESTNET)
const { data } = await tatum.rpc.beacon.v1.getNodeVersion()
await tatum.destroy()
expect(data).toBeDefined()
})

it('should get genesis', async () => {
const tatum = await EvmE2eUtils.initTatum<Ethereum>(network, process.env.V4_API_KEY_TESTNET)
const { data } = await tatum.rpc.beacon.v1.getGenesis()
Expand Down
19 changes: 14 additions & 5 deletions src/e2e/rpc/other/tatum.rpc.xrp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ const getXrpRpc = async (testnet?: boolean) =>
await TatumSDK.init<Xrp>(e2eUtil.initConfig(testnet ? Network.XRP_TESTNET : Network.XRP))

describe('RPCs', () => {
afterEach(async () => {
// wait for 200ms to avoid rate limit
await new Promise((resolve) => setTimeout(resolve, 100))
})

describe('XRP', () => {
describe('testnet', () => {
it('ping', async () => {
Expand All @@ -18,6 +13,20 @@ describe('RPCs', () => {
await tatum.destroy()
expect(result.status).toBe('success')
})

it('ledger_closed', async () => {
const tatum = await getXrpRpc(true)
const { result } = await tatum.rpc.ledgerClosed()
await tatum.destroy()
expect(result.ledger_index).toBeGreaterThan(0)
})

it('fee', async () => {
const tatum = await getXrpRpc(true)
const { result } = await tatum.rpc.fee()
await tatum.destroy()
expect(result.ledger_current_index).toBeGreaterThan(0)
})
})
})
describe('XRP', () => {
Expand Down
12 changes: 0 additions & 12 deletions src/e2e/tatum.notification.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,7 @@ import {
} from './e2e.constant'
import { e2eUtil } from './e2e.util'

// TODO pipeline dont work with API keys

describe('notification', () => {
beforeAll(async () => {
const tatum = await TatumSDK.init<Ethereum>(e2eUtil.initConfig(Network.ETHEREUM))
const notifications = await tatum.notification.getAll()

if (notifications?.data?.length > 0) {
for (const notification of notifications.data as NotificationSubscription[]) {
await tatum.notification.unsubscribe(notification.id)
}
}
})
describe('createSubscription', () => {
describe('IP auth', () => {
describe('Address Event', () => {
Expand Down
12 changes: 10 additions & 2 deletions src/service/rpc/evm/AbstractBeaconV1EvmRpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ import { Constant, Utils } from '../../../util'
export abstract class AbstractBeaconV1EvmRpc implements EvmBeaconV1Interface {
protected abstract get<T>(get: GetI): Promise<T>

private sendGet<T>(path: string, params: QueryParams): Promise<T> {
const fullPath = Utils.addQueryParams(`${Constant.BEACON_PREFIX}/${path}`, Utils.camelToSnakeCase, params)
private sendGet<T>(path: string, params: QueryParams, prefix?: string): Promise<T> {
const fullPath = Utils.addQueryParams(
`${prefix ?? Constant.BEACON_PREFIX}/${path}`,
Utils.camelToSnakeCase,
params,
)
return this.get({ path: fullPath })
}

Expand Down Expand Up @@ -80,4 +84,8 @@ export abstract class AbstractBeaconV1EvmRpc implements EvmBeaconV1Interface {
getStateValidators({ stateId, ...rest }: ValidatorsQuery): Promise<EvmBeaconResponse<any>> {
return this.sendGet(`states/${stateId}/validators`, rest)
}

getNodeVersion(): Promise<EvmBeaconResponse<any>> {
return this.sendGet('node/version', {}, Constant.BEACON_BASE_PREFIX)
}
}
2 changes: 1 addition & 1 deletion src/service/rpc/generic/LoadBalancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export class LoadBalancer implements AbstractRpcInterface {

if (config.rpc?.oneTimeLoadBalancing) {
Utils.log({ id: this.id, message: 'oneTimeLoadBalancing enabled' })
setTimeout(() => this.checkStatuses(), Constant.OPEN_RPC.LB_INTERVAL)
await this.checkStatuses()
} else {
this.interval = setInterval(() => this.checkStatuses(), Constant.OPEN_RPC.LB_INTERVAL)
}
Expand Down
2 changes: 1 addition & 1 deletion src/service/tatum/tatum.evm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class ArbitrumNova extends BaseEvm {}
export class ArbitrumOne extends BaseEvm {}
export class Aurora extends BaseEvm {}
export class AvalancheC extends BaseEvm {}
export class Cronos extends BaseEvm {}
export class Cronos extends NotificationEvm {}
export class EthereumClassic extends BaseEvm {}
export class Fantom extends BaseEvm {}
export class Gnosis extends BaseEvm {}
Expand Down
1 change: 1 addition & 0 deletions src/util/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,5 @@ export const Constant = {
},
EOS_PREFIX: 'v1/chain/',
BEACON_PREFIX: '/eth/v1/beacon',
BEACON_BASE_PREFIX: '/eth/v1',
}
Loading

0 comments on commit a8b0ce9

Please sign in to comment.