@instadapp/interop-x 0.0.0-dev.ef7acff → 0.0.0-dev.f45bd03

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 (88) hide show
  1. package/dist/package.json +8 -2
  2. package/dist/src/abi/aaveV2Resolver.json +832 -0
  3. package/dist/src/abi/aaveV3Resolver.json +628 -0
  4. package/dist/src/abi/balanceResolver.json +211 -0
  5. package/dist/src/abi/index.js +6 -0
  6. package/dist/src/constants/addresses.js +6 -0
  7. package/dist/src/constants/capPerChain.js +8 -0
  8. package/dist/src/constants/index.js +2 -0
  9. package/dist/src/constants/tokens.js +44 -44
  10. package/dist/src/constants/wrappedNativeToken.js +8 -0
  11. package/dist/src/crons/index.js +3 -0
  12. package/dist/src/crons/prices.js +16 -0
  13. package/dist/src/db/models/transaction.js +1 -1
  14. package/dist/src/errors/index.js +14 -1
  15. package/dist/src/gnosis/actions/aaveV2/source.js +26 -1
  16. package/dist/src/gnosis/actions/aaveV2/target.js +16 -1
  17. package/dist/src/gnosis/actions/aaveV3/index.js +11 -0
  18. package/dist/src/gnosis/actions/aaveV3/source.js +74 -0
  19. package/dist/src/gnosis/actions/aaveV3/target.js +87 -0
  20. package/dist/src/gnosis/actions/index.js +2 -0
  21. package/dist/src/index.js +2 -1
  22. package/dist/src/providers/index.js +17 -0
  23. package/dist/src/providers/retry-provider.js +45 -0
  24. package/dist/src/services/Prices.js +74 -0
  25. package/dist/src/services/index.js +8 -0
  26. package/dist/src/tasks/InteropX/ProcessSubmitEvents.js +92 -12
  27. package/dist/src/tasks/InteropX/ProcessValidateEvents.js +28 -9
  28. package/dist/src/tasks/InteropX/SyncLogExecuteEvents.js +2 -1
  29. package/dist/src/tasks/InteropX/SyncLogSubmitEvents.js +2 -1
  30. package/dist/src/tasks/InteropX/SyncLogValidateEvents.js +2 -1
  31. package/dist/src/typechain/AaveV2Resolver.js +2 -0
  32. package/dist/src/typechain/AaveV3Resolver.js +2 -0
  33. package/dist/src/typechain/BalanceResolver.js +2 -0
  34. package/dist/src/typechain/factories/AaveV2Resolver__factory.js +1191 -0
  35. package/dist/src/typechain/factories/AaveV3Resolver__factory.js +887 -0
  36. package/dist/src/typechain/factories/BalanceResolver__factory.js +228 -0
  37. package/dist/src/typechain/factories/index.js +7 -1
  38. package/dist/src/typechain/index.js +7 -1
  39. package/dist/src/utils/formatting.js +51 -1
  40. package/dist/src/utils/gnosis.js +42 -17
  41. package/dist/src/utils/index.js +1 -0
  42. package/dist/src/utils/tokens.js +22 -0
  43. package/dist/src/utils/validate.js +85 -1
  44. package/dist/src/utils/web3.js +2 -1
  45. package/package.json +8 -2
  46. package/src/abi/aaveV2Resolver.json +832 -0
  47. package/src/abi/aaveV3Resolver.json +628 -0
  48. package/src/abi/balanceResolver.json +211 -0
  49. package/src/abi/index.ts +6 -0
  50. package/src/constants/addresses.ts +18 -1
  51. package/src/constants/capPerChain.ts +5 -0
  52. package/src/constants/index.ts +2 -0
  53. package/src/constants/tokens.ts +44 -44
  54. package/src/constants/wrappedNativeToken.ts +5 -0
  55. package/src/crons/index.ts +1 -0
  56. package/src/crons/prices.ts +12 -0
  57. package/src/db/models/transaction.ts +1 -1
  58. package/src/errors/index.ts +13 -0
  59. package/src/gnosis/actions/aaveV2/source.ts +58 -2
  60. package/src/gnosis/actions/aaveV2/target.ts +29 -2
  61. package/src/gnosis/actions/aaveV3/index.ts +9 -0
  62. package/src/gnosis/actions/aaveV3/source.ts +119 -0
  63. package/src/gnosis/actions/aaveV3/target.ts +142 -0
  64. package/src/gnosis/actions/index.ts +2 -0
  65. package/src/index.ts +1 -0
  66. package/src/providers/index.ts +1 -0
  67. package/src/providers/retry-provider.ts +51 -0
  68. package/src/services/Prices.ts +89 -0
  69. package/src/services/index.ts +1 -0
  70. package/src/tasks/InteropX/ProcessSubmitEvents.ts +120 -17
  71. package/src/tasks/InteropX/ProcessValidateEvents.ts +39 -14
  72. package/src/tasks/InteropX/SyncLogExecuteEvents.ts +3 -2
  73. package/src/tasks/InteropX/SyncLogSubmitEvents.ts +3 -2
  74. package/src/tasks/InteropX/SyncLogValidateEvents.ts +3 -2
  75. package/src/typechain/AaveV2Resolver.ts +1017 -0
  76. package/src/typechain/AaveV3Resolver.ts +935 -0
  77. package/src/typechain/BalanceResolver.ts +266 -0
  78. package/src/typechain/factories/AaveV2Resolver__factory.ts +1198 -0
  79. package/src/typechain/factories/AaveV3Resolver__factory.ts +894 -0
  80. package/src/typechain/factories/BalanceResolver__factory.ts +235 -0
  81. package/src/typechain/factories/index.ts +3 -0
  82. package/src/typechain/index.ts +6 -0
  83. package/src/utils/formatting.ts +53 -0
  84. package/src/utils/gnosis.ts +75 -32
  85. package/src/utils/index.ts +1 -0
  86. package/src/utils/tokens.ts +21 -0
  87. package/src/utils/validate.ts +149 -2
  88. package/src/utils/web3.ts +3 -2
@@ -0,0 +1,235 @@
1
+ /* Autogenerated file. Do not edit manually. */
2
+ /* tslint:disable */
3
+ /* eslint-disable */
4
+
5
+ import { Contract, Signer, utils } from "ethers";
6
+ import type { Provider } from "@ethersproject/providers";
7
+ import type {
8
+ BalanceResolver,
9
+ BalanceResolverInterface,
10
+ } from "../BalanceResolver";
11
+
12
+ const _abi = [
13
+ {
14
+ inputs: [
15
+ {
16
+ internalType: "address",
17
+ name: "_wnativeToken",
18
+ type: "address",
19
+ },
20
+ ],
21
+ stateMutability: "nonpayable",
22
+ type: "constructor",
23
+ },
24
+ {
25
+ inputs: [
26
+ {
27
+ components: [
28
+ {
29
+ components: [
30
+ {
31
+ internalType: "address",
32
+ name: "sourceToken",
33
+ type: "address",
34
+ },
35
+ {
36
+ internalType: "address",
37
+ name: "targetToken",
38
+ type: "address",
39
+ },
40
+ {
41
+ internalType: "uint256",
42
+ name: "amount",
43
+ type: "uint256",
44
+ },
45
+ ],
46
+ internalType: "struct Variables.TokenInfo[]",
47
+ name: "supply",
48
+ type: "tuple[]",
49
+ },
50
+ {
51
+ components: [
52
+ {
53
+ internalType: "address",
54
+ name: "sourceToken",
55
+ type: "address",
56
+ },
57
+ {
58
+ internalType: "address",
59
+ name: "targetToken",
60
+ type: "address",
61
+ },
62
+ {
63
+ internalType: "uint256",
64
+ name: "amount",
65
+ type: "uint256",
66
+ },
67
+ ],
68
+ internalType: "struct Variables.TokenInfo[]",
69
+ name: "withdraw",
70
+ type: "tuple[]",
71
+ },
72
+ ],
73
+ internalType: "struct Variables.Position",
74
+ name: "position",
75
+ type: "tuple",
76
+ },
77
+ {
78
+ internalType: "address",
79
+ name: "liquidityContract",
80
+ type: "address",
81
+ },
82
+ {
83
+ internalType: "bool",
84
+ name: "isSupply",
85
+ type: "bool",
86
+ },
87
+ {
88
+ internalType: "bool",
89
+ name: "isTargetToken",
90
+ type: "bool",
91
+ },
92
+ ],
93
+ name: "checkLiquidity",
94
+ outputs: [
95
+ {
96
+ components: [
97
+ {
98
+ internalType: "bool",
99
+ name: "isOk",
100
+ type: "bool",
101
+ },
102
+ {
103
+ components: [
104
+ {
105
+ internalType: "address",
106
+ name: "token",
107
+ type: "address",
108
+ },
109
+ {
110
+ internalType: "uint256",
111
+ name: "liquidityAvailable",
112
+ type: "uint256",
113
+ },
114
+ {
115
+ internalType: "uint256",
116
+ name: "liquidityShort",
117
+ type: "uint256",
118
+ },
119
+ ],
120
+ internalType: "struct Helpers.LiquidityData[]",
121
+ name: "supply",
122
+ type: "tuple[]",
123
+ },
124
+ {
125
+ components: [
126
+ {
127
+ internalType: "address",
128
+ name: "token",
129
+ type: "address",
130
+ },
131
+ {
132
+ internalType: "uint256",
133
+ name: "liquidityAvailable",
134
+ type: "uint256",
135
+ },
136
+ {
137
+ internalType: "uint256",
138
+ name: "liquidityShort",
139
+ type: "uint256",
140
+ },
141
+ ],
142
+ internalType: "struct Helpers.LiquidityData[]",
143
+ name: "withdraw",
144
+ type: "tuple[]",
145
+ },
146
+ ],
147
+ internalType: "struct Helpers.PositionData",
148
+ name: "p",
149
+ type: "tuple",
150
+ },
151
+ ],
152
+ stateMutability: "view",
153
+ type: "function",
154
+ },
155
+ {
156
+ inputs: [
157
+ {
158
+ internalType: "address[]",
159
+ name: "tokens",
160
+ type: "address[]",
161
+ },
162
+ {
163
+ internalType: "address",
164
+ name: "liquidityContract",
165
+ type: "address",
166
+ },
167
+ ],
168
+ name: "getLiquidity",
169
+ outputs: [
170
+ {
171
+ components: [
172
+ {
173
+ internalType: "address",
174
+ name: "token",
175
+ type: "address",
176
+ },
177
+ {
178
+ internalType: "uint256",
179
+ name: "liquidityAvailable",
180
+ type: "uint256",
181
+ },
182
+ {
183
+ internalType: "uint256",
184
+ name: "liquidityShort",
185
+ type: "uint256",
186
+ },
187
+ ],
188
+ internalType: "struct Helpers.LiquidityData[]",
189
+ name: "l",
190
+ type: "tuple[]",
191
+ },
192
+ ],
193
+ stateMutability: "view",
194
+ type: "function",
195
+ },
196
+ {
197
+ inputs: [],
198
+ name: "nativeToken",
199
+ outputs: [
200
+ {
201
+ internalType: "address",
202
+ name: "",
203
+ type: "address",
204
+ },
205
+ ],
206
+ stateMutability: "view",
207
+ type: "function",
208
+ },
209
+ {
210
+ inputs: [],
211
+ name: "wnativeToken",
212
+ outputs: [
213
+ {
214
+ internalType: "address",
215
+ name: "",
216
+ type: "address",
217
+ },
218
+ ],
219
+ stateMutability: "view",
220
+ type: "function",
221
+ },
222
+ ];
223
+
224
+ export class BalanceResolver__factory {
225
+ static readonly abi = _abi;
226
+ static createInterface(): BalanceResolverInterface {
227
+ return new utils.Interface(_abi) as BalanceResolverInterface;
228
+ }
229
+ static connect(
230
+ address: string,
231
+ signerOrProvider: Signer | Provider
232
+ ): BalanceResolver {
233
+ return new Contract(address, _abi, signerOrProvider) as BalanceResolver;
234
+ }
235
+ }
@@ -1,6 +1,9 @@
1
1
  /* Autogenerated file. Do not edit manually. */
2
2
  /* tslint:disable */
3
3
  /* eslint-disable */
4
+ export { AaveV2Resolver__factory } from "./AaveV2Resolver__factory";
5
+ export { AaveV3Resolver__factory } from "./AaveV3Resolver__factory";
6
+ export { BalanceResolver__factory } from "./BalanceResolver__factory";
4
7
  export { Erc20__factory } from "./Erc20__factory";
5
8
  export { GnosisSafe__factory } from "./GnosisSafe__factory";
6
9
  export { InstList__factory } from "./InstList__factory";
@@ -1,11 +1,17 @@
1
1
  /* Autogenerated file. Do not edit manually. */
2
2
  /* tslint:disable */
3
3
  /* eslint-disable */
4
+ export type { AaveV2Resolver } from "./AaveV2Resolver";
5
+ export type { AaveV3Resolver } from "./AaveV3Resolver";
6
+ export type { BalanceResolver } from "./BalanceResolver";
4
7
  export type { Erc20 } from "./Erc20";
5
8
  export type { GnosisSafe } from "./GnosisSafe";
6
9
  export type { InstList } from "./InstList";
7
10
  export type { InteropX } from "./InteropX";
8
11
  export * as factories from "./factories";
12
+ export { AaveV2Resolver__factory } from "./factories/AaveV2Resolver__factory";
13
+ export { AaveV3Resolver__factory } from "./factories/AaveV3Resolver__factory";
14
+ export { BalanceResolver__factory } from "./factories/BalanceResolver__factory";
9
15
  export { Erc20__factory } from "./factories/Erc20__factory";
10
16
  export { GnosisSafe__factory } from "./factories/GnosisSafe__factory";
11
17
  export { InstList__factory } from "./factories/InstList__factory";
@@ -1,3 +1,16 @@
1
+ const locale = "en-US";
2
+
3
+ export function formatUsd(value: any, fractionDigits = 0) {
4
+ const formatter = new Intl.NumberFormat(locale, {
5
+ style: "currency",
6
+ currency: "USD",
7
+ minimumFractionDigits: fractionDigits,
8
+ maximumFractionDigits: fractionDigits
9
+ });
10
+
11
+ return formatter.format(value);
12
+ }
13
+
1
14
  export function shortenHash(hash: string, length: number = 4) {
2
15
  if (!hash) return;
3
16
 
@@ -12,4 +25,44 @@ export function shortenHash(hash: string, length: number = 4) {
12
25
 
13
26
  export function short(buffer: Buffer): string {
14
27
  return buffer.toString("hex").slice(0, 8) + "...";
28
+ }
29
+
30
+ export const chainIdToName = (chainId: string | number) => {
31
+ switch (String(chainId)) {
32
+ case "1":
33
+ return "Mainnet";
34
+ case "137":
35
+ return "Polygon";
36
+ case "43114":
37
+ return "Avalanche";
38
+ default:
39
+ return "Mainnet";
40
+ }
41
+ };
42
+
43
+ export const getChainIdNativeSymbol = (chainId: string | number) => {
44
+ switch (String(chainId)) {
45
+ case "137":
46
+ return "MATIC";
47
+ case "43114":
48
+ return "AVAX";
49
+ case "1":
50
+ return "ETH";
51
+ default:
52
+ return "ETH";
53
+ }
54
+ };
55
+
56
+
57
+ export const getExplorerUrl = (chainId: string | number, suffix = '/') => {
58
+ switch (String(chainId)) {
59
+ case "1":
60
+ return `https://etherscan.io${suffix}`;
61
+ case "137":
62
+ return `https://polygonscan.com${suffix}`;
63
+ case "43114":
64
+ return `https://snowtrace.io${suffix}`;
65
+ default:
66
+ return `https://etherscan.io${suffix}`;
67
+ }
15
68
  }
@@ -1,6 +1,8 @@
1
1
  import { addresses } from "@/constants";
2
+ import { peerPool } from "@/net";
2
3
  import { GnosisSafe } from "@/typechain";
3
4
  import { ChainId } from "@/types";
5
+ import { messagePrefix } from "@ethersproject/hash";
4
6
  import { ethers, Wallet } from "ethers";
5
7
 
6
8
  export interface Signature {
@@ -20,22 +22,23 @@ export const buildSignatureBytes = (signatures: Signature[]): string => {
20
22
  return signatureBytes;
21
23
  };
22
24
 
23
- export const signGnosisSafeTx = async (
24
- {
25
- to,
26
- data = null as any,
27
- value = "0",
28
- operation = "1",
29
- baseGas = "0",
30
- gasPrice = "0",
31
- gasToken = "0x0000000000000000000000000000000000000000",
32
- refundReceiver = "0x0000000000000000000000000000000000000000",
33
- safeTxGas = "79668" as string | number,
34
- nonce = "0",
35
- chainId = 137 as ChainId,
36
- },
37
- { signer }: { signer: Wallet }
38
- ) => {
25
+ const gnosisSignatureTypes = {
26
+ SafeTx: [
27
+ { type: "address", name: "to" },
28
+ { type: "uint256", name: "value" },
29
+ { type: "bytes", name: "data" },
30
+ { type: "uint8", name: "operation" },
31
+ { type: "uint256", name: "safeTxGas" },
32
+ { type: "uint256", name: "baseGas" },
33
+ { type: "uint256", name: "gasPrice" },
34
+ { type: "address", name: "gasToken" },
35
+ { type: "address", name: "refundReceiver" },
36
+ { type: "uint256", name: "nonce" },
37
+ ],
38
+ };
39
+
40
+ export const getGnosisSignatureAddress = ({ signature, message, chainId }: { signature: string, message: Record<string, any>, chainId: ChainId }) => {
41
+
39
42
  const gnosisSafe = addresses[chainId].gnosisSafe;
40
43
 
41
44
  const domain = {
@@ -43,20 +46,24 @@ export const signGnosisSafeTx = async (
43
46
  chainId,
44
47
  };
45
48
 
46
- const types = {
47
- SafeTx: [
48
- { type: "address", name: "to" },
49
- { type: "uint256", name: "value" },
50
- { type: "bytes", name: "data" },
51
- { type: "uint8", name: "operation" },
52
- { type: "uint256", name: "safeTxGas" },
53
- { type: "uint256", name: "baseGas" },
54
- { type: "uint256", name: "gasPrice" },
55
- { type: "address", name: "gasToken" },
56
- { type: "address", name: "refundReceiver" },
57
- { type: "uint256", name: "nonce" },
58
- ],
59
- };
49
+ return ethers.utils.verifyTypedData(domain, gnosisSignatureTypes, message, signature)
50
+
51
+ }
52
+
53
+ export const generateGnosisSignatureMessage = ({
54
+ to,
55
+ data = null as any,
56
+ value = "0",
57
+ operation = "1",
58
+ baseGas = "0",
59
+ gasPrice = "0",
60
+ gasToken = "0x0000000000000000000000000000000000000000",
61
+ refundReceiver = "0x0000000000000000000000000000000000000000",
62
+ safeTxGas = "79668" as string | number,
63
+ nonce = "0",
64
+ chainId = 137 as ChainId,
65
+ }) => {
66
+ const gnosisSafe = addresses[chainId].gnosisSafe;
60
67
 
61
68
  const message = {
62
69
  baseGas,
@@ -72,7 +79,33 @@ export const signGnosisSafeTx = async (
72
79
  value,
73
80
  };
74
81
 
75
- return await signer._signTypedData(domain, types, message);
82
+ return message;
83
+ }
84
+
85
+ export const signGnosisSafeTx = async ({
86
+ to,
87
+ data = null as any,
88
+ value = "0",
89
+ operation = "1",
90
+ baseGas = "0",
91
+ gasPrice = "0",
92
+ gasToken = "0x0000000000000000000000000000000000000000",
93
+ refundReceiver = "0x0000000000000000000000000000000000000000",
94
+ safeTxGas = "79668" as string | number,
95
+ nonce = "0",
96
+ chainId = 137 as ChainId,
97
+ }, { signer }: { signer: Wallet }
98
+ ) => {
99
+ const gnosisSafe = addresses[chainId].gnosisSafe;
100
+
101
+ const domain = {
102
+ verifyingContract: gnosisSafe,
103
+ chainId,
104
+ };
105
+
106
+ const message = generateGnosisSignatureMessage({ to, data, value, operation, baseGas, gasPrice, gasToken, refundReceiver, safeTxGas, nonce, chainId });
107
+
108
+ return await signer._signTypedData(domain, gnosisSignatureTypes, message);
76
109
  };
77
110
 
78
111
  export const generateGnosisTransaction = async (
@@ -120,4 +153,14 @@ export const generateGnosisTransaction = async (
120
153
  }
121
154
 
122
155
  return transactionData;
123
- };
156
+ };
157
+
158
+ export const getGnosisOwnerPeerIds = async ({ gnosisContract = null as unknown as GnosisSafe }) => {
159
+ const owners = await gnosisContract
160
+ .getOwners()
161
+ .then((owners) => owners.map((owner) => owner.toLowerCase()));
162
+
163
+ return peerPool.activePeers
164
+ .filter((peer) => owners.includes(peer.publicAddress.toLowerCase()))
165
+ .map((peer) => peer.id);
166
+ }
@@ -6,3 +6,4 @@ export * from './http';
6
6
  export * from './interop';
7
7
  export * from './web3';
8
8
  export * from './validate';
9
+ export * from './tokens';
@@ -0,0 +1,21 @@
1
+ import { wrappedNativeToken } from "@/constants"
2
+
3
+ export const isNativeToken = (tokenAddress: string) => {
4
+ return tokenAddress.toLowerCase() === "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE".toLowerCase()
5
+ }
6
+
7
+ export const isNativeWrappedToken = (tokenAddress: string, chainId: number | string) => {
8
+ return wrappedNativeToken[chainId].toLowerCase() == tokenAddress.toLowerCase()
9
+ }
10
+
11
+ export const isNativeOrWrappedToken = (tokenAddress: string, chainId: number | string) => {
12
+ if (isNativeToken(tokenAddress)) {
13
+ return true
14
+ }
15
+
16
+ if (isNativeWrappedToken(tokenAddress, chainId)) {
17
+ return true
18
+ }
19
+
20
+ return false
21
+ }
@@ -1,5 +1,14 @@
1
- import { addresses } from "@/constants";
2
- import { InvalidChaindIdError } from "@/errors";
1
+ import abi from "@/abi";
2
+ import { addresses, capPerChain, tokens } from "@/constants";
3
+ import { InvalidChaindIdError, LowLiquidityError } from "@/errors";
4
+ import { ethers } from "ethers";
5
+ import { getContract, getRpcProviderUrl } from "./web3";
6
+ import BigNumber from "bignumber.js";
7
+ import { chainIdToName, formatUsd } from "./formatting";
8
+ import { isNativeWrappedToken } from "./tokens";
9
+ import { Prices } from "@/services";
10
+ import { JsonRpcRetryProvider } from "@/providers";
11
+ import { IPosition } from "@/db";
3
12
 
4
13
  export const validateChains = ({ sourceChainId, targetChainId }) => {
5
14
  if (!sourceChainId) {
@@ -22,3 +31,141 @@ export const validateChains = ({ sourceChainId, targetChainId }) => {
22
31
  throw new InvalidChaindIdError("Invalid target chain id");
23
32
  }
24
33
  };
34
+
35
+
36
+ export const validateSourceLiquidity = async ({
37
+ position = null as unknown as IPosition,
38
+ sourceChainId,
39
+ isSupply = false,
40
+ sourceProvider = null as unknown as JsonRpcRetryProvider,
41
+ }) => {
42
+ sourceProvider = sourceProvider || new JsonRpcRetryProvider(getRpcProviderUrl(sourceChainId));
43
+
44
+
45
+ const sourceBalanceResolverContract = getContract(
46
+ addresses[sourceChainId].balanceResolver,
47
+ abi.balanceResolver,
48
+ sourceProvider,
49
+ )
50
+
51
+ const srcBalance = await sourceBalanceResolverContract.checkLiquidity(position, addresses[sourceChainId].interopX, isSupply, false)
52
+
53
+ if (!srcBalance.isOk) {
54
+
55
+ let liquidityRequired = ""
56
+
57
+ srcBalance.withdraw.forEach(a => {
58
+ const token = tokens[sourceChainId].find(t => t.address.toLowerCase() === a.token.toLowerCase());
59
+
60
+ if (token && (new BigNumber(a.liquidityShort).isZero()) == false) {
61
+ liquidityRequired += `- Need ${new BigNumber(a.liquidityShort).dividedBy(10 ** token.decimals).toFormat()} ${token.symbol}\n`
62
+ }
63
+ })
64
+
65
+ throw new LowLiquidityError(`Liquidity low on ${chainIdToName(sourceChainId)}: \n` + liquidityRequired);
66
+ }
67
+ };
68
+
69
+ export const validateTargetLiquidity = async ({
70
+ position = null as unknown as IPosition,
71
+ targetChainId,
72
+ targetProvider = null as unknown as JsonRpcRetryProvider,
73
+ }) => {
74
+ targetProvider = targetProvider || new JsonRpcRetryProvider(getRpcProviderUrl(targetChainId));
75
+
76
+
77
+ const sourceBalanceResolverContract = getContract(
78
+ addresses[targetChainId].balanceResolver,
79
+ abi.balanceResolver,
80
+ targetProvider,
81
+ )
82
+
83
+ const targetBalance = await sourceBalanceResolverContract.checkLiquidity(position, addresses[targetChainId].interopX, true, true)
84
+
85
+ if (!targetBalance.isOk) {
86
+
87
+ let liquidityRequired = ""
88
+
89
+ targetBalance.supply.forEach(a => {
90
+ const token = tokens[targetChainId].find(t => t.address.toLowerCase() === a.token.toLowerCase());
91
+
92
+
93
+ if (token) {
94
+ liquidityRequired += `- Need ${new BigNumber(a.liquidityShort).dividedBy(10 ** token.decimals).toFormat()} ${token.symbol}\n`
95
+ }
96
+ })
97
+
98
+ throw new LowLiquidityError(`Liquidity low on ${chainIdToName(targetChainId)}: \n` + liquidityRequired);
99
+ }
100
+ };
101
+
102
+ const mapTokenAddressToMainnetToken = (
103
+ address: string,
104
+ targetChainId: number | string
105
+ ) => {
106
+ targetChainId = String(targetChainId);
107
+ const token = tokens[targetChainId].find(
108
+ (t) => t.address.toLowerCase() === address.toLowerCase()
109
+ );
110
+
111
+ if (targetChainId === "1") {
112
+ return token;
113
+ }
114
+
115
+ if (!token) {
116
+ return null;
117
+ }
118
+
119
+ return tokens["1"].find((t) => t.symbol === token.symbol);
120
+ };
121
+
122
+ export const validateLiquidityCap = async (
123
+ position: IPosition,
124
+ sourceChainId: number | string,
125
+ targetChainId: number | string
126
+ ) => {
127
+ const cap = capPerChain[targetChainId] || 1_000_000;
128
+ const mainnetPrices = Prices.getMainnetPrices();
129
+ const nativeTokenPrice = Prices.getNetworkTokenPrices()[targetChainId];
130
+
131
+ let total = new BigNumber(0);
132
+
133
+ const supplyPosition = position.supply;
134
+
135
+ for (const { sourceToken: srcTokenAddress, targetToken: tokenAddress, amount } of supplyPosition) {
136
+ let tokenPrice;
137
+ let token;
138
+
139
+ if (isNativeWrappedToken(srcTokenAddress, sourceChainId)) {
140
+ tokenPrice = nativeTokenPrice;
141
+ token = tokens[targetChainId].find(t => t.address === '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE');
142
+ } else {
143
+ const mainnetToken = mapTokenAddressToMainnetToken(
144
+ tokenAddress,
145
+ targetChainId
146
+ );
147
+ if (!mainnetToken) {
148
+ throw new Error(`Token ${tokenAddress} is not valid.`);
149
+ }
150
+
151
+ tokenPrice = mainnetPrices[mainnetToken.address];
152
+
153
+ if (!tokenPrice) {
154
+ throw new Error(`Token ${tokenAddress} is not valid!`);
155
+ }
156
+
157
+ token = mainnetToken;
158
+ }
159
+
160
+
161
+ total = total.plus((tokenPrice * Number(amount)) / 10 ** token.decimals);
162
+ }
163
+
164
+ if (total.isGreaterThan(cap)) {
165
+ throw new Error(
166
+ `Total amount of tokens in the transaction is greater than the maximum limit of estimated ${formatUsd(
167
+ cap
168
+ )}`
169
+ );
170
+ }
171
+ };
package/src/utils/web3.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { UnsupportedChaindIdError } from "@/errors";
1
2
  import { ChainId } from "@/types";
2
3
  import retry from "async-retry";
3
4
  import { ethers } from "ethers";
@@ -11,7 +12,7 @@ export const getRpcProviderUrl = (chainId: ChainId) => {
11
12
  case 43114:
12
13
  return "https://rpc.ankr.com/avalanche";
13
14
  default:
14
- throw new Error(`Unknown chainId: ${chainId}`);
15
+ throw new UnsupportedChaindIdError(chainId);
15
16
  }
16
17
  };
17
18
 
@@ -25,7 +26,7 @@ export class ContractError extends Error {
25
26
  export function getContract<TContract extends ethers.Contract>(
26
27
  address: string,
27
28
  contractInterface: ethers.ContractInterface | any,
28
- signerOrProvider?: ethers.Signer | ethers.providers.Provider
29
+ signerOrProvider: ethers.Signer | ethers.providers.Provider
29
30
  ) {
30
31
  if (
31
32
  !ethers.utils.getAddress(address) ||