@lifi/sdk 1.6.3 → 1.7.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.
- package/CHANGELOG.md +9 -0
- package/dist/Lifi.d.ts +1 -1
- package/dist/Lifi.js +5 -5
- package/dist/{cjs/execution/allowance.execute.d.ts → allowance/checkAllowance.d.ts} +1 -1
- package/dist/{execution/allowance.execute.js → allowance/checkAllowance.js} +1 -0
- package/dist/allowance/index.d.ts +2 -22
- package/dist/allowance/index.js +2 -68
- package/dist/allowance/tokenApproval.d.ts +22 -0
- package/dist/allowance/tokenApproval.js +68 -0
- package/dist/balance/checkBalance.d.ts +3 -0
- package/dist/{execution/balanceCheck.execute.js → balance/checkBalance.js} +12 -6
- package/dist/balance/getTokenBalance.d.ts +8 -0
- package/dist/{balances/index.js → balance/getTokenBalance.js} +3 -8
- package/dist/balance/index.d.ts +2 -0
- package/dist/balance/index.js +2 -0
- package/dist/{balances → balance}/utils.d.ts +0 -0
- package/dist/{balances → balance}/utils.js +1 -1
- package/dist/cjs/Lifi.d.ts +1 -1
- package/dist/cjs/Lifi.js +28 -5
- package/dist/{execution/allowance.execute.d.ts → cjs/allowance/checkAllowance.d.ts} +1 -1
- package/dist/cjs/{execution/allowance.execute.js → allowance/checkAllowance.js} +1 -0
- package/dist/cjs/allowance/index.d.ts +2 -22
- package/dist/cjs/allowance/index.js +15 -75
- package/dist/cjs/allowance/tokenApproval.d.ts +22 -0
- package/dist/cjs/allowance/tokenApproval.js +78 -0
- package/dist/cjs/balance/checkBalance.d.ts +3 -0
- package/dist/cjs/{execution/balanceCheck.execute.js → balance/checkBalance.js} +13 -7
- package/dist/cjs/balance/getTokenBalance.d.ts +8 -0
- package/dist/cjs/{balances/index.js → balance/getTokenBalance.js} +6 -7
- package/dist/cjs/balance/index.d.ts +2 -0
- package/dist/cjs/balance/index.js +18 -0
- package/dist/cjs/{balances → balance}/utils.d.ts +0 -0
- package/dist/cjs/{balances → balance}/utils.js +1 -1
- package/dist/cjs/execution/{exchanges/swap.execute.d.ts → ExecutionManager.d.ts} +3 -3
- package/dist/cjs/execution/{bridges/bridge.execute.js → ExecutionManager.js} +52 -37
- package/dist/cjs/execution/StatusManager.d.ts +6 -3
- package/dist/cjs/execution/StatusManager.js +1 -3
- package/dist/cjs/execution/StepExecutor.d.ts +3 -5
- package/dist/cjs/execution/StepExecutor.js +7 -32
- package/dist/cjs/execution/stepComparison.d.ts +4 -4
- package/dist/cjs/execution/stepComparison.js +11 -7
- package/dist/cjs/execution/switchChain.js +1 -1
- package/dist/cjs/execution/utils.d.ts +1 -1
- package/dist/cjs/execution/utils.js +11 -10
- package/dist/cjs/services/ConfigService.js +1 -0
- package/dist/cjs/types/internal.types.d.ts +18 -24
- package/dist/cjs/utils/errors.d.ts +7 -3
- package/dist/cjs/utils/errors.js +9 -2
- package/dist/cjs/utils/multicall.d.ts +1 -1
- package/dist/cjs/utils/parseError.d.ts +0 -1
- package/dist/cjs/utils/parseError.js +1 -7
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/execution/{exchanges/swap.execute.d.ts → ExecutionManager.d.ts} +3 -3
- package/dist/execution/{bridges/bridge.execute.js → ExecutionManager.js} +49 -34
- package/dist/execution/StatusManager.d.ts +6 -3
- package/dist/execution/StatusManager.js +1 -3
- package/dist/execution/StepExecutor.d.ts +3 -5
- package/dist/execution/StepExecutor.js +7 -32
- package/dist/execution/stepComparison.d.ts +4 -4
- package/dist/execution/stepComparison.js +12 -8
- package/dist/execution/switchChain.js +1 -1
- package/dist/execution/utils.d.ts +1 -1
- package/dist/execution/utils.js +9 -8
- package/dist/services/ConfigService.js +1 -0
- package/dist/types/internal.types.d.ts +18 -24
- package/dist/utils/errors.d.ts +7 -3
- package/dist/utils/errors.js +7 -1
- package/dist/utils/multicall.d.ts +1 -1
- package/dist/utils/parseError.d.ts +0 -1
- package/dist/utils/parseError.js +0 -5
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +13 -15
- package/dist/balances/index.d.ts +0 -11
- package/dist/cjs/balances/index.d.ts +0 -11
- package/dist/cjs/execution/balanceCheck.execute.d.ts +0 -3
- package/dist/cjs/execution/bridges/bridge.execute.d.ts +0 -7
- package/dist/cjs/execution/exchanges/swap.execute.js +0 -152
- package/dist/execution/balanceCheck.execute.d.ts +0 -3
- package/dist/execution/bridges/bridge.execute.d.ts +0 -7
- package/dist/execution/exchanges/swap.execute.js +0 -145
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,15 @@
|
|
|
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.7.0](https://github.com/lifinance/sdk/compare/v1.6.4...v1.7.0) (2022-11-23)
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
* add acceptExchangeRateUpdateHook and return gas info during route execution ([#111](https://github.com/lifinance/sdk/issues/111)) ([65c4cb1](https://github.com/lifinance/sdk/commit/65c4cb1c68fc0e7b60f1c19a42105772998cdb16))
|
|
11
|
+
|
|
12
|
+
### [1.6.4](https://github.com/lifinance/sdk/compare/v1.6.3...v1.6.4) (2022-11-16)
|
|
13
|
+
|
|
5
14
|
### [1.6.3](https://github.com/lifinance/sdk/compare/v1.6.2...v1.6.3) (2022-10-26)
|
|
6
15
|
|
|
7
16
|
|
package/dist/Lifi.d.ts
CHANGED
|
@@ -170,7 +170,7 @@ export default class LIFI {
|
|
|
170
170
|
* This method queries the balances of tokens for a specific list of chains for a given wallet.
|
|
171
171
|
* @param {string} walletAddress - A walletaddress.
|
|
172
172
|
* @param {{ [chainId: number]: Token[] }} tokensByChain - A list of Token objects organized by chain ids.
|
|
173
|
-
* @return {Promise<{ [chainId: number]: TokenAmount[] }} A list of objects containing the tokens and the amounts on different chains organized by the chosen chains.
|
|
173
|
+
* @return {Promise<{ [chainId: number]: TokenAmount[] }>} A list of objects containing the tokens and the amounts on different chains organized by the chosen chains.
|
|
174
174
|
* @throws {ValidationError} Throws a ValidationError if parameters are invalid.
|
|
175
175
|
*/
|
|
176
176
|
getTokenBalancesForChains: (walletAddress: string, tokensByChain: {
|
package/dist/Lifi.js
CHANGED
|
@@ -8,7 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import { approveToken, bulkGetTokenApproval, getTokenApproval, revokeTokenApproval, } from './allowance';
|
|
11
|
-
import
|
|
11
|
+
import * as balance from './balance';
|
|
12
12
|
import { getRpcProvider } from './connectors';
|
|
13
13
|
import { StatusManager } from './execution/StatusManager';
|
|
14
14
|
import { StepExecutor } from './execution/StepExecutor';
|
|
@@ -333,7 +333,7 @@ export default class LIFI {
|
|
|
333
333
|
if (!isToken(token)) {
|
|
334
334
|
throw new ValidationError(`Invalid token passed: address "${token.address}" on chainId "${token.chainId}"`);
|
|
335
335
|
}
|
|
336
|
-
return
|
|
336
|
+
return balance.getTokenBalance(walletAddress, token);
|
|
337
337
|
});
|
|
338
338
|
/**
|
|
339
339
|
* Returns the balances for a list tokens a wallet holds across all aggregated chains.
|
|
@@ -350,13 +350,13 @@ export default class LIFI {
|
|
|
350
350
|
if (invalidTokens.length) {
|
|
351
351
|
throw new ValidationError(`Invalid token passed: address "${invalidTokens[0].address}" on chainId "${invalidTokens[0].chainId}"`);
|
|
352
352
|
}
|
|
353
|
-
return
|
|
353
|
+
return balance.getTokenBalances(walletAddress, tokens);
|
|
354
354
|
});
|
|
355
355
|
/**
|
|
356
356
|
* This method queries the balances of tokens for a specific list of chains for a given wallet.
|
|
357
357
|
* @param {string} walletAddress - A walletaddress.
|
|
358
358
|
* @param {{ [chainId: number]: Token[] }} tokensByChain - A list of Token objects organized by chain ids.
|
|
359
|
-
* @return {Promise<{ [chainId: number]: TokenAmount[] }} A list of objects containing the tokens and the amounts on different chains organized by the chosen chains.
|
|
359
|
+
* @return {Promise<{ [chainId: number]: TokenAmount[] }>} A list of objects containing the tokens and the amounts on different chains organized by the chosen chains.
|
|
360
360
|
* @throws {ValidationError} Throws a ValidationError if parameters are invalid.
|
|
361
361
|
*/
|
|
362
362
|
this.getTokenBalancesForChains = (walletAddress, tokensByChain) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -368,7 +368,7 @@ export default class LIFI {
|
|
|
368
368
|
if (invalidTokens.length) {
|
|
369
369
|
throw new ValidationError(`Invalid token passed: address "${invalidTokens[0].address}" on chainId "${invalidTokens[0].chainId}"`);
|
|
370
370
|
}
|
|
371
|
-
return
|
|
371
|
+
return balance.getTokenBalancesForChains(walletAddress, tokensByChain);
|
|
372
372
|
});
|
|
373
373
|
/**
|
|
374
374
|
* Get the current approval for a certain token.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Signer } from 'ethers';
|
|
2
|
+
import { StatusManager } from '../execution/StatusManager';
|
|
2
3
|
import { Chain, InternalExecutionSettings, Step } from '../types';
|
|
3
|
-
import { StatusManager } from './StatusManager';
|
|
4
4
|
export declare const checkAllowance: (signer: Signer, step: Step, statusManager: StatusManager, settings: InternalExecutionSettings, chain: Chain, allowUserInteraction?: boolean) => Promise<void>;
|
|
@@ -7,6 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
10
11
|
import BigNumber from 'bignumber.js';
|
|
11
12
|
import { constants } from 'ethers';
|
|
12
13
|
import { getApproved, setApproval } from '../allowance/utils';
|
|
@@ -1,22 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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>;
|
|
1
|
+
export * from './checkAllowance';
|
|
2
|
+
export * from './tokenApproval';
|
package/dist/allowance/index.js
CHANGED
|
@@ -1,68 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import BigNumber from 'bignumber.js';
|
|
11
|
-
import { constants } from 'ethers';
|
|
12
|
-
import { isSameToken } from '../helpers';
|
|
13
|
-
import { isNativeTokenAddress } from '../utils/utils';
|
|
14
|
-
import { getAllowanceViaMulticall, getApproved, groupByChain, setApproval, } from './utils';
|
|
15
|
-
export const getTokenApproval = (signer, token, approvalAddress) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
-
// native token don't need approval
|
|
17
|
-
if (isNativeTokenAddress(token.address)) {
|
|
18
|
-
return;
|
|
19
|
-
}
|
|
20
|
-
const approved = yield getApproved(signer, token.address, approvalAddress);
|
|
21
|
-
return approved.toString();
|
|
22
|
-
});
|
|
23
|
-
export const bulkGetTokenApproval = (signer, tokenData) => __awaiter(void 0, void 0, void 0, function* () {
|
|
24
|
-
// filter out native tokens
|
|
25
|
-
const filteredTokenData = tokenData.filter(({ token }) => !isNativeTokenAddress(token.address));
|
|
26
|
-
// group by chain
|
|
27
|
-
const tokenDataByChain = groupByChain(filteredTokenData);
|
|
28
|
-
const approvalPromises = Object.keys(tokenDataByChain).map((chainId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
29
|
-
const parsedChainId = Number.parseInt(chainId);
|
|
30
|
-
// get allowances for current chain and token list
|
|
31
|
-
return getAllowanceViaMulticall(signer, parsedChainId, tokenDataByChain[parsedChainId]);
|
|
32
|
-
}));
|
|
33
|
-
const approvalsByChain = yield Promise.all(approvalPromises);
|
|
34
|
-
const approvals = approvalsByChain.flat();
|
|
35
|
-
return tokenData.map(({ token }) => {
|
|
36
|
-
// native token don't need approval
|
|
37
|
-
if (isNativeTokenAddress(token.address)) {
|
|
38
|
-
return { token, approval: undefined };
|
|
39
|
-
}
|
|
40
|
-
const approved = approvals.find((approval) => isSameToken(approval.token, token));
|
|
41
|
-
return { token, approval: approved === null || approved === void 0 ? void 0 : approved.approvedAmount.toString() };
|
|
42
|
-
});
|
|
43
|
-
});
|
|
44
|
-
export const approveToken = ({ signer, token, approvalAddress, amount, infiniteApproval = false, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
45
|
-
// native token don't need approval
|
|
46
|
-
if (isNativeTokenAddress(token.address)) {
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
const approvedAmount = yield getApproved(signer, token.address, approvalAddress);
|
|
50
|
-
if (new BigNumber(amount).gt(approvedAmount)) {
|
|
51
|
-
const approvalAmount = infiniteApproval
|
|
52
|
-
? constants.MaxUint256.toString()
|
|
53
|
-
: amount;
|
|
54
|
-
const approveTx = yield setApproval(signer, token.address, approvalAddress, approvalAmount);
|
|
55
|
-
yield approveTx.wait();
|
|
56
|
-
}
|
|
57
|
-
});
|
|
58
|
-
export const revokeTokenApproval = ({ signer, token, approvalAddress, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
|
-
// native token don't need approval
|
|
60
|
-
if (isNativeTokenAddress(token.address)) {
|
|
61
|
-
return;
|
|
62
|
-
}
|
|
63
|
-
const approvedAmount = yield getApproved(signer, token.address, approvalAddress);
|
|
64
|
-
if (!approvedAmount.isZero()) {
|
|
65
|
-
const approveTx = yield setApproval(signer, token.address, approvalAddress, '0');
|
|
66
|
-
yield approveTx.wait();
|
|
67
|
-
}
|
|
68
|
-
});
|
|
1
|
+
export * from './checkAllowance';
|
|
2
|
+
export * from './tokenApproval';
|
|
@@ -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>;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import BigNumber from 'bignumber.js';
|
|
11
|
+
import { constants } from 'ethers';
|
|
12
|
+
import { isSameToken } from '../helpers';
|
|
13
|
+
import { isNativeTokenAddress } from '../utils/utils';
|
|
14
|
+
import { getAllowanceViaMulticall, getApproved, groupByChain, setApproval, } from './utils';
|
|
15
|
+
export const getTokenApproval = (signer, token, approvalAddress) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
+
// native token don't need approval
|
|
17
|
+
if (isNativeTokenAddress(token.address)) {
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const approved = yield getApproved(signer, token.address, approvalAddress);
|
|
21
|
+
return approved.toString();
|
|
22
|
+
});
|
|
23
|
+
export const bulkGetTokenApproval = (signer, tokenData) => __awaiter(void 0, void 0, void 0, function* () {
|
|
24
|
+
// filter out native tokens
|
|
25
|
+
const filteredTokenData = tokenData.filter(({ token }) => !isNativeTokenAddress(token.address));
|
|
26
|
+
// group by chain
|
|
27
|
+
const tokenDataByChain = groupByChain(filteredTokenData);
|
|
28
|
+
const approvalPromises = Object.keys(tokenDataByChain).map((chainId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
29
|
+
const parsedChainId = Number.parseInt(chainId);
|
|
30
|
+
// get allowances for current chain and token list
|
|
31
|
+
return getAllowanceViaMulticall(signer, parsedChainId, tokenDataByChain[parsedChainId]);
|
|
32
|
+
}));
|
|
33
|
+
const approvalsByChain = yield Promise.all(approvalPromises);
|
|
34
|
+
const approvals = approvalsByChain.flat();
|
|
35
|
+
return tokenData.map(({ token }) => {
|
|
36
|
+
// native token don't need approval
|
|
37
|
+
if (isNativeTokenAddress(token.address)) {
|
|
38
|
+
return { token, approval: undefined };
|
|
39
|
+
}
|
|
40
|
+
const approved = approvals.find((approval) => isSameToken(approval.token, token));
|
|
41
|
+
return { token, approval: approved === null || approved === void 0 ? void 0 : approved.approvedAmount.toString() };
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
export const approveToken = ({ signer, token, approvalAddress, amount, infiniteApproval = false, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
45
|
+
// native token don't need approval
|
|
46
|
+
if (isNativeTokenAddress(token.address)) {
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
const approvedAmount = yield getApproved(signer, token.address, approvalAddress);
|
|
50
|
+
if (new BigNumber(amount).gt(approvedAmount)) {
|
|
51
|
+
const approvalAmount = infiniteApproval
|
|
52
|
+
? constants.MaxUint256.toString()
|
|
53
|
+
: amount;
|
|
54
|
+
const approveTx = yield setApproval(signer, token.address, approvalAddress, approvalAmount);
|
|
55
|
+
yield approveTx.wait();
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
export const revokeTokenApproval = ({ signer, token, approvalAddress, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
59
|
+
// native token don't need approval
|
|
60
|
+
if (isNativeTokenAddress(token.address)) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const approvedAmount = yield getApproved(signer, token.address, approvalAddress);
|
|
64
|
+
if (!approvedAmount.isZero()) {
|
|
65
|
+
const approveTx = yield setApproval(signer, token.address, approvalAddress, '0');
|
|
66
|
+
yield approveTx.wait();
|
|
67
|
+
}
|
|
68
|
+
});
|
|
@@ -8,15 +8,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import BigNumber from 'bignumber.js';
|
|
11
|
-
import
|
|
12
|
-
import {
|
|
13
|
-
export const
|
|
14
|
-
const tokenAmount = yield
|
|
11
|
+
import { BalanceError } from '../utils/errors';
|
|
12
|
+
import { getTokenBalance } from './getTokenBalance';
|
|
13
|
+
export const checkBalance = (signer, step, depth = 0) => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
|
+
const tokenAmount = yield getTokenBalance(yield signer.getAddress(), step.action.fromToken);
|
|
15
15
|
if (tokenAmount) {
|
|
16
16
|
const currentBalance = new BigNumber(tokenAmount.amount).shiftedBy(tokenAmount.decimals);
|
|
17
17
|
const neededBalance = new BigNumber(step.action.fromAmount);
|
|
18
18
|
if (currentBalance.lt(neededBalance)) {
|
|
19
|
-
if (
|
|
19
|
+
if (depth <= 3) {
|
|
20
|
+
yield new Promise((resolve) => {
|
|
21
|
+
setTimeout(resolve, 200);
|
|
22
|
+
});
|
|
23
|
+
yield checkBalance(signer, step, depth + 1);
|
|
24
|
+
}
|
|
25
|
+
else if (neededBalance.multipliedBy(1 - step.action.slippage).lte(currentBalance)) {
|
|
20
26
|
// adjust amount in slippage limits
|
|
21
27
|
step.action.fromAmount = currentBalance.toFixed(0);
|
|
22
28
|
}
|
|
@@ -34,7 +40,7 @@ export const balanceCheck = (signer, step) => __awaiter(void 0, void 0, void 0,
|
|
|
34
40
|
`If the problem consists, please delete this transfer and ` +
|
|
35
41
|
`start a new one with a maximum of ${current} ${tokenAmount.symbol}.`;
|
|
36
42
|
}
|
|
37
|
-
throw new
|
|
43
|
+
throw new BalanceError('The balance is too low.', errorMessage);
|
|
38
44
|
}
|
|
39
45
|
}
|
|
40
46
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Token, TokenAmount } from '@lifi/types';
|
|
2
|
+
export declare const getTokenBalance: (walletAddress: string, token: Token) => Promise<TokenAmount | null>;
|
|
3
|
+
export declare const getTokenBalances: (walletAddress: string, tokens: Token[]) => Promise<TokenAmount[]>;
|
|
4
|
+
export declare const getTokenBalancesForChains: (walletAddress: string, tokensByChain: {
|
|
5
|
+
[chainId: number]: Token[];
|
|
6
|
+
}) => Promise<{
|
|
7
|
+
[chainId: number]: TokenAmount[];
|
|
8
|
+
}>;
|
|
@@ -8,11 +8,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import utils from './utils';
|
|
11
|
-
const getTokenBalance = (walletAddress, token) => __awaiter(void 0, void 0, void 0, function* () {
|
|
11
|
+
export const getTokenBalance = (walletAddress, token) => __awaiter(void 0, void 0, void 0, function* () {
|
|
12
12
|
const tokenAmounts = yield getTokenBalances(walletAddress, [token]);
|
|
13
13
|
return tokenAmounts.length ? tokenAmounts[0] : null;
|
|
14
14
|
});
|
|
15
|
-
const getTokenBalances = (walletAddress, tokens) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
|
+
export const getTokenBalances = (walletAddress, tokens) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
16
|
// split by chain
|
|
17
17
|
const tokensByChain = {};
|
|
18
18
|
tokens.forEach((token) => {
|
|
@@ -24,7 +24,7 @@ const getTokenBalances = (walletAddress, tokens) => __awaiter(void 0, void 0, vo
|
|
|
24
24
|
const tokenAmountsByChain = yield getTokenBalancesForChains(walletAddress, tokensByChain);
|
|
25
25
|
return Object.values(tokenAmountsByChain).flat();
|
|
26
26
|
});
|
|
27
|
-
const getTokenBalancesForChains = (walletAddress, tokensByChain) => __awaiter(void 0, void 0, void 0, function* () {
|
|
27
|
+
export const getTokenBalancesForChains = (walletAddress, tokensByChain) => __awaiter(void 0, void 0, void 0, function* () {
|
|
28
28
|
const tokenAmountsByChain = {};
|
|
29
29
|
const promises = Object.keys(tokensByChain).map((chainIdStr) => __awaiter(void 0, void 0, void 0, function* () {
|
|
30
30
|
const chainId = parseInt(chainIdStr);
|
|
@@ -34,8 +34,3 @@ const getTokenBalancesForChains = (walletAddress, tokensByChain) => __awaiter(vo
|
|
|
34
34
|
yield Promise.allSettled(promises);
|
|
35
35
|
return tokenAmountsByChain;
|
|
36
36
|
});
|
|
37
|
-
export default {
|
|
38
|
-
getTokenBalance,
|
|
39
|
-
getTokenBalances,
|
|
40
|
-
getTokenBalancesForChains,
|
|
41
|
-
};
|
|
File without changes
|
|
@@ -40,7 +40,7 @@ const getBalances = (walletAddress, tokens) => __awaiter(void 0, void 0, void 0,
|
|
|
40
40
|
tokens.forEach((token) => {
|
|
41
41
|
if (token.chainId !== chainId) {
|
|
42
42
|
// eslint-disable-next-line no-console
|
|
43
|
-
console.warn(`Requested tokens have to be on same chain.`);
|
|
43
|
+
console.warn(`Requested tokens have to be on the same chain.`);
|
|
44
44
|
return [];
|
|
45
45
|
}
|
|
46
46
|
});
|
package/dist/cjs/Lifi.d.ts
CHANGED
|
@@ -170,7 +170,7 @@ export default class LIFI {
|
|
|
170
170
|
* This method queries the balances of tokens for a specific list of chains for a given wallet.
|
|
171
171
|
* @param {string} walletAddress - A walletaddress.
|
|
172
172
|
* @param {{ [chainId: number]: Token[] }} tokensByChain - A list of Token objects organized by chain ids.
|
|
173
|
-
* @return {Promise<{ [chainId: number]: TokenAmount[] }} A list of objects containing the tokens and the amounts on different chains organized by the chosen chains.
|
|
173
|
+
* @return {Promise<{ [chainId: number]: TokenAmount[] }>} A list of objects containing the tokens and the amounts on different chains organized by the chosen chains.
|
|
174
174
|
* @throws {ValidationError} Throws a ValidationError if parameters are invalid.
|
|
175
175
|
*/
|
|
176
176
|
getTokenBalancesForChains: (walletAddress: string, tokensByChain: {
|
package/dist/cjs/Lifi.js
CHANGED
|
@@ -1,4 +1,27 @@
|
|
|
1
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
26
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
27
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -13,7 +36,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
13
36
|
};
|
|
14
37
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
38
|
const allowance_1 = require("./allowance");
|
|
16
|
-
const
|
|
39
|
+
const balance = __importStar(require("./balance"));
|
|
17
40
|
const connectors_1 = require("./connectors");
|
|
18
41
|
const StatusManager_1 = require("./execution/StatusManager");
|
|
19
42
|
const StepExecutor_1 = require("./execution/StepExecutor");
|
|
@@ -338,7 +361,7 @@ class LIFI {
|
|
|
338
361
|
if (!(0, typeguards_1.isToken)(token)) {
|
|
339
362
|
throw new errors_1.ValidationError(`Invalid token passed: address "${token.address}" on chainId "${token.chainId}"`);
|
|
340
363
|
}
|
|
341
|
-
return
|
|
364
|
+
return balance.getTokenBalance(walletAddress, token);
|
|
342
365
|
});
|
|
343
366
|
/**
|
|
344
367
|
* Returns the balances for a list tokens a wallet holds across all aggregated chains.
|
|
@@ -355,13 +378,13 @@ class LIFI {
|
|
|
355
378
|
if (invalidTokens.length) {
|
|
356
379
|
throw new errors_1.ValidationError(`Invalid token passed: address "${invalidTokens[0].address}" on chainId "${invalidTokens[0].chainId}"`);
|
|
357
380
|
}
|
|
358
|
-
return
|
|
381
|
+
return balance.getTokenBalances(walletAddress, tokens);
|
|
359
382
|
});
|
|
360
383
|
/**
|
|
361
384
|
* This method queries the balances of tokens for a specific list of chains for a given wallet.
|
|
362
385
|
* @param {string} walletAddress - A walletaddress.
|
|
363
386
|
* @param {{ [chainId: number]: Token[] }} tokensByChain - A list of Token objects organized by chain ids.
|
|
364
|
-
* @return {Promise<{ [chainId: number]: TokenAmount[] }} A list of objects containing the tokens and the amounts on different chains organized by the chosen chains.
|
|
387
|
+
* @return {Promise<{ [chainId: number]: TokenAmount[] }>} A list of objects containing the tokens and the amounts on different chains organized by the chosen chains.
|
|
365
388
|
* @throws {ValidationError} Throws a ValidationError if parameters are invalid.
|
|
366
389
|
*/
|
|
367
390
|
this.getTokenBalancesForChains = (walletAddress, tokensByChain) => __awaiter(this, void 0, void 0, function* () {
|
|
@@ -373,7 +396,7 @@ class LIFI {
|
|
|
373
396
|
if (invalidTokens.length) {
|
|
374
397
|
throw new errors_1.ValidationError(`Invalid token passed: address "${invalidTokens[0].address}" on chainId "${invalidTokens[0].chainId}"`);
|
|
375
398
|
}
|
|
376
|
-
return
|
|
399
|
+
return balance.getTokenBalancesForChains(walletAddress, tokensByChain);
|
|
377
400
|
});
|
|
378
401
|
/**
|
|
379
402
|
* Get the current approval for a certain token.
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import { Signer } from 'ethers';
|
|
2
|
+
import { StatusManager } from '../execution/StatusManager';
|
|
2
3
|
import { Chain, InternalExecutionSettings, Step } from '../types';
|
|
3
|
-
import { StatusManager } from './StatusManager';
|
|
4
4
|
export declare const checkAllowance: (signer: Signer, step: Step, statusManager: StatusManager, settings: InternalExecutionSettings, chain: Chain, allowUserInteraction?: boolean) => Promise<void>;
|
|
@@ -13,6 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.checkAllowance = void 0;
|
|
16
|
+
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
16
17
|
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
17
18
|
const ethers_1 = require("ethers");
|
|
18
19
|
const utils_1 = require("../allowance/utils");
|
|
@@ -1,22 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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>;
|
|
1
|
+
export * from './checkAllowance';
|
|
2
|
+
export * from './tokenApproval';
|
|
@@ -1,78 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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);
|
|
13
15
|
};
|
|
14
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const ethers_1 = require("ethers");
|
|
18
|
-
const helpers_1 = require("../helpers");
|
|
19
|
-
const utils_1 = require("../utils/utils");
|
|
20
|
-
const utils_2 = require("./utils");
|
|
21
|
-
const getTokenApproval = (signer, token, approvalAddress) => __awaiter(void 0, void 0, void 0, function* () {
|
|
22
|
-
// native token don't need approval
|
|
23
|
-
if ((0, utils_1.isNativeTokenAddress)(token.address)) {
|
|
24
|
-
return;
|
|
25
|
-
}
|
|
26
|
-
const approved = yield (0, utils_2.getApproved)(signer, token.address, approvalAddress);
|
|
27
|
-
return approved.toString();
|
|
28
|
-
});
|
|
29
|
-
exports.getTokenApproval = getTokenApproval;
|
|
30
|
-
const bulkGetTokenApproval = (signer, tokenData) => __awaiter(void 0, void 0, void 0, function* () {
|
|
31
|
-
// filter out native tokens
|
|
32
|
-
const filteredTokenData = tokenData.filter(({ token }) => !(0, utils_1.isNativeTokenAddress)(token.address));
|
|
33
|
-
// group by chain
|
|
34
|
-
const tokenDataByChain = (0, utils_2.groupByChain)(filteredTokenData);
|
|
35
|
-
const approvalPromises = Object.keys(tokenDataByChain).map((chainId) => __awaiter(void 0, void 0, void 0, function* () {
|
|
36
|
-
const parsedChainId = Number.parseInt(chainId);
|
|
37
|
-
// get allowances for current chain and token list
|
|
38
|
-
return (0, utils_2.getAllowanceViaMulticall)(signer, parsedChainId, tokenDataByChain[parsedChainId]);
|
|
39
|
-
}));
|
|
40
|
-
const approvalsByChain = yield Promise.all(approvalPromises);
|
|
41
|
-
const approvals = approvalsByChain.flat();
|
|
42
|
-
return tokenData.map(({ token }) => {
|
|
43
|
-
// native token don't need approval
|
|
44
|
-
if ((0, utils_1.isNativeTokenAddress)(token.address)) {
|
|
45
|
-
return { token, approval: undefined };
|
|
46
|
-
}
|
|
47
|
-
const approved = approvals.find((approval) => (0, helpers_1.isSameToken)(approval.token, token));
|
|
48
|
-
return { token, approval: approved === null || approved === void 0 ? void 0 : approved.approvedAmount.toString() };
|
|
49
|
-
});
|
|
50
|
-
});
|
|
51
|
-
exports.bulkGetTokenApproval = bulkGetTokenApproval;
|
|
52
|
-
const approveToken = ({ signer, token, approvalAddress, amount, infiniteApproval = false, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
-
// native token don't need approval
|
|
54
|
-
if ((0, utils_1.isNativeTokenAddress)(token.address)) {
|
|
55
|
-
return;
|
|
56
|
-
}
|
|
57
|
-
const approvedAmount = yield (0, utils_2.getApproved)(signer, token.address, approvalAddress);
|
|
58
|
-
if (new bignumber_js_1.default(amount).gt(approvedAmount)) {
|
|
59
|
-
const approvalAmount = infiniteApproval
|
|
60
|
-
? ethers_1.constants.MaxUint256.toString()
|
|
61
|
-
: amount;
|
|
62
|
-
const approveTx = yield (0, utils_2.setApproval)(signer, token.address, approvalAddress, approvalAmount);
|
|
63
|
-
yield approveTx.wait();
|
|
64
|
-
}
|
|
65
|
-
});
|
|
66
|
-
exports.approveToken = approveToken;
|
|
67
|
-
const revokeTokenApproval = ({ signer, token, approvalAddress, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
68
|
-
// native token don't need approval
|
|
69
|
-
if ((0, utils_1.isNativeTokenAddress)(token.address)) {
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
const approvedAmount = yield (0, utils_2.getApproved)(signer, token.address, approvalAddress);
|
|
73
|
-
if (!approvedAmount.isZero()) {
|
|
74
|
-
const approveTx = yield (0, utils_2.setApproval)(signer, token.address, approvalAddress, '0');
|
|
75
|
-
yield approveTx.wait();
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
exports.revokeTokenApproval = revokeTokenApproval;
|
|
17
|
+
__exportStar(require("./checkAllowance"), exports);
|
|
18
|
+
__exportStar(require("./tokenApproval"), exports);
|
|
@@ -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>;
|