Skip to content

Commit

Permalink
inverse formula
Browse files Browse the repository at this point in the history
  • Loading branch information
Velua committed Oct 21, 2019
1 parent 433e174 commit 53a86a7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
26 changes: 13 additions & 13 deletions __tests__/bancorx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ test("bancorx.bancorFormula - EOS/BNT", () => {
});
});

test.skip("bancorx.bancorInverseFormula - EOS/BNT", () => {
const balanceFrom = split(`77814.0638 EOS`);
const balanceTo = split(`429519.5539120331 BNT`);
const amountDesired = split(`1.0000000000 BNT`);
expect(balanceFrom.symbol.precision).toBe(4);
expect(balanceTo.symbol.precision).toBe(10);
expect(amountDesired.symbol.precision).toBe(10);

expect(
bancorx
.bancorInverseFormula(balanceFrom, balanceTo, amountDesired)
.toNumber()
).toBe(0.1811657798);
test("bancorx.bancorInverseFormula - EOS/BNT", () => {
trades
.map(([amount, bluBalance, redBalance, reward]) => [
split(amount),
split(bluBalance),
split(redBalance),
split(reward)
])
.forEach(([amount, blueBalance, redBalance, reward]) => {
expect(
bancorx.bancorInverseFormula(blueBalance, redBalance, reward)
).toEqual(amount);
});
});

test("bancorx.composeMemo", () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bancorx",
"version": "0.2.65",
"version": "0.2.66",
"description": "BancorX Utility",
"main": "build/index.js",
"types": "index.d.ts",
Expand Down
19 changes: 15 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,21 @@ export function bancorInverseFormula(
) {
if (!balanceTo.symbol.isEqual(amountDesired.symbol))
throw new Error("From symbol does not match amount symbol");
const reward =
balanceFrom.amount / (1.0 - amountDesired.amount / balanceTo.amount) -
balanceFrom.amount;
return new Asset(reward, amountDesired.symbol);
const balanceFromNumber = balanceFrom.toDecimal()
const balanceToNumber = balanceTo.toDecimal()
const amountNumber = amountDesired.toDecimal()
const oneNumber = new Decimal(1);

const reward = balanceFromNumber
.div(oneNumber.minus(amountNumber.div(balanceToNumber)))
.minus(balanceFromNumber)
.toFixed(0, Decimal.ROUND_UP);

const formatted = new Decimal(
Number(reward) * Math.pow(10, balanceTo.symbol.precision)
).toFixed(0, Decimal.ROUND_DOWN);

return new Asset(Number(formatted), balanceFrom.symbol);
}

/**
Expand Down

0 comments on commit 53a86a7

Please sign in to comment.