@polymarket/relayer-client 0.0.2 → 0.0.3

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.
@@ -23,11 +23,7 @@ class AuthHandler {
23
23
  if (this.token == undefined) {
24
24
  throw new Error("authorization token missing");
25
25
  }
26
- const resp = yield http_helpers_1.raw_get(`${this.url}/login`, {
27
- headers: {
28
- Authorization: `Bearer ${this.token}`
29
- },
30
- });
26
+ const resp = yield http_helpers_1.request(`${this.url}/login`, http_helpers_1.GET, { Authorization: `Bearer ${this.token}` });
31
27
  const cookies = resp.headers['set-cookie'];
32
28
  let aggregatedCookie = "";
33
29
  for (const cookie of cookies) {
package/dist/client.d.ts CHANGED
@@ -6,8 +6,8 @@ export declare class RelayClient {
6
6
  readonly chainId: number;
7
7
  readonly signer?: Wallet | JsonRpcSigner;
8
8
  readonly wsRelayerUrl?: string;
9
- private authHandler;
10
- constructor(relayerUrl: string, chainId: number, authArgs: AuthArgs, signer?: Wallet | JsonRpcSigner, wsRelayerUrl?: string);
9
+ private authHandler?;
10
+ constructor(relayerUrl: string, chainId: number, authArgs?: AuthArgs, signer?: Wallet | JsonRpcSigner, wsRelayerUrl?: string);
11
11
  getOk(): Promise<any>;
12
12
  getRelayAddress(): Promise<AddressPayload>;
13
13
  getNonce(signerAddress: string, signerType: string): Promise<NoncePayload>;
@@ -17,5 +17,6 @@ export declare class RelayClient {
17
17
  executeSafeTransactions(txns: SafeTransaction[]): Promise<any>;
18
18
  deploySafe(): Promise<any>;
19
19
  initializeWsConnection(transactionId?: string): Promise<void>;
20
- private sendTransactionRequest;
20
+ private postTransactionRequest;
21
+ private send;
21
22
  }
package/dist/client.js CHANGED
@@ -15,7 +15,9 @@ class RelayClient {
15
15
  constructor(relayerUrl, chainId, authArgs, signer, wsRelayerUrl) {
16
16
  this.relayerUrl = relayerUrl.endsWith("/") ? relayerUrl.slice(0, -1) : relayerUrl;
17
17
  this.chainId = chainId;
18
- this.authHandler = new auth_1.AuthHandler(authArgs);
18
+ if (authArgs !== undefined) {
19
+ this.authHandler = new auth_1.AuthHandler(authArgs);
20
+ }
19
21
  if (signer !== undefined) {
20
22
  this.signer = signer;
21
23
  if (signer.provider == undefined) {
@@ -28,27 +30,27 @@ class RelayClient {
28
30
  }
29
31
  getOk() {
30
32
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
31
- return http_helpers_1.get(`${this.relayerUrl}/`);
33
+ return this.send(`${this.relayerUrl}/`, http_helpers_1.GET);
32
34
  });
33
35
  }
34
36
  getRelayAddress() {
35
37
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
36
- return http_helpers_1.get(`${this.relayerUrl}${endpoints_1.GET_ADDRESS}`, { headers: yield this.authHandler.getAuthHeader() });
38
+ return this.send(`${this.relayerUrl}${endpoints_1.GET_ADDRESS}`, http_helpers_1.GET);
37
39
  });
38
40
  }
39
41
  getNonce(signerAddress, signerType) {
40
42
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
41
- return http_helpers_1.get(`${this.relayerUrl}${endpoints_1.GET_NONCE}?address=${signerAddress}&type=${signerType}`, { headers: yield this.authHandler.getAuthHeader() });
43
+ return this.send(`${this.relayerUrl}${endpoints_1.GET_NONCE}?address=${signerAddress}&type=${signerType}`, http_helpers_1.GET);
42
44
  });
43
45
  }
44
46
  getRelayPayload(signerAddress, signerType) {
45
47
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
46
- return http_helpers_1.get(`${this.relayerUrl}${endpoints_1.GET_RELAY_PAYLOAD}?address=${signerAddress}&type=${signerType}`, { headers: yield this.authHandler.getAuthHeader() });
48
+ return this.send(`${this.relayerUrl}${endpoints_1.GET_RELAY_PAYLOAD}?address=${signerAddress}&type=${signerType}`, http_helpers_1.GET);
47
49
  });
48
50
  }
49
51
  getTransaction(transactionId) {
50
52
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
51
- return http_helpers_1.get(`${this.relayerUrl}${endpoints_1.GET_TRANSACTION}?id=${transactionId}`, { headers: yield this.authHandler.getAuthHeader() });
53
+ return this.send(`${this.relayerUrl}${endpoints_1.GET_TRANSACTION}?id=${transactionId}`, http_helpers_1.GET);
52
54
  });
53
55
  }
54
56
  executeProxyTransactions(txns) {
@@ -68,7 +70,7 @@ class RelayClient {
68
70
  };
69
71
  const request = yield builder_1.buildProxyTransactionRequest(this.signer, args);
70
72
  console.log(`Client side request creation took: ${(Date.now() - start) / 1000} seconds`);
71
- return this.sendTransactionRequest(request);
73
+ return this.postTransactionRequest(request);
72
74
  });
73
75
  }
74
76
  executeSafeTransactions(txns) {
@@ -88,7 +90,7 @@ class RelayClient {
88
90
  };
89
91
  const request = yield builder_1.buildSafeTransactionRequest(this.signer, args);
90
92
  console.log(`Client side request creation took: ${(Date.now() - start) / 1000} seconds`);
91
- return this.sendTransactionRequest(request);
93
+ return this.postTransactionRequest(request);
92
94
  });
93
95
  }
94
96
  deploySafe() {
@@ -108,7 +110,7 @@ class RelayClient {
108
110
  };
109
111
  const request = yield create_1.buildSafeCreateTransactionRequest(this.signer, args);
110
112
  console.log(`Client side request creation took: ${(Date.now() - start) / 1000} seconds`);
111
- return this.sendTransactionRequest(request);
113
+ return this.postTransactionRequest(request);
112
114
  });
113
115
  }
114
116
  initializeWsConnection(transactionId) {
@@ -116,6 +118,9 @@ class RelayClient {
116
118
  if (this.signer == undefined) {
117
119
  throw new Error("missing signer");
118
120
  }
121
+ if (this.authHandler == undefined) {
122
+ throw new Error("missing auth handler");
123
+ }
119
124
  if (this.wsRelayerUrl == undefined) {
120
125
  throw new Error("missing websocket url");
121
126
  }
@@ -147,12 +152,29 @@ class RelayClient {
147
152
  });
148
153
  });
149
154
  }
150
- sendTransactionRequest(req) {
155
+ postTransactionRequest(req) {
151
156
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
152
- return http_helpers_1.post(`${this.relayerUrl}${endpoints_1.SUBMIT_TRANSACTION}`, {
153
- data: req,
154
- headers: yield this.authHandler.getAuthHeader()
155
- });
157
+ return this.send(`${this.relayerUrl}${endpoints_1.SUBMIT_TRANSACTION}`, http_helpers_1.POST, undefined, req);
158
+ });
159
+ }
160
+ send(endpoint, method, headers, data, params) {
161
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
162
+ // If client is instantiated locally/not on the browser, inject authorization cookies
163
+ // this is to ensure examples still run locally
164
+ if (this.authHandler != undefined) {
165
+ const cookie = yield this.authHandler.getPolymarketCookies();
166
+ if (headers != undefined) {
167
+ headers["Cookie"] = cookie;
168
+ }
169
+ else {
170
+ headers = { "Cookie": cookie };
171
+ }
172
+ const resp = yield http_helpers_1.request(endpoint, method, headers, data, params);
173
+ return resp.data;
174
+ }
175
+ // If the client is instantiated on the browser, do not inject cookies
176
+ const resp = yield http_helpers_1.request(endpoint, method, headers, data, params);
177
+ return resp.data;
156
178
  });
157
179
  }
158
180
  }
@@ -7,28 +7,39 @@ 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;
10
11
  exports.request = (endpoint, method, headers, data, params) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
11
- var _a, _b, _c, _d;
12
+ var _a, _b, _c;
12
13
  try {
13
- const response = yield axios_1.default({ method, url: endpoint, headers, data, params });
14
- return response;
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;
15
23
  }
16
24
  catch (err) {
17
25
  if (axios_1.default.isAxiosError(err)) {
18
26
  if (err.response) {
19
- console.error("request error", {
27
+ const errPayload = {
20
28
  error: "request error",
21
29
  status: (_a = err.response) === null || _a === void 0 ? void 0 : _a.status,
22
30
  statusText: (_b = err.response) === null || _b === void 0 ? void 0 : _b.statusText,
23
31
  data: (_c = err.response) === null || _c === void 0 ? void 0 : _c.data,
24
- });
25
- return (_d = err.response) === null || _d === void 0 ? void 0 : _d.data;
32
+ };
33
+ console.error("request error", errPayload);
34
+ throw new Error(JSON.stringify(errPayload));
26
35
  }
27
36
  else {
28
- return { error: "connection error" };
37
+ const errPayload = { error: "connection error" };
38
+ console.error("connection error", errPayload);
39
+ throw new Error(JSON.stringify(errPayload));
29
40
  }
30
41
  }
31
- return { error: err };
42
+ throw new Error(JSON.stringify({ error: err }));
32
43
  }
33
44
  });
34
45
  exports.post = (endpoint, options) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
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.2",
4
+ "version": "0.0.3",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "files": [