From 40de6f0f30fcb1b3beadadf4ba566c71b7620900 Mon Sep 17 00:00:00 2001 From: Rodrigo <95635797+poly-rodr@users.noreply.github.com> Date: Fri, 27 May 2022 17:57:55 -0300 Subject: [PATCH] Adding Clob API URL Env var --- .env.example | 3 +- README.md | 2 +- examples/cancelAll.ts | 2 +- examples/cancelOrder.ts | 2 +- examples/createApiKey.ts | 2 +- examples/deriveApiKey.ts | 2 +- examples/getApiKeys.ts | 2 +- examples/getOpenOrders.ts | 2 +- examples/getOrderHistory.ts | 2 +- examples/getOrderbook.ts | 2 +- examples/getPrice.ts | 46 +++++++++++++++++++++++++++++ examples/getServerTime.ts | 2 +- examples/getTradeHistory.ts | 2 +- examples/limitOrder.ts | 2 +- examples/marketBuy.ts | 2 +- examples/marketOrder.ts | 2 +- examples/marketOrderWorstPrice.ts | 2 +- examples/populateOrderbook.ts | 2 +- examples/testMultiSigMarketBuy.ts | 2 +- examples/testPolyProxyMarketBuy.ts | 2 +- examples/testPolyProxyMarketSell.ts | 2 +- 21 files changed, 67 insertions(+), 20 deletions(-) create mode 100644 examples/getPrice.ts diff --git a/.env.example b/.env.example index ffe093d..4c17dff 100644 --- a/.env.example +++ b/.env.example @@ -2,4 +2,5 @@ RPC_URL=http://polygon-mumbai.infura.io/v3/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx PK=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx CLOB_API_KEY= CLOB_SECRET= -CLOB_PASS_PHRASE= \ No newline at end of file +CLOB_PASS_PHRASE= +CLOB_API_URL= \ No newline at end of file diff --git a/README.md b/README.md index 4cd0b20..eda1f69 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Typescript client for the Polymarket CLOB ### Usage ```ts -const host = "http://localhost:8080"; +const host = process.env.CLOB_API_URL || "http://localhost:8080"; const signer = new ethers.Wallet(`${process.env.PK}`); const creds: ApiKeyCreds = { key: `${process.env.CLOB_API_KEY}`, diff --git a/examples/cancelAll.ts b/examples/cancelAll.ts index e4d42c8..1c972ba 100644 --- a/examples/cancelAll.ts +++ b/examples/cancelAll.ts @@ -11,7 +11,7 @@ async function main() { const wallet = pk.connect(provider); console.log(`Address: ${await wallet.getAddress()}`); - const host = "http://localhost:8080"; + const host = process.env.CLOB_API_URL || "http://localhost:8080"; const creds: ApiKeyCreds = { key: `${process.env.CLOB_API_KEY}`, secret: `${process.env.CLOB_SECRET}`, diff --git a/examples/cancelOrder.ts b/examples/cancelOrder.ts index 52dfde5..91241e9 100644 --- a/examples/cancelOrder.ts +++ b/examples/cancelOrder.ts @@ -11,7 +11,7 @@ async function main() { const wallet = pk.connect(provider); console.log(`Address: ${await wallet.getAddress()}`); - const host = "http://localhost:8080"; + const host = process.env.CLOB_API_URL || "http://localhost:8080"; const creds: ApiKeyCreds = { key: `${process.env.CLOB_API_KEY}`, secret: `${process.env.CLOB_SECRET}`, diff --git a/examples/createApiKey.ts b/examples/createApiKey.ts index 999d445..40b56cc 100644 --- a/examples/createApiKey.ts +++ b/examples/createApiKey.ts @@ -11,7 +11,7 @@ async function main() { const wallet = pk.connect(provider); console.log(`Address: ${await wallet.getAddress()}`); - const host = "http://localhost:8080"; + const host = process.env.CLOB_API_URL || "http://localhost:8080"; const clobClient = new ClobClient(host, wallet); console.log(`Response: `); diff --git a/examples/deriveApiKey.ts b/examples/deriveApiKey.ts index 135fba7..3774fa6 100644 --- a/examples/deriveApiKey.ts +++ b/examples/deriveApiKey.ts @@ -11,7 +11,7 @@ async function main() { const wallet = pk.connect(provider); console.log(`Address: ${await wallet.getAddress()}`); - const host = "http://localhost:8080"; + const host = process.env.CLOB_API_URL || "http://localhost:8080"; const clobClient = new ClobClient(host, wallet); console.log(`Response: `); diff --git a/examples/getApiKeys.ts b/examples/getApiKeys.ts index 1d128b4..1c7066a 100644 --- a/examples/getApiKeys.ts +++ b/examples/getApiKeys.ts @@ -11,7 +11,7 @@ async function main() { const wallet = pk.connect(provider); console.log(`Address: ${await wallet.getAddress()}`); - const host = "http://localhost:8080"; + const host = process.env.CLOB_API_URL || "http://localhost:8080"; const creds: ApiKeyCreds = { key: `${process.env.CLOB_API_KEY}`, secret: `${process.env.CLOB_SECRET}`, diff --git a/examples/getOpenOrders.ts b/examples/getOpenOrders.ts index 257ad2e..831b18a 100644 --- a/examples/getOpenOrders.ts +++ b/examples/getOpenOrders.ts @@ -11,7 +11,7 @@ async function main() { const wallet = pk.connect(provider); console.log(`Address: ${await wallet.getAddress()}`); - const host = "http://localhost:8080"; + const host = process.env.CLOB_API_URL || "http://localhost:8080"; const creds: ApiKeyCreds = { key: `${process.env.CLOB_API_KEY}`, secret: `${process.env.CLOB_SECRET}`, diff --git a/examples/getOrderHistory.ts b/examples/getOrderHistory.ts index 772c4f5..66cef08 100644 --- a/examples/getOrderHistory.ts +++ b/examples/getOrderHistory.ts @@ -11,7 +11,7 @@ async function main() { const wallet = pk.connect(provider); console.log(`Address: ${await wallet.getAddress()}`); - const host = "http://localhost:8080"; + const host = process.env.CLOB_API_URL || "http://localhost:8080"; const creds: ApiKeyCreds = { key: `${process.env.CLOB_API_KEY}`, secret: `${process.env.CLOB_SECRET}`, diff --git a/examples/getOrderbook.ts b/examples/getOrderbook.ts index af64093..4dbdd00 100644 --- a/examples/getOrderbook.ts +++ b/examples/getOrderbook.ts @@ -5,7 +5,7 @@ import { ClobClient } from "../src"; dotenvConfig({ path: resolve(__dirname, "../.env") }); async function main() { - const host = "http://localhost:8080"; + const host = process.env.CLOB_API_URL || "http://localhost:8080"; const clobClient = new ClobClient(host); const resp = await clobClient.getOrderBook("16678291189211314787145083999015737376658799626183230671758641503291735614088") console.log(resp); diff --git a/examples/getPrice.ts b/examples/getPrice.ts new file mode 100644 index 0000000..4813351 --- /dev/null +++ b/examples/getPrice.ts @@ -0,0 +1,46 @@ +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 = 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 YES_TOKEN_ID="16678291189211314787145083999015737376658799626183230671758641503291735614088" + const NO_TOKEN_ID="1343197538147866997676250008839231694243646439454152539053893078719042421992" + + clobClient.getPrice( + YES_TOKEN_ID, + "buy" + ).then((price:any)=> console.log('YES', 'BUY', price)); + + clobClient.getPrice( + YES_TOKEN_ID, + "sell" + ).then((price:any)=> console.log('YES', 'SELL', price)); + + clobClient.getPrice( + NO_TOKEN_ID, + "buy" + ).then((price:any)=> console.log('NO', 'BUY', price)); + + clobClient.getPrice( + NO_TOKEN_ID, + "sell" + ).then((price:any)=> console.log('NO', 'SELL', price)); +} + +main(); diff --git a/examples/getServerTime.ts b/examples/getServerTime.ts index 031512e..58e5ccc 100644 --- a/examples/getServerTime.ts +++ b/examples/getServerTime.ts @@ -5,7 +5,7 @@ import { ClobClient } from "../src"; dotenvConfig({ path: resolve(__dirname, "../.env") }); async function main() { - const host = "http://localhost:8080"; + const host = process.env.CLOB_API_URL || "http://localhost:8080"; const clobClient = new ClobClient(host); console.log(`Server time: ${await clobClient.getServerTime()}`); diff --git a/examples/getTradeHistory.ts b/examples/getTradeHistory.ts index a2c786e..08338a1 100644 --- a/examples/getTradeHistory.ts +++ b/examples/getTradeHistory.ts @@ -11,7 +11,7 @@ async function main() { const wallet = pk.connect(provider); console.log(`Address: ${await wallet.getAddress()}`); - const host = "http://localhost:8080"; + const host = process.env.CLOB_API_URL || "http://localhost:8080"; const creds: ApiKeyCreds = { key: `${process.env.CLOB_API_KEY}`, secret: `${process.env.CLOB_SECRET}`, diff --git a/examples/limitOrder.ts b/examples/limitOrder.ts index 3b16dea..34d99f4 100644 --- a/examples/limitOrder.ts +++ b/examples/limitOrder.ts @@ -11,7 +11,7 @@ async function main() { const wallet = pk.connect(provider); console.log(`Address: ${await wallet.getAddress()}`); - const host = "http://localhost:8080"; + const host = process.env.CLOB_API_URL || "http://localhost:8080"; const creds: ApiKeyCreds = { key: `${process.env.CLOB_API_KEY}`, secret: `${process.env.CLOB_SECRET}`, diff --git a/examples/marketBuy.ts b/examples/marketBuy.ts index f469b40..2685244 100644 --- a/examples/marketBuy.ts +++ b/examples/marketBuy.ts @@ -11,7 +11,7 @@ async function main() { const wallet = pk.connect(provider); console.log(`Address: ${await wallet.getAddress()}`); - const host = "http://localhost:8080"; + const host = process.env.CLOB_API_URL || "http://localhost:8080"; const creds: ApiKeyCreds = { key: `${process.env.CLOB_API_KEY}`, secret: `${process.env.CLOB_SECRET}`, diff --git a/examples/marketOrder.ts b/examples/marketOrder.ts index 7ffe737..fc618f2 100644 --- a/examples/marketOrder.ts +++ b/examples/marketOrder.ts @@ -11,7 +11,7 @@ async function main() { const wallet = pk.connect(provider); console.log(`Address: ${await wallet.getAddress()}`); - const host = "http://localhost:8080"; + const host = process.env.CLOB_API_URL || "http://localhost:8080"; const creds: ApiKeyCreds = { key: `${process.env.CLOB_API_KEY}`, secret: `${process.env.CLOB_SECRET}`, diff --git a/examples/marketOrderWorstPrice.ts b/examples/marketOrderWorstPrice.ts index 64fc02c..ab81d75 100644 --- a/examples/marketOrderWorstPrice.ts +++ b/examples/marketOrderWorstPrice.ts @@ -11,7 +11,7 @@ async function main() { const wallet = pk.connect(provider); console.log(`Address: ${await wallet.getAddress()}`); - const host = "http://localhost:8080"; + const host = process.env.CLOB_API_URL || "http://localhost:8080"; const creds: ApiKeyCreds = { key: `${process.env.CLOB_API_KEY}`, secret: `${process.env.CLOB_SECRET}`, diff --git a/examples/populateOrderbook.ts b/examples/populateOrderbook.ts index 09a1dad..76afe27 100644 --- a/examples/populateOrderbook.ts +++ b/examples/populateOrderbook.ts @@ -11,7 +11,7 @@ async function main() { const wallet = pk.connect(provider); console.log(`Address: ${await wallet.getAddress()}`); - const host = "http://localhost:8080"; + const host = process.env.CLOB_API_URL || "http://localhost:8080"; const creds: ApiKeyCreds = { key: `${process.env.CLOB_API_KEY}`, secret: `${process.env.CLOB_SECRET}`, diff --git a/examples/testMultiSigMarketBuy.ts b/examples/testMultiSigMarketBuy.ts index d018d7d..97da4e0 100644 --- a/examples/testMultiSigMarketBuy.ts +++ b/examples/testMultiSigMarketBuy.ts @@ -39,7 +39,7 @@ async function main() { const wallet = pk.connect(provider); console.log(`Address: ${await wallet.getAddress()}`); - const host = "http://localhost:8080"; + const host = process.env.CLOB_API_URL || "http://localhost:8080"; const creds: ApiKeyCreds = { key: `${process.env.CLOB_API_KEY}`, secret: `${process.env.CLOB_SECRET}`, diff --git a/examples/testPolyProxyMarketBuy.ts b/examples/testPolyProxyMarketBuy.ts index 28883f1..7f12f97 100644 --- a/examples/testPolyProxyMarketBuy.ts +++ b/examples/testPolyProxyMarketBuy.ts @@ -39,7 +39,7 @@ async function main() { const wallet = pk.connect(provider); console.log(`Address: ${await wallet.getAddress()}`); - const host = "http://localhost:8080"; + const host = process.env.CLOB_API_URL || "http://localhost:8080"; const creds: ApiKeyCreds = { key: `${process.env.CLOB_API_KEY}`, secret: `${process.env.CLOB_SECRET}`, diff --git a/examples/testPolyProxyMarketSell.ts b/examples/testPolyProxyMarketSell.ts index 066719f..18a2702 100644 --- a/examples/testPolyProxyMarketSell.ts +++ b/examples/testPolyProxyMarketSell.ts @@ -39,7 +39,7 @@ async function main() { const wallet = pk.connect(provider); console.log(`Address: ${await wallet.getAddress()}`); - const host = "http://localhost:8080"; + const host = process.env.CLOB_API_URL || "http://localhost:8080"; const creds: ApiKeyCreds = { key: `${process.env.CLOB_API_KEY}`, secret: `${process.env.CLOB_SECRET}`,