Skip to content

Commit

Permalink
Merge pull request #39 from Polymarket/feature/marketable-limit-orders
Browse files Browse the repository at this point in the history
Marketable limit orders
  • Loading branch information
poly-rodr authored Jun 27, 2022
2 parents da5aeed + c7cd5e0 commit 2291741
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
51 changes: 51 additions & 0 deletions examples/marketOrderIOC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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 = process.env.CLOB_API_URL || "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);

const NO_TOKEN = "1343197538147866997676250008839231694243646439454152539053893078719042421992"

await clobClient.postOrder(await clobClient.createLimitOrder({
tokenID: NO_TOKEN,
price: 0.4,
side: Side.BUY,
size: 200,
}));
await clobClient.postOrder(await clobClient.createLimitOrder({
tokenID: NO_TOKEN,
price: 0.45,
side: Side.BUY,
size: 250,
}));

// Create a IOC market sell that will match
const ioc_order = await clobClient.createMarketOrder({
tokenID: NO_TOKEN,
side: Side.SELL,
size: 500,
worstPrice: 0.45,
timeInForce: "IOC"
});
console.log(`IOC market order: `);
console.log(ioc_order);

console.log(await clobClient.postOrder(ioc_order));
}

main();
4 changes: 2 additions & 2 deletions 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.31",
"version": "1.0.32",
"contributors": [
{
"name": "Jonathan Amenechi",
Expand All @@ -23,7 +23,7 @@
"@ethersproject/random": "^5.5.0",
"@ethersproject/transactions": "^5.5.0",
"@ethersproject/wallet": "^5.5.0",
"@polymarket/order-utils": "^1.2.13",
"@polymarket/order-utils": "^1.2.14",
"axios": "^0.21.1",
"commitizen": "^4.2.1",
"cz-conventional-changelog": "^3.3.0",
Expand Down
10 changes: 10 additions & 0 deletions src/order-builder/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
MarketOrderBuilder,
MarketOrderData,
SignatureType,
TimeInForce,
} from "@polymarket/order-utils";
import { ethers } from "ethers";
import { OrderCreationArgs, UserMarketOrder, UserLimitOrder, MarketOrderCreationArgs, Side } from "../types";
Expand Down Expand Up @@ -103,6 +104,8 @@ export const buildMarketOrderCreationArgs = async (

let minAmountReceived = "0"; // Default to 0

let timeInForce:TimeInForce = "FOK"

if (userOrder.side === Side.BUY) {
// market buy
makerAsset = collateral; // Set maker asset to collateral if market buy
Expand Down Expand Up @@ -135,6 +138,11 @@ export const buildMarketOrderCreationArgs = async (
minAmountReceived = ethers.utils.parseUnits(minAmt.toString(), COLLATERAL_TOKEN_DECIMALS).toString();
}
}

if (userOrder.timeInForce) {
timeInForce = userOrder.timeInForce
}

return {
chainID,
exchange,
Expand All @@ -147,6 +155,7 @@ export const buildMarketOrderCreationArgs = async (
takerAssetID,
signatureType,
minAmountReceived,
timeInForce
};
};

Expand Down Expand Up @@ -246,6 +255,7 @@ const buildMarketOrder = async (signer: Wallet | JsonRpcSigner, args: MarketOrde
signature: sig,
orderType: "market",
minAmountReceived: args.minAmountReceived,
timeInForce: args.timeInForce
};
console.log(`Generated Market order!`);
return orderAndSignature;
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { TimeInForce } from "@polymarket/order-utils";

export * from "@polymarket/order-utils";

export interface ApiKeyCreds {
Expand Down Expand Up @@ -59,6 +61,9 @@ export interface UserMarketOrder {
// (Optional) Worst price
// The worst price this market order can be executed at
worstPrice?: number;
// (Optional) FOK (fill or kill) / IOC (immediate or cancel)
// Default: FOK
timeInForce?: TimeInForce
}

export interface OrderPayload {
Expand Down Expand Up @@ -92,6 +97,7 @@ export interface MarketOrderCreationArgs {
takerAssetID?: string;
signatureType: number;
minAmountReceived: string;
timeInForce: TimeInForce
}

export interface ApiKeysResponse {
Expand Down
1 change: 1 addition & 0 deletions src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const marketOrderToJson = (mktOrder: MarketOrderAndSignature): any => {
signature: mktOrder.signature,
orderType: "market",
minAmountReceived: mktOrder.minAmountReceived,
timeInForce: mktOrder.timeInForce,
};
};

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -636,10 +636,10 @@
"@nodelib/fs.scandir" "2.1.5"
fastq "^1.6.0"

"@polymarket/order-utils@^1.2.13":
version "1.2.13"
resolved "https://registry.yarnpkg.com/@polymarket/order-utils/-/order-utils-1.2.13.tgz#ddff9c6f074ce363f7d281cd74766b3ba2ede992"
integrity sha512-QcZb4CIS0EF0+EJbHmOiXnG0ZY2S2NVJTD+zzrNBUkMVpyy9c863LBSkZnThzUOnkQlcwljcenzwObK8zvGU4g==
"@polymarket/order-utils@^1.2.14":
version "1.2.14"
resolved "https://registry.yarnpkg.com/@polymarket/order-utils/-/order-utils-1.2.14.tgz#279728af3dac24e2b17cdab5386034ba8281dc09"
integrity sha512-PbBaqwOvtUL1Ks/ZabtQpb/M8cVMR7TZqHfTOemJQbYsTPjuxwRWhyGr5BhgdnU8Dw60cXBEhBSw7b1lrCtrKw==

"@types/bn.js@^4.11.3":
version "4.11.6"
Expand Down

0 comments on commit 2291741

Please sign in to comment.