Skip to content

Commit

Permalink
feat: Added getSwapableTo()
Browse files Browse the repository at this point in the history
  • Loading branch information
sbp-rib committed Apr 10, 2021
1 parent ff88e8d commit 7960745
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 14 deletions.
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ export type { SkybridgeContext } from './modules/context';
export type { SkybridgeCoin } from './modules/coins';
export type { SkybridgeChain } from './modules/chains';

export { getCoinsFor, getSwapableWith, getDisplayNameForCoin } from './modules/coins';
export {
getCoinsFor,
getSwapableFrom,
getSwapableTo,
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
59 changes: 49 additions & 10 deletions src/modules/coins/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { buildContext } from '../context';
import type { SkybridgeDirection } from '../directions';
import type { SkybridgeMode } from '../modes';

import { getBridgesForCoin, getCoinsFor, getSwapableWith, SkybridgeCoin } from './';
import { getBridgesForCoin, getCoinsFor, getSwapableFrom, getSwapableTo, SkybridgeCoin } from './';

jest.mock('../context/buildContext');

Expand All @@ -18,9 +18,9 @@ describe('getCoinsFor()', () => {
}>([
{ 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: 'production', expected: ['BTC', 'WBTC', 'sbBTC', 'BTCB.BEP20', 'sbBTC.BEP20'] },
{ mode: 'test', bridge: 'btc_bep20', expected: ['BTC', 'BTCB.BEP20', 'sbBTC.BEP20'] },
{ mode: 'production', bridge: 'btc_bep20', expected: [] },
{ mode: 'production', bridge: 'btc_bep20', expected: ['BTC', 'BTCB.BEP20', 'sbBTC.BEP20'] },
{ 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.BEP20', 'sbBTC.BEP20'] },
Expand Down Expand Up @@ -53,13 +53,13 @@ describe('getBridgesForCoin()', () => {
expected: any;
}>([
{ mode: 'test', coin: 'BTC', expected: ['btc_erc', 'btc_bep20'] },
{ mode: 'production', coin: 'BTC', expected: ['btc_erc'] },
{ mode: 'production', coin: 'BTC', expected: ['btc_erc', 'btc_bep20'] },
{ mode: 'test', coin: 'BTCB.BEP20', expected: ['btc_bep20'] },
{ mode: 'production', coin: 'BTCB.BEP20', expected: [] },
{ mode: 'production', coin: 'BTCB.BEP20', expected: ['btc_bep20'] },
{ 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.BEP20', expected: [] },
{ mode: 'production', resource: 'pool', coin: 'BTCB.BEP20', expected: ['btc_bep20'] },
])('works for %O', async ({ mode, coin, direction, resource, expected }) => {
expect.assertions(1);

Expand All @@ -74,7 +74,7 @@ describe('getBridgesForCoin()', () => {
});
});

describe('getSwapableWith()', () => {
describe('getSwapableFrom()', () => {
it.each<{
mode: SkybridgeMode;
bridge?: SkybridgeBridge;
Expand All @@ -83,9 +83,9 @@ describe('getSwapableWith()', () => {
expected: SkybridgeCoin[];
}>([
{ mode: 'test', resource: 'swap', coin: 'BTC', expected: ['WBTC', 'BTCB.BEP20'] },
{ mode: 'production', resource: 'swap', coin: 'BTC', expected: ['WBTC'] },
{ mode: 'production', resource: 'swap', coin: 'BTC', expected: ['WBTC', 'BTCB.BEP20'] },
{ mode: 'test', resource: 'swap', coin: 'BTCB.BEP20', expected: ['BTC'] },
{ mode: 'production', resource: 'swap', coin: 'BTCB.BEP20', expected: [] },
{ mode: 'production', resource: 'swap', coin: 'BTCB.BEP20', expected: ['BTC'] },
{ mode: 'test', resource: 'swap', coin: 'WBTC', expected: ['BTC'] },
{ mode: 'production', resource: 'swap', coin: 'WBTC', expected: ['BTC'] },
{ mode: 'production', resource: 'swap', coin: 'WBTC', expected: ['BTC'] },
Expand All @@ -98,6 +98,45 @@ describe('getSwapableWith()', () => {
expect.assertions(1);

const context = await buildContext({ mode });
expect(getSwapableWith({ context, bridge, coin, resource })).toEqual(expected);
expect(getSwapableFrom({ context, bridge, coin, resource })).toEqual(expected);
});
});

describe('getSwapableTo()', () => {
it.each<{
mode: SkybridgeMode;
bridge?: SkybridgeBridge;
coin: SkybridgeCoin;
resource: SkybridgeResource;
expected: SkybridgeCoin[];
}>([
{
mode: 'test',
resource: 'swap',
coin: 'BTC',
expected: ['WBTC', 'sbBTC', 'BTCB.BEP20', 'sbBTC.BEP20'],
},
{
mode: 'production',
resource: 'swap',
coin: 'BTC',
expected: ['WBTC', 'sbBTC', 'BTCB.BEP20', 'sbBTC.BEP20'],
},
{ mode: 'test', resource: 'swap', coin: 'BTCB.BEP20', expected: ['BTC', 'sbBTC.BEP20'] },
{ mode: 'production', resource: 'swap', coin: 'BTCB.BEP20', expected: ['BTC', 'sbBTC.BEP20'] },
{ mode: 'test', resource: 'swap', coin: 'WBTC', expected: ['BTC', 'sbBTC'] },
{ mode: 'production', resource: 'swap', coin: 'WBTC', expected: ['BTC', 'sbBTC'] },
{ mode: 'test', resource: 'swap', coin: 'WBTC', expected: ['BTC', 'sbBTC'] },
{ mode: 'test', resource: 'swap', coin: 'BTC', bridge: 'btc_erc', expected: ['WBTC', 'sbBTC'] },
{ mode: 'test', resource: 'pool', coin: 'WBTC', expected: [] },
{ mode: 'test', resource: 'withdrawal', coin: 'WBTC', expected: ['sbBTC'] },
{ mode: 'test', resource: 'withdrawal', coin: 'BTCB.BEP20', expected: ['sbBTC.BEP20'] },
{ mode: 'test', resource: 'withdrawal', coin: 'BTC', expected: ['sbBTC', 'sbBTC.BEP20'] },
{ mode: 'test', resource: 'withdrawal', coin: 'sbBTC', expected: [] },
])('works for %O', async ({ mode, bridge, coin, resource, expected }) => {
expect.assertions(1);

const context = await buildContext({ mode });
expect(getSwapableTo({ context, bridge, coin, resource })).toEqual(expected);
});
});
39 changes: 38 additions & 1 deletion src/modules/coins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export const getBridgesForCoin = <
return Array.from(result);
};

export const getSwapableWith = <
export const getSwapableFrom = <
A extends SkybridgeResource,
M extends SkybridgeMode,
B extends SkybridgeBridge
Expand Down Expand Up @@ -196,6 +196,43 @@ export const getSwapableWith = <
return Array.from(set);
};

export const getSwapableTo = <
A extends SkybridgeResource,
M extends SkybridgeMode,
B extends SkybridgeBridge
>({
context: { mode },
coin,
bridge,
resource,
}: {
context: { mode: M };
coin: SkybridgeCoin;
bridge?: B;
resource: A;
}): SkybridgeCoin<A, M, 'in', B>[] => {
const result: SkybridgeCoin<A, M, 'in', B>[] = [];

typedKeys(COINS).forEach((resourceIt) => {
typedKeys(COINS[resourceIt]).forEach((bridgeIt) => {
typedKeys(COINS[resourceIt][bridgeIt]).forEach((modeIt) => {
if (resource && resource !== resourceIt) return;
if (bridge && bridge !== bridgeIt) return;
if (mode && mode !== modeIt) return;

if (((COINS[resourceIt][bridgeIt][modeIt].out as unknown) as string[]).includes(coin)) {
result.push(...COINS[resourceIt][bridgeIt][modeIt].in);
}
});
});
});

const set = new Set(result);
set.delete(coin as any);

return Array.from(set);
};

export const getDisplayNameForCoin = ({ coin }: { coin: SkybridgeCoin }): string => {
switch (coin) {
case 'BTC':
Expand Down
7 changes: 6 additions & 1 deletion src/modules/context/getBridgeFor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ it.each<{
{ mode: 'test', currencyDeposit: 'sbBTC', currencyReceiving: 'BTC', expected: 'btc_erc' },
{ mode: 'test', currencyDeposit: 'BTC', currencyReceiving: 'BTCB.BEP20', expected: 'btc_bep20' },
{ mode: 'test', currencyDeposit: 'BTCB.BEP20', currencyReceiving: 'BTC', expected: 'btc_bep20' },
{
mode: 'production',
currencyDeposit: 'BTC',
currencyReceiving: 'BTCB.BEP20',
expected: 'btc_bep20',
},
])('works for %O', async ({ mode, currencyReceiving, currencyDeposit, expected }) => {
expect.assertions(1);
const context = await buildContext({ mode });
Expand All @@ -35,7 +41,6 @@ it.each<{
{ mode: 'test', currencyDeposit: 'BTCB.BEP20', currencyReceiving: 'sbBTC' },
{ mode: 'test', currencyDeposit: 'BTCB.BEP20', currencyReceiving: 'WBTC' },
{ mode: 'test', currencyDeposit: 'BTC', currencyReceiving: 'fake coin' as any },
{ mode: 'production', currencyDeposit: 'BTC', currencyReceiving: 'BTCB.BEP20' },
])('throws for %O', async ({ mode, currencyReceiving, currencyDeposit }) => {
expect.assertions(1);
const context = await buildContext({ mode });
Expand Down
2 changes: 1 addition & 1 deletion src/modules/swap/getSwapDetails/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ it.each<Pick<SkybridgeParams<'swap', 'test'>, 'hash'>>([

return expect(result).toMatchObject({
addressReceiving: '0x8d977a2c4736b42c89af5cff05e14dd8c1ca1d30',
addressDeposit: '1jkxfyP5Ds5MQzQrWv81mPBEGuYGyWWzb',
addressDeposit: expect.any(String),
amountDeposit: '0.23499648',
amountReceiving: '0.23437648',
currencyDeposit: 'BTC',
Expand Down

0 comments on commit 7960745

Please sign in to comment.