Skip to content

Commit

Permalink
Merge pull request #117 from Polymarket/fix/market-order-types
Browse files Browse the repository at this point in the history
[PLAT-42] Fixing market orders args
  • Loading branch information
poly-rodr authored May 21, 2024
2 parents e90bffe + cd6f24f commit aab9620
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 43 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@polymarket/clob-client",
"description": "Typescript client for Polymarket's CLOB",
"version": "4.5.4",
"version": "4.5.5",
"contributors": [
{
"name": "Jonathan Amenechi",
Expand Down
30 changes: 28 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ import {
} from "./endpoints";
import { OrderBuilder } from "./order-builder/builder";
import { END_CURSOR, INITIAL_CURSOR } from "./constants";
import { calculateMarketPrice } from "./order-builder/helpers";

export class ClobClient {
readonly host: string;
Expand Down Expand Up @@ -507,8 +508,11 @@ export class ClobClient {
const negRisk = options?.negRisk ?? false;

if (!userMarketOrder.price) {
const marketPrice = await this.getPrice(tokenID, Side.BUY);
userMarketOrder.price = parseFloat(marketPrice);
userMarketOrder.price = await this.calculateMarketPrice(
tokenID,
Side.BUY,
userMarketOrder.amount,
);
}

if (!priceValid(userMarketOrder.price, tickSize)) {
Expand Down Expand Up @@ -824,6 +828,28 @@ export class ClobClient {
return this.get(`${this.host}${GET_MARKET_TRADES_EVENTS}${conditionID}`);
}

public async calculateMarketPrice(
tokenID: string,
side: Side,
amount: number,
): Promise<number> {
const book = await this.getOrderBook(tokenID);
if (!book) {
throw new Error("no orderbook");
}
if (side === Side.BUY) {
if (!book.asks) {
throw new Error("no match");
}
return calculateMarketPrice(book.asks, amount);
} else {
if (!book.bids) {
throw new Error("no match");
}
return calculateMarketPrice(book.bids, amount);
}
}

private canL1Auth(): void {
if (this.signer === undefined) {
throw L1_AUTH_UNAVAILABLE_ERROR;
Expand Down
15 changes: 14 additions & 1 deletion src/order-builder/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
TickSize,
RoundConfig,
CreateOrderOptions,
OrderSummary,
} from "../types";
import { decimalPlaces, roundDown, roundNormal, roundUp } from "../utilities";
import {
Expand Down Expand Up @@ -268,7 +269,7 @@ export const buildMarketBuyOrderCreationArgs = async (
feeRateBps,
nonce,
signer,
expiration: (userMarketOrder.expiration || 0).toString(),
expiration: "0",
signatureType,
} as OrderData;
};
Expand Down Expand Up @@ -301,3 +302,15 @@ export const createMarketBuyOrder = async (

return buildOrder(eoaSigner, exchangeContract, chainId, orderData);
};

export const calculateMarketPrice = (positions: OrderSummary[], amountToMatch: number) => {
let sum = 0;
for (let i = 0; i < positions.length; i++) {
const p = positions[i];
sum += parseFloat(p.size) * parseFloat(p.price);
if (sum >= amountToMatch) {
return parseFloat(p.price);
}
}
throw new Error("no match");
};
5 changes: 0 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,6 @@ export interface UserMarketOrder {
*/
nonce?: number;

/**
* Timestamp after which the order is expired.
*/
expiration?: number;

/**
* Address of the order taker. The zero address is used to indicate a public order
*/
Expand Down
Loading

0 comments on commit aab9620

Please sign in to comment.