Skip to content

Commit

Permalink
feat: estimateAmountReceiving works for floats too (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbp-rib authored Dec 27, 2020
1 parent a78618b commit f789ed4
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 36 deletions.
4 changes: 2 additions & 2 deletions examples/swap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { buildContext, createSwap, estimateSwapAmountReceiving } from '@swingby-protocol/sdk';
import { buildContext, createSwap, estimateAmountReceiving } from '@swingby-protocol/sdk';

const addressReceiving = '0x3F4341a0599f63F444B6f1e0c7C5cAf81b5843Cc';
const amountDesired = '1';
Expand All @@ -8,7 +8,7 @@ const currencyReceiving = 'WBTC';
(async () => {
const context = await buildContext({ mode: 'test' });

const { feeTotal, feeBridgePercent, feeMiner, feeCurrency } = await estimateSwapAmountReceiving({
const { feeTotal, feeBridgePercent, feeMiner, feeCurrency } = await estimateAmountReceiving({
context,
amountDesired,
currencyDeposit,
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export type { SkybridgeCoin } from './modules/coins';
export type { SkybridgeChain } from './modules/chains';

export { getCoinsFor, getSwapableWith } from './modules/coins';
export { createSwap, estimateSwapAmountReceiving, getSwapDetails } from './modules/swap';
export { createSwap, getSwapDetails } from './modules/swap';
export { buildContext } from './modules/context';
export { isAddressValid } from './modules/validate-address';
export { buildExplorerLink } from './modules/explorer-link';
Expand All @@ -18,3 +18,4 @@ export { createWithdrawal, getWithdrawalDetails } from './modules/withdrawal';
export { isSkybridgeBridge } from './modules/bridges';
export { isSkybridgeMode } from './modules/modes';
export { isSkybridgeResource } from './modules/resources';
export { estimateAmountReceiving } from './modules/generic-fees';
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import type { SkybridgeCoin } from '../../coins';
import { buildContext } from '../../context';
import type { SkybridgeMode } from '../../modes';
import type { SkybridgeResource } from '../../resources';

import { calculateSwapFees } from './';
import { calculateFees } from './';

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

it.each<
[
{
currencyDeposit: SkybridgeCoin<'swap', 'test', 'in'>;
currencyReceiving: SkybridgeCoin<'swap', 'test', 'out'>;
currencyDeposit: SkybridgeCoin<SkybridgeResource, 'test', 'in'>;
currencyReceiving: SkybridgeCoin<SkybridgeResource, 'test', 'out'>;
},
any,
]
Expand All @@ -21,17 +22,21 @@ it.each<
// ],
[
{ currencyDeposit: 'BTC', currencyReceiving: 'WBTC' },
{ feeBridgePercent: '0.002', feeMiner: '0.00025', feeCurrency: 'WBTC' },
{ feeBridgePercent: '0.002', feeMiner: '0.00015', feeCurrency: 'WBTC' },
],
[
{ currencyDeposit: 'WBTC', currencyReceiving: 'BTC' },
{ feeBridgePercent: '0.002', feeMiner: '0.0003', feeCurrency: 'BTC' },
{ feeBridgePercent: '0.002', feeMiner: '0.00001', feeCurrency: 'BTC' },
],
[
{ currencyDeposit: 'WBTC', currencyReceiving: 'sbBTC' },
{ feeBridgePercent: '0.002', feeMiner: '0.00015', feeCurrency: 'sbBTC' },
],
])('works for %O', async ({ currencyDeposit, currencyReceiving }, expected) => {
expect.assertions(1);

const context = await buildContext({ mode: 'test' });
const result = await calculateSwapFees({
const result = await calculateFees({
context,
currencyDeposit,
currencyReceiving,
Expand All @@ -49,7 +54,7 @@ it.each<{ currencyDeposit: any; mode: SkybridgeMode; currencyReceiving: any }>([

const context = await buildContext({ mode });
try {
await calculateSwapFees({
await calculateFees({
context,
currencyDeposit,
currencyReceiving,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ import { fetch } from '../../fetch';
import type { SkybridgeMode } from '../../modes';
import type { SkybridgeParams } from '../../common-params';
import type { SkybridgeCoin } from '../../coins';
import { SkybridgeResource } from '../../resources';

export const calculateSwapFees = async <M extends SkybridgeMode>({
export const calculateFees = async <M extends SkybridgeMode>({
context,
currencyDeposit,
currencyReceiving,
}: Pick<SkybridgeParams<'swap', M>, 'context' | 'currencyDeposit' | 'currencyReceiving'>): Promise<
Pick<SkybridgeParams<'swap', M>, 'feeBridgePercent' | 'feeMiner' | 'feeCurrency'>
}: Pick<
SkybridgeParams<SkybridgeResource, M>,
'context' | 'currencyDeposit' | 'currencyReceiving'
>): Promise<
Pick<SkybridgeParams<SkybridgeResource, M>, 'feeBridgePercent' | 'feeMiner' | 'feeCurrency'>
> => {
const bridge = getBridgeFor({ context, currencyDeposit, currencyReceiving });
const result = await fetch<
Array<{ bridgeFeePercent: string; currency: SkybridgeCoin<'swap', M>; minerFee: string }>
Array<{
bridgeFeePercent: string;
currency: SkybridgeCoin<SkybridgeResource, M>;
minerFee: string;
}>
>(`${context.servers.swapNode[bridge]}/api/v1/swaps/fees`);

if (!result.ok) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import type { SkybridgeCoin } from '../../coins';
import { buildContext } from '../../context';
import type { SkybridgeMode } from '../../modes';
import { SkybridgeResource } from '../../resources';

import { estimateSwapAmountReceiving } from '.';
import { estimateAmountReceiving } from '.';

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

it.each<
[
{
amountDesired: string;
currencyDeposit: SkybridgeCoin<'swap', 'test', 'in'>;
currencyReceiving: SkybridgeCoin<'swap', 'test', 'out'>;
currencyDeposit: SkybridgeCoin<SkybridgeResource, 'test', 'in'>;
currencyReceiving: SkybridgeCoin<SkybridgeResource, 'test', 'out'>;
},
any,
]
Expand All @@ -29,38 +30,48 @@ it.each<
[
{ amountDesired: '3', currencyDeposit: 'BTC', currencyReceiving: 'WBTC' },
{
amountReceiving: '2.99375',
amountReceiving: '2.99385',
feeBridgePercent: '0.002',
feeMiner: '0.00025',
feeMiner: '0.00015',
feeCurrency: 'WBTC',
feeTotal: '0.00625',
feeTotal: '0.00615',
},
],
[
{ amountDesired: '156', currencyDeposit: 'WBTC', currencyReceiving: 'BTC' },
{
amountReceiving: '155.6877',
amountReceiving: '155.68799',
feeBridgePercent: '0.002',
feeMiner: '0.0003',
feeMiner: '0.00001',
feeCurrency: 'BTC',
feeTotal: '0.3123',
feeTotal: '0.31201',
},
],
[
{ amountDesired: '156', currencyDeposit: 'sbBTC', currencyReceiving: 'WBTC' },
{
amountReceiving: '155.68775',
amountReceiving: '155.68785',
feeBridgePercent: '0.002',
feeMiner: '0.00025',
feeMiner: '0.00015',
feeCurrency: 'WBTC',
feeTotal: '0.31225',
feeTotal: '0.31215',
},
],
[
{ amountDesired: '156', currencyDeposit: 'BTC', currencyReceiving: 'sbBTC' },
{
amountReceiving: '155.68785',
feeBridgePercent: '0.002',
feeMiner: '0.00015',
feeCurrency: 'sbBTC',
feeTotal: '0.31215',
},
],
])('works for %O', async ({ amountDesired, currencyDeposit, currencyReceiving }, expected) => {
expect.assertions(1);

const context = await buildContext({ mode: 'test' });
const result = await estimateSwapAmountReceiving({
const result = await estimateAmountReceiving({
context,
currencyDeposit,
currencyReceiving,
Expand All @@ -79,7 +90,7 @@ it.each<{ currencyDeposit: any; mode: SkybridgeMode; currencyReceiving: any }>([

const context = await buildContext({ mode });
try {
await estimateSwapAmountReceiving({
await estimateAmountReceiving({
context,
currencyDeposit,
currencyReceiving,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@ import { Big } from 'big.js';

import type { SkybridgeMode } from '../../modes';
import type { SkybridgeParams } from '../../common-params';
import { calculateSwapFees } from '../calculateSwapFees';
import { calculateFees } from '../calculateFees';
import { SkybridgeResource } from '../../resources';

export const estimateSwapAmountReceiving = async <M extends SkybridgeMode>({
export const estimateAmountReceiving = async <M extends SkybridgeMode>({
context,
currencyDeposit,
currencyReceiving,
amountDesired,
}: Pick<
SkybridgeParams<'swap', M>,
SkybridgeParams<SkybridgeResource, M>,
'context' | 'amountDesired' | 'currencyDeposit' | 'currencyReceiving'
>): Promise<
Pick<
SkybridgeParams<'swap', M>,
SkybridgeParams<SkybridgeResource, M>,
'feeBridgePercent' | 'feeMiner' | 'feeCurrency' | 'amountReceiving' | 'feeTotal'
>
> => {
const fees = await calculateSwapFees({ context, currencyDeposit, currencyReceiving });
const fees = await calculateFees({ context, currencyDeposit, currencyReceiving });
const totalFee = new Big(amountDesired).times(fees.feeBridgePercent).plus(fees.feeMiner);

return {
Expand Down
1 change: 1 addition & 0 deletions src/modules/generic-fees/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { estimateAmountReceiving } from './estimateAmountReceiving';
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 @@ -40,7 +40,7 @@ it.each<Pick<SkybridgeParams<'swap', 'test'>, 'hash'>>([

return expect(result).toMatchObject({
addressReceiving: '0x3f4341a0599f63f444b6f1e0c7c5caf81b5843cc',
addressDeposit: '0x13001cb1C8dbB605Ba7Ba438c7103988D3d16B71',
addressDeposit: '0xB7dA48101a29697B78a1f5ece4b6975506B88819',
amountDeposit: '0.00098851',
amountReceiving: '0.00073653',
currencyDeposit: 'sbBTC',
Expand Down
1 change: 0 additions & 1 deletion src/modules/swap/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export { createSwap } from './createSwap';
export { estimateSwapAmountReceiving } from './estimateSwapAmountReceiving';
export { getSwapDetails } from './getSwapDetails';
2 changes: 1 addition & 1 deletion src/modules/withdrawal/getWithdrawalDetails/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: '0x3f4341a0599f63f444b6f1e0c7c5caf81b5843cc',
addressDeposit: '0x13001cb1C8dbB605Ba7Ba438c7103988D3d16B71',
addressDeposit: '0xB7dA48101a29697B78a1f5ece4b6975506B88819',
amountDeposit: '0.00098851',
amountReceiving: '0.00073653',
currencyDeposit: 'sbBTC',
Expand Down

0 comments on commit f789ed4

Please sign in to comment.