Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PLAT-42] Fixing market orders args #117

Merged
merged 5 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading