forked from shapeshift/lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…hift#286) * Add coincap * add coincap adapter * remove tokenId check * readd tokenId check * remove scripts to generate marketcap data * apply linter fixes
- Loading branch information
Showing
20 changed files
with
642 additions
and
9,329 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { coincapUrl } from './index' | ||
import { fetchData, parseData, writeFiles } from './utils' | ||
|
||
const main = async () => { | ||
const data = await fetchData(coincapUrl) | ||
const output = parseData(data) | ||
await writeFiles(output) | ||
} | ||
|
||
main() |
1 change: 1 addition & 0 deletions
1
.../caip/src/adapters/coincap/generated/bip122:000000000019d6689c085ae165831e93/adapter.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"bip122:000000000019d6689c085ae165831e93/slip44:0":"bitcoin"} |
1 change: 1 addition & 0 deletions
1
packages/caip/src/adapters/coincap/generated/eip155:1/adapter.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import bip122 from './bip122:000000000019d6689c085ae165831e93/adapter.json' | ||
import eip155 from './eip155:1/adapter.json' | ||
|
||
export { bip122, eip155 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { ChainTypes, ContractTypes, NetworkTypes } from '@shapeshiftoss/types' | ||
|
||
import { toCAIP19 } from './../../caip19/caip19' | ||
import { CAIP19ToCoinCap, coincapToCAIP19 } from '.' | ||
|
||
describe('coincap adapter', () => { | ||
describe('coincapToCAIP19', () => { | ||
it('can get CAIP19 for bitcoin', () => { | ||
const chain = ChainTypes.Bitcoin | ||
const network = NetworkTypes.MAINNET | ||
const caip19 = toCAIP19({ chain, network }) | ||
expect(coincapToCAIP19('bitcoin')).toEqual(caip19) | ||
}) | ||
|
||
it('can get CAIP19 id for ethereum', () => { | ||
const chain = ChainTypes.Ethereum | ||
const network = NetworkTypes.MAINNET | ||
const caip19 = toCAIP19({ chain, network }) | ||
expect(coincapToCAIP19('ethereum')).toEqual(caip19) | ||
}) | ||
|
||
it('can get CAIP19 id for FOX', () => { | ||
const chain = ChainTypes.Ethereum | ||
const network = NetworkTypes.MAINNET | ||
const contractType = ContractTypes.ERC20 | ||
const tokenId = '0xc770eefad204b5180df6a14ee197d99d808ee52d' | ||
const caip19 = toCAIP19({ chain, network, contractType, tokenId }) | ||
expect(coincapToCAIP19('fox-token')).toEqual(caip19) | ||
}) | ||
}) | ||
|
||
describe('CAIP19ToCoinCap', () => { | ||
it('can get coincap id for bitcoin CAIP19', () => { | ||
const chain = ChainTypes.Bitcoin | ||
const network = NetworkTypes.MAINNET | ||
const caip19 = toCAIP19({ chain, network }) | ||
expect(CAIP19ToCoinCap(caip19)).toEqual('bitcoin') | ||
}) | ||
|
||
it('can get coincap id for ethereum CAIP19', () => { | ||
const chain = ChainTypes.Ethereum | ||
const network = NetworkTypes.MAINNET | ||
const caip19 = toCAIP19({ chain, network }) | ||
expect(CAIP19ToCoinCap(caip19)).toEqual('ethereum') | ||
}) | ||
|
||
it('can get coincap id for FOX', () => { | ||
const chain = ChainTypes.Ethereum | ||
const network = NetworkTypes.MAINNET | ||
const contractType = ContractTypes.ERC20 | ||
const tokenId = '0xc770eefad204b5180df6a14ee197d99d808ee52d' | ||
const caip19 = toCAIP19({ chain, network, contractType, tokenId }) | ||
expect(CAIP19ToCoinCap(caip19)).toEqual('fox-token') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import toLower from 'lodash/toLower' | ||
|
||
import * as adapters from './generated' | ||
|
||
export const coincapUrl = 'https://api.coincap.io/v2/assets?limit=2000' | ||
|
||
const generatedCAIP19ToCoinCapMap = Object.values(adapters).reduce((acc, cur) => ({ | ||
...acc, | ||
...cur | ||
})) as Record<string, string> | ||
|
||
const invert = <T extends Record<string, string>>(data: T) => | ||
Object.entries(data).reduce((acc, [k, v]) => ((acc[v] = k), acc), {} as Record<string, string>) | ||
|
||
const generatedCoinCapToCAIP19Map: Record<string, string> = invert(generatedCAIP19ToCoinCapMap) | ||
|
||
export const coincapToCAIP19 = (id: string): string => { | ||
const generated = generatedCoinCapToCAIP19Map[id] | ||
if (!generated) { | ||
throw new Error(`coincapToCAIP19: no caip19 found for id ${id}`) | ||
} | ||
return generated | ||
} | ||
|
||
export const CAIP19ToCoinCap = (caip19: string): string => { | ||
const generated = generatedCAIP19ToCoinCapMap[toLower(caip19)] | ||
if (!generated) { | ||
throw new Error(`CAIP19ToCoinCap: no id found for caip19 ${toLower(caip19)}`) | ||
} | ||
return generated | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import realFs from 'fs' | ||
|
||
import { makeBtcData, parseData, parseEthData, writeFiles } from './utils' | ||
|
||
const eth = { | ||
id: 'ethereum', | ||
rank: '2', | ||
symbol: 'ETH', | ||
name: 'Ethereum', | ||
supply: '118739782.1240000000000000', | ||
maxSupply: null, | ||
marketCapUsd: '461557096820.5397856216327206', | ||
volumeUsd24Hr: '13216473429.9114945699035335', | ||
priceUsd: '3887.1310740534754598', | ||
changePercent24Hr: '1.7301970732523704', | ||
vwap24Hr: '3796.0013297212388563', | ||
explorer: 'https://etherscan.io/' | ||
} | ||
|
||
const fox = { | ||
id: 'fox-token', | ||
rank: '396', | ||
symbol: 'FOX', | ||
name: 'FOX Token', | ||
supply: '117022448.6044180000000000', | ||
maxSupply: '1000001337.0000000000000000', | ||
marketCapUsd: '76108238.1995641297877127', | ||
volumeUsd24Hr: '1574012.7599955363585018', | ||
priceUsd: '0.6503729763580659', | ||
changePercent24Hr: '-3.1066427856364231', | ||
vwap24Hr: '0.6546275575306273', | ||
explorer: 'https://etherscan.io/token/0xc770eefad204b5180df6a14ee197d99d808ee52d' | ||
} | ||
|
||
const btc = { | ||
id: 'bitcoin', | ||
rank: '1', | ||
symbol: 'BTC', | ||
name: 'Bitcoin', | ||
supply: '18901193.0000000000000000', | ||
maxSupply: '21000000.0000000000000000', | ||
marketCapUsd: '908356345541.2269154394485668', | ||
volumeUsd24Hr: '19001957914.4173604708767279', | ||
priceUsd: '48058.1487920485715076', | ||
changePercent24Hr: '2.0370678507913180', | ||
vwap24Hr: '47473.8260811456834087', | ||
explorer: 'https://blockchain.info/' | ||
} | ||
|
||
jest.mock('fs', () => ({ | ||
promises: { | ||
writeFile: jest.fn(async () => undefined) | ||
} | ||
})) | ||
|
||
describe('parseEthData', () => { | ||
it('can parse eth data', async () => { | ||
const result = parseEthData([eth, fox]) | ||
const expected = { | ||
'eip155:1/slip44:60': 'ethereum', | ||
'eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d': 'fox-token' | ||
} | ||
expect(result).toEqual(expected) | ||
}) | ||
|
||
it('can parse btc data', async () => { | ||
const result = makeBtcData() | ||
const expected = { 'bip122:000000000019d6689c085ae165831e93/slip44:0': 'bitcoin' } | ||
expect(result).toEqual(expected) | ||
}) | ||
}) | ||
|
||
describe('parseData', () => { | ||
it('can parse all data', async () => { | ||
const result = parseData([eth, fox, btc]) | ||
const expected = { | ||
'bip122:000000000019d6689c085ae165831e93': { | ||
'bip122:000000000019d6689c085ae165831e93/slip44:0': 'bitcoin' | ||
}, | ||
'eip155:1': { | ||
'eip155:1/slip44:60': 'ethereum', | ||
'eip155:1/erc20:0xc770eefad204b5180df6a14ee197d99d808ee52d': 'fox-token' | ||
} | ||
} | ||
expect(result).toEqual(expected) | ||
}) | ||
}) | ||
|
||
describe('writeFiles', () => { | ||
it('can writeFiles', async () => { | ||
const data = { | ||
foo: { | ||
caip19abc: 'bitcorn', | ||
caip19def: 'efferium' | ||
}, | ||
bar: { | ||
caip19ghi: 'fox', | ||
caip19jkl: 'shib' | ||
} | ||
} | ||
const fooCaips = JSON.stringify(data.foo) | ||
const barCaips = JSON.stringify(data.bar) | ||
console.info = jest.fn() | ||
await writeFiles(data) | ||
expect(realFs.promises.writeFile).toBeCalledWith( | ||
'./src/adapters/coincap/generated/foo/adapter.json', | ||
fooCaips | ||
) | ||
expect(realFs.promises.writeFile).toBeCalledWith( | ||
'./src/adapters/coincap/generated/bar/adapter.json', | ||
barCaips | ||
) | ||
expect(console.info).toBeCalledWith('Generated CoinCap CAIP19 adapter data.') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { ChainTypes, ContractTypes, NetworkTypes } from '@shapeshiftoss/types' | ||
import axios from 'axios' | ||
import fs from 'fs' | ||
|
||
import { toCAIP2 } from '../../caip2/caip2' | ||
import { toCAIP19 } from './../../caip19/caip19' | ||
|
||
export type CoinCapCoin = { | ||
id: string | ||
rank: string | ||
symbol: string | ||
name: string | ||
supply: string | ||
maxSupply: string | null | ||
marketCapUsd: string | ||
volumeUsd24Hr: string | ||
priceUsd: string | ||
changePercent24Hr: string | ||
vwap24Hr: string | ||
explorer: string | null | ||
} | ||
|
||
export const writeFiles = async (data: Record<string, Record<string, string>>) => { | ||
const path = './src/adapters/coincap/generated/' | ||
const file = '/adapter.json' | ||
const writeFile = async ([k, v]: [string, unknown]) => | ||
await fs.promises.writeFile(`${path}${k}${file}`, JSON.stringify(v)) | ||
await Promise.all(Object.entries(data).map(writeFile)) | ||
console.info('Generated CoinCap CAIP19 adapter data.') | ||
} | ||
|
||
export const fetchData = async (URL: string) => | ||
(await axios.get<{ data: CoinCapCoin[] }>(URL)).data.data | ||
|
||
export const parseEthData = (data: CoinCapCoin[]) => { | ||
const ethCoins = data.filter( | ||
({ id, explorer }) => | ||
(explorer && explorer.startsWith('https://etherscan.io/token/0x')) || id === 'ethereum' | ||
) | ||
|
||
const chain = ChainTypes.Ethereum | ||
const network = NetworkTypes.MAINNET | ||
const contractType = ContractTypes.ERC20 | ||
|
||
const result = ethCoins.reduce((acc, { id, explorer }) => { | ||
let tokenId | ||
if (id !== 'ethereum' && explorer) { | ||
tokenId = explorer | ||
.replace('https://etherscan.io/token/', '') | ||
.split('#')[0] | ||
.split('?')[0] | ||
} | ||
const caip19 = toCAIP19({ chain, network, ...(tokenId ? { contractType, tokenId } : {}) }) | ||
acc[caip19] = id | ||
return acc | ||
}, {} as Record<string, string>) | ||
|
||
return result | ||
} | ||
|
||
export const makeBtcData = () => { | ||
const chain = ChainTypes.Bitcoin | ||
const network = NetworkTypes.MAINNET | ||
const caip19 = toCAIP19({ chain, network }) | ||
return { [caip19]: 'bitcoin' } | ||
} | ||
|
||
export const parseData = (d: CoinCapCoin[]) => { | ||
const ethMainnet = toCAIP2({ chain: ChainTypes.Ethereum, network: NetworkTypes.MAINNET }) | ||
const btcMainnet = toCAIP2({ chain: ChainTypes.Bitcoin, network: NetworkTypes.MAINNET }) | ||
return { [ethMainnet]: parseEthData(d), [btcMainnet]: makeBtcData() } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './coingecko' | ||
export * from './coincap' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export type CoinCapMarketCap = { | ||
id: string | ||
rank: string | ||
symbol: string | ||
name: string | ||
supply: string | ||
maxSupply: string | null | ||
marketCapUsd: string | ||
volumeUsd24Hr: string | ||
priceUsd: string | ||
changePercent24Hr: string | ||
vwap24Hr: string | ||
explorer: string | ||
} |
Oops, something went wrong.