@instadapp/interop-x 0.0.0-dev.20e87fa → 0.0.0-dev.2187d72

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 (86) hide show
  1. package/dist/package.json +8 -1
  2. package/dist/src/abi/aaveV2Resolver.json +832 -0
  3. package/dist/src/abi/balanceResolver.json +211 -0
  4. package/dist/src/abi/index.js +4 -0
  5. package/dist/src/api/index.js +7 -0
  6. package/dist/src/constants/addresses.js +5 -1
  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 +4 -0
  14. package/dist/src/errors/index.js +30 -0
  15. package/dist/src/gnosis/actions/aaveV2/source.js +2 -1
  16. package/dist/src/gnosis/actions/aaveV2/target.js +3 -2
  17. package/dist/src/index.js +2 -1
  18. package/dist/src/providers/index.js +17 -0
  19. package/dist/src/providers/retry-provider.js +45 -0
  20. package/dist/src/services/Prices.js +74 -0
  21. package/dist/src/services/index.js +8 -0
  22. package/dist/src/tasks/InteropX/{ProcessSubmitSubmitEvents.js → ProcessSubmitEvents.js} +95 -8
  23. package/dist/src/tasks/InteropX/ProcessValidateEvents.js +27 -4
  24. package/dist/src/tasks/InteropX/SyncLogExecuteEvents.js +113 -0
  25. package/dist/src/tasks/InteropX/SyncLogSubmitEvents.js +2 -1
  26. package/dist/src/tasks/InteropX/SyncLogValidateEvents.js +5 -4
  27. package/dist/src/tasks/index.js +7 -5
  28. package/dist/src/typechain/AaveV2Resolver.js +2 -0
  29. package/dist/src/typechain/BalanceResolver.js +2 -0
  30. package/dist/src/typechain/factories/AaveV2Resolver__factory.js +1191 -0
  31. package/dist/src/typechain/factories/BalanceResolver__factory.js +228 -0
  32. package/dist/src/typechain/factories/index.js +5 -1
  33. package/dist/src/typechain/index.js +5 -1
  34. package/dist/src/utils/async.js +18 -0
  35. package/dist/src/utils/dsa.js +24 -0
  36. package/dist/src/utils/formatting.js +67 -0
  37. package/dist/src/utils/gnosis.js +77 -0
  38. package/dist/src/utils/http.js +10 -0
  39. package/dist/src/utils/index.js +22 -220
  40. package/dist/src/utils/interop.js +16 -0
  41. package/dist/src/utils/tokens.js +22 -0
  42. package/dist/src/utils/validate.js +111 -0
  43. package/dist/src/utils/web3.js +93 -0
  44. package/package.json +8 -1
  45. package/src/abi/aaveV2Resolver.json +832 -0
  46. package/src/abi/balanceResolver.json +211 -0
  47. package/src/abi/index.ts +4 -0
  48. package/src/api/index.ts +8 -0
  49. package/src/constants/addresses.ts +16 -2
  50. package/src/constants/capPerChain.ts +5 -0
  51. package/src/constants/index.ts +2 -0
  52. package/src/constants/tokens.ts +44 -44
  53. package/src/constants/wrappedNativeToken.ts +5 -0
  54. package/src/crons/index.ts +1 -0
  55. package/src/crons/prices.ts +12 -0
  56. package/src/db/models/transaction.ts +21 -0
  57. package/src/errors/index.ts +26 -0
  58. package/src/gnosis/actions/aaveV2/source.ts +2 -1
  59. package/src/gnosis/actions/aaveV2/target.ts +4 -2
  60. package/src/index.ts +1 -0
  61. package/src/providers/index.ts +1 -0
  62. package/src/providers/retry-provider.ts +51 -0
  63. package/src/services/Prices.ts +89 -0
  64. package/src/services/index.ts +1 -0
  65. package/src/tasks/InteropX/{ProcessSubmitSubmitEvents.ts → ProcessSubmitEvents.ts} +120 -14
  66. package/src/tasks/InteropX/ProcessValidateEvents.ts +38 -12
  67. package/src/tasks/InteropX/SyncLogExecuteEvents.ts +161 -0
  68. package/src/tasks/InteropX/SyncLogSubmitEvents.ts +5 -6
  69. package/src/tasks/InteropX/SyncLogValidateEvents.ts +8 -9
  70. package/src/tasks/index.ts +8 -5
  71. package/src/typechain/AaveV2Resolver.ts +1017 -0
  72. package/src/typechain/BalanceResolver.ts +266 -0
  73. package/src/typechain/factories/AaveV2Resolver__factory.ts +1198 -0
  74. package/src/typechain/factories/BalanceResolver__factory.ts +235 -0
  75. package/src/typechain/factories/index.ts +2 -0
  76. package/src/typechain/index.ts +4 -0
  77. package/src/utils/async.ts +22 -0
  78. package/src/utils/dsa.ts +30 -0
  79. package/src/utils/formatting.ts +68 -0
  80. package/src/utils/gnosis.ts +155 -0
  81. package/src/utils/http.ts +6 -0
  82. package/src/utils/index.ts +9 -365
  83. package/src/utils/interop.ts +28 -0
  84. package/src/utils/tokens.ts +21 -0
  85. package/src/utils/validate.ts +174 -0
  86. package/src/utils/web3.ts +132 -0
@@ -0,0 +1,211 @@
1
+ [
2
+ {
3
+ "inputs": [
4
+ {
5
+ "internalType": "address",
6
+ "name": "_wnativeToken",
7
+ "type": "address"
8
+ }
9
+ ],
10
+ "stateMutability": "nonpayable",
11
+ "type": "constructor"
12
+ },
13
+ {
14
+ "inputs": [
15
+ {
16
+ "components": [
17
+ {
18
+ "components": [
19
+ {
20
+ "internalType": "address",
21
+ "name": "sourceToken",
22
+ "type": "address"
23
+ },
24
+ {
25
+ "internalType": "address",
26
+ "name": "targetToken",
27
+ "type": "address"
28
+ },
29
+ {
30
+ "internalType": "uint256",
31
+ "name": "amount",
32
+ "type": "uint256"
33
+ }
34
+ ],
35
+ "internalType": "struct Variables.TokenInfo[]",
36
+ "name": "supply",
37
+ "type": "tuple[]"
38
+ },
39
+ {
40
+ "components": [
41
+ {
42
+ "internalType": "address",
43
+ "name": "sourceToken",
44
+ "type": "address"
45
+ },
46
+ {
47
+ "internalType": "address",
48
+ "name": "targetToken",
49
+ "type": "address"
50
+ },
51
+ {
52
+ "internalType": "uint256",
53
+ "name": "amount",
54
+ "type": "uint256"
55
+ }
56
+ ],
57
+ "internalType": "struct Variables.TokenInfo[]",
58
+ "name": "withdraw",
59
+ "type": "tuple[]"
60
+ }
61
+ ],
62
+ "internalType": "struct Variables.Position",
63
+ "name": "position",
64
+ "type": "tuple"
65
+ },
66
+ {
67
+ "internalType": "address",
68
+ "name": "liquidityContract",
69
+ "type": "address"
70
+ },
71
+ {
72
+ "internalType": "bool",
73
+ "name": "isSupply",
74
+ "type": "bool"
75
+ },
76
+ {
77
+ "internalType": "bool",
78
+ "name": "isTargetToken",
79
+ "type": "bool"
80
+ }
81
+ ],
82
+ "name": "checkLiquidity",
83
+ "outputs": [
84
+ {
85
+ "components": [
86
+ {
87
+ "internalType": "bool",
88
+ "name": "isOk",
89
+ "type": "bool"
90
+ },
91
+ {
92
+ "components": [
93
+ {
94
+ "internalType": "address",
95
+ "name": "token",
96
+ "type": "address"
97
+ },
98
+ {
99
+ "internalType": "uint256",
100
+ "name": "liquidityAvailable",
101
+ "type": "uint256"
102
+ },
103
+ {
104
+ "internalType": "uint256",
105
+ "name": "liquidityShort",
106
+ "type": "uint256"
107
+ }
108
+ ],
109
+ "internalType": "struct Helpers.LiquidityData[]",
110
+ "name": "supply",
111
+ "type": "tuple[]"
112
+ },
113
+ {
114
+ "components": [
115
+ {
116
+ "internalType": "address",
117
+ "name": "token",
118
+ "type": "address"
119
+ },
120
+ {
121
+ "internalType": "uint256",
122
+ "name": "liquidityAvailable",
123
+ "type": "uint256"
124
+ },
125
+ {
126
+ "internalType": "uint256",
127
+ "name": "liquidityShort",
128
+ "type": "uint256"
129
+ }
130
+ ],
131
+ "internalType": "struct Helpers.LiquidityData[]",
132
+ "name": "withdraw",
133
+ "type": "tuple[]"
134
+ }
135
+ ],
136
+ "internalType": "struct Helpers.PositionData",
137
+ "name": "p",
138
+ "type": "tuple"
139
+ }
140
+ ],
141
+ "stateMutability": "view",
142
+ "type": "function"
143
+ },
144
+ {
145
+ "inputs": [
146
+ {
147
+ "internalType": "address[]",
148
+ "name": "tokens",
149
+ "type": "address[]"
150
+ },
151
+ {
152
+ "internalType": "address",
153
+ "name": "liquidityContract",
154
+ "type": "address"
155
+ }
156
+ ],
157
+ "name": "getLiquidity",
158
+ "outputs": [
159
+ {
160
+ "components": [
161
+ {
162
+ "internalType": "address",
163
+ "name": "token",
164
+ "type": "address"
165
+ },
166
+ {
167
+ "internalType": "uint256",
168
+ "name": "liquidityAvailable",
169
+ "type": "uint256"
170
+ },
171
+ {
172
+ "internalType": "uint256",
173
+ "name": "liquidityShort",
174
+ "type": "uint256"
175
+ }
176
+ ],
177
+ "internalType": "struct Helpers.LiquidityData[]",
178
+ "name": "l",
179
+ "type": "tuple[]"
180
+ }
181
+ ],
182
+ "stateMutability": "view",
183
+ "type": "function"
184
+ },
185
+ {
186
+ "inputs": [],
187
+ "name": "nativeToken",
188
+ "outputs": [
189
+ {
190
+ "internalType": "address",
191
+ "name": "",
192
+ "type": "address"
193
+ }
194
+ ],
195
+ "stateMutability": "view",
196
+ "type": "function"
197
+ },
198
+ {
199
+ "inputs": [],
200
+ "name": "wnativeToken",
201
+ "outputs": [
202
+ {
203
+ "internalType": "address",
204
+ "name": "",
205
+ "type": "address"
206
+ }
207
+ ],
208
+ "stateMutability": "view",
209
+ "type": "function"
210
+ }
211
+ ]
package/src/abi/index.ts CHANGED
@@ -3,6 +3,8 @@ import erc20 from "./erc20.json";
3
3
  import interopX from "./interopX.json";
4
4
  import { connectors } from "./connectors";
5
5
  import instList from "./instList.json";
6
+ import balanceResolver from "./balanceResolver.json";
7
+ import aaveV2Resolver from "./aaveV2Resolver.json";
6
8
 
7
9
  export default {
8
10
  gnosisSafe,
@@ -10,4 +12,6 @@ export default {
10
12
  interopX,
11
13
  connectors,
12
14
  instList,
15
+ balanceResolver,
16
+ aaveV2Resolver,
13
17
  };
package/src/api/index.ts CHANGED
@@ -26,6 +26,14 @@ export const startApiServer = async () => {
26
26
  })
27
27
  })
28
28
 
29
+ server.get('/transactions/:transaction', async (req) => {
30
+ return await Transaction.findOne({
31
+ where: {
32
+ transactionHash: (req.params as any).transaction
33
+ }
34
+ })
35
+ })
36
+
29
37
  await server.listen(PORT, HOST)
30
38
 
31
39
  logger.log(`RPC Server listening at http://${HOST}:${PORT}`)
@@ -1,3 +1,13 @@
1
+ export type NetworkAddresses = {
2
+ gnosisSafe: string
3
+ multisend: string
4
+ interopX: string
5
+ dsaAddress: string
6
+ instList: string
7
+ aaveResolver: string
8
+ balanceResolver: string
9
+ }
10
+
1
11
  export const addresses = {
2
12
  137: {
3
13
  gnosisSafe: '0x5635d2910e51da33d9DC0422c893CF4F28B69A25',
@@ -5,12 +15,16 @@ export const addresses = {
5
15
  interopX: '0xDB6083df37C5F224a3dF84A4B5f9fB60b6c8670a',
6
16
  dsaAddress: '0x9Fbd453a8e7a158510fBae5D9935958507cf4b19',
7
17
  instList: "0x839c2D3aDe63DF5b0b8F3E57D5e145057Ab41556",
18
+ aaveResolver: "0x0731c1d0154C2bE74d50e2083Df10aa531AAbb10",
19
+ balanceResolver: "0xecc3991846bB21717B95A593Fd86C5930A491F95",
8
20
  },
9
21
  43114: {
10
22
  gnosisSafe: '0x31d7a5194Fe60AC209Cf1Ce2d539C9A60662Ed6b',
11
23
  multisend: '0x998739BFdAAdde7C933B942a68053933098f9EDa',
12
24
  interopX: '0xA82A87096709E3D8648c9d9a22f31133bC4B6d32',
13
- dsaAddress: '0x053949Af585bDF29A7bdFB97568F953770028D2b',
25
+ dsaAddress: '0x995e67a652d2be45a8bff438b3dfac66dfff1c46',
14
26
  instList: "0x9926955e0Dd681Dc303370C52f4Ad0a4dd061687",
27
+ aaveResolver: "0x7C5C59944263311a1e350E3B871B0d1d8E9A513E",
28
+ balanceResolver: "0xD45dFA34cEeAB567208041331F5ed9916C23b1E8",
15
29
  }
16
- } as Record<number, { gnosisSafe: string, multisend: string, interopX: string, dsaAddress: string, instList: string }>
30
+ } as Record<number, NetworkAddresses>
@@ -0,0 +1,5 @@
1
+ export const capPerChain = {
2
+ "1": 100_000_000,
3
+ "137": 100_000_000,
4
+ "43114": 100_000_000,
5
+ };
@@ -1,3 +1,5 @@
1
1
  export * from './addresses';
2
2
  export * from './tokens';
3
3
  export * from './blockConfirmations';
4
+ export * from './wrappedNativeToken';
5
+ export * from './capPerChain';
@@ -1,48 +1,48 @@
1
1
  export const tokens = {
2
- // 1: [
3
- // {
4
- // aliases: ['eth'],
5
- // symbol: "ETH",
6
- // name: "Ethereum",
7
- // address: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
8
- // decimals: 18,
9
- // },
10
- // {
11
- // aliases: ['weth'],
12
- // symbol: "WETH",
13
- // name: "Wrapped Ethereum",
14
- // address: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
15
- // decimals: 18,
16
- // },
17
- // {
18
- // aliases: ['dai'],
19
- // symbol: "DAI",
20
- // name: "DAI Stable",
21
- // address: "0x6B175474E89094C44Da98b954EedeAC495271d0F",
22
- // decimals: 18,
23
- // },
24
- // {
25
- // aliases: ['usdc'],
26
- // symbol: "USDC",
27
- // name: "USD Coin",
28
- // address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
29
- // decimals: 6,
30
- // },
31
- // {
32
- // aliases: ['usdt'],
33
- // symbol: "USDT",
34
- // name: "Tether USD Coin",
35
- // address: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
36
- // decimals: 6,
37
- // },
38
- // {
39
- // aliases: ['wbtc'],
40
- // symbol: "WBTC",
41
- // name: "Wrapped BTC",
42
- // address: "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599",
43
- // decimals: 8,
44
- // },
45
- // ],
2
+ 1: [
3
+ {
4
+ aliases: ['eth'],
5
+ symbol: "ETH",
6
+ name: "Ethereum",
7
+ address: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
8
+ decimals: 18,
9
+ },
10
+ {
11
+ aliases: ['weth'],
12
+ symbol: "WETH",
13
+ name: "Wrapped Ethereum",
14
+ address: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
15
+ decimals: 18,
16
+ },
17
+ {
18
+ aliases: ['dai'],
19
+ symbol: "DAI",
20
+ name: "DAI Stable",
21
+ address: "0x6B175474E89094C44Da98b954EedeAC495271d0F",
22
+ decimals: 18,
23
+ },
24
+ {
25
+ aliases: ['usdc'],
26
+ symbol: "USDC",
27
+ name: "USD Coin",
28
+ address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
29
+ decimals: 6,
30
+ },
31
+ {
32
+ aliases: ['usdt'],
33
+ symbol: "USDT",
34
+ name: "Tether USD Coin",
35
+ address: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
36
+ decimals: 6,
37
+ },
38
+ {
39
+ aliases: ['wbtc'],
40
+ symbol: "WBTC",
41
+ name: "Wrapped BTC",
42
+ address: "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599",
43
+ decimals: 8,
44
+ },
45
+ ],
46
46
  137: [
47
47
  {
48
48
  aliases: ['eth', 'weth'],
@@ -0,0 +1,5 @@
1
+ export const wrappedNativeToken = {
2
+ "1": '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
3
+ "137": '0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270',
4
+ "43114": '0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7'
5
+ };
@@ -0,0 +1 @@
1
+ import "./prices"
@@ -0,0 +1,12 @@
1
+ import { Prices } from "@/services";
2
+ import cron from "node-cron";
3
+
4
+ Prices.fetch().catch(console.log);
5
+
6
+ cron.schedule(`* * * * *`, async () => {
7
+ try {
8
+ await Prices.fetch();
9
+ } catch (error) {
10
+ console.log("Prices: ", error.message);
11
+ }
12
+ });
@@ -83,6 +83,22 @@ export class Transaction extends Model<
83
83
  metadata: string;
84
84
  }>;
85
85
 
86
+ declare executeEvent: CreationOptional<{
87
+ sourceSpells: Array<{ connector: string; data: string }>;
88
+ targetSpells: Array<{ connector: string; data: string }>;
89
+ position: IPosition;
90
+ actionId: string;
91
+ actionIdHashHash: string;
92
+ actionIdHash: string;
93
+ sourceSender: string;
94
+ sourceDsaId: string;
95
+ targetDsaId: string;
96
+ sourceChainId: number;
97
+ targetChainId: number;
98
+ vnonce: string;
99
+ metadata: string;
100
+ }>;
101
+
86
102
  declare status: CreationOptional<string>;
87
103
 
88
104
  declare createdAt: CreationOptional<Date>;
@@ -158,6 +174,11 @@ Transaction.init(
158
174
  allowNull: true,
159
175
  },
160
176
 
177
+ executeEvent: {
178
+ type: DataTypes.JSON,
179
+ allowNull: true,
180
+ },
181
+
161
182
  status: {
162
183
  type: DataTypes.STRING,
163
184
  defaultValue: "pending",
@@ -0,0 +1,26 @@
1
+ export class UnsupportedChaindIdError extends Error {
2
+ constructor(chainId: any) {
3
+ super(`Unsupported chainId: ${chainId}`);
4
+ }
5
+ }
6
+
7
+ export class LiquidityError extends Error {
8
+ constructor(message?: string) {
9
+ super(message || "Not enough liquidity");
10
+ Object.setPrototypeOf(this, new.target.prototype);
11
+ }
12
+ }
13
+
14
+ export class InvalidChaindIdError extends Error {
15
+ constructor(message?: string) {
16
+ super(message || "Invalid chain id");
17
+ Object.setPrototypeOf(this, new.target.prototype);
18
+ }
19
+ }
20
+
21
+ export class LowLiquidityError extends Error {
22
+ constructor(message?: string) {
23
+ super(message || "Low liquidity");
24
+ Object.setPrototypeOf(this, new.target.prototype);
25
+ }
26
+ }
@@ -2,6 +2,7 @@ import abi from "@/abi";
2
2
  import config from "@/config";
3
3
  import { addresses } from "@/constants";
4
4
  import { Transaction } from "@/db";
5
+ import { JsonRpcRetryProvider } from "@/providers";
5
6
  import { InteropX } from "@/typechain";
6
7
  import { ChainId } from "@/types";
7
8
  import { encodeConnectorMethod, getContract, getRpcProviderUrl } from "@/utils";
@@ -25,7 +26,7 @@ export default async function (transaction: Transaction) {
25
26
  metadata,
26
27
  } = transaction.submitEvent;
27
28
 
28
- const sourceChainProvider = new ethers.providers.JsonRpcProvider(
29
+ const sourceChainProvider = new JsonRpcRetryProvider(
29
30
  getRpcProviderUrl(sourceChainId as ChainId)
30
31
  );
31
32
  const sourceWallet = new ethers.Wallet(
@@ -2,6 +2,7 @@ import abi from "@/abi";
2
2
  import config from "@/config";
3
3
  import { addresses } from "@/constants";
4
4
  import { Transaction } from "@/db";
5
+ import { JsonRpcRetryProvider } from "@/providers";
5
6
  import { InteropX } from "@/typechain";
6
7
  import { InstList } from "@/typechain/InstList";
7
8
  import { ChainId } from "@/types";
@@ -27,7 +28,7 @@ export default async function (transaction: Transaction) {
27
28
  metadata,
28
29
  } = transaction.validateEvent;
29
30
 
30
- const targetChainProvider = new ethers.providers.JsonRpcProvider(
31
+ const targetChainProvider = new JsonRpcRetryProvider(
31
32
  getRpcProviderUrl(targetChainId as ChainId)
32
33
  );
33
34
  const targetWallet = new ethers.Wallet(
@@ -38,7 +39,8 @@ export default async function (transaction: Transaction) {
38
39
 
39
40
  const targetInstListContract = getContract<InstList>(
40
41
  addresses[targetChainId].instList,
41
- abi.instList ,
42
+ abi.instList,
43
+ targetChainProvider,
42
44
  );
43
45
 
44
46
  const targetDsaAddress = await targetInstListContract.accountAddr(targetDsaId)
package/src/index.ts CHANGED
@@ -82,6 +82,7 @@ import { startPeer, protocol, peerPool } from "@/net";
82
82
  import { startApiServer } from '@/api';
83
83
  import { Transaction } from './db';
84
84
  import { shortenHash } from './utils';
85
+ import './crons';
85
86
 
86
87
  async function main() {
87
88
 
@@ -0,0 +1 @@
1
+ export * from './retry-provider';
@@ -0,0 +1,51 @@
1
+ import { ethers } from "ethers";
2
+ import wait from "waait";
3
+ import Bluebird from "bluebird";
4
+
5
+ export interface RetryOptions {
6
+ delay?: number;
7
+ timeouts: number[];
8
+ }
9
+
10
+ export function promiseTimeout<T>(ms: number, promise: Promise<T>): Promise<T> {
11
+ return Bluebird.resolve(promise).timeout(ms);
12
+ }
13
+
14
+ export function retryOperation(
15
+ retriesLeft: number,
16
+ operation: () => Promise<any>,
17
+ options: RetryOptions
18
+ ) {
19
+ return new Promise((resolve, reject) => {
20
+ const { timeouts } = options;
21
+ // Find the timeout for this specific iteration
22
+ const timeout = timeouts[timeouts.length - retriesLeft];
23
+
24
+ // Wrap the original operation in a timeout
25
+ const execution = promiseTimeout(timeout, operation());
26
+
27
+ // If the promise is successful, resolve it and bubble the result up
28
+ return execution.then(resolve).catch((reason: any) => {
29
+ // If there are any retries left, we call the same retryOperation function again,
30
+ // but decrementing the number of retries left by 1
31
+ if (retriesLeft - 1 > 0) {
32
+ // Delay the new attempt slightly
33
+ return wait(options.delay || 50)
34
+ .then(retryOperation.bind(null, retriesLeft - 1, operation, options))
35
+ .then(resolve)
36
+ .catch(reject);
37
+ }
38
+ // Reject (and bubble the result up) if there are no more retries
39
+ return reject(reason);
40
+ });
41
+ });
42
+ }
43
+
44
+ export class JsonRpcRetryProvider extends ethers.providers.JsonRpcProvider {
45
+ public perform(method: string, params: any): Promise<any> {
46
+ const timeouts = [5_000, 10_000];
47
+ const operation = () => super.perform(method, params);
48
+
49
+ return retryOperation(2, operation, { timeouts, delay: 50 });
50
+ }
51
+ }
@@ -0,0 +1,89 @@
1
+ import Web3Utils from "web3-utils";
2
+ import path from "path";
3
+ import fs from "fs";
4
+ import { tokens } from "@/constants";
5
+ import expandHomeDir from "expand-home-dir";
6
+ import config from "@/config";
7
+ import { http } from "@/utils";
8
+
9
+ const basePath = expandHomeDir(`~/.interop-x/data/${config.publicAddress}/${config.staging ? 'staging' : ''}`);
10
+ const mainnetPricesFilePath = path.resolve(basePath, "./mainnetPrices.json");
11
+ const networkTokenPricesFilePath = path.resolve(basePath, "./networkTokenPrices.json");
12
+
13
+ class Prices {
14
+ private static mainnetPrices = {};
15
+
16
+ private static networkTokenPrices = {};
17
+
18
+ static async fetch() {
19
+
20
+ if (!fs.existsSync(mainnetPricesFilePath)) {
21
+ fs.writeFileSync(mainnetPricesFilePath, JSON.stringify({}));
22
+ }
23
+
24
+ if (!fs.existsSync(networkTokenPricesFilePath)) {
25
+ fs.writeFileSync(networkTokenPricesFilePath, JSON.stringify({}));
26
+ }
27
+
28
+ this.mainnetPrices = JSON.parse(fs.readFileSync(mainnetPricesFilePath, "utf8"));
29
+ this.networkTokenPrices = JSON.parse(fs.readFileSync(networkTokenPricesFilePath, "utf8"));
30
+
31
+ try {
32
+ const path = tokens["1"]
33
+ .filter((a) => a.symbol !== "ETH")
34
+ .map((token) => token.address)
35
+ .join(",");
36
+
37
+ const [response, ethResponse, avaxResponse, polygonResponse] =
38
+ await Promise.all([
39
+ http.get(
40
+ "https://api.coingecko.com/api/v3/simple/token_price/ethereum",
41
+ {
42
+ params: { contract_addresses: path, vs_currencies: "usd" },
43
+ }
44
+ ),
45
+ http.get("https://api.coingecko.com/api/v3/simple/price", {
46
+ params: { ids: "ethereum", vs_currencies: "usd" },
47
+ }),
48
+ http.get("https://api.coingecko.com/api/v3/simple/price", {
49
+ params: { ids: "avalanche-2", vs_currencies: "usd" },
50
+ }),
51
+ http.get("https://api.coingecko.com/api/v3/simple/price", {
52
+ params: { ids: "matic-network", vs_currencies: "usd" },
53
+ }),
54
+ ]);
55
+
56
+ this.networkTokenPrices = {
57
+ "1": ethResponse.data.ethereum.usd.toString(),
58
+ "137": polygonResponse.data["matic-network"].usd.toString(),
59
+ "43114": avaxResponse.data["avalanche-2"].usd.toString(),
60
+ };
61
+
62
+ const data = response.data;
63
+
64
+ for (const key in data) {
65
+ const _key = Web3Utils.toChecksumAddress(key);
66
+ if (!data[key].usd) continue;
67
+ this.mainnetPrices[_key] = data[key].usd.toString();
68
+ }
69
+
70
+ this.mainnetPrices["0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE"] =
71
+ ethResponse.data.ethereum.usd.toString();
72
+ } catch (error) { }
73
+
74
+ fs.writeFileSync(mainnetPricesFilePath, JSON.stringify(this.mainnetPrices));
75
+ fs.writeFileSync(networkTokenPricesFilePath, JSON.stringify(this.networkTokenPrices));
76
+
77
+ return this.mainnetPrices;
78
+ }
79
+
80
+ static getMainnetPrices() {
81
+ return this.mainnetPrices;
82
+ }
83
+
84
+ static getNetworkTokenPrices() {
85
+ return this.networkTokenPrices;
86
+ }
87
+ }
88
+
89
+ export default Prices;
@@ -0,0 +1 @@
1
+ export { default as Prices } from './Prices';