Skip to content

Commit

Permalink
TatumUtils better error messages and types (#984)
Browse files Browse the repository at this point in the history
  • Loading branch information
Smrecz authored Oct 15, 2023
1 parent 6790295 commit 4e1e04b
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 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.7] - 2023.10.15
### Changed
- `TatumUtils` chainId <-> `Network` mappings always return usable value or throw error for ease of use.

## [4.1.6] - 2023.10.15
### Added
- `TatumUtils` added with access to chainId <-> `Network` mapping
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.6",
"version": "4.1.7",
"description": "Tatum JS SDK",
"author": "Tatum",
"repository": "https://github.com/tatumio/tatum-js",
Expand Down
2 changes: 1 addition & 1 deletion src/e2e/tatum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('Network to chainId mapping check', () => {
const chainId = TatumUtils.getChainId(network)

expect(chainId).toBeGreaterThan(0)
expect(TatumUtils.getNetwork(chainId as number)).toBe(network)
expect(TatumUtils.getNetwork(chainId)).toBe(network)
})
}
})
12 changes: 8 additions & 4 deletions src/util/tatum.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ import { Network } from '../dto'
import { Constant } from './constant'

export const TatumUtils = {
getChainId: (network: Network): number | undefined => {
getChainId: (network: Network): number => {
if (network in Constant.NETWORK.ChainId) {
return Constant.NETWORK.ChainId[network as keyof typeof Constant.NETWORK.ChainId]
}
return undefined
throw new Error(`Tatum Network to ChainId for network ${network} does not exist`)
},
getNetwork: (chainId: number): keyof typeof Constant.NETWORK.ChainId | undefined => {
getNetwork: (chainId: number): Network => {
if (Object.keys(chainIdToNetworkCache).length === 0) {
buildChainIdToNetworkCache()
}
return chainIdToNetworkCache[chainId] || undefined
const network = chainIdToNetworkCache[chainId]
if (!network) {
throw new Error(`ChainId to Tatum Network for chainId ${chainId} does not exist`)
}
return network
},
}

Expand Down

0 comments on commit 4e1e04b

Please sign in to comment.