Skip to content

Commit

Permalink
[TypeScript] Add orderbook JSON types (#737)
Browse files Browse the repository at this point in the history
This PR adds orderbook JSON types

This change was included as part of #736 but I think it deserves some special attention.

### Test Plan

Unit tests validate serialization and deserialization.

### Commit History

* add orderbook JSON types

* separate JSON types
  • Loading branch information
nlordell authored May 6, 2020
1 parent d27b937 commit 6e0203d
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
7 changes: 6 additions & 1 deletion src/fraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,14 @@ export class Fraction {
return new Fraction(this.numerator.clone(), this.denominator.clone());
}

static fromJSON(o: any): Fraction {
static fromJSON(o: FractionJson): Fraction {
const numerator = new BN(o.numerator, "hex");
const denominator = new BN(o.denominator, "hex");
return new Fraction(numerator, denominator);
}
}

export interface FractionJson {
numerator: string;
denominator: string;
}
55 changes: 38 additions & 17 deletions src/orderbook.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Fraction } from "./fraction";
import { Fraction, FractionJson } from "./fraction";
import BN from "bn.js";

export class Offer {
Expand All @@ -18,11 +18,16 @@ export class Offer {
return new Offer(this.price.clone(), this.volume.clone());
}

static fromJSON(o: any): Offer {
return new Offer(Fraction.fromJSON(o.price), Fraction.fromJSON(o.volume));
static fromJSON(o: OfferJson): Offer {
return new Offer(Fraction.fromJSON(o.price), Fraction.fromJSON(o.volume))
}
}

export interface OfferJson {
price: FractionJson;
volume: FractionJson;
}

type Fee = { fee: Fraction };
type RemainingFractionAfterFee = { remainingFractionAfterFee: Fraction }

Expand Down Expand Up @@ -57,7 +62,7 @@ export class Orderbook {
return { bids, asks };
}

toJSON() {
toJSON(): OrderbookToJson {
return {
baseToken: this.baseToken,
quoteToken: this.quoteToken,
Expand All @@ -67,8 +72,9 @@ export class Orderbook {
};
}

static fromJSON(o: any): Orderbook {
const result = new Orderbook(o.baseToken, o.quoteToken, { remainingFractionAfterFee: Fraction.fromJSON(o.remainingFractionAfterFee) });
static fromJSON(o: OrderbookJson): Orderbook {
const remainingFractionAfterFee = Fraction.fromJSON(o.remainingFractionAfterFee);
const result = new Orderbook(o.baseToken, o.quoteToken, { remainingFractionAfterFee })
result.asks = offersFromJSON(o.asks);
result.bids = offersFromJSON(o.bids);
return result;
Expand Down Expand Up @@ -293,6 +299,22 @@ export class Orderbook {
}
}

export interface OrderbookToJson {
baseToken: string;
quoteToken: string;
remainingFractionAfterFee: Fraction;
asks: Record<string, Offer>;
bids: Record<string, Offer>;
}

export interface OrderbookJson {
baseToken: string;
quoteToken: string;
remainingFractionAfterFee: FractionJson;
asks: Record<string, OfferJson>;
bids: Record<string, OfferJson>;
}

/**
* Given a list of direct orderbooks this method returns the transitive orderbook
* between two tokens by computing the transitive closure via a certain number of "hops".
Expand Down Expand Up @@ -436,17 +458,16 @@ function invertPricePoints(
);
}


function offersFromJSON(o: any): Map<number, Offer> {
const offers = new Map();
for (let [key, value] of Object.entries(o)) {
offers.set(key, Offer.fromJSON(value));
function offersFromJSON(o: Record<string, OfferJson>): Map<number, Offer> {
const offers = new Map()
for (const [key, value] of Object.entries(o)) {
offers.set(key, Offer.fromJSON(value))
}
return offers;
return offers
}

function offersToJSON(offers: Map<number, Offer>): object {
const o: any = {};
offers.forEach((value, key) => { o[key] = value; });
return o;
}
function offersToJSON(offers: Map<number, Offer>): Record<string, Offer> {
const o: Record<string, Offer> = {}
offers.forEach((value, key) => { o[key] = value })
return o
}

0 comments on commit 6e0203d

Please sign in to comment.