@show-karma/karma-gap-sdk 0.1.34 → 0.1.36

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,6 +1,6 @@
1
- import { GelatoRelay } from "@gelatonetwork/relay-sdk";
2
- import { Hex } from "../../types";
3
- declare function sendByUrl(...params: Parameters<GelatoRelay["sponsoredCall"]>): Promise<string>;
1
+ import { GelatoRelay } from '@gelatonetwork/relay-sdk';
2
+ import { Hex } from '../../types';
3
+ declare function sendByUrl(...params: Parameters<GelatoRelay['sponsoredCall']>): Promise<string>;
4
4
  /**
5
5
  * Send gelato using an explicit api key.
6
6
  *
@@ -9,20 +9,21 @@ declare function sendByUrl(...params: Parameters<GelatoRelay["sponsoredCall"]>):
9
9
  * @param params
10
10
  * @returns Gelato's task id and a wait function.
11
11
  */
12
- declare function sendByApiKey(...params: Parameters<GelatoRelay["sponsoredCall"]>): Promise<{
12
+ declare function sendByApiKey(...params: Parameters<GelatoRelay['sponsoredCall']>): Promise<{
13
13
  taskId: string;
14
14
  /**
15
15
  * Waits for the transaction to be confirmed by Gelato.
16
+ * @param ttl interval between checks in ms
16
17
  * @returns Txn id
17
18
  */
18
- wait: () => Promise<string>;
19
+ wait: (ttl?: number) => Promise<string>;
19
20
  }>;
20
21
  /**
21
22
  * Sends a sponsored call using GelatoRelay
22
23
  * @param payload
23
24
  * @returns txn hash
24
25
  */
25
- declare function sendGelatoTxn(...params: Parameters<GelatoRelay["sponsoredCall"]>): Promise<string>;
26
+ declare function sendGelatoTxn(...params: Parameters<GelatoRelay['sponsoredCall']>): Promise<string>;
26
27
  /**
27
28
  * Builds the arguments for a sponsored call using GelatoRelay
28
29
  * @param data Populated contract call.
@@ -45,7 +46,7 @@ declare function buildArgs(
45
46
  /**
46
47
  * Populated transaction data.
47
48
  */
48
- data: string, chainId: bigint, target: Hex): Parameters<GelatoRelay["sponsoredCall"]>;
49
+ data: string, chainId: bigint, target: Hex): Parameters<GelatoRelay['sponsoredCall']>;
49
50
  declare const Gelato: {
50
51
  sendByApiKey: typeof sendByApiKey;
51
52
  sendByUrl: typeof sendByUrl;
@@ -22,9 +22,9 @@ async function sendByUrl(...params) {
22
22
  */
23
23
  async function sendByApiKey(...params) {
24
24
  const { apiKey } = GAP_1.GAP?.gelatoOpts || {};
25
- if (!apiKey && params[1] === "{apiKey}")
26
- throw new Error("No api key provided.");
27
- if (apiKey && params[1] === "{apiKey}")
25
+ if (!apiKey && params[1] === '{apiKey}')
26
+ throw new Error('No api key provided.');
27
+ if (apiKey && params[1] === '{apiKey}')
28
28
  params[1] = apiKey;
29
29
  const client = new relay_sdk_1.GelatoRelay();
30
30
  const relayResponse = await client.sponsoredCall(...params);
@@ -32,9 +32,10 @@ async function sendByApiKey(...params) {
32
32
  taskId: relayResponse.taskId,
33
33
  /**
34
34
  * Waits for the transaction to be confirmed by Gelato.
35
+ * @param ttl interval between checks in ms
35
36
  * @returns Txn id
36
37
  */
37
- wait: () => (0, watch_gelato_txn_1.watchGelatoTxn)(relayResponse.taskId),
38
+ wait: (ttl) => (0, watch_gelato_txn_1.watchGelatoTxn)(relayResponse.taskId, ttl),
38
39
  };
39
40
  }
40
41
  /**
@@ -44,10 +45,10 @@ async function sendByApiKey(...params) {
44
45
  */
45
46
  async function sendGelatoTxn(...params) {
46
47
  if (!GAP_1.GAP.gelatoOpts)
47
- throw new Error("Gelato opts not set.");
48
+ throw new Error('Gelato opts not set.');
48
49
  const { env_gelatoApiKey, sponsorUrl, useGasless, contained } = GAP_1.GAP.gelatoOpts;
49
50
  if (!useGasless)
50
- throw new Error("Gasless is not enabled.");
51
+ throw new Error('Gasless is not enabled.');
51
52
  if ((sponsorUrl && contained && env_gelatoApiKey) ||
52
53
  (sponsorUrl && !contained)) {
53
54
  return sendByUrl(...params);
@@ -85,7 +86,7 @@ data, chainId, target) {
85
86
  chainId,
86
87
  target,
87
88
  },
88
- "{apiKey}",
89
+ '{apiKey}',
89
90
  {
90
91
  retries: 3,
91
92
  },
@@ -3,5 +3,5 @@
3
3
  * @param taskId
4
4
  * @returns
5
5
  */
6
- declare function watchGelatoTxn(taskId: string): Promise<string>;
6
+ declare function watchGelatoTxn(taskId: string, ttl?: number): Promise<string>;
7
7
  export { watchGelatoTxn };
@@ -18,35 +18,43 @@ var TaskState;
18
18
  * @param taskId
19
19
  * @returns
20
20
  */
21
- async function watchGelatoTxn(taskId) {
21
+ async function watchGelatoTxn(taskId, ttl = 500) {
22
22
  const client = new relay_sdk_1.GelatoRelay();
23
23
  return new Promise((resolve, reject) => {
24
24
  const loop = async () => {
25
25
  const oneSecond = 1;
26
- while (oneSecond) {
27
- const status = await client.getTaskStatus(taskId);
28
- // print status :D so we can debug this for now
29
- // eslint-disable-next-line no-console
30
- console.log(status);
31
- if (!status) {
32
- reject(new Error("Transaction goes wrong."));
33
- break;
26
+ try {
27
+ while (oneSecond) {
28
+ const status = await client.getTaskStatus(taskId);
29
+ // print status :D so we can debug this for now
30
+ // eslint-disable-next-line no-console
31
+ console.log(status);
32
+ if (!status) {
33
+ reject(new Error('Transaction goes wrong.'));
34
+ break;
35
+ }
36
+ if (status && status.taskState === TaskState.ExecSuccess) {
37
+ resolve(status.transactionHash || '');
38
+ break;
39
+ }
40
+ else if ([
41
+ TaskState.Cancelled,
42
+ TaskState.ExecReverted,
43
+ TaskState.Blacklisted,
44
+ ].includes(status?.taskState)) {
45
+ reject(new Error(status.lastCheckMessage
46
+ ?.split(/(RegisterDelegate)|(Execution error): /)
47
+ .at(-1) || ''));
48
+ break;
49
+ }
50
+ await new Promise((r) => setTimeout(r, ttl));
34
51
  }
35
- if (status && status.taskState === TaskState.ExecSuccess) {
36
- resolve(status.transactionHash || "");
37
- break;
38
- }
39
- else if ([
40
- TaskState.Cancelled,
41
- TaskState.ExecReverted,
42
- TaskState.Blacklisted,
43
- ].includes(status?.taskState)) {
44
- reject(new Error(status.lastCheckMessage
45
- ?.split(/(RegisterDelegate)|(Execution error): /)
46
- .at(-1) || ""));
47
- break;
48
- }
49
- await new Promise((r) => setTimeout(r, 500));
52
+ }
53
+ catch {
54
+ // gelato may throw 429 error, so we need to retry
55
+ // Increase ttl to avoid too deadlocking
56
+ // Max ttl is 30s
57
+ ttl += Math.max(30000, ttl + 1000);
50
58
  }
51
59
  };
52
60
  loop();
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.1.34",
6
+ "version": "0.1.36",
7
7
  "description": "Simple and easy interface between EAS and Karma GAP.",
8
8
  "main": "dist/index.js",
9
9
  "author": "KarmaHQ",