Skip to content

Commit

Permalink
Merge pull request #118 from Polymarket/feat/update-balance-allowance
Browse files Browse the repository at this point in the history
New endpoint: Update balance and allowance
  • Loading branch information
poly-rodr authored Jun 7, 2024
2 parents 65c262d + 916244c commit c267bd9
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 2 deletions.
3 changes: 2 additions & 1 deletion examples/socketConnection.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable max-len */
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
dotenvConfig({ path: resolve(__dirname, "../.env") });
Expand Down Expand Up @@ -62,7 +63,7 @@ async function main(type: "user" | "market" | "live-activity") {
}
}

ws.on("error", function (_: any, err: Error) {
ws.on("error", function (err: Error) {
console.log("error SOCKET", "error", err);
process.exit(1);
});
Expand Down
37 changes: 37 additions & 0 deletions examples/updateBalanceAllowance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { ApiKeyCreds, AssetType, Chain, ClobClient } from "../src";

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

async function main() {
const wallet = new ethers.Wallet(`${process.env.PK}`);
const chainId = parseInt(`${process.env.CHAIN_ID || Chain.AMOY}`) as Chain;
console.log(`Address: ${await wallet.getAddress()}, chainId: ${chainId}`);

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, chainId, wallet, creds);

// USDC
await clobClient.updateBalanceAllowance({ asset_type: AssetType.COLLATERAL });

// YES
await clobClient.updateBalanceAllowance({
asset_type: AssetType.CONDITIONAL,
token_id: "71321045679252212594626385532706912750332728571942532289631379312455583992563",
});

// NO
await clobClient.updateBalanceAllowance({
asset_type: AssetType.CONDITIONAL,
token_id: "52114319501245915516055106046884209969926127482827954674443846427813813222426",
});
}

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": "4.7.0",
"version": "4.8.0",
"contributors": [
{
"name": "Jonathan Amenechi",
Expand Down
24 changes: 24 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ import {
GET_TOTAL_EARNINGS_FOR_USER_FOR_DAY,
GET_SPREAD,
GET_SPREADS,
UPDATE_BALANCE_ALLOWANCE,
} from "./endpoints";
import { OrderBuilder } from "./order-builder/builder";
import { END_CURSOR, INITIAL_CURSOR } from "./constants";
Expand Down Expand Up @@ -491,6 +492,29 @@ export class ClobClient {
return this.get(`${this.host}${endpoint}`, { headers, params: _params });
}

public async updateBalanceAllowance(params?: BalanceAllowanceParams): Promise<void> {
this.canL2Auth();

const endpoint = UPDATE_BALANCE_ALLOWANCE;
const headerArgs = {
method: GET,
requestPath: endpoint,
};

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

const _params = {
...params,
signature_type: this.orderBuilder.signatureType,
};

return this.get(`${this.host}${endpoint}`, { headers, params: _params });
}

public async createOrder(
userOrder: UserOrder,
options?: Partial<CreateOrderOptions>,
Expand Down
1 change: 1 addition & 0 deletions src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const DROP_NOTIFICATIONS = "/notifications";

// Balance
export const GET_BALANCE_ALLOWANCE = "/balance-allowance";
export const UPDATE_BALANCE_ALLOWANCE = "/balance-allowance/update";

// Live activity
export const GET_MARKET_TRADES_EVENTS = "/live-activity/events/";
Expand Down

0 comments on commit c267bd9

Please sign in to comment.