@lifi/sdk 1.0.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.
Files changed (63) hide show
  1. package/CHANGELOG.md +354 -0
  2. package/LICENSE +1 -0
  3. package/README.md +16 -0
  4. package/dist/Lifi.d.ts +186 -0
  5. package/dist/Lifi.js +461 -0
  6. package/dist/allowance/index.d.ts +22 -0
  7. package/dist/allowance/index.js +160 -0
  8. package/dist/allowance/utils.d.ts +14 -0
  9. package/dist/allowance/utils.js +137 -0
  10. package/dist/balances/index.d.ts +11 -0
  11. package/dist/balances/index.js +104 -0
  12. package/dist/balances/utils.d.ts +5 -0
  13. package/dist/balances/utils.js +255 -0
  14. package/dist/connectors.d.ts +6 -0
  15. package/dist/connectors.js +150 -0
  16. package/dist/execution/StatusManager.d.ts +63 -0
  17. package/dist/execution/StatusManager.js +150 -0
  18. package/dist/execution/StepExecutor.d.ts +15 -0
  19. package/dist/execution/StepExecutor.js +139 -0
  20. package/dist/execution/allowance.execute.d.ts +4 -0
  21. package/dist/execution/allowance.execute.js +156 -0
  22. package/dist/execution/balanceCheck.execute.d.ts +3 -0
  23. package/dist/execution/balanceCheck.execute.js +86 -0
  24. package/dist/execution/bridges/bridge.execute.d.ts +7 -0
  25. package/dist/execution/bridges/bridge.execute.js +218 -0
  26. package/dist/execution/exchanges/swap.execute.d.ts +7 -0
  27. package/dist/execution/exchanges/swap.execute.js +222 -0
  28. package/dist/execution/index.d.ts +1 -0
  29. package/dist/execution/index.js +17 -0
  30. package/dist/execution/switchChain.d.ts +16 -0
  31. package/dist/execution/switchChain.js +102 -0
  32. package/dist/execution/utils.d.ts +5 -0
  33. package/dist/execution/utils.js +175 -0
  34. package/dist/helpers.d.ts +18 -0
  35. package/dist/helpers.js +85 -0
  36. package/dist/index.d.ts +6 -0
  37. package/dist/index.js +27 -0
  38. package/dist/services/ApiService.d.ts +14 -0
  39. package/dist/services/ApiService.js +350 -0
  40. package/dist/services/ChainsService.d.ts +11 -0
  41. package/dist/services/ChainsService.js +108 -0
  42. package/dist/services/ConfigService.d.ts +23 -0
  43. package/dist/services/ConfigService.js +133 -0
  44. package/dist/typeguards.d.ts +4 -0
  45. package/dist/typeguards.js +55 -0
  46. package/dist/types/ERC20.d.ts +22 -0
  47. package/dist/types/ERC20.js +53 -0
  48. package/dist/types/index.d.ts +4 -0
  49. package/dist/types/index.js +22 -0
  50. package/dist/types/internal.types.d.ts +75 -0
  51. package/dist/types/internal.types.js +2 -0
  52. package/dist/utils/errors.d.ts +73 -0
  53. package/dist/utils/errors.js +147 -0
  54. package/dist/utils/getProvider.d.ts +3 -0
  55. package/dist/utils/getProvider.js +11 -0
  56. package/dist/utils/multicall.d.ts +10 -0
  57. package/dist/utils/multicall.js +111 -0
  58. package/dist/utils/multicallAbi.json +313 -0
  59. package/dist/utils/parseError.d.ts +37 -0
  60. package/dist/utils/parseError.js +184 -0
  61. package/dist/utils/utils.d.ts +26 -0
  62. package/dist/utils/utils.js +188 -0
  63. package/package.json +90 -0
@@ -0,0 +1,175 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
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 };
36
+ }
37
+ };
38
+ var __importDefault = (this && this.__importDefault) || function (mod) {
39
+ return (mod && mod.__esModule) ? mod : { "default": mod };
40
+ };
41
+ Object.defineProperty(exports, "__esModule", { value: true });
42
+ exports.getSubstatusMessage = exports.getProcessMessage = exports.waitForReceivingTransaction = void 0;
43
+ var ApiService_1 = __importDefault(require("../services/ApiService"));
44
+ var errors_1 = require("../utils/errors");
45
+ var utils_1 = require("../utils/utils");
46
+ var TRANSACTION_HASH_OBSERVERS = {};
47
+ function waitForReceivingTransaction(txHash, statusManager, processType, step) {
48
+ return __awaiter(this, void 0, void 0, function () {
49
+ var getStatus, status;
50
+ var _this = this;
51
+ return __generator(this, function (_a) {
52
+ switch (_a.label) {
53
+ case 0:
54
+ getStatus = function () {
55
+ return new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () {
56
+ var statusResponse, e_1;
57
+ return __generator(this, function (_a) {
58
+ switch (_a.label) {
59
+ case 0:
60
+ _a.trys.push([0, 2, , 3]);
61
+ return [4 /*yield*/, ApiService_1.default.getStatus({
62
+ bridge: step.tool,
63
+ fromChain: step.action.fromChainId,
64
+ toChain: step.action.toChainId,
65
+ txHash: txHash,
66
+ })];
67
+ case 1:
68
+ statusResponse = _a.sent();
69
+ return [3 /*break*/, 3];
70
+ case 2:
71
+ e_1 = _a.sent();
72
+ console.debug('Fetching status from backend failed.', e_1);
73
+ return [2 /*return*/, resolve(undefined)];
74
+ case 3:
75
+ switch (statusResponse.status) {
76
+ case 'DONE':
77
+ return [2 /*return*/, resolve(statusResponse)];
78
+ case 'PENDING':
79
+ statusManager === null || statusManager === void 0 ? void 0 : statusManager.updateProcess(step, processType, 'PENDING', {
80
+ substatus: statusResponse.substatus,
81
+ substatusMessage: statusResponse.substatusMessage ||
82
+ getSubstatusMessage(statusResponse.status, statusResponse.substatus),
83
+ });
84
+ return [2 /*return*/, resolve(undefined)];
85
+ case 'NOT_FOUND':
86
+ return [2 /*return*/, resolve(undefined)];
87
+ case 'FAILED':
88
+ default:
89
+ return [2 /*return*/, reject()];
90
+ }
91
+ return [2 /*return*/];
92
+ }
93
+ });
94
+ }); });
95
+ };
96
+ if (!(txHash in TRANSACTION_HASH_OBSERVERS)) return [3 /*break*/, 2];
97
+ return [4 /*yield*/, TRANSACTION_HASH_OBSERVERS[txHash]];
98
+ case 1:
99
+ status = _a.sent();
100
+ return [3 /*break*/, 4];
101
+ case 2:
102
+ TRANSACTION_HASH_OBSERVERS[txHash] = (0, utils_1.repeatUntilDone)(getStatus, 5000);
103
+ return [4 /*yield*/, TRANSACTION_HASH_OBSERVERS[txHash]];
104
+ case 3:
105
+ status = _a.sent();
106
+ _a.label = 4;
107
+ case 4:
108
+ if (!status.receiving) {
109
+ throw new errors_1.ServerError("Status doesn't contain receiving information.");
110
+ }
111
+ return [2 /*return*/, status];
112
+ }
113
+ });
114
+ });
115
+ }
116
+ exports.waitForReceivingTransaction = waitForReceivingTransaction;
117
+ var processMessages = {
118
+ TOKEN_ALLOWANCE: {
119
+ STARTED: 'Setting token allowance.',
120
+ PENDING: 'Waiting for token allowance approval.',
121
+ DONE: 'Token allowance approved.',
122
+ },
123
+ SWITCH_CHAIN: {
124
+ PENDING: 'Chain switch required.',
125
+ DONE: 'Chain switched successfully.',
126
+ },
127
+ SWAP: {
128
+ STARTED: 'Preparing swap.',
129
+ ACTION_REQUIRED: 'Please sign the transaction.',
130
+ PENDING: 'Swapping.',
131
+ DONE: 'Swap completed.',
132
+ },
133
+ CROSS_CHAIN: {
134
+ STARTED: 'Preparing transaction.',
135
+ ACTION_REQUIRED: 'Please sign the transaction.',
136
+ PENDING: 'Waiting for transaction.',
137
+ DONE: 'Transaction approved.',
138
+ },
139
+ RECEIVING_CHAIN: {
140
+ PENDING: 'Waiting for receiving chain.',
141
+ DONE: 'Funds received.',
142
+ },
143
+ TRANSACTION: {},
144
+ };
145
+ var substatusMessages = {
146
+ PENDING: {
147
+ BRIDGE_NOT_AVAILABLE: 'Bridge communication is temporarily unavailable.',
148
+ CHAIN_NOT_AVAILABLE: 'RPC communication is temporarily unavailable.',
149
+ NOT_PROCESSABLE_REFUND_NEEDED: 'The transfer cannot be completed successfully. A refund operation is required.',
150
+ UNKNOWN_ERROR: 'An unexpected error occurred. Please seek assistance in the LI.FI discord server.',
151
+ WAIT_SOURCE_CONFIRMATIONS: 'The bridge deposit has been received. The bridge is waiting for more confirmations to start the off-chain logic.',
152
+ WAIT_DESTINATION_TRANSACTION: 'The bridge off-chain logic is being executed. Wait for the transaction to appear on the destination chain.',
153
+ },
154
+ DONE: {
155
+ PARTIAL: 'Some of the received tokens are not the requested destination tokens.',
156
+ REFUNDED: 'The tokens were refunded to the sender address.',
157
+ COMPLETED: 'The transfer is complete.',
158
+ },
159
+ FAILED: {},
160
+ INVALID: {},
161
+ NOT_FOUND: {},
162
+ };
163
+ function getProcessMessage(type, status) {
164
+ var processMessage = processMessages[type][status];
165
+ return processMessage;
166
+ }
167
+ exports.getProcessMessage = getProcessMessage;
168
+ function getSubstatusMessage(status, substatus) {
169
+ if (!substatus) {
170
+ return;
171
+ }
172
+ var message = substatusMessages[status][substatus];
173
+ return message;
174
+ }
175
+ exports.getSubstatusMessage = getSubstatusMessage;
@@ -0,0 +1,18 @@
1
+ import { Token } from '@lifi/types';
2
+ /**
3
+ * Predefined hook that decrypts calldata using EIP-1193 compliant wallet functions.
4
+ * @param {string} walletAddress - The wallet address of the user that should decrypt the calldata.
5
+ * @return {(encryptedData: string) => Promise<any>} A function that decrypts data using EIP-1193 compliant wallet functions.
6
+ */
7
+ export declare const getEthereumDecryptionHook: (walletAddress: string) => (encryptedData: string) => Promise<string>;
8
+ /**
9
+ * Predefined hook that get the public encryption key of a user using EIP-1193 compliant wallet functions.
10
+ * @param {string} walletAddress - The wallet address of the user.
11
+ * @return {(walletAddress: string) => () => Promise<any>} A function that return the public encryption key using EIP-1193 compliant wallet functions.
12
+ */
13
+ export declare const getEthereumPublicKeyHook: (walletAddress: string) => () => Promise<string>;
14
+ /**
15
+ * Returns a random number between min (inclusive) and max (inclusive)
16
+ */
17
+ export declare const getRandomNumber: (min: number, max: number) => number;
18
+ export declare const isSameToken: (tokenA: Token, tokenB: Token) => boolean;
@@ -0,0 +1,85 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
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 };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.isSameToken = exports.getRandomNumber = exports.getEthereumPublicKeyHook = exports.getEthereumDecryptionHook = void 0;
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
+ })];
50
+ });
51
+ }); };
52
+ /**
53
+ * Predefined hook that decrypts calldata using EIP-1193 compliant wallet functions.
54
+ * @param {string} walletAddress - The wallet address of the user that should decrypt the calldata.
55
+ * @return {(encryptedData: string) => Promise<any>} A function that decrypts data using EIP-1193 compliant wallet functions.
56
+ */
57
+ var getEthereumDecryptionHook = function (walletAddress) {
58
+ return function (encryptedData) {
59
+ return ethereumRequest('eth_decrypt', [encryptedData, walletAddress]);
60
+ };
61
+ };
62
+ exports.getEthereumDecryptionHook = getEthereumDecryptionHook;
63
+ /**
64
+ * Predefined hook that get the public encryption key of a user using EIP-1193 compliant wallet functions.
65
+ * @param {string} walletAddress - The wallet address of the user.
66
+ * @return {(walletAddress: string) => () => Promise<any>} A function that return the public encryption key using EIP-1193 compliant wallet functions.
67
+ */
68
+ var getEthereumPublicKeyHook = function (walletAddress) {
69
+ return function () {
70
+ return ethereumRequest('eth_getEncryptionPublicKey', [walletAddress]);
71
+ };
72
+ };
73
+ exports.getEthereumPublicKeyHook = getEthereumPublicKeyHook;
74
+ /**
75
+ * Returns a random number between min (inclusive) and max (inclusive)
76
+ */
77
+ var getRandomNumber = function (min, max) {
78
+ return Math.floor(Math.random() * (max - min + 1) + min);
79
+ };
80
+ exports.getRandomNumber = getRandomNumber;
81
+ var isSameToken = function (tokenA, tokenB) {
82
+ return tokenA.chainId === tokenB.chainId &&
83
+ tokenA.address.toLowerCase() === tokenB.address.toLowerCase();
84
+ };
85
+ exports.isSameToken = isSameToken;
@@ -0,0 +1,6 @@
1
+ import LIFI from './Lifi';
2
+ export * from './execution';
3
+ export * from './helpers';
4
+ export * from './types';
5
+ export * from './utils/errors';
6
+ export default LIFI;
package/dist/index.js ADDED
@@ -0,0 +1,27 @@
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
+ // expose types and helpers
21
+ var Lifi_1 = __importDefault(require("./Lifi"));
22
+ __exportStar(require("./execution"), exports);
23
+ __exportStar(require("./helpers"), exports);
24
+ __exportStar(require("./types"), exports);
25
+ __exportStar(require("./utils/errors"), exports);
26
+ // expose sdk
27
+ exports.default = Lifi_1.default;
@@ -0,0 +1,14 @@
1
+ import { GetStatusRequest, QuoteRequest, RequestOptions, TokensRequest, TokensResponse } from '@lifi/types';
2
+ import { ChainId, ChainKey, PossibilitiesRequest, PossibilitiesResponse, RoutesRequest, RoutesResponse, StatusResponse, Step, Token, ToolsRequest, ToolsResponse } from '../types';
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: ({ fromChain, fromToken, fromAddress, fromAmount, toChain, toToken, toAddress, order, slippage, integrator, referrer, allowBridges, denyBridges, preferBridges, allowExchanges, denyExchanges, preferExchanges, }: QuoteRequest, options?: RequestOptions | undefined) => Promise<Step>;
7
+ getStatus: ({ bridge, fromChain, toChain, txHash }: GetStatusRequest, options?: RequestOptions | undefined) => Promise<StatusResponse>;
8
+ getChains: (options?: RequestOptions | undefined) => Promise<import("@lifi/types").EVMChain[]>;
9
+ getRoutes: (request: RoutesRequest, options?: RequestOptions | undefined) => Promise<RoutesResponse>;
10
+ getStepTransaction: (step: Step, options?: RequestOptions | undefined) => Promise<Step>;
11
+ getTools: (request?: ToolsRequest | undefined, options?: RequestOptions | undefined) => Promise<ToolsResponse>;
12
+ getTokens: (request?: TokensRequest | undefined, options?: RequestOptions | undefined) => Promise<TokensResponse>;
13
+ };
14
+ export default _default;