@axelar-network/axelarjs-sdk 0.13.0 → 0.13.1-alpha

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.
Files changed (60) hide show
  1. package/dist/src/chains/supported-chains-list.d.ts +4 -0
  2. package/dist/src/chains/supported-chains-list.d.ts.map +1 -1
  3. package/dist/src/chains/supported-chains-list.js +5 -1
  4. package/dist/src/chains/supported-chains-list.js.map +1 -1
  5. package/dist/src/chains/types/index.d.ts +1 -0
  6. package/dist/src/chains/types/index.d.ts.map +1 -1
  7. package/dist/src/chains/types/index.js.map +1 -1
  8. package/dist/src/libs/AxelarAssetTransfer.d.ts +49 -1
  9. package/dist/src/libs/AxelarAssetTransfer.d.ts.map +1 -1
  10. package/dist/src/libs/AxelarAssetTransfer.js +128 -0
  11. package/dist/src/libs/AxelarAssetTransfer.js.map +1 -1
  12. package/dist/src/libs/AxelarGateway.d.ts +4 -2
  13. package/dist/src/libs/AxelarGateway.d.ts.map +1 -1
  14. package/dist/src/libs/AxelarGateway.js +21 -3
  15. package/dist/src/libs/AxelarGateway.js.map +1 -1
  16. package/dist/src/libs/AxelarQueryAPI.d.ts +4 -2
  17. package/dist/src/libs/AxelarQueryAPI.d.ts.map +1 -1
  18. package/dist/src/libs/AxelarQueryAPI.js +18 -9
  19. package/dist/src/libs/AxelarQueryAPI.js.map +1 -1
  20. package/dist/src/libs/AxelarSigningClient/types/AxelarnetTxTypes.d.ts.map +1 -1
  21. package/dist/src/libs/AxelarSigningClient/types/AxelarnetTxTypes.js +2 -0
  22. package/dist/src/libs/AxelarSigningClient/types/AxelarnetTxTypes.js.map +1 -1
  23. package/dist/src/libs/TransactionRecoveryApi/AxelarGMPRecoveryAPI.d.ts +25 -18
  24. package/dist/src/libs/TransactionRecoveryApi/AxelarGMPRecoveryAPI.d.ts.map +1 -1
  25. package/dist/src/libs/TransactionRecoveryApi/AxelarGMPRecoveryAPI.js +299 -185
  26. package/dist/src/libs/TransactionRecoveryApi/AxelarGMPRecoveryAPI.js.map +1 -1
  27. package/dist/src/libs/TransactionRecoveryApi/AxelarRecoveryApi.d.ts +5 -2
  28. package/dist/src/libs/TransactionRecoveryApi/AxelarRecoveryApi.d.ts.map +1 -1
  29. package/dist/src/libs/TransactionRecoveryApi/AxelarRecoveryApi.js +29 -19
  30. package/dist/src/libs/TransactionRecoveryApi/AxelarRecoveryApi.js.map +1 -1
  31. package/dist/src/libs/TransactionRecoveryApi/constants/chain/mainnet.d.ts.map +1 -1
  32. package/dist/src/libs/TransactionRecoveryApi/constants/chain/mainnet.js +6 -1
  33. package/dist/src/libs/TransactionRecoveryApi/constants/chain/mainnet.js.map +1 -1
  34. package/dist/src/libs/TransactionRecoveryApi/constants/chain/testnet.d.ts.map +1 -1
  35. package/dist/src/libs/TransactionRecoveryApi/constants/chain/testnet.js +10 -0
  36. package/dist/src/libs/TransactionRecoveryApi/constants/chain/testnet.js.map +1 -1
  37. package/dist/src/libs/TransactionRecoveryApi/constants/contract.d.ts +1 -0
  38. package/dist/src/libs/TransactionRecoveryApi/constants/contract.d.ts.map +1 -1
  39. package/dist/src/libs/TransactionRecoveryApi/constants/contract.js +1 -0
  40. package/dist/src/libs/TransactionRecoveryApi/constants/contract.js.map +1 -1
  41. package/dist/src/libs/TransactionRecoveryApi/helpers/axelarHelper.d.ts.map +1 -1
  42. package/dist/src/libs/TransactionRecoveryApi/helpers/axelarHelper.js +3 -0
  43. package/dist/src/libs/TransactionRecoveryApi/helpers/axelarHelper.js.map +1 -1
  44. package/dist/src/libs/types/index.d.ts +7 -5
  45. package/dist/src/libs/types/index.d.ts.map +1 -1
  46. package/dist/src/libs/types/index.js +2 -1
  47. package/dist/src/libs/types/index.js.map +1 -1
  48. package/dist/src/utils/index.d.ts +1 -1
  49. package/dist/src/utils/index.d.ts.map +1 -1
  50. package/dist/src/utils/index.js +1 -1
  51. package/dist/src/utils/index.js.map +1 -1
  52. package/dist/src/utils/retry.d.ts +2 -0
  53. package/dist/src/utils/retry.d.ts.map +1 -0
  54. package/dist/src/utils/retry.js +29 -0
  55. package/dist/src/utils/retry.js.map +1 -0
  56. package/package.json +1 -1
  57. package/dist/src/utils/asyncRetry.d.ts +0 -2
  58. package/dist/src/utils/asyncRetry.d.ts.map +0 -1
  59. package/dist/src/utils/asyncRetry.js +0 -35
  60. package/dist/src/utils/asyncRetry.js.map +0 -1
@@ -12,7 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
12
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.AxelarGMPRecoveryAPI = exports.GMPErrorResponse = exports.GMPErrorMap = void 0;
15
+ exports.AxelarGMPRecoveryAPI = exports.RouteDir = exports.GMPErrorMap = void 0;
16
16
  const types_1 = require("../types");
17
17
  const AxelarRecoveryApi_1 = require("./AxelarRecoveryApi");
18
18
  const EVMClient_1 = __importDefault(require("./client/EVMClient"));
@@ -35,11 +35,12 @@ exports.GMPErrorMap = {
35
35
  [AxelarRecoveryApi_1.GMPStatus.DEST_EXECUTED]: types_1.ApproveGatewayError.ALREADY_EXECUTED,
36
36
  [AxelarRecoveryApi_1.GMPStatus.DEST_GATEWAY_APPROVED]: types_1.ApproveGatewayError.ALREADY_APPROVED,
37
37
  };
38
- const GMPErrorResponse = (error, errorDetails) => ({
39
- success: false,
40
- error: errorDetails || error,
41
- });
42
- exports.GMPErrorResponse = GMPErrorResponse;
38
+ var RouteDir;
39
+ (function (RouteDir) {
40
+ RouteDir["COSMOS_TO_EVM"] = "cosmos_to_evm";
41
+ RouteDir["EVM_TO_COSMOS"] = "evm_to_cosmos";
42
+ RouteDir["EVM_TO_EVM"] = "evm_to_evm";
43
+ })(RouteDir = exports.RouteDir || (exports.RouteDir = {}));
43
44
  class AxelarGMPRecoveryAPI extends AxelarRecoveryApi_1.AxelarRecoveryApi {
44
45
  constructor(config) {
45
46
  super(config);
@@ -66,11 +67,14 @@ class AxelarGMPRecoveryAPI extends AxelarRecoveryApi_1.AxelarRecoveryApi {
66
67
  getCidFromSrcTxHash(destChainId, txHash, eventIndex) {
67
68
  return (0, helpers_1.getCommandId)(destChainId, txHash, eventIndex, this.environment, chain_1.default);
68
69
  }
69
- doesTxMeetConfirmHt(chain, currHeight) {
70
+ doesTxMeetConfirmHt(chain, txHash) {
70
71
  return __awaiter(this, void 0, void 0, function* () {
72
+ const confirmations = yield this.getSigner(chain)
73
+ .provider.getTransactionReceipt(txHash)
74
+ .then((receipt) => receipt.confirmations);
71
75
  return this.axelarQueryApi
72
76
  .getConfirmationHeight(chain)
73
- .then((res) => res.height.greaterThan(currHeight))
77
+ .then((minConfirmHeight) => minConfirmHeight <= confirmations)
74
78
  .catch(() => undefined);
75
79
  });
76
80
  }
@@ -94,265 +98,375 @@ class AxelarGMPRecoveryAPI extends AxelarRecoveryApi_1.AxelarRecoveryApi {
94
98
  }
95
99
  getEvmEvent(srcChainId, destChainId, srcTxHash, evmWalletDetails) {
96
100
  return __awaiter(this, void 0, void 0, function* () {
97
- let eventIndex = -1, success = false, errorMessage = "", infoLog = "";
98
- try {
99
- eventIndex = (yield this.getEventIndex(srcChainId, srcTxHash, evmWalletDetails));
100
- }
101
- catch (e) {
101
+ const eventIndex = yield this.getEventIndex(srcChainId, srcTxHash, evmWalletDetails)
102
+ .then((index) => index)
103
+ .catch(() => -1);
104
+ if (eventIndex === -1) {
102
105
  return {
103
- success,
106
+ success: false,
104
107
  errorMessage: `getEvmEvent(): could not find event index for ${srcTxHash}`,
105
108
  commandId: "",
106
109
  eventResponse: {},
107
- infoLog,
110
+ infoLog: "",
108
111
  };
109
112
  }
110
113
  const commandId = this.getCidFromSrcTxHash(destChainId, srcTxHash, eventIndex);
111
- infoLog = `srcTxHash: ${srcTxHash}, generated commandId: ${commandId}`;
112
- const eventResponse = yield this.axelarQueryApi.getEVMEvent(srcChainId, srcTxHash, eventIndex);
114
+ const eventResponse = yield (0, utils_1.retry)(() => this.axelarQueryApi.getEVMEvent(srcChainId, srcTxHash, eventIndex), 12, 10000);
113
115
  if (!eventResponse || this.isEVMEventFailed(eventResponse)) {
114
- errorMessage = this.isEVMEventFailed(eventResponse)
116
+ const errorMessage = this.isEVMEventFailed(eventResponse)
115
117
  ? `getEvmEvent(): event on source chain is not successful for: ${srcTxHash}`
116
118
  : `getEvmEvent(): could not determine status of event: ${srcTxHash}`;
117
119
  return {
118
- success,
120
+ success: false,
119
121
  errorMessage,
120
122
  commandId,
121
123
  eventResponse: {},
122
- infoLog,
124
+ infoLog: `srcTxHash: ${srcTxHash}, generated commandId: ${commandId}`,
123
125
  };
124
126
  }
125
- success = true;
126
- infoLog = `${srcTxHash} correspondes to command ID: ${commandId}`;
127
127
  return {
128
+ success: true,
128
129
  commandId,
129
130
  eventResponse,
130
- success,
131
- errorMessage,
132
- infoLog,
131
+ errorMessage: "",
132
+ infoLog: `${srcTxHash} correspondes to command ID: ${commandId}`,
133
133
  };
134
134
  });
135
135
  }
136
- findEventAndConfirmIfNeeded(srcChain, destChain, txHash, evmWalletDetails, sleepSeconds = 60) {
136
+ findEventAndConfirmIfNeeded(srcChain, destChain, txHash, evmWalletDetails) {
137
137
  return __awaiter(this, void 0, void 0, function* () {
138
- const res = {
139
- confirmTx: null,
140
- success: true,
141
- errorMessage: "",
142
- infoLogs: [],
143
- commandId: "",
144
- eventResponse: null,
145
- };
146
- let confirmLog = "";
147
138
  if (this.debugMode)
148
139
  console.debug(`confirmation: checking whether ${txHash} needs to be confirmed on Axelar`);
149
- const evmEventResponse = yield this.getEvmEvent(srcChain, destChain, txHash, evmWalletDetails);
150
- res.commandId = evmEventResponse.commandId;
151
- res.eventResponse = evmEventResponse.eventResponse;
152
- const { infoLog: getEvmEventInfoLog } = evmEventResponse;
140
+ const evmEvent = yield this.getEvmEvent(srcChain, destChain, txHash, evmWalletDetails);
141
+ const { infoLog: getEvmEventInfoLog } = evmEvent;
153
142
  if (this.debugMode)
154
143
  console.debug(`confirmation: ${getEvmEventInfoLog}`);
155
- if (this.isEVMEventCompleted(res.eventResponse) ||
156
- this.isEVMEventConfirmed(res.eventResponse)) {
157
- confirmLog = `confirmation: event for ${txHash} was already detected on the network and did not need to be confirmed`;
144
+ if (this.isEVMEventCompleted(evmEvent.eventResponse) ||
145
+ this.isEVMEventConfirmed(evmEvent.eventResponse)) {
146
+ return {
147
+ success: true,
148
+ commandId: evmEvent.commandId,
149
+ eventResponse: evmEvent.eventResponse,
150
+ infoLogs: [
151
+ `confirmation: event for ${txHash} was already detected on the network and did not need to be confirmed`,
152
+ ],
153
+ };
158
154
  }
159
155
  else {
160
- /**todo, need to check whether tx is finalized */
161
- // const confirmationHeight = await this.axelarQueryApi.getConfirmationHeight(srcChain);
162
- res.confirmTx = yield this.confirmGatewayTx(txHash, srcChain).catch((e) => {
163
- console.error(e);
164
- return null;
165
- });
166
- if (!res.confirmTx) {
167
- res.success = false;
168
- res.errorMessage = "findEventAndConfirmIfNeeded(): unable to confirm transaction on Axelar";
169
- return res;
156
+ const isConfirmFinalized = yield this.doesTxMeetConfirmHt(srcChain, txHash);
157
+ if (!isConfirmFinalized) {
158
+ const minConfirmLevel = yield this.axelarQueryApi.getConfirmationHeight(srcChain);
159
+ return {
160
+ success: false,
161
+ commandId: evmEvent.commandId,
162
+ eventResponse: evmEvent.eventResponse,
163
+ infoLogs: [],
164
+ errorMessage: `findEventAndConfirmIfNeeded(): ${txHash} is not confirmed on ${srcChain}. The minimum confirmation height is ${minConfirmLevel}`,
165
+ };
166
+ }
167
+ const confirmTx = yield this.confirmGatewayTx(txHash, srcChain).catch(() => undefined);
168
+ if (!confirmTx) {
169
+ return {
170
+ success: false,
171
+ commandId: evmEvent.commandId,
172
+ eventResponse: evmEvent.eventResponse,
173
+ infoLogs: [],
174
+ errorMessage: `findEventAndConfirmIfNeeded(): could not confirm transaction on Axelar`,
175
+ };
170
176
  }
171
- confirmLog = `confirmation: successfully confirmed ${txHash} on Axelar; waiting ${sleepSeconds} seconds for network confirmation`;
172
- if (this.debugMode)
173
- console.debug(confirmLog);
174
- yield (0, utils_1.sleep)(sleepSeconds);
175
177
  const updatedEvent = yield this.getEvmEvent(srcChain, destChain, txHash, evmWalletDetails);
176
178
  if (this.isEVMEventCompleted(updatedEvent === null || updatedEvent === void 0 ? void 0 : updatedEvent.eventResponse)) {
177
- res.eventResponse = updatedEvent.eventResponse;
178
- confirmLog += `; confirmed event was finalized`;
179
+ return {
180
+ success: true,
181
+ commandId: updatedEvent.commandId,
182
+ eventResponse: updatedEvent.eventResponse,
183
+ infoLogs: [
184
+ `confirmation: successfully confirmed ${txHash} on Axelar; confirmed event was finalized`,
185
+ getEvmEventInfoLog,
186
+ ],
187
+ };
179
188
  }
180
189
  else {
181
- res.success = false;
182
- res.errorMessage = `findEventAndConfirmIfNeeded(): could not confirm and finalize event successfully: ${txHash}`;
183
- confirmLog += `; confirmed event was unable to be finalized`;
190
+ return {
191
+ success: false,
192
+ eventResponse: evmEvent.eventResponse,
193
+ commandId: updatedEvent.commandId,
194
+ errorMessage: `findEventAndConfirmIfNeeded(): could not confirm and finalize event successfully: ${txHash};. Your transaction may not have enough confirmations yet.`,
195
+ infoLogs: [
196
+ `confirmation: successfully confirmed ${txHash} on Axelar; confirmed event was unable to be finalized`,
197
+ getEvmEventInfoLog,
198
+ ],
199
+ };
184
200
  }
185
201
  }
186
- if (this.debugMode)
187
- console.debug(confirmLog);
188
- getEvmEventInfoLog && res.infoLogs.push(getEvmEventInfoLog);
189
- res.infoLogs.push(confirmLog);
190
- return res;
191
202
  });
192
203
  }
193
- findBatchAndSignIfNeeded(commandId, destChainId, sleepSeconds = 60) {
204
+ findBatchAndSignIfNeeded(commandId, destChainId) {
194
205
  return __awaiter(this, void 0, void 0, function* () {
195
206
  let signTxLog = "";
196
- const res = {
197
- success: true,
198
- errorMessage: "",
199
- signCommandTx: null,
200
- infoLogs: [],
201
- };
202
- if (this.debugMode)
203
- console.debug(`signing: checking whether command ID: ${commandId} needs to be signed`);
204
207
  try {
205
208
  const batchData = yield this.fetchBatchData(destChainId, commandId);
206
209
  if (batchData) {
207
- signTxLog = `signing: batch data exists so do not need to sign. commandId: ${commandId}, batchId: ${batchData.batch_id}; waiting ${sleepSeconds} seconds to attempt broadcast`;
210
+ signTxLog = `signing: batch data exists so do not need to sign. commandId: ${commandId}, batchId: ${batchData.batch_id}`;
211
+ if (this.debugMode)
212
+ console.debug(signTxLog);
213
+ return {
214
+ success: true,
215
+ infoLogs: [signTxLog],
216
+ };
208
217
  }
209
218
  else {
210
- res.signCommandTx = yield this.signCommands(destChainId);
211
- signTxLog = `signing: signed batch for commandId (${commandId}) in tx: ${res.signCommandTx.transactionHash}; waiting ${sleepSeconds} seconds to attempt broadcast`;
219
+ const signCommandTx = yield this.signCommands(destChainId);
220
+ signTxLog = `signing: signed batch for commandId (${commandId}) in tx: ${signCommandTx.transactionHash}`;
221
+ if (this.debugMode)
222
+ console.debug(signTxLog);
223
+ return {
224
+ success: true,
225
+ signCommandTx,
226
+ infoLogs: [signTxLog],
227
+ };
212
228
  }
213
- if (this.debugMode)
214
- console.debug(signTxLog);
215
- yield (0, utils_1.sleep)(sleepSeconds);
216
229
  }
217
230
  catch (e) {
218
- console.error(e);
219
- res.errorMessage = `findBatchAndSignIfNeeded(): issue retrieving and signing command data: ${commandId}`;
220
- res.success = false;
231
+ return {
232
+ success: false,
233
+ errorMessage: `findBatchAndSignIfNeeded(): issue retrieving and signing command data: ${commandId}`,
234
+ infoLogs: [signTxLog],
235
+ };
221
236
  }
222
- res.infoLogs.push(signTxLog);
223
- return res;
224
237
  });
225
238
  }
226
- findBatchAndBroadcast(commandId, destChainId, wallet, iter = 0, maxTries = 2) {
239
+ findBatchAndApproveGateway(commandId, destChainId, wallet) {
227
240
  return __awaiter(this, void 0, void 0, function* () {
228
- if (iter > maxTries)
229
- return {
230
- success: false,
231
- errorMessage: `findBatchAndBroadcast(): this recovery stalled out on waiting for signing. please try again later. `,
232
- approveTx: null,
233
- infoLogs: [],
234
- };
235
241
  if (this.debugMode)
236
242
  console.debug(`broadcasting: checking for command ID: ${commandId} to broadcast`);
237
- let broadcastTxLog = "";
238
- const res = {
239
- success: true,
240
- errorMessage: "",
241
- approveTx: null,
242
- infoLogs: [],
243
- };
244
- try {
243
+ return (0, utils_1.retry)(() => __awaiter(this, void 0, void 0, function* () {
245
244
  const batchData = yield this.fetchBatchData(destChainId, commandId);
246
245
  if (!batchData) {
247
- res.success = false;
248
- res.errorMessage = `findBatchAndBroadcast(): unable to retrieve batch data for ${commandId}`;
249
- return res;
246
+ return Promise.reject(`findBatchAndApproveGateway(): unable to retrieve batch data for ${commandId}`);
250
247
  }
251
248
  const commandData = batchData.command_ids.find((t) => t === commandId);
252
249
  if (!commandData) {
253
- res.success = false;
254
- res.errorMessage = `findBatchAndBroadcast(): unable to retrieve command ID (${commandId}) in batch data`;
255
- return res;
250
+ return Promise.reject(`findBatchAndApproveGateway(): unable to retrieve command ID (${commandId}) in batch data`);
256
251
  }
257
- switch (batchData.status) {
258
- case "BATCHED_COMMANDS_STATUS_SIGNED":
259
- res.approveTx = yield this.sendApproveTx(destChainId, batchData.execute_data, wallet);
260
- broadcastTxLog = `broadcasting: batch ID ${batchData.batch_id} broadcasted to ${destChainId}`;
261
- res.infoLogs.push(broadcastTxLog);
262
- break;
263
- case "BATCHED_COMMANDS_STATUS_SIGNING": {
264
- broadcastTxLog = `broadcasting: batch ID ${batchData.batch_id} signing in process, checking again in 15 seconds`;
265
- if (this.debugMode)
266
- console.debug(broadcastTxLog);
267
- res.infoLogs.push(broadcastTxLog);
268
- (0, utils_1.sleep)(15);
269
- const retry = yield this.findBatchAndBroadcast(commandId, destChainId, wallet, iter + 1, maxTries);
270
- if (retry.infoLogs)
271
- res.infoLogs = [...res.infoLogs, ...retry.infoLogs];
272
- break;
273
- }
274
- default:
275
- res.errorMessage = `findBatchAndBroadcastIfNeeded(): status unsuccessful for command data: ${commandId}`;
276
- res.success = false;
252
+ if (batchData.status === "BATCHED_COMMANDS_STATUS_SIGNING") {
253
+ return Promise.reject(`findBatchAndApproveGateway(): batch ID ${batchData.batch_id} signing in process`);
277
254
  }
278
- }
279
- catch (e) {
280
- res.errorMessage = `findBatchAndBroadcastIfNeeded(): issue retrieving and broadcasting command data: ${commandId}`;
281
- res.success = false;
282
- }
283
- return res;
255
+ else if (batchData.status === "BATCHED_COMMANDS_STATUS_SIGNED") {
256
+ const approveTx = yield this.sendApproveTx(destChainId, batchData.execute_data, wallet);
257
+ return {
258
+ success: true,
259
+ approveTx,
260
+ infoLogs: [
261
+ `broadcasting: batch ID ${batchData.batch_id} broadcasted to ${destChainId}`,
262
+ ],
263
+ };
264
+ }
265
+ else {
266
+ return Promise.reject(`findBatchAndApproveGateway(): batch ID ${batchData.batch_id} is in an unknown state for command data: ${commandId}`);
267
+ }
268
+ }), 3, 10).catch((error) => {
269
+ return {
270
+ success: false,
271
+ errorMessage: error.message || // error can be both a string or an object with a message property
272
+ error ||
273
+ `findBatchAndApproveGatewayIfNeeded(): issue retrieving and broadcasting command data: ${commandId}`,
274
+ infoLogs: [],
275
+ };
276
+ });
284
277
  });
285
278
  }
286
279
  manualRelayToDestChain(txHash, evmWalletDetails, escapeAfterConfirm = true) {
287
280
  return __awaiter(this, void 0, void 0, function* () {
288
- let confirmTx = null;
289
- let signCommandTx = null;
290
- let approveTx = null;
291
- const success = true;
292
- let infoLogs = [];
293
- const _evmWalletDetails = evmWalletDetails || { useWindowEthereum: true };
294
281
  const { callTx, status } = yield this.queryTransactionStatus(txHash);
295
282
  /**first check if transaction is already executed */
296
283
  if (exports.GMPErrorMap[status])
297
- return (0, exports.GMPErrorResponse)(exports.GMPErrorMap[status]);
284
+ return {
285
+ success: false,
286
+ error: exports.GMPErrorMap[status],
287
+ };
298
288
  const srcChain = callTx.chain;
299
289
  const destChain = callTx.returnValues.destinationChain;
300
- let commandId = "";
301
- /**find event and confirm if needed */
302
- let confirmTxRequest;
303
- try {
304
- confirmTxRequest = yield this.findEventAndConfirmIfNeeded(srcChain, destChain, txHash, _evmWalletDetails);
305
- confirmTx = confirmTxRequest.confirmTx;
306
- commandId = confirmTxRequest.commandId;
307
- // eventResponse = confirmTxRequest.eventResponse;
308
- if (confirmTxRequest.infoLogs)
309
- infoLogs = [...infoLogs, ...confirmTxRequest.infoLogs];
290
+ const srcChainInfo = yield this.getChainInfo(srcChain);
291
+ const destChainInfo = yield this.getChainInfo(destChain);
292
+ const routeDir = this.getRouteDir(srcChainInfo, destChainInfo);
293
+ const _evmWalletDetails = evmWalletDetails || { useWindowEthereum: true };
294
+ if (routeDir === RouteDir.COSMOS_TO_EVM) {
295
+ return this.recoverCosmosToEvmTx(txHash, _evmWalletDetails);
310
296
  }
311
- catch (e) {
312
- return (0, exports.GMPErrorResponse)(types_1.ApproveGatewayError.CONFIRM_COMMAND_FAILED, e.errorMessage);
297
+ else if (routeDir === RouteDir.EVM_TO_COSMOS) {
298
+ return this.recoverEvmToCosmosTx(srcChain, txHash);
299
+ }
300
+ else {
301
+ return this.recoverEvmToEvmTx(srcChain, destChain, txHash, _evmWalletDetails, escapeAfterConfirm);
313
302
  }
314
- if (!(confirmTxRequest === null || confirmTxRequest === void 0 ? void 0 : confirmTxRequest.success))
315
- return (0, exports.GMPErrorResponse)(types_1.ApproveGatewayError.ERROR_BATCHED_COMMAND, confirmTxRequest.errorMessage);
316
- else if (confirmTx && escapeAfterConfirm) {
303
+ });
304
+ }
305
+ getRouteDir(srcChain, destChain) {
306
+ if (srcChain.module === "axelarnet" && destChain.module === "evm") {
307
+ return RouteDir.COSMOS_TO_EVM;
308
+ }
309
+ else if (srcChain.module === "evm" && destChain.module === "axelarnet") {
310
+ return RouteDir.EVM_TO_COSMOS;
311
+ }
312
+ else {
313
+ return RouteDir.EVM_TO_EVM;
314
+ }
315
+ }
316
+ recoverEvmToCosmosTx(srcChain, txHash) {
317
+ return __awaiter(this, void 0, void 0, function* () {
318
+ // Check if the tx is confirmed on the source chain
319
+ const isConfirmed = yield this.doesTxMeetConfirmHt(srcChain, txHash);
320
+ if (!isConfirmed) {
321
+ const minConfirmLevel = yield this.axelarQueryApi.getConfirmationHeight(srcChain);
317
322
  return {
318
- success,
319
- confirmTx,
320
- infoLogs,
323
+ success: false,
324
+ error: `${txHash} is not confirmed on ${srcChain}. The minimum confirmation height is ${minConfirmLevel}`,
321
325
  };
322
326
  }
323
- /**find batch and sign if needed */
324
- let signTxRequest;
327
+ // ConfirmGatewayTx and check if it is successfully executed
328
+ const confirmTx = yield this.confirmGatewayTx(txHash, srcChain).catch(() => undefined);
329
+ if (!confirmTx) {
330
+ return {
331
+ success: false,
332
+ error: "Failed to send ConfirmGatewayTx to Axelar",
333
+ };
334
+ }
335
+ // Fetch all necessary data to send the route message tx
336
+ const payload = yield this.fetchGMPTransaction(txHash).then((data) => data.call.returnValues.payload);
337
+ const logIndex = yield this.getEventIndex(srcChain, txHash);
338
+ // Send the route message tx
339
+ const routeMessageTx = yield this.routeMessageRequest(txHash, payload, logIndex).catch(() => undefined);
340
+ // If the `success` flag is false, return the error response
341
+ if (!routeMessageTx) {
342
+ return {
343
+ success: false,
344
+ error: "Failed to send RouteMessage to Axelar",
345
+ };
346
+ }
347
+ // Return the success response
348
+ return {
349
+ success: true,
350
+ confirmTx,
351
+ routeMessageTx,
352
+ infoLogs: [
353
+ `Successfully sent ConfirmGatewayTx tx for given tx hash ${txHash}`,
354
+ `Successfully sent RouteMessage tx for given tx hash ${txHash}`,
355
+ ],
356
+ };
357
+ });
358
+ }
359
+ recoverCosmosToEvmTx(txHash, evmWalletDetails) {
360
+ return __awaiter(this, void 0, void 0, function* () {
361
+ const txDetails = yield this.fetchGMPTransaction(txHash);
362
+ const { messageId, payload, destinationChain } = txDetails.call.returnValues;
363
+ const { command_id: commandId } = txDetails;
364
+ // Send RouteMessageTx
365
+ const routeMessageTx = yield this.routeMessageRequest(messageId, payload, -1).catch(() => undefined);
366
+ if (!routeMessageTx) {
367
+ return {
368
+ success: false,
369
+ error: "Failed to send RouteMessage to Axelar",
370
+ };
371
+ }
372
+ // Dispatch a SignCommand transaction and an Approve transaction to the Gateway contract.
373
+ const response = yield this.signAndApproveGateway(commandId, destinationChain, evmWalletDetails);
374
+ // If the response.success is false, we will return the error response
375
+ if (!response.success) {
376
+ return {
377
+ success: false,
378
+ error: response.error,
379
+ };
380
+ }
381
+ // Otherwise, we will return the success response
382
+ const { signCommandTx, infoLogs: signTxLogs } = response;
383
+ return {
384
+ success: true,
385
+ routeMessageTx,
386
+ signCommandTx,
387
+ infoLogs: [`Successfully sent RouteMessage tx for given ${txHash}`, ...signTxLogs],
388
+ };
389
+ });
390
+ }
391
+ recoverEvmToEvmTx(srcChain, destChain, txHash, evmWalletDetails, escapeAfterConfirm = true) {
392
+ return __awaiter(this, void 0, void 0, function* () {
325
393
  try {
326
- signTxRequest = yield this.findBatchAndSignIfNeeded(commandId, destChain);
327
- signCommandTx = signTxRequest.signCommandTx;
328
- if (signTxRequest.infoLogs)
329
- infoLogs = [...infoLogs, ...signTxRequest.infoLogs];
394
+ // ConfirmGatewayTx and check if it is successfully executed
395
+ const confirmTxRequest = yield this.findEventAndConfirmIfNeeded(srcChain, destChain, txHash, evmWalletDetails);
396
+ // If the `success` flag is false, we will return the error response
397
+ if (!(confirmTxRequest === null || confirmTxRequest === void 0 ? void 0 : confirmTxRequest.success)) {
398
+ return {
399
+ success: false,
400
+ error: confirmTxRequest.errorMessage || types_1.ApproveGatewayError.ERROR_BATCHED_COMMAND,
401
+ };
402
+ }
403
+ const { infoLogs: confirmTxLogs, commandId, confirmTx } = confirmTxRequest;
404
+ // If the `escapeAfterConfirm` flag is set to true, we will return the `confirmTx` and `infoLogs` immediately without signing the batch.
405
+ if (confirmTx && escapeAfterConfirm) {
406
+ return {
407
+ success: true,
408
+ confirmTx,
409
+ infoLogs: confirmTxLogs,
410
+ };
411
+ }
412
+ // Find the batch and sign it
413
+ const response = yield this.signAndApproveGateway(commandId, destChain, evmWalletDetails);
414
+ // If the response.success is false, we will return the error response
415
+ if (!response.success) {
416
+ return {
417
+ success: false,
418
+ error: response.error,
419
+ };
420
+ }
421
+ // Otherwise, we will return the success response
422
+ const { signCommandTx, approveTx, infoLogs: signTxLogs } = response;
423
+ return {
424
+ success: true,
425
+ confirmTx,
426
+ signCommandTx,
427
+ approveTx,
428
+ infoLogs: [...confirmTxLogs, ...signTxLogs],
429
+ };
430
+ // If more code is required here, you can add it below.
330
431
  }
331
432
  catch (e) {
332
- throw `error finding batch to sign, ${e}`;
433
+ return {
434
+ success: false,
435
+ error: e.message || types_1.ApproveGatewayError.CONFIRM_COMMAND_FAILED,
436
+ };
333
437
  }
334
- if (!(signTxRequest === null || signTxRequest === void 0 ? void 0 : signTxRequest.success))
335
- return (0, exports.GMPErrorResponse)(types_1.ApproveGatewayError.SIGN_COMMAND_FAILED, signTxRequest.errorMessage);
336
- /**find batch and manually execute if needed */
337
- let broadcastTxRequest;
438
+ });
439
+ }
440
+ signAndApproveGateway(commandId, destChain, evmWalletDetails) {
441
+ return __awaiter(this, void 0, void 0, function* () {
338
442
  try {
339
- broadcastTxRequest = yield this.findBatchAndBroadcast(commandId, destChain, _evmWalletDetails);
340
- approveTx = broadcastTxRequest.approveTx;
341
- if (broadcastTxRequest.infoLogs)
342
- infoLogs = [...infoLogs, ...broadcastTxRequest.infoLogs];
443
+ const signTxRequest = yield this.findBatchAndSignIfNeeded(commandId, destChain);
444
+ if (!(signTxRequest === null || signTxRequest === void 0 ? void 0 : signTxRequest.success)) {
445
+ return {
446
+ success: false,
447
+ error: signTxRequest.errorMessage || types_1.ApproveGatewayError.SIGN_COMMAND_FAILED,
448
+ };
449
+ }
450
+ const broadcastTxRequest = yield this.findBatchAndApproveGateway(commandId, destChain, evmWalletDetails);
451
+ if (!(broadcastTxRequest === null || broadcastTxRequest === void 0 ? void 0 : broadcastTxRequest.success)) {
452
+ return {
453
+ success: false,
454
+ error: broadcastTxRequest.errorMessage || types_1.ApproveGatewayError.ERROR_BROADCAST_EVENT,
455
+ };
456
+ }
457
+ return {
458
+ success: true,
459
+ signCommandTx: signTxRequest.signCommandTx,
460
+ approveTx: broadcastTxRequest.approveTx,
461
+ infoLogs: [...(signTxRequest.infoLogs || []), ...(broadcastTxRequest.infoLogs || [])],
462
+ };
343
463
  }
344
464
  catch (e) {
345
- throw `error finding batch to broadcast, ${e}`;
465
+ return {
466
+ success: false,
467
+ error: e.message || `Error signing and approving gateway for commandId: ${commandId}`,
468
+ };
346
469
  }
347
- if (!(broadcastTxRequest === null || broadcastTxRequest === void 0 ? void 0 : broadcastTxRequest.success))
348
- return (0, exports.GMPErrorResponse)(types_1.ApproveGatewayError.ERROR_BROADCAST_EVENT, broadcastTxRequest.errorMessage);
349
- return {
350
- success,
351
- confirmTx,
352
- signCommandTx,
353
- approveTx,
354
- infoLogs,
355
- };
356
470
  });
357
471
  }
358
472
  /**
@@ -503,7 +617,7 @@ class AxelarGMPRecoveryAPI extends AxelarRecoveryApi_1.AxelarRecoveryApi {
503
617
  // Check if given `gasTokenAddress` exists
504
618
  if (!gasTokenSymbol)
505
619
  return (0, error_1.InvalidGasTokenError)();
506
- const axelarGateway = AxelarGateway_1.AxelarGateway.create(this.environment, chain, signer.provider);
620
+ const axelarGateway = yield AxelarGateway_1.AxelarGateway.create(this.environment, chain, signer.provider);
507
621
  const gatewayGasTokenAddress = yield axelarGateway.getTokenAddress(gasTokenSymbol);
508
622
  // Check if given `gasTokenAddress` is supported by Axelar.
509
623
  if (gatewayGasTokenAddress === ethers_1.ethers.constants.AddressZero)