@instadapp/interop-x 0.0.0-dev.051cd4e

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 (94) hide show
  1. package/.env.example +2 -0
  2. package/.github/workflows/publish-dev.yml +30 -0
  3. package/README.md +21 -0
  4. package/bin/interop-x +2 -0
  5. package/dist/package.json +72 -0
  6. package/dist/src/abi/erc20.json +350 -0
  7. package/dist/src/abi/gnosisSafe.json +747 -0
  8. package/dist/src/abi/index.js +15 -0
  9. package/dist/src/abi/interopBridgeToken.json +286 -0
  10. package/dist/src/abi/interopXGateway.json +184 -0
  11. package/dist/src/api/index.js +33 -0
  12. package/dist/src/config/index.js +22 -0
  13. package/dist/src/constants/addresses.js +20 -0
  14. package/dist/src/constants/index.js +19 -0
  15. package/dist/src/constants/itokens.js +13 -0
  16. package/dist/src/constants/tokens.js +107 -0
  17. package/dist/src/db/index.js +17 -0
  18. package/dist/src/db/models/index.js +17 -0
  19. package/dist/src/db/models/transaction.js +54 -0
  20. package/dist/src/db/sequelize.js +23 -0
  21. package/dist/src/index.js +103 -0
  22. package/dist/src/logger/index.js +138 -0
  23. package/dist/src/net/index.js +19 -0
  24. package/dist/src/net/peer/index.js +108 -0
  25. package/dist/src/net/pool/index.js +125 -0
  26. package/dist/src/net/protocol/dial/BaseDialProtocol.js +106 -0
  27. package/dist/src/net/protocol/dial/SignatureDialProtocol.js +56 -0
  28. package/dist/src/net/protocol/index.js +121 -0
  29. package/dist/src/tasks/AutoUpdateTask.js +45 -0
  30. package/dist/src/tasks/BaseTask.js +61 -0
  31. package/dist/src/tasks/InteropBridge/ProcessWithdrawEvents.js +147 -0
  32. package/dist/src/tasks/InteropBridge/SyncWithdrawEvents.js +70 -0
  33. package/dist/src/tasks/InteropXGateway/ProcessDepositEvents.js +150 -0
  34. package/dist/src/tasks/InteropXGateway/SyncDepositEvents.js +75 -0
  35. package/dist/src/tasks/index.js +42 -0
  36. package/dist/src/typechain/Erc20.js +2 -0
  37. package/dist/src/typechain/GnosisSafe.js +2 -0
  38. package/dist/src/typechain/InteropBridgeToken.js +2 -0
  39. package/dist/src/typechain/InteropXGateway.js +2 -0
  40. package/dist/src/typechain/common.js +2 -0
  41. package/dist/src/typechain/factories/Erc20__factory.js +367 -0
  42. package/dist/src/typechain/factories/GnosisSafe__factory.js +1174 -0
  43. package/dist/src/typechain/factories/InteropBridgeToken__factory.js +459 -0
  44. package/dist/src/typechain/factories/InteropXGateway__factory.js +265 -0
  45. package/dist/src/typechain/factories/index.js +14 -0
  46. package/dist/src/typechain/index.js +35 -0
  47. package/dist/src/types.js +21 -0
  48. package/dist/src/utils/index.js +228 -0
  49. package/package.json +72 -0
  50. package/patches/@ethersproject+properties+5.6.0.patch +13 -0
  51. package/src/abi/erc20.json +350 -0
  52. package/src/abi/gnosisSafe.json +747 -0
  53. package/src/abi/index.ts +11 -0
  54. package/src/abi/interopBridgeToken.json +286 -0
  55. package/src/abi/interopXGateway.json +184 -0
  56. package/src/api/index.ts +33 -0
  57. package/src/config/index.ts +32 -0
  58. package/src/constants/addresses.ts +17 -0
  59. package/src/constants/index.ts +3 -0
  60. package/src/constants/itokens.ts +10 -0
  61. package/src/constants/tokens.ts +104 -0
  62. package/src/db/index.ts +1 -0
  63. package/src/db/models/index.ts +1 -0
  64. package/src/db/models/transaction.ts +96 -0
  65. package/src/db/sequelize.ts +22 -0
  66. package/src/index.ts +123 -0
  67. package/src/logger/index.ts +157 -0
  68. package/src/net/index.ts +3 -0
  69. package/src/net/peer/index.ts +120 -0
  70. package/src/net/pool/index.ts +154 -0
  71. package/src/net/protocol/dial/BaseDialProtocol.ts +104 -0
  72. package/src/net/protocol/dial/SignatureDialProtocol.ts +71 -0
  73. package/src/net/protocol/index.ts +182 -0
  74. package/src/tasks/AutoUpdateTask.ts +54 -0
  75. package/src/tasks/BaseTask.ts +75 -0
  76. package/src/tasks/InteropBridge/ProcessWithdrawEvents.ts +233 -0
  77. package/src/tasks/InteropBridge/SyncWithdrawEvents.ts +121 -0
  78. package/src/tasks/InteropXGateway/ProcessDepositEvents.ts +245 -0
  79. package/src/tasks/InteropXGateway/SyncDepositEvents.ts +126 -0
  80. package/src/tasks/index.ts +41 -0
  81. package/src/typechain/Erc20.ts +491 -0
  82. package/src/typechain/GnosisSafe.ts +1728 -0
  83. package/src/typechain/InteropBridgeToken.ts +686 -0
  84. package/src/typechain/InteropXGateway.ts +407 -0
  85. package/src/typechain/common.ts +44 -0
  86. package/src/typechain/factories/Erc20__factory.ts +368 -0
  87. package/src/typechain/factories/GnosisSafe__factory.ts +1178 -0
  88. package/src/typechain/factories/InteropBridgeToken__factory.ts +466 -0
  89. package/src/typechain/factories/InteropXGateway__factory.ts +272 -0
  90. package/src/typechain/factories/index.ts +7 -0
  91. package/src/typechain/index.ts +12 -0
  92. package/src/types.ts +39 -0
  93. package/src/utils/index.ts +307 -0
  94. package/tsconfig.json +30 -0
@@ -0,0 +1,7 @@
1
+ /* Autogenerated file. Do not edit manually. */
2
+ /* tslint:disable */
3
+ /* eslint-disable */
4
+ export { Erc20__factory } from "./Erc20__factory";
5
+ export { GnosisSafe__factory } from "./GnosisSafe__factory";
6
+ export { InteropBridgeToken__factory } from "./InteropBridgeToken__factory";
7
+ export { InteropXGateway__factory } from "./InteropXGateway__factory";
@@ -0,0 +1,12 @@
1
+ /* Autogenerated file. Do not edit manually. */
2
+ /* tslint:disable */
3
+ /* eslint-disable */
4
+ export type { Erc20 } from "./Erc20";
5
+ export type { GnosisSafe } from "./GnosisSafe";
6
+ export type { InteropBridgeToken } from "./InteropBridgeToken";
7
+ export type { InteropXGateway } from "./InteropXGateway";
8
+ export * as factories from "./factories";
9
+ export { Erc20__factory } from "./factories/Erc20__factory";
10
+ export { GnosisSafe__factory } from "./factories/GnosisSafe__factory";
11
+ export { InteropBridgeToken__factory } from "./factories/InteropBridgeToken__factory";
12
+ export { InteropXGateway__factory } from "./factories/InteropXGateway__factory";
package/src/types.ts ADDED
@@ -0,0 +1,39 @@
1
+ import { EventEmitter } from 'events'
2
+ import { IPeerInfo } from '@/net'
3
+
4
+ /**
5
+ * Types for the central event bus, emitted
6
+ * by different components of the client.
7
+ */
8
+ export enum Event {
9
+ PEER_CONNECTED = 'peer:connected',
10
+ PEER_DISCONNECTED = 'peer:disconnected',
11
+ PEER_ERROR = 'peer:error',
12
+ POOL_PEER_ADDED = 'pool:peer:added',
13
+ POOL_PEER_REMOVED = 'pool:peer:removed',
14
+ POOL_PEER_BANNED = 'pool:peer:banned',
15
+ }
16
+ export interface EventParams {
17
+ [Event.PEER_CONNECTED]: [connectedPeer: IPeerInfo]
18
+ [Event.PEER_DISCONNECTED]: [disconnectedPeer: IPeerInfo]
19
+ [Event.PEER_ERROR]: [error: Error, peerCausingError: IPeerInfo]
20
+ [Event.POOL_PEER_ADDED]: [addedPeer: IPeerInfo]
21
+ [Event.POOL_PEER_REMOVED]: [removedPeer: IPeerInfo]
22
+ [Event.POOL_PEER_BANNED]: [bannedPeer: IPeerInfo]
23
+ }
24
+
25
+ export declare interface EventBus<T extends Event> {
26
+ emit(event: T, ...args: EventParams[T]): boolean
27
+ on(event: T, listener: (...args: EventParams[T]) => void): this
28
+ }
29
+
30
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
31
+ export class EventBus<T extends Event> extends EventEmitter { }
32
+ export type EventBusType = EventBus<Event.PEER_CONNECTED> &
33
+ EventBus<Event.PEER_DISCONNECTED> &
34
+ EventBus<Event.POOL_PEER_ADDED> &
35
+ EventBus<Event.POOL_PEER_REMOVED> &
36
+ EventBus<Event.POOL_PEER_BANNED>
37
+
38
+
39
+ export type ChainId = 1 | 137 | 43114;
@@ -0,0 +1,307 @@
1
+ /**
2
+ * @module util
3
+ */
4
+ import axios from 'axios'
5
+ import axiosRetry from "axios-retry";
6
+ import { addresses, itokens, tokens } from '@/constants';
7
+ import { ChainId } from '@/types'
8
+ import { ethers } from 'ethers';
9
+ import { encodeMulti, MetaTransaction, OperationType } from 'ethers-multisend';
10
+ import { Transaction } from '@/db';
11
+ import config from '@/config';
12
+ import abi from '@/abi';
13
+ import { InteropBridgeToken, InteropXGateway } from '@/typechain';
14
+
15
+ export const http = axios.create();
16
+
17
+ axiosRetry(http, { retries: 3, retryDelay: axiosRetry.exponentialDelay });
18
+
19
+
20
+ export function short(buffer: Buffer): string {
21
+ return buffer.toString('hex').slice(0, 8) + '...'
22
+ }
23
+
24
+ export const signGnosisSafeTx = async ({
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
+ }, { signer }) => {
37
+ const gnosisSafe = addresses[chainId].gnosisSafe;
38
+
39
+ const domain = {
40
+ verifyingContract: gnosisSafe,
41
+ chainId,
42
+ };
43
+
44
+ const types = {
45
+ SafeTx: [
46
+ { type: 'address', name: 'to' },
47
+ { type: 'uint256', name: 'value' },
48
+ { type: 'bytes', name: 'data' },
49
+ { type: 'uint8', name: 'operation' },
50
+ { type: 'uint256', name: 'safeTxGas' },
51
+ { type: 'uint256', name: 'baseGas' },
52
+ { type: 'uint256', name: 'gasPrice' },
53
+ { type: 'address', name: 'gasToken' },
54
+ { type: 'address', name: 'refundReceiver' },
55
+ { type: 'uint256', name: 'nonce' },
56
+ ],
57
+ };
58
+
59
+ const message = {
60
+ baseGas,
61
+ data,
62
+ gasPrice,
63
+ gasToken,
64
+ nonce: Number(nonce),
65
+ operation,
66
+ refundReceiver,
67
+ safeAddress: gnosisSafe,
68
+ safeTxGas: String(safeTxGas),
69
+ to,
70
+ value,
71
+ };
72
+
73
+ return await signer._signTypedData(domain, types, message)
74
+ };
75
+
76
+ export const getRpcProviderUrl = (chainId: ChainId) => {
77
+ switch (chainId) {
78
+ case 1:
79
+ return 'https://rpc.instadapp.io/mainnet';
80
+ case 137:
81
+ return 'https://rpc.instadapp.io/polygon';
82
+ case 43114:
83
+ return 'https://rpc.instadapp.io/avalanche';
84
+ default:
85
+ throw new Error(`Unknown chainId: ${chainId}`);
86
+ }
87
+ }
88
+
89
+ export interface Signature {
90
+ signer: string;
91
+ hash?: string;
92
+ data: string;
93
+ }
94
+
95
+ export const buildSignatureBytes = (signatures: Signature[]): string => {
96
+ signatures.sort((left, right) =>
97
+ left.signer.toLowerCase().localeCompare(right.signer.toLowerCase())
98
+ );
99
+ let signatureBytes = "0x";
100
+ for (const sig of signatures) {
101
+ signatureBytes += sig.data.slice(2);
102
+ }
103
+ return signatureBytes;
104
+ };
105
+
106
+ /**
107
+ * Call an async function with a maximum time limit (in milliseconds) for the timeout
108
+ * Resolved promise for async function call, or an error if time limit reached
109
+ */
110
+ export const asyncCallWithTimeout = async <T>(asyncPromise: Promise<T>, timeout: number) => {
111
+ let timeoutHandle;
112
+
113
+ const timeoutPromise = new Promise((_resolve, reject) => {
114
+ timeoutHandle = setTimeout(
115
+ () => reject(new Error('Async call timeout limit reached')),
116
+ timeout
117
+ );
118
+ });
119
+
120
+ return Promise.race([asyncPromise, timeoutPromise]).then(result => {
121
+ clearTimeout(timeoutHandle);
122
+ return result;
123
+ }) as Promise<T>
124
+ }
125
+
126
+
127
+ export const generateInteropTransactionHash = (data: { action: string, submitTransactionHash: string, sourceChainId: string | number, targetChainId: string | number }) => {
128
+ return ethers.utils.solidityKeccak256(['string', 'string', 'string', 'string'], [
129
+ String(data.action),
130
+ String(data.submitTransactionHash),
131
+ String(data.sourceChainId),
132
+ String(data.targetChainId),
133
+ ]);
134
+ }
135
+
136
+ export const buildDataForTransaction = async (transaction: Transaction, type?: 'source' | 'target') => {
137
+ type = type || transaction.sourceStatus === 'pending' ? 'source' : 'target';
138
+
139
+ switch (transaction.action) {
140
+ case "deposit":
141
+ return await buildDepositDataForTransaction(transaction, type);
142
+ case "withdraw":
143
+ return await buildWithdrawDataForTransaction(transaction, type);
144
+ default:
145
+ throw new Error(`Unknown action: ${transaction.action}`);
146
+ }
147
+ }
148
+
149
+ export const buildDepositDataForTransaction = async (transaction: Transaction, type: 'source' | 'target') => {
150
+ const transactions: MetaTransaction[] = [];
151
+
152
+ if (transaction.action !== 'deposit') {
153
+ throw new Error(`Invalid action: ${transaction.action}`)
154
+ }
155
+
156
+ if (transaction.action === 'deposit' && transaction.sourceStatus === 'pending') {
157
+ throw Error('Cannot build data for pending deposit transaction');
158
+ }
159
+
160
+ if (!transaction.submitEvent) {
161
+ throw Error('Cannot build data for transaction without submitEvent');
162
+ }
163
+
164
+
165
+ const token = tokens[transaction.sourceChainId].find(token => token.address.toLowerCase() === transaction.submitEvent.token.toLowerCase());
166
+
167
+ if (!token) {
168
+ throw Error('Cannot build data for transaction without token');
169
+ }
170
+
171
+ const itoken = itokens[transaction.targetChainId].find(itoken => itoken.symbol.toLowerCase() === token.symbol.toLowerCase());
172
+
173
+ if (!itoken) {
174
+ throw Error('Cannot build data for transaction without itoken');
175
+ }
176
+
177
+ const targetChainProvider = new ethers.providers.JsonRpcProvider(getRpcProviderUrl(transaction.targetChainId as ChainId));
178
+ const targetWallet = new ethers.Wallet(config.privateKey, targetChainProvider);
179
+ const interopBridgeContract = getContract<InteropBridgeToken>(itoken.address, abi.interopBridgeToken, targetWallet);
180
+
181
+ const { data } = await interopBridgeContract.populateTransaction.mint(
182
+ transaction.submitEvent.user,
183
+ ethers.BigNumber.from(transaction.submitEvent.amount.toString()),
184
+ ethers.BigNumber.from(transaction.submitEvent.sourceChainId.toString()),
185
+ transaction.submitTransactionHash,
186
+ );
187
+
188
+ transactions.push({
189
+ to: itoken.address,
190
+ data: data!,
191
+ value: '0',
192
+ operation: OperationType.Call,
193
+ });
194
+
195
+ return encodeMulti(transactions).data
196
+ }
197
+
198
+ export const buildWithdrawDataForTransaction = async (transaction: Transaction, type: 'source' | 'target') => {
199
+ const transactions: MetaTransaction[] = [];
200
+
201
+ if (transaction.action !== 'withdraw') {
202
+ throw new Error(`Invalid action: ${transaction.action}`)
203
+ }
204
+
205
+ if (transaction.action === 'withdraw' && transaction.sourceStatus === 'pending') {
206
+ throw Error('Cannot build data for pending withdraw transaction');
207
+ }
208
+
209
+ if (!transaction.submitEvent) {
210
+ throw Error('Cannot build data for transaction without submitEvent');
211
+ }
212
+
213
+ const { to, amount, chainId, itoken: itokenAddress } = transaction.submitEvent;
214
+
215
+ const itoken = itokens[transaction.sourceChainId].find(token => token.address.toLowerCase() === itokenAddress.toLowerCase());
216
+
217
+ if (!itoken) {
218
+ throw Error('Cannot build data for transaction without itoken');
219
+ }
220
+
221
+ const token = tokens[chainId].find(t => t.symbol.toLowerCase() === itoken.symbol.toLowerCase());
222
+
223
+ if (!token) {
224
+ throw Error('Cannot build data for transaction without token');
225
+ }
226
+
227
+ const targetChainProvider = new ethers.providers.JsonRpcProvider(getRpcProviderUrl(transaction.targetChainId as ChainId));
228
+ const targetWallet = new ethers.Wallet(config.privateKey, targetChainProvider);
229
+ const gatewayAddress = addresses[chainId].interopXGateway;
230
+ const interopBridgeContract = getContract<InteropXGateway>(gatewayAddress, abi.interopXGateway, targetWallet);
231
+
232
+ const { data } = await interopBridgeContract.populateTransaction.systemWithdraw(
233
+ ethers.BigNumber.from(amount.toString()),
234
+ to,
235
+ token.address,
236
+ ethers.BigNumber.from(transaction.sourceChainId.toString()),
237
+ transaction.submitTransactionHash,
238
+ );
239
+
240
+ transactions.push({
241
+ to: gatewayAddress,
242
+ data: data!,
243
+ value: '0',
244
+ operation: OperationType.Call,
245
+ });
246
+
247
+ return encodeMulti(transactions).data
248
+ }
249
+
250
+
251
+ export function getContract<TContract extends ethers.Contract>(address: string, contractInterface: ethers.ContractInterface | any, signerOrProvider?: ethers.Signer | ethers.providers.Provider) {
252
+ if (!ethers.utils.getAddress(address) || address === ethers.constants.AddressZero) {
253
+ throw Error(`Invalid 'address' parameter '${address}'.`)
254
+ }
255
+
256
+ const contract = new ethers.Contract(
257
+ address,
258
+ contractInterface,
259
+ signerOrProvider
260
+ ) as TContract
261
+
262
+ // Make sure the contract properties is writable
263
+ const desc = Object.getOwnPropertyDescriptor(contract, 'functions');
264
+
265
+ if (!desc || desc.writable !== true) {
266
+ return contract
267
+ }
268
+
269
+ return new Proxy(contract, {
270
+ get(target, prop, receiver) {
271
+ const value = Reflect.get(target, prop, receiver);
272
+
273
+ if (typeof value === 'function' && (contract.functions.hasOwnProperty(prop) || ['queryFilter'].includes(String(prop)))) {
274
+ return async (...args: any[]) => {
275
+ try {
276
+ return await value.bind(contract)(...args);
277
+ } catch (error) {
278
+ throw new Error(`Error calling "${String(prop)}" on "${address}": ${error.reason || error.message}`)
279
+ }
280
+ }
281
+ }
282
+
283
+
284
+ if (typeof value === 'object' && ['populateTransaction', 'estimateGas', 'functions', 'callStatic'].includes(String(prop))) {
285
+ const parentProp = String(prop);
286
+
287
+ return new Proxy(value, {
288
+ get(target, prop, receiver) {
289
+ const value = Reflect.get(target, prop, receiver);
290
+
291
+ if (typeof value === 'function') {
292
+ return async (...args: any[]) => {
293
+ try {
294
+ return await value.bind(contract)(...args);
295
+ } catch (error) {
296
+ throw new Error(`Error calling "${String(prop)}" using "${parentProp}" on "${address}": ${error.reason || error.message}`)
297
+ }
298
+ }
299
+ }
300
+ }
301
+ })
302
+ }
303
+
304
+ return value;
305
+ },
306
+ });
307
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,30 @@
1
+ {
2
+ "compilerOptions": {
3
+ "module": "commonjs",
4
+ "esModuleInterop": true,
5
+ "resolveJsonModule": true,
6
+ "allowSyntheticDefaultImports": true,
7
+ "emitDecoratorMetadata": true,
8
+ "experimentalDecorators": true,
9
+ "target": "ES2017",
10
+ "noImplicitAny": false,
11
+ "noUnusedLocals": false,
12
+ "strictNullChecks": true,
13
+ "moduleResolution": "node",
14
+ "skipLibCheck": true,
15
+ "sourceMap": false,
16
+ "noEmit": false,
17
+ "outDir": "dist",
18
+ "baseUrl": "src",
19
+ "paths": {
20
+ "@/*" : ["./*" ]
21
+ },
22
+ "typeRoots": [
23
+ "./node_modules/@types",
24
+ "./@types"
25
+ ]
26
+ },
27
+ "include": [
28
+ "src/**/*",
29
+ ]
30
+ }