@instadapp/interop-x 0.0.0-dev.9387bd9 → 0.0.0-dev.fd6776c

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 (73) hide show
  1. package/.env.example +2 -1
  2. package/dist/abi/erc20.json +350 -0
  3. package/dist/abi/gnosisSafe.json +747 -0
  4. package/dist/abi/index.js +15 -0
  5. package/dist/abi/interopBridgeToken.json +286 -0
  6. package/dist/abi/interopXGateway.json +184 -0
  7. package/dist/config/index.js +5 -1
  8. package/dist/constants/addresses.js +17 -2
  9. package/dist/constants/index.js +1 -0
  10. package/dist/constants/tokens.js +107 -0
  11. package/dist/db/models/index.js +1 -1
  12. package/dist/db/models/transaction.js +52 -0
  13. package/dist/db/sequelize.js +2 -1
  14. package/dist/index.js +24 -3
  15. package/dist/net/peer/index.js +5 -5
  16. package/dist/net/pool/index.js +2 -2
  17. package/dist/net/protocol/dial/BaseDialProtocol.js +1 -1
  18. package/dist/net/protocol/dial/SignatureDialProtocol.js +11 -13
  19. package/dist/net/protocol/index.js +3 -3
  20. package/dist/tasks/BaseTask.js +2 -2
  21. package/dist/tasks/InteropXGateway/SyncDepositEvents.js +79 -0
  22. package/dist/tasks/index.js +9 -1
  23. package/dist/typechain/Erc20.js +2 -0
  24. package/dist/typechain/GnosisSafe.js +2 -0
  25. package/dist/typechain/InteropBridgeToken.js +2 -0
  26. package/dist/typechain/InteropXGateway.js +2 -0
  27. package/dist/typechain/common.js +2 -0
  28. package/dist/typechain/factories/Erc20__factory.js +367 -0
  29. package/dist/typechain/factories/GnosisSafe__factory.js +1174 -0
  30. package/dist/typechain/factories/InteropBridgeToken__factory.js +459 -0
  31. package/dist/typechain/factories/InteropXGateway__factory.js +265 -0
  32. package/dist/typechain/factories/index.js +14 -0
  33. package/dist/typechain/index.js +35 -0
  34. package/dist/utils/index.js +14 -2
  35. package/package.json +10 -3
  36. package/src/abi/erc20.json +350 -0
  37. package/src/abi/gnosisSafe.json +747 -0
  38. package/src/abi/index.ts +11 -0
  39. package/src/abi/interopBridgeToken.json +286 -0
  40. package/src/abi/interopXGateway.json +184 -0
  41. package/src/config/index.ts +7 -1
  42. package/src/constants/addresses.ts +17 -2
  43. package/src/constants/index.ts +1 -0
  44. package/src/constants/tokens.ts +104 -0
  45. package/src/db/index.ts +1 -1
  46. package/src/db/models/index.ts +1 -1
  47. package/src/db/models/transaction.ts +92 -0
  48. package/src/db/sequelize.ts +2 -1
  49. package/src/index.ts +29 -4
  50. package/src/net/peer/index.ts +3 -3
  51. package/src/net/pool/index.ts +2 -2
  52. package/src/net/protocol/dial/BaseDialProtocol.ts +1 -1
  53. package/src/net/protocol/dial/SignatureDialProtocol.ts +13 -16
  54. package/src/net/protocol/index.ts +3 -3
  55. package/src/tasks/BaseTask.ts +2 -3
  56. package/src/tasks/InteropXGateway/SyncDepositEvents.ts +114 -0
  57. package/src/tasks/index.ts +4 -1
  58. package/src/typechain/Erc20.ts +491 -0
  59. package/src/typechain/GnosisSafe.ts +1728 -0
  60. package/src/typechain/InteropBridgeToken.ts +686 -0
  61. package/src/typechain/InteropXGateway.ts +407 -0
  62. package/src/typechain/common.ts +44 -0
  63. package/src/typechain/factories/Erc20__factory.ts +368 -0
  64. package/src/typechain/factories/GnosisSafe__factory.ts +1178 -0
  65. package/src/typechain/factories/InteropBridgeToken__factory.ts +466 -0
  66. package/src/typechain/factories/InteropXGateway__factory.ts +272 -0
  67. package/src/typechain/factories/index.ts +7 -0
  68. package/src/typechain/index.ts +12 -0
  69. package/src/types.ts +2 -2
  70. package/src/utils/index.ts +15 -2
  71. package/tsconfig.json +3 -0
  72. package/dist/db/models/execution.js +0 -38
  73. package/src/db/models/execution.ts +0 -57
@@ -0,0 +1,92 @@
1
+ import { sequelize } from '@/db/sequelize'
2
+ import { CreationOptional, InferAttributes, InferCreationAttributes, Model, DataTypes } from 'sequelize';
3
+
4
+ export class Transaction extends Model<InferAttributes<Transaction>, InferCreationAttributes<Transaction>> {
5
+ declare id: CreationOptional<number>;
6
+
7
+ declare transactionHash: string;
8
+ declare type: string;
9
+ declare from: string;
10
+ declare to: string;
11
+
12
+
13
+ declare sourceChainId: number;
14
+ declare sourceTransactionHash: string;
15
+ declare sourceBlockNumber: number;
16
+ declare sourceStatus: string;
17
+ declare sourceErrors: CreationOptional<string[]>;
18
+ declare sourceCreatedAt: CreationOptional<Date>;
19
+ declare sourceDelayUntil: CreationOptional<Date>;
20
+
21
+ declare targetChainId: number;
22
+ declare targetTransactionHash: CreationOptional<string>;
23
+ declare targetBlockNumber: CreationOptional<number>;
24
+ declare targetStatus: string;
25
+ declare targetErrors: CreationOptional<string[]>;
26
+ declare targetCreatedAt: CreationOptional<Date>;
27
+ declare targetDelayUntil: CreationOptional<Date>;
28
+
29
+ declare submitEvent: any;
30
+ declare sourceEvent: CreationOptional<any>;
31
+ declare targetEvent: CreationOptional<any>;
32
+
33
+ declare metadata: CreationOptional<any>;
34
+
35
+ declare status: string;
36
+
37
+ declare createdAt: CreationOptional<Date>;
38
+ declare updatedAt: CreationOptional<Date>;
39
+ }
40
+
41
+ Transaction.init({
42
+ id: {
43
+ type: DataTypes.INTEGER,
44
+ autoIncrement: true,
45
+ primaryKey: true
46
+ },
47
+
48
+
49
+ transactionHash: DataTypes.STRING,
50
+ type: DataTypes.STRING,
51
+
52
+ from: DataTypes.STRING,
53
+ to: DataTypes.STRING,
54
+
55
+ sourceChainId: DataTypes.NUMBER,
56
+ sourceTransactionHash: DataTypes.STRING,
57
+ sourceBlockNumber: DataTypes.NUMBER,
58
+ sourceStatus: DataTypes.STRING,
59
+ sourceErrors: {
60
+ type: DataTypes.JSON,
61
+ // defaultValue: [],
62
+ },
63
+ sourceCreatedAt: {
64
+ type: DataTypes.DATE,
65
+ defaultValue: Date.now()
66
+ },
67
+ sourceDelayUntil: DataTypes.STRING,
68
+
69
+ targetChainId: DataTypes.NUMBER,
70
+ targetTransactionHash: DataTypes.STRING,
71
+ targetBlockNumber: DataTypes.NUMBER,
72
+ targetStatus: DataTypes.STRING,
73
+ targetErrors: {
74
+ type: DataTypes.JSON,
75
+ // defaultValue: [],
76
+ },
77
+ targetCreatedAt: DataTypes.DATE,
78
+ targetDelayUntil: DataTypes.DATE,
79
+
80
+ submitEvent: DataTypes.JSON,
81
+ sourceEvent: DataTypes.JSON,
82
+ targetEvent: DataTypes.JSON,
83
+
84
+ metadata: DataTypes.JSON,
85
+
86
+ status: {
87
+ type: DataTypes.STRING,
88
+ defaultValue: 'pending'
89
+ },
90
+ createdAt: DataTypes.DATE,
91
+ updatedAt: DataTypes.DATE,
92
+ }, { sequelize, tableName: 'transactions' });
@@ -1,9 +1,10 @@
1
1
  //@ts-ignore
2
+ import config from "@/config";
2
3
  import expandHomeDir from "expand-home-dir";
3
4
 
4
5
  import { Sequelize } from 'sequelize';
5
6
 
6
- const basePath = expandHomeDir('~/.interop-x/data');
7
+ const basePath = expandHomeDir(`~/.interop-x/data/${config.publicAddress}/${config.staging ? 'staging' : ''}`);
7
8
 
8
9
  export const sequelize = new Sequelize({
9
10
  dialect: 'sqlite',
package/src/index.ts CHANGED
@@ -1,10 +1,36 @@
1
+ import moduleAlias from 'module-alias';
2
+
3
+ moduleAlias.addAliases({
4
+ "@/": __dirname + "/",
5
+ "@/logger": __dirname + "/logger",
6
+ "@/tasks": __dirname + "/tasks",
7
+ "@/utils": __dirname + "/utils",
8
+ "@/net": __dirname + "/net",
9
+ "@/db": __dirname + "/db",
10
+ "@/config": __dirname + "/config",
11
+ "@/types": __dirname + "/types",
12
+ "@/abi": __dirname + "/abi",
13
+ "@/constants": __dirname + "/constants",
14
+ "@/typechain": __dirname + "/typechain"
15
+ })
16
+
17
+ moduleAlias();
1
18
  import assert from "assert";
2
19
  import dotenv from "dotenv";
3
- import Logger from "./logger";
4
20
  import { ethers } from "ethers";
5
21
  dotenv.config();
22
+
23
+ import Logger from "@/logger";
6
24
  const logger = new Logger('Process')
7
25
 
26
+
27
+ if (process.argv.at(-1) === 'help') {
28
+ console.log('Usage:')
29
+ console.log(' PRIVATE_KEY=abcd1234 interop-x')
30
+ console.log(' PRIVATE_KEY=abcd1234 STAGING=true interop-x')
31
+ process.exit(0)
32
+ }
33
+
8
34
  assert(process.env.PRIVATE_KEY, "PRIVATE_KEY is not defined");
9
35
 
10
36
  try {
@@ -14,9 +40,8 @@ try {
14
40
  process.exit(1)
15
41
  }
16
42
 
17
- import { Tasks } from "./tasks";
18
- import { startPeer } from "./net";
19
-
43
+ import { Tasks } from "@/tasks";
44
+ import { startPeer } from "@/net";
20
45
 
21
46
  async function main() {
22
47
 
@@ -5,7 +5,7 @@ import WS from "libp2p-websockets";
5
5
  //@ts-ignore
6
6
  import Mplex from "libp2p-mplex";
7
7
  import { NOISE } from "libp2p-noise";
8
- import Logger from "../../logger";
8
+ import Logger from "@/logger";
9
9
  import Bootstrap from "libp2p-bootstrap";
10
10
  import wait from "waait";
11
11
  import Gossipsub from "@achingbrain/libp2p-gossipsub";
@@ -15,8 +15,8 @@ import MulticastDNS from "libp2p-mdns";
15
15
  import KadDHT from "libp2p-kad-dht";
16
16
  //@ts-ignore
17
17
  import PubsubPeerDiscovery from "libp2p-pubsub-peer-discovery";
18
- import { protocol } from "../protocol";
19
- import config from "../../config";
18
+ import { protocol } from "@/net";
19
+ import config from "@/config";
20
20
 
21
21
  const logger = new Logger("Peer");
22
22
 
@@ -1,5 +1,5 @@
1
- import { Event } from "../../types";
2
- import config from "../../config";
1
+ import { Event } from "@/types";
2
+ import config from "@/config";
3
3
 
4
4
  export interface IPeerInfo {
5
5
  id: string;
@@ -1,7 +1,7 @@
1
1
  import pipe from 'it-pipe';
2
2
  import Libp2p from 'libp2p';
3
3
  import PeerId from 'peer-id';
4
- import { asyncCallWithTimeout } from '../../../utils';
4
+ import { asyncCallWithTimeout } from '@/utils';
5
5
  import wait from 'waait';
6
6
 
7
7
  export class BaseDialProtocol<TRequest extends any, TResponse extends any> {
@@ -1,14 +1,11 @@
1
- import { signGnosisSafeTx } from "../../../utils";
2
1
  import { BaseDialProtocol } from "./BaseDialProtocol";
3
2
  import wait from "waait";
4
- import { ChainId } from "../../../types";
5
- import config from "../../../config";
6
- import { addresses } from "../../../constants";
7
- import { Execution } from "db";
3
+ import config from "@/config";
4
+ import { Transaction } from "@/db";
8
5
 
9
6
  export interface ISignatureRequest {
10
7
  type: string,
11
- vnonce: string
8
+ transactionHash: string
12
9
  safeTxGas: string
13
10
  safeNonce: string
14
11
  }
@@ -21,17 +18,17 @@ export class SignatureDialProtocol extends BaseDialProtocol<ISignatureRequest, I
21
18
  protected timeout = 30000;
22
19
 
23
20
  constructor(libp2p) {
24
- super(libp2p, '/signatures')
21
+ super(libp2p, '/interop-x/signatures')
25
22
  }
26
23
 
27
24
  async response(data: ISignatureRequest): Promise<ISignatureResponse> {
28
25
  const signer = config.wallet;
29
26
 
30
- let event: Execution | null;
27
+ let event: Transaction | null;
31
28
  let maxTimeout = 20000;
32
29
 
33
30
  do {
34
- event = await Execution.findOne({ where: { vnonce: data.vnonce.toString() } })
31
+ event = await Transaction.findOne({ where: { transactionHash: data.transactionHash } })
35
32
 
36
33
  if (!event) {
37
34
  await wait(1000);
@@ -47,16 +44,16 @@ export class SignatureDialProtocol extends BaseDialProtocol<ISignatureRequest, I
47
44
  };
48
45
  }
49
46
 
50
- const signedData = await signGnosisSafeTx({
51
- to: addresses[event.chainId as ChainId].multisend,
52
- data: 'TODO',
53
- chainId: event.chainId as ChainId,
54
- safeTxGas: data.safeTxGas,
55
- }, { signer });
47
+ // const signedData = await signGnosisSafeTx({
48
+ // to: addresses[event.chainId as ChainId].multisend,
49
+ // data: 'TODO',
50
+ // chainId: event.chainId as ChainId,
51
+ // safeTxGas: data.safeTxGas,
52
+ // }, { signer });
56
53
 
57
54
  return {
58
55
  signer: signer.address,
59
- data: signedData,
56
+ data: null, //signedData,
60
57
  }
61
58
  }
62
59
  }
@@ -3,8 +3,8 @@ import Libp2p from 'libp2p';
3
3
  import { bufferToInt, rlp } from 'ethereumjs-util'
4
4
  import { SignatureDialProtocol, ISignatureRequest, ISignatureResponse } from "./dial/SignatureDialProtocol";
5
5
  import { IPeerInfo, peerPool } from "..";
6
- import config from "config";
7
- import { Event } from "types";
6
+ import config from "@/config";
7
+ import { Event } from "@/types";
8
8
 
9
9
  export interface ProtocolOptions {
10
10
  /* Handshake timeout in ms (default: 8000) */
@@ -58,7 +58,7 @@ class Protocol extends EventEmitter {
58
58
 
59
59
  start({ libp2p, topic = null, }) {
60
60
  this.libp2p = libp2p
61
- this.topic = topic || 'protocol'
61
+ this.topic = topic || 'itnerop-x-protocol'
62
62
 
63
63
  if (this.libp2p.isStarted()) this.init()
64
64
 
@@ -1,8 +1,7 @@
1
- import config from "config";
2
- import { ethers } from "ethers";
1
+ import config from "@/config";
3
2
  import EventEmitter from "events";
4
3
  import wait from "waait";
5
- import Logger from "../logger";
4
+ import Logger from "@/logger";
6
5
 
7
6
  export interface IBaseTask {
8
7
  pollCheck(): Promise<void>
@@ -0,0 +1,114 @@
1
+ import { BaseTask } from "../BaseTask";
2
+ import Logger from '@/logger';
3
+ import { ethers } from "ethers";
4
+ import abi from "@/abi";
5
+ import { Transaction } from "@/db";
6
+ import { generateInteropTransactionHash, getRpcProviderUrl } from "@/utils";
7
+ import { addresses } from "@/constants";
8
+ import { ChainId } from "@/types";
9
+ import config from "@/config";
10
+ import { InteropXGateway } from "@/typechain";
11
+
12
+ class SyncDepositEvents extends BaseTask {
13
+ contractAddress: string;
14
+ provider: ethers.providers.JsonRpcProvider;
15
+ contract: InteropXGateway;
16
+ chainId: ChainId;
17
+
18
+ constructor({ chainId }: { chainId: ChainId }) {
19
+ super({
20
+ logger: new Logger("InteropXGateway::SyncDepositEvents"),
21
+ })
22
+ this.chainId = chainId;
23
+ }
24
+
25
+ async pollHandler() {
26
+ const currentBlock = await this.provider.getBlockNumber();
27
+
28
+ const events = await this.contract.queryFilter(
29
+ this.contract.filters.LogGatewayDeposit(),
30
+ currentBlock - 2000,
31
+ currentBlock,
32
+ );
33
+
34
+ let processedEvents = 0;
35
+
36
+ for (const event of events) {
37
+
38
+ try {
39
+ if (!event.args) {
40
+ continue;
41
+ }
42
+
43
+ const { sourceChainId, targetChainId, user, vnonce, amount, token } = event.args;
44
+
45
+ const uniqueIdentifier = {
46
+ type: 'desposit',
47
+ sourceTransactionHash: event.transactionHash,
48
+ sourceChainId: sourceChainId.toNumber(),
49
+ targetChainId: targetChainId.toNumber(),
50
+ }
51
+
52
+ if (await Transaction.findOne({ where: uniqueIdentifier })) {
53
+ continue;
54
+ }
55
+
56
+ const tx = await event.getTransaction()
57
+
58
+ await Transaction.create({
59
+ transactionHash: generateInteropTransactionHash(uniqueIdentifier),
60
+ type: 'deposit',
61
+ from: tx.from,
62
+ to: user,
63
+
64
+ sourceChainId: sourceChainId.toNumber(),
65
+ sourceTransactionHash: event.transactionHash,
66
+ sourceBlockNumber: event.blockNumber,
67
+ sourceStatus: "uninitialised",
68
+
69
+ targetChainId: targetChainId.toNumber(),
70
+ targetStatus: "uninitialised",
71
+
72
+ submitEvent: {
73
+ user,
74
+ sourceChainId: sourceChainId.toString(),
75
+ targetChainId: targetChainId.toString(),
76
+ token: token,
77
+ ammout: amount.toString(),
78
+ vnonce: vnonce.toString(),
79
+ },
80
+ status: "pending",
81
+ })
82
+
83
+ this.logger.info(
84
+ `Execution queued: ${event.transactionHash} ${event.blockNumber}`
85
+ );
86
+ } catch (error) {
87
+ this.logger.error(error);
88
+ }
89
+ }
90
+
91
+ if (processedEvents > 0)
92
+ this.logger.info(`${processedEvents} events processed`);
93
+ }
94
+
95
+ async start(): Promise<void> {
96
+ this.logger.info(`Starting execution watcher on interop chain`);
97
+
98
+ this.contractAddress = addresses[this.chainId].interopXGateway;
99
+
100
+ this.provider = new ethers.providers.JsonRpcProvider(
101
+ getRpcProviderUrl(this.chainId)
102
+ );
103
+
104
+ this.contract = new ethers.Contract(
105
+ this.contractAddress,
106
+ abi.interopXGateway,
107
+ new ethers.Wallet(config.privateKey!, this.provider)
108
+ ) as InteropXGateway;
109
+
110
+ await super.start()
111
+ }
112
+ }
113
+
114
+ export default SyncDepositEvents;
@@ -1,9 +1,12 @@
1
1
  import { BaseTask } from "./BaseTask";
2
+ import SyncInteropXGatewayDepositEvents from "./InteropXGateway/SyncDepositEvents";
2
3
 
3
4
  export class Tasks {
4
5
 
5
6
  tasks: BaseTask[] = [
6
-
7
+ new SyncInteropXGatewayDepositEvents({
8
+ chainId: 43114
9
+ })
7
10
  ];
8
11
 
9
12
  async start() {