Skip to content
This repository was archived by the owner on Apr 11, 2023. It is now read-only.

Commit

Permalink
chore: refactor common types out to separate package (#84)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xdef1cafe authored Sep 30, 2021
1 parent 3f23938 commit 0f6455a
Show file tree
Hide file tree
Showing 43 changed files with 534 additions and 402 deletions.
3 changes: 2 additions & 1 deletion packages/asset-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"type-check": "tsc --project ./tsconfig.json --noEmit"
},
"dependencies": {
"lodash": "^4.17.21"
"lodash": "^4.17.21",
"@shapeshiftoss/types": "^1.0.0"
},
"devDependencies": {
"@types/lodash": "^4.14.172"
Expand Down
2 changes: 1 addition & 1 deletion packages/asset-service/src/generateAssetData/baseAssets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseAsset, ContractTypes, NetworkTypes, ChainTypes } from '../types'
import { BaseAsset, ContractTypes, NetworkTypes, ChainTypes } from '@shapeshiftoss/types'

export const baseAssets: Array<BaseAsset> = [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios'
import { ContractTypes, TokenAsset } from '../../types'
import { ContractTypes, TokenAsset } from '@shapeshiftoss/types'
import lodash from 'lodash'
import { tokensToOverride } from './overrides'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TokenAsset, ContractTypes } from '../../types'
import { TokenAsset, ContractTypes } from '@shapeshiftoss/types'

export const tokensToOverride: Array<TokenAsset> = [
// example overriding FOX token with custom values instead of goingecko
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import fs from 'fs'
import { baseAssets } from './baseAssets'
import { getTokens } from './ethTokens'
import { BaseAsset, NetworkTypes } from '../types'
import { ChainTypes } from '..'
import { BaseAsset, ChainTypes, NetworkTypes } from '@shapeshiftoss/types'

const generateAssetData = async () => {
const generatedAssetData = await Promise.all(
Expand Down
1 change: 0 additions & 1 deletion packages/asset-service/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export * from './types'
export * from './service/AssetService'
2 changes: 1 addition & 1 deletion packages/asset-service/src/service/AssetService.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from 'axios'
import { Asset, ChainTypes, NetworkTypes } from '../types'
import { Asset, ChainTypes, NetworkTypes } from '@shapeshiftoss/types'
import { AssetService, flattenAssetData, indexAssetData } from './AssetService'
import { mockBaseAssets, mockAssets, mockIndexedAssetData } from './AssetServiceTestData'

Expand Down
2 changes: 1 addition & 1 deletion packages/asset-service/src/service/AssetService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseAsset, Asset, NetworkTypes, ChainTypes } from '../types'
import { BaseAsset, Asset, NetworkTypes, ChainTypes } from '@shapeshiftoss/types'
import axios from 'axios'
import localAssetData from './generatedAssetData.json'

Expand Down
2 changes: 1 addition & 1 deletion packages/asset-service/src/service/AssetServiceTestData.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IndexedAssetData } from '..'
import { Asset, BaseAsset, ChainTypes, ContractTypes, NetworkTypes } from '../types'
import { Asset, BaseAsset, ChainTypes, ContractTypes, NetworkTypes } from '@shapeshiftoss/types'

export const mockBaseAssets: BaseAsset[] = [
{
Expand Down
44 changes: 0 additions & 44 deletions packages/asset-service/src/types.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/chain-adapters/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"dependencies": {
"@shapeshiftoss/hdwallet-core": "^1.15.5-alpha.0",
"@shapeshiftoss/hdwallet-native": "^1.15.5-alpha.1",
"@shapeshiftoss/types": "^1.0.0",
"axios": "^0.21.1",
"bignumber.js": "^9.0.1",
"ethers": "^5.4.4",
Expand Down
6 changes: 3 additions & 3 deletions packages/chain-adapters/src/ChainAdapterCLI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ChainAdapterManager } from './ChainAdapterManager'
import { ChainIdentifier } from '.'
import { ChainTypes } from '@shapeshiftoss/types'
import { NativeAdapterArgs, NativeHDWallet } from '@shapeshiftoss/hdwallet-native'
import { HDWallet } from '@shapeshiftoss/hdwallet-core'
import dotenv from 'dotenv'
Expand All @@ -20,14 +20,14 @@ const getWallet = async (): Promise<HDWallet> => {
}

const unchainedUrls = {
[ChainIdentifier.Ethereum]: 'http://localhost:31300/api/v1'
[ChainTypes.Ethereum]: 'http://localhost:31300/api/v1'
}

const main = async () => {
try {
const chainAdapterManager = new ChainAdapterManager(unchainedUrls)
const wallet = await getWallet()
const ethChainAdapter = chainAdapterManager.byChain(ChainIdentifier.Ethereum)
const ethChainAdapter = chainAdapterManager.byChain(ChainTypes.Ethereum)
const address = await ethChainAdapter.getAddress({ wallet, path: defaultEthPath })

const balanceInfo = await ethChainAdapter.getBalance(address)
Expand Down
10 changes: 5 additions & 5 deletions packages/chain-adapters/src/ChainAdapterManager.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/ban-ts-comment */
import { ChainIdentifier } from './api'
import { ChainTypes } from '@shapeshiftoss/types'
import { ChainAdapterManager } from './ChainAdapterManager'
import { EthereumChainAdapter } from './ethereum'

Expand Down Expand Up @@ -40,7 +40,7 @@ describe('ChainAdapterManager', () => {
it('should add a network', () => {
expect(
// @ts-ignore
getCAM().addChain(ChainIdentifier.Ethereum, () => new EthereumChainAdapter())
getCAM().addChain(ChainTypes.Ethereum, () => new EthereumChainAdapter())
).toBeUndefined()
})
})
Expand All @@ -54,8 +54,8 @@ describe('ChainAdapterManager', () => {

it('should get an adapter factory', () => {
const cam = getCAM()
const adapter = cam.byChain(ChainIdentifier.Ethereum)
const adapter2 = cam.byChain(ChainIdentifier.Ethereum)
const adapter = cam.byChain(ChainTypes.Ethereum)
const adapter2 = cam.byChain(ChainTypes.Ethereum)
// @ts-ignore
expect(adapter).toBeInstanceOf(EthereumChainAdapter)
expect(adapter2).toBe(adapter)
Expand All @@ -64,7 +64,7 @@ describe('ChainAdapterManager', () => {

describe('getSupportedChains', () => {
it('should return array of keys', () => {
expect(getCAM().getSupportedChains()).toStrictEqual([ChainIdentifier.Ethereum])
expect(getCAM().getSupportedChains()).toStrictEqual([ChainTypes.Ethereum])
})
})

Expand Down
33 changes: 20 additions & 13 deletions packages/chain-adapters/src/ChainAdapterManager.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import { ChainAdapter, ChainIdentifier } from './api'
import { ChainAdapter } from './api'
import { ChainTypes } from '@shapeshiftoss/types'
import { EthereumChainAdapter } from './ethereum'
import { UnchainedProvider } from './providers'

export type UnchainedUrls = Record<ChainIdentifier.Ethereum, string>
export type UnchainedUrls = Record<ChainTypes.Ethereum, string>

const chainAdapterMap = {
[ChainIdentifier.Ethereum]: EthereumChainAdapter
[ChainTypes.Ethereum]: EthereumChainAdapter
} as const

export class ChainAdapterManager {
private supported: Map<ChainIdentifier, () => ChainAdapter> = new Map()
private supported: Map<ChainTypes, () => ChainAdapter> = new Map()
private instances: Map<string, ChainAdapter> = new Map()

constructor(unchainedUrls: UnchainedUrls) {
if (!unchainedUrls) {
throw new Error('Blockchain urls required')
}
;(Object.keys(unchainedUrls) as Array<ChainIdentifier>).forEach((key: ChainIdentifier) => {
const Adapter = chainAdapterMap[key]
if (!Adapter) throw new Error(`No chain adapter for ${key}`)
this.addChain(key, () => new Adapter({ provider: new UnchainedProvider(unchainedUrls[key]) }))
})
// TODO(0xdef1cafe): loosen this from ChainTypes.Ethereum to ChainTypes once we implement more than ethereum
;(Object.keys(unchainedUrls) as Array<ChainTypes.Ethereum>).forEach(
(key: ChainTypes.Ethereum) => {
const Adapter = chainAdapterMap[key]
if (!Adapter) throw new Error(`No chain adapter for ${key}`)
this.addChain(
key,
() => new Adapter({ provider: new UnchainedProvider(unchainedUrls[key]) })
)
}
)
}

/**
Expand All @@ -30,17 +37,17 @@ export class ChainAdapterManager {
* import { ChainAdapterManager, UtxoChainAdapter } from 'chain-adapters'
* const manager = new ChainAdapterManager(client)
* manager.addChain('bitcoin', () => new UtxoChainAdapter('BTG', client))
* @param {ChainIdentifier} network - Coin/network symbol from Asset query
* @param {ChainTypes} network - Coin/network symbol from Asset query
* @param {Function} factory - A function that returns a ChainAdapter instance
*/
addChain(chain: ChainIdentifier, factory: () => ChainAdapter): void {
addChain(chain: ChainTypes, factory: () => ChainAdapter): void {
if (typeof chain !== 'string' || typeof factory !== 'function') {
throw new Error('Parameter validation error')
}
this.supported.set(chain, factory)
}

getSupportedChains(): Array<ChainIdentifier> {
getSupportedChains(): Array<ChainTypes> {
return Array.from(this.supported.keys())
}

Expand All @@ -49,7 +56,7 @@ export class ChainAdapterManager {
}

/*** Get a ChainAdapter instance for a network */
byChain(chain: ChainIdentifier): ChainAdapter {
byChain(chain: ChainTypes): ChainAdapter {
let adapter = this.instances.get(chain)
if (!adapter) {
const factory = this.supported.get(chain)
Expand Down
Loading

0 comments on commit 0f6455a

Please sign in to comment.