@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,188 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
+ return new (P || (P = Promise))(function (resolve, reject) {
16
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
17
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
18
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
19
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
20
+ });
21
+ };
22
+ var __generator = (this && this.__generator) || function (thisArg, body) {
23
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
+ function verb(n) { return function (v) { return step([n, v]); }; }
26
+ function step(op) {
27
+ if (f) throw new TypeError("Generator is already executing.");
28
+ while (_) try {
29
+ 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;
30
+ if (y = 0, t) op = [op[0] & 2, t.value];
31
+ switch (op[0]) {
32
+ case 0: case 1: t = op; break;
33
+ case 4: _.label++; return { value: op[1], done: false };
34
+ case 5: _.label++; y = op[1]; op = [0]; continue;
35
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
+ default:
37
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
+ if (t[2]) _.ops.pop();
42
+ _.trys.pop(); continue;
43
+ }
44
+ op = body.call(thisArg, _);
45
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
+ }
48
+ };
49
+ var __importDefault = (this && this.__importDefault) || function (mod) {
50
+ return (mod && mod.__esModule) ? mod : { "default": mod };
51
+ };
52
+ Object.defineProperty(exports, "__esModule", { value: true });
53
+ exports.isNativeTokenAddress = exports.isZeroAddress = exports.loadTransactionReceipt = exports.repeatUntilDone = exports.formatTokenAmountOnly = exports.splitListIntoChunks = exports.personalizeStep = exports.sleep = exports.deepClone = void 0;
54
+ var bignumber_js_1 = __importDefault(require("bignumber.js"));
55
+ var ethers_1 = require("ethers");
56
+ var connectors_1 = require("../connectors");
57
+ var deepClone = function (src) {
58
+ return JSON.parse(JSON.stringify(src));
59
+ };
60
+ exports.deepClone = deepClone;
61
+ var sleep = function (mills) {
62
+ return new Promise(function (resolve) {
63
+ setTimeout(resolve, mills);
64
+ });
65
+ };
66
+ exports.sleep = sleep;
67
+ var personalizeStep = function (signer, step) { return __awaiter(void 0, void 0, void 0, function () {
68
+ var address, fromAddress, toAddress;
69
+ return __generator(this, function (_a) {
70
+ switch (_a.label) {
71
+ case 0:
72
+ if (step.action.toAddress && step.action.fromAddress) {
73
+ return [2 /*return*/, step];
74
+ }
75
+ return [4 /*yield*/, signer.getAddress()];
76
+ case 1:
77
+ address = _a.sent();
78
+ fromAddress = step.action.fromAddress || address;
79
+ toAddress = step.action.toAddress || address;
80
+ return [2 /*return*/, __assign(__assign({}, step), { action: __assign(__assign({}, step.action), { fromAddress: fromAddress, toAddress: toAddress }) })];
81
+ }
82
+ });
83
+ }); };
84
+ exports.personalizeStep = personalizeStep;
85
+ var splitListIntoChunks = function (list, chunkSize) {
86
+ return list.reduce(function (resultList, item, index) {
87
+ var chunkIndex = Math.floor(index / chunkSize);
88
+ if (!resultList[chunkIndex]) {
89
+ resultList[chunkIndex] = []; // start a new chunk
90
+ }
91
+ resultList[chunkIndex].push(item);
92
+ return resultList;
93
+ }, []);
94
+ };
95
+ exports.splitListIntoChunks = splitListIntoChunks;
96
+ var formatTokenAmountOnly = function (token, amount) {
97
+ if (!amount) {
98
+ return '0.0';
99
+ }
100
+ var floated;
101
+ if (typeof amount === 'string') {
102
+ if (amount === '0') {
103
+ return '0.0';
104
+ }
105
+ floated = new bignumber_js_1.default(amount).shiftedBy(-token.decimals);
106
+ }
107
+ else {
108
+ floated = amount;
109
+ if (floated.isZero()) {
110
+ return '0.0';
111
+ }
112
+ }
113
+ // show at least 4 decimal places and at least two non-zero digests
114
+ var decimalPlaces = 3;
115
+ while (floated.lt(1 / Math.pow(10, decimalPlaces))) {
116
+ decimalPlaces++;
117
+ }
118
+ return floated.toFixed(decimalPlaces + 1, 1);
119
+ };
120
+ exports.formatTokenAmountOnly = formatTokenAmountOnly;
121
+ /**
122
+ * Repeatedly calls a given asynchronous function until it resolves with a value
123
+ * @param toRepeat The function that should be repeated
124
+ * @param timeout The timeout in milliseconds between retries, defaults to 5000
125
+ * @returns The result of the toRepeat function
126
+ */
127
+ var repeatUntilDone = function (toRepeat, timeout) {
128
+ if (timeout === void 0) { timeout = 5000; }
129
+ return __awaiter(void 0, void 0, void 0, function () {
130
+ var result;
131
+ return __generator(this, function (_a) {
132
+ switch (_a.label) {
133
+ case 0:
134
+ if (!!result) return [3 /*break*/, 4];
135
+ return [4 /*yield*/, toRepeat()];
136
+ case 1:
137
+ result = _a.sent();
138
+ if (!!result) return [3 /*break*/, 3];
139
+ return [4 /*yield*/, (0, exports.sleep)(timeout)];
140
+ case 2:
141
+ _a.sent();
142
+ _a.label = 3;
143
+ case 3: return [3 /*break*/, 0];
144
+ case 4: return [2 /*return*/, result];
145
+ }
146
+ });
147
+ });
148
+ };
149
+ exports.repeatUntilDone = repeatUntilDone;
150
+ /**
151
+ * Loads a transaction receipt using the rpc for the given chain id
152
+ * @param chainId The chain id where the transaction should be loaded from
153
+ * @param txHash The hash of the transaction
154
+ * @returns TransactionReceipt
155
+ */
156
+ var loadTransactionReceipt = function (chainId, txHash) { return __awaiter(void 0, void 0, void 0, function () {
157
+ var rpc, tx;
158
+ return __generator(this, function (_a) {
159
+ switch (_a.label) {
160
+ case 0: return [4 /*yield*/, (0, connectors_1.getRpcProvider)(chainId)];
161
+ case 1:
162
+ rpc = _a.sent();
163
+ return [4 /*yield*/, rpc.getTransaction(txHash)];
164
+ case 2:
165
+ tx = _a.sent();
166
+ return [2 /*return*/, tx.wait()];
167
+ }
168
+ });
169
+ }); };
170
+ exports.loadTransactionReceipt = loadTransactionReceipt;
171
+ var isZeroAddress = function (address) {
172
+ if (address === ethers_1.constants.AddressZero ||
173
+ address === '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee') {
174
+ return true;
175
+ }
176
+ return false;
177
+ };
178
+ exports.isZeroAddress = isZeroAddress;
179
+ var isNativeTokenAddress = function (address) {
180
+ if (address === ethers_1.constants.AddressZero ||
181
+ address === '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' ||
182
+ // CELO native token
183
+ address === '0x471ece3750da237f93b8e339c536989b8978a438') {
184
+ return true;
185
+ }
186
+ return false;
187
+ };
188
+ exports.isNativeTokenAddress = isNativeTokenAddress;
package/package.json ADDED
@@ -0,0 +1,90 @@
1
+ {
2
+ "name": "@lifi/sdk",
3
+ "version": "1.0.0",
4
+ "description": "LI.FI Any-to-Any Cross-Chain-Swap SDK",
5
+ "main": "./dist/index.js",
6
+ "types": "./dist/index.d.ts",
7
+ "scripts": {
8
+ "watch": "tsc -w -p ./tsconfig.json",
9
+ "build": "yarn clean && tsc --project ./tsconfig.json",
10
+ "clean": "node tools/cleanup",
11
+ "package": "npm run build && npm pack",
12
+ "test": "jest --no-cache --runInBand --forceExit",
13
+ "test:unit": "yarn test .unit.spec.ts",
14
+ "test:cov": "jest --coverage --no-cache --runInBand --forceExit",
15
+ "test:e2e": "jest -c jest.e2e.config.js",
16
+ "addscope": "node tools/packagejson name @lifi/sdk",
17
+ "pre-commit": "lint-staged",
18
+ "pre-push": "yarn types:check && yarn build && yarn test:unit",
19
+ "lint": "eslint --ext .tsx --ext .ts ./src",
20
+ "lint:fix": "eslint --ext .tsx --ext .ts ./src --fix",
21
+ "types:check": "tsc --noEmit",
22
+ "prettier:fix": "prettier --write ./src/.",
23
+ "use:npmReadme": "mv 'README.md' 'git.README.md' && mv 'npm.README.md' 'README.md'",
24
+ "use:gitReadme": "mv 'README.md' 'npm.README.md' && mv 'git.README.md' 'README.md'",
25
+ "prepublishOnly": "run-s build use:npmReadme && pinst --enable",
26
+ "postpublish": "npm run use:gitReadme && pinst --enable",
27
+ "prepare": "husky install",
28
+ "release": "standard-version"
29
+ },
30
+ "lint-staged": {
31
+ "src/**/*.{ts,tsx}": [
32
+ "yarn run lint:fix",
33
+ "yarn run prettier:fix"
34
+ ]
35
+ },
36
+ "files": [
37
+ "dist"
38
+ ],
39
+ "publishConfig": {
40
+ "access": "public"
41
+ },
42
+ "author": "Max Klenk <max@li.finance>",
43
+ "license": "UNLICENSED",
44
+ "homepage": "https://github.com/lifinance/sdk",
45
+ "repository": {
46
+ "type": "git",
47
+ "url": "git+ssh://git@github.com/lifinance/sdk.git"
48
+ },
49
+ "bugs": {
50
+ "url": "https://github.com/lifinance/sdk/issues"
51
+ },
52
+ "dependencies": {
53
+ "@ethersproject/abi": "^5.6.4",
54
+ "@ethersproject/contracts": "^5.6.2",
55
+ "@lifi/types": "^1.0.0",
56
+ "axios": "^0.27.2",
57
+ "bignumber.js": "^9.0.1",
58
+ "eth-rpc-errors": "^4.0.3",
59
+ "ethers": "^5.6.9"
60
+ },
61
+ "devDependencies": {
62
+ "@commitlint/cli": "^17.0.3",
63
+ "@commitlint/config-conventional": "^17.0.3",
64
+ "@types/bip39": "^3.0.0",
65
+ "@types/chai": "^4.3.1",
66
+ "@types/hdkey": "^2.0.0",
67
+ "@types/jest": "^28.1.4",
68
+ "@types/websocket": "^1.0.4",
69
+ "@typescript-eslint/eslint-plugin": "^5.30.6",
70
+ "@typescript-eslint/parser": "^5.30.6",
71
+ "eslint": "^8.19.0",
72
+ "eslint-config-prettier": "^8.3.0",
73
+ "eslint-plugin-prettier": "^4.2.1",
74
+ "husky": "^8.0.1",
75
+ "jest": "^28.1.2",
76
+ "lint-staged": "^13.0.3",
77
+ "npm-run-all": "^4.1.5",
78
+ "pinst": "^3.0.0",
79
+ "prettier": "^2.7.1",
80
+ "standard-version": "^9.5.0",
81
+ "ts-jest": "^28.0.5",
82
+ "ts-loader": "^9.3.1",
83
+ "typescript": "^4.7.4",
84
+ "webpack": "^5.73.0",
85
+ "webpack-cli": "^4.10.0"
86
+ },
87
+ "directories": {
88
+ "test": "test"
89
+ }
90
+ }