Skip to content

Commit

Permalink
Merge pull request #22 from Polymarket/fix/get-order-history
Browse files Browse the repository at this point in the history
Fix: get order history
  • Loading branch information
JonathanAmenechi authored Apr 5, 2022
2 parents 8430e33 + 225a45a commit 7aec008
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 4 deletions.
27 changes: 27 additions & 0 deletions examples/getOrderHistory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { ApiKeyCreds, ClobClient } from "../src";

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

async function main() {
const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL);
const pk = new ethers.Wallet(`${process.env.PK}`);
const wallet = pk.connect(provider);
console.log(`Address: ${await wallet.getAddress()}`);

const host = "http://localhost:8080";
const creds: ApiKeyCreds = {
key: `${process.env.CLOB_API_KEY}`,
secret: `${process.env.CLOB_SECRET}`,
passphrase: `${process.env.CLOB_PASS_PHRASE}`,
};
const clobClient = new ClobClient(host, wallet, creds);

console.log(`Response: `);
const resp = await clobClient.getOrderHistory();
console.log(resp);
}

main();
40 changes: 40 additions & 0 deletions examples/marketBuy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { ApiKeyCreds, ClobClient, Side } from "../src";

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

async function main() {
const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL);
const pk = new ethers.Wallet(`${process.env.PK}`);
const wallet = pk.connect(provider);
console.log(`Address: ${await wallet.getAddress()}`);

const host = "http://localhost:8080";
const creds: ApiKeyCreds = {
key: `${process.env.CLOB_API_KEY}`,
secret: `${process.env.CLOB_SECRET}`,
passphrase: `${process.env.CLOB_PASS_PHRASE}`,
};
const clobClient = new ClobClient(host, wallet, creds);

// YES: 16678291189211314787145083999015737376658799626183230671758641503291735614088
// Create a market buy order, using 100 USDC to buy as many tokens as possible
// Note: Size in market buy is the COLLATERAL size used to initiate the buy
// While in a market sell, size is the amount of YES/NO tokens used to initiate the sell
const order = await clobClient.createMarketOrder({
tokenID: "16678291189211314787145083999015737376658799626183230671758641503291735614088",
side: Side.BUY,
size: 100,
});
console.log(`Market order: `);
console.log(order);

// Send it to the server
const resp = await clobClient.postOrder(order);
console.log(resp);
console.log(`Done!`);
}

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": "1.0.18",
"version": "1.0.19",
"contributors": [
{
"name": "Jonathan Amenechi",
Expand Down
37 changes: 34 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import {
DELETE_API_KEY,
MIDPOINT,
PRICE,
OPEN_ORDERS,
ORDER_HISTORY,
} from "./endpoints";
import { OrderBuilder } from "./order-builder/builder";

Expand Down Expand Up @@ -151,7 +153,30 @@ export class ClobClient {
return get(`${this.host}${endpoint}`, headers);
}

public async getTradeHistory(): Promise<Order[]> {
public async getOrderHistory(tokenID?: string): Promise<any> {
this.canL2Auth();
const endpoint = ORDER_HISTORY;
const l2HeaderArgs = {
method: GET,
requestPath: endpoint,
};

const headers = await createL2Headers(
this.signer as Wallet | JsonRpcSigner,
this.creds as ApiKeyCreds,
l2HeaderArgs,
);

let url = `${this.host}${endpoint}`;

if (tokenID != null) {
url = `${url}?market=${tokenID}`;
}

return get(url, headers);
}

public async getTradeHistory(tokenID?: string): Promise<any> {
this.canL2Auth();

const endpoint = TRADE_HISTORY;
Expand All @@ -166,7 +191,13 @@ export class ClobClient {
headerArgs,
);

return get(`${this.host}${endpoint}`, headers);
let url = `${this.host}${endpoint}`;

if (tokenID != null) {
url = `${url}?market=${tokenID}`;
}

return get(url, headers);
}

public async createLimitOrder(userOrder: UserLimitOrder): Promise<LimitOrderAndSignature> {
Expand All @@ -185,7 +216,7 @@ export class ClobClient {

public async getOpenOrders(tokenID?: string): Promise<OpenOrdersResponse> {
this.canL2Auth();
const endpoint = "/open-orders";
const endpoint = OPEN_ORDERS;
const l2HeaderArgs = {
method: GET,
requestPath: endpoint,
Expand Down
2 changes: 2 additions & 0 deletions src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const DELETE_API_KEY = "/delete-api-key";

export const TRADE_HISTORY = "/trade-history";

export const ORDER_HISTORY = "/order-history";

export const GET_ORDER = "/order/";

export const OPEN_ORDERS = "/open-orders";
Expand Down

0 comments on commit 7aec008

Please sign in to comment.