@polymarket/relayer-client 0.0.5 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,12 +1,15 @@
1
- import { AxiosRequestHeaders } from "axios";
1
+ import { HttpClient } from "../http-helpers";
2
2
  import { AuthArgs } from "../types";
3
3
  export declare class AuthHandler {
4
4
  readonly url: string;
5
+ readonly httpClient: HttpClient;
5
6
  readonly token?: string;
6
7
  private cookie?;
7
- constructor(args: AuthArgs);
8
+ constructor(httpClient: HttpClient, args: AuthArgs);
9
+ /**
10
+ * Logs in to the auth url
11
+ * Extracts polymarket session cookies from the auth response header and cache it locally.
12
+ */
8
13
  login(): Promise<void>;
9
- isLoggedIn(): boolean;
10
- getAuthHeader(): Promise<AxiosRequestHeaders>;
11
14
  getPolymarketCookies(): Promise<string>;
12
15
  }
@@ -5,49 +5,40 @@ const tslib_1 = require("tslib");
5
5
  const http_helpers_1 = require("../http-helpers");
6
6
  const POLYMARKET_COOKIE_NAME = "polymarket";
7
7
  class AuthHandler {
8
- constructor(args) {
9
- if (args.token == undefined && args.cookie == undefined) {
8
+ constructor(httpClient, args) {
9
+ if (args.authUrl == undefined || args.authToken == undefined) {
10
10
  throw new Error("invalid authorization arguments");
11
11
  }
12
12
  this.url = args.authUrl.endsWith("/") ? args.authUrl.slice(0, -1) : args.authUrl;
13
- ;
14
- if (args.token !== undefined) {
15
- this.token = args.token;
16
- }
17
- if (args.cookie !== undefined) {
18
- this.cookie = args.cookie;
19
- }
13
+ this.token = args.authToken;
14
+ this.httpClient = httpClient;
20
15
  }
16
+ /**
17
+ * Logs in to the auth url
18
+ * Extracts polymarket session cookies from the auth response header and cache it locally.
19
+ */
21
20
  login() {
22
21
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
23
- if (this.token == undefined) {
24
- throw new Error("authorization token missing");
25
- }
26
- const resp = yield http_helpers_1.request(`${this.url}/login`, http_helpers_1.GET, { Authorization: `Bearer ${this.token}` });
27
- const cookies = resp.headers['set-cookie'];
28
- let aggregatedCookie = "";
29
- for (const cookie of cookies) {
30
- if (cookie.includes(POLYMARKET_COOKIE_NAME)) {
31
- aggregatedCookie += `${cookie}; `;
22
+ if (this.cookie == undefined) {
23
+ // Logging in will set the session cookies on the httpClient
24
+ const resp = yield this.httpClient.send(`${this.url}/login`, http_helpers_1.GET, { Authorization: `Bearer ${this.token}` });
25
+ const cookies = resp.headers['set-cookie'];
26
+ let aggregatedCookie = "";
27
+ for (const cookie of cookies) {
28
+ if (cookie.includes(POLYMARKET_COOKIE_NAME)) {
29
+ aggregatedCookie += `${cookie}; `;
30
+ }
31
+ }
32
+ if (aggregatedCookie != "") {
33
+ this.cookie = aggregatedCookie;
32
34
  }
33
- }
34
- if (aggregatedCookie != "") {
35
- this.cookie = aggregatedCookie;
36
35
  console.log(`Successfully logged in!`);
37
36
  }
38
37
  });
39
38
  }
40
- isLoggedIn() {
41
- return this.cookie != undefined;
42
- }
43
- getAuthHeader() {
44
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
45
- return { "Cookie": yield this.getPolymarketCookies() };
46
- });
47
- }
48
39
  getPolymarketCookies() {
49
40
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
50
- if (!this.isLoggedIn()) {
41
+ if (this.cookie == undefined) {
51
42
  console.log("Authorization not set, logging in...");
52
43
  yield this.login();
53
44
  }
@@ -1,2 +1,3 @@
1
1
  export * from "./proxy";
2
2
  export * from "./safe";
3
+ export * from "./create";
@@ -3,3 +3,4 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const tslib_1 = require("tslib");
4
4
  tslib_1.__exportStar(require("./proxy"), exports);
5
5
  tslib_1.__exportStar(require("./safe"), exports);
6
+ tslib_1.__exportStar(require("./create"), exports);
@@ -1,11 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.deriveSafe = exports.deriveProxyWallet = void 0;
4
- const sdk_1 = require("@polymarket/sdk");
5
4
  const ethers_1 = require("ethers");
6
5
  const constants_1 = require("../constants");
7
6
  exports.deriveProxyWallet = (address) => {
8
- return sdk_1.getProxyWalletAddress(constants_1.PROXY_WALLET_FACTORY_ADDRESS, address);
7
+ return ethers_1.ethers.utils.getCreate2Address(constants_1.PROXY_WALLET_FACTORY_ADDRESS, ethers_1.ethers.utils.solidityKeccak256(["address"], [address]), constants_1.PROXY_INIT_CODE_HASH);
9
8
  };
10
9
  exports.deriveSafe = (address) => {
11
10
  return ethers_1.ethers.utils.getCreate2Address(constants_1.SAFE_FACTORY_ADDRESS, ethers_1.ethers.utils.keccak256(ethers_1.ethers.utils.defaultAbiCoder.encode(["address"], [address])), constants_1.SAFE_INIT_CODE_HASH);
package/dist/client.d.ts CHANGED
@@ -1,17 +1,19 @@
1
1
  import { Wallet } from "@ethersproject/wallet";
2
2
  import { JsonRpcSigner } from "@ethersproject/providers";
3
+ import { HttpClient } from "./http-helpers";
3
4
  import { AddressPayload, AuthArgs, NoncePayload, ProxyTransaction, RelayPayload, SafeTransaction } from "./types";
4
5
  export declare class RelayClient {
5
6
  readonly relayerUrl: string;
6
7
  readonly chainId: number;
8
+ readonly httpClient: HttpClient;
9
+ private authHandler;
7
10
  readonly signer?: Wallet | JsonRpcSigner;
8
- private authHandler?;
9
- constructor(relayerUrl: string, chainId: number, signer?: Wallet | JsonRpcSigner, authArgs?: AuthArgs);
10
- getOk(): Promise<any>;
11
+ constructor(relayerUrl: string, chainId: number, authArgs: AuthArgs, signer?: Wallet | JsonRpcSigner);
11
12
  getRelayAddress(): Promise<AddressPayload>;
12
13
  getNonce(signerAddress: string, signerType: string): Promise<NoncePayload>;
13
14
  getRelayPayload(signerAddress: string, signerType: string): Promise<RelayPayload>;
14
15
  getTransaction(transactionId: string): Promise<RelayPayload>;
16
+ getTransactions(): Promise<RelayPayload>;
15
17
  executeProxyTransactions(txns: ProxyTransaction[]): Promise<any>;
16
18
  executeSafeTransactions(txns: SafeTransaction[]): Promise<any>;
17
19
  deploySafe(): Promise<any>;
package/dist/client.js CHANGED
@@ -3,31 +3,26 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RelayClient = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const ethers_1 = require("ethers");
6
+ const browser_or_node_1 = require("browser-or-node");
6
7
  const http_helpers_1 = require("./http-helpers");
7
8
  const types_1 = require("./types");
8
9
  const endpoints_1 = require("./endpoints");
9
10
  const builder_1 = require("./builder");
10
11
  const encode_1 = require("./encode");
11
- const create_1 = require("./builder/create");
12
+ const builder_2 = require("./builder");
12
13
  const auth_1 = require("./auth");
13
14
  class RelayClient {
14
- constructor(relayerUrl, chainId, signer, authArgs) {
15
+ constructor(relayerUrl, chainId, authArgs, signer) {
15
16
  this.relayerUrl = relayerUrl.endsWith("/") ? relayerUrl.slice(0, -1) : relayerUrl;
16
17
  this.chainId = chainId;
18
+ this.httpClient = new http_helpers_1.HttpClient();
19
+ this.authHandler = new auth_1.AuthHandler(this.httpClient, authArgs);
17
20
  if (signer !== undefined) {
18
21
  this.signer = signer;
19
22
  if (signer.provider == undefined) {
20
23
  throw new Error("signer must have provider attached");
21
24
  }
22
25
  }
23
- if (authArgs !== undefined) {
24
- this.authHandler = new auth_1.AuthHandler(authArgs);
25
- }
26
- }
27
- getOk() {
28
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
29
- return this.send(`${this.relayerUrl}/`, http_helpers_1.GET);
30
- });
31
26
  }
32
27
  getRelayAddress() {
33
28
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
@@ -49,6 +44,11 @@ class RelayClient {
49
44
  return this.send(`${this.relayerUrl}${endpoints_1.GET_TRANSACTION}?id=${transactionId}`, http_helpers_1.GET);
50
45
  });
51
46
  }
47
+ getTransactions() {
48
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
49
+ return this.send(`${this.relayerUrl}${endpoints_1.GET_TRANSACTION}s`, http_helpers_1.GET);
50
+ });
51
+ }
52
52
  executeProxyTransactions(txns) {
53
53
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
54
54
  if (this.signer == undefined) {
@@ -104,7 +104,7 @@ class RelayClient {
104
104
  payment: "0",
105
105
  paymentReceiver: ethers_1.ethers.constants.AddressZero,
106
106
  };
107
- const request = yield create_1.buildSafeCreateTransactionRequest(this.signer, args);
107
+ const request = yield builder_2.buildSafeCreateTransactionRequest(this.signer, args);
108
108
  console.log(`Client side request creation took: ${(Date.now() - start) / 1000} seconds`);
109
109
  return this.postTransactionRequest(request);
110
110
  });
@@ -116,9 +116,10 @@ class RelayClient {
116
116
  }
117
117
  send(endpoint, method, headers, data, params) {
118
118
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
119
- // If client is instantiated locally/not on the browser, inject authorization cookies
119
+ yield this.authHandler.login();
120
+ // If client is instantiated locally/not on the browser, explicitly inject the polymarket session cookies
120
121
  // this is to ensure examples still run locally
121
- if (this.authHandler != undefined) {
122
+ if (!browser_or_node_1.isBrowser) {
122
123
  const cookie = yield this.authHandler.getPolymarketCookies();
123
124
  if (headers != undefined) {
124
125
  headers["Cookie"] = cookie;
@@ -126,11 +127,9 @@ class RelayClient {
126
127
  else {
127
128
  headers = { "Cookie": cookie };
128
129
  }
129
- const resp = yield http_helpers_1.request(endpoint, method, headers, data, params);
130
- return resp.data;
131
130
  }
132
131
  // If the client is instantiated on the browser, do not inject cookies
133
- const resp = yield http_helpers_1.request(endpoint, method, headers, data, params);
132
+ const resp = yield this.httpClient.send(endpoint, method, headers, data, params);
134
133
  return resp.data;
135
134
  });
136
135
  }
@@ -2,5 +2,6 @@ export declare const RELAY_HUB_ADDRESS = "0xD216153c06E857cD7f72665E0aF1d7D82172
2
2
  export declare const PROXY_WALLET_FACTORY_ADDRESS = "0xaB45c5A4B0c941a2F231C04C3f49182e1A254052";
3
3
  export declare const SAFE_FACTORY_ADDRESS = "0xaacFeEa03eb1561C4e67d661e40682Bd20E3541b";
4
4
  export declare const SAFE_MULTISEND_ADDRESS = "0xA238CBeb142c10Ef7Ad8442C6D1f9E89e07e7761";
5
+ export declare const PROXY_INIT_CODE_HASH = "0xd21df8dc65880a8606f09fe0ce3df9b8869287ab0b058be05aa9e8af6330a00b";
5
6
  export declare const SAFE_INIT_CODE_HASH = "0x2bce2127ff07fb632d16c8347c4ebf501f4841168bed00d9e6ef715ddb6fcecf";
6
7
  export declare const SAFE_FACTORY_NAME = "Polymarket Contract Proxy Factory";
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SAFE_FACTORY_NAME = exports.SAFE_INIT_CODE_HASH = exports.SAFE_MULTISEND_ADDRESS = exports.SAFE_FACTORY_ADDRESS = exports.PROXY_WALLET_FACTORY_ADDRESS = exports.RELAY_HUB_ADDRESS = void 0;
3
+ exports.SAFE_FACTORY_NAME = exports.SAFE_INIT_CODE_HASH = exports.PROXY_INIT_CODE_HASH = exports.SAFE_MULTISEND_ADDRESS = exports.SAFE_FACTORY_ADDRESS = exports.PROXY_WALLET_FACTORY_ADDRESS = exports.RELAY_HUB_ADDRESS = void 0;
4
4
  exports.RELAY_HUB_ADDRESS = "0xD216153c06E857cD7f72665E0aF1d7D82172F494";
5
5
  exports.PROXY_WALLET_FACTORY_ADDRESS = "0xaB45c5A4B0c941a2F231C04C3f49182e1A254052";
6
6
  exports.SAFE_FACTORY_ADDRESS = "0xaacFeEa03eb1561C4e67d661e40682Bd20E3541b";
7
7
  exports.SAFE_MULTISEND_ADDRESS = "0xA238CBeb142c10Ef7Ad8442C6D1f9E89e07e7761";
8
+ exports.PROXY_INIT_CODE_HASH = "0xd21df8dc65880a8606f09fe0ce3df9b8869287ab0b058be05aa9e8af6330a00b";
8
9
  exports.SAFE_INIT_CODE_HASH = "0x2bce2127ff07fb632d16c8347c4ebf501f4841168bed00d9e6ef715ddb6fcecf";
9
10
  exports.SAFE_FACTORY_NAME = "Polymarket Contract Proxy Factory";
@@ -1,16 +1,10 @@
1
- import { AxiosRequestHeaders, Method } from "axios";
1
+ import { AxiosInstance, Method } from "axios";
2
2
  export declare const GET = "GET";
3
3
  export declare const POST = "POST";
4
4
  export declare const DELETE = "DELETE";
5
5
  export declare const PUT = "PUT";
6
- export declare const request: (endpoint: string, method: Method, headers?: any, data?: any, params?: any) => Promise<any>;
7
- export declare type QueryParams = Record<string, any>;
8
- export interface RequestOptions {
9
- headers?: AxiosRequestHeaders;
10
- data?: any;
11
- params?: QueryParams;
6
+ export declare class HttpClient {
7
+ readonly instance: AxiosInstance;
8
+ constructor();
9
+ send(endpoint: string, method: Method, headers?: any, data?: any, params?: any): Promise<any>;
12
10
  }
13
- export declare const post: (endpoint: string, options?: RequestOptions | undefined) => Promise<any>;
14
- export declare const raw_get: (endpoint: string, options?: RequestOptions | undefined) => Promise<any>;
15
- export declare const get: (endpoint: string, options?: RequestOptions | undefined) => Promise<any>;
16
- export declare const del: (endpoint: string, options?: RequestOptions | undefined) => Promise<any>;
@@ -1,69 +1,53 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.del = exports.get = exports.raw_get = exports.post = exports.request = exports.PUT = exports.DELETE = exports.POST = exports.GET = void 0;
3
+ exports.HttpClient = exports.PUT = exports.DELETE = exports.POST = exports.GET = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const axios_1 = tslib_1.__importDefault(require("axios"));
6
6
  exports.GET = "GET";
7
7
  exports.POST = "POST";
8
8
  exports.DELETE = "DELETE";
9
9
  exports.PUT = "PUT";
10
- axios_1.default.defaults.withCredentials = true;
11
- exports.request = (endpoint, method, headers, data, params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
12
- var _a, _b, _c;
13
- try {
14
- const resp = yield axios_1.default({
15
- withCredentials: true,
16
- method,
17
- url: endpoint,
18
- headers,
19
- data,
20
- params
21
- });
22
- return resp;
10
+ class HttpClient {
11
+ constructor() {
12
+ this.instance = axios_1.default.create({ withCredentials: true });
23
13
  }
24
- catch (err) {
25
- if (axios_1.default.isAxiosError(err)) {
26
- if (err.response) {
27
- const errPayload = {
28
- error: "request error",
29
- status: (_a = err.response) === null || _a === void 0 ? void 0 : _a.status,
30
- statusText: (_b = err.response) === null || _b === void 0 ? void 0 : _b.statusText,
31
- data: (_c = err.response) === null || _c === void 0 ? void 0 : _c.data,
32
- };
33
- console.error("request error", errPayload);
34
- throw new Error(JSON.stringify(errPayload));
14
+ send(endpoint, method, headers, data, params) {
15
+ var _a, _b, _c;
16
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
17
+ if (headers != undefined) {
18
+ headers["Access-Control-Allow-Credentials"] = true;
35
19
  }
36
- else {
37
- const errPayload = { error: "connection error" };
38
- console.error("connection error", errPayload);
39
- throw new Error(JSON.stringify(errPayload));
20
+ try {
21
+ const resp = yield this.instance.request({
22
+ method,
23
+ url: endpoint,
24
+ headers,
25
+ data,
26
+ params
27
+ });
28
+ return resp;
40
29
  }
41
- }
42
- throw new Error(JSON.stringify({ error: err }));
43
- }
44
- });
45
- exports.post = (endpoint, options) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
46
- const resp = yield exports.request(endpoint, exports.POST, options === null || options === void 0 ? void 0 : options.headers, options === null || options === void 0 ? void 0 : options.data, options === null || options === void 0 ? void 0 : options.params);
47
- if ("error" in resp) {
48
- throw new Error(resp);
49
- }
50
- return resp.data;
51
- });
52
- exports.raw_get = (endpoint, options) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
53
- const resp = yield exports.request(endpoint, exports.GET, options === null || options === void 0 ? void 0 : options.headers, options === null || options === void 0 ? void 0 : options.data, options === null || options === void 0 ? void 0 : options.params);
54
- if ("error" in resp) {
55
- throw new Error(resp);
56
- }
57
- return resp;
58
- });
59
- exports.get = (endpoint, options) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
60
- const resp = yield exports.raw_get(endpoint, options);
61
- return resp.data;
62
- });
63
- exports.del = (endpoint, options) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
64
- const resp = yield exports.request(endpoint, exports.DELETE, options === null || options === void 0 ? void 0 : options.headers, options === null || options === void 0 ? void 0 : options.data, options === null || options === void 0 ? void 0 : options.params);
65
- if ("error" in resp) {
66
- throw new Error(resp);
30
+ catch (err) {
31
+ if (axios_1.default.isAxiosError(err)) {
32
+ if (err.response) {
33
+ const errPayload = {
34
+ error: "request error",
35
+ status: (_a = err.response) === null || _a === void 0 ? void 0 : _a.status,
36
+ statusText: (_b = err.response) === null || _b === void 0 ? void 0 : _b.statusText,
37
+ data: (_c = err.response) === null || _c === void 0 ? void 0 : _c.data,
38
+ };
39
+ console.error("request error", errPayload);
40
+ throw new Error(JSON.stringify(errPayload));
41
+ }
42
+ else {
43
+ const errPayload = { error: "connection error" };
44
+ console.error("connection error", errPayload);
45
+ throw new Error(JSON.stringify(errPayload));
46
+ }
47
+ }
48
+ throw new Error(JSON.stringify({ error: err }));
49
+ }
50
+ });
67
51
  }
68
- return resp.data;
69
- });
52
+ }
53
+ exports.HttpClient = HttpClient;
package/dist/types.d.ts CHANGED
@@ -87,6 +87,5 @@ export interface RelayerSubRequest {
87
87
  }
88
88
  export interface AuthArgs {
89
89
  authUrl: string;
90
- token?: string;
91
- cookie?: string;
90
+ authToken: string;
92
91
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@polymarket/relayer-client",
3
3
  "description": "Client for Polymarket relayers",
4
- "version": "0.0.5",
4
+ "version": "0.0.7",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "files": [
@@ -12,8 +12,8 @@
12
12
  "clean": "rm -rf ./dist"
13
13
  },
14
14
  "dependencies": {
15
- "@polymarket/sdk": "^5.0.3",
16
15
  "axios": "^0.27.2",
16
+ "browser-or-node": "^3.0.0",
17
17
  "ethers": "^5.7.1"
18
18
  },
19
19
  "devDependencies": {
@@ -24,6 +24,7 @@
24
24
  "@typescript-eslint/eslint-plugin": "^5.37.0",
25
25
  "@typescript-eslint/parser": "^5.37.0",
26
26
  "chai": "^4.3.6",
27
+ "dotenv": "^16.0.2",
27
28
  "eslint": "^8.23.1",
28
29
  "eslint-config-prettier": "^8.5.0",
29
30
  "eslint-config-standard-with-typescript": "^23.0.0",
@@ -33,10 +34,9 @@
33
34
  "eslint-plugin-promise": "^6.0.1",
34
35
  "eslint-plugin-unused-imports": "^2.0.0",
35
36
  "esm": "^3.2.25",
36
- "dotenv": "^16.0.2",
37
- "path": "^0.12.7",
38
37
  "mocha": "^10.0.0",
39
38
  "nyc": "^15.1.0",
39
+ "path": "^0.12.7",
40
40
  "prettier": "^2.7.1",
41
41
  "ts-mocha": "^10.0.0",
42
42
  "ts-node": "^9.1.1",