@instadapp/interop-x 0.0.0-dev.0ee2ee3

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 (44) hide show
  1. package/.env.example +1 -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/config/index.js +17 -0
  6. package/dist/constants/addresses.js +13 -0
  7. package/dist/constants/index.js +17 -0
  8. package/dist/db/index.js +17 -0
  9. package/dist/db/models/execution.js +38 -0
  10. package/dist/db/models/index.js +17 -0
  11. package/dist/db/sequelize.js +22 -0
  12. package/dist/index.js +34 -0
  13. package/dist/logger/index.js +138 -0
  14. package/dist/net/index.js +19 -0
  15. package/dist/net/peer/index.js +100 -0
  16. package/dist/net/pool/index.js +107 -0
  17. package/dist/net/protocol/dial/BaseDialProtocol.js +106 -0
  18. package/dist/net/protocol/dial/SignatureDialProtocol.js +48 -0
  19. package/dist/net/protocol/index.js +92 -0
  20. package/dist/tasks/BaseTask.js +61 -0
  21. package/dist/tasks/index.js +19 -0
  22. package/dist/types.js +21 -0
  23. package/dist/utils/index.js +89 -0
  24. package/package.json +59 -0
  25. package/src/config/index.ts +24 -0
  26. package/src/constants/addresses.ts +10 -0
  27. package/src/constants/index.ts +1 -0
  28. package/src/db/index.ts +1 -0
  29. package/src/db/models/execution.ts +57 -0
  30. package/src/db/models/index.ts +1 -0
  31. package/src/db/sequelize.ts +21 -0
  32. package/src/index.ts +39 -0
  33. package/src/logger/index.ts +157 -0
  34. package/src/net/index.ts +3 -0
  35. package/src/net/peer/index.ts +113 -0
  36. package/src/net/pool/index.ts +128 -0
  37. package/src/net/protocol/dial/BaseDialProtocol.ts +104 -0
  38. package/src/net/protocol/dial/SignatureDialProtocol.ts +62 -0
  39. package/src/net/protocol/index.ts +138 -0
  40. package/src/tasks/BaseTask.ts +76 -0
  41. package/src/tasks/index.ts +18 -0
  42. package/src/types.ts +39 -0
  43. package/src/utils/index.ts +116 -0
  44. package/tsconfig.json +27 -0
@@ -0,0 +1,76 @@
1
+ import config from "config";
2
+ import { ethers } from "ethers";
3
+ import EventEmitter from "events";
4
+ import wait from "waait";
5
+ import Logger from "../logger";
6
+
7
+ export interface IBaseTask {
8
+ pollCheck(): Promise<void>
9
+ prePollHandler(): boolean
10
+ pollHandler(): Promise<void>
11
+ postPollHandler(): Promise<void>
12
+
13
+ start(): Promise<void>
14
+ stop(): Promise<void>
15
+ }
16
+
17
+
18
+ export class BaseTask extends EventEmitter implements IBaseTask {
19
+ logger: Logger
20
+ started: boolean = false
21
+ pollIntervalMs: number = 10 * 1000
22
+ leadNodeOnly: boolean = false
23
+
24
+ public constructor({ logger }: { logger?: Logger }) {
25
+ super()
26
+
27
+ this.logger = logger ?? new Logger('BaseTask')
28
+ }
29
+
30
+ async pollCheck() {
31
+ while (true) {
32
+ if (!this.started) {
33
+ return
34
+ }
35
+ try {
36
+ const shouldPoll = this.prePollHandler()
37
+ if (shouldPoll) {
38
+ await this.pollHandler()
39
+ }
40
+ } catch (err) {
41
+ this.logger.error(`poll check error: ${err.message}\ntrace: ${err.stack}`)
42
+ }
43
+
44
+ await this.postPollHandler()
45
+ }
46
+ }
47
+
48
+ prePollHandler(): boolean {
49
+ if (!this.leadNodeOnly) {
50
+ return true
51
+ }
52
+
53
+ return config.isLeadNode()
54
+ }
55
+
56
+ async pollHandler() {
57
+ this.logger.warn('pollHandler not implemented')
58
+ }
59
+
60
+ async postPollHandler() {
61
+ await wait(this.pollIntervalMs)
62
+ }
63
+
64
+ async start() {
65
+ this.started = true
66
+ try {
67
+ await this.pollCheck()
68
+ } catch (err) {
69
+ this.logger.error(`base task error: ${err.message}\ntrace: ${err.stack}`)
70
+ }
71
+ }
72
+
73
+ async stop() {
74
+ this.started = false
75
+ }
76
+ }
@@ -0,0 +1,18 @@
1
+ import { BaseTask } from "./BaseTask";
2
+
3
+ export class Tasks {
4
+
5
+ tasks: BaseTask[] = [
6
+
7
+ ];
8
+
9
+ async start() {
10
+ for (const task of this.tasks) {
11
+ try {
12
+ task.start();
13
+ } catch (error) {
14
+ console.error(`Error starting task: ${task.constructor.name}`);
15
+ }
16
+ }
17
+ }
18
+ }
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;
@@ -0,0 +1,116 @@
1
+ /**
2
+ * @module util
3
+ */
4
+ import axios from 'axios'
5
+ import axiosRetry from "axios-retry";
6
+ import { addresses } from '../constants';
7
+ import { ChainId } from '../types'
8
+
9
+ export const http = axios.create();
10
+
11
+ axiosRetry(http, { retries: 3, retryDelay: axiosRetry.exponentialDelay });
12
+
13
+
14
+ export function short(buffer: Buffer): string {
15
+ return buffer.toString('hex').slice(0, 8) + '...'
16
+ }
17
+
18
+ export const signGnosisSafeTx = async ({
19
+ to,
20
+ data = null as any,
21
+ value = '0',
22
+ operation = '1',
23
+ baseGas = '0',
24
+ gasPrice = "0",
25
+ gasToken = "0x0000000000000000000000000000000000000000",
26
+ refundReceiver = "0x0000000000000000000000000000000000000000",
27
+ safeTxGas = "79668" as string | number,
28
+ nonce = "0",
29
+ chainId = 137 as ChainId,
30
+ }, { signer }) => {
31
+ const gnosisSafe = addresses[chainId].gnosisSafe;
32
+
33
+ const domain = {
34
+ verifyingContract: gnosisSafe,
35
+ chainId,
36
+ };
37
+
38
+ const types = {
39
+ SafeTx: [
40
+ { type: 'address', name: 'to' },
41
+ { type: 'uint256', name: 'value' },
42
+ { type: 'bytes', name: 'data' },
43
+ { type: 'uint8', name: 'operation' },
44
+ { type: 'uint256', name: 'safeTxGas' },
45
+ { type: 'uint256', name: 'baseGas' },
46
+ { type: 'uint256', name: 'gasPrice' },
47
+ { type: 'address', name: 'gasToken' },
48
+ { type: 'address', name: 'refundReceiver' },
49
+ { type: 'uint256', name: 'nonce' },
50
+ ],
51
+ };
52
+
53
+ const message = {
54
+ baseGas,
55
+ data,
56
+ gasPrice,
57
+ gasToken,
58
+ nonce: Number(nonce),
59
+ operation,
60
+ refundReceiver,
61
+ safeAddress: gnosisSafe,
62
+ safeTxGas: String(safeTxGas),
63
+ to,
64
+ value,
65
+ };
66
+
67
+ return await signer._signTypedData(domain, types, message)
68
+ };
69
+
70
+ export const getRpcProviderUrl = (chainId: ChainId) => {
71
+ switch (chainId) {
72
+ case 1:
73
+ return 'https://rpc.instadapp.io/mainnet';
74
+ case 137:
75
+ return 'https://rpc.instadapp.io/polygon';
76
+ default:
77
+ throw new Error(`Unknown chainId: ${chainId}`);
78
+ }
79
+ }
80
+
81
+ export interface Signature {
82
+ signer: string;
83
+ hash?: string;
84
+ data: string;
85
+ }
86
+
87
+ export const buildSignatureBytes = (signatures: Signature[]): string => {
88
+ signatures.sort((left, right) =>
89
+ left.signer.toLowerCase().localeCompare(right.signer.toLowerCase())
90
+ );
91
+ let signatureBytes = "0x";
92
+ for (const sig of signatures) {
93
+ signatureBytes += sig.data.slice(2);
94
+ }
95
+ return signatureBytes;
96
+ };
97
+
98
+ /**
99
+ * Call an async function with a maximum time limit (in milliseconds) for the timeout
100
+ * Resolved promise for async function call, or an error if time limit reached
101
+ */
102
+ export const asyncCallWithTimeout = async <T>(asyncPromise: Promise<T>, timeout: number) => {
103
+ let timeoutHandle;
104
+
105
+ const timeoutPromise = new Promise((_resolve, reject) => {
106
+ timeoutHandle = setTimeout(
107
+ () => reject(new Error('Async call timeout limit reached')),
108
+ timeout
109
+ );
110
+ });
111
+
112
+ return Promise.race([asyncPromise, timeoutPromise]).then(result => {
113
+ clearTimeout(timeoutHandle);
114
+ return result;
115
+ }) as Promise<T>
116
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,27 @@
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
+ "typeRoots": [
20
+ "./node_modules/@types",
21
+ "./@types"
22
+ ]
23
+ },
24
+ "include": [
25
+ "src/**/*",
26
+ ]
27
+ }