@lifi/sdk 1.1.2 → 1.1.5
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 +11 -0
- 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/cjs/Lifi.d.ts +200 -0
- package/dist/cjs/Lifi.js +376 -0
- package/dist/cjs/allowance/index.d.ts +22 -0
- package/dist/cjs/allowance/index.js +78 -0
- package/dist/cjs/allowance/utils.d.ts +14 -0
- package/dist/cjs/allowance/utils.js +82 -0
- package/dist/cjs/balances/index.d.ts +11 -0
- package/dist/cjs/balances/index.js +46 -0
- package/dist/cjs/balances/utils.d.ts +5 -0
- package/dist/cjs/balances/utils.js +150 -0
- package/dist/cjs/connectors.d.ts +6 -0
- package/dist/cjs/connectors.js +77 -0
- package/dist/cjs/execution/StatusManager.d.ts +65 -0
- package/dist/cjs/execution/StatusManager.js +167 -0
- package/dist/cjs/execution/StepExecutor.d.ts +15 -0
- package/dist/cjs/execution/StepExecutor.js +74 -0
- package/dist/cjs/execution/allowance.execute.d.ts +4 -0
- package/dist/cjs/execution/allowance.execute.js +97 -0
- package/dist/cjs/execution/balanceCheck.execute.d.ts +3 -0
- package/dist/cjs/execution/balanceCheck.execute.js +48 -0
- package/dist/cjs/execution/bridges/bridge.execute.d.ts +7 -0
- package/dist/cjs/execution/bridges/bridge.execute.js +154 -0
- package/dist/cjs/execution/exchanges/swap.execute.d.ts +7 -0
- package/dist/cjs/execution/exchanges/swap.execute.js +164 -0
- package/dist/cjs/execution/index.d.ts +1 -0
- package/dist/cjs/execution/index.js +17 -0
- package/dist/cjs/execution/stepComparison.d.ts +14 -0
- package/dist/cjs/execution/stepComparison.js +46 -0
- package/dist/cjs/execution/switchChain.d.ts +16 -0
- package/dist/cjs/execution/switchChain.js +58 -0
- package/dist/cjs/execution/utils.d.ts +6 -0
- package/dist/cjs/execution/utils.js +137 -0
- package/dist/cjs/helpers.d.ts +18 -0
- package/dist/cjs/helpers.js +54 -0
- package/dist/cjs/index.d.ts +6 -0
- package/dist/cjs/index.js +27 -0
- package/dist/cjs/services/ApiService.d.ts +15 -0
- package/dist/cjs/services/ApiService.js +272 -0
- package/dist/cjs/services/ChainsService.d.ts +11 -0
- package/dist/cjs/services/ChainsService.js +54 -0
- package/dist/cjs/services/ConfigService.d.ts +23 -0
- package/dist/cjs/services/ConfigService.js +98 -0
- package/dist/cjs/typeguards.d.ts +4 -0
- package/dist/cjs/typeguards.js +53 -0
- package/dist/cjs/types/ERC20.d.ts +22 -0
- package/dist/cjs/types/ERC20.js +53 -0
- package/dist/cjs/types/index.d.ts +4 -0
- package/dist/cjs/types/index.js +22 -0
- package/dist/cjs/types/internal.types.d.ts +85 -0
- package/dist/cjs/types/internal.types.js +2 -0
- package/dist/cjs/utils/errors.d.ts +75 -0
- package/dist/cjs/utils/errors.js +115 -0
- package/dist/cjs/utils/getProvider.d.ts +3 -0
- package/dist/cjs/utils/getProvider.js +11 -0
- package/dist/cjs/utils/multicall.d.ts +10 -0
- package/dist/cjs/utils/multicall.js +77 -0
- package/dist/cjs/utils/multicallAbi.json +313 -0
- package/dist/cjs/utils/parseError.d.ts +38 -0
- package/dist/cjs/utils/parseError.js +141 -0
- package/dist/cjs/utils/preRestart.d.ts +2 -0
- package/dist/cjs/utils/preRestart.js +31 -0
- package/dist/cjs/utils/utils.d.ts +26 -0
- package/dist/cjs/utils/utils.js +120 -0
- package/dist/connectors.js +50 -133
- package/dist/execution/StatusManager.js +34 -41
- package/dist/execution/StepExecutor.js +54 -123
- package/dist/execution/allowance.execute.js +76 -142
- 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 +34 -11
|
@@ -0,0 +1,137 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.updatedStepMeetsSlippageConditions = exports.getSubstatusMessage = exports.getProcessMessage = exports.waitForReceivingTransaction = void 0;
|
|
16
|
+
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
17
|
+
const ApiService_1 = __importDefault(require("../services/ApiService"));
|
|
18
|
+
const errors_1 = require("../utils/errors");
|
|
19
|
+
const utils_1 = require("../utils/utils");
|
|
20
|
+
const TRANSACTION_HASH_OBSERVERS = {};
|
|
21
|
+
function waitForReceivingTransaction(txHash, statusManager, processType, step) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
const getStatus = () => new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
let statusResponse;
|
|
25
|
+
try {
|
|
26
|
+
statusResponse = yield ApiService_1.default.getStatus({
|
|
27
|
+
bridge: step.tool,
|
|
28
|
+
fromChain: step.action.fromChainId,
|
|
29
|
+
toChain: step.action.toChainId,
|
|
30
|
+
txHash,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
catch (e) {
|
|
34
|
+
console.debug('Fetching status from backend failed.', e);
|
|
35
|
+
return resolve(undefined);
|
|
36
|
+
}
|
|
37
|
+
switch (statusResponse.status) {
|
|
38
|
+
case 'DONE':
|
|
39
|
+
return resolve(statusResponse);
|
|
40
|
+
case 'PENDING':
|
|
41
|
+
statusManager === null || statusManager === void 0 ? void 0 : statusManager.updateProcess(step, processType, 'PENDING', {
|
|
42
|
+
substatus: statusResponse.substatus,
|
|
43
|
+
substatusMessage: statusResponse.substatusMessage ||
|
|
44
|
+
getSubstatusMessage(statusResponse.status, statusResponse.substatus),
|
|
45
|
+
});
|
|
46
|
+
return resolve(undefined);
|
|
47
|
+
case 'NOT_FOUND':
|
|
48
|
+
return resolve(undefined);
|
|
49
|
+
case 'FAILED':
|
|
50
|
+
default:
|
|
51
|
+
return reject();
|
|
52
|
+
}
|
|
53
|
+
}));
|
|
54
|
+
let status;
|
|
55
|
+
if (txHash in TRANSACTION_HASH_OBSERVERS) {
|
|
56
|
+
status = yield TRANSACTION_HASH_OBSERVERS[txHash];
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
TRANSACTION_HASH_OBSERVERS[txHash] = (0, utils_1.repeatUntilDone)(getStatus, 5000);
|
|
60
|
+
status = yield TRANSACTION_HASH_OBSERVERS[txHash];
|
|
61
|
+
}
|
|
62
|
+
if (!status.receiving) {
|
|
63
|
+
throw new errors_1.ServerError("Status doesn't contain receiving information.");
|
|
64
|
+
}
|
|
65
|
+
return status;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
exports.waitForReceivingTransaction = waitForReceivingTransaction;
|
|
69
|
+
const processMessages = {
|
|
70
|
+
TOKEN_ALLOWANCE: {
|
|
71
|
+
STARTED: 'Setting token allowance.',
|
|
72
|
+
PENDING: 'Waiting for token allowance.',
|
|
73
|
+
DONE: 'Token allowance set.',
|
|
74
|
+
},
|
|
75
|
+
SWITCH_CHAIN: {
|
|
76
|
+
PENDING: 'Chain switch required.',
|
|
77
|
+
DONE: 'Chain switched successfully.',
|
|
78
|
+
},
|
|
79
|
+
SWAP: {
|
|
80
|
+
STARTED: 'Preparing swap.',
|
|
81
|
+
ACTION_REQUIRED: 'Please sign the transaction.',
|
|
82
|
+
PENDING: 'Swapping.',
|
|
83
|
+
DONE: 'Swap completed.',
|
|
84
|
+
},
|
|
85
|
+
CROSS_CHAIN: {
|
|
86
|
+
STARTED: 'Preparing transaction.',
|
|
87
|
+
ACTION_REQUIRED: 'Please sign the transaction.',
|
|
88
|
+
PENDING: 'Waiting for transaction.',
|
|
89
|
+
DONE: 'Transaction approved.',
|
|
90
|
+
},
|
|
91
|
+
RECEIVING_CHAIN: {
|
|
92
|
+
PENDING: 'Waiting for receiving chain.',
|
|
93
|
+
DONE: 'Funds received.',
|
|
94
|
+
},
|
|
95
|
+
TRANSACTION: {},
|
|
96
|
+
};
|
|
97
|
+
const substatusMessages = {
|
|
98
|
+
PENDING: {
|
|
99
|
+
BRIDGE_NOT_AVAILABLE: 'Bridge communication is temporarily unavailable.',
|
|
100
|
+
CHAIN_NOT_AVAILABLE: 'RPC communication is temporarily unavailable.',
|
|
101
|
+
NOT_PROCESSABLE_REFUND_NEEDED: 'The transfer cannot be completed successfully. A refund operation is required.',
|
|
102
|
+
UNKNOWN_ERROR: 'An unexpected error occurred. Please seek assistance in the LI.FI discord server.',
|
|
103
|
+
WAIT_SOURCE_CONFIRMATIONS: 'The bridge deposit has been received. The bridge is waiting for more confirmations to start the off-chain logic.',
|
|
104
|
+
WAIT_DESTINATION_TRANSACTION: 'The bridge off-chain logic is being executed. Wait for the transaction to appear on the destination chain.',
|
|
105
|
+
},
|
|
106
|
+
DONE: {
|
|
107
|
+
PARTIAL: 'Some of the received tokens are not the requested destination tokens.',
|
|
108
|
+
REFUNDED: 'The tokens were refunded to the sender address.',
|
|
109
|
+
COMPLETED: 'The transfer is complete.',
|
|
110
|
+
},
|
|
111
|
+
FAILED: {},
|
|
112
|
+
INVALID: {},
|
|
113
|
+
NOT_FOUND: {},
|
|
114
|
+
};
|
|
115
|
+
function getProcessMessage(type, status) {
|
|
116
|
+
const processMessage = processMessages[type][status];
|
|
117
|
+
return processMessage;
|
|
118
|
+
}
|
|
119
|
+
exports.getProcessMessage = getProcessMessage;
|
|
120
|
+
function getSubstatusMessage(status, substatus) {
|
|
121
|
+
if (!substatus) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const message = substatusMessages[status][substatus];
|
|
125
|
+
return message;
|
|
126
|
+
}
|
|
127
|
+
exports.getSubstatusMessage = getSubstatusMessage;
|
|
128
|
+
function updatedStepMeetsSlippageConditions(oldStep, newStep) {
|
|
129
|
+
const setSlippage = new bignumber_js_1.default(oldStep.action.slippage);
|
|
130
|
+
const oldEstimatedToAmount = new bignumber_js_1.default(oldStep.estimate.toAmountMin);
|
|
131
|
+
const newEstimatedToAmount = new bignumber_js_1.default(newStep.estimate.toAmountMin);
|
|
132
|
+
const amountDifference = oldEstimatedToAmount.minus(newEstimatedToAmount);
|
|
133
|
+
const actualSlippage = amountDifference.dividedBy(oldEstimatedToAmount);
|
|
134
|
+
return (newEstimatedToAmount.gte(oldEstimatedToAmount) &&
|
|
135
|
+
actualSlippage.lte(setSlippage));
|
|
136
|
+
}
|
|
137
|
+
exports.updatedStepMeetsSlippageConditions = updatedStepMeetsSlippageConditions;
|
|
@@ -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,54 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.isSameToken = exports.getRandomNumber = exports.getEthereumPublicKeyHook = exports.getEthereumDecryptionHook = void 0;
|
|
13
|
+
const ethereumRequest = (method, params) => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
|
+
// If ethereum.request() exists, the provider is probably EIP-1193 compliant.
|
|
15
|
+
if (!ethereum || !ethereum.request) {
|
|
16
|
+
throw new Error('Provider not available.');
|
|
17
|
+
}
|
|
18
|
+
return ethereum.request({
|
|
19
|
+
method,
|
|
20
|
+
params,
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
/**
|
|
24
|
+
* Predefined hook that decrypts calldata using EIP-1193 compliant wallet functions.
|
|
25
|
+
* @param {string} walletAddress - The wallet address of the user that should decrypt the calldata.
|
|
26
|
+
* @return {(encryptedData: string) => Promise<any>} A function that decrypts data using EIP-1193 compliant wallet functions.
|
|
27
|
+
*/
|
|
28
|
+
const getEthereumDecryptionHook = (walletAddress) => {
|
|
29
|
+
return (encryptedData) => {
|
|
30
|
+
return ethereumRequest('eth_decrypt', [encryptedData, walletAddress]);
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
exports.getEthereumDecryptionHook = getEthereumDecryptionHook;
|
|
34
|
+
/**
|
|
35
|
+
* Predefined hook that get the public encryption key of a user using EIP-1193 compliant wallet functions.
|
|
36
|
+
* @param {string} walletAddress - The wallet address of the user.
|
|
37
|
+
* @return {(walletAddress: string) => () => Promise<any>} A function that return the public encryption key using EIP-1193 compliant wallet functions.
|
|
38
|
+
*/
|
|
39
|
+
const getEthereumPublicKeyHook = (walletAddress) => {
|
|
40
|
+
return () => {
|
|
41
|
+
return ethereumRequest('eth_getEncryptionPublicKey', [walletAddress]);
|
|
42
|
+
};
|
|
43
|
+
};
|
|
44
|
+
exports.getEthereumPublicKeyHook = getEthereumPublicKeyHook;
|
|
45
|
+
/**
|
|
46
|
+
* Returns a random number between min (inclusive) and max (inclusive)
|
|
47
|
+
*/
|
|
48
|
+
const getRandomNumber = (min, max) => {
|
|
49
|
+
return Math.floor(Math.random() * (max - min + 1) + min);
|
|
50
|
+
};
|
|
51
|
+
exports.getRandomNumber = getRandomNumber;
|
|
52
|
+
const isSameToken = (tokenA, tokenB) => tokenA.chainId === tokenB.chainId &&
|
|
53
|
+
tokenA.address.toLowerCase() === tokenB.address.toLowerCase();
|
|
54
|
+
exports.isSameToken = isSameToken;
|
|
@@ -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
|
+
const 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,15 @@
|
|
|
1
|
+
import { ContractCallQuoteRequest, 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: (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
|
+
getChains: (options?: RequestOptions | undefined) => Promise<import("@lifi/types").EVMChain[]>;
|
|
10
|
+
getRoutes: (request: RoutesRequest, options?: RequestOptions | undefined) => Promise<RoutesResponse>;
|
|
11
|
+
getStepTransaction: (step: Step, options?: RequestOptions | undefined) => Promise<Step>;
|
|
12
|
+
getTools: (request?: ToolsRequest | undefined, options?: RequestOptions | undefined) => Promise<ToolsResponse>;
|
|
13
|
+
getTokens: (request?: TokensRequest | undefined, options?: RequestOptions | undefined) => Promise<TokensResponse>;
|
|
14
|
+
};
|
|
15
|
+
export default _default;
|
|
@@ -0,0 +1,272 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const axios_1 = __importDefault(require("axios"));
|
|
16
|
+
const typeguards_1 = require("../typeguards");
|
|
17
|
+
const errors_1 = require("../utils/errors");
|
|
18
|
+
const parseError_1 = require("../utils/parseError");
|
|
19
|
+
const ConfigService_1 = __importDefault(require("./ConfigService"));
|
|
20
|
+
const getPossibilities = (request, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
|
+
if (!request) {
|
|
22
|
+
request = {};
|
|
23
|
+
}
|
|
24
|
+
const configService = ConfigService_1.default.getInstance();
|
|
25
|
+
const config = configService.getConfig();
|
|
26
|
+
// apply defaults
|
|
27
|
+
request.bridges = request.bridges || config.defaultRouteOptions.bridges;
|
|
28
|
+
request.exchanges = request.exchanges || config.defaultRouteOptions.exchanges;
|
|
29
|
+
// send request
|
|
30
|
+
try {
|
|
31
|
+
const result = yield axios_1.default.post(config.apiUrl + 'advanced/possibilities', request, {
|
|
32
|
+
signal: options === null || options === void 0 ? void 0 : options.signal,
|
|
33
|
+
});
|
|
34
|
+
return result.data;
|
|
35
|
+
}
|
|
36
|
+
catch (e) {
|
|
37
|
+
throw (0, parseError_1.parseBackendError)(e);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
const getToken = (chain, token, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
41
|
+
if (!chain) {
|
|
42
|
+
throw new errors_1.ValidationError('Required parameter "chain" is missing.');
|
|
43
|
+
}
|
|
44
|
+
if (!token) {
|
|
45
|
+
throw new errors_1.ValidationError('Required parameter "token" is missing.');
|
|
46
|
+
}
|
|
47
|
+
const configService = ConfigService_1.default.getInstance();
|
|
48
|
+
const config = configService.getConfig();
|
|
49
|
+
try {
|
|
50
|
+
const result = yield axios_1.default.get(config.apiUrl + 'token', {
|
|
51
|
+
params: {
|
|
52
|
+
chain,
|
|
53
|
+
token,
|
|
54
|
+
},
|
|
55
|
+
signal: options === null || options === void 0 ? void 0 : options.signal,
|
|
56
|
+
});
|
|
57
|
+
return result.data;
|
|
58
|
+
}
|
|
59
|
+
catch (e) {
|
|
60
|
+
throw (0, parseError_1.parseBackendError)(e);
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
const getQuote = (request, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
64
|
+
var _a, _b, _c, _d, _e, _f;
|
|
65
|
+
const configService = ConfigService_1.default.getInstance();
|
|
66
|
+
const config = configService.getConfig();
|
|
67
|
+
// validation
|
|
68
|
+
const requiredParameters = [
|
|
69
|
+
'fromChain',
|
|
70
|
+
'fromToken',
|
|
71
|
+
'fromAddress',
|
|
72
|
+
'fromAmount',
|
|
73
|
+
'toChain',
|
|
74
|
+
'toToken',
|
|
75
|
+
];
|
|
76
|
+
requiredParameters.forEach((requiredParameter) => {
|
|
77
|
+
if (!request[requiredParameter]) {
|
|
78
|
+
throw new errors_1.ValidationError(`Required parameter "${requiredParameter}" is missing.`);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
// apply defaults
|
|
82
|
+
request.order = request.order || config.defaultRouteOptions.order;
|
|
83
|
+
request.slippage = request.slippage || config.defaultRouteOptions.slippage;
|
|
84
|
+
request.integrator =
|
|
85
|
+
request.integrator || config.defaultRouteOptions.integrator;
|
|
86
|
+
request.referrer = request.referrer || config.defaultRouteOptions.referrer;
|
|
87
|
+
request.fee = request.fee || config.defaultRouteOptions.fee;
|
|
88
|
+
request.allowBridges =
|
|
89
|
+
request.allowBridges || ((_a = config.defaultRouteOptions.bridges) === null || _a === void 0 ? void 0 : _a.allow);
|
|
90
|
+
request.denyBridges =
|
|
91
|
+
request.denyBridges || ((_b = config.defaultRouteOptions.bridges) === null || _b === void 0 ? void 0 : _b.deny);
|
|
92
|
+
request.preferBridges =
|
|
93
|
+
request.preferBridges || ((_c = config.defaultRouteOptions.bridges) === null || _c === void 0 ? void 0 : _c.prefer);
|
|
94
|
+
request.allowExchanges =
|
|
95
|
+
request.allowExchanges || ((_d = config.defaultRouteOptions.bridges) === null || _d === void 0 ? void 0 : _d.allow);
|
|
96
|
+
request.denyExchanges =
|
|
97
|
+
request.denyExchanges || ((_e = config.defaultRouteOptions.bridges) === null || _e === void 0 ? void 0 : _e.deny);
|
|
98
|
+
request.preferExchanges =
|
|
99
|
+
request.preferExchanges || ((_f = config.defaultRouteOptions.bridges) === null || _f === void 0 ? void 0 : _f.prefer);
|
|
100
|
+
try {
|
|
101
|
+
const result = yield axios_1.default.get(config.apiUrl + 'quote', {
|
|
102
|
+
params: request,
|
|
103
|
+
signal: options === null || options === void 0 ? void 0 : options.signal,
|
|
104
|
+
});
|
|
105
|
+
return result.data;
|
|
106
|
+
}
|
|
107
|
+
catch (e) {
|
|
108
|
+
throw (0, parseError_1.parseBackendError)(e);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
const getContractCallQuote = (request, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
112
|
+
var _g, _h, _j, _k, _l, _m;
|
|
113
|
+
const configService = ConfigService_1.default.getInstance();
|
|
114
|
+
const config = configService.getConfig();
|
|
115
|
+
// validation
|
|
116
|
+
const requiredParameters = [
|
|
117
|
+
'fromChain',
|
|
118
|
+
'fromToken',
|
|
119
|
+
'fromAddress',
|
|
120
|
+
'toChain',
|
|
121
|
+
'toToken',
|
|
122
|
+
'toAmount',
|
|
123
|
+
'toContractAddress',
|
|
124
|
+
'toContractCallData',
|
|
125
|
+
'toContractGasLimit',
|
|
126
|
+
];
|
|
127
|
+
requiredParameters.forEach((requiredParameter) => {
|
|
128
|
+
if (!request[requiredParameter]) {
|
|
129
|
+
throw new errors_1.ValidationError(`Required parameter "${requiredParameter}" is missing.`);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
// apply defaults
|
|
133
|
+
// option.order is not used in this endpoint
|
|
134
|
+
request.slippage = request.slippage || config.defaultRouteOptions.slippage;
|
|
135
|
+
request.integrator =
|
|
136
|
+
request.integrator || config.defaultRouteOptions.integrator;
|
|
137
|
+
request.referrer = request.referrer || config.defaultRouteOptions.referrer;
|
|
138
|
+
request.fee = request.fee || config.defaultRouteOptions.fee;
|
|
139
|
+
request.allowBridges =
|
|
140
|
+
request.allowBridges || ((_g = config.defaultRouteOptions.bridges) === null || _g === void 0 ? void 0 : _g.allow);
|
|
141
|
+
request.denyBridges =
|
|
142
|
+
request.denyBridges || ((_h = config.defaultRouteOptions.bridges) === null || _h === void 0 ? void 0 : _h.deny);
|
|
143
|
+
request.preferBridges =
|
|
144
|
+
request.preferBridges || ((_j = config.defaultRouteOptions.bridges) === null || _j === void 0 ? void 0 : _j.prefer);
|
|
145
|
+
request.allowExchanges =
|
|
146
|
+
request.allowExchanges || ((_k = config.defaultRouteOptions.bridges) === null || _k === void 0 ? void 0 : _k.allow);
|
|
147
|
+
request.denyExchanges =
|
|
148
|
+
request.denyExchanges || ((_l = config.defaultRouteOptions.bridges) === null || _l === void 0 ? void 0 : _l.deny);
|
|
149
|
+
request.preferExchanges =
|
|
150
|
+
request.preferExchanges || ((_m = config.defaultRouteOptions.bridges) === null || _m === void 0 ? void 0 : _m.prefer);
|
|
151
|
+
// send request
|
|
152
|
+
try {
|
|
153
|
+
const result = yield axios_1.default.post(config.apiUrl + 'quote/contractCall', request, {
|
|
154
|
+
signal: options === null || options === void 0 ? void 0 : options.signal,
|
|
155
|
+
});
|
|
156
|
+
return result.data;
|
|
157
|
+
}
|
|
158
|
+
catch (e) {
|
|
159
|
+
throw (0, parseError_1.parseBackendError)(e);
|
|
160
|
+
}
|
|
161
|
+
});
|
|
162
|
+
const getStatus = ({ bridge, fromChain, toChain, txHash }, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
163
|
+
if (fromChain !== toChain && !bridge) {
|
|
164
|
+
throw new errors_1.ValidationError('Parameter "bridge" is required for cross chain transfers.');
|
|
165
|
+
}
|
|
166
|
+
if (!fromChain) {
|
|
167
|
+
throw new errors_1.ValidationError('Required parameter "fromChain" is missing.');
|
|
168
|
+
}
|
|
169
|
+
if (!toChain) {
|
|
170
|
+
throw new errors_1.ValidationError('Required parameter "toChain" is missing.');
|
|
171
|
+
}
|
|
172
|
+
if (!txHash) {
|
|
173
|
+
throw new errors_1.ValidationError('Required parameter "txHash" is missing.');
|
|
174
|
+
}
|
|
175
|
+
const configService = ConfigService_1.default.getInstance();
|
|
176
|
+
const config = configService.getConfig();
|
|
177
|
+
try {
|
|
178
|
+
const result = yield axios_1.default.get(config.apiUrl + 'status', {
|
|
179
|
+
params: {
|
|
180
|
+
bridge,
|
|
181
|
+
fromChain,
|
|
182
|
+
toChain,
|
|
183
|
+
txHash,
|
|
184
|
+
},
|
|
185
|
+
signal: options === null || options === void 0 ? void 0 : options.signal,
|
|
186
|
+
});
|
|
187
|
+
return result.data;
|
|
188
|
+
}
|
|
189
|
+
catch (e) {
|
|
190
|
+
throw (0, parseError_1.parseBackendError)(e);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
const getChains = (options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
194
|
+
const configService = ConfigService_1.default.getInstance();
|
|
195
|
+
const config = configService.getConfig();
|
|
196
|
+
try {
|
|
197
|
+
const result = yield axios_1.default.get(config.apiUrl + 'chains', {
|
|
198
|
+
signal: options === null || options === void 0 ? void 0 : options.signal,
|
|
199
|
+
});
|
|
200
|
+
return result.data.chains;
|
|
201
|
+
}
|
|
202
|
+
catch (e) {
|
|
203
|
+
throw (0, parseError_1.parseBackendError)(e);
|
|
204
|
+
}
|
|
205
|
+
});
|
|
206
|
+
const getRoutes = (request, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
207
|
+
if (!(0, typeguards_1.isRoutesRequest)(request)) {
|
|
208
|
+
throw new errors_1.ValidationError('Invalid routes request.');
|
|
209
|
+
}
|
|
210
|
+
const configService = ConfigService_1.default.getInstance();
|
|
211
|
+
const config = configService.getConfig();
|
|
212
|
+
// apply defaults
|
|
213
|
+
request.options = Object.assign(Object.assign({}, config.defaultRouteOptions), request.options);
|
|
214
|
+
// send request
|
|
215
|
+
try {
|
|
216
|
+
const result = yield axios_1.default.post(config.apiUrl + 'advanced/routes', request, {
|
|
217
|
+
signal: options === null || options === void 0 ? void 0 : options.signal,
|
|
218
|
+
});
|
|
219
|
+
return result.data;
|
|
220
|
+
}
|
|
221
|
+
catch (e) {
|
|
222
|
+
throw (0, parseError_1.parseBackendError)(e);
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
const getStepTransaction = (step, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
226
|
+
if (!(0, typeguards_1.isStep)(step)) {
|
|
227
|
+
// While the validation fails for some users we should not enforce it
|
|
228
|
+
// eslint-disable-next-line no-console
|
|
229
|
+
console.warn('SDK Validation: Invalid Step', step);
|
|
230
|
+
}
|
|
231
|
+
const configService = ConfigService_1.default.getInstance();
|
|
232
|
+
const config = configService.getConfig();
|
|
233
|
+
try {
|
|
234
|
+
const result = yield axios_1.default.post(config.apiUrl + 'advanced/stepTransaction', step, {
|
|
235
|
+
signal: options === null || options === void 0 ? void 0 : options.signal,
|
|
236
|
+
});
|
|
237
|
+
return result.data;
|
|
238
|
+
}
|
|
239
|
+
catch (e) {
|
|
240
|
+
throw (0, parseError_1.parseBackendError)(e);
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
const getTools = (request, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
244
|
+
const configService = ConfigService_1.default.getInstance();
|
|
245
|
+
const config = configService.getConfig();
|
|
246
|
+
const r = yield axios_1.default.get(config.apiUrl + 'tools', {
|
|
247
|
+
params: request,
|
|
248
|
+
signal: options === null || options === void 0 ? void 0 : options.signal,
|
|
249
|
+
});
|
|
250
|
+
return r.data;
|
|
251
|
+
});
|
|
252
|
+
const getTokens = (request, options) => __awaiter(void 0, void 0, void 0, function* () {
|
|
253
|
+
const configService = ConfigService_1.default.getInstance();
|
|
254
|
+
const config = configService.getConfig();
|
|
255
|
+
const r = yield axios_1.default.get(config.apiUrl + 'tokens', {
|
|
256
|
+
params: request,
|
|
257
|
+
signal: options === null || options === void 0 ? void 0 : options.signal,
|
|
258
|
+
});
|
|
259
|
+
return r.data;
|
|
260
|
+
});
|
|
261
|
+
exports.default = {
|
|
262
|
+
getPossibilities,
|
|
263
|
+
getToken,
|
|
264
|
+
getQuote,
|
|
265
|
+
getContractCallQuote,
|
|
266
|
+
getStatus,
|
|
267
|
+
getChains,
|
|
268
|
+
getRoutes,
|
|
269
|
+
getStepTransaction,
|
|
270
|
+
getTools,
|
|
271
|
+
getTokens,
|
|
272
|
+
};
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Chain, ChainId } from '@lifi/types';
|
|
2
|
+
export default class ChainsService {
|
|
3
|
+
private static instance;
|
|
4
|
+
private readonly loadingPromise;
|
|
5
|
+
private chains;
|
|
6
|
+
constructor();
|
|
7
|
+
private loadAvailableChains;
|
|
8
|
+
static getInstance(): ChainsService;
|
|
9
|
+
getChainById(chainId: ChainId): Promise<Chain>;
|
|
10
|
+
getChains(): Promise<Chain[]>;
|
|
11
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
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 __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const errors_1 = require("../utils/errors");
|
|
16
|
+
const ApiService_1 = __importDefault(require("./ApiService"));
|
|
17
|
+
class ChainsService {
|
|
18
|
+
constructor() {
|
|
19
|
+
this.chains = [];
|
|
20
|
+
this.loadingPromise = this.loadAvailableChains();
|
|
21
|
+
}
|
|
22
|
+
loadAvailableChains() {
|
|
23
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
this.chains = yield ApiService_1.default.getChains();
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
static getInstance() {
|
|
28
|
+
if (!this.instance) {
|
|
29
|
+
this.instance = new ChainsService();
|
|
30
|
+
}
|
|
31
|
+
return this.instance;
|
|
32
|
+
}
|
|
33
|
+
getChainById(chainId) {
|
|
34
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
+
if (this.loadingPromise) {
|
|
36
|
+
yield this.loadingPromise;
|
|
37
|
+
}
|
|
38
|
+
const chain = this.chains.find((chain) => chain.id === chainId);
|
|
39
|
+
if (!chain) {
|
|
40
|
+
throw new errors_1.ValidationError(`Unknown chainId passed: ${chainId}.`);
|
|
41
|
+
}
|
|
42
|
+
return chain;
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
getChains() {
|
|
46
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
47
|
+
if (this.loadingPromise) {
|
|
48
|
+
yield this.loadingPromise;
|
|
49
|
+
}
|
|
50
|
+
return this.chains;
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
exports.default = ChainsService;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { Chain, Config, ConfigUpdate } from '../types';
|
|
2
|
+
export default class ConfigService {
|
|
3
|
+
private static instance;
|
|
4
|
+
private readonly config;
|
|
5
|
+
private readonly setupPromise;
|
|
6
|
+
private resolveSetupPromise;
|
|
7
|
+
constructor();
|
|
8
|
+
private static chainIdToObject;
|
|
9
|
+
private static getDefaultConfig;
|
|
10
|
+
static getInstance(): ConfigService;
|
|
11
|
+
/**
|
|
12
|
+
* This call immediately returns the current config. It does not make sure that all chain data is already loaded
|
|
13
|
+
* Use this if you need access to basic information like API urls or settings
|
|
14
|
+
*/
|
|
15
|
+
getConfig: () => Config;
|
|
16
|
+
/**
|
|
17
|
+
* This call waits for all setup promises to be done.
|
|
18
|
+
* Use this if you need access to chain data (RPCs or multicalls)
|
|
19
|
+
*/
|
|
20
|
+
getConfigAsync: () => Promise<Config>;
|
|
21
|
+
updateConfig: (configUpdate: ConfigUpdate) => Config;
|
|
22
|
+
updateChains: (chains: Chain[]) => Config;
|
|
23
|
+
}
|