@lifi/sdk 1.1.0 → 1.1.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.
- package/CHANGELOG.md +21 -0
- package/dist/Lifi.d.ts +1 -1
- package/dist/Lifi.js +179 -285
- package/dist/allowance/index.js +56 -148
- package/dist/allowance/utils.js +51 -116
- package/dist/balances/index.js +29 -92
- package/dist/balances/utils.js +108 -218
- package/dist/connectors.js +50 -133
- package/dist/execution/StatusManager.js +38 -40
- package/dist/execution/StepExecutor.js +54 -123
- package/dist/execution/allowance.execute.js +77 -143
- package/dist/execution/balanceCheck.execute.js +29 -74
- package/dist/execution/bridges/bridge.execute.js +132 -221
- package/dist/execution/exchanges/swap.execute.js +142 -225
- package/dist/execution/index.js +1 -17
- package/dist/execution/stepComparison.js +22 -61
- package/dist/execution/switchChain.js +33 -81
- package/dist/execution/utils.js +60 -119
- package/dist/helpers.js +15 -53
- package/dist/index.js +6 -25
- package/dist/services/ApiService.js +248 -385
- package/dist/services/ChainsService.js +29 -89
- package/dist/services/ConfigService.js +47 -86
- package/dist/typeguards.js +13 -21
- package/dist/types/ERC20.js +1 -4
- package/dist/types/index.js +4 -22
- package/dist/types/internal.types.js +1 -2
- package/dist/utils/errors.js +47 -93
- package/dist/utils/getProvider.js +3 -7
- package/dist/utils/multicall.js +61 -117
- package/dist/utils/parseError.js +73 -141
- package/dist/utils/preRestart.js +14 -21
- package/dist/utils/utils.js +47 -130
- package/package.json +3 -3
package/dist/execution/utils.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
2
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
3
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -8,114 +7,59 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
8
|
});
|
|
10
9
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
10
|
+
import BigNumber from 'bignumber.js';
|
|
11
|
+
import ApiService from '../services/ApiService';
|
|
12
|
+
import { ServerError } from '../utils/errors';
|
|
13
|
+
import { repeatUntilDone } from '../utils/utils';
|
|
14
|
+
const TRANSACTION_HASH_OBSERVERS = {};
|
|
15
|
+
export function waitForReceivingTransaction(txHash, statusManager, processType, step) {
|
|
16
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
17
|
+
const getStatus = () => new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
18
|
+
let statusResponse;
|
|
19
|
+
try {
|
|
20
|
+
statusResponse = yield ApiService.getStatus({
|
|
21
|
+
bridge: step.tool,
|
|
22
|
+
fromChain: step.action.fromChainId,
|
|
23
|
+
toChain: step.action.toChainId,
|
|
24
|
+
txHash,
|
|
25
|
+
});
|
|
32
26
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
return __generator(this, function (_a) {
|
|
53
|
-
switch (_a.label) {
|
|
54
|
-
case 0:
|
|
55
|
-
getStatus = function () {
|
|
56
|
-
return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
|
|
57
|
-
var statusResponse, e_1;
|
|
58
|
-
return __generator(this, function (_a) {
|
|
59
|
-
switch (_a.label) {
|
|
60
|
-
case 0:
|
|
61
|
-
_a.trys.push([0, 2, , 3]);
|
|
62
|
-
return [4 /*yield*/, ApiService_1.default.getStatus({
|
|
63
|
-
bridge: step.tool,
|
|
64
|
-
fromChain: step.action.fromChainId,
|
|
65
|
-
toChain: step.action.toChainId,
|
|
66
|
-
txHash: txHash,
|
|
67
|
-
})];
|
|
68
|
-
case 1:
|
|
69
|
-
statusResponse = _a.sent();
|
|
70
|
-
return [3 /*break*/, 3];
|
|
71
|
-
case 2:
|
|
72
|
-
e_1 = _a.sent();
|
|
73
|
-
console.debug('Fetching status from backend failed.', e_1);
|
|
74
|
-
return [2 /*return*/, resolve(undefined)];
|
|
75
|
-
case 3:
|
|
76
|
-
switch (statusResponse.status) {
|
|
77
|
-
case 'DONE':
|
|
78
|
-
return [2 /*return*/, resolve(statusResponse)];
|
|
79
|
-
case 'PENDING':
|
|
80
|
-
statusManager === null || statusManager === void 0 ? void 0 : statusManager.updateProcess(step, processType, 'PENDING', {
|
|
81
|
-
substatus: statusResponse.substatus,
|
|
82
|
-
substatusMessage: statusResponse.substatusMessage ||
|
|
83
|
-
getSubstatusMessage(statusResponse.status, statusResponse.substatus),
|
|
84
|
-
});
|
|
85
|
-
return [2 /*return*/, resolve(undefined)];
|
|
86
|
-
case 'NOT_FOUND':
|
|
87
|
-
return [2 /*return*/, resolve(undefined)];
|
|
88
|
-
case 'FAILED':
|
|
89
|
-
default:
|
|
90
|
-
return [2 /*return*/, reject()];
|
|
91
|
-
}
|
|
92
|
-
return [2 /*return*/];
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
}); });
|
|
96
|
-
};
|
|
97
|
-
if (!(txHash in TRANSACTION_HASH_OBSERVERS)) return [3 /*break*/, 2];
|
|
98
|
-
return [4 /*yield*/, TRANSACTION_HASH_OBSERVERS[txHash]];
|
|
99
|
-
case 1:
|
|
100
|
-
status = _a.sent();
|
|
101
|
-
return [3 /*break*/, 4];
|
|
102
|
-
case 2:
|
|
103
|
-
TRANSACTION_HASH_OBSERVERS[txHash] = (0, utils_1.repeatUntilDone)(getStatus, 5000);
|
|
104
|
-
return [4 /*yield*/, TRANSACTION_HASH_OBSERVERS[txHash]];
|
|
105
|
-
case 3:
|
|
106
|
-
status = _a.sent();
|
|
107
|
-
_a.label = 4;
|
|
108
|
-
case 4:
|
|
109
|
-
if (!status.receiving) {
|
|
110
|
-
throw new errors_1.ServerError("Status doesn't contain receiving information.");
|
|
111
|
-
}
|
|
112
|
-
return [2 /*return*/, status];
|
|
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', {
|
|
36
|
+
substatus: statusResponse.substatus,
|
|
37
|
+
substatusMessage: statusResponse.substatusMessage ||
|
|
38
|
+
getSubstatusMessage(statusResponse.status, statusResponse.substatus),
|
|
39
|
+
});
|
|
40
|
+
return resolve(undefined);
|
|
41
|
+
case 'NOT_FOUND':
|
|
42
|
+
return resolve(undefined);
|
|
43
|
+
case 'FAILED':
|
|
44
|
+
default:
|
|
45
|
+
return reject();
|
|
113
46
|
}
|
|
114
|
-
});
|
|
47
|
+
}));
|
|
48
|
+
let status;
|
|
49
|
+
if (txHash in TRANSACTION_HASH_OBSERVERS) {
|
|
50
|
+
status = yield TRANSACTION_HASH_OBSERVERS[txHash];
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
TRANSACTION_HASH_OBSERVERS[txHash] = repeatUntilDone(getStatus, 5000);
|
|
54
|
+
status = yield TRANSACTION_HASH_OBSERVERS[txHash];
|
|
55
|
+
}
|
|
56
|
+
if (!status.receiving) {
|
|
57
|
+
throw new ServerError("Status doesn't contain receiving information.");
|
|
58
|
+
}
|
|
59
|
+
return status;
|
|
115
60
|
});
|
|
116
61
|
}
|
|
117
|
-
|
|
118
|
-
var processMessages = {
|
|
62
|
+
const processMessages = {
|
|
119
63
|
TOKEN_ALLOWANCE: {
|
|
120
64
|
STARTED: 'Setting token allowance.',
|
|
121
65
|
PENDING: 'Waiting for token allowance.',
|
|
@@ -143,7 +87,7 @@ var processMessages = {
|
|
|
143
87
|
},
|
|
144
88
|
TRANSACTION: {},
|
|
145
89
|
};
|
|
146
|
-
|
|
90
|
+
const substatusMessages = {
|
|
147
91
|
PENDING: {
|
|
148
92
|
BRIDGE_NOT_AVAILABLE: 'Bridge communication is temporarily unavailable.',
|
|
149
93
|
CHAIN_NOT_AVAILABLE: 'RPC communication is temporarily unavailable.',
|
|
@@ -161,26 +105,23 @@ var substatusMessages = {
|
|
|
161
105
|
INVALID: {},
|
|
162
106
|
NOT_FOUND: {},
|
|
163
107
|
};
|
|
164
|
-
function getProcessMessage(type, status) {
|
|
165
|
-
|
|
108
|
+
export function getProcessMessage(type, status) {
|
|
109
|
+
const processMessage = processMessages[type][status];
|
|
166
110
|
return processMessage;
|
|
167
111
|
}
|
|
168
|
-
|
|
169
|
-
function getSubstatusMessage(status, substatus) {
|
|
112
|
+
export function getSubstatusMessage(status, substatus) {
|
|
170
113
|
if (!substatus) {
|
|
171
114
|
return;
|
|
172
115
|
}
|
|
173
|
-
|
|
116
|
+
const message = substatusMessages[status][substatus];
|
|
174
117
|
return message;
|
|
175
118
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
var actualSlippage = amountDifference.dividedBy(oldEstimatedToAmount);
|
|
119
|
+
export function updatedStepMeetsSlippageConditions(oldStep, newStep) {
|
|
120
|
+
const setSlippage = new BigNumber(oldStep.action.slippage);
|
|
121
|
+
const oldEstimatedToAmount = new BigNumber(oldStep.estimate.toAmountMin);
|
|
122
|
+
const newEstimatedToAmount = new BigNumber(newStep.estimate.toAmountMin);
|
|
123
|
+
const amountDifference = oldEstimatedToAmount.minus(newEstimatedToAmount);
|
|
124
|
+
const actualSlippage = amountDifference.dividedBy(oldEstimatedToAmount);
|
|
183
125
|
return (newEstimatedToAmount.gte(oldEstimatedToAmount) &&
|
|
184
126
|
actualSlippage.lte(setSlippage));
|
|
185
127
|
}
|
|
186
|
-
exports.updatedStepMeetsSlippageConditions = updatedStepMeetsSlippageConditions;
|
package/dist/helpers.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
2
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
3
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -8,78 +7,41 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
8
|
});
|
|
10
9
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
function step(op) {
|
|
16
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
-
while (_) try {
|
|
18
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
-
switch (op[0]) {
|
|
21
|
-
case 0: case 1: t = op; break;
|
|
22
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
-
default:
|
|
26
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
-
if (t[2]) _.ops.pop();
|
|
31
|
-
_.trys.pop(); continue;
|
|
32
|
-
}
|
|
33
|
-
op = body.call(thisArg, _);
|
|
34
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
10
|
+
const ethereumRequest = (method, params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
11
|
+
// If ethereum.request() exists, the provider is probably EIP-1193 compliant.
|
|
12
|
+
if (!ethereum || !ethereum.request) {
|
|
13
|
+
throw new Error('Provider not available.');
|
|
36
14
|
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
var ethereumRequest = function (method, params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
41
|
-
return __generator(this, function (_a) {
|
|
42
|
-
// If ethereum.request() exists, the provider is probably EIP-1193 compliant.
|
|
43
|
-
if (!ethereum || !ethereum.request) {
|
|
44
|
-
throw new Error('Provider not available.');
|
|
45
|
-
}
|
|
46
|
-
return [2 /*return*/, ethereum.request({
|
|
47
|
-
method: method,
|
|
48
|
-
params: params,
|
|
49
|
-
})];
|
|
15
|
+
return ethereum.request({
|
|
16
|
+
method,
|
|
17
|
+
params,
|
|
50
18
|
});
|
|
51
|
-
});
|
|
19
|
+
});
|
|
52
20
|
/**
|
|
53
21
|
* Predefined hook that decrypts calldata using EIP-1193 compliant wallet functions.
|
|
54
22
|
* @param {string} walletAddress - The wallet address of the user that should decrypt the calldata.
|
|
55
23
|
* @return {(encryptedData: string) => Promise<any>} A function that decrypts data using EIP-1193 compliant wallet functions.
|
|
56
24
|
*/
|
|
57
|
-
|
|
58
|
-
return
|
|
25
|
+
export const getEthereumDecryptionHook = (walletAddress) => {
|
|
26
|
+
return (encryptedData) => {
|
|
59
27
|
return ethereumRequest('eth_decrypt', [encryptedData, walletAddress]);
|
|
60
28
|
};
|
|
61
29
|
};
|
|
62
|
-
exports.getEthereumDecryptionHook = getEthereumDecryptionHook;
|
|
63
30
|
/**
|
|
64
31
|
* Predefined hook that get the public encryption key of a user using EIP-1193 compliant wallet functions.
|
|
65
32
|
* @param {string} walletAddress - The wallet address of the user.
|
|
66
33
|
* @return {(walletAddress: string) => () => Promise<any>} A function that return the public encryption key using EIP-1193 compliant wallet functions.
|
|
67
34
|
*/
|
|
68
|
-
|
|
69
|
-
return
|
|
35
|
+
export const getEthereumPublicKeyHook = (walletAddress) => {
|
|
36
|
+
return () => {
|
|
70
37
|
return ethereumRequest('eth_getEncryptionPublicKey', [walletAddress]);
|
|
71
38
|
};
|
|
72
39
|
};
|
|
73
|
-
exports.getEthereumPublicKeyHook = getEthereumPublicKeyHook;
|
|
74
40
|
/**
|
|
75
41
|
* Returns a random number between min (inclusive) and max (inclusive)
|
|
76
42
|
*/
|
|
77
|
-
|
|
43
|
+
export const getRandomNumber = (min, max) => {
|
|
78
44
|
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
79
45
|
};
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return tokenA.chainId === tokenB.chainId &&
|
|
83
|
-
tokenA.address.toLowerCase() === tokenB.address.toLowerCase();
|
|
84
|
-
};
|
|
85
|
-
exports.isSameToken = isSameToken;
|
|
46
|
+
export const isSameToken = (tokenA, tokenB) => tokenA.chainId === tokenB.chainId &&
|
|
47
|
+
tokenA.address.toLowerCase() === tokenB.address.toLowerCase();
|
package/dist/index.js
CHANGED
|
@@ -1,27 +1,8 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
-
};
|
|
19
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
1
|
// expose types and helpers
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
2
|
+
import LIFI from './Lifi';
|
|
3
|
+
export * from './execution';
|
|
4
|
+
export * from './helpers';
|
|
5
|
+
export * from './types';
|
|
6
|
+
export * from './utils/errors';
|
|
26
7
|
// expose sdk
|
|
27
|
-
|
|
8
|
+
export default LIFI;
|