@lifi/sdk 1.1.4 → 1.2.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 (73) hide show
  1. package/CHANGELOG.md +11 -0
  2. package/dist/Lifi.js +3 -0
  3. package/dist/cjs/Lifi.d.ts +200 -0
  4. package/dist/cjs/Lifi.js +379 -0
  5. package/dist/cjs/allowance/index.d.ts +22 -0
  6. package/dist/cjs/allowance/index.js +78 -0
  7. package/dist/cjs/allowance/utils.d.ts +14 -0
  8. package/dist/cjs/allowance/utils.js +82 -0
  9. package/dist/cjs/balances/index.d.ts +11 -0
  10. package/dist/cjs/balances/index.js +46 -0
  11. package/dist/cjs/balances/utils.d.ts +5 -0
  12. package/dist/cjs/balances/utils.js +150 -0
  13. package/dist/cjs/connectors.d.ts +6 -0
  14. package/dist/cjs/connectors.js +77 -0
  15. package/dist/cjs/execution/StatusManager.d.ts +65 -0
  16. package/dist/cjs/execution/StatusManager.js +167 -0
  17. package/dist/cjs/execution/StepExecutor.d.ts +15 -0
  18. package/dist/cjs/execution/StepExecutor.js +74 -0
  19. package/dist/cjs/execution/allowance.execute.d.ts +4 -0
  20. package/dist/cjs/execution/allowance.execute.js +97 -0
  21. package/dist/cjs/execution/balanceCheck.execute.d.ts +3 -0
  22. package/dist/cjs/execution/balanceCheck.execute.js +48 -0
  23. package/dist/cjs/execution/bridges/bridge.execute.d.ts +7 -0
  24. package/dist/cjs/execution/bridges/bridge.execute.js +154 -0
  25. package/dist/cjs/execution/exchanges/swap.execute.d.ts +7 -0
  26. package/dist/cjs/execution/exchanges/swap.execute.js +164 -0
  27. package/dist/cjs/execution/index.d.ts +1 -0
  28. package/dist/cjs/execution/index.js +17 -0
  29. package/dist/cjs/execution/stepComparison.d.ts +14 -0
  30. package/dist/cjs/execution/stepComparison.js +46 -0
  31. package/dist/cjs/execution/switchChain.d.ts +16 -0
  32. package/dist/cjs/execution/switchChain.js +58 -0
  33. package/dist/cjs/execution/utils.d.ts +6 -0
  34. package/dist/cjs/execution/utils.js +137 -0
  35. package/dist/cjs/helpers.d.ts +19 -0
  36. package/dist/cjs/helpers.js +88 -0
  37. package/dist/cjs/index.d.ts +6 -0
  38. package/dist/cjs/index.js +27 -0
  39. package/dist/cjs/services/ApiService.d.ts +15 -0
  40. package/dist/cjs/services/ApiService.js +272 -0
  41. package/dist/cjs/services/ChainsService.d.ts +11 -0
  42. package/dist/cjs/services/ChainsService.js +54 -0
  43. package/dist/cjs/services/ConfigService.d.ts +23 -0
  44. package/dist/cjs/services/ConfigService.js +98 -0
  45. package/dist/cjs/typeguards.d.ts +4 -0
  46. package/dist/cjs/typeguards.js +53 -0
  47. package/dist/cjs/types/ERC20.d.ts +22 -0
  48. package/dist/cjs/types/ERC20.js +53 -0
  49. package/dist/cjs/types/index.d.ts +4 -0
  50. package/dist/cjs/types/index.js +22 -0
  51. package/dist/cjs/types/internal.types.d.ts +87 -0
  52. package/dist/cjs/types/internal.types.js +2 -0
  53. package/dist/cjs/utils/errors.d.ts +75 -0
  54. package/dist/cjs/utils/errors.js +115 -0
  55. package/dist/cjs/utils/getProvider.d.ts +3 -0
  56. package/dist/cjs/utils/getProvider.js +11 -0
  57. package/dist/cjs/utils/multicall.d.ts +10 -0
  58. package/dist/cjs/utils/multicall.js +77 -0
  59. package/dist/cjs/utils/multicallAbi.json +313 -0
  60. package/dist/cjs/utils/parseError.d.ts +38 -0
  61. package/dist/cjs/utils/parseError.js +141 -0
  62. package/dist/cjs/utils/preRestart.d.ts +2 -0
  63. package/dist/cjs/utils/preRestart.js +31 -0
  64. package/dist/cjs/utils/utils.d.ts +26 -0
  65. package/dist/cjs/utils/utils.js +120 -0
  66. package/dist/cjs/version.d.ts +2 -0
  67. package/dist/cjs/version.js +5 -0
  68. package/dist/helpers.d.ts +1 -0
  69. package/dist/helpers.js +33 -0
  70. package/dist/types/internal.types.d.ts +2 -0
  71. package/dist/version.d.ts +2 -0
  72. package/dist/version.js +2 -0
  73. package/package.json +40 -12
package/CHANGELOG.md CHANGED
@@ -2,6 +2,17 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [1.2.0](https://github.com/lifinance/sdk/compare/v1.1.6...v1.2.0) (2022-08-22)
6
+
7
+
8
+ ### Features
9
+
10
+ * add version check ([#102](https://github.com/lifinance/sdk/issues/102)) ([dbc0fc5](https://github.com/lifinance/sdk/commit/dbc0fc5805a2d6c9b3da8547d44c3e93f1a3b8ed))
11
+
12
+ ### [1.1.6](https://github.com/lifinance/sdk/compare/v1.1.5...v1.1.6) (2022-08-19)
13
+
14
+ ### [1.1.5](https://github.com/lifinance/sdk/compare/v1.1.4...v1.1.5) (2022-08-19)
15
+
5
16
  ### [1.1.4](https://github.com/lifinance/sdk/compare/v1.1.3...v1.1.4) (2022-08-19)
6
17
 
7
18
  ### [1.1.3](https://github.com/lifinance/sdk/compare/v1.1.2...v1.1.3) (2022-08-06)
package/dist/Lifi.js CHANGED
@@ -12,6 +12,7 @@ import balances from './balances';
12
12
  import { getRpcProvider } from './connectors';
13
13
  import { StatusManager } from './execution/StatusManager';
14
14
  import { StepExecutor } from './execution/StepExecutor';
15
+ import { checkPackageUpdates } from './helpers';
15
16
  import ApiService from './services/ApiService';
16
17
  import ChainsService from './services/ChainsService';
17
18
  import ConfigService from './services/ConfigService';
@@ -19,6 +20,7 @@ import { isToken } from './typeguards';
19
20
  import { ValidationError } from './utils/errors';
20
21
  import { handlePreRestart } from './utils/preRestart';
21
22
  import { deepClone } from './utils/utils';
23
+ import { name, version } from './version';
22
24
  export default class LIFI {
23
25
  constructor(configUpdate) {
24
26
  this.activeRouteDictionary = {};
@@ -366,5 +368,6 @@ export default class LIFI {
366
368
  this.chainsService.getChains().then((chains) => {
367
369
  this.configService.updateChains(chains);
368
370
  });
371
+ checkPackageUpdates(name, version, configUpdate === null || configUpdate === void 0 ? void 0 : configUpdate.disableVersionCheck);
369
372
  }
370
373
  }
@@ -0,0 +1,200 @@
1
+ import { FallbackProvider } from '@ethersproject/providers';
2
+ import { Chain, ChainId, ChainKey, ContractCallQuoteRequest, GetStatusRequest, PossibilitiesRequest, PossibilitiesResponse, QuoteRequest, RequestOptions, Route, RoutesRequest, RoutesResponse, StatusResponse, Step, Token, TokenAmount, TokensRequest, TokensResponse, ToolsRequest, ToolsResponse } from '@lifi/types';
3
+ import { Signer } from 'ethers';
4
+ import { ApproveTokenRequest, RevokeApprovalRequest } from './allowance';
5
+ import { Config, ConfigUpdate, ExecutionSettings, RevokeTokenData } from './types';
6
+ export default class LIFI {
7
+ private activeRouteDictionary;
8
+ private configService;
9
+ private chainsService;
10
+ constructor(configUpdate?: ConfigUpdate);
11
+ /**
12
+ * Get the current configuration of the SDK
13
+ * @return {Config} - The config object
14
+ */
15
+ getConfig: () => Config;
16
+ /**
17
+ * Get the SDK configuration after all setup calls are finished
18
+ * @return {Promise<Config>} - The config object
19
+ */
20
+ getConfigAsync: () => Promise<Config>;
21
+ /**
22
+ * Get an instance of a provider for a specific cahin
23
+ * @param {number} chainId - Id of the chain the provider is for
24
+ * @param {boolean} archive - Whether to use an archive provider that is based on a default rpc or not. defaults to false
25
+ * @return {FallbackProvider} The provider for the given chain
26
+ */
27
+ getRpcProvider: (chainId: number, archive?: boolean) => Promise<FallbackProvider>;
28
+ /**
29
+ * Set a new confuration for the SDK
30
+ * @param {ConfigUpdate} configUpdate - An object containing the configuration fields that should be updated.
31
+ * @return {Config} The renewed config object
32
+ */
33
+ setConfig: (configUpdate: ConfigUpdate) => Config;
34
+ /**
35
+ * Get a set of current possibilities based on a request that specifies which chains, exchanges and bridges are preferred or unwanted.
36
+ * @param {PossibilitiesRequest} request - Object defining preferences regarding chain, exchanges and bridges
37
+ * @return {Promise<PossibilitiesResponse>} Object listing current possibilities for any-to-any cross-chain-swaps based on the provided preferences.
38
+ * @throws {LifiError} Throws a LifiError if request fails.
39
+ */
40
+ getPossibilities: (request?: PossibilitiesRequest, options?: RequestOptions) => Promise<PossibilitiesResponse>;
41
+ /**
42
+ * Fetch information about a Token
43
+ * @param {ChainKey | ChainId} chain - Id or key of the chain that contains the token
44
+ * @param {string} token - Address or symbol of the token on the requested chain
45
+ * @throws {LifiError} - Throws a LifiError if request fails
46
+ */
47
+ getToken: (chain: ChainKey | ChainId, token: string, options?: RequestOptions) => Promise<Token>;
48
+ /**
49
+ * Get a quote for a token transfer
50
+ * @param {QuoteRequest} request - The configuration of the requested quote
51
+ * @throws {LifiError} - Throws a LifiError if request fails
52
+ */
53
+ getQuote: (request: QuoteRequest, options?: RequestOptions) => Promise<Step>;
54
+ /**
55
+ * Get a quote for a destination contract call
56
+ * @param {ContractCallQuoteRequest} request - The configuration of the requested destination call
57
+ * @throws {LifiError} - Throws a LifiError if request fails
58
+ */
59
+ getContractCallQuote: (request: ContractCallQuoteRequest, options?: RequestOptions) => Promise<Step>;
60
+ /**
61
+ * Check the status of a transfer. For cross chain transfers, the "bridge" parameter is required.
62
+ * @param {GetStatusRequest} request - Configuration of the requested status
63
+ * @throws {LifiError} - Throws a LifiError if request fails
64
+ */
65
+ getStatus: (request: GetStatusRequest, options?: RequestOptions) => Promise<StatusResponse>;
66
+ /**
67
+ * Get the available tools to bridge and swap tokens.
68
+ * @param {ToolsRequest?} request - The configuration of the requested tools
69
+ * @returns The tools that are available on the requested chains
70
+ */
71
+ getTools: (request?: ToolsRequest, options?: RequestOptions) => Promise<ToolsResponse>;
72
+ /**
73
+ * Get all known tokens.
74
+ * @param {TokensRequest?} request - The configuration of the requested tokens
75
+ * @returns The tokens that are available on the requested chains
76
+ */
77
+ getTokens: (request?: TokensRequest, options?: RequestOptions) => Promise<TokensResponse>;
78
+ /**
79
+ * Get all available chains
80
+ * @return {Promise<Chain[]>} A list of all available chains
81
+ * @throws {LifiError} Throws a LifiError if request fails.
82
+ */
83
+ getChains: () => Promise<Chain[]>;
84
+ /**
85
+ * Get a set of routes for a request that describes a transfer of tokens.
86
+ * @param {RoutesRequest} routesRequest - A description of the transfer.
87
+ * @return {Promise<RoutesResponse>} The resulting routes that can be used to realize the described transfer of tokens.
88
+ * @throws {LifiError} Throws a LifiError if request fails.
89
+ */
90
+ getRoutes: (request: RoutesRequest, options?: RequestOptions) => Promise<RoutesResponse>;
91
+ /**
92
+ * Get the transaction data for a single step of a route
93
+ * @param {Step} step - The step object.
94
+ * @return {Promise<Step>} The step populated with the transaction data.
95
+ * @throws {LifiError} Throws a LifiError if request fails.
96
+ */
97
+ getStepTransaction: (step: Step, options?: RequestOptions) => Promise<Step>;
98
+ /**
99
+ * Stops the execution of an active route.
100
+ * @param {Route} route - A route that is currently in execution.
101
+ * @return {Route} The stopped route.
102
+ */
103
+ stopExecution: (route: Route) => Route;
104
+ /**
105
+ * Executes a route until a user interaction is necessary (signing transactions, etc.) and then halts until the route is resumed.
106
+ * @param {Route} route - A route that is currently in execution.
107
+ */
108
+ moveExecutionToBackground: (route: Route) => void;
109
+ /**
110
+ * Execute a route.
111
+ * @param {Signer} signer - The signer required to send the transactions.
112
+ * @param {Route} route - The route that should be executed. Cannot be an active route.
113
+ * @param {ExecutionSettings} settings - An object containing settings and callbacks.
114
+ * @return {Promise<Route>} The executed route.
115
+ * @throws {LifiError} Throws a LifiError if the execution fails.
116
+ */
117
+ executeRoute: (signer: Signer, route: Route, settings?: ExecutionSettings) => Promise<Route>;
118
+ /**
119
+ * Resume the execution of a route that has been stopped or had an error while executing.
120
+ * @param {Signer} signer - The signer required to send the transactions.
121
+ * @param {Route} route - The route that is to be executed. Cannot be an active route.
122
+ * @param {ExecutionSettings} settings - An object containing settings and callbacks.
123
+ * @return {Promise<Route>} The executed route.
124
+ * @throws {LifiError} Throws a LifiError if the execution fails.
125
+ */
126
+ resumeRoute: (signer: Signer, route: Route, settings?: ExecutionSettings) => Promise<Route>;
127
+ private executeSteps;
128
+ /**
129
+ * Update the ExecutionSettings for an active route.
130
+ * @param {ExecutionSettings} settings - An object with execution settings.
131
+ * @param {Route} route - The active route that gets the new execution settings.
132
+ * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
133
+ */
134
+ updateExecutionSettings: (settings: ExecutionSettings, route: Route) => void;
135
+ /**
136
+ * Get the list of active routes.
137
+ * @return {Route[]} A list of routes.
138
+ */
139
+ getActiveRoutes: () => Route[];
140
+ /**
141
+ * Return the current route information for given route. The route has to be active.
142
+ * @param {Route} route - A route object.
143
+ * @return {Route} The updated route.
144
+ */
145
+ getActiveRoute: (route: Route) => Route | undefined;
146
+ /**
147
+ * Returns the balances of a specific token a wallet holds across all aggregated chains.
148
+ * @param {string} walletAddress - A wallet address.
149
+ * @param {Token} token - A Token object.
150
+ * @return {Promise<TokenAmount | null>} An object containing the token and the amounts on different chains.
151
+ * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
152
+ */
153
+ getTokenBalance: (walletAddress: string, token: Token) => Promise<TokenAmount | null>;
154
+ /**
155
+ * Returns the balances for a list tokens a wallet holds across all aggregated chains.
156
+ * @param {string} walletAddress - A wallet address.
157
+ * @param {Token[]} tokens - A list of Token objects.
158
+ * @return {Promise<TokenAmount[]>} A list of objects containing the tokens and the amounts on different chains.
159
+ * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
160
+ */
161
+ getTokenBalances: (walletAddress: string, tokens: Token[]) => Promise<TokenAmount[]>;
162
+ /**
163
+ * This method queries the balances of tokens for a specific list of chains for a given wallet.
164
+ * @param {string} walletAddress - A walletaddress.
165
+ * @param {{ [chainId: number]: Token[] }} tokensByChain - A list of Token objects organized by chain ids.
166
+ * @return {Promise<{ [chainId: number]: TokenAmount[] }} A list of objects containing the tokens and the amounts on different chains organized by the chosen chains.
167
+ * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
168
+ */
169
+ getTokenBalancesForChains: (walletAddress: string, tokensByChain: {
170
+ [chainId: number]: Token[];
171
+ }) => Promise<{
172
+ [chainId: number]: TokenAmount[];
173
+ }>;
174
+ /**
175
+ * Get the current approval for a certain token.
176
+ * @param signer - The signer owning the token
177
+ * @param token - The token that should be checked
178
+ * @param approvalAddress - The address that has be approved
179
+ */
180
+ getTokenApproval: (signer: Signer, token: Token, approvalAddress: string) => Promise<string | undefined>;
181
+ /**
182
+ * Get the current approval for a list of token / approval address pairs.
183
+ * @param signer - The signer owning the tokens
184
+ * @param tokenData - A list of token and approval address pairs
185
+ */
186
+ bulkGetTokenApproval: (signer: Signer, tokenData: RevokeTokenData[]) => Promise<{
187
+ token: Token;
188
+ approval: string | undefined;
189
+ }[]>;
190
+ /**
191
+ * Set approval for a certain token and amount.
192
+ * @param { ApproveTokenRequest } request - The approval request
193
+ */
194
+ approveToken: (request: ApproveTokenRequest) => Promise<void>;
195
+ /**
196
+ * Revoke approval for a certain token.
197
+ * @param { RevokeApprovalRequest } request - The revoke request
198
+ */
199
+ revokeTokenApproval: (request: RevokeApprovalRequest) => Promise<void>;
200
+ }
@@ -0,0 +1,379 @@
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 allowance_1 = require("./allowance");
16
+ const balances_1 = __importDefault(require("./balances"));
17
+ const connectors_1 = require("./connectors");
18
+ const StatusManager_1 = require("./execution/StatusManager");
19
+ const StepExecutor_1 = require("./execution/StepExecutor");
20
+ const helpers_1 = require("./helpers");
21
+ const ApiService_1 = __importDefault(require("./services/ApiService"));
22
+ const ChainsService_1 = __importDefault(require("./services/ChainsService"));
23
+ const ConfigService_1 = __importDefault(require("./services/ConfigService"));
24
+ const typeguards_1 = require("./typeguards");
25
+ const errors_1 = require("./utils/errors");
26
+ const preRestart_1 = require("./utils/preRestart");
27
+ const utils_1 = require("./utils/utils");
28
+ const version_1 = require("./version");
29
+ class LIFI {
30
+ constructor(configUpdate) {
31
+ this.activeRouteDictionary = {};
32
+ /**
33
+ * Get the current configuration of the SDK
34
+ * @return {Config} - The config object
35
+ */
36
+ this.getConfig = () => {
37
+ return this.configService.getConfig();
38
+ };
39
+ /**
40
+ * Get the SDK configuration after all setup calls are finished
41
+ * @return {Promise<Config>} - The config object
42
+ */
43
+ this.getConfigAsync = () => {
44
+ return this.configService.getConfigAsync();
45
+ };
46
+ /**
47
+ * Get an instance of a provider for a specific cahin
48
+ * @param {number} chainId - Id of the chain the provider is for
49
+ * @param {boolean} archive - Whether to use an archive provider that is based on a default rpc or not. defaults to false
50
+ * @return {FallbackProvider} The provider for the given chain
51
+ */
52
+ this.getRpcProvider = (chainId, archive = false) => {
53
+ return (0, connectors_1.getRpcProvider)(chainId, archive);
54
+ };
55
+ /**
56
+ * Set a new confuration for the SDK
57
+ * @param {ConfigUpdate} configUpdate - An object containing the configuration fields that should be updated.
58
+ * @return {Config} The renewed config object
59
+ */
60
+ this.setConfig = (configUpdate) => {
61
+ return this.configService.updateConfig(configUpdate);
62
+ };
63
+ /**
64
+ * Get a set of current possibilities based on a request that specifies which chains, exchanges and bridges are preferred or unwanted.
65
+ * @param {PossibilitiesRequest} request - Object defining preferences regarding chain, exchanges and bridges
66
+ * @return {Promise<PossibilitiesResponse>} Object listing current possibilities for any-to-any cross-chain-swaps based on the provided preferences.
67
+ * @throws {LifiError} Throws a LifiError if request fails.
68
+ */
69
+ this.getPossibilities = (request, options) => __awaiter(this, void 0, void 0, function* () {
70
+ return ApiService_1.default.getPossibilities(request, options);
71
+ });
72
+ /**
73
+ * Fetch information about a Token
74
+ * @param {ChainKey | ChainId} chain - Id or key of the chain that contains the token
75
+ * @param {string} token - Address or symbol of the token on the requested chain
76
+ * @throws {LifiError} - Throws a LifiError if request fails
77
+ */
78
+ this.getToken = (chain, token, options) => __awaiter(this, void 0, void 0, function* () {
79
+ return ApiService_1.default.getToken(chain, token, options);
80
+ });
81
+ /**
82
+ * Get a quote for a token transfer
83
+ * @param {QuoteRequest} request - The configuration of the requested quote
84
+ * @throws {LifiError} - Throws a LifiError if request fails
85
+ */
86
+ this.getQuote = (request, options) => __awaiter(this, void 0, void 0, function* () {
87
+ return ApiService_1.default.getQuote(request, options);
88
+ });
89
+ /**
90
+ * Get a quote for a destination contract call
91
+ * @param {ContractCallQuoteRequest} request - The configuration of the requested destination call
92
+ * @throws {LifiError} - Throws a LifiError if request fails
93
+ */
94
+ this.getContractCallQuote = (request, options) => __awaiter(this, void 0, void 0, function* () {
95
+ return ApiService_1.default.getContractCallQuote(request, options);
96
+ });
97
+ /**
98
+ * Check the status of a transfer. For cross chain transfers, the "bridge" parameter is required.
99
+ * @param {GetStatusRequest} request - Configuration of the requested status
100
+ * @throws {LifiError} - Throws a LifiError if request fails
101
+ */
102
+ this.getStatus = (request, options) => __awaiter(this, void 0, void 0, function* () {
103
+ return ApiService_1.default.getStatus(request, options);
104
+ });
105
+ /**
106
+ * Get the available tools to bridge and swap tokens.
107
+ * @param {ToolsRequest?} request - The configuration of the requested tools
108
+ * @returns The tools that are available on the requested chains
109
+ */
110
+ this.getTools = (request, options) => __awaiter(this, void 0, void 0, function* () {
111
+ return ApiService_1.default.getTools(request || {}, options);
112
+ });
113
+ /**
114
+ * Get all known tokens.
115
+ * @param {TokensRequest?} request - The configuration of the requested tokens
116
+ * @returns The tokens that are available on the requested chains
117
+ */
118
+ this.getTokens = (request, options) => __awaiter(this, void 0, void 0, function* () {
119
+ return ApiService_1.default.getTokens(request || {}, options);
120
+ });
121
+ /**
122
+ * Get all available chains
123
+ * @return {Promise<Chain[]>} A list of all available chains
124
+ * @throws {LifiError} Throws a LifiError if request fails.
125
+ */
126
+ this.getChains = () => __awaiter(this, void 0, void 0, function* () {
127
+ return this.chainsService.getChains();
128
+ });
129
+ /**
130
+ * Get a set of routes for a request that describes a transfer of tokens.
131
+ * @param {RoutesRequest} routesRequest - A description of the transfer.
132
+ * @return {Promise<RoutesResponse>} The resulting routes that can be used to realize the described transfer of tokens.
133
+ * @throws {LifiError} Throws a LifiError if request fails.
134
+ */
135
+ this.getRoutes = (request, options) => __awaiter(this, void 0, void 0, function* () {
136
+ return ApiService_1.default.getRoutes(request, options);
137
+ });
138
+ /**
139
+ * Get the transaction data for a single step of a route
140
+ * @param {Step} step - The step object.
141
+ * @return {Promise<Step>} The step populated with the transaction data.
142
+ * @throws {LifiError} Throws a LifiError if request fails.
143
+ */
144
+ this.getStepTransaction = (step, options) => __awaiter(this, void 0, void 0, function* () {
145
+ return ApiService_1.default.getStepTransaction(step, options);
146
+ });
147
+ /**
148
+ * Stops the execution of an active route.
149
+ * @param {Route} route - A route that is currently in execution.
150
+ * @return {Route} The stopped route.
151
+ */
152
+ this.stopExecution = (route) => {
153
+ if (!this.activeRouteDictionary[route.id]) {
154
+ return route;
155
+ }
156
+ for (const executor of this.activeRouteDictionary[route.id].executors) {
157
+ executor.stopStepExecution({ allowUpdates: false });
158
+ }
159
+ delete this.activeRouteDictionary[route.id];
160
+ return route;
161
+ };
162
+ /**
163
+ * Executes a route until a user interaction is necessary (signing transactions, etc.) and then halts until the route is resumed.
164
+ * @param {Route} route - A route that is currently in execution.
165
+ */
166
+ this.moveExecutionToBackground = (route) => {
167
+ if (!this.activeRouteDictionary[route.id]) {
168
+ return;
169
+ }
170
+ for (const executor of this.activeRouteDictionary[route.id].executors) {
171
+ executor.stopStepExecution({ allowUpdates: true });
172
+ }
173
+ };
174
+ /**
175
+ * Execute a route.
176
+ * @param {Signer} signer - The signer required to send the transactions.
177
+ * @param {Route} route - The route that should be executed. Cannot be an active route.
178
+ * @param {ExecutionSettings} settings - An object containing settings and callbacks.
179
+ * @return {Promise<Route>} The executed route.
180
+ * @throws {LifiError} Throws a LifiError if the execution fails.
181
+ */
182
+ this.executeRoute = (signer, route, settings) => __awaiter(this, void 0, void 0, function* () {
183
+ const clonedRoute = (0, utils_1.deepClone)(route); // deep clone to prevent side effects
184
+ // check if route is already running
185
+ if (this.activeRouteDictionary[clonedRoute.id]) {
186
+ // TODO: maybe inform user why nothing happens?
187
+ return clonedRoute;
188
+ }
189
+ return this.executeSteps(signer, clonedRoute, settings);
190
+ });
191
+ /**
192
+ * Resume the execution of a route that has been stopped or had an error while executing.
193
+ * @param {Signer} signer - The signer required to send the transactions.
194
+ * @param {Route} route - The route that is to be executed. Cannot be an active route.
195
+ * @param {ExecutionSettings} settings - An object containing settings and callbacks.
196
+ * @return {Promise<Route>} The executed route.
197
+ * @throws {LifiError} Throws a LifiError if the execution fails.
198
+ */
199
+ this.resumeRoute = (signer, route, settings) => __awaiter(this, void 0, void 0, function* () {
200
+ const clonedRoute = (0, utils_1.deepClone)(route); // deep clone to prevent side effects
201
+ const activeRoute = this.activeRouteDictionary[clonedRoute.id];
202
+ if (activeRoute) {
203
+ const executionHalted = activeRoute.executors.some((executor) => executor.executionStopped);
204
+ if (!executionHalted) {
205
+ return clonedRoute;
206
+ }
207
+ }
208
+ (0, preRestart_1.handlePreRestart)(clonedRoute);
209
+ return this.executeSteps(signer, clonedRoute, settings);
210
+ });
211
+ this.executeSteps = (signer, route, settings) => __awaiter(this, void 0, void 0, function* () {
212
+ const config = this.configService.getConfig();
213
+ const execData = {
214
+ route,
215
+ executors: [],
216
+ settings: Object.assign(Object.assign({}, config.defaultExecutionSettings), settings),
217
+ };
218
+ this.activeRouteDictionary[route.id] = execData;
219
+ const statusManager = new StatusManager_1.StatusManager(route, this.activeRouteDictionary[route.id].settings, (route) => (this.activeRouteDictionary[route.id].route = route));
220
+ // loop over steps and execute them
221
+ for (let index = 0; index < route.steps.length; index++) {
222
+ //check if execution has stopped in meantime
223
+ if (!this.activeRouteDictionary[route.id]) {
224
+ break;
225
+ }
226
+ const step = route.steps[index];
227
+ const previousStep = index !== 0 ? route.steps[index - 1] : undefined;
228
+ // check if step already done
229
+ if (step.execution && step.execution.status === 'DONE') {
230
+ continue;
231
+ }
232
+ // update amount using output of previous execution. In the future this should be handled by calling `updateRoute`
233
+ if (previousStep &&
234
+ previousStep.execution &&
235
+ previousStep.execution.toAmount) {
236
+ step.action.fromAmount = previousStep.execution.toAmount;
237
+ }
238
+ let stepExecutor;
239
+ try {
240
+ stepExecutor = new StepExecutor_1.StepExecutor(statusManager, this.activeRouteDictionary[route.id].settings);
241
+ this.activeRouteDictionary[route.id].executors.push(stepExecutor);
242
+ yield stepExecutor.executeStep(signer, step);
243
+ }
244
+ catch (e) {
245
+ this.stopExecution(route);
246
+ throw e;
247
+ }
248
+ // execution stopped during the current step, we don't want to continue to the next step so we return already
249
+ if (stepExecutor.executionStopped) {
250
+ return route;
251
+ }
252
+ }
253
+ //clean up after execution
254
+ delete this.activeRouteDictionary[route.id];
255
+ return route;
256
+ });
257
+ /**
258
+ * Update the ExecutionSettings for an active route.
259
+ * @param {ExecutionSettings} settings - An object with execution settings.
260
+ * @param {Route} route - The active route that gets the new execution settings.
261
+ * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
262
+ */
263
+ this.updateExecutionSettings = (settings, route) => {
264
+ if (!this.activeRouteDictionary[route.id]) {
265
+ throw new errors_1.ValidationError("Can't set ExecutionSettings for the inactive route.");
266
+ }
267
+ const config = this.configService.getConfig();
268
+ this.activeRouteDictionary[route.id].settings = Object.assign(Object.assign({}, config.defaultExecutionSettings), settings);
269
+ };
270
+ /**
271
+ * Get the list of active routes.
272
+ * @return {Route[]} A list of routes.
273
+ */
274
+ this.getActiveRoutes = () => {
275
+ return Object.values(this.activeRouteDictionary).map((dict) => dict.route);
276
+ };
277
+ /**
278
+ * Return the current route information for given route. The route has to be active.
279
+ * @param {Route} route - A route object.
280
+ * @return {Route} The updated route.
281
+ */
282
+ this.getActiveRoute = (route) => {
283
+ var _a;
284
+ return (_a = this.activeRouteDictionary[route.id]) === null || _a === void 0 ? void 0 : _a.route;
285
+ };
286
+ /**
287
+ * Returns the balances of a specific token a wallet holds across all aggregated chains.
288
+ * @param {string} walletAddress - A wallet address.
289
+ * @param {Token} token - A Token object.
290
+ * @return {Promise<TokenAmount | null>} An object containing the token and the amounts on different chains.
291
+ * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
292
+ */
293
+ this.getTokenBalance = (walletAddress, token) => __awaiter(this, void 0, void 0, function* () {
294
+ if (!walletAddress) {
295
+ throw new errors_1.ValidationError('Missing walletAddress.');
296
+ }
297
+ if (!(0, typeguards_1.isToken)(token)) {
298
+ throw new errors_1.ValidationError(`Invalid token passed: address "${token.address}" on chainId "${token.chainId}"`);
299
+ }
300
+ return balances_1.default.getTokenBalance(walletAddress, token);
301
+ });
302
+ /**
303
+ * Returns the balances for a list tokens a wallet holds across all aggregated chains.
304
+ * @param {string} walletAddress - A wallet address.
305
+ * @param {Token[]} tokens - A list of Token objects.
306
+ * @return {Promise<TokenAmount[]>} A list of objects containing the tokens and the amounts on different chains.
307
+ * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
308
+ */
309
+ this.getTokenBalances = (walletAddress, tokens) => __awaiter(this, void 0, void 0, function* () {
310
+ if (!walletAddress) {
311
+ throw new errors_1.ValidationError('Missing walletAddress.');
312
+ }
313
+ const invalidTokens = tokens.filter((token) => !(0, typeguards_1.isToken)(token));
314
+ if (invalidTokens.length) {
315
+ throw new errors_1.ValidationError(`Invalid token passed: address "${invalidTokens[0].address}" on chainId "${invalidTokens[0].chainId}"`);
316
+ }
317
+ return balances_1.default.getTokenBalances(walletAddress, tokens);
318
+ });
319
+ /**
320
+ * This method queries the balances of tokens for a specific list of chains for a given wallet.
321
+ * @param {string} walletAddress - A walletaddress.
322
+ * @param {{ [chainId: number]: Token[] }} tokensByChain - A list of Token objects organized by chain ids.
323
+ * @return {Promise<{ [chainId: number]: TokenAmount[] }} A list of objects containing the tokens and the amounts on different chains organized by the chosen chains.
324
+ * @throws {ValidationError} Throws a ValidationError if parameters are invalid.
325
+ */
326
+ this.getTokenBalancesForChains = (walletAddress, tokensByChain) => __awaiter(this, void 0, void 0, function* () {
327
+ if (!walletAddress) {
328
+ throw new errors_1.ValidationError('Missing walletAddress.');
329
+ }
330
+ const tokenList = Object.values(tokensByChain).flat();
331
+ const invalidTokens = tokenList.filter((token) => !(0, typeguards_1.isToken)(token));
332
+ if (invalidTokens.length) {
333
+ throw new errors_1.ValidationError(`Invalid token passed: address "${invalidTokens[0].address}" on chainId "${invalidTokens[0].chainId}"`);
334
+ }
335
+ return balances_1.default.getTokenBalancesForChains(walletAddress, tokensByChain);
336
+ });
337
+ /**
338
+ * Get the current approval for a certain token.
339
+ * @param signer - The signer owning the token
340
+ * @param token - The token that should be checked
341
+ * @param approvalAddress - The address that has be approved
342
+ */
343
+ this.getTokenApproval = (signer, token, approvalAddress) => __awaiter(this, void 0, void 0, function* () {
344
+ return (0, allowance_1.getTokenApproval)(signer, token, approvalAddress);
345
+ });
346
+ /**
347
+ * Get the current approval for a list of token / approval address pairs.
348
+ * @param signer - The signer owning the tokens
349
+ * @param tokenData - A list of token and approval address pairs
350
+ */
351
+ this.bulkGetTokenApproval = (signer, tokenData) => __awaiter(this, void 0, void 0, function* () {
352
+ return (0, allowance_1.bulkGetTokenApproval)(signer, tokenData);
353
+ });
354
+ /**
355
+ * Set approval for a certain token and amount.
356
+ * @param { ApproveTokenRequest } request - The approval request
357
+ */
358
+ this.approveToken = (request) => {
359
+ return (0, allowance_1.approveToken)(request);
360
+ };
361
+ /**
362
+ * Revoke approval for a certain token.
363
+ * @param { RevokeApprovalRequest } request - The revoke request
364
+ */
365
+ this.revokeTokenApproval = (request) => {
366
+ return (0, allowance_1.revokeTokenApproval)(request);
367
+ };
368
+ this.configService = ConfigService_1.default.getInstance();
369
+ if (configUpdate) {
370
+ this.configService.updateConfig(configUpdate); // update API urls before we request chains
371
+ }
372
+ this.chainsService = ChainsService_1.default.getInstance();
373
+ this.chainsService.getChains().then((chains) => {
374
+ this.configService.updateChains(chains);
375
+ });
376
+ (0, helpers_1.checkPackageUpdates)(version_1.name, version_1.version, configUpdate === null || configUpdate === void 0 ? void 0 : configUpdate.disableVersionCheck);
377
+ }
378
+ }
379
+ exports.default = LIFI;
@@ -0,0 +1,22 @@
1
+ import { Token } from '@lifi/types';
2
+ import { Signer } from 'ethers';
3
+ import { RevokeTokenData } from '../types';
4
+ export interface ApproveTokenRequest {
5
+ signer: Signer;
6
+ token: Token;
7
+ approvalAddress: string;
8
+ amount: string;
9
+ infiniteApproval?: boolean;
10
+ }
11
+ export interface RevokeApprovalRequest {
12
+ signer: Signer;
13
+ token: Token;
14
+ approvalAddress: string;
15
+ }
16
+ export declare const getTokenApproval: (signer: Signer, token: Token, approvalAddress: string) => Promise<string | undefined>;
17
+ export declare const bulkGetTokenApproval: (signer: Signer, tokenData: RevokeTokenData[]) => Promise<{
18
+ token: Token;
19
+ approval: string | undefined;
20
+ }[]>;
21
+ export declare const approveToken: ({ signer, token, approvalAddress, amount, infiniteApproval, }: ApproveTokenRequest) => Promise<void>;
22
+ export declare const revokeTokenApproval: ({ signer, token, approvalAddress, }: RevokeApprovalRequest) => Promise<void>;