@lifi/sdk 1.7.1 → 2.0.0-beta.0
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.
- package/dist/Lifi.js +97 -85
- package/dist/allowance/checkAllowance.js +12 -21
- package/dist/allowance/tokenApproval.js +20 -29
- package/dist/allowance/utils.js +12 -24
- package/dist/balance/checkBalance.js +5 -14
- package/dist/balance/getTokenBalance.js +12 -21
- package/dist/balance/index.d.ts +11 -0
- package/dist/balance/index.js +8 -0
- package/dist/balance/utils.d.ts +1 -0
- package/dist/balance/utils.js +36 -38
- package/dist/cjs/Lifi.js +97 -85
- package/dist/cjs/allowance/checkAllowance.js +12 -21
- package/dist/cjs/allowance/tokenApproval.js +20 -29
- package/dist/cjs/allowance/utils.js +12 -24
- package/dist/cjs/balance/checkBalance.js +5 -14
- package/dist/cjs/balance/getTokenBalance.js +12 -21
- package/dist/cjs/balance/index.d.ts +11 -0
- package/dist/cjs/balance/index.js +8 -0
- package/dist/cjs/balance/utils.d.ts +1 -0
- package/dist/cjs/balance/utils.js +39 -39
- package/dist/cjs/connectors.js +17 -25
- package/dist/cjs/execution/ExecutionManager.js +27 -33
- package/dist/cjs/execution/StatusManager.js +13 -13
- package/dist/cjs/execution/StepExecutor.js +8 -14
- package/dist/cjs/execution/stepComparison.js +4 -14
- package/dist/cjs/execution/switchChain.js +5 -14
- package/dist/cjs/execution/utils.js +43 -50
- package/dist/cjs/helpers.d.ts +12 -1
- package/dist/cjs/helpers.js +68 -19
- package/dist/cjs/services/ApiService.d.ts +10 -9
- package/dist/cjs/services/ApiService.js +190 -152
- package/dist/cjs/services/ApiService.unit.handlers.d.ts +1 -0
- package/dist/cjs/services/ApiService.unit.handlers.js +50 -0
- package/dist/cjs/services/ChainsService.js +16 -31
- package/dist/cjs/services/ConfigService.js +6 -16
- package/dist/cjs/typeguards.js +1 -1
- package/dist/cjs/types/internal.types.d.ts +4 -1
- package/dist/cjs/utils/errors.d.ts +5 -0
- package/dist/cjs/utils/errors.js +14 -1
- package/dist/cjs/utils/multicall.js +6 -15
- package/dist/cjs/utils/parseError.d.ts +2 -2
- package/dist/cjs/utils/parseError.js +36 -38
- package/dist/cjs/utils/preRestart.js +5 -7
- package/dist/cjs/utils/utils.d.ts +0 -1
- package/dist/cjs/utils/utils.js +21 -28
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/connectors.js +15 -23
- package/dist/execution/ExecutionManager.js +27 -33
- package/dist/execution/StatusManager.js +11 -11
- package/dist/execution/StepExecutor.js +8 -14
- package/dist/execution/stepComparison.js +4 -14
- package/dist/execution/switchChain.js +5 -14
- package/dist/execution/utils.js +43 -50
- package/dist/helpers.d.ts +12 -1
- package/dist/helpers.js +65 -18
- package/dist/services/ApiService.d.ts +10 -9
- package/dist/services/ApiService.js +190 -152
- package/dist/services/ApiService.unit.handlers.d.ts +1 -0
- package/dist/services/ApiService.unit.handlers.js +44 -0
- package/dist/services/ChainsService.js +16 -31
- package/dist/services/ConfigService.js +6 -16
- package/dist/typeguards.js +1 -1
- package/dist/types/internal.types.d.ts +4 -1
- package/dist/utils/errors.d.ts +5 -0
- package/dist/utils/errors.js +12 -0
- package/dist/utils/multicall.js +6 -15
- package/dist/utils/parseError.d.ts +2 -2
- package/dist/utils/parseError.js +36 -38
- package/dist/utils/preRestart.js +5 -7
- package/dist/utils/utils.d.ts +0 -1
- package/dist/utils/utils.js +20 -26
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +23 -25
- package/CHANGELOG.md +0 -490
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { emptyExecution, } from '../types';
|
|
2
|
-
import { deepClone } from '../utils/utils';
|
|
3
2
|
import { getProcessMessage } from './utils';
|
|
4
3
|
/**
|
|
5
4
|
* Manages status updates of a route and provides various functions for tracking processes
|
|
@@ -17,7 +16,7 @@ export class StatusManager {
|
|
|
17
16
|
* @return {Execution} The initialized execution object for this step and a function to update this step
|
|
18
17
|
*/
|
|
19
18
|
this.initExecutionObject = (step) => {
|
|
20
|
-
const currentExecution = step.execution ||
|
|
19
|
+
const currentExecution = step.execution || structuredClone(emptyExecution);
|
|
21
20
|
if (!step.execution) {
|
|
22
21
|
step.execution = currentExecution;
|
|
23
22
|
step.execution.status = 'PENDING';
|
|
@@ -38,8 +37,7 @@ export class StatusManager {
|
|
|
38
37
|
* @return {Process}
|
|
39
38
|
*/
|
|
40
39
|
this.findOrCreateProcess = (step, type, status) => {
|
|
41
|
-
|
|
42
|
-
if (!((_a = step.execution) === null || _a === void 0 ? void 0 : _a.process)) {
|
|
40
|
+
if (!step.execution?.process) {
|
|
43
41
|
throw new Error("Execution hasn't been initialized.");
|
|
44
42
|
}
|
|
45
43
|
const process = step.execution.process.find((p) => p.type === type);
|
|
@@ -53,8 +51,8 @@ export class StatusManager {
|
|
|
53
51
|
const newProcess = {
|
|
54
52
|
type: type,
|
|
55
53
|
startedAt: Date.now(),
|
|
56
|
-
message: getProcessMessage(type, status
|
|
57
|
-
status: status
|
|
54
|
+
message: getProcessMessage(type, status ?? 'STARTED'),
|
|
55
|
+
status: status ?? 'STARTED',
|
|
58
56
|
};
|
|
59
57
|
step.execution.process.push(newProcess);
|
|
60
58
|
this.updateStepInRoute(step);
|
|
@@ -69,11 +67,10 @@ export class StatusManager {
|
|
|
69
67
|
* @return {Process} The update process
|
|
70
68
|
*/
|
|
71
69
|
this.updateProcess = (step, type, status, params) => {
|
|
72
|
-
var _a, _b, _c;
|
|
73
70
|
if (!step.execution) {
|
|
74
71
|
throw new Error("Can't update an empty step execution.");
|
|
75
72
|
}
|
|
76
|
-
const currentProcess =
|
|
73
|
+
const currentProcess = step?.execution?.process.find((p) => p.type === type);
|
|
77
74
|
if (!currentProcess) {
|
|
78
75
|
throw new Error("Can't find a process for the given type.");
|
|
79
76
|
}
|
|
@@ -107,8 +104,8 @@ export class StatusManager {
|
|
|
107
104
|
}
|
|
108
105
|
// Sort processes, the ones with DONE status go first
|
|
109
106
|
step.execution.process = [
|
|
110
|
-
...
|
|
111
|
-
...
|
|
107
|
+
...step?.execution?.process.filter((process) => process.status === 'DONE'),
|
|
108
|
+
...step?.execution?.process.filter((process) => process.status !== 'DONE'),
|
|
112
109
|
];
|
|
113
110
|
this.updateStepInRoute(step); // updates the step in the route
|
|
114
111
|
return currentProcess;
|
|
@@ -157,7 +154,10 @@ export class StatusManager {
|
|
|
157
154
|
}
|
|
158
155
|
step.execution.status = status;
|
|
159
156
|
if (receipt) {
|
|
160
|
-
step.execution =
|
|
157
|
+
step.execution = {
|
|
158
|
+
...step.execution,
|
|
159
|
+
...receipt,
|
|
160
|
+
};
|
|
161
161
|
}
|
|
162
162
|
this.updateStepInRoute(step);
|
|
163
163
|
return step;
|
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import { ExecutionManager } from './ExecutionManager';
|
|
11
2
|
import { switchChain } from './switchChain';
|
|
12
3
|
// Please be careful when changing the defaults as it may break the behavior (e.g., background execution)
|
|
@@ -20,7 +11,10 @@ export class StepExecutor {
|
|
|
20
11
|
this.allowUserInteraction = true;
|
|
21
12
|
this.executionStopped = false;
|
|
22
13
|
this.setInteraction = (settings) => {
|
|
23
|
-
const interactionSettings =
|
|
14
|
+
const interactionSettings = {
|
|
15
|
+
...defaultInteractionSettings,
|
|
16
|
+
...settings,
|
|
17
|
+
};
|
|
24
18
|
this.allowUserInteraction = interactionSettings.allowInteraction;
|
|
25
19
|
this.executionManager.allowInteraction(interactionSettings.allowInteraction);
|
|
26
20
|
this.statusManager.allowUpdates(interactionSettings.allowUpdates);
|
|
@@ -32,9 +26,9 @@ export class StepExecutor {
|
|
|
32
26
|
this.checkChain = () => {
|
|
33
27
|
throw new Error('checkChain is not implemented.');
|
|
34
28
|
};
|
|
35
|
-
this.executeStep = (signer, step) =>
|
|
29
|
+
this.executeStep = async (signer, step) => {
|
|
36
30
|
// Make sure that the chain is still correct
|
|
37
|
-
const updatedSigner =
|
|
31
|
+
const updatedSigner = await switchChain(signer, this.statusManager, step, this.settings.switchChainHook, this.allowUserInteraction);
|
|
38
32
|
if (!updatedSigner) {
|
|
39
33
|
// Chain switch was not successful, stop execution here
|
|
40
34
|
return step;
|
|
@@ -46,9 +40,9 @@ export class StepExecutor {
|
|
|
46
40
|
settings: this.settings,
|
|
47
41
|
statusManager: this.statusManager,
|
|
48
42
|
};
|
|
49
|
-
|
|
43
|
+
await this.executionManager.execute(parameters);
|
|
50
44
|
return step;
|
|
51
|
-
}
|
|
45
|
+
};
|
|
52
46
|
this.executionManager = new ExecutionManager();
|
|
53
47
|
this.statusManager = statusManager;
|
|
54
48
|
this.settings = settings;
|
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import { LifiErrorCode, TransactionError } from '../utils/errors';
|
|
11
2
|
import { checkStepSlippageThreshold } from './utils';
|
|
12
3
|
/**
|
|
@@ -20,16 +11,15 @@ import { checkStepSlippageThreshold } from './utils';
|
|
|
20
11
|
* @param acceptSlippageUpdateHook
|
|
21
12
|
* @param allowUserInteraction
|
|
22
13
|
*/
|
|
23
|
-
export const stepComparison = (statusManager, oldStep, newStep, settings, allowUserInteraction) =>
|
|
24
|
-
var _a;
|
|
14
|
+
export const stepComparison = async (statusManager, oldStep, newStep, settings, allowUserInteraction) => {
|
|
25
15
|
// Check if changed exchange rate is in the range of slippage threshold
|
|
26
16
|
if (checkStepSlippageThreshold(oldStep, newStep)) {
|
|
27
17
|
return statusManager.updateStepInRoute(newStep);
|
|
28
18
|
}
|
|
29
|
-
const acceptExchangeRateUpdateHook =
|
|
19
|
+
const acceptExchangeRateUpdateHook = settings.acceptExchangeRateUpdateHook ?? settings.acceptSlippageUpdateHook;
|
|
30
20
|
let allowStepUpdate;
|
|
31
21
|
if (allowUserInteraction) {
|
|
32
|
-
allowStepUpdate =
|
|
22
|
+
allowStepUpdate = await acceptExchangeRateUpdateHook({
|
|
33
23
|
oldToAmount: oldStep.estimate.toAmount,
|
|
34
24
|
newToAmount: newStep.estimate.toAmount,
|
|
35
25
|
toToken: newStep.action.toToken,
|
|
@@ -43,4 +33,4 @@ export const stepComparison = (statusManager, oldStep, newStep, settings, allowU
|
|
|
43
33
|
The exchange rate has changed and the previous estimation can not be fulfilled due to value loss.`);
|
|
44
34
|
}
|
|
45
35
|
return statusManager.updateStepInRoute(newStep);
|
|
46
|
-
}
|
|
36
|
+
};
|
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import { LifiErrorCode, ProviderError } from '../utils/errors';
|
|
11
2
|
/**
|
|
12
3
|
* This method checks whether the signer is configured for the correct chain.
|
|
@@ -19,9 +10,9 @@ import { LifiErrorCode, ProviderError } from '../utils/errors';
|
|
|
19
10
|
* @param switchChainHook
|
|
20
11
|
* @param allowUserInteraction
|
|
21
12
|
*/
|
|
22
|
-
export const switchChain = (signer, statusManager, step, switchChainHook, allowUserInteraction) =>
|
|
13
|
+
export const switchChain = async (signer, statusManager, step, switchChainHook, allowUserInteraction) => {
|
|
23
14
|
// if we are already on the correct chain we can proceed directly
|
|
24
|
-
if ((
|
|
15
|
+
if ((await signer.getChainId()) === step.action.fromChainId) {
|
|
25
16
|
return signer;
|
|
26
17
|
}
|
|
27
18
|
// -> set status message
|
|
@@ -32,8 +23,8 @@ export const switchChain = (signer, statusManager, step, switchChainHook, allowU
|
|
|
32
23
|
return;
|
|
33
24
|
}
|
|
34
25
|
try {
|
|
35
|
-
const updatedSigner =
|
|
36
|
-
const updatedChainId =
|
|
26
|
+
const updatedSigner = await switchChainHook(step.action.fromChainId);
|
|
27
|
+
const updatedChainId = await updatedSigner?.getChainId();
|
|
37
28
|
if (updatedChainId !== step.action.fromChainId) {
|
|
38
29
|
throw new ProviderError(LifiErrorCode.ChainSwitchError, 'Chain switch required.');
|
|
39
30
|
}
|
|
@@ -51,4 +42,4 @@ export const switchChain = (signer, statusManager, step, switchChainHook, allowU
|
|
|
51
42
|
statusManager.updateExecution(step, 'FAILED');
|
|
52
43
|
throw error;
|
|
53
44
|
}
|
|
54
|
-
}
|
|
45
|
+
};
|
package/dist/execution/utils.js
CHANGED
|
@@ -1,62 +1,55 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import BigNumber from 'bignumber.js';
|
|
11
2
|
import ApiService from '../services/ApiService';
|
|
12
3
|
import { ServerError } from '../utils/errors';
|
|
13
4
|
import { repeatUntilDone } from '../utils/utils';
|
|
14
5
|
const TRANSACTION_HASH_OBSERVERS = {};
|
|
15
|
-
export function waitForReceivingTransaction(txHash, statusManager, processType, step) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
catch (e) {
|
|
28
|
-
console.debug('Fetching status from backend failed.', e);
|
|
29
|
-
return resolve(undefined);
|
|
30
|
-
}
|
|
31
|
-
switch (statusResponse.status) {
|
|
32
|
-
case 'DONE':
|
|
33
|
-
return resolve(statusResponse);
|
|
34
|
-
case 'PENDING':
|
|
35
|
-
statusManager === null || statusManager === void 0 ? void 0 : statusManager.updateProcess(step, processType, 'PENDING', Object.assign({ substatus: statusResponse.substatus, substatusMessage: statusResponse.substatusMessage ||
|
|
36
|
-
getSubstatusMessage(statusResponse.status, statusResponse.substatus) }, (statusResponse.bridgeExplorerLink && {
|
|
37
|
-
txLink: statusResponse.bridgeExplorerLink,
|
|
38
|
-
})));
|
|
39
|
-
return resolve(undefined);
|
|
40
|
-
case 'NOT_FOUND':
|
|
41
|
-
return resolve(undefined);
|
|
42
|
-
case 'FAILED':
|
|
43
|
-
default:
|
|
44
|
-
return reject();
|
|
45
|
-
}
|
|
46
|
-
}));
|
|
47
|
-
let status;
|
|
48
|
-
if (txHash in TRANSACTION_HASH_OBSERVERS) {
|
|
49
|
-
status = yield TRANSACTION_HASH_OBSERVERS[txHash];
|
|
6
|
+
export async function waitForReceivingTransaction(txHash, statusManager, processType, step) {
|
|
7
|
+
const getStatus = () => new Promise(async (resolve, reject) => {
|
|
8
|
+
let statusResponse;
|
|
9
|
+
try {
|
|
10
|
+
statusResponse = await ApiService.getStatus({
|
|
11
|
+
bridge: step.tool,
|
|
12
|
+
fromChain: step.action.fromChainId,
|
|
13
|
+
toChain: step.action.toChainId,
|
|
14
|
+
txHash,
|
|
15
|
+
});
|
|
50
16
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
17
|
+
catch (e) {
|
|
18
|
+
console.debug('Fetching status from backend failed.', e);
|
|
19
|
+
return resolve(undefined);
|
|
54
20
|
}
|
|
55
|
-
|
|
56
|
-
|
|
21
|
+
switch (statusResponse.status) {
|
|
22
|
+
case 'DONE':
|
|
23
|
+
return resolve(statusResponse);
|
|
24
|
+
case 'PENDING':
|
|
25
|
+
statusManager?.updateProcess(step, processType, 'PENDING', {
|
|
26
|
+
substatus: statusResponse.substatus,
|
|
27
|
+
substatusMessage: statusResponse.substatusMessage ||
|
|
28
|
+
getSubstatusMessage(statusResponse.status, statusResponse.substatus),
|
|
29
|
+
...(statusResponse.bridgeExplorerLink && {
|
|
30
|
+
txLink: statusResponse.bridgeExplorerLink,
|
|
31
|
+
}),
|
|
32
|
+
});
|
|
33
|
+
return resolve(undefined);
|
|
34
|
+
case 'NOT_FOUND':
|
|
35
|
+
return resolve(undefined);
|
|
36
|
+
case 'FAILED':
|
|
37
|
+
default:
|
|
38
|
+
return reject();
|
|
57
39
|
}
|
|
58
|
-
return status;
|
|
59
40
|
});
|
|
41
|
+
let status;
|
|
42
|
+
if (txHash in TRANSACTION_HASH_OBSERVERS) {
|
|
43
|
+
status = await TRANSACTION_HASH_OBSERVERS[txHash];
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
TRANSACTION_HASH_OBSERVERS[txHash] = repeatUntilDone(getStatus, 5000);
|
|
47
|
+
status = await TRANSACTION_HASH_OBSERVERS[txHash];
|
|
48
|
+
}
|
|
49
|
+
if (!status.receiving) {
|
|
50
|
+
throw new ServerError("Status doesn't contain receiving information.");
|
|
51
|
+
}
|
|
52
|
+
return status;
|
|
60
53
|
}
|
|
61
54
|
const processMessages = {
|
|
62
55
|
TOKEN_ALLOWANCE: {
|
package/dist/helpers.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Token } from '@lifi/types';
|
|
1
|
+
import { Route, Step, Token } from '@lifi/types';
|
|
2
2
|
/**
|
|
3
3
|
* Predefined hook that decrypts calldata using EIP-1193 compliant wallet functions.
|
|
4
4
|
* @param {string} walletAddress - The wallet address of the user that should decrypt the calldata.
|
|
@@ -17,3 +17,14 @@ export declare const getEthereumPublicKeyHook: (walletAddress: string) => () =>
|
|
|
17
17
|
export declare const getRandomNumber: (min: number, max: number) => number;
|
|
18
18
|
export declare const isSameToken: (tokenA: Token, tokenB: Token) => boolean;
|
|
19
19
|
export declare const checkPackageUpdates: (packageName?: string, packageVersion?: string, disableCheck?: boolean) => Promise<void>;
|
|
20
|
+
/**
|
|
21
|
+
* Converts a quote to Route
|
|
22
|
+
* @param {Step} step - Step returned from the quote endpoint.
|
|
23
|
+
* @return {Route} - The route to be executed.
|
|
24
|
+
* @throws {ValidationError} Throws a ValidationError if the step has missing values.
|
|
25
|
+
*/
|
|
26
|
+
export declare const convertQuoteToRoute: (step: Step) => Route;
|
|
27
|
+
export declare const requestSettings: {
|
|
28
|
+
retries: number;
|
|
29
|
+
};
|
|
30
|
+
export declare const request: <T = Response>(url: string, options?: RequestInit, retries?: number) => Promise<T>;
|
package/dist/helpers.js
CHANGED
|
@@ -1,23 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
1
|
+
import { HTTPError, ValidationError } from './utils/errors';
|
|
2
|
+
import { sleep } from './utils/utils';
|
|
10
3
|
import { name, version } from './version';
|
|
11
|
-
const ethereumRequest = (method, params) =>
|
|
4
|
+
const ethereumRequest = async (method, params) => {
|
|
12
5
|
// If ethereum.request() exists, the provider is probably EIP-1193 compliant.
|
|
13
|
-
if (!
|
|
6
|
+
if (!ethereum?.request) {
|
|
14
7
|
throw new Error('Provider not available.');
|
|
15
8
|
}
|
|
16
9
|
return ethereum.request({
|
|
17
10
|
method,
|
|
18
11
|
params,
|
|
19
12
|
});
|
|
20
|
-
}
|
|
13
|
+
};
|
|
21
14
|
/**
|
|
22
15
|
* Predefined hook that decrypts calldata using EIP-1193 compliant wallet functions.
|
|
23
16
|
* @param {string} walletAddress - The wallet address of the user that should decrypt the calldata.
|
|
@@ -59,15 +52,16 @@ function semverCompare(a, b) {
|
|
|
59
52
|
caseFirst: 'upper',
|
|
60
53
|
});
|
|
61
54
|
}
|
|
62
|
-
export const checkPackageUpdates = (packageName, packageVersion, disableCheck) =>
|
|
55
|
+
export const checkPackageUpdates = async (packageName, packageVersion, disableCheck) => {
|
|
63
56
|
if (disableCheck || process.env.NODE_ENV !== 'development') {
|
|
64
57
|
return;
|
|
65
58
|
}
|
|
66
59
|
try {
|
|
67
|
-
const pkgName = packageName
|
|
68
|
-
const response =
|
|
69
|
-
const
|
|
70
|
-
const
|
|
60
|
+
const pkgName = packageName ?? name;
|
|
61
|
+
const response = await request('https://registry.npmjs.org/${pkgName}/latest');
|
|
62
|
+
const data = await response.json();
|
|
63
|
+
const latestVersion = data.version;
|
|
64
|
+
const currentVersion = packageVersion ?? version;
|
|
71
65
|
if (semverCompare(latestVersion, currentVersion)) {
|
|
72
66
|
console.warn(
|
|
73
67
|
// eslint-disable-next-line max-len
|
|
@@ -77,4 +71,57 @@ export const checkPackageUpdates = (packageName, packageVersion, disableCheck) =
|
|
|
77
71
|
catch (error) {
|
|
78
72
|
// Cannot verify version, might be network error etc. We don't bother showing anything in that case
|
|
79
73
|
}
|
|
80
|
-
}
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Converts a quote to Route
|
|
77
|
+
* @param {Step} step - Step returned from the quote endpoint.
|
|
78
|
+
* @return {Route} - The route to be executed.
|
|
79
|
+
* @throws {ValidationError} Throws a ValidationError if the step has missing values.
|
|
80
|
+
*/
|
|
81
|
+
export const convertQuoteToRoute = (step) => {
|
|
82
|
+
if (!step.estimate.fromAmountUSD) {
|
|
83
|
+
throw new ValidationError("Missing 'fromAmountUSD' in step estimate.");
|
|
84
|
+
}
|
|
85
|
+
if (!step.estimate.toAmountUSD) {
|
|
86
|
+
throw new ValidationError("Missing 'toAmountUSD' in step estimate.");
|
|
87
|
+
}
|
|
88
|
+
const lifiStep = {
|
|
89
|
+
...step,
|
|
90
|
+
type: 'lifi',
|
|
91
|
+
includedSteps: [],
|
|
92
|
+
};
|
|
93
|
+
const route = {
|
|
94
|
+
fromToken: step.action.fromToken,
|
|
95
|
+
toToken: step.action.toToken,
|
|
96
|
+
fromAmount: step.action.fromAmount,
|
|
97
|
+
toAmount: step.estimate.toAmount,
|
|
98
|
+
id: step.id,
|
|
99
|
+
fromChainId: step.action.fromToken.chainId,
|
|
100
|
+
toChainId: step.action.toToken.chainId,
|
|
101
|
+
fromAmountUSD: step.estimate.fromAmountUSD,
|
|
102
|
+
toAmountUSD: step.estimate.toAmountUSD,
|
|
103
|
+
steps: [lifiStep],
|
|
104
|
+
toAmountMin: step.estimate.toAmountMin,
|
|
105
|
+
};
|
|
106
|
+
return route;
|
|
107
|
+
};
|
|
108
|
+
export const requestSettings = {
|
|
109
|
+
retries: 1,
|
|
110
|
+
};
|
|
111
|
+
export const request = async (url, options, retries = requestSettings.retries) => {
|
|
112
|
+
try {
|
|
113
|
+
const response = await fetch(url, options);
|
|
114
|
+
if (!response.ok) {
|
|
115
|
+
throw new HTTPError(response);
|
|
116
|
+
}
|
|
117
|
+
const data = await response.json();
|
|
118
|
+
return data;
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
if (retries > 0 && error?.status === 500) {
|
|
122
|
+
await sleep(500);
|
|
123
|
+
return request(url, options, retries - 1);
|
|
124
|
+
}
|
|
125
|
+
throw error;
|
|
126
|
+
}
|
|
127
|
+
};
|
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import { ContractCallQuoteRequest, GetStatusRequest, QuoteRequest, RequestOptions, TokensRequest, TokensResponse } from '@lifi/types';
|
|
1
|
+
import { ContractCallQuoteRequest, GasRecommendationRequest, GasRecommendationResponse, GetStatusRequest, QuoteRequest, RequestOptions, TokensRequest, TokensResponse } from '@lifi/types';
|
|
2
2
|
import { ChainId, ChainKey, ExtendedChain, PossibilitiesRequest, PossibilitiesResponse, RoutesRequest, RoutesResponse, StatusResponse, Step, Token, ToolsRequest, ToolsResponse } from '../types';
|
|
3
3
|
declare const _default: {
|
|
4
|
-
getPossibilities: (request?: PossibilitiesRequest | undefined, options?: RequestOptions | undefined) => Promise<PossibilitiesResponse>;
|
|
5
|
-
getToken: (chain: ChainKey | ChainId, token: string, options?: RequestOptions | undefined) => Promise<Token>;
|
|
6
|
-
getQuote: (request: QuoteRequest, options?: RequestOptions | undefined) => Promise<Step>;
|
|
7
|
-
getContractCallQuote: (request: ContractCallQuoteRequest, options?: RequestOptions | undefined) => Promise<Step>;
|
|
8
|
-
getStatus: ({ bridge, fromChain, toChain, txHash }: GetStatusRequest, options?: RequestOptions | undefined) => Promise<StatusResponse>;
|
|
9
4
|
getChains: (options?: RequestOptions | undefined) => Promise<ExtendedChain[]>;
|
|
10
|
-
|
|
5
|
+
getContractCallQuote: (requestConfig: ContractCallQuoteRequest, options?: RequestOptions | undefined) => Promise<Step>;
|
|
6
|
+
getGasRecommendation: ({ chainId, fromChain, fromToken }: GasRecommendationRequest, options?: RequestOptions | undefined) => Promise<GasRecommendationResponse>;
|
|
7
|
+
getPossibilities: (requestConfig?: PossibilitiesRequest | undefined, options?: RequestOptions | undefined) => Promise<PossibilitiesResponse>;
|
|
8
|
+
getQuote: (requestConfig: QuoteRequest, options?: RequestOptions | undefined) => Promise<Step>;
|
|
9
|
+
getRoutes: (requestConfig: RoutesRequest, options?: RequestOptions | undefined) => Promise<RoutesResponse>;
|
|
10
|
+
getStatus: ({ bridge, fromChain, toChain, txHash }: GetStatusRequest, options?: RequestOptions | undefined) => Promise<StatusResponse>;
|
|
11
11
|
getStepTransaction: (step: Step, options?: RequestOptions | undefined) => Promise<Step>;
|
|
12
|
-
|
|
13
|
-
getTokens: (
|
|
12
|
+
getToken: (chain: ChainKey | ChainId, token: string, options?: RequestOptions | undefined) => Promise<Token>;
|
|
13
|
+
getTokens: (requestConfig?: TokensRequest | undefined, options?: RequestOptions | undefined) => Promise<TokensResponse>;
|
|
14
|
+
getTools: (requestConfig?: ToolsRequest | undefined, options?: RequestOptions | undefined) => Promise<ToolsResponse>;
|
|
14
15
|
};
|
|
15
16
|
export default _default;
|