Skip to content

Commit

Permalink
Merge pull request #89 from Polymarket/feat/price-history
Browse files Browse the repository at this point in the history
Adapting price history to the new feature
  • Loading branch information
poly-rodr authored May 18, 2023
2 parents 957856c + 5670668 commit 13fb8fd
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 13 deletions.
49 changes: 46 additions & 3 deletions examples/getPricesHistory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { ApiKeyCreds, Chain, ClobClient, FilterParams } from "../src";
import {
ApiKeyCreds,
Chain,
ClobClient,
PriceHistoryFilterParams,
PriceHistoryInterval,
} from "../src";

dotenvConfig({ path: resolve(__dirname, "../.env") });

Expand All @@ -27,17 +33,54 @@ async function main() {
startTs: new Date().getTime() / 1000 - 1000,
endTs: new Date().getTime() / 1000,
market: YES_TOKEN_ID,
} as FilterParams);
} as PriceHistoryFilterParams);

console.log(yes_prices_history);

const no_prices_history = await clobClient.getPricesHistory({
startTs: new Date().getTime() / 1000 - 1000,
endTs: new Date().getTime() / 1000,
market: NO_TOKEN_ID,
} as FilterParams);
} as PriceHistoryFilterParams);

console.log(no_prices_history);

// intervals
// ONE HOUR
const one_hour_history = await clobClient.getPricesHistory({
market: YES_TOKEN_ID,
interval: PriceHistoryInterval.ONE_HOUR,
fidelity: 1,
} as PriceHistoryFilterParams);

console.log(one_hour_history);

// SIX HOURS
const six_hours_history = await clobClient.getPricesHistory({
market: YES_TOKEN_ID,
interval: PriceHistoryInterval.SIX_HOURS,
fidelity: 3,
} as PriceHistoryFilterParams);

console.log(six_hours_history);

// ONE DAY
const one_day_history = await clobClient.getPricesHistory({
market: YES_TOKEN_ID,
interval: PriceHistoryInterval.ONE_DAY,
fidelity: 5,
} as PriceHistoryFilterParams);

console.log(one_day_history);

// ONE WEEK
const one_week_history = await clobClient.getPricesHistory({
market: YES_TOKEN_ID,
interval: PriceHistoryInterval.ONE_WEEK,
fidelity: 10,
} as PriceHistoryFilterParams);

console.log(one_week_history);
}

main();
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": "2.2.0",
"version": "2.3.0",
"contributors": [
{
"name": "Jonathan Amenechi",
Expand Down
8 changes: 4 additions & 4 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
ApiKeyCreds,
ApiKeysResponse,
Chain,
FilterParams,
MarketPrice,
OpenOrderParams,
OpenOrdersResponse,
Expand All @@ -30,14 +29,15 @@ import {
TickSizes,
TickSize,
OrdersScoringParams,
PriceHistoryFilterParams,
} from "./types";
import { createL1Headers, createL2Headers } from "./headers";
import {
addBalanceAllowanceParamsToUrl,
addFilterParamsToUrl,
addOpenOrderParamsToUrl,
addOrderScoringParamsToUrl,
addOrdersScoringParamsToUrl,
addPriceHistoryFilterParamsToUrl,
addTradeNotificationParamsToUrl,
addTradeParamsToUrl,
del,
Expand Down Expand Up @@ -178,8 +178,8 @@ export class ClobClient {
return get(`${this.host}${GET_LARGE_ORDERS}?min_value=${minValue}`);
}

public async getPricesHistory(params: FilterParams): Promise<MarketPrice[]> {
const url = addFilterParamsToUrl(`${this.host}${GET_PRICES_HISTORY}`, params);
public async getPricesHistory(params: PriceHistoryFilterParams): Promise<MarketPrice[]> {
const url = addPriceHistoryFilterParamsToUrl(`${this.host}${GET_PRICES_HISTORY}`, params);
return get(url);
}

Expand Down
27 changes: 27 additions & 0 deletions src/http-helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
FilterParams,
OrderScoringParams,
OrdersScoringParams,
PriceHistoryFilterParams,
TradeNotificationParams,
TradeParams,
} from "src/types";
Expand Down Expand Up @@ -254,3 +255,29 @@ export const addOrdersScoringParamsToUrl = (
}
return url;
};

export const addPriceHistoryFilterParamsToUrl = (
baseUrl: string,
params?: PriceHistoryFilterParams,
): string => {
let url = baseUrl;
if (params !== undefined) {
url = `${url}?`;
if (params.market !== undefined) {
url = buildQueryParams(url, "market", params.market as string);
}
if (params.startTs !== undefined) {
url = buildQueryParams(url, "startTs", `${params.startTs}`);
}
if (params.endTs !== undefined) {
url = buildQueryParams(url, "endTs", `${params.endTs}`);
}
if (params.fidelity !== undefined) {
url = buildQueryParams(url, "fidelity", `${params.fidelity}`);
}
if (params.interval !== undefined) {
url = buildQueryParams(url, "interval", `${params.interval}`);
}
}
return url;
};
23 changes: 18 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,24 @@ export enum Chain {
}

export interface MarketPrice {
t: string; // timestamp
l: string; // liquidity
p: string; // price
v: string; // volume
ps: string; // positions
t: number; // timestamp
p: number; // price
}

export interface PriceHistoryFilterParams {
market?: string;
startTs?: number;
endTs?: number;
fidelity?: number;
interval?: PriceHistoryInterval;
}

export enum PriceHistoryInterval {
MAX = "max",
ONE_WEEK = "1w",
ONE_DAY = "1d",
SIX_HOURS = "6h",
ONE_HOUR = "1h",
}

export interface TradeNotificationParams {
Expand Down
20 changes: 20 additions & 0 deletions tests/http-helpers/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import {
addBalanceAllowanceParamsToUrl,
addOrderScoringParamsToUrl,
addOrdersScoringParamsToUrl,
addPriceHistoryFilterParamsToUrl,
} from "../../src/http-helpers/index";
import {
AssetType,
BalanceAllowanceParams,
OpenOrderParams,
OrderScoringParams,
OrdersScoringParams,
PriceHistoryInterval,
Side,
TradeNotificationParams,
TradeParams,
Expand Down Expand Up @@ -155,4 +157,22 @@ describe("utilities", () => {
expect(url).equal("http://tracker?order_ids=0x0,0x1,0x2");
});
});

describe("addPriceHistoryFilterParamsToUrl", () => {
it("checking url + params", () => {
const url = addPriceHistoryFilterParamsToUrl("http://tracker", {
market: "10000",
startTs: 1450000,
endTs: 1460000,
fidelity: 15,
interval: PriceHistoryInterval.SIX_HOURS,
});
expect(url).not.null;
expect(url).not.undefined;
expect(url).not.empty;
expect(url).equal(
"http://tracker?market=10000&startTs=1450000&endTs=1460000&fidelity=15&interval=6h",
);
});
});
});

0 comments on commit 13fb8fd

Please sign in to comment.