Skip to content

Commit

Permalink
improve naming, simplify test case
Browse files Browse the repository at this point in the history
  • Loading branch information
phraktle committed Jun 8, 2019
1 parent e7298bb commit 8168fde
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 48 deletions.
50 changes: 25 additions & 25 deletions src/Exchange.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {Rates} from "./Rates";
import {Transaction} from "./Transaction";
import {Ratio, Tokens, Wei} from "./units";

interface ISimpleMatchData {
tokens: Tokens;
ethers: Wei;
interface IMarketMatch {
filledTokens: Tokens;
filledEthers: Wei;
limitPrice: Ratio;
averagePrice: Ratio;
}
Expand All @@ -29,33 +29,33 @@ export class OrderBook {
return cmp !== 0 ? cmp : o1.id - o2.id;
}

private static estimateSimpleParams<T extends IOrder>(list: T[],
tokenAmount: Tokens,
ethFiatRate: Tokens,
toTokens: (order: T) => Tokens): ISimpleMatchData {
let remainingTokens: Tokens = tokenAmount;
private static estimateMarketOrder<T extends IOrder>(orders: T[],
tokens: Tokens,
rate: Tokens,
toTokens: (order: T) => Tokens): IMarketMatch {
let remainingTokens: Tokens = tokens;
let filledEthers: Wei = Wei.of(0);
let lastPrice: Ratio = Ratio.of(0);
let limitPrice: Ratio = Ratio.of(0);

for (const order of list) {
for (const order of orders) {
if (remainingTokens.isZero()) {
break;
}

const tokens: Tokens = Tokens.min(toTokens(order), remainingTokens);
const ethers: Wei = tokens.toWeiAt(ethFiatRate, order.price);
const fillTokens: Tokens = Tokens.min(toTokens(order), remainingTokens);
const fillEthers: Wei = fillTokens.toWeiAt(rate, order.price);

remainingTokens = remainingTokens.sub(tokens);
filledEthers = filledEthers.add(ethers);
lastPrice = order.price;
remainingTokens = remainingTokens.sub(fillTokens);
filledEthers = filledEthers.add(fillEthers);
limitPrice = order.price;
}
const totalTokens: Tokens = tokenAmount.sub(remainingTokens);
const averagePrice: Ratio = ethFiatRate.divToRatio(totalTokens.toRate(filledEthers));
const filledTokens: Tokens = tokens.sub(remainingTokens);
const averagePrice: Ratio = rate.divToRatio(filledTokens.toRate(filledEthers));

return {
tokens: totalTokens,
ethers: filledEthers,
limitPrice: lastPrice,
filledTokens,
filledEthers,
limitPrice,
averagePrice
};
}
Expand Down Expand Up @@ -143,11 +143,11 @@ export class OrderBook {
/**
* calculate price for n amount of token to buy
* @return {object} simple buy data { tokens, ethers, limitPrice, averagePrice }
* @param tokenAmount - amount of token to buy
* @param tokenAmount - amount of token to buy
* @param ethFiatRate - current rate
*/
public estimateSimpleBuy(tokenAmount: Tokens, ethFiatRate: Tokens): ISimpleMatchData {
return OrderBook.estimateSimpleParams(this.sellOrders, tokenAmount, ethFiatRate,
public estimateMarketBuy(tokenAmount: Tokens, ethFiatRate: Tokens): IMarketMatch {
return OrderBook.estimateMarketOrder(this.sellOrders, tokenAmount, ethFiatRate,
order => order.amount);
}

Expand All @@ -157,8 +157,8 @@ export class OrderBook {
* @param tokenAmount - amount of token to sell
* @param ethFiatRate - current rate
*/
public estimateSimpleSell(tokenAmount: Tokens, ethFiatRate: Tokens): ISimpleMatchData {
return OrderBook.estimateSimpleParams(this.buyOrders, tokenAmount, ethFiatRate,
public estimateMarketSell(tokenAmount: Tokens, ethFiatRate: Tokens): IMarketMatch {
return OrderBook.estimateMarketOrder(this.buyOrders, tokenAmount, ethFiatRate,
order => order.amount.toTokensAt(ethFiatRate, order.price));
}
}
Expand Down
46 changes: 23 additions & 23 deletions test/Exchange.simpleBuy.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { expect } = require("chai");
const { assert } = require("chai");

const { Augmint, utils, Wei, Tokens, Ratio } = require("../dist/index.js");
const loadEnv = require("./testHelpers/loadEnv.js");
Expand All @@ -12,46 +12,46 @@ if (config.LOG) {

const RATE = Tokens.of(400.00);

describe("calculate simplebuy", () => {
it("should return simplebuy data", () => {
function assertMatches(expected, actual) {
assert(actual.filledTokens.eq(expected.filledTokens));
assert(actual.filledEthers.eq(expected.filledEthers));
assert(actual.limitPrice.eq(expected.limitPrice));
assert(actual.averagePrice.eq(expected.averagePrice));
}

describe("calculate market orders", () => {
it("should return matching info", () => {
const buyOrders = [
{ amount: Wei.of(0.0070), buy: true, price: Ratio.of(1.2) },
{ amount: Wei.of(0.0094), buy: true, price: Ratio.of(1) },
{ amount: Wei.of(0.0064), buy: true, price: Ratio.of(0.7) }
{ amount: Wei.of(0.0070), price: Ratio.of(1.2) },
{ amount: Wei.of(0.0094), price: Ratio.of(1) },
{ amount: Wei.of(0.0064), price: Ratio.of(0.7) }
];


const sellOrders = [
{ amount: Tokens.of(1), buy: false, price: Ratio.of(1.02) },
{ amount: Tokens.of(10), buy: false, price: Ratio.of(1.03) }
{ amount: Tokens.of(1), price: Ratio.of(1.02) },
{ amount: Tokens.of(10), price: Ratio.of(1.03) }
];

const sellResult = {
tokens: Tokens.of(6),
ethers: Wei.of(0.016165),
filledTokens: Tokens.of(6),
filledEthers: Wei.of(0.016165),
limitPrice: Ratio.of(1),
averagePrice: Ratio.of(1.077673)
};

const sellMatches = new OrderBook(buyOrders, sellOrders).estimateSimpleSell(Tokens.of(6), RATE);
const sellMatches = new OrderBook(buyOrders, sellOrders).estimateMarketSell(Tokens.of(6), RATE);

expect(sellMatches.tokens.eq(sellResult.tokens)).to.be.equal(true);
expect(sellMatches.ethers.eq(sellResult.ethers)).to.be.equal(true);
expect(sellMatches.limitPrice.eq(sellResult.limitPrice)).to.be.equal(true);
expect(sellMatches.averagePrice.eq(sellResult.averagePrice)).to.be.equal(true);
assertMatches(sellResult, sellMatches);

const buyResult = {
tokens: Tokens.of(2),
ethers: Wei.of(0.005125),
filledTokens: Tokens.of(2),
filledEthers: Wei.of(0.005125),
limitPrice: Ratio.of(1.03),
averagePrice: Ratio.of(1.02501)
};

const buyMatches = new OrderBook(buyOrders, sellOrders).estimateSimpleBuy(Tokens.of(2), RATE);
const buyMatches = new OrderBook(buyOrders, sellOrders).estimateMarketBuy(Tokens.of(2), RATE);

expect(buyMatches.tokens.eq(buyResult.tokens)).to.be.equal(true);
expect(buyMatches.ethers.eq(buyResult.ethers)).to.be.equal(true);
expect(buyMatches.limitPrice.eq(buyResult.limitPrice)).to.be.equal(true);
expect(buyMatches.averagePrice.eq(buyResult.averagePrice)).to.be.equal(true);
assertMatches(buyResult, buyMatches);
});
});

0 comments on commit 8168fde

Please sign in to comment.