Skip to content

Commit

Permalink
fix(ts): #224 cancelled order displayed as open still (#276)
Browse files Browse the repository at this point in the history
fix(ts): 224
  • Loading branch information
lidatong committed Nov 16, 2022
1 parent ff0861d commit 0def3f7
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 53 deletions.
4 changes: 2 additions & 2 deletions aptos/api/aux-ts/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,13 @@ export default class AuxAccount {
return market.openOrders(this.owner);
}

async orderHistory(marketInput: MarketInput): Promise<OrderPlacedEvent[]> {
async orderHistory(marketInput: MarketInput): Promise<{ order: OrderPlacedEvent; status: "open" | "canceled" | "filled" }[]> {
const market = await Market.read(this.auxClient, marketInput);
return market.orderHistory(this.owner);
}

async tradeHistory(marketInput: MarketInput): Promise<OrderFillEvent[]> {
const market = await Market.read(this.auxClient, marketInput);
return market.tradeHistory(this.owner);
return market.fillHistory(this.owner);
}
}
69 changes: 48 additions & 21 deletions aptos/api/aux-ts/src/clob/core/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,10 +362,7 @@ export async function openOrders(
arguments: [owner],
};
simulator = simulator ?? auxClient.simulator;
const txResult = await auxClient.simulateTransaction(
payload,
simulator,
);
const txResult = await auxClient.simulateTransaction(payload, simulator);
const event: RawOpenOrdersEvent = txResult.events[0]! as RawOpenOrdersEvent;
if (_.isUndefined(event)) {
return [];
Expand Down Expand Up @@ -393,37 +390,67 @@ export async function orderHistory(
quoteCoinType: Types.MoveStructTag,
owner: Types.Address | undefined,
pagination?: { start: BN } | { limit: BN }
): Promise<OrderPlacedEvent[]> {
const events = await orderPlacedEvents(
): Promise<
{ order: OrderPlacedEvent; status: "open" | "canceled" | "filled" }[]
> {
const market_ = await market(auxClient, baseCoinType, quoteCoinType);
const orders = await orderPlacedEvents(
auxClient,
auxClient.moduleAddress,
await market(auxClient, baseCoinType, quoteCoinType),
market_,
pagination
);
return events.filter(
(orderPlacedEvent) =>
_.isUndefined(owner) || orderPlacedEvent.owner.hex() === owner
const fills = await orderFillEvents(
auxClient,
auxClient.moduleAddress,
market_,
pagination
);
const fillIds = new Set(fills.map((order) => order.orderId.toString()));
const cancels = await orderCancelEvents(
auxClient,
auxClient.moduleAddress,
market_,
pagination
);
const cancelIds = new Set(cancels.map((order) => order.orderId.toString()));
const orderEvents: {
order: OrderPlacedEvent;
status: "open" | "canceled" | "filled";
}[] = orders.map((order) => {
const orderId = order.orderId.toString();
if (fillIds.has(orderId)) {
return { order, status: "filled" };
} else if (cancelIds.has(orderId)) {
return { order, status: "canceled" };
} else {
return { order, status: "open" };
}
});
if (_.isUndefined(owner)) {
return orderEvents;
}
return orderEvents.filter((order) => order.order.owner.hex() === owner);
}

export async function tradeHistory(
export async function fillHistory(
auxClient: AuxClient,
baseCoinType: Types.MoveStructTag,
quoteCoinType: Types.MoveStructTag,
owner: Types.Address | undefined,
pagination?: { start: BN } | { limit: BN }
): Promise<OrderFillEvent[]> {
return (
await orderFillEvents(
auxClient,
auxClient.moduleAddress,
await market(auxClient, baseCoinType, quoteCoinType),
pagination
)
).filter(
(orderFillEvent) =>
_.isUndefined(owner) || orderFillEvent.owner.hex() === owner
const fills = await orderFillEvents(
auxClient,
auxClient.moduleAddress,
await market(auxClient, baseCoinType, quoteCoinType),
pagination
);
if (_.isUndefined(owner)) {
// return all events for the market
return fills;
}
return fills.filter((orderFillEvent) => orderFillEvent.owner.hex() === owner);
}

export async function markets(
Expand Down
10 changes: 6 additions & 4 deletions aptos/api/aux-ts/src/clob/dsl/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,9 @@ export default class Market implements core.query.Market {
async orderHistory(
owner: Types.Address,
pagination?: { start: BN } | { limit: BN }
): Promise<OrderPlacedEvent[]> {
): Promise<
{ order: OrderPlacedEvent; status: "open" | "canceled" | "filled" }[]
> {
return core.query.orderHistory(
this.auxClient,
this.baseCoinInfo.coinType,
Expand All @@ -437,15 +439,15 @@ export default class Market implements core.query.Market {
}

/**
* Returns trades for the given trader.
* Returns fills for the given trader.
* @param owner
* @returns
*/
async tradeHistory(
async fillHistory(
owner?: Types.Address,
pagination?: { start: BN } | { limit: BN }
): Promise<OrderFillEvent[]> {
return core.query.tradeHistory(
return core.query.fillHistory(
this.auxClient,
this.baseCoinInfo.coinType,
this.quoteCoinInfo.coinType,
Expand Down
41 changes: 32 additions & 9 deletions aptos/api/aux-ts/src/graphql/conversion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,44 @@ export function orderToOrder(
};
}

export function orderEventToOrder(
orderEvent: OrderPlacedEvent | OrderFillEvent,
export function orderPlacedEventToOrder(
orderEvent: OrderPlacedEvent,
baseCoinInfo: CoinInfo,
quoteCoinInfo: CoinInfo
): GqlOrder {
const price = orderEvent.price
.toDecimalUnits(quoteCoinInfo.decimals)
.toNumber();
let quantity;
if (orderEvent.type === "OrderPlacedEvent") {
quantity = orderEvent.quantity;
} else {
quantity = orderEvent.baseQuantity;
}
quantity = quantity.toDecimalUnits(baseCoinInfo.decimals).toNumber();
const quantity = orderEvent.quantity
.toDecimalUnits(baseCoinInfo.decimals)
.toNumber();
return {
baseCoinType: baseCoinInfo.coinType,
quoteCoinType: quoteCoinInfo.coinType,
orderId: orderEvent.orderId.toString(),
owner: orderEvent.owner.toString(),
orderType: OrderType.Limit,
orderStatus: OrderStatus.Open,
side: orderEvent.isBid ? Side.Buy : Side.Sell,
auxBurned: 0,
time: orderEvent.timestamp.divn(1000).toString(),
price: orderEvent.price.toDecimalUnits(quoteCoinInfo.decimals).toNumber(),
quantity,
value: price * quantity,
};
}

export function orderFillEventToOrder(
orderEvent: OrderFillEvent,
baseCoinInfo: CoinInfo,
quoteCoinInfo: CoinInfo
): GqlOrder {
const price = orderEvent.price
.toDecimalUnits(quoteCoinInfo.decimals)
.toNumber();
const quantity = orderEvent.baseQuantity
.toDecimalUnits(baseCoinInfo.decimals)
.toNumber();
return {
baseCoinType: baseCoinInfo.coinType,
quoteCoinType: quoteCoinInfo.coinType,
Expand Down
6 changes: 3 additions & 3 deletions aptos/api/aux-ts/src/graphql/resolvers/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AU } from "../../";
import AuxAccount from "../../account";
import { parseTypeArgs } from "../../client";
import { auxClient } from "../client";
import { orderEventToOrder, orderToOrder } from "../conversion";
import { orderPlacedEventToOrder, orderFillEventToOrder, orderToOrder } from "../conversion";
import type {
Account,
AccountIsCoinRegisteredArgs,
Expand Down Expand Up @@ -212,7 +212,7 @@ export const account = {
const market = await aux.Market.read(auxClient, marketInput);
const orders = await account.orderHistory(marketInput);
return orders.map((order) =>
orderEventToOrder(order, market.baseCoinInfo, market.quoteCoinInfo)
orderPlacedEventToOrder(order.order, market.baseCoinInfo, market.quoteCoinInfo)
);
})
);
Expand All @@ -230,7 +230,7 @@ export const account = {
const market = await aux.Market.read(auxClient, marketInput);
const fills = await account.tradeHistory(marketInput);
return fills.map((fill) =>
orderEventToOrder(fill, market.baseCoinInfo, market.quoteCoinInfo)
orderFillEventToOrder(fill, market.baseCoinInfo, market.quoteCoinInfo)
);
})
);
Expand Down
16 changes: 6 additions & 10 deletions aptos/api/aux-ts/src/graphql/resolvers/market.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import _ from "lodash";
import * as aux from "../../";
import {
ALL_USD_STABLES,
COIN_MAPPING,
fakeMapping
} from "../../coin";
import { ALL_USD_STABLES, COIN_MAPPING, fakeMapping } from "../../coin";
import { auxClient, pythClient, redisClient } from "../client";
import { orderEventToOrder, orderToOrder } from "../conversion";
import { orderPlacedEventToOrder, orderFillEventToOrder, orderToOrder } from "../conversion";
import {
Bar,
Market,
Expand All @@ -21,7 +17,7 @@ import {
Order,
PythRating,
Resolution as GqlResolution,
Side
Side,
} from "../generated/types";
import { generatePythRating, LATEST_PYTH_PRICE } from "../pyth";

Expand Down Expand Up @@ -115,7 +111,7 @@ export const market = {
});
const orders = await market.orderHistory(owner);
return orders.map((order) =>
orderEventToOrder(order, market.baseCoinInfo, market.quoteCoinInfo)
orderPlacedEventToOrder(order.order, market.baseCoinInfo, market.quoteCoinInfo)
);
},
async tradeHistory(
Expand All @@ -126,11 +122,11 @@ export const market = {
baseCoinType: parent.baseCoinInfo.coinType,
quoteCoinType: parent.quoteCoinInfo.coinType,
});
const fills = await market.tradeHistory(owner);
const fills = await market.fillHistory(owner);
return fills
.filter((fill) => fill.sequenceNumber.toNumber() % 2 === 0)
.map((fill) =>
orderEventToOrder(fill, market.baseCoinInfo, market.quoteCoinInfo)
orderFillEventToOrder(fill, market.baseCoinInfo, market.quoteCoinInfo)
);
},
high24h(parent: Market): Promise<Maybe<number>> {
Expand Down
8 changes: 4 additions & 4 deletions aptos/api/aux-ts/src/indexer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { AuxClient } from "../client";
import type { Types } from "aptos";
import { RedisPubSub } from "graphql-redis-subscriptions";
import _ from "lodash";
import { orderEventToOrder } from "../graphql/conversion";
import { AuxEnv } from "../env";
import { orderFillEventToOrder } from "../graphql/conversion";
import type { MarketInput, Maybe, Side } from "../graphql/generated/types";
import { Resolution, RESOLUTIONS } from "../graphql/resolvers/query";
import { resolutionToSeconds } from "../graphql/resolvers/market";
import { AuxEnv } from "../env";
import { Resolution, RESOLUTIONS } from "../graphql/resolvers/query";

const auxEnv = new AuxEnv();
const auxClient = new AuxClient(auxEnv.aptosNetwork, auxEnv.aptosClient);
Expand Down Expand Up @@ -171,7 +171,7 @@ async function publishMarketEvents(
const trades = fills
.filter((fill) => fill.sequenceNumber.toNumber() % 2 === 0)
.map((fill) =>
orderEventToOrder(fill, market.baseCoinInfo, market.quoteCoinInfo)
orderFillEventToOrder(fill, market.baseCoinInfo, market.quoteCoinInfo)
)
.map((fill) => _.pick(fill, "side", "price", "quantity", "value", "time"));
await publishTrades(trades, { baseCoinType, quoteCoinType });
Expand Down

0 comments on commit 0def3f7

Please sign in to comment.