Skip to content

Commit

Permalink
implement getBetterWorse
Browse files Browse the repository at this point in the history
  • Loading branch information
epheph committed Dec 5, 2017
1 parent 9a00cd2 commit 910419a
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 76 deletions.
11 changes: 11 additions & 0 deletions scripts/payloads/getBetterWorseOrders-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": 22,
"jsonrpc": "2.0",
"method": "getBetterWorseOrders",
"params": {
"marketID": "0x866b5edd2a58dc095a52f2b18bba290b79fb253f",
"outcome": 0,
"orderType": "buy",
"price": 0.16
}
}
7 changes: 4 additions & 3 deletions scripts/payloads/getBetterWorseOrders.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
"jsonrpc": "2.0",
"method": "getBetterWorseOrders",
"params": {
"marketID": "0x0000000000000000000000000000000000000001",
"outcome": 1,
"normalizedPrice": "1.29282"
"marketID": "0x866b5edd2a58dc095a52f2b18bba290b79fb253f",
"outcome": 0,
"orderType": "buy",
"price": 0.1
}
}
22 changes: 9 additions & 13 deletions src/blockchain/sync-augur-node-with-blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,15 @@ export function syncAugurNodeWithBlockchain(db: Knex, augur: Augur, ethereumNod
if (err) return callback(err);
if (!rows || !rows.length || !rows[0]) return callback(new Error("blockchain_sync_history lookup failed"));
const row: HighestBlockNumberRow = rows[0];
if (row.highestBlockNumber === null) { // sync from scratch
const fromBlock: number = (!row || !row.highestBlockNumber) ? uploadBlockNumbers[networkID!] : row.highestBlockNumber + 1;
const highestBlockNumber: number = parseInt(augur.rpc.getCurrentBlock().number, 16) - 1;
if (fromBlock >= highestBlockNumber) return callback(null); // augur-node is already up-to-date
downloadAugurLogs(db, augur, fromBlock, highestBlockNumber, (err?: Error|null): void => {
if (err) return callback(err);
db.insert({ highestBlockNumber }).into("blockchain_sync_history").asCallback(callback);
console.log(`Finished batch load from ${fromBlock} to ${highestBlockNumber}`);
startAugurListeners(db, augur, highestBlockNumber);
});
} else {
callback(new Error("Please clear your augur.db and start over (must sync from scratch until issue #4386 is resolved)"));
}
const fromBlock: number = (!row || !row.highestBlockNumber) ? uploadBlockNumbers[networkID!] : row.highestBlockNumber + 1;
const highestBlockNumber: number = parseInt(augur.rpc.getCurrentBlock().number, 16) - 1;
if (fromBlock >= highestBlockNumber) return callback(null); // augur-node is already up-to-date
downloadAugurLogs(db, augur, fromBlock, highestBlockNumber, (err?: Error|null): void => {
if (err) return callback(err);
db.insert({ highestBlockNumber }).into("blockchain_sync_history").asCallback(callback);
console.log(`Finished batch load from ${fromBlock} to ${highestBlockNumber}`);
startAugurListeners(db, augur, highestBlockNumber);
});
});
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/server/dispatch-json-rpc-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function dispatchJsonRpcRequest(db: Knex, request: JsonRpcRequest, callba
case "getOrders":
return getOrders(db, request.params.universe, request.params.marketID, request.params.outcome, request.params.orderType, request.params.creator, request.params.orderState, request.params.earliestCreationTime, request.params.latestCreationTime, request.params.sortBy, request.params.isSortDescending, request.params.limit, request.params.offset, callback);
case "getBetterWorseOrders":
return getBetterWorseOrders(db, request.params.marketID, request.params.outcome, request.params.amount, request.params.price, callback);
return getBetterWorseOrders(db, request.params.marketID, request.params.outcome, request.params.orderType, request.params.price, callback);
default:
callback(new Error("unknown json rpc method"));
}
Expand Down
69 changes: 24 additions & 45 deletions src/server/getters/get-better-worse-orders.ts
Original file line number Diff line number Diff line change
@@ -1,55 +1,34 @@
import { each } from "async";
import { parallel } from "async";
import * as Knex from "knex";
import * as _ from "lodash";
import { Address, MarketsContractAddressRow } from "../../types";
import { Address, MarketsContractAddressRow, AsyncCallback } from "../../types";
import { queryModifier, getMarketsWithReportingState } from "./database";

/*
Logic:
immediateFill = There's two ways an order can be resolved.
1.) An open order for the same outcome !orderType (buy->sell) with >=amount or "better" price (means diff things based on buy|sell)
2.) If binary/scalar, an open order for the OPPOSITE outcome, with a price that allows us to reach that of complete set
*/

interface OrderPrice {
orderID: Address;
fullPrecisionPrice: string;
orderType: string;
interface PricesResult {
higherPriceRow: {orderID: string|null};
lowerPriceRow: {orderID: string|null};
}

export function getBetterWorseOrders(db: Knex, marketID: Address, outcome: number, orderType: string, amount: string, price: string|number, callback: (err?: Error|null, result?: any) => void): void {
if (marketID == null || outcome == null || price == null) return callback(new Error("Must provide marketID, outcome, and price"));
if ( orderType !== "buy" && orderType !== "sell" ) return callback(new Error(`orderType must be either buy|sell`));
const query: Knex.QueryBuilder = db.select("orderID", "fullPrecisionPrice", "orderType").from("orders").where({ marketID, outcome });
query.orderBy("fullPrecisionPrice", "asc");
let betterOrderID: string;
let worseOrderID: string;
query.asCallback((err: Error|null, orderPrices?: Array<OrderPrice>): void => {
let immediateFill = false;
if (orderPrices == null || orderPrices.length == 0) {
return callback(null, {immediateFill: false,
betterOrderID: "",
worseOrderID: "",
export function getBetterWorseOrders(db: Knex, marketID: Address, outcome: number, orderType: string, price: number, callback: (err?: Error|null, result?: any) => void): void {
if (marketID == null || outcome == null || orderType == null) return callback(new Error("Must provide marketID, outcome, and orderType"));
if ( orderType !== "buy" && orderType !== "sell" ) return callback(new Error(`orderType must be either "bid" or "ask"`));
const ordersQuery = db("orders").first("orderID").where({ orderState: "OPEN", marketID });
parallel({
higherPriceRow: (next: AsyncCallback) => ordersQuery.clone().where("price", ">", price).orderBy("price", "ASC").asCallback(next),
lowerPriceRow: (next: AsyncCallback) => ordersQuery.clone().where("price", "<", price).orderBy("price", "DESC").asCallback(next),
}, (err: Error|null, pricesResult: PricesResult ): void => {
if (err) return callback(err);
const { higherPriceRow, lowerPriceRow } = pricesResult;
if (orderType === "buy") {
return callback(null, {
betterOrderID: (higherPriceRow ? higherPriceRow.orderID : null),
worseOrderID: (lowerPriceRow ? lowerPriceRow.orderID : null),
});
} else {
const typedOrders = _.keyBy(orderPrices, (order) => order.orderType );
const counterSide = (orderType == "sell") ? "buy" : "sell";
let bestOrder: OrderPrice;
let worseOrder = orderPrices[orderPrices.length];
if ( orderType === "buy" ) {
bestOrder = orderPrices[0];
worseOrder = orderPrices[orderPrices.length];
} else if (orderType === "sell" ) {

} else {
return callback(new Error(`orderType must be "buy" or "sell"`))
}
console.log(typedOrders );
callback(null, {immediateFill: true,
betterOrderID: 0,
worseOrderID: 0,
return callback(null, {
betterOrderID: (lowerPriceRow ? lowerPriceRow.orderID : null),
worseOrderID: (higherPriceRow ? higherPriceRow.orderID : null),
});
}
}
});
}
}
58 changes: 44 additions & 14 deletions test/server/getters/get-better-worse-orders.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("server/getters/get-better-worse-orders", () => {
it(t.description, (done) => {
setupTestDb((err, db) => {
assert.isNull(err);
getBetterWorseOrders(db, t.params.marketID, t.params.outcome, t.params.amount, t.params.price, (err, betterWorseOrders) => {
getBetterWorseOrders(db, t.params.marketID, t.params.outcome, t.params.orderType, t.params.price, (err, betterWorseOrders) => {
t.assertions(err, betterWorseOrders);
done();
});
Expand All @@ -19,34 +19,64 @@ describe("server/getters/get-better-worse-orders", () => {
test({
description: "get better worse with no orders",
params: {
marketID: "0x0000000000000000000000000000000000000011",
marketID: "0x00000000000000000000000000000fffffffff11",
outcome: 1,
amount: 1,
orderType: "buy",
price: 2,
},
assertions: (err, betterWorseOrders) => {
assert.isNull(err);
assert.deepEqual(betterWorseOrders, {
immediateFill: true,
betterOrderID: 0,
worseOrderID: 0,
betterOrderID: null,
worseOrderID: null,
});
},
});
test({
description: "get better worse with orders",
description: "get better worse with better orders",
params: {
marketID: "0x6b112463b19eb428333f17f7635f5d97ee599880",
outcome: 1,
amount: 1,
normalizedPrice: 2,
marketID: "0x0000000000000000000000000000000000000001",
outcome: 0,
orderType: "buy",
price: 0.4,
},
assertions: (err, betterWorseOrders) => {
assert.isNull(err);
assert.deepEqual(betterWorseOrders, {
betterOrderID: "0x2000000000000000000000000000000000000000000000000000000000000000",
worseOrderID: null,
});
},
});
test({
description: "get better worse with worse orders",
params: {
marketID: "0x0000000000000000000000000000000000000001",
outcome: 0,
orderType: "buy",
price: 0.99,
},
assertions: (err, betterWorseOrders) => {
assert.isNull(err);
assert.deepEqual(betterWorseOrders, {
betterOrderID: null,
worseOrderID: "0x1000000000000000000000000000000000000000000000000000000000000000",
});
},
});
test({
description: "get better worse with better and worse orders",
params: {
marketID: "0x0000000000000000000000000000000000000001",
outcome: 0,
orderType: "buy",
price: 0.65,
},
assertions: (err, betterWorseOrders) => {
assert.isNull(err);
assert.deepEqual(betterWorseOrders, {
immediateFill: true,
betterOrderID: 0,
worseOrderID: 0,
betterOrderID: "0x1000000000000000000000000000000000000000000000000000000000000000",
worseOrderID: "0x2000000000000000000000000000000000000000000000000000000000000000",
});
},
});
Expand Down

0 comments on commit 910419a

Please sign in to comment.