Skip to content

Commit

Permalink
feat: Added support for the BTC<>BTCB testnet bridge (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbp-rib authored Mar 23, 2021
1 parent a151e19 commit 018c5bd
Show file tree
Hide file tree
Showing 38 changed files with 1,224 additions and 1,122 deletions.
49 changes: 25 additions & 24 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@
"keywords": [
"Crypto",
"Bitcoin",
"Javascript",
"Ethereum",
"Binance Smart Chain",
"BSC",
"JavaScript",
"TypeScript",
"NodeJS",
"Swingby",
"Skybridge",
"ECDSA",
"Trustless"
],
Expand All @@ -41,39 +46,35 @@
"format": "prettier --write '**/*.{js,jsx,ts,tsx,css,json,md,mdx,html}'",
"run-example": "ts-node --compiler-options '{ \"module\": \"commonjs\" }'"
},
"husky": {
"hooks": {
"pre-commit": "yarn lint"
}
},
"devDependencies": {
"@babel/core": "^7.12.3",
"@size-limit/preset-small-lib": "^4.7.0",
"@swingby-protocol/eslint-config": "^1.2.0",
"@babel/core": "^7.13.10",
"@size-limit/preset-small-lib": "^4.10.1",
"@swingby-protocol/eslint-config": "^2.3.0",
"@swingby-protocol/prettier-config": "^1.0.2",
"@swingby-protocol/sdk": "link:.",
"@types/big.js": "^6.0.0",
"@types/big.js": "^6.0.2",
"@types/debug": "^4.1.5",
"babel-loader": "^8.1.0",
"husky": "^4.3.0",
"babel-jest": "^26.6.3",
"babel-loader": "^8.2.2",
"jest": "^26.6.3",
"prettier": "^2.1.2",
"semantic-release": "^17.2.2",
"ts-node": "^9.0.0",
"prettier": "^2.2.1",
"semantic-release": "^17.4.2",
"ts-node": "^9.1.1",
"tsdx": "^0.14.1",
"tslib": "^2.0.3",
"type-fest": "^0.20.2",
"typescript": "^4.0.5"
"tslib": "^2.1.0",
"type-fest": "^0.21.3",
"typescript": "^4.2.3"
},
"dependencies": {
"bech32": "^1.1.4",
"big.js": "^6.0.2",
"bitcoin-address-validation": "^1.0.2",
"debug": "^4.2.0",
"@typescript-eslint/eslint-plugin": "4.18.0",
"bech32": "^2.0.0",
"big.js": "^6.0.3",
"bitcoin-address-validation": "^2.0.1",
"debug": "^4.3.1",
"hex-to-binary": "^1.0.1",
"isomorphic-unfetch": "^3.1.0",
"isomorphic-webcrypto": "^2.3.6",
"web3-utils": "^1.3.1"
"isomorphic-webcrypto": "^2.3.8",
"web3-utils": "^1.3.4"
},
"resolutions": {
"**/@typescript-eslint/eslint-plugin": "4.6.1"
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export type { SkybridgeContext } from './modules/context';
export type { SkybridgeCoin } from './modules/coins';
export type { SkybridgeChain } from './modules/chains';

export { getCoinsFor, getSwapableWith } from './modules/coins';
export { getCoinsFor, getSwapableWith, getDisplayNameForCoin } from './modules/coins';
export { createSwap, getSwapDetails } from './modules/swap';
export { buildContext, getNetworkDetails, getBridgeFor } from './modules/context';
export { isAddressValid } from './modules/validate-address';
Expand Down
2 changes: 1 addition & 1 deletion src/modules/bridges/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const SKYBRIDGE_BRIDGES = ['btc_erc', 'btc_bep'] as const;
export const SKYBRIDGE_BRIDGES = ['btc_erc', 'btc_bep20'] as const;
export type SkybridgeBridge = typeof SKYBRIDGE_BRIDGES[number];

export const isSkybridgeBridge = (value: any): value is SkybridgeBridge =>
Expand Down
7 changes: 4 additions & 3 deletions src/modules/chains/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { SkybridgeCoin } from '../coins';

const CHAINS = ['bitcoin', 'ethereum', 'binance'] as const;
const CHAINS = ['bitcoin', 'ethereum', 'binance-smart'] as const;
export type SkybridgeChain = typeof CHAINS[number];

export const isSkybridgeChain = (value: any): value is SkybridgeChain => CHAINS.includes(value);

export const getChainFor = ({ coin }: { coin: SkybridgeCoin | 'sbBTC' }): SkybridgeChain => {
switch (coin) {
case 'BTCB':
return 'binance';
case 'sbBTC.BEP20':
case 'BTCB.BEP20':
return 'binance-smart';
case 'BTC':
return 'bitcoin';
case 'sbBTC':
Expand Down
39 changes: 19 additions & 20 deletions src/modules/coins/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@ describe('getCoinsFor()', () => {
direction?: SkybridgeDirection;
expected: SkybridgeCoin[];
}>([
{ expected: ['BTC', 'WBTC', 'sbBTC', 'BTCB'] },
{ mode: 'test', expected: ['BTC', 'WBTC', 'sbBTC', 'BTCB'] },
{ expected: ['BTC', 'WBTC', 'sbBTC', 'BTCB.BEP20', 'sbBTC.BEP20'] },
{ mode: 'test', expected: ['BTC', 'WBTC', 'sbBTC', 'BTCB.BEP20', 'sbBTC.BEP20'] },
{ mode: 'production', expected: ['BTC', 'WBTC', 'sbBTC'] },
{ mode: 'test', bridge: 'btc_bep', expected: ['BTC', 'BTCB'] },
{ mode: 'production', bridge: 'btc_bep', expected: [] },
{ mode: 'test', bridge: 'btc_bep20', expected: ['BTC', 'BTCB.BEP20', 'sbBTC.BEP20'] },
{ mode: 'production', bridge: 'btc_bep20', expected: [] },
{ mode: 'test', bridge: 'btc_erc', expected: ['BTC', 'WBTC', 'sbBTC'] },
{ mode: 'production', bridge: 'btc_erc', expected: ['BTC', 'WBTC', 'sbBTC'] },
{ resource: 'swap', expected: ['BTC', 'WBTC', 'sbBTC', 'BTCB'] },
{ resource: 'pool', expected: ['BTC', 'WBTC', 'sbBTC'] },
{ resource: 'withdrawal', expected: ['sbBTC', 'BTC', 'WBTC'] },
{ resource: 'pool', direction: 'in', expected: ['BTC', 'WBTC'] },
{ resource: 'pool', direction: 'out', expected: ['sbBTC'] },
{ resource: 'withdrawal', direction: 'in', expected: ['sbBTC'] },
{ resource: 'withdrawal', direction: 'out', expected: ['BTC', 'WBTC'] },
{ resource: 'swap', expected: ['BTC', 'WBTC', 'sbBTC', 'BTCB.BEP20', 'sbBTC.BEP20'] },
{ resource: 'pool', expected: ['BTC', 'WBTC', 'sbBTC', 'BTCB.BEP20', 'sbBTC.BEP20'] },
{ resource: 'withdrawal', expected: ['sbBTC', 'BTC', 'WBTC', 'sbBTC.BEP20', 'BTCB.BEP20'] },
{ resource: 'pool', direction: 'in', expected: ['BTC', 'WBTC', 'BTCB.BEP20'] },
{ resource: 'pool', direction: 'out', expected: ['sbBTC', 'sbBTC.BEP20'] },
{ resource: 'withdrawal', direction: 'in', expected: ['sbBTC', 'sbBTC.BEP20'] },
{ resource: 'withdrawal', direction: 'out', expected: ['BTC', 'WBTC', 'BTCB.BEP20'] },
])('works for %O', async ({ mode, bridge, resource, direction, expected }) => {
expect.assertions(1);

Expand All @@ -52,14 +52,14 @@ describe('getBridgesForCoin()', () => {
coin: SkybridgeCoin;
expected: any;
}>([
{ mode: 'test', coin: 'BTC', expected: ['btc_erc', 'btc_bep'] },
{ mode: 'test', coin: 'BTC', expected: ['btc_erc', 'btc_bep20'] },
{ mode: 'production', coin: 'BTC', expected: ['btc_erc'] },
{ mode: 'test', coin: 'BTCB', expected: ['btc_bep'] },
{ mode: 'production', coin: 'BTCB', expected: [] },
{ mode: 'test', coin: 'BTCB.BEP20', expected: ['btc_bep20'] },
{ mode: 'production', coin: 'BTCB.BEP20', expected: [] },
{ mode: 'test', coin: 'WBTC', expected: ['btc_erc'] },
{ mode: 'production', coin: 'WBTC', expected: ['btc_erc'] },
{ mode: 'production', resource: 'pool', coin: 'sbBTC', expected: ['btc_erc'] },
{ mode: 'production', resource: 'pool', coin: 'BTCB', expected: [] },
{ mode: 'production', resource: 'pool', coin: 'BTCB.BEP20', expected: [] },
])('works for %O', async ({ mode, coin, direction, resource, expected }) => {
expect.assertions(1);

Expand All @@ -80,17 +80,16 @@ describe('getSwapableWith()', () => {
bridge?: SkybridgeBridge;
coin: SkybridgeCoin;
resource: SkybridgeResource;
expected: any;
expected: SkybridgeCoin[];
}>([
{ mode: 'test', resource: 'swap', coin: 'BTC', expected: ['WBTC', 'BTCB'] },
{ mode: 'test', resource: 'swap', coin: 'BTC', expected: ['WBTC', 'BTCB.BEP20'] },
{ mode: 'production', resource: 'swap', coin: 'BTC', expected: ['WBTC'] },
{ mode: 'test', resource: 'swap', coin: 'BTCB', expected: ['BTC'] },
{ mode: 'production', resource: 'swap', coin: 'BTCB', expected: [] },
{ mode: 'test', resource: 'swap', coin: 'BTCB.BEP20', expected: ['BTC'] },
{ mode: 'production', resource: 'swap', coin: 'BTCB.BEP20', expected: [] },
{ mode: 'test', resource: 'swap', coin: 'WBTC', expected: ['BTC'] },
{ mode: 'production', resource: 'swap', coin: 'WBTC', expected: ['BTC'] },
{ mode: 'production', resource: 'swap', coin: 'WBTC', expected: ['BTC'] },
{ mode: 'test', resource: 'swap', coin: 'WBTC', expected: ['BTC'] },
{ mode: 'test', resource: 'swap', coin: 'BTC', expected: ['WBTC', 'BTCB'] },
{ mode: 'test', resource: 'swap', coin: 'BTC', bridge: 'btc_erc', expected: ['WBTC'] },
{ mode: 'test', resource: 'pool', coin: 'WBTC', expected: ['sbBTC'] },
{ mode: 'test', resource: 'withdrawal', coin: 'WBTC', expected: [] },
Expand Down
123 changes: 108 additions & 15 deletions src/modules/coins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,73 @@ import type { SkybridgeBridge } from '../bridges';
import type { SkybridgeResource } from '../resources';
import type { SkybridgeDirection } from '../directions';

export type SkybridgeApiCoin = 'BTC' | 'WBTC' | 'sbBTC' | 'BTCB';

const COINS = {
swap: {
btc_erc: {
test: { in: ['BTC', 'WBTC', 'sbBTC'], out: ['BTC', 'WBTC'] },
production: { in: ['BTC', 'WBTC', 'sbBTC'], out: ['BTC', 'WBTC'] },
test: {
in: ['BTC', 'WBTC', 'sbBTC'],
out: ['BTC', 'WBTC'],
},
production: {
in: ['BTC', 'WBTC', 'sbBTC'],
out: ['BTC', 'WBTC'],
},
},
btc_bep: {
test: { in: ['BTC', 'BTCB'], out: ['BTC', 'BTCB'] },
production: { in: [], out: [] },
btc_bep20: {
test: {
in: ['BTC', 'BTCB.BEP20', 'sbBTC.BEP20'],
out: ['BTC', 'BTCB.BEP20'],
},
production: {
in: [],
out: [],
},
},
},
pool: {
btc_erc: {
test: { in: ['BTC', 'WBTC'], out: ['sbBTC'] },
production: { in: ['BTC', 'WBTC'], out: ['sbBTC'] },
test: {
in: ['BTC', 'WBTC'],
out: ['sbBTC'],
},
production: {
in: ['BTC', 'WBTC'],
out: ['sbBTC'],
},
},
btc_bep: {
test: { in: [], out: [] },
production: { in: [], out: [] },
btc_bep20: {
test: {
in: ['BTC', 'BTCB.BEP20'],
out: ['sbBTC.BEP20'],
},
production: {
in: [],
out: [],
},
},
},
withdrawal: {
btc_erc: {
test: { in: ['sbBTC'], out: ['BTC', 'WBTC'] },
production: { in: ['sbBTC'], out: ['BTC', 'WBTC'] },
test: {
in: ['sbBTC'],
out: ['BTC', 'WBTC'],
},
production: {
in: ['sbBTC'],
out: ['BTC', 'WBTC'],
},
},
btc_bep: {
test: { in: [], out: [] },
production: { in: [], out: [] },
btc_bep20: {
test: {
in: ['sbBTC.BEP20'],
out: ['BTC', 'BTCB.BEP20'],
},
production: {
in: [],
out: [],
},
},
},
} as const;
Expand Down Expand Up @@ -157,3 +195,58 @@ export const getSwapableWith = <

return Array.from(set);
};

export const getDisplayNameForCoin = ({ coin }: { coin: SkybridgeCoin }): string => {
switch (coin) {
case 'BTC':
return 'BTC';
case 'BTCB.BEP20':
return 'BTCB (BEP20)';
case 'sbBTC.BEP20':
return 'sbBTC (BEP20)';
case 'WBTC':
return 'WBTC (ERC20)';
case 'sbBTC':
return 'sbBTC (ERC20)';
}
};

export const toApiCoin = ({ coin }: { coin: SkybridgeCoin }): SkybridgeApiCoin => {
switch (coin) {
case 'BTC':
return 'BTC';
case 'BTCB.BEP20':
return 'BTCB';
case 'WBTC':
return 'WBTC';
case 'sbBTC.BEP20':
case 'sbBTC':
return 'sbBTC';
}
};

export const fromApiCoin = ({
coin,
bridge,
}: {
coin: SkybridgeApiCoin;
bridge: SkybridgeBridge;
}): SkybridgeCoin => {
if (bridge === 'btc_erc') {
if ((coin as any) === 'BTCE') return 'WBTC';
return coin as SkybridgeCoin;
}

if (bridge === 'btc_bep20') {
switch (coin) {
case 'BTC':
return 'BTC';
case 'BTCB':
return 'BTCB.BEP20';
case 'sbBTC':
return 'sbBTC.BEP20';
}
}

throw new Error(`Could not find SDK coin ID for "${coin}" in bridge "${bridge}"`);
};
8 changes: 4 additions & 4 deletions src/modules/context/__mocks__/buildContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export const buildContext = async <M extends SkybridgeMode>({
affiliateApi: 'https://affiliate.swingby.network',
servers: {
swapNode: {
btc_erc: 'https://tbtc-goerli-node-1.swingby.network',
btc_bep: 'https://tbtc-bc-node-1.swingby.network',
btc_erc: 'https://btc-wbtc-mainnet.quantexe.com',
btc_bep20: 'https://tbtc-bsc-1.swingby.network',
},
indexer: {
btc_erc: 'https://eth-indexer-testnet-sw1.swingby.network',
btc_bep: 'https://btc-indexer-testnet-sw1.swingby.network',
btc_erc: 'https://eth-indexer.zoo.farm',
btc_bep20: 'https://tbtc-bsc-1.swingby.network/bb-bsc',
},
},
};
Expand Down
Loading

0 comments on commit 018c5bd

Please sign in to comment.