Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
Fee calculation: Support blank fixed or percent costs (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
Morley Zhi authored Jul 17, 2019
1 parent 703d864 commit 2b815b7
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@stellar/wallet-sdk",
"version": "0.0.3-rc.12",
"version": "0.0.3-rc.13",
"description": "Libraries to help you write Stellar-enabled wallets in Javascript",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
65 changes: 65 additions & 0 deletions src/transfers/DepositProvider.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { DepositProvider } from "./DepositProvider";

import { DepositInfo } from "../types";

describe("fetchFinalFee", () => {
test("AnchorUSD", async () => {
const info: DepositInfo = {
USD: {
assetCode: "USD",
fee: { type: "simple", fixed: 5, percent: 1 },
minAmount: 15,
fields: [
{
description: "your email address for transaction status updates",
name: "email_address",
},
{
description: "amount in USD that you plan to deposit",
name: "amount",
},
],
},
};

const provider = new DepositProvider("test");

expect(
await provider.fetchFinalFee({
supportedAssets: info,
assetCode: info.USD.assetCode,
amount: "15",
type: "",
}),
).toEqual(5.15);
});

test("EUR from Nucleo staging", async () => {
const info: DepositInfo = {
EUR: {
assetCode: "EUR",
fee: { type: "simple", percent: 0.5 },
minAmount: 1,
maxAmount: 1000000000,
fields: [
{
description: "Type of deposit method for EUR",
choices: ["SWIFT", "SEPA"],
name: "type",
},
],
},
};

const provider = new DepositProvider("test");

expect(
await provider.fetchFinalFee({
supportedAssets: info,
assetCode: info.EUR.assetCode,
amount: "10",
type: "",
}),
).toEqual(0.05);
});
});
18 changes: 9 additions & 9 deletions src/transfers/TransferProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import queryString from "query-string";

import {
DepositInfo,
Fee,
FeeArgs,
Info,
RawInfoResponse,
Expand Down Expand Up @@ -33,33 +34,32 @@ export abstract class TransferProvider {
| Promise<DepositInfo>;

protected async fetchFinalFee(args: FeeArgs): Promise<number> {
const { supportedAssets, ...rest } = args;
const { supportedAssets, assetCode, amount } = args;

if (!supportedAssets[rest.assetCode]) {
throw new Error(
`Can't get fee for an unsupported asset, '${rest.assetCode}`,
);
if (!supportedAssets[assetCode]) {
throw new Error(`Can't get fee for an unsupported asset, '${assetCode}`);
}
const { fee } = supportedAssets[rest.assetCode];
const { fee } = supportedAssets[assetCode];
switch (fee.type) {
case "none":
return 0;
case "simple":
const simpleFee = fee as SimpleFee;
return (
(simpleFee.percent / 100) * Number(rest.amount) + simpleFee.fixed
((simpleFee.percent || 0) / 100) * Number(amount) +
(simpleFee.fixed || 0)
);
case "complex":
const response = await fetch(
`${this.transferServer}/fee?${queryString.stringify(rest)}`,
`${this.transferServer}/fee?${queryString.stringify(args)}`,
);
const { fee: feeResponse } = await response.json();
return feeResponse as number;

default:
throw new Error(
`Invalid fee type found! Got '${
fee.type
(fee as Fee).type
}' but expected one of 'none', 'simple', 'complex'`,
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/transfers/parseInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const parseFee = (
} else {
return {
type: feeEnabled ? "complex" : "none",
};
} as Fee;
}
};

Expand Down
18 changes: 12 additions & 6 deletions src/types/transfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export interface DepositAssetInfo {
assetCode: string;
fee: Fee;
minAmount: number;
maxAmount: number;
maxAmount?: number;
fields: Field[];
}

Expand Down Expand Up @@ -195,16 +195,22 @@ export interface Field {
choices?: string[];
}

export interface Fee {
type: "none" | "simple" | "complex";
export interface NoneFee {
type: "none";
}

export interface SimpleFee extends Fee {
export interface ComplexFee {
type: "complex";
}

export interface SimpleFee {
type: "simple";
fixed: number;
percent: number;
fixed?: number;
percent?: number;
}

export type Fee = NoneFee | ComplexFee | SimpleFee;

export interface Memo {
type: "text" | "id" | "hash";
value: string;
Expand Down

0 comments on commit 2b815b7

Please sign in to comment.