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

Commit

Permalink
feat: splitFund() to split fixed amount
Browse files Browse the repository at this point in the history
  • Loading branch information
totorogendut committed Jul 31, 2020
1 parent c5d7bd7 commit a2bd6b1
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
4 changes: 4 additions & 0 deletions RELEASE.md
@@ -1,5 +1,9 @@
# Releases

## 0.1.7

- FEATURE: new feature splitFund(amount) can return an array of parameter's fraction based on established fundme pointer pool.

## 0.1.6

- Bump dependencies
Expand Down
3 changes: 3 additions & 0 deletions src/fund/errors.ts
Expand Up @@ -60,6 +60,9 @@ export const invalidWeight = (address: string, weight: string | number | undefin
return `weight for payment pointer ${address}#${weight} is invalid.`;
};

// split fund
export const splitFundError = `must set web monetization pointer address with fund() before split.`;

/*****************************
* *
* Server-side fund() *
Expand Down
1 change: 1 addition & 0 deletions src/fund/mod.ts
@@ -1,2 +1,3 @@
export { fund } from "./fund";
export { splitFund } from "./split";
export { setDefaultAddress, getCurrentPointerPool, getCurrentPointerAddress } from "./utils";
31 changes: 31 additions & 0 deletions src/fund/split.ts
@@ -0,0 +1,31 @@
import { FundmeError, splitFundError } from "./errors";
import { getCurrentPointerPool, getPoolWeightSum } from "./utils";
import { calculateRelativeWeight } from "./relative-weight";
import { createPool } from "./set-pointer-multiple";

interface splittedFund {
address: string;
amount: number;
weight: string | number;
}

export function splitFund(amount: number): splittedFund[] {
let pool: WMPointer[] = createPool(getCurrentPointerPool());
let sum = 0;
if (!pool.length) {
throw FundmeError(splitFundError);
} else {
// process pointer pool
pool = calculateRelativeWeight(pool);
sum = getPoolWeightSum(pool);
}

return pool.map((pointer: WMPointer) => {
const fraction = (pointer.weight as number) / sum;
return {
address: pointer.address,
amount: amount * fraction,
weight: pointer.weight!,
};
});
}
37 changes: 37 additions & 0 deletions tests/fund/client-side/split-fund.spec.ts
@@ -0,0 +1,37 @@
import { FundmeError, splitFundError } from "../../../src/fund/errors";
import { fund } from "../../../src/fund/fund";
import { splitFund } from "../../../src/fund/split";
import { setCurrentPointer } from "../../../src/fund/utils";

describe("split fund works as expected", () => {
test("it split correct amount", () => {
fund([
"$wallet.example/test-1#10",
"$wallet.example/test-2#20",
"$wallet.example/platform#30%",
]);
const amount = 60;
expect(splitFund(amount)).toEqual([
{
address: "$wallet.example/test-1",
amount: 14,
weight: 7,
},
{
address: "$wallet.example/test-2",
amount: 28,
weight: 14,
},
{
address: "$wallet.example/platform",
amount: 18,
weight: 9,
},
]);
});

test("it throw an error if pointers haven't yet initiated", () => {
setCurrentPointer([]);
expect(() => splitFund(1)).toThrowError(FundmeError(splitFundError));
});
});

0 comments on commit a2bd6b1

Please sign in to comment.