@lifi/sdk 1.7.2 → 2.0.0-beta.1
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/dist/Lifi.js +97 -85
- package/dist/allowance/checkAllowance.js +12 -21
- package/dist/allowance/tokenApproval.js +20 -29
- package/dist/allowance/utils.js +12 -24
- package/dist/balance/checkBalance.js +5 -14
- package/dist/balance/getTokenBalance.js +12 -21
- package/dist/balance/index.d.ts +11 -0
- package/dist/balance/index.js +8 -0
- package/dist/balance/utils.d.ts +1 -0
- package/dist/balance/utils.js +36 -38
- package/dist/cjs/Lifi.js +97 -85
- package/dist/cjs/allowance/checkAllowance.js +12 -21
- package/dist/cjs/allowance/tokenApproval.js +20 -29
- package/dist/cjs/allowance/utils.js +12 -24
- package/dist/cjs/balance/checkBalance.js +5 -14
- package/dist/cjs/balance/getTokenBalance.js +12 -21
- package/dist/cjs/balance/index.d.ts +11 -0
- package/dist/cjs/balance/index.js +8 -0
- package/dist/cjs/balance/utils.d.ts +1 -0
- package/dist/cjs/balance/utils.js +39 -39
- package/dist/cjs/connectors.js +17 -25
- package/dist/cjs/execution/ExecutionManager.js +27 -33
- package/dist/cjs/execution/StatusManager.js +13 -13
- package/dist/cjs/execution/StepExecutor.js +8 -14
- package/dist/cjs/execution/stepComparison.js +4 -14
- package/dist/cjs/execution/switchChain.js +5 -14
- package/dist/cjs/execution/utils.js +43 -50
- package/dist/cjs/helpers.d.ts +12 -1
- package/dist/cjs/helpers.js +66 -18
- package/dist/cjs/services/ApiService.d.ts +10 -9
- package/dist/cjs/services/ApiService.js +190 -152
- package/dist/cjs/services/ApiService.unit.handlers.d.ts +1 -0
- package/dist/cjs/services/ApiService.unit.handlers.js +50 -0
- package/dist/cjs/services/ChainsService.js +16 -31
- package/dist/cjs/services/ConfigService.js +6 -16
- package/dist/cjs/typeguards.js +1 -1
- package/dist/cjs/types/internal.types.d.ts +4 -1
- package/dist/cjs/utils/errors.d.ts +5 -0
- package/dist/cjs/utils/errors.js +14 -1
- package/dist/cjs/utils/multicall.js +6 -15
- package/dist/cjs/utils/parseError.d.ts +2 -2
- package/dist/cjs/utils/parseError.js +36 -38
- package/dist/cjs/utils/preRestart.js +5 -7
- package/dist/cjs/utils/utils.d.ts +0 -1
- package/dist/cjs/utils/utils.js +21 -28
- package/dist/cjs/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/connectors.js +15 -23
- package/dist/execution/ExecutionManager.js +27 -33
- package/dist/execution/StatusManager.js +11 -11
- package/dist/execution/StepExecutor.js +8 -14
- package/dist/execution/stepComparison.js +4 -14
- package/dist/execution/switchChain.js +5 -14
- package/dist/execution/utils.js +43 -50
- package/dist/helpers.d.ts +12 -1
- package/dist/helpers.js +63 -17
- package/dist/services/ApiService.d.ts +10 -9
- package/dist/services/ApiService.js +190 -152
- package/dist/services/ApiService.unit.handlers.d.ts +1 -0
- package/dist/services/ApiService.unit.handlers.js +44 -0
- package/dist/services/ChainsService.js +16 -31
- package/dist/services/ConfigService.js +6 -16
- package/dist/typeguards.js +1 -1
- package/dist/types/internal.types.d.ts +4 -1
- package/dist/utils/errors.d.ts +5 -0
- package/dist/utils/errors.js +12 -0
- package/dist/utils/multicall.js +6 -15
- package/dist/utils/parseError.d.ts +2 -2
- package/dist/utils/parseError.js +36 -38
- package/dist/utils/preRestart.js +5 -7
- package/dist/utils/utils.d.ts +0 -1
- package/dist/utils/utils.js +20 -26
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +22 -24
- package/CHANGELOG.md +0 -492
|
@@ -1,13 +1,4 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -19,30 +10,30 @@ const ChainsService_1 = __importDefault(require("../services/ChainsService"));
|
|
|
19
10
|
const types_1 = require("../types");
|
|
20
11
|
const errors_1 = require("../utils/errors");
|
|
21
12
|
const multicall_1 = require("../utils/multicall");
|
|
22
|
-
const getApproved = (signer, tokenAddress, contractAddress) =>
|
|
23
|
-
const signerAddress =
|
|
13
|
+
const getApproved = async (signer, tokenAddress, contractAddress) => {
|
|
14
|
+
const signerAddress = await signer.getAddress();
|
|
24
15
|
const erc20 = new ethers_1.Contract(tokenAddress, types_1.ERC20_ABI, signer);
|
|
25
16
|
try {
|
|
26
|
-
const approved =
|
|
17
|
+
const approved = await erc20.allowance(signerAddress, contractAddress);
|
|
27
18
|
return new bignumber_js_1.default(approved.toString());
|
|
28
19
|
}
|
|
29
20
|
catch (e) {
|
|
30
21
|
return new bignumber_js_1.default(0);
|
|
31
22
|
}
|
|
32
|
-
}
|
|
23
|
+
};
|
|
33
24
|
exports.getApproved = getApproved;
|
|
34
25
|
const setApproval = (signer, tokenAddress, contractAddress, amount) => {
|
|
35
26
|
const erc20 = new ethers_1.Contract(tokenAddress, types_1.ERC20_ABI, signer);
|
|
36
27
|
return erc20.approve(contractAddress, amount);
|
|
37
28
|
};
|
|
38
29
|
exports.setApproval = setApproval;
|
|
39
|
-
const getAllowanceViaMulticall = (signer, chainId, tokenData) =>
|
|
30
|
+
const getAllowanceViaMulticall = async (signer, chainId, tokenData) => {
|
|
40
31
|
const chainsService = ChainsService_1.default.getInstance();
|
|
41
|
-
const chain =
|
|
32
|
+
const chain = await chainsService.getChainById(chainId);
|
|
42
33
|
if (!chain.multicallAddress) {
|
|
43
34
|
throw new errors_1.ServerError(`No multicall address configured for chainId ${chainId}.`);
|
|
44
35
|
}
|
|
45
|
-
const ownerAddress =
|
|
36
|
+
const ownerAddress = await signer.getAddress();
|
|
46
37
|
const calls = [];
|
|
47
38
|
tokenData.map(({ token, approvalAddress }) => {
|
|
48
39
|
calls.push({
|
|
@@ -51,22 +42,19 @@ const getAllowanceViaMulticall = (signer, chainId, tokenData) => __awaiter(void
|
|
|
51
42
|
params: [ownerAddress, approvalAddress],
|
|
52
43
|
});
|
|
53
44
|
});
|
|
54
|
-
const result =
|
|
45
|
+
const result = await (0, multicall_1.fetchDataUsingMulticall)(calls, types_1.ERC20_ABI, chainId, chain.multicallAddress);
|
|
55
46
|
if (!result.length) {
|
|
56
47
|
throw new errors_1.ServerError(`Couldn't load allowance from chainId ${chainId} using multicall.`);
|
|
57
48
|
}
|
|
58
|
-
const parsedResult = result.map(({ data }) => {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
approvalAmount: (_a = data) !== null && _a !== void 0 ? _a : new bignumber_js_1.default(0),
|
|
62
|
-
});
|
|
63
|
-
});
|
|
49
|
+
const parsedResult = result.map(({ data }) => ({
|
|
50
|
+
approvalAmount: data ?? new bignumber_js_1.default(0),
|
|
51
|
+
}));
|
|
64
52
|
return tokenData.map(({ token, approvalAddress }, i) => ({
|
|
65
53
|
token,
|
|
66
54
|
approvalAddress,
|
|
67
55
|
approvedAmount: parsedResult[i].approvalAmount,
|
|
68
56
|
}));
|
|
69
|
-
}
|
|
57
|
+
};
|
|
70
58
|
exports.getAllowanceViaMulticall = getAllowanceViaMulticall;
|
|
71
59
|
const groupByChain = (tokenDataList) => {
|
|
72
60
|
// group by chain
|
|
@@ -1,13 +1,4 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -16,17 +7,17 @@ exports.checkBalance = void 0;
|
|
|
16
7
|
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
17
8
|
const errors_1 = require("../utils/errors");
|
|
18
9
|
const getTokenBalance_1 = require("./getTokenBalance");
|
|
19
|
-
const checkBalance = (signer, step, depth = 0) =>
|
|
20
|
-
const tokenAmount =
|
|
10
|
+
const checkBalance = async (signer, step, depth = 0) => {
|
|
11
|
+
const tokenAmount = await (0, getTokenBalance_1.getTokenBalance)(await signer.getAddress(), step.action.fromToken);
|
|
21
12
|
if (tokenAmount) {
|
|
22
13
|
const currentBalance = new bignumber_js_1.default(tokenAmount.amount).shiftedBy(tokenAmount.decimals);
|
|
23
14
|
const neededBalance = new bignumber_js_1.default(step.action.fromAmount);
|
|
24
15
|
if (currentBalance.lt(neededBalance)) {
|
|
25
16
|
if (depth <= 3) {
|
|
26
|
-
|
|
17
|
+
await new Promise((resolve) => {
|
|
27
18
|
setTimeout(resolve, 200);
|
|
28
19
|
});
|
|
29
|
-
|
|
20
|
+
await (0, exports.checkBalance)(signer, step, depth + 1);
|
|
30
21
|
}
|
|
31
22
|
else if (neededBalance.multipliedBy(1 - step.action.slippage).lte(currentBalance)) {
|
|
32
23
|
// adjust amount in slippage limits
|
|
@@ -50,5 +41,5 @@ const checkBalance = (signer, step, depth = 0) => __awaiter(void 0, void 0, void
|
|
|
50
41
|
}
|
|
51
42
|
}
|
|
52
43
|
}
|
|
53
|
-
}
|
|
44
|
+
};
|
|
54
45
|
exports.checkBalance = checkBalance;
|
|
@@ -1,25 +1,16 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
14
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
6
|
exports.getTokenBalancesForChains = exports.getTokenBalances = exports.getTokenBalance = void 0;
|
|
16
7
|
const utils_1 = __importDefault(require("./utils"));
|
|
17
|
-
const getTokenBalance = (walletAddress, token) =>
|
|
18
|
-
const tokenAmounts =
|
|
8
|
+
const getTokenBalance = async (walletAddress, token) => {
|
|
9
|
+
const tokenAmounts = await (0, exports.getTokenBalances)(walletAddress, [token]);
|
|
19
10
|
return tokenAmounts.length ? tokenAmounts[0] : null;
|
|
20
|
-
}
|
|
11
|
+
};
|
|
21
12
|
exports.getTokenBalance = getTokenBalance;
|
|
22
|
-
const getTokenBalances = (walletAddress, tokens) =>
|
|
13
|
+
const getTokenBalances = async (walletAddress, tokens) => {
|
|
23
14
|
// split by chain
|
|
24
15
|
const tokensByChain = {};
|
|
25
16
|
tokens.forEach((token) => {
|
|
@@ -28,18 +19,18 @@ const getTokenBalances = (walletAddress, tokens) => __awaiter(void 0, void 0, vo
|
|
|
28
19
|
}
|
|
29
20
|
tokensByChain[token.chainId].push(token);
|
|
30
21
|
});
|
|
31
|
-
const tokenAmountsByChain =
|
|
22
|
+
const tokenAmountsByChain = await (0, exports.getTokenBalancesForChains)(walletAddress, tokensByChain);
|
|
32
23
|
return Object.values(tokenAmountsByChain).flat();
|
|
33
|
-
}
|
|
24
|
+
};
|
|
34
25
|
exports.getTokenBalances = getTokenBalances;
|
|
35
|
-
const getTokenBalancesForChains = (walletAddress, tokensByChain) =>
|
|
26
|
+
const getTokenBalancesForChains = async (walletAddress, tokensByChain) => {
|
|
36
27
|
const tokenAmountsByChain = {};
|
|
37
|
-
const promises = Object.keys(tokensByChain).map((chainIdStr) =>
|
|
28
|
+
const promises = Object.keys(tokensByChain).map(async (chainIdStr) => {
|
|
38
29
|
const chainId = parseInt(chainIdStr);
|
|
39
|
-
const tokenAmounts =
|
|
30
|
+
const tokenAmounts = await utils_1.default.getBalances(walletAddress, tokensByChain[chainId]);
|
|
40
31
|
tokenAmountsByChain[chainId] = tokenAmounts;
|
|
41
|
-
})
|
|
42
|
-
|
|
32
|
+
});
|
|
33
|
+
await Promise.allSettled(promises);
|
|
43
34
|
return tokenAmountsByChain;
|
|
44
|
-
}
|
|
35
|
+
};
|
|
45
36
|
exports.getTokenBalancesForChains = getTokenBalancesForChains;
|
|
@@ -1,2 +1,13 @@
|
|
|
1
1
|
export * from './checkBalance';
|
|
2
2
|
export * from './getTokenBalance';
|
|
3
|
+
declare const _default: {
|
|
4
|
+
checkBalance: (signer: import("ethers").Signer, step: import("@lifi/types").Step, depth?: number) => Promise<void>;
|
|
5
|
+
getTokenBalance: (walletAddress: string, token: import("@lifi/types").Token) => Promise<import("@lifi/types").TokenAmount | null>;
|
|
6
|
+
getTokenBalances: (walletAddress: string, tokens: import("@lifi/types").Token[]) => Promise<import("@lifi/types").TokenAmount[]>;
|
|
7
|
+
getTokenBalancesForChains: (walletAddress: string, tokensByChain: {
|
|
8
|
+
[chainId: number]: import("@lifi/types").Token[];
|
|
9
|
+
}) => Promise<{
|
|
10
|
+
[chainId: number]: import("@lifi/types").TokenAmount[];
|
|
11
|
+
}>;
|
|
12
|
+
};
|
|
13
|
+
export default _default;
|
|
@@ -16,3 +16,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./checkBalance"), exports);
|
|
18
18
|
__exportStar(require("./getTokenBalance"), exports);
|
|
19
|
+
const checkBalance_1 = require("./checkBalance");
|
|
20
|
+
const getTokenBalance_1 = require("./getTokenBalance");
|
|
21
|
+
exports.default = {
|
|
22
|
+
checkBalance: checkBalance_1.checkBalance,
|
|
23
|
+
getTokenBalance: getTokenBalance_1.getTokenBalance,
|
|
24
|
+
getTokenBalances: getTokenBalance_1.getTokenBalances,
|
|
25
|
+
getTokenBalancesForChains: getTokenBalance_1.getTokenBalancesForChains,
|
|
26
|
+
};
|
|
@@ -1,17 +1,9 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
14
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getBalances = void 0;
|
|
15
7
|
const bignumber_js_1 = __importDefault(require("bignumber.js"));
|
|
16
8
|
const ethers_1 = require("ethers");
|
|
17
9
|
const connectors_1 = require("../connectors");
|
|
@@ -37,7 +29,7 @@ const balanceAbi = [
|
|
|
37
29
|
type: 'function',
|
|
38
30
|
},
|
|
39
31
|
];
|
|
40
|
-
const getBalances = (walletAddress, tokens) =>
|
|
32
|
+
const getBalances = async (walletAddress, tokens) => {
|
|
41
33
|
if (tokens.length === 0) {
|
|
42
34
|
return [];
|
|
43
35
|
}
|
|
@@ -49,23 +41,24 @@ const getBalances = (walletAddress, tokens) => __awaiter(void 0, void 0, void 0,
|
|
|
49
41
|
return [];
|
|
50
42
|
}
|
|
51
43
|
});
|
|
52
|
-
if ((
|
|
44
|
+
if ((await (0, connectors_1.getMulticallAddress)(chainId)) && tokens.length > 1) {
|
|
53
45
|
return getBalancesFromProviderUsingMulticall(walletAddress, tokens);
|
|
54
46
|
}
|
|
55
47
|
else {
|
|
56
48
|
return getBalancesFromProvider(walletAddress, tokens);
|
|
57
49
|
}
|
|
58
|
-
}
|
|
59
|
-
|
|
50
|
+
};
|
|
51
|
+
exports.getBalances = getBalances;
|
|
52
|
+
const getBalancesFromProviderUsingMulticall = async (walletAddress, tokens) => {
|
|
60
53
|
// Configuration
|
|
61
54
|
const { chainId } = tokens[0];
|
|
62
|
-
const multicallAddress =
|
|
55
|
+
const multicallAddress = await (0, connectors_1.getMulticallAddress)(chainId);
|
|
63
56
|
if (!multicallAddress) {
|
|
64
57
|
throw new Error('No multicallAddress found for the given chain.');
|
|
65
58
|
}
|
|
66
59
|
return executeMulticall(walletAddress, tokens, multicallAddress, chainId);
|
|
67
|
-
}
|
|
68
|
-
const executeMulticall = (walletAddress, tokens, multicallAddress, chainId) =>
|
|
60
|
+
};
|
|
61
|
+
const executeMulticall = async (walletAddress, tokens, multicallAddress, chainId) => {
|
|
69
62
|
// Collect calls we want to make
|
|
70
63
|
const calls = [];
|
|
71
64
|
tokens.map((token) => {
|
|
@@ -84,7 +77,7 @@ const executeMulticall = (walletAddress, tokens, multicallAddress, chainId) => _
|
|
|
84
77
|
});
|
|
85
78
|
}
|
|
86
79
|
});
|
|
87
|
-
const res =
|
|
80
|
+
const res = await fetchViaMulticall(calls, balanceAbi, chainId, multicallAddress);
|
|
88
81
|
if (!res.length) {
|
|
89
82
|
return [];
|
|
90
83
|
}
|
|
@@ -92,24 +85,28 @@ const executeMulticall = (walletAddress, tokens, multicallAddress, chainId) => _
|
|
|
92
85
|
const amount = new bignumber_js_1.default(res[i].amount.toString() || '0')
|
|
93
86
|
.shiftedBy(-token.decimals)
|
|
94
87
|
.toFixed();
|
|
95
|
-
return
|
|
88
|
+
return {
|
|
89
|
+
...token,
|
|
90
|
+
amount: amount || '0',
|
|
91
|
+
blockNumber: res[i].blockNumber,
|
|
92
|
+
};
|
|
96
93
|
});
|
|
97
|
-
}
|
|
98
|
-
const fetchViaMulticall = (calls, abi, chainId, multicallAddress) =>
|
|
99
|
-
const result =
|
|
94
|
+
};
|
|
95
|
+
const fetchViaMulticall = async (calls, abi, chainId, multicallAddress) => {
|
|
96
|
+
const result = await (0, multicall_1.fetchDataUsingMulticall)(calls, abi, chainId, multicallAddress);
|
|
100
97
|
return result.map(({ data, blockNumber }) => ({
|
|
101
98
|
amount: data ? data : new bignumber_js_1.default(0),
|
|
102
99
|
blockNumber,
|
|
103
100
|
}));
|
|
104
|
-
}
|
|
105
|
-
const getBalancesFromProvider = (walletAddress, tokens) =>
|
|
101
|
+
};
|
|
102
|
+
const getBalancesFromProvider = async (walletAddress, tokens) => {
|
|
106
103
|
const chainId = tokens[0].chainId;
|
|
107
|
-
const rpc =
|
|
108
|
-
const tokenAmountPromises = tokens.map((token) =>
|
|
104
|
+
const rpc = await (0, connectors_1.getRpcProvider)(chainId);
|
|
105
|
+
const tokenAmountPromises = tokens.map(async (token) => {
|
|
109
106
|
let amount = '0';
|
|
110
107
|
let blockNumber;
|
|
111
108
|
try {
|
|
112
|
-
const balance =
|
|
109
|
+
const balance = await getBalanceFromProvider(walletAddress, token.address, chainId, rpc);
|
|
113
110
|
amount = new bignumber_js_1.default(balance.amount.toString())
|
|
114
111
|
.shiftedBy(-token.decimals)
|
|
115
112
|
.toString();
|
|
@@ -119,20 +116,23 @@ const getBalancesFromProvider = (walletAddress, tokens) => __awaiter(void 0, voi
|
|
|
119
116
|
// eslint-disable-next-line no-console
|
|
120
117
|
console.warn(e);
|
|
121
118
|
}
|
|
122
|
-
return
|
|
123
|
-
|
|
124
|
-
|
|
119
|
+
return {
|
|
120
|
+
...token,
|
|
121
|
+
amount,
|
|
122
|
+
blockNumber,
|
|
123
|
+
};
|
|
124
|
+
});
|
|
125
125
|
return Promise.all(tokenAmountPromises);
|
|
126
|
-
}
|
|
127
|
-
const getBalanceFromProvider = (walletAddress, assetId, chainId, provider) =>
|
|
128
|
-
const blockNumber =
|
|
126
|
+
};
|
|
127
|
+
const getBalanceFromProvider = async (walletAddress, assetId, chainId, provider) => {
|
|
128
|
+
const blockNumber = await getCurrentBlockNumber(chainId);
|
|
129
129
|
let balance;
|
|
130
130
|
if ((0, utils_1.isZeroAddress)(assetId)) {
|
|
131
|
-
balance =
|
|
131
|
+
balance = await provider.getBalance(walletAddress, blockNumber);
|
|
132
132
|
}
|
|
133
133
|
else {
|
|
134
134
|
const contract = new ethers_1.ethers.Contract(assetId, ['function balanceOf(address owner) view returns (uint256)'], provider);
|
|
135
|
-
balance =
|
|
135
|
+
balance = await contract.balanceOf(walletAddress, {
|
|
136
136
|
blockTag: blockNumber,
|
|
137
137
|
});
|
|
138
138
|
}
|
|
@@ -140,11 +140,11 @@ const getBalanceFromProvider = (walletAddress, assetId, chainId, provider) => __
|
|
|
140
140
|
amount: balance,
|
|
141
141
|
blockNumber,
|
|
142
142
|
};
|
|
143
|
-
}
|
|
144
|
-
const getCurrentBlockNumber = (chainId) =>
|
|
145
|
-
const rpc =
|
|
143
|
+
};
|
|
144
|
+
const getCurrentBlockNumber = async (chainId) => {
|
|
145
|
+
const rpc = await (0, connectors_1.getRpcProvider)(chainId);
|
|
146
146
|
return rpc.getBlockNumber();
|
|
147
|
-
}
|
|
147
|
+
};
|
|
148
148
|
exports.default = {
|
|
149
|
-
getBalances,
|
|
149
|
+
getBalances: exports.getBalances,
|
|
150
150
|
};
|
package/dist/cjs/connectors.js
CHANGED
|
@@ -1,22 +1,14 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
14
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
6
|
exports.getMulticallAddress = exports.getRpcProvider = exports.getRpcUrls = exports.getRpcUrl = void 0;
|
|
16
7
|
const ethers_1 = require("ethers");
|
|
17
|
-
const _1 = require(".");
|
|
18
8
|
const types_1 = require("./types");
|
|
19
9
|
const ConfigService_1 = __importDefault(require("./services/ConfigService"));
|
|
10
|
+
const helpers_1 = require("./helpers");
|
|
11
|
+
const errors_1 = require("./utils/errors");
|
|
20
12
|
// cached providers
|
|
21
13
|
const chainProviders = {};
|
|
22
14
|
// Archive RPC Provider
|
|
@@ -27,35 +19,35 @@ const archiveRpcs = {
|
|
|
27
19
|
[types_1.ChainId.FTM]: 'https://speedy-nodes-nyc.moralis.io/5ed6053dc39eba789ff466c9/fantom/mainnet',
|
|
28
20
|
};
|
|
29
21
|
// RPC Urls
|
|
30
|
-
const getRpcUrl = (chainId, archive = false) =>
|
|
31
|
-
const rpcUrls =
|
|
22
|
+
const getRpcUrl = async (chainId, archive = false) => {
|
|
23
|
+
const rpcUrls = await (0, exports.getRpcUrls)(chainId, archive);
|
|
32
24
|
return rpcUrls[0];
|
|
33
|
-
}
|
|
25
|
+
};
|
|
34
26
|
exports.getRpcUrl = getRpcUrl;
|
|
35
|
-
const getRpcUrls = (chainId, archive = false) =>
|
|
27
|
+
const getRpcUrls = async (chainId, archive = false) => {
|
|
36
28
|
if (archive && archiveRpcs[chainId]) {
|
|
37
29
|
return [archiveRpcs[chainId]];
|
|
38
30
|
}
|
|
39
31
|
const configService = ConfigService_1.default.getInstance();
|
|
40
|
-
const config =
|
|
32
|
+
const config = await configService.getConfigAsync();
|
|
41
33
|
return config.rpcs[chainId];
|
|
42
|
-
}
|
|
34
|
+
};
|
|
43
35
|
exports.getRpcUrls = getRpcUrls;
|
|
44
36
|
const getRandomProvider = (providerList) => {
|
|
45
|
-
const index = (0,
|
|
37
|
+
const index = (0, helpers_1.getRandomNumber)(0, providerList.length - 1);
|
|
46
38
|
return providerList[index];
|
|
47
39
|
};
|
|
48
40
|
// Provider
|
|
49
|
-
const getRpcProvider = (chainId, archive = false) =>
|
|
41
|
+
const getRpcProvider = async (chainId, archive = false) => {
|
|
50
42
|
if (archive && archiveRpcs[chainId]) {
|
|
51
43
|
// return archive PRC, but don't cache it
|
|
52
44
|
return new ethers_1.providers.FallbackProvider([
|
|
53
|
-
new ethers_1.providers.StaticJsonRpcProvider(
|
|
45
|
+
new ethers_1.providers.StaticJsonRpcProvider(await (0, exports.getRpcUrl)(chainId, archive), chainId),
|
|
54
46
|
]);
|
|
55
47
|
}
|
|
56
48
|
if (!chainProviders[chainId]) {
|
|
57
49
|
chainProviders[chainId] = [];
|
|
58
|
-
const urls =
|
|
50
|
+
const urls = await (0, exports.getRpcUrls)(chainId, archive);
|
|
59
51
|
urls.forEach((url) => {
|
|
60
52
|
chainProviders[chainId].push(new ethers_1.providers.FallbackProvider([
|
|
61
53
|
new ethers_1.providers.StaticJsonRpcProvider(url, chainId),
|
|
@@ -63,15 +55,15 @@ const getRpcProvider = (chainId, archive = false) => __awaiter(void 0, void 0, v
|
|
|
63
55
|
});
|
|
64
56
|
}
|
|
65
57
|
if (!chainProviders[chainId].length) {
|
|
66
|
-
throw new
|
|
58
|
+
throw new errors_1.ServerError(`Unable to configure provider for chain ${chainId}`);
|
|
67
59
|
}
|
|
68
60
|
return getRandomProvider(chainProviders[chainId]);
|
|
69
|
-
}
|
|
61
|
+
};
|
|
70
62
|
exports.getRpcProvider = getRpcProvider;
|
|
71
63
|
// Multicall
|
|
72
|
-
const getMulticallAddress = (chainId) =>
|
|
64
|
+
const getMulticallAddress = async (chainId) => {
|
|
73
65
|
const configService = ConfigService_1.default.getInstance();
|
|
74
|
-
const config =
|
|
66
|
+
const config = await configService.getConfigAsync();
|
|
75
67
|
return config.multicallAddresses[chainId];
|
|
76
|
-
}
|
|
68
|
+
};
|
|
77
69
|
exports.getMulticallAddress = getMulticallAddress;
|
|
@@ -1,13 +1,4 @@
|
|
|
1
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
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -30,20 +21,19 @@ class ExecutionManager {
|
|
|
30
21
|
this.allowInteraction = (value) => {
|
|
31
22
|
this.allowUserInteraction = value;
|
|
32
23
|
};
|
|
33
|
-
this.execute = ({ signer, step, statusManager, settings, }) =>
|
|
34
|
-
var _a, _b, _c, _d;
|
|
24
|
+
this.execute = async ({ signer, step, statusManager, settings, }) => {
|
|
35
25
|
step.execution = statusManager.initExecutionObject(step);
|
|
36
26
|
const chainsService = ChainsService_1.default.getInstance();
|
|
37
|
-
const fromChain =
|
|
38
|
-
const toChain =
|
|
27
|
+
const fromChain = await chainsService.getChainById(step.action.fromChainId);
|
|
28
|
+
const toChain = await chainsService.getChainById(step.action.toChainId);
|
|
39
29
|
const isBridgeExecution = fromChain.id !== toChain.id;
|
|
40
30
|
const currentProcessType = isBridgeExecution ? 'CROSS_CHAIN' : 'SWAP';
|
|
41
31
|
// STEP 1: Check allowance
|
|
42
32
|
const existingProcess = step.execution.process.find((p) => p.type === currentProcessType);
|
|
43
33
|
// Check token approval only if fromToken is not the native token => no approval needed in that case
|
|
44
|
-
if (!
|
|
34
|
+
if (!existingProcess?.txHash &&
|
|
45
35
|
!(0, utils_1.isZeroAddress)(step.action.fromToken.address)) {
|
|
46
|
-
|
|
36
|
+
await (0, allowance_1.checkAllowance)(signer, step, statusManager, settings, fromChain, this.allowUserInteraction);
|
|
47
37
|
}
|
|
48
38
|
// STEP 2: Get transaction
|
|
49
39
|
let process = statusManager.findOrCreateProcess(step, currentProcessType);
|
|
@@ -52,25 +42,28 @@ class ExecutionManager {
|
|
|
52
42
|
let transaction;
|
|
53
43
|
if (process.txHash) {
|
|
54
44
|
// Make sure that the chain is still correct
|
|
55
|
-
const updatedSigner =
|
|
45
|
+
const updatedSigner = await (0, switchChain_1.switchChain)(signer, statusManager, step, settings.switchChainHook, this.allowUserInteraction);
|
|
56
46
|
if (!updatedSigner) {
|
|
57
47
|
// Chain switch was not successful, stop execution here
|
|
58
48
|
return step.execution;
|
|
59
49
|
}
|
|
60
50
|
signer = updatedSigner;
|
|
61
51
|
// Load exiting transaction
|
|
62
|
-
transaction =
|
|
52
|
+
transaction = await (0, getProvider_1.getProvider)(signer).getTransaction(process.txHash);
|
|
63
53
|
}
|
|
64
54
|
else {
|
|
65
55
|
process = statusManager.updateProcess(step, process.type, 'STARTED');
|
|
66
56
|
// Check balance
|
|
67
|
-
|
|
57
|
+
await (0, balance_1.checkBalance)(signer, step);
|
|
68
58
|
// Create new transaction
|
|
69
59
|
if (!step.transactionRequest) {
|
|
70
|
-
const personalizedStep =
|
|
71
|
-
const updatedStep =
|
|
72
|
-
const comparedStep =
|
|
73
|
-
step =
|
|
60
|
+
const personalizedStep = await (0, utils_1.personalizeStep)(signer, step);
|
|
61
|
+
const updatedStep = await ApiService_1.default.getStepTransaction(personalizedStep);
|
|
62
|
+
const comparedStep = await (0, stepComparison_1.stepComparison)(statusManager, personalizedStep, updatedStep, settings, this.allowUserInteraction);
|
|
63
|
+
step = {
|
|
64
|
+
...comparedStep,
|
|
65
|
+
execution: step.execution,
|
|
66
|
+
};
|
|
74
67
|
}
|
|
75
68
|
const { transactionRequest } = step;
|
|
76
69
|
if (!transactionRequest) {
|
|
@@ -78,7 +71,7 @@ class ExecutionManager {
|
|
|
78
71
|
}
|
|
79
72
|
// STEP 3: Send the transaction
|
|
80
73
|
// Make sure that the chain is still correct
|
|
81
|
-
const updatedSigner =
|
|
74
|
+
const updatedSigner = await (0, switchChain_1.switchChain)(signer, statusManager, step, settings.switchChainHook, this.allowUserInteraction);
|
|
82
75
|
if (!updatedSigner) {
|
|
83
76
|
// Chain switch was not successful, stop execution here
|
|
84
77
|
return step.execution;
|
|
@@ -89,7 +82,7 @@ class ExecutionManager {
|
|
|
89
82
|
return step.execution;
|
|
90
83
|
}
|
|
91
84
|
// Submit the transaction
|
|
92
|
-
transaction =
|
|
85
|
+
transaction = await signer.sendTransaction(transactionRequest);
|
|
93
86
|
// STEP 4: Wait for the transaction
|
|
94
87
|
process = statusManager.updateProcess(step, process.type, 'PENDING', {
|
|
95
88
|
txHash: transaction.hash,
|
|
@@ -98,7 +91,7 @@ class ExecutionManager {
|
|
|
98
91
|
transaction.hash,
|
|
99
92
|
});
|
|
100
93
|
}
|
|
101
|
-
|
|
94
|
+
await transaction.wait();
|
|
102
95
|
process = statusManager.updateProcess(step, process.type, 'PENDING', {
|
|
103
96
|
txHash: transaction.hash,
|
|
104
97
|
txLink: fromChain.metamask.blockExplorerUrls[0] + 'tx/' + transaction.hash,
|
|
@@ -117,7 +110,7 @@ class ExecutionManager {
|
|
|
117
110
|
});
|
|
118
111
|
}
|
|
119
112
|
else {
|
|
120
|
-
const error =
|
|
113
|
+
const error = await (0, parseError_1.parseError)(e, step, process);
|
|
121
114
|
process = statusManager.updateProcess(step, process.type, 'FAILED', {
|
|
122
115
|
error: {
|
|
123
116
|
message: error.message,
|
|
@@ -140,20 +133,20 @@ class ExecutionManager {
|
|
|
140
133
|
if (!processTxHash) {
|
|
141
134
|
throw new Error('Transaction hash is undefined.');
|
|
142
135
|
}
|
|
143
|
-
statusResponse =
|
|
136
|
+
statusResponse = await (0, utils_2.waitForReceivingTransaction)(processTxHash, statusManager, process.type, step);
|
|
144
137
|
process = statusManager.updateProcess(step, process.type, 'DONE', {
|
|
145
138
|
substatus: statusResponse.substatus,
|
|
146
139
|
substatusMessage: statusResponse.substatusMessage ||
|
|
147
140
|
(0, utils_2.getSubstatusMessage)(statusResponse.status, statusResponse.substatus),
|
|
148
|
-
txHash:
|
|
141
|
+
txHash: statusResponse.receiving?.txHash,
|
|
149
142
|
txLink: toChain.metamask.blockExplorerUrls[0] +
|
|
150
143
|
'tx/' +
|
|
151
|
-
|
|
144
|
+
statusResponse.receiving?.txHash,
|
|
152
145
|
});
|
|
153
146
|
statusManager.updateExecution(step, 'DONE', {
|
|
154
147
|
fromAmount: statusResponse.sending.amount,
|
|
155
|
-
toAmount:
|
|
156
|
-
toToken:
|
|
148
|
+
toAmount: statusResponse.receiving?.amount,
|
|
149
|
+
toToken: statusResponse.receiving?.token,
|
|
157
150
|
gasAmount: statusResponse.sending.gasAmount,
|
|
158
151
|
gasAmountUSD: statusResponse.sending.gasAmountUSD,
|
|
159
152
|
gasPrice: statusResponse.sending.gasPrice,
|
|
@@ -162,11 +155,12 @@ class ExecutionManager {
|
|
|
162
155
|
});
|
|
163
156
|
}
|
|
164
157
|
catch (e) {
|
|
158
|
+
const htmlMessage = await (0, parseError_1.getTransactionFailedMessage)(step, process.txLink);
|
|
165
159
|
process = statusManager.updateProcess(step, process.type, 'FAILED', {
|
|
166
160
|
error: {
|
|
167
161
|
code: errors_1.LifiErrorCode.TransactionFailed,
|
|
168
162
|
message: 'Failed while waiting for receiving chain.',
|
|
169
|
-
htmlMessage
|
|
163
|
+
htmlMessage,
|
|
170
164
|
},
|
|
171
165
|
});
|
|
172
166
|
statusManager.updateExecution(step, 'FAILED');
|
|
@@ -175,7 +169,7 @@ class ExecutionManager {
|
|
|
175
169
|
}
|
|
176
170
|
// DONE
|
|
177
171
|
return step.execution;
|
|
178
|
-
}
|
|
172
|
+
};
|
|
179
173
|
}
|
|
180
174
|
}
|
|
181
175
|
exports.ExecutionManager = ExecutionManager;
|