@instadapp/interop-x 0.0.0-dev.6088815 → 0.0.0-dev.6a2c9c4

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 (84) hide show
  1. package/dist/package.json +16 -14
  2. package/dist/src/abi/index.js +2 -4
  3. package/dist/src/abi/interopXContract.json +454 -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 +7 -7
  8. package/dist/src/constants/index.js +0 -1
  9. package/dist/src/constants/tokens.js +62 -39
  10. package/dist/src/db/models/transaction.js +27 -11
  11. package/dist/src/gnosis/actions/index.js +9 -0
  12. package/dist/src/gnosis/actions/withdraw/index.js +115 -0
  13. package/dist/src/gnosis/index.js +20 -0
  14. package/dist/src/index.js +39 -23
  15. package/dist/src/net/peer/index.js +2 -1
  16. package/dist/src/net/pool/index.js +7 -2
  17. package/dist/src/net/protocol/dial/SignatureDialProtocol.js +9 -10
  18. package/dist/src/net/protocol/dial/TransactionStatusDialProtocol.js +30 -0
  19. package/dist/src/net/protocol/index.js +26 -5
  20. package/dist/src/tasks/AutoUpdateTask.js +42 -18
  21. package/dist/src/tasks/BaseTask.js +11 -3
  22. package/dist/src/tasks/{InteropXGateway/ProcessDepositEvents.js → InteropXContract/ProcessBridgeRequestEvents.js} +61 -53
  23. package/dist/src/tasks/InteropXContract/SyncBridgeCommittedEvents.js +93 -0
  24. package/dist/src/tasks/InteropXContract/SyncBridgeRequestEvents.js +78 -0
  25. package/dist/src/tasks/InteropXContract/SyncBridgeRequestSentEvents.js +90 -0
  26. package/dist/src/tasks/Transactions/SyncTransactionStatusTask.js +57 -0
  27. package/dist/src/tasks/index.js +16 -17
  28. package/dist/src/typechain/{InteropBridgeToken.js → InteropXContract.js} +0 -0
  29. package/dist/src/typechain/factories/InteropXContract__factory.js +635 -0
  30. package/dist/src/typechain/factories/index.js +3 -5
  31. package/dist/src/typechain/index.js +3 -5
  32. package/dist/src/utils/index.js +57 -92
  33. package/package.json +16 -14
  34. package/src/abi/index.ts +2 -4
  35. package/src/abi/interopXContract.json +454 -0
  36. package/src/alias.ts +6 -0
  37. package/src/api/index.ts +5 -2
  38. package/src/config/index.ts +9 -1
  39. package/src/constants/addresses.ts +8 -8
  40. package/src/constants/index.ts +0 -1
  41. package/src/constants/tokens.ts +63 -40
  42. package/src/db/models/transaction.ts +76 -27
  43. package/src/gnosis/actions/index.ts +5 -0
  44. package/src/gnosis/actions/withdraw/index.ts +155 -0
  45. package/src/gnosis/index.ts +19 -0
  46. package/src/index.ts +52 -25
  47. package/src/net/peer/index.ts +2 -1
  48. package/src/net/pool/index.ts +7 -3
  49. package/src/net/protocol/dial/SignatureDialProtocol.ts +11 -13
  50. package/src/net/protocol/dial/TransactionStatusDialProtocol.ts +33 -0
  51. package/src/net/protocol/index.ts +28 -6
  52. package/src/tasks/AutoUpdateTask.ts +48 -21
  53. package/src/tasks/BaseTask.ts +13 -3
  54. package/src/tasks/{InteropBridge/ProcessWithdrawEvents.ts → InteropXContract/ProcessBridgeRequestEvents.ts} +88 -95
  55. package/src/tasks/InteropXContract/SyncBridgeCommittedEvents.ts +125 -0
  56. package/src/tasks/InteropXContract/SyncBridgeRequestEvents.ts +115 -0
  57. package/src/tasks/InteropXContract/SyncBridgeRequestSentEvents.ts +121 -0
  58. package/src/tasks/Transactions/SyncTransactionStatusTask.ts +69 -0
  59. package/src/tasks/index.ts +21 -18
  60. package/src/typechain/InteropXContract.ts +680 -0
  61. package/src/typechain/factories/InteropXContract__factory.ts +642 -0
  62. package/src/typechain/factories/index.ts +1 -2
  63. package/src/typechain/index.ts +2 -4
  64. package/src/utils/index.ts +97 -129
  65. package/tsconfig.json +7 -2
  66. package/dist/src/abi/interopBridgeToken.json +0 -286
  67. package/dist/src/abi/interopXGateway.json +0 -184
  68. package/dist/src/constants/itokens.js +0 -13
  69. package/dist/src/tasks/InteropBridge/ProcessWithdrawEvents.js +0 -147
  70. package/dist/src/tasks/InteropBridge/SyncWithdrawEvents.js +0 -70
  71. package/dist/src/tasks/InteropXGateway/SyncDepositEvents.js +0 -75
  72. package/dist/src/typechain/InteropXGateway.js +0 -2
  73. package/dist/src/typechain/factories/InteropBridgeToken__factory.js +0 -459
  74. package/dist/src/typechain/factories/InteropXGateway__factory.js +0 -265
  75. package/src/abi/interopBridgeToken.json +0 -286
  76. package/src/abi/interopXGateway.json +0 -184
  77. package/src/constants/itokens.ts +0 -10
  78. package/src/tasks/InteropBridge/SyncWithdrawEvents.ts +0 -121
  79. package/src/tasks/InteropXGateway/ProcessDepositEvents.ts +0 -245
  80. package/src/tasks/InteropXGateway/SyncDepositEvents.ts +0 -126
  81. package/src/typechain/InteropBridgeToken.ts +0 -686
  82. package/src/typechain/InteropXGateway.ts +0 -407
  83. package/src/typechain/factories/InteropBridgeToken__factory.ts +0 -466
  84. package/src/typechain/factories/InteropXGateway__factory.ts +0 -272
@@ -1,104 +1,127 @@
1
1
  export const tokens = {
2
- 1: [
3
- {
4
- symbol: "ETH",
5
- name: "Ethereum",
6
- address: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
7
- decimals: 18,
8
- },
9
- {
10
- symbol: "DAI",
11
- name: "DAI Stable",
12
- address: "0x6B175474E89094C44Da98b954EedeAC495271d0F",
13
- decimals: 18,
14
- },
15
- {
16
- symbol: "USDC",
17
- name: "USD Coin",
18
- address: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
19
- decimals: 6,
20
- },
21
- {
22
- symbol: "USDT",
23
- name: "Tether USD Coin",
24
- address: "0xdAC17F958D2ee523a2206206994597C13D831ec7",
25
- decimals: 6,
26
- },
27
- {
28
- symbol: "WBTC",
29
- name: "Wrapped BTC",
30
- address: "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599",
31
- decimals: 8,
32
- },
33
- ],
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
+ // ],
34
46
  137: [
35
47
  {
48
+ aliases: ['eth', 'weth'],
36
49
  symbol: "ETH",
37
50
  name: "Ethereum",
38
51
  address: "0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619",
39
52
  decimals: 18,
40
53
  },
41
54
  {
55
+ aliases: ['dai'],
42
56
  symbol: "DAI",
43
57
  name: "DAI Stable",
44
58
  address: "0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063",
45
59
  decimals: 18,
46
60
  },
47
61
  {
62
+ aliases: ['usdc'],
48
63
  symbol: "USDC",
49
64
  name: "USD Coin",
50
65
  address: "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174",
51
66
  decimals: 6,
52
67
  },
53
68
  {
69
+ aliases: ['usdt'],
54
70
  symbol: "USDT",
55
71
  name: "Tether USD Coin",
56
72
  address: "0xc2132D05D31c914a87C6611C10748AEb04B58e8F",
57
73
  decimals: 6,
58
74
  },
59
75
  {
76
+ aliases: ['wbtc'],
60
77
  symbol: "WBTC",
61
78
  name: "Wrapped BTC",
62
79
  address: "0x1BFD67037B42Cf73acF2047067bd4F2C47D9BfD6",
63
80
  decimals: 8,
64
81
  },
65
- {
66
- symbol: "AVAX",
67
- name: "Avalanche Token",
68
- address: "0x2C89bbc92BD86F8075d1DEcc58C7F4E0107f286b",
69
- decimals: 18,
70
- },
71
82
  ],
72
83
  43114: [
73
84
  {
85
+ aliases: ['eth', 'weth'],
74
86
  symbol: "ETH",
75
87
  name: "Ethereum",
76
88
  address: "0x49D5c2BdFfac6CE2BFdB6640F4F80f226bc10bAB",
77
89
  decimals: 18,
78
90
  },
79
91
  {
92
+ aliases: ['dai'],
80
93
  symbol: "DAI",
81
94
  name: "DAI Stable",
82
95
  address: "0xd586E7F844cEa2F87f50152665BCbc2C279D8d70",
83
96
  decimals: 18,
84
97
  },
85
98
  {
99
+ aliases: ['usdc'],
86
100
  symbol: "USDC",
87
101
  name: "USD Coin",
88
102
  address: "0xA7D7079b0FEaD91F3e65f86E8915Cb59c1a4C664",
89
103
  decimals: 6,
90
104
  },
91
105
  {
92
- symbol: "USDT",
106
+ aliases: ['usdt'],
107
+ symbol: "USDt",
93
108
  name: "Tether USD Coin",
109
+ address: "0x9702230A8Ea53601f5cD2dc00fDBc13d4dF4A8c7",
110
+ decimals: 6,
111
+ },
112
+ {
113
+ aliases: ['usdt'],
114
+ symbol: "USDT.e",
115
+ name: "Tether USD",
94
116
  address: "0xc7198437980c041c805A1EDcbA50c1Ce5db95118",
95
117
  decimals: 6,
96
118
  },
97
119
  {
120
+ aliases: ["wbtc"],
98
121
  symbol: "WBTC",
99
122
  name: "Wrapped BTC",
100
123
  address: "0x50b7545627a5162F82A992c33b87aDc75187B218",
101
124
  decimals: 8,
102
125
  },
103
126
  ],
104
- };
127
+ } as Record<number, { aliases: string[], symbol: string, name: string, address: string, decimals: number }[]>;
@@ -1,40 +1,75 @@
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;
8
- declare action: string;
9
- declare from: string;
10
- declare to: string;
19
+
20
+ declare actionId: 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
-
35
- declare metadata: CreationOptional<any>;
36
-
37
- declare status: string;
44
+ declare requestEvent: {
45
+ actionId: string;
46
+ bridger: string;
47
+ metadata: string;
48
+ position: IPosition;
49
+ sourceChainId: number;
50
+ targetChainId: number;
51
+ };
52
+ declare requestSentEvent: CreationOptional<{
53
+ actionId: string;
54
+ bridger: string;
55
+ metadata: string;
56
+ position: IPosition;
57
+ sourceChainId: number;
58
+ targetChainId: number;
59
+ requestTransactionHash: string;
60
+ }>;
61
+ declare committedEvent: CreationOptional<{
62
+ actionId: string;
63
+ bridger: string;
64
+ metadata: string;
65
+ position: IPosition;
66
+ sourceChainId: number;
67
+ targetChainId: number;
68
+ requestTransactionHash: string;
69
+ }>;
70
+ declare completedEvent: CreationOptional<any>;
71
+
72
+ declare status: CreationOptional<string>;
38
73
 
39
74
  declare createdAt: CreationOptional<Date>;
40
75
  declare updatedAt: CreationOptional<Date>;
@@ -47,23 +82,28 @@ Transaction.init({
47
82
  primaryKey: true
48
83
  },
49
84
 
50
- submitTransactionHash: DataTypes.NUMBER,
51
- submitBlockNumber: DataTypes.NUMBER,
85
+ requestTransactionHash: DataTypes.NUMBER,
86
+ requestBlockNumber: DataTypes.NUMBER,
52
87
 
53
88
  transactionHash: DataTypes.STRING,
54
- action: DataTypes.STRING,
55
-
56
- from: DataTypes.STRING,
57
- to: DataTypes.STRING,
89
+ actionId: DataTypes.STRING,
90
+ bridger: DataTypes.STRING,
58
91
 
59
92
  sourceChainId: DataTypes.NUMBER,
60
93
  sourceTransactionHash: DataTypes.STRING,
61
94
  sourceBlockNumber: DataTypes.NUMBER,
62
- sourceStatus: DataTypes.STRING,
95
+ sourceStatus: {
96
+ type: DataTypes.STRING,
97
+ defaultValue: 'uninitialised'
98
+ },
63
99
  sourceErrors: {
64
100
  type: DataTypes.JSON,
65
101
  // defaultValue: [],
66
102
  },
103
+ sourceLogs: {
104
+ type: DataTypes.JSON,
105
+ // defaultValue: [],
106
+ },
67
107
  sourceCreatedAt: {
68
108
  type: DataTypes.DATE,
69
109
  defaultValue: Date.now()
@@ -73,19 +113,28 @@ Transaction.init({
73
113
  targetChainId: DataTypes.NUMBER,
74
114
  targetTransactionHash: DataTypes.STRING,
75
115
  targetBlockNumber: DataTypes.NUMBER,
76
- targetStatus: DataTypes.STRING,
116
+ targetStatus: {
117
+ type: DataTypes.STRING,
118
+ defaultValue: 'uninitialised'
119
+ },
77
120
  targetErrors: {
78
121
  type: DataTypes.JSON,
79
122
  // defaultValue: [],
80
123
  },
124
+ targetLogs: {
125
+ type: DataTypes.JSON,
126
+ // defaultValue: [],
127
+ },
81
128
  targetCreatedAt: DataTypes.DATE,
82
129
  targetDelayUntil: DataTypes.DATE,
83
130
 
84
- submitEvent: DataTypes.JSON,
85
- sourceEvent: DataTypes.JSON,
86
- targetEvent: DataTypes.JSON,
87
-
88
- metadata: DataTypes.JSON,
131
+ requestEvent: {
132
+ type: DataTypes.JSON,
133
+ allowNull: false
134
+ },
135
+ requestSentEvent: DataTypes.JSON,
136
+ committedEvent: DataTypes.JSON,
137
+ completedEvent: DataTypes.JSON,
89
138
 
90
139
  status: {
91
140
  type: DataTypes.STRING,
@@ -0,0 +1,5 @@
1
+ import withdraw from "./withdraw"
2
+
3
+ export default {
4
+ withdraw,
5
+ }
@@ -0,0 +1,155 @@
1
+ import abi from "@/abi";
2
+ import config from "@/config";
3
+ import { addresses, tokens } from "@/constants";
4
+ import { Transaction } from "@/db";
5
+ import { Erc20, InteropXContract } from "@/typechain";
6
+ import { ChainId } from "@/types";
7
+ import { getContract, getRpcProviderUrl, LiquidityError } from "@/utils";
8
+ import { BigNumber, ethers } from "ethers";
9
+ import { MetaTransaction, OperationType } from "ethers-multisend";
10
+
11
+
12
+ const getBridgeAmounts = async (user: string, tokens: string[], chainId: ChainId) => {
13
+ const sourceChainProvider = new ethers.providers.JsonRpcProvider(getRpcProviderUrl(chainId));
14
+ const sourceWallet = new ethers.Wallet(config.privateKey, sourceChainProvider);
15
+ const contractAddress = addresses[chainId].interopXContract;
16
+ const contract = getContract<InteropXContract>(contractAddress, abi.interopXContract, sourceWallet);
17
+ const data = await contract.getBridgeAmounts(user, tokens);
18
+
19
+
20
+ return data.map((item, index) => ({
21
+ token: tokens[index],
22
+ deposit: item.deposit,
23
+ withdraw: item.withdraw,
24
+ }))
25
+ }
26
+
27
+ export default async function (transaction: Transaction, type: 'source' | 'target') {
28
+ const transactions: MetaTransaction[] = [];
29
+ const logs: any[] = [];
30
+
31
+ if (transaction.actionId !== 'withdraw') {
32
+ throw new Error(`Invalid action: ${transaction.actionId}`)
33
+ }
34
+
35
+ if (type !== 'source') {
36
+ throw new Error(`[WIP] Type not supported: ${type}`)
37
+ }
38
+
39
+ if (transaction.sourceStatus === 'pending') {
40
+ throw Error('Source transaction already processesing')
41
+ }
42
+
43
+ if (transaction.sourceStatus === 'pending') {
44
+ throw Error('Source transaction already processed')
45
+ }
46
+
47
+ if (!transaction.requestEvent) {
48
+ throw Error('Something went wrong, source transaction has no request event')
49
+ }
50
+
51
+ const { actionId, bridger, position, sourceChainId, targetChainId, metadata } = transaction.requestEvent;
52
+
53
+ const sourceChainProvider = new ethers.providers.JsonRpcProvider(getRpcProviderUrl(sourceChainId as ChainId));
54
+ const sourceWallet = new ethers.Wallet(config.privateKey, sourceChainProvider);
55
+ const contractAddress = addresses[sourceChainId].interopXContract;
56
+ const contract = getContract<InteropXContract>(contractAddress, abi.interopXContract, sourceWallet);
57
+
58
+ const sourceToken = tokens[sourceChainId].find(t => t.address.toLowerCase() === position.withdraw[0].sourceToken.toLowerCase());
59
+
60
+ if (!sourceToken) {
61
+ throw Error('Source token not found')
62
+ }
63
+
64
+ const targetToken = tokens[targetChainId].find(t => t.address.toLowerCase() === position.withdraw[0].targetToken.toLowerCase());
65
+
66
+ if (!targetToken) {
67
+ throw Error('Target token not found')
68
+ }
69
+
70
+ if (!sourceToken.aliases.some(alias => targetToken.aliases.includes(alias))) {
71
+ throw Error('Source and target token must be the same')
72
+ }
73
+
74
+ const networks: ChainId[] = [137, 43114];
75
+ const networkUserData = {}
76
+ for (const network of networks) {
77
+ networkUserData[network] = await getBridgeAmounts(bridger, tokens[network].map(t => t.address), network)
78
+ }
79
+
80
+ for (const tokenSymbol of ["dai", "usdc", "usdt", "eth", "wbtc"]) {
81
+ let totalDeposit = BigNumber.from(0)
82
+ let totalWithdraw = BigNumber.from(0)
83
+
84
+ for (const network of networks) {
85
+ // on avax might we have 2 usdc/usdt tokens
86
+ const matchedTokens = tokens[network].filter(t => t.aliases.includes(tokenSymbol));
87
+
88
+ for (const matchedToken of matchedTokens) {
89
+ const data = networkUserData[network].find(t => t.token.toLowerCase() === matchedToken.address.toLowerCase());
90
+
91
+ if (data) {
92
+ totalDeposit = totalDeposit.add(data.deposit)
93
+ totalWithdraw = totalWithdraw.add(data.withdraw)
94
+ }
95
+ }
96
+
97
+ // on Mainent add weth too
98
+ if (tokenSymbol === "eth" && network === 1) {
99
+ const weth = tokens[1].find(t => t.symbol === 'WETH');
100
+
101
+ if (weth) {
102
+ const data = networkUserData[1].find(t => t.token.toLowerCase() === weth.address.toLowerCase());
103
+
104
+ if (data) {
105
+ totalDeposit = totalDeposit.add(data.deposit)
106
+ totalWithdraw = totalWithdraw.add(data.withdraw)
107
+ }
108
+ }
109
+ }
110
+ }
111
+
112
+ if (totalWithdraw.gt(totalDeposit)) {
113
+ throw Error(`if withdraw > deposit, user has debt and we can't process the withdraw and reject it`)
114
+ }
115
+
116
+ if (totalWithdraw.lt(totalDeposit)) {
117
+ throw Error('Something went wrong')
118
+ }
119
+ }
120
+
121
+ let balance = BigNumber.from(0);
122
+
123
+ if (position.withdraw[0].sourceToken.toLowerCase() === '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE'.toLowerCase()) {
124
+ balance = await sourceChainProvider.getBalance(bridger);
125
+
126
+ } else {
127
+ const erc20 = getContract<Erc20>(position.withdraw[0].sourceToken, abi.erc20, sourceChainProvider);
128
+ balance = await erc20.balanceOf(addresses[sourceChainId].gnosisSafe);
129
+ }
130
+
131
+ if (balance.lt(position.withdraw[0].amount)) {
132
+ throw new LiquidityError()
133
+ }
134
+
135
+
136
+ const { data } = await contract.populateTransaction.withdrawRequested(
137
+ actionId,
138
+ bridger,
139
+ position.withdraw[0].sourceToken,
140
+ position.withdraw[0].targetToken,
141
+ position.withdraw[0].amount,
142
+ targetChainId,
143
+ transaction.requestTransactionHash,
144
+ metadata,
145
+ );
146
+
147
+ transactions.push({
148
+ to: contractAddress,
149
+ data: data!,
150
+ value: '0',
151
+ operation: OperationType.Call,
152
+ });
153
+
154
+ return { transactions, logs }
155
+ }
@@ -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 === 'success' ? 'target' : 'source')
7
+
8
+ if (actions.hasOwnProperty(transaction.actionId)) {
9
+
10
+ const { transactions, logs } = await actions[transaction.actionId](transaction, type);
11
+
12
+ return {
13
+ data: encodeMulti(transactions).data,
14
+ logs
15
+ };
16
+ }
17
+
18
+ throw new Error(`Unknown action: ${transaction.actionId}`);
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,14 +42,27 @@ if (process.argv.at(-1) === 'help') {
38
42
  process.exit(0)
39
43
  }
40
44
 
41
- const GIT_SHORT_HASH = '@GIT_SHORT_HASH@';
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
+
42
59
 
43
60
  if (process.argv.at(-1) === 'version') {
44
61
  console.log(`Interop X Node (v${packageJson.version} - rev.${GIT_SHORT_HASH})`)
45
62
  process.exit(0)
46
63
  }
47
64
 
48
- if(! process.env.PRIVATE_KEY) {
65
+ if (!process.env.PRIVATE_KEY) {
49
66
  console.error(chalk.bgRed.white.bold('Please provide a private key\n'))
50
67
  printUsage()
51
68
  process.exit(1)
@@ -64,6 +81,7 @@ import { Tasks } from "@/tasks";
64
81
  import { startPeer, protocol, peerPool } from "@/net";
65
82
  import { startApiServer } from '@/api';
66
83
  import { Transaction } from './db';
84
+ import { shortenHash } from './utils';
67
85
 
68
86
  async function main() {
69
87
 
@@ -71,14 +89,21 @@ async function main() {
71
89
 
72
90
  const tasks = new Tasks()
73
91
 
74
- tasks.start();
92
+ setTimeout(() => {
93
+ tasks.start();
94
+ }, 10000)
75
95
 
76
96
  startApiServer()
77
97
 
78
98
  protocol.on('TransactionStatus', async (payload) => {
79
99
  if (!peerPool.isLeadNode(payload.peerId)) {
80
100
  const peer = peerPool.getPeer(payload.peerId)
81
- logger.info(`ignored transaction status from ${payload.peerId} ${peer?.publicAddress} `)
101
+
102
+ if (!peer) {
103
+ return;
104
+ }
105
+
106
+ logger.info(`ignored transaction status from ${payload.peerId} ${shortenHash(peer.publicAddress)} `)
82
107
  return;
83
108
  }
84
109
 
@@ -91,10 +116,12 @@ async function main() {
91
116
  transaction.sourceStatus = payload.data.sourceStatus
92
117
  transaction.sourceTransactionHash = payload.data.sourceTransactionHash
93
118
  transaction.sourceErrors = payload.data.sourceErrors
119
+ transaction.sourceLogs = payload.data.sourceLogs
94
120
 
95
121
  transaction.targetStatus = payload.data.targetStatus
96
122
  transaction.targetTransactionHash = payload.data.targetTransactionHash
97
123
  transaction.targetErrors = payload.data.targetErrors
124
+ transaction.targetLogs = payload.data.targetLogs
98
125
 
99
126
  transaction.status = payload.data.status
100
127
 
@@ -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