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.
feat: unchained client (shapeshift#603)
* chore: adds issue automation * feat(unchained-client): add unchained-client package * Dockerized generator for different environments * Remove local openapi-generator-cli * Pull in recent cleanup changes in unchained * Update common-api package * Fix docker context * Use published docker image * Fix ethers mock to play nicely with hdwallet * review changes * formatting Co-authored-by: 0xean <[email protected]>
- Loading branch information
1 parent
546926b
commit 0e0df76
Showing
92 changed files
with
10,827 additions
and
36 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 @@ | ||
process.env.NETWORK = 'mainnet' |
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,69 @@ | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
const ethers = { | ||
...jest.requireActual('ethers').ethers, | ||
providers: { | ||
JsonRpcProvider: jest.fn() | ||
}, | ||
Contract: jest.fn().mockImplementation((address) => ({ | ||
decimals: () => { | ||
switch (address as string) { | ||
case '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48': | ||
return 6 | ||
case '0xc770EEfAd204B5180dF6a14Ee197D99d808ee52d': | ||
return 18 | ||
case '0x470e8de2eBaef52014A47Cb5E6aF86884947F08c': | ||
return 18 | ||
case '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2': | ||
return 18 | ||
default: | ||
throw new Error(`no decimals mock for address: ${address}`) | ||
} | ||
}, | ||
name: () => { | ||
switch (address as string) { | ||
case '0xc770EEfAd204B5180dF6a14Ee197D99d808ee52d': | ||
return 'FOX' | ||
case '0x470e8de2eBaef52014A47Cb5E6aF86884947F08c': | ||
return 'Uniswap V2' | ||
case '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2': | ||
return 'Wrapped Ether' | ||
default: | ||
throw new Error(`no decimals mock for address: ${address}`) | ||
} | ||
}, | ||
symbol: () => { | ||
switch (address as string) { | ||
case '0xc770EEfAd204B5180dF6a14Ee197D99d808ee52d': | ||
return 'FOX' | ||
case '0x470e8de2eBaef52014A47Cb5E6aF86884947F08c': | ||
return 'UNI-V2' | ||
case '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2': | ||
return 'WETH' | ||
default: | ||
throw new Error(`no decimals mock for address: ${address}`) | ||
} | ||
} | ||
})) | ||
} | ||
|
||
// extra mocks for hdwallet which uses explicit imports instead of the standard `import { ethers } from 'ethers'` | ||
const BigNumber = jest.requireActual('ethers').BigNumber | ||
const Bytes = jest.requireActual('ethers').Bytes | ||
const BytesLike = jest.requireActual('ethers').BytesLike | ||
const Signature = jest.requireActual('ethers').Signature | ||
const Signer = jest.requireActual('ethers').Signer | ||
const UnsignedTransaction = jest.requireActual('ethers').UnsignedTransaction | ||
const providers = jest.requireActual('ethers').providers | ||
const utils = jest.requireActual('ethers').utils | ||
|
||
export { | ||
ethers, | ||
BigNumber, | ||
Bytes, | ||
BytesLike, | ||
Signature, | ||
Signer, | ||
UnsignedTransaction, | ||
providers, | ||
utils | ||
} |
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,54 @@ | ||
# Unchained API Client | ||
|
||
Provides a typescript axios client to interact with all supported unchained API's | ||
|
||
## Installing | ||
|
||
Using npm: | ||
``` | ||
npm install @shapeshiftoss/unchained-client | ||
``` | ||
|
||
Using yarn: | ||
``` | ||
yarn add @shapeshiftoss/unchained-client | ||
``` | ||
|
||
## Resources | ||
|
||
- [CAIP-2](https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md) naming conventions used for `chainId` | ||
- [CAIP-19](https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-19.md) naming conventions used for `assetId` | ||
|
||
## Usage | ||
|
||
``` | ||
import * as unchained from '@shapeshiftoss/unchained-client' | ||
const address = 'cosmos1t5u0jfg3ljsjrh2m9e47d4ny2hea7eehxrzdgd' | ||
// configuration for the api client | ||
const config = new unchained.cosmos.Configuration({ basePath: 'https://dev-api.cosmos.shapeshift.com' }) | ||
// create new instance of the api client | ||
const apiClient = new unchained.cosmos.V1Api(config) | ||
// create new instance of the ws client | ||
const wsClient = new unchained.ws.Client<unchained.cosmos.Tx>('wss://dev-api.cosmos.shapeshift.com') | ||
// create new instance of the transaction parser | ||
// chainId is in the format of [Caip2](https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-2.md) | ||
const parser = new unchained.cosmos.TransactionParser({ chainId: 'cosmos:cosmoshub-4' }) | ||
// example api client request | ||
const { data } = await apiClient.getBalance({ pubkey: address }) | ||
// example websocket subscription for new transaction and transaction parsing | ||
await this.providers.ws.subscribeTxs( | ||
'test', | ||
{ topic: 'txs', addresses: [address] }, | ||
async (msg) => { | ||
const tx = await parser.parse(msg.data, msg.address) | ||
}, | ||
(err) => console.warn(err) | ||
) | ||
``` |
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,6 @@ | ||
FROM timbru31/java-node:11-jdk | ||
|
||
RUN npm install @openapitools/openapi-generator-cli -g | ||
RUN openapi-generator-cli version-manager set 5.4.0 | ||
|
||
ENTRYPOINT [ "openapi-generator-cli" ] |
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,39 @@ | ||
{ | ||
"$schema": "usr/local/node_modules/@openapitools/openapi-generator-cli/config.schema.json", | ||
"spaces": 2, | ||
"generator-cli": { | ||
"version": "5.4.0", | ||
"generators": { | ||
"ethereum": { | ||
"inputSpec": "https://dev-api.ethereum.shapeshift.com/swagger.json", | ||
"generatorName": "typescript-axios", | ||
"output": "#{cwd}/../../src/generated/ethereum", | ||
"reservedWordsMappings": { "in": "in" }, | ||
"additionalProperties": { | ||
"supportsES6": "true", | ||
"useSingleRequestParameter": true | ||
} | ||
}, | ||
"bitcoin": { | ||
"inputSpec": "https://dev-api.bitcoin.shapeshift.com/swagger.json", | ||
"generatorName": "typescript-axios", | ||
"output": "#{cwd}/../../src/generated/bitcoin", | ||
"reservedWordsMappings": { "in": "in" }, | ||
"additionalProperties": { | ||
"supportsES6": "true", | ||
"useSingleRequestParameter": true | ||
} | ||
}, | ||
"cosmos": { | ||
"inputSpec": "https://dev-api.cosmos.shapeshift.com/swagger", | ||
"generatorName": "typescript-axios", | ||
"output": "#{cwd}/../../src/generated/cosmos", | ||
"reservedWordsMappings": { "in": "in" }, | ||
"additionalProperties": { | ||
"supportsES6": "true", | ||
"useSingleRequestParameter": true | ||
} | ||
} | ||
} | ||
} | ||
} |
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,23 @@ | ||
#!/bin/bash | ||
|
||
IMAGE=shapeshiftdao/openapi-generator-cli-v5.4.0 | ||
USER=$(id -u):$(id -g) | ||
ROOTDIR=$(git rev-parse --show-toplevel)/packages/unchained-client | ||
GENDIR=$(pwd)/generator | ||
|
||
while getopts ':e:h' flag; do | ||
case "${flag}" in | ||
e) env=${OPTARG};; | ||
h) echo -e "Usage: $(basename $0) -e (local|dev|public)" && exit 1;; | ||
:) echo -e "Option requires an argument.\nUsage: $(basename $0) -e (local|dev|public)" && exit 1;; | ||
?) echo -e "Invalid command option.\nUsage: $(basename $0) -e (local|dev|public)" && exit 1;; | ||
esac | ||
done | ||
|
||
if [[ $env == "" ]]; then | ||
echo -e "Usage: $(basename $0) -e (local|dev|public)" && exit 1 | ||
fi | ||
|
||
docker run --platform=linux/amd64 --rm --user $USER -e JAVA_OPTS='-Dlog.level=error' -v "$ROOTDIR:$ROOTDIR" -w $GENDIR/$env $IMAGE generate | ||
|
||
exit 0 |
39 changes: 39 additions & 0 deletions
39
packages/unchained-client/generator/local/openapitools.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,39 @@ | ||
{ | ||
"$schema": "usr/local/node_modules/@openapitools/openapi-generator-cli/config.schema.json", | ||
"spaces": 2, | ||
"generator-cli": { | ||
"version": "5.4.0", | ||
"generators": { | ||
"ethereum": { | ||
"inputSpec": "http://api.ethereum.localhost/swagger.json", | ||
"generatorName": "typescript-axios", | ||
"output": "#{cwd}/../../src/generated/ethereum", | ||
"reservedWordsMappings": { "in": "in" }, | ||
"additionalProperties": { | ||
"supportsES6": "true", | ||
"useSingleRequestParameter": true | ||
} | ||
}, | ||
"bitcoin": { | ||
"inputSpec": "http://api.bitcoin.localhost/swagger.json", | ||
"generatorName": "typescript-axios", | ||
"output": "#{cwd}/../../src/generated/bitcoin", | ||
"reservedWordsMappings": { "in": "in" }, | ||
"additionalProperties": { | ||
"supportsES6": "true", | ||
"useSingleRequestParameter": true | ||
} | ||
}, | ||
"cosmos": { | ||
"inputSpec": "http://api.cosmos.localhost/swagger", | ||
"generatorName": "typescript-axios", | ||
"output": "#{cwd}/../../src/generated/cosmos", | ||
"reservedWordsMappings": { "in": "in" }, | ||
"additionalProperties": { | ||
"supportsES6": "true", | ||
"useSingleRequestParameter": true | ||
} | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
packages/unchained-client/generator/public/openapitools.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,39 @@ | ||
{ | ||
"$schema": "usr/local/node_modules/@openapitools/openapi-generator-cli/config.schema.json", | ||
"spaces": 2, | ||
"generator-cli": { | ||
"version": "5.4.0", | ||
"generators": { | ||
"ethereum": { | ||
"inputSpec": "https://api.ethereum.shapeshift.com/swagger.json", | ||
"generatorName": "typescript-axios", | ||
"output": "#{cwd}/../../src/generated/ethereum", | ||
"reservedWordsMappings": { "in": "in" }, | ||
"additionalProperties": { | ||
"supportsES6": "true", | ||
"useSingleRequestParameter": true | ||
} | ||
}, | ||
"bitcoin": { | ||
"inputSpec": "https://api.bitcoin.shapeshift.com/swagger.json", | ||
"generatorName": "typescript-axios", | ||
"output": "#{cwd}/../../src/generated/bitcoin", | ||
"reservedWordsMappings": { "in": "in" }, | ||
"additionalProperties": { | ||
"supportsES6": "true", | ||
"useSingleRequestParameter": true | ||
} | ||
}, | ||
"cosmos": { | ||
"inputSpec": "https://api.cosmos.shapeshift.com/swagger", | ||
"generatorName": "typescript-axios", | ||
"output": "#{cwd}/../../src/generated/cosmos", | ||
"reservedWordsMappings": { "in": "in" }, | ||
"additionalProperties": { | ||
"supportsES6": "true", | ||
"useSingleRequestParameter": true | ||
} | ||
} | ||
} | ||
} | ||
} |
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,37 @@ | ||
{ | ||
"name": "@shapeshiftoss/unchained-client", | ||
"version": "6.11.0", | ||
"license": "MIT", | ||
"main": "dist/index.js", | ||
"source": "src/index.ts", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist" | ||
], | ||
"scripts": { | ||
"build": "yarn clean && yarn generate && tsc -p tsconfig.build.json", | ||
"build:public": "yarn clean && yarn generate:public && tsc -p tsconfig.build.json", | ||
"build:local": "yarn clean && yarn generate:local && tsc -p tsconfig.build.json", | ||
"dev": "tsc --watch", | ||
"clean": "rm -rf dist node_modules src/generated tsconfig.build.tsbuildinfo", | ||
"generate": "./generator/generate.sh -e dev", | ||
"generate:public": "./generator/generate.sh -e public", | ||
"generate:local": "./generator/generate.sh -e local", | ||
"type-check": "tsc --project ./tsconfig.build.json --noEmit" | ||
}, | ||
"dependencies": { | ||
"@shapeshiftoss/blockbook": "^6.11.0", | ||
"@shapeshiftoss/caip": "^2.2.3", | ||
"@shapeshiftoss/logger": "^1.1.2", | ||
"@shapeshiftoss/types": "^3.1.2", | ||
"@yfi/sdk": "^1.0.30", | ||
"bignumber.js": "^9.0.1", | ||
"ethers": "^5.5.3", | ||
"isomorphic-ws": "^4.0.1", | ||
"ws": "^8.3.0" | ||
}, | ||
"devDependencies": { | ||
"@shapeshiftoss/common-api": "^6.11.0", | ||
"@types/ws": "^8.2.1" | ||
} | ||
} |
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,3 @@ | ||
export * from '../generated/bitcoin' | ||
export * from './parser' | ||
export * from './types' |
Oops, something went wrong.