@instadapp/interop-x 0.0.0-dev.285d847 → 0.0.0-dev.28f049d

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 (78) hide show
  1. package/dist/package.json +5 -4
  2. package/dist/src/abi/index.js +2 -4
  3. package/dist/src/abi/interopXContract.json +391 -0
  4. package/dist/src/alias.js +10 -0
  5. package/dist/src/api/index.js +6 -3
  6. package/dist/src/config/index.js +10 -1
  7. package/dist/src/constants/addresses.js +3 -3
  8. package/dist/src/constants/index.js +0 -1
  9. package/dist/src/db/models/transaction.js +27 -9
  10. package/dist/src/gnosis/actions/index.js +9 -0
  11. package/dist/src/gnosis/actions/withdraw/index.js +41 -0
  12. package/dist/src/gnosis/index.js +20 -0
  13. package/dist/src/index.js +38 -23
  14. package/dist/src/net/peer/index.js +2 -1
  15. package/dist/src/net/pool/index.js +7 -2
  16. package/dist/src/net/protocol/dial/SignatureDialProtocol.js +3 -8
  17. package/dist/src/net/protocol/dial/TransactionStatusDialProtocol.js +30 -0
  18. package/dist/src/net/protocol/index.js +26 -5
  19. package/dist/src/tasks/AutoUpdateTask.js +42 -16
  20. package/dist/src/tasks/BaseTask.js +11 -3
  21. package/dist/src/tasks/{InteropXGateway/ProcessDepositEvents.js → InteropXContract/ProcessBridgeRequestEvents.js} +42 -46
  22. package/dist/src/tasks/InteropXContract/SyncBridgeRequestEvents.js +78 -0
  23. package/dist/src/tasks/Transactions/SyncTransactionStatusTask.js +55 -0
  24. package/dist/src/tasks/index.js +8 -17
  25. package/dist/src/typechain/{InteropBridgeToken.js → InteropXContract.js} +0 -0
  26. package/dist/src/typechain/factories/InteropXContract__factory.js +526 -0
  27. package/dist/src/typechain/factories/index.js +3 -5
  28. package/dist/src/typechain/index.js +3 -5
  29. package/dist/src/utils/index.js +27 -87
  30. package/package.json +5 -4
  31. package/src/abi/index.ts +2 -4
  32. package/src/abi/interopXContract.json +391 -0
  33. package/src/alias.ts +6 -0
  34. package/src/api/index.ts +5 -2
  35. package/src/config/index.ts +9 -1
  36. package/src/constants/addresses.ts +3 -3
  37. package/src/constants/index.ts +0 -1
  38. package/src/db/models/transaction.ts +59 -21
  39. package/src/gnosis/actions/index.ts +5 -0
  40. package/src/gnosis/actions/withdraw/index.ts +56 -0
  41. package/src/gnosis/index.ts +19 -0
  42. package/src/index.ts +52 -25
  43. package/src/net/peer/index.ts +2 -1
  44. package/src/net/pool/index.ts +7 -3
  45. package/src/net/protocol/dial/SignatureDialProtocol.ts +5 -11
  46. package/src/net/protocol/dial/TransactionStatusDialProtocol.ts +33 -0
  47. package/src/net/protocol/index.ts +28 -6
  48. package/src/tasks/AutoUpdateTask.ts +48 -20
  49. package/src/tasks/BaseTask.ts +13 -3
  50. package/src/tasks/{InteropBridge/ProcessWithdrawEvents.ts → InteropXContract/ProcessBridgeRequestEvents.ts} +66 -91
  51. package/src/tasks/InteropXContract/SyncBridgeRequestEvents.ts +116 -0
  52. package/src/tasks/Transactions/SyncTransactionStatusTask.ts +67 -0
  53. package/src/tasks/index.ts +10 -18
  54. package/src/typechain/InteropXContract.ts +524 -0
  55. package/src/typechain/factories/InteropXContract__factory.ts +533 -0
  56. package/src/typechain/factories/index.ts +1 -2
  57. package/src/typechain/index.ts +2 -4
  58. package/src/utils/index.ts +61 -128
  59. package/tsconfig.json +7 -2
  60. package/dist/src/abi/interopBridgeToken.json +0 -286
  61. package/dist/src/abi/interopXGateway.json +0 -184
  62. package/dist/src/constants/itokens.js +0 -13
  63. package/dist/src/tasks/InteropBridge/ProcessWithdrawEvents.js +0 -147
  64. package/dist/src/tasks/InteropBridge/SyncWithdrawEvents.js +0 -70
  65. package/dist/src/tasks/InteropXGateway/SyncDepositEvents.js +0 -75
  66. package/dist/src/typechain/InteropXGateway.js +0 -2
  67. package/dist/src/typechain/factories/InteropBridgeToken__factory.js +0 -459
  68. package/dist/src/typechain/factories/InteropXGateway__factory.js +0 -265
  69. package/src/abi/interopBridgeToken.json +0 -286
  70. package/src/abi/interopXGateway.json +0 -184
  71. package/src/constants/itokens.ts +0 -10
  72. package/src/tasks/InteropBridge/SyncWithdrawEvents.ts +0 -121
  73. package/src/tasks/InteropXGateway/ProcessDepositEvents.ts +0 -245
  74. package/src/tasks/InteropXGateway/SyncDepositEvents.ts +0 -126
  75. package/src/typechain/InteropBridgeToken.ts +0 -686
  76. package/src/typechain/InteropXGateway.ts +0 -407
  77. package/src/typechain/factories/InteropBridgeToken__factory.ts +0 -466
  78. package/src/typechain/factories/InteropXGateway__factory.ts +0 -272
@@ -1,5 +1,7 @@
1
1
  import { ethers, Wallet } from "ethers"
2
2
  import { EventBus, EventBusType } from "@/types"
3
+ import fs from 'fs-extra'
4
+ import expandHomeDir from "expand-home-dir";
3
5
 
4
6
  class Config {
5
7
  public readonly events: EventBusType
@@ -9,15 +11,17 @@ class Config {
9
11
  public readonly wallet: Wallet
10
12
  public readonly staging: boolean
11
13
  public readonly autoUpdate: boolean
14
+ public readonly baseConfigPath: string
12
15
 
13
16
  constructor() {
14
17
  this.events = new EventBus() as EventBusType
15
- this.maxPeers = 10
18
+ this.maxPeers = 20
16
19
  this.privateKey = process.env.PRIVATE_KEY as string;
17
20
  this.staging = !! process.env.STAGING && process.env.STAGING === 'true';
18
21
  this.autoUpdate = !! process.env.AUTO_UPDATE && process.env.AUTO_UPDATE === 'true';
19
22
  this.wallet = new Wallet(this.privateKey);
20
23
  this.leadNodeAddress = '0x910E413DBF3F6276Fe8213fF656726bDc142E08E'
24
+ this.baseConfigPath = expandHomeDir(`~/.interop-x`);
21
25
  }
22
26
 
23
27
  get publicAddress(){
@@ -27,6 +31,10 @@ class Config {
27
31
  isLeadNode() {
28
32
  return ethers.utils.getAddress(this.leadNodeAddress) === ethers.utils.getAddress(this.wallet.address)
29
33
  }
34
+
35
+ isMaintenanceMode(){
36
+ return fs.existsSync(this.baseConfigPath + '/maintenance')
37
+ }
30
38
  }
31
39
 
32
40
  export default new Config()
@@ -2,16 +2,16 @@ export const addresses = {
2
2
  1: {
3
3
  gnosisSafe: '0x811Bff6eF88dAAA0aD6438386B534A81cE3F160F',
4
4
  multisend: '0xA238CBeb142c10Ef7Ad8442C6D1f9E89e07e7761',
5
- interopXGateway: '',
5
+ interopXContract: '',
6
6
  },
7
7
  137: {
8
8
  gnosisSafe: '0x5635d2910e51da33d9DC0422c893CF4F28B69A25',
9
9
  multisend: '0xA238CBeb142c10Ef7Ad8442C6D1f9E89e07e7761',
10
- interopXGateway: '',
10
+ interopXContract: '0x2189741c8cAc1cf51570932817A7d6390646C80e',
11
11
  },
12
12
  43114: {
13
13
  gnosisSafe: '0x31d7a5194Fe60AC209Cf1Ce2d539C9A60662Ed6b',
14
14
  multisend: '0x998739BFdAAdde7C933B942a68053933098f9EDa',
15
- interopXGateway: '0x8D27758751BA488690974B6Ccfcda771D462945f',
15
+ interopXContract: '0x1867DF97Ec24bb0bbD4AD464F0Be9C6713422EAE',
16
16
  }
17
17
  }
@@ -1,3 +1,2 @@
1
1
  export * from './addresses';
2
2
  export * from './tokens';
3
- export * from './itokens';
@@ -1,40 +1,61 @@
1
1
  import { sequelize } from '@/db/sequelize'
2
2
  import { CreationOptional, InferAttributes, InferCreationAttributes, Model, DataTypes } from 'sequelize';
3
3
 
4
+ export interface IPositionTokenInfo{
5
+ amount: string;
6
+ sourceToken: string;
7
+ targetToken: string;
8
+ }
9
+
10
+ export interface IPosition {
11
+ supply: IPositionTokenInfo[];
12
+ withdraw: IPositionTokenInfo[];
13
+ }
14
+
4
15
  export class Transaction extends Model<InferAttributes<Transaction>, InferCreationAttributes<Transaction>> {
5
16
  declare id: CreationOptional<number>;
6
17
 
7
18
  declare transactionHash: string;
19
+
8
20
  declare action: string;
9
- declare from: string;
10
- declare to: string;
21
+ declare bridger: string;
22
+ declare requestTransactionHash: string;
11
23
 
12
- declare submitTransactionHash: string;
13
- declare submitBlockNumber: number;
24
+ declare requestBlockNumber: number;
14
25
 
15
26
  declare sourceChainId: number;
16
27
  declare sourceTransactionHash: CreationOptional<string>;
17
28
  declare sourceBlockNumber: CreationOptional<number>;
18
- declare sourceStatus: string;
29
+ declare sourceStatus: CreationOptional<string>;
19
30
  declare sourceErrors: CreationOptional<string[]>;
31
+ declare sourceLogs: CreationOptional<any[]>;
20
32
  declare sourceCreatedAt: CreationOptional<Date>;
21
33
  declare sourceDelayUntil: CreationOptional<Date>;
22
34
 
23
35
  declare targetChainId: number;
24
36
  declare targetTransactionHash: CreationOptional<string>;
25
37
  declare targetBlockNumber: CreationOptional<number>;
26
- declare targetStatus: string;
38
+ declare targetStatus: CreationOptional<string>;
27
39
  declare targetErrors: CreationOptional<string[]>;
40
+ declare targetLogs: CreationOptional<any[]>;
28
41
  declare targetCreatedAt: CreationOptional<Date>;
29
42
  declare targetDelayUntil: CreationOptional<Date>;
30
43
 
31
- declare submitEvent: any;
32
- declare sourceEvent: CreationOptional<any>;
33
- declare targetEvent: CreationOptional<any>;
34
-
44
+ declare requestEvent: {
45
+ bridger: string;
46
+ metadata: string;
47
+ position: IPosition;
48
+ sourceChainId: number;
49
+ targetChainId: number;
50
+ };
51
+ declare requestSendEvent: CreationOptional<any>;
52
+ declare committedEvent: CreationOptional<any>;
53
+ declare completedEvent: CreationOptional<any>;
54
+
55
+ declare position: any;
35
56
  declare metadata: CreationOptional<any>;
36
57
 
37
- declare status: string;
58
+ declare status: CreationOptional<string>;
38
59
 
39
60
  declare createdAt: CreationOptional<Date>;
40
61
  declare updatedAt: CreationOptional<Date>;
@@ -47,23 +68,28 @@ Transaction.init({
47
68
  primaryKey: true
48
69
  },
49
70
 
50
- submitTransactionHash: DataTypes.NUMBER,
51
- submitBlockNumber: DataTypes.NUMBER,
71
+ requestTransactionHash: DataTypes.NUMBER,
72
+ requestBlockNumber: DataTypes.NUMBER,
52
73
 
53
74
  transactionHash: DataTypes.STRING,
54
75
  action: DataTypes.STRING,
55
-
56
- from: DataTypes.STRING,
57
- to: DataTypes.STRING,
76
+ bridger: DataTypes.STRING,
58
77
 
59
78
  sourceChainId: DataTypes.NUMBER,
60
79
  sourceTransactionHash: DataTypes.STRING,
61
80
  sourceBlockNumber: DataTypes.NUMBER,
62
- sourceStatus: DataTypes.STRING,
81
+ sourceStatus: {
82
+ type: DataTypes.STRING,
83
+ defaultValue: 'uninitialised'
84
+ },
63
85
  sourceErrors: {
64
86
  type: DataTypes.JSON,
65
87
  // defaultValue: [],
66
88
  },
89
+ sourceLogs: {
90
+ type: DataTypes.JSON,
91
+ // defaultValue: [],
92
+ },
67
93
  sourceCreatedAt: {
68
94
  type: DataTypes.DATE,
69
95
  defaultValue: Date.now()
@@ -73,18 +99,30 @@ Transaction.init({
73
99
  targetChainId: DataTypes.NUMBER,
74
100
  targetTransactionHash: DataTypes.STRING,
75
101
  targetBlockNumber: DataTypes.NUMBER,
76
- targetStatus: DataTypes.STRING,
102
+ targetStatus: {
103
+ type: DataTypes.STRING,
104
+ defaultValue: 'uninitialised'
105
+ },
77
106
  targetErrors: {
78
107
  type: DataTypes.JSON,
79
108
  // defaultValue: [],
80
109
  },
110
+ targetLogs: {
111
+ type: DataTypes.JSON,
112
+ // defaultValue: [],
113
+ },
81
114
  targetCreatedAt: DataTypes.DATE,
82
115
  targetDelayUntil: DataTypes.DATE,
83
116
 
84
- submitEvent: DataTypes.JSON,
85
- sourceEvent: DataTypes.JSON,
86
- targetEvent: DataTypes.JSON,
117
+ requestEvent: {
118
+ type: DataTypes.JSON,
119
+ allowNull: false
120
+ },
121
+ requestSendEvent: DataTypes.JSON,
122
+ committedEvent: DataTypes.JSON,
123
+ completedEvent: DataTypes.JSON,
87
124
 
125
+ position: DataTypes.JSON,
88
126
  metadata: DataTypes.JSON,
89
127
 
90
128
  status: {
@@ -0,0 +1,5 @@
1
+ import withdraw from "./withdraw"
2
+
3
+ export default {
4
+ withdraw,
5
+ }
@@ -0,0 +1,56 @@
1
+ import abi from "@/abi";
2
+ import config from "@/config";
3
+ import { addresses, tokens } from "@/constants";
4
+ import { Transaction } from "@/db";
5
+ import { InteropXContract } from "@/typechain";
6
+ import { ChainId } from "@/types";
7
+ import { getContract, getRpcProviderUrl } from "@/utils";
8
+ import { ethers } from "ethers";
9
+ import { MetaTransaction, OperationType } from "ethers-multisend";
10
+
11
+ export default async function (transaction: Transaction, type: 'source' | 'target') {
12
+ const transactions: MetaTransaction[] = [];
13
+ const logs: any[] = [];
14
+
15
+ if (transaction.action !== 'withdraw') {
16
+ throw new Error(`Invalid action: ${transaction.action}`)
17
+ }
18
+
19
+ if (type !== 'source') {
20
+ throw new Error(`Type not supported: ${type}`)
21
+ }
22
+
23
+ if (transaction.action === 'withdraw' && transaction.sourceStatus === 'pending') {
24
+ throw Error('Cannot build data for pending withdraw transaction');
25
+ }
26
+
27
+ if (!transaction.requestEvent) {
28
+ throw Error('Cannot build data for transaction without requestEvent');
29
+ }
30
+
31
+ const { bridger, position, sourceChainId, targetChainId, metadata, } = transaction.requestEvent;
32
+
33
+ const sourceChainProvider = new ethers.providers.JsonRpcProvider(getRpcProviderUrl(targetChainId as ChainId));
34
+ const sourceWallet = new ethers.Wallet(config.privateKey, sourceChainProvider);
35
+ const contractAddress = addresses[sourceChainId].interopXContract;
36
+ const contract = getContract<InteropXContract>(contractAddress, abi.interopXContract, sourceWallet);
37
+
38
+ const { data } = await contract.populateTransaction.withdrawRequested(
39
+ bridger,
40
+ position.withdraw[0].sourceToken,
41
+ position.withdraw[0].targetToken,
42
+ position.withdraw[0].amount,
43
+ targetChainId,
44
+ transaction.requestTransactionHash,
45
+ metadata,
46
+ );
47
+
48
+ transactions.push({
49
+ to: contractAddress,
50
+ data: data!,
51
+ value: '0',
52
+ operation: OperationType.Call,
53
+ });
54
+
55
+ return { transactions, logs }
56
+ }
@@ -0,0 +1,19 @@
1
+ import { Transaction } from "@/db";
2
+ import { encodeMulti } from "ethers-multisend";
3
+ import actions from "./actions";
4
+
5
+ export const buildGnosisAction = async (transaction: Transaction, type?: 'source' | 'target') => {
6
+ type = type || transaction.sourceStatus === 'pending' ? 'source' : 'target';
7
+
8
+ if (actions.hasOwnProperty(transaction.action)) {
9
+
10
+ const { transactions, logs } = await actions[transaction.action](transaction, type);
11
+
12
+ return {
13
+ data: encodeMulti(transactions).data,
14
+ logs
15
+ };
16
+ }
17
+
18
+ throw new Error(`Unknown action: ${transaction.action}`);
19
+ }
package/src/index.ts CHANGED
@@ -1,21 +1,7 @@
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
- "@/api": __dirname + "/api",
9
- "@/net": __dirname + "/net",
10
- "@/db": __dirname + "/db",
11
- "@/config": __dirname + "/config",
12
- "@/types": __dirname + "/types",
13
- "@/abi": __dirname + "/abi",
14
- "@/constants": __dirname + "/constants",
15
- "@/typechain": __dirname + "/typechain"
16
- })
1
+ import './alias'
2
+ import expandHomeDir from "expand-home-dir";
3
+ import fs from 'fs-extra'
17
4
 
18
- moduleAlias();
19
5
  import dotenv from "dotenv";
20
6
  import chalk from 'chalk';
21
7
  import { ethers } from "ethers";
@@ -25,12 +11,30 @@ dotenv.config();
25
11
  import Logger from "@/logger";
26
12
  const logger = new Logger('Process')
27
13
 
14
+ const GIT_SHORT_HASH = '@GIT_SHORT_HASH@';
15
+
28
16
  const printUsage = () => {
17
+ console.log()
18
+ console.log(`Interop X Node (v${packageJson.version} - rev.${GIT_SHORT_HASH})`)
19
+ console.log()
20
+
29
21
  console.log('Usage:')
30
- console.log(' PRIVATE_KEY=abcd1234 interop-x')
31
- console.log(' PRIVATE_KEY=abcd1234 STAGING=true interop-x')
32
- console.log(' PRIVATE_KEY=abcd1234 AUTO_UPDATE=true interop-x')
33
- console.log(' PRIVATE_KEY=abcd1234 API_HOST=0.0.0.0 API_PORT=8080 interop-x')
22
+ console.log(' interop-x help Show this message')
23
+ console.log(' interop-x version Print out the installed version of Interop X')
24
+
25
+ console.log()
26
+
27
+ console.log(' interop-x down Put the node into maintenance mode')
28
+ console.log(' interop-x up Take the node out of maintenance mode')
29
+
30
+ console.log()
31
+
32
+ console.log(' PRIVATE_KEY=abcd1234 interop-x Start the node with the given private key')
33
+ console.log(' PRIVATE_KEY=abcd1234 STAGING=true interop-x Start the node in staging mode')
34
+ console.log(' PRIVATE_KEY=abcd1234 AUTO_UPDATE=true interop-x Start the node in auto update mode')
35
+ console.log(' PRIVATE_KEY=abcd1234 API_HOST=0.0.0.0 API_PORT=8080 interop-x Start the node with custom API host and port')
36
+ console.log()
37
+
34
38
  }
35
39
 
36
40
  if (process.argv.at(-1) === 'help') {
@@ -38,12 +42,27 @@ if (process.argv.at(-1) === 'help') {
38
42
  process.exit(0)
39
43
  }
40
44
 
45
+ const basePath = expandHomeDir(`~/.interop-x`);
46
+
47
+ if (process.argv.at(-1) === 'down') {
48
+ fs.outputFileSync(basePath + '/maintenance', Date.now().toString())
49
+ console.log(chalk.red('Maintenance mode enabled'))
50
+ process.exit(0)
51
+ }
52
+
53
+ if (process.argv.at(-1) === 'up') {
54
+ fs.removeSync(basePath + '/maintenance')
55
+ console.log(chalk.green('Maintenance mode disabled'))
56
+ process.exit(0)
57
+ }
58
+
59
+
41
60
  if (process.argv.at(-1) === 'version') {
42
- console.log(`Interop X Node (v${packageJson.version} - rev.@GIT_SHORT_HASH@)`)
61
+ console.log(`Interop X Node (v${packageJson.version} - rev.${GIT_SHORT_HASH})`)
43
62
  process.exit(0)
44
63
  }
45
64
 
46
- if(! process.env.PRIVATE_KEY) {
65
+ if (!process.env.PRIVATE_KEY) {
47
66
  console.error(chalk.bgRed.white.bold('Please provide a private key\n'))
48
67
  printUsage()
49
68
  process.exit(1)
@@ -56,12 +75,13 @@ try {
56
75
  process.exit(1)
57
76
  }
58
77
 
59
- logger.debug(`Starting Interop X Node (v${packageJson.version} - rev.@GIT_SHORT_HASH@)`)
78
+ logger.debug(`Starting Interop X Node (v${packageJson.version} - rev.${GIT_SHORT_HASH})`)
60
79
 
61
80
  import { Tasks } from "@/tasks";
62
81
  import { startPeer, protocol, peerPool } from "@/net";
63
82
  import { startApiServer } from '@/api';
64
83
  import { Transaction } from './db';
84
+ import { shortenHash } from './utils';
65
85
 
66
86
  async function main() {
67
87
 
@@ -76,7 +96,12 @@ async function main() {
76
96
  protocol.on('TransactionStatus', async (payload) => {
77
97
  if (!peerPool.isLeadNode(payload.peerId)) {
78
98
  const peer = peerPool.getPeer(payload.peerId)
79
- logger.info(`ignored transaction status from ${payload.peerId} ${peer?.publicAddress} `)
99
+
100
+ if (!peer) {
101
+ return;
102
+ }
103
+
104
+ logger.info(`ignored transaction status from ${payload.peerId} ${shortenHash(peer.publicAddress)} `)
80
105
  return;
81
106
  }
82
107
 
@@ -89,10 +114,12 @@ async function main() {
89
114
  transaction.sourceStatus = payload.data.sourceStatus
90
115
  transaction.sourceTransactionHash = payload.data.sourceTransactionHash
91
116
  transaction.sourceErrors = payload.data.sourceErrors
117
+ transaction.sourceLogs = payload.data.sourceLogs
92
118
 
93
119
  transaction.targetStatus = payload.data.targetStatus
94
120
  transaction.targetTransactionHash = payload.data.targetTransactionHash
95
121
  transaction.targetErrors = payload.data.targetErrors
122
+ transaction.targetLogs = payload.data.targetLogs
96
123
 
97
124
  transaction.status = payload.data.status
98
125
 
@@ -17,6 +17,7 @@ import KadDHT from "libp2p-kad-dht";
17
17
  import PubsubPeerDiscovery from "libp2p-pubsub-peer-discovery";
18
18
  import { protocol } from "@/net";
19
19
  import config from "@/config";
20
+ import chalk from "chalk";
20
21
 
21
22
  const logger = new Logger("Peer");
22
23
 
@@ -80,7 +81,7 @@ export const startPeer = async ({ }: IPeerOptions) => {
80
81
  },
81
82
  });
82
83
 
83
- logger.info("Peer ID:", node.peerId.toB58String());
84
+ logger.info("Peer ID:", chalk.bold(node.peerId.toB58String()));
84
85
 
85
86
  await node.start();
86
87
 
@@ -2,6 +2,8 @@ import { Event } from "@/types";
2
2
  import config from "@/config";
3
3
  import Logger from "@/logger";
4
4
  import { getAddress } from "ethers/lib/utils";
5
+ import { shortenHash } from "@/utils";
6
+ import chalk from "chalk";
5
7
 
6
8
 
7
9
  const logger = new Logger('PeerPool')
@@ -87,7 +89,7 @@ export class PeerPool {
87
89
 
88
90
  if (newPeer) {
89
91
  config.events.emit(Event.POOL_PEER_ADDED, peer)
90
- logger.info(`Peer ${peer.id} with address ${peer.publicAddress} added to pool`)
92
+ logger.info(`Peer ${chalk.bold(shortenHash(peer.id, 16))} with address ${chalk.bold(shortenHash(peer.publicAddress))} added to pool`)
91
93
  }
92
94
  }
93
95
  }
@@ -102,7 +104,7 @@ export class PeerPool {
102
104
  if (this.pool.delete(peer.id)) {
103
105
  peer.pooled = false
104
106
  config.events.emit(Event.POOL_PEER_REMOVED, peer)
105
- logger.info(`Peer ${peer.id} with address ${peer.publicAddress} removed from pool`)
107
+ logger.info(`Peer ${chalk.bold(shortenHash(peer.id, 16))} with address ${chalk.bold(shortenHash(peer.publicAddress))} removed from pool`)
106
108
  }
107
109
  }
108
110
  }
@@ -123,7 +125,6 @@ export class PeerPool {
123
125
  return this.activePeers.map((p) => p.id)
124
126
  }
125
127
 
126
-
127
128
  getPeer(id: string){
128
129
  return this.pool.get(id);
129
130
  }
@@ -138,6 +139,9 @@ export class PeerPool {
138
139
  return getAddress(peer.publicAddress) === getAddress(config.leadNodeAddress)
139
140
  }
140
141
 
142
+ getLeadPeer() {
143
+ return this.peers.find((p) => this.isLeadNode(p.id))
144
+ }
141
145
 
142
146
  cleanup() {
143
147
  // let compDate = Date.now() - this.PEERS_CLEANUP_TIME_LIMIT * 60
@@ -2,12 +2,13 @@ import { BaseDialProtocol } from "./BaseDialProtocol";
2
2
  import wait from "waait";
3
3
  import config from "@/config";
4
4
  import { Transaction } from "@/db";
5
- import { buildDataForTransaction, signGnosisSafeTx } from "@/utils";
5
+ import { signGnosisSafeTx } from "@/utils";
6
6
  import { addresses } from "@/constants";
7
7
  import { ChainId } from "@/types";
8
+ import { buildGnosisAction } from "@/gnosis";
8
9
 
9
10
  export interface ISignatureRequest {
10
- type: 'source' | 'target' ,
11
+ type: 'source' | 'target',
11
12
  transactionHash: string
12
13
  safeTxGas: string
13
14
  safeNonce: string
@@ -46,18 +47,11 @@ export class SignatureDialProtocol extends BaseDialProtocol<ISignatureRequest, I
46
47
  error: 'Event not found'
47
48
  };
48
49
  }
49
-
50
- console.log("signing:", {
51
- to: addresses[transaction.targetChainId].multisend,
52
- data: await buildDataForTransaction(transaction, data.type),
53
- chainId: transaction.targetChainId as ChainId,
54
- safeTxGas: data.safeTxGas,
55
- nonce: data.safeNonce,
56
- });
50
+ const { data: gnosisData } = await buildGnosisAction(transaction, data.type);
57
51
 
58
52
  const signedData = await signGnosisSafeTx({
59
53
  to: addresses[transaction.targetChainId].multisend,
60
- data: await buildDataForTransaction(transaction, data.type),
54
+ data: gnosisData,
61
55
  chainId: transaction.targetChainId as ChainId,
62
56
  safeTxGas: data.safeTxGas,
63
57
  nonce: data.safeNonce,
@@ -0,0 +1,33 @@
1
+ import { BaseDialProtocol } from "./BaseDialProtocol";
2
+ import { Transaction } from "@/db";
3
+
4
+ export class TransactionStatusDialProtocol extends BaseDialProtocol<string, Pick<Transaction, 'transactionHash' | 'sourceStatus' | 'sourceTransactionHash' | 'sourceErrors' | 'sourceLogs' | 'targetStatus' | 'targetTransactionHash' | 'targetErrors' | 'targetLogs' | 'status'> | null> {
5
+ protected timeout = 30000;
6
+
7
+ constructor(libp2p) {
8
+ super(libp2p, '/interop-x/transaction-status')
9
+ }
10
+
11
+ async response(transactionHash: string){
12
+ const transaction = await Transaction.findOne({ where: { transactionHash } })
13
+
14
+ if(! transaction){
15
+ return null
16
+ }
17
+ return {
18
+ transactionHash: transaction.transactionHash,
19
+
20
+ sourceStatus: transaction.sourceStatus,
21
+ sourceTransactionHash: transaction.sourceTransactionHash,
22
+ sourceErrors: transaction.sourceErrors,
23
+ sourceLogs: transaction.sourceLogs,
24
+
25
+ targetStatus: transaction.targetStatus,
26
+ targetTransactionHash: transaction.targetTransactionHash,
27
+ targetErrors: transaction.targetErrors,
28
+ targetLogs: transaction.targetLogs,
29
+
30
+ status: transaction.status,
31
+ }
32
+ }
33
+ }
@@ -6,6 +6,7 @@ import { IPeerInfo, peerPool } from "..";
6
6
  import config from "@/config";
7
7
  import { Event } from "@/types";
8
8
  import { Transaction } from "@/db";
9
+ import { TransactionStatusDialProtocol } from "./dial/TransactionStatusDialProtocol";
9
10
 
10
11
  export interface ProtocolOptions {
11
12
  /* Handshake timeout in ms (default: 8000) */
@@ -35,7 +36,7 @@ interface PeerInfoEvent extends BaseMessageEvent {
35
36
  }
36
37
 
37
38
  interface TransactionStatusEvent extends BaseMessageEvent {
38
- data: Pick<Transaction, 'transactionHash' | 'sourceStatus' | 'sourceTransactionHash' | 'sourceErrors' | 'targetStatus' | 'targetTransactionHash' | 'targetErrors' | 'status'>
39
+ data: Pick<Transaction, 'transactionHash' | 'sourceStatus' | 'sourceTransactionHash' | 'sourceErrors' | 'sourceLogs' | 'targetStatus' | 'targetTransactionHash' | 'targetErrors' | 'targetLogs' | 'status'>
39
40
  }
40
41
 
41
42
  declare interface Protocol {
@@ -65,31 +66,42 @@ class Protocol extends EventEmitter {
65
66
  Buffer.from(transaction.transactionHash),
66
67
 
67
68
  Buffer.from(transaction.sourceStatus),
68
- Buffer.from(transaction.sourceTransactionHash),
69
+ Buffer.from(transaction.sourceTransactionHash || ''),
69
70
  transaction.sourceErrors ? transaction.sourceErrors.map((e) => Buffer.from(e)) : [],
71
+ transaction.sourceLogs ? transaction.sourceLogs.map((e) => [Buffer.from(e.type), Buffer.from(e.message)]) : [],
70
72
 
71
73
  Buffer.from(transaction.targetStatus),
72
- Buffer.from(transaction.targetTransactionHash),
74
+ Buffer.from(transaction.targetTransactionHash || ''),
73
75
  transaction.targetErrors ? transaction.targetErrors.map((e) => Buffer.from(e)) : [],
76
+ transaction.targetLogs ? transaction.targetLogs.map((e) => [Buffer.from(e.type), Buffer.from(e.message)]) : [],
74
77
 
75
78
  Buffer.from(transaction.status),
76
79
  ],
77
- decode: ([transactionHash, sourceStatus, sourceTransactionHash, sourceErrors, targetStatus, targetTransactionHash, targetErrors, status]: [Buffer, Buffer, Buffer, Buffer[], Buffer, Buffer, Buffer[], Buffer]) => ({
80
+ decode: ([transactionHash, sourceStatus, sourceTransactionHash, sourceErrors, sourceLogs, targetStatus, targetTransactionHash, targetErrors, targetLogs, status]: [Buffer, Buffer, Buffer, Buffer[],[Buffer,Buffer][], Buffer, Buffer, Buffer[],[Buffer,Buffer][], Buffer]) => ({
78
81
  transactionHash: transactionHash.toString(),
79
82
 
80
83
  sourceStatus: sourceStatus.toString(),
81
- sourceTransactionHash: sourceTransactionHash.toString(),
84
+ sourceTransactionHash: sourceTransactionHash.toString() || null,
82
85
  sourceErrors: sourceErrors.map((e) => e.toString()),
86
+ sourceLogs: sourceLogs.map(e => ({
87
+ type: e[0].toString(),
88
+ message: e[1].toString(),
89
+ })),
83
90
 
84
91
  targetStatus: targetStatus.toString(),
85
- targetTransactionHash: targetTransactionHash.toString(),
92
+ targetTransactionHash: targetTransactionHash.toString() || null,
86
93
  targetErrors: targetErrors.map((e) => e.toString()),
94
+ targetLogs: targetLogs.map(e => ({
95
+ type: e[0].toString(),
96
+ message: e[1].toString(),
97
+ })),
87
98
 
88
99
  status: status.toString(),
89
100
  }),
90
101
  },
91
102
  ];
92
103
  private signature: SignatureDialProtocol;
104
+ private transactionStatus: TransactionStatusDialProtocol;
93
105
 
94
106
 
95
107
  start({ libp2p, topic = null, }) {
@@ -109,6 +121,7 @@ class Protocol extends EventEmitter {
109
121
  })
110
122
 
111
123
  this.signature = new SignatureDialProtocol(this.libp2p);
124
+ this.transactionStatus = new TransactionStatusDialProtocol(this.libp2p);
112
125
  }
113
126
 
114
127
 
@@ -177,6 +190,15 @@ class Protocol extends EventEmitter {
177
190
  return []
178
191
  }
179
192
  }
193
+
194
+ async requestTransactionStatus(transactionHash: string, peerId: string) {
195
+ try {
196
+ return await this.transactionStatus.send(transactionHash, peerId);
197
+ } catch (error) {
198
+ console.log(error);
199
+ return null
200
+ }
201
+ }
180
202
  }
181
203
 
182
204
  export const protocol = new Protocol();