Skip to content

Commit

Permalink
Merge pull request #48 from Polymarket/feat/protocol-contracts
Browse files Browse the repository at this point in the history
Feat/adapt client to new contracts
  • Loading branch information
poly-rodr authored Sep 23, 2022
2 parents a4386a6 + 6089a23 commit 84baac4
Show file tree
Hide file tree
Showing 59 changed files with 3,150 additions and 3,621 deletions.
3 changes: 0 additions & 3 deletions .commitlintrc.js

This file was deleted.

2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
RPC_URL=http://polygon-mumbai.infura.io/v3/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
PK=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
CHAIN_ID=
CLOB_API_KEY=
CLOB_SECRET=
CLOB_PASS_PHRASE=
Expand Down
49 changes: 27 additions & 22 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
module.exports = {
parserOptions: {
"project": "./tsconfig.json",
"tsconfigRootDir": __dirname,
"sourceType": "module"
},
rules: {
"no-console": "off",
"no-case-declarations": "off",
"no-await-in-loop": "off",
"import/prefer-default-export": "off",
"no-underscore-dangle" : "off",
"import/no-extraneous-dependencies": "off",
"@typescript-eslint/no-explicit-any": "off",
"no-restricted-syntax": "off"
},
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
],
extends: "airbnb-base-typescript-prettier"
};
root: true,
plugins: ["@typescript-eslint", "unused-imports"],
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"prettier",
],
parserOptions: {
"project": "./tsconfig.json",
"tsconfigRootDir": __dirname,
"sourceType": "module"
},
rules: {
"@typescript-eslint/member-ordering": "error",
"lines-between-class-members": "off",
"padding-line-between-statements": "error",
"no-unused-vars": "off",
"max-len": ["error", { code: 100 }],
"max-depth": ["error", 3],
"max-lines-per-function": "off",
"max-params": ["error", 6],
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": "off",
"unused-imports/no-unused-imports": "error",
"unused-imports/no-unused-vars": 0,
},
};

25 changes: 25 additions & 0 deletions .github/workflows/workflow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Build, Lint, and Test

on:
push:
branches: [ main ]
pull_request:

jobs:
build-lint-test:
name: Build, Lint, and Test
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- run: yarn --frozen-lockfile
- run: yarn lint
- run: yarn build
- run: yarn test

all-jobs-pass:
name: All jobs pass
runs-on: ubuntu-20.04
needs:
- build-lint-test
steps:
- run: echo "success"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dist/
node_modules/
typechain/
scripts/
.nyc_output/

# files
.env
Expand All @@ -22,4 +23,4 @@ yarn-debug.log*
yarn-error.log*
.DS_Store
packages/tracker/main
package-lock.json
package-lock.json
5 changes: 0 additions & 5 deletions .huskyrc

This file was deleted.

12 changes: 12 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.editorconfig
.eslintignore
.eslintrc.js
.github/workflows/workflow.yaml
.prettierignore
.prettierrc.json
Makefile
tsconfig.json

src
tests
examples
2 changes: 1 addition & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"arrowParens": "avoid",
"bracketSpacing": true,
"endOfLine": "auto",
"printWidth": 120,
"printWidth": 100,
"singleQuote": false,
"tabWidth": 4,
"trailingComma": "all"
Expand Down
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.PHONY: build
build:
@echo "Building ts code..."
rm -rf dist
tsc --module commonjs

.PHONY: test
test:
yarn nyc -a \
--reporter=html \
--reporter=text mocha './tests' \
--require esm \
--require jsdom-global/register \
--require ts-node/register 'tests/**/*.test.ts' \
--require tsconfig-paths/register \
--timeout 10000 \
--exit

.PHONY: lint
lint:
@echo "Linting code..."
./node_modules/.bin/eslint ./src --ext .js,.ts
17 changes: 5 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,21 @@ const creds: ApiKeyCreds = {
}

// Initialize the clob client
// NOTE: the signer must be approved on the LimitOrderProtocol and OrderExecutor contracts
// NOTE: the signer must be approved on the CTFExchange contract
const clobClient = new ClobClient(host, signer, creds);

// Create a limit buy order for 100 YES for 0.50c
const order = await clobClient.createLimitOrder({
// Create a buy order for 100 YES for 0.50c
const order = await clobClient.createOrder({
tokenId: "16678291189211314787145083999015737376658799626183230671758641503291735614088",
price: 0.5,
side: Side.Buy,
size: 100,
feeRateBps: "100",
nonce: 1,
});

// Send it to the server
const resp = await clobClient.postOrder(order);

// Init a market sell of 100 YES tokens
const marketSell = await clobClient.createMarketOrder({
tokenId: "16678291189211314787145083999015737376658799626183230671758641503291735614088",
side: Side.Sell,
size: 100,
});

await clobClient.postOrder(marketSell);
```

See [examples](examples/) for more information
11 changes: 5 additions & 6 deletions examples/cancelAll.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { ApiKeyCreds, ClobClient } from "../src";
import { ApiKeyCreds, Chain, 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 wallet = new ethers.Wallet(`${process.env.PK}`);
const chainId = parseInt(`${process.env.CHAIN_ID || Chain.MUMBAI}`) 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, wallet, creds);
const clobClient = new ClobClient(host, chainId, wallet, creds);

// Send it to the server
const resp = await clobClient.cancelAll();
Expand Down
11 changes: 5 additions & 6 deletions examples/cancelOrder.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { ApiKeyCreds, ClobClient } from "../src";
import { ApiKeyCreds, Chain, 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 wallet = new ethers.Wallet(`${process.env.PK}`);
const chainId = parseInt(`${process.env.CHAIN_ID || Chain.MUMBAI}`) 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, wallet, creds);
const clobClient = new ClobClient(host, chainId, wallet, creds);

// Send it to the server
const resp = await clobClient.cancelOrder({
Expand Down
11 changes: 5 additions & 6 deletions examples/createApiKey.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { ClobClient } from "../src";
import { Chain, 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 wallet = new ethers.Wallet(`${process.env.PK}`);
const chainId = parseInt(`${process.env.CHAIN_ID || Chain.MUMBAI}`) as Chain;
console.log(`Address: ${await wallet.getAddress()}, chainId: ${chainId}`);

const host = process.env.CLOB_API_URL || "http://localhost:8080";
const clobClient = new ClobClient(host, wallet);
const clobClient = new ClobClient(host, chainId, wallet);

console.log(`Response: `);
const resp = await clobClient.createApiKey();
Expand Down
11 changes: 5 additions & 6 deletions examples/deriveApiKey.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { ClobClient } from "../src";
import { Chain, 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 wallet = new ethers.Wallet(`${process.env.PK}`);
const chainId = parseInt(`${process.env.CHAIN_ID || Chain.MUMBAI}`) as Chain;
console.log(`Address: ${await wallet.getAddress()}, chainId: ${chainId}`);

const host = process.env.CLOB_API_URL || "http://localhost:8080";
const clobClient = new ClobClient(host, wallet);
const clobClient = new ClobClient(host, chainId, wallet);

console.log(`Response: `);
const resp = await clobClient.deriveApiKey();
Expand Down
11 changes: 5 additions & 6 deletions examples/getApiKeys.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { ApiKeyCreds, ClobClient } from "../src";
import { ApiKeyCreds, Chain, 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 wallet = new ethers.Wallet(`${process.env.PK}`);
const chainId = parseInt(`${process.env.CHAIN_ID || Chain.MUMBAI}`) 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, wallet, creds);
const clobClient = new ClobClient(host, chainId, wallet, creds);

console.log(`Response: `);
const resp = await clobClient.getApiKeys();
Expand Down
11 changes: 5 additions & 6 deletions examples/getLargeOrders.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { ApiKeyCreds, ClobClient } from "../src";
import { ApiKeyCreds, Chain, 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 wallet = new ethers.Wallet(`${process.env.PK}`);
const chainId = parseInt(`${process.env.CHAIN_ID || Chain.MUMBAI}`) 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, wallet, creds);
const clobClient = new ClobClient(host, chainId, wallet, creds);

const resp = await clobClient.getLargeOrders();
console.log(resp);
Expand Down
15 changes: 8 additions & 7 deletions examples/getLastTradePrice.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { ApiKeyCreds, ClobClient } from "../src";
import { Chain, 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 wallet = new ethers.Wallet(`${process.env.PK}`);
const chainId = parseInt(`${process.env.CHAIN_ID || Chain.MUMBAI}`) as Chain;
console.log(`Address: ${await wallet.getAddress()}, chainId: ${chainId}`);

const host = process.env.CLOB_API_URL || "http://localhost:8080";
const clobClient = new ClobClient(host, wallet);
const clobClient = new ClobClient(host, chainId, wallet);

console.log(`Price: `);
const resp = await clobClient.getLastTradePrice("16678291189211314787145083999015737376658799626183230671758641503291735614088");
const resp = await clobClient.getLastTradePrice(
"16678291189211314787145083999015737376658799626183230671758641503291735614088",
);
console.log(resp);
}

Expand Down
Loading

0 comments on commit 84baac4

Please sign in to comment.