@instadapp/interop-x 0.0.0-dev.92afe89 → 0.0.0-dev.9e91661
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.
- package/.env.example +2 -1
- package/dist/package.json +73 -0
- package/dist/src/abi/erc20.json +350 -0
- package/dist/src/abi/gnosisSafe.json +747 -0
- package/dist/src/abi/index.js +15 -0
- package/dist/src/abi/interopBridgeToken.json +298 -0
- package/dist/src/abi/interopXGateway.json +184 -0
- package/dist/src/api/index.js +36 -0
- package/dist/src/config/index.js +31 -0
- package/dist/src/constants/addresses.js +20 -0
- package/dist/{constants → src/constants}/index.js +2 -0
- package/dist/src/constants/itokens.js +13 -0
- package/dist/src/constants/tokens.js +107 -0
- package/dist/{db → src/db}/index.js +0 -0
- package/dist/{db → src/db}/models/index.js +1 -1
- package/dist/src/db/models/transaction.js +54 -0
- package/dist/{db → src/db}/sequelize.js +2 -1
- package/dist/src/gnosis/actions/deposit.js +40 -0
- package/dist/src/gnosis/actions/index.js +11 -0
- package/dist/src/gnosis/actions/withdraw.js +45 -0
- package/dist/src/gnosis/index.js +16 -0
- package/dist/src/index.js +130 -0
- package/dist/{logger → src/logger}/index.js +0 -0
- package/dist/{net → src/net}/index.js +0 -0
- package/dist/{net → src/net}/peer/index.js +13 -8
- package/dist/{net → src/net}/pool/index.js +34 -11
- package/dist/{net → src/net}/protocol/dial/BaseDialProtocol.js +1 -1
- package/dist/{net → src/net}/protocol/dial/SignatureDialProtocol.js +16 -14
- package/dist/src/net/protocol/dial/TransactionStatusDialProtocol.js +28 -0
- package/dist/{net → src/net}/protocol/index.js +44 -4
- package/dist/src/tasks/AutoUpdateTask.js +70 -0
- package/dist/{tasks → src/tasks}/BaseTask.js +14 -6
- package/dist/src/tasks/InteropBridge/ProcessWithdrawEvents.js +147 -0
- package/dist/src/tasks/InteropBridge/SyncBurnEvents.js +71 -0
- package/dist/src/tasks/InteropBridge/SyncMintEvents.js +67 -0
- package/dist/src/tasks/InteropXGateway/ProcessDepositEvents.js +163 -0
- package/dist/src/tasks/InteropXGateway/SyncDepositEvents.js +74 -0
- package/dist/src/tasks/InteropXGateway/SyncWithdrawtEvents.js +72 -0
- package/dist/src/tasks/Transactions/SyncTransactionStatusTask.js +53 -0
- package/dist/src/tasks/index.js +55 -0
- package/dist/src/typechain/Erc20.js +2 -0
- package/dist/src/typechain/GnosisSafe.js +2 -0
- package/dist/src/typechain/InteropBridgeToken.js +2 -0
- package/dist/src/typechain/InteropXGateway.js +2 -0
- package/dist/src/typechain/common.js +2 -0
- package/dist/src/typechain/factories/Erc20__factory.js +367 -0
- package/dist/src/typechain/factories/GnosisSafe__factory.js +1174 -0
- package/dist/src/typechain/factories/InteropBridgeToken__factory.js +471 -0
- package/dist/src/typechain/factories/InteropXGateway__factory.js +265 -0
- package/dist/src/typechain/factories/index.js +14 -0
- package/dist/src/typechain/index.js +35 -0
- package/dist/{types.js → src/types.js} +0 -0
- package/dist/src/utils/index.js +157 -0
- package/package.json +21 -7
- package/patches/@ethersproject+properties+5.6.0.patch +13 -0
- package/src/abi/erc20.json +350 -0
- package/src/abi/gnosisSafe.json +747 -0
- package/src/abi/index.ts +11 -0
- package/src/abi/interopBridgeToken.json +298 -0
- package/src/abi/interopXGateway.json +184 -0
- package/src/api/index.ts +36 -0
- package/src/config/index.ts +18 -2
- package/src/constants/addresses.ts +10 -3
- package/src/constants/index.ts +2 -0
- package/src/constants/itokens.ts +10 -0
- package/src/constants/tokens.ts +104 -0
- package/src/db/index.ts +1 -1
- package/src/db/models/index.ts +1 -1
- package/src/db/models/transaction.ts +96 -0
- package/src/db/sequelize.ts +2 -1
- package/src/gnosis/actions/deposit.ts +54 -0
- package/src/gnosis/actions/index.ts +7 -0
- package/src/gnosis/actions/withdraw.ts +61 -0
- package/src/gnosis/index.ts +13 -0
- package/src/index.ts +128 -6
- package/src/net/peer/index.ts +12 -10
- package/src/net/pool/index.ts +43 -13
- package/src/net/protocol/dial/BaseDialProtocol.ts +1 -1
- package/src/net/protocol/dial/SignatureDialProtocol.ts +19 -17
- package/src/net/protocol/dial/TransactionStatusDialProtocol.ts +31 -0
- package/src/net/protocol/index.ts +60 -4
- package/src/tasks/AutoUpdateTask.ts +82 -0
- package/src/tasks/BaseTask.ts +16 -7
- package/src/tasks/InteropBridge/ProcessWithdrawEvents.ts +232 -0
- package/src/tasks/InteropBridge/SyncBurnEvents.ts +121 -0
- package/src/tasks/InteropBridge/SyncMintEvents.ts +99 -0
- package/src/tasks/InteropXGateway/ProcessDepositEvents.ts +258 -0
- package/src/tasks/InteropXGateway/SyncDepositEvents.ts +124 -0
- package/src/tasks/InteropXGateway/SyncWithdrawtEvents.ts +105 -0
- package/src/tasks/Transactions/SyncTransactionStatusTask.ts +65 -0
- package/src/tasks/index.ts +45 -1
- package/src/typechain/Erc20.ts +491 -0
- package/src/typechain/GnosisSafe.ts +1728 -0
- package/src/typechain/InteropBridgeToken.ts +692 -0
- package/src/typechain/InteropXGateway.ts +407 -0
- package/src/typechain/common.ts +44 -0
- package/src/typechain/factories/Erc20__factory.ts +368 -0
- package/src/typechain/factories/GnosisSafe__factory.ts +1178 -0
- package/src/typechain/factories/InteropBridgeToken__factory.ts +478 -0
- package/src/typechain/factories/InteropXGateway__factory.ts +272 -0
- package/src/typechain/factories/index.ts +7 -0
- package/src/typechain/index.ts +12 -0
- package/src/types.ts +2 -2
- package/src/utils/index.ts +87 -5
- package/tsconfig.json +3 -0
- package/dist/config/index.js +0 -17
- package/dist/constants/addresses.js +0 -13
- package/dist/db/models/execution.js +0 -38
- package/dist/index.js +0 -34
- package/dist/tasks/index.js +0 -19
- package/dist/utils/index.js +0 -89
- package/src/db/models/execution.ts +0 -57
@@ -1,14 +1,15 @@
|
|
1
|
-
import { signGnosisSafeTx } from "../../../utils";
|
2
1
|
import { BaseDialProtocol } from "./BaseDialProtocol";
|
3
2
|
import wait from "waait";
|
4
|
-
import
|
5
|
-
import
|
6
|
-
import {
|
7
|
-
import {
|
3
|
+
import config from "@/config";
|
4
|
+
import { Transaction } from "@/db";
|
5
|
+
import { signGnosisSafeTx } from "@/utils";
|
6
|
+
import { addresses } from "@/constants";
|
7
|
+
import { ChainId } from "@/types";
|
8
|
+
import { buildGnosisData } from "@/gnosis";
|
8
9
|
|
9
10
|
export interface ISignatureRequest {
|
10
|
-
type:
|
11
|
-
|
11
|
+
type: 'source' | 'target' ,
|
12
|
+
transactionHash: string
|
12
13
|
safeTxGas: string
|
13
14
|
safeNonce: string
|
14
15
|
}
|
@@ -21,25 +22,25 @@ export class SignatureDialProtocol extends BaseDialProtocol<ISignatureRequest, I
|
|
21
22
|
protected timeout = 30000;
|
22
23
|
|
23
24
|
constructor(libp2p) {
|
24
|
-
super(libp2p, '/signatures')
|
25
|
+
super(libp2p, '/interop-x/signatures')
|
25
26
|
}
|
26
27
|
|
27
28
|
async response(data: ISignatureRequest): Promise<ISignatureResponse> {
|
28
29
|
const signer = config.wallet;
|
29
30
|
|
30
|
-
let
|
31
|
+
let transaction: Transaction | null;
|
31
32
|
let maxTimeout = 20000;
|
32
33
|
|
33
34
|
do {
|
34
|
-
|
35
|
+
transaction = await Transaction.findOne({ where: { transactionHash: data.transactionHash } })
|
35
36
|
|
36
|
-
if (!
|
37
|
+
if (!transaction) {
|
37
38
|
await wait(1000);
|
38
39
|
maxTimeout -= 1000;
|
39
40
|
}
|
40
|
-
} while (!
|
41
|
+
} while (!transaction && maxTimeout > 0)
|
41
42
|
|
42
|
-
if (!
|
43
|
+
if (!transaction) {
|
43
44
|
return {
|
44
45
|
signer: signer.address,
|
45
46
|
data: null,
|
@@ -48,15 +49,16 @@ export class SignatureDialProtocol extends BaseDialProtocol<ISignatureRequest, I
|
|
48
49
|
}
|
49
50
|
|
50
51
|
const signedData = await signGnosisSafeTx({
|
51
|
-
to: addresses[
|
52
|
-
data:
|
53
|
-
chainId:
|
52
|
+
to: addresses[transaction.targetChainId].multisend,
|
53
|
+
data: await buildGnosisData(transaction, data.type),
|
54
|
+
chainId: transaction.targetChainId as ChainId,
|
54
55
|
safeTxGas: data.safeTxGas,
|
56
|
+
nonce: data.safeNonce,
|
55
57
|
}, { signer });
|
56
58
|
|
57
59
|
return {
|
58
60
|
signer: signer.address,
|
59
|
-
data: signedData
|
61
|
+
data: signedData
|
60
62
|
}
|
61
63
|
}
|
62
64
|
}
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import { BaseDialProtocol } from "./BaseDialProtocol";
|
2
|
+
import { Transaction } from "@/db";
|
3
|
+
|
4
|
+
export class TransactionStatusDialProtocol extends BaseDialProtocol<string, Pick<Transaction, 'transactionHash' | 'sourceStatus' | 'sourceTransactionHash' | 'sourceErrors' | 'targetStatus' | 'targetTransactionHash' | 'targetErrors' | '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
|
+
|
24
|
+
targetStatus: transaction.targetStatus,
|
25
|
+
targetTransactionHash: transaction.targetTransactionHash,
|
26
|
+
targetErrors: transaction.targetErrors,
|
27
|
+
|
28
|
+
status: transaction.status,
|
29
|
+
}
|
30
|
+
}
|
31
|
+
}
|
@@ -3,8 +3,10 @@ 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
|
+
import { Transaction } from "@/db";
|
9
|
+
import { TransactionStatusDialProtocol } from "./dial/TransactionStatusDialProtocol";
|
8
10
|
|
9
11
|
export interface ProtocolOptions {
|
10
12
|
/* Handshake timeout in ms (default: 8000) */
|
@@ -33,7 +35,12 @@ interface PeerInfoEvent extends BaseMessageEvent {
|
|
33
35
|
data: Omit<IPeerInfo, 'id' | 'updated' | 'idle' | 'pooled'>
|
34
36
|
}
|
35
37
|
|
38
|
+
interface TransactionStatusEvent extends BaseMessageEvent {
|
39
|
+
data: Pick<Transaction, 'transactionHash' | 'sourceStatus' | 'sourceTransactionHash' | 'sourceErrors' | 'targetStatus' | 'targetTransactionHash' | 'targetErrors' | 'status'>
|
40
|
+
}
|
41
|
+
|
36
42
|
declare interface Protocol {
|
43
|
+
on(event: 'TransactionStatus', listener: (payload: TransactionStatusEvent) => void): this;
|
37
44
|
on(event: 'PeerInfo', listener: (payload: PeerInfoEvent) => void): this;
|
38
45
|
on(event: string, listener: (payload: BaseMessageEvent) => void): this;
|
39
46
|
}
|
@@ -44,7 +51,7 @@ class Protocol extends EventEmitter {
|
|
44
51
|
private protocolMessages: Message[] = [
|
45
52
|
{
|
46
53
|
name: 'PeerInfo',
|
47
|
-
code:
|
54
|
+
code: 0x01,
|
48
55
|
encode: (info: Pick<IPeerInfo, 'publicAddress'>) => [
|
49
56
|
Buffer.from(info.publicAddress),
|
50
57
|
],
|
@@ -52,13 +59,44 @@ class Protocol extends EventEmitter {
|
|
52
59
|
publicAddress: publicAddress.toString(),
|
53
60
|
}),
|
54
61
|
},
|
62
|
+
{
|
63
|
+
name: 'TransactionStatus',
|
64
|
+
code: 0x02,
|
65
|
+
encode: (transaction: Transaction) => [
|
66
|
+
Buffer.from(transaction.transactionHash),
|
67
|
+
|
68
|
+
Buffer.from(transaction.sourceStatus),
|
69
|
+
Buffer.from(transaction.sourceTransactionHash || ''),
|
70
|
+
transaction.sourceErrors ? transaction.sourceErrors.map((e) => Buffer.from(e)) : [],
|
71
|
+
|
72
|
+
Buffer.from(transaction.targetStatus),
|
73
|
+
Buffer.from(transaction.targetTransactionHash || ''),
|
74
|
+
transaction.targetErrors ? transaction.targetErrors.map((e) => Buffer.from(e)) : [],
|
75
|
+
|
76
|
+
Buffer.from(transaction.status),
|
77
|
+
],
|
78
|
+
decode: ([transactionHash, sourceStatus, sourceTransactionHash, sourceErrors, targetStatus, targetTransactionHash, targetErrors, status]: [Buffer, Buffer, Buffer, Buffer[], Buffer, Buffer, Buffer[], Buffer]) => ({
|
79
|
+
transactionHash: transactionHash.toString(),
|
80
|
+
|
81
|
+
sourceStatus: sourceStatus.toString(),
|
82
|
+
sourceTransactionHash: sourceTransactionHash.toString() || null,
|
83
|
+
sourceErrors: sourceErrors.map((e) => e.toString()),
|
84
|
+
|
85
|
+
targetStatus: targetStatus.toString(),
|
86
|
+
targetTransactionHash: targetTransactionHash.toString()|| null,
|
87
|
+
targetErrors: targetErrors.map((e) => e.toString()),
|
88
|
+
|
89
|
+
status: status.toString(),
|
90
|
+
}),
|
91
|
+
},
|
55
92
|
];
|
56
93
|
private signature: SignatureDialProtocol;
|
94
|
+
private transactionStatus: TransactionStatusDialProtocol;
|
57
95
|
|
58
96
|
|
59
97
|
start({ libp2p, topic = null, }) {
|
60
98
|
this.libp2p = libp2p
|
61
|
-
this.topic = topic || 'protocol'
|
99
|
+
this.topic = topic || 'itnerop-x-protocol'
|
62
100
|
|
63
101
|
if (this.libp2p.isStarted()) this.init()
|
64
102
|
|
@@ -73,6 +111,7 @@ class Protocol extends EventEmitter {
|
|
73
111
|
})
|
74
112
|
|
75
113
|
this.signature = new SignatureDialProtocol(this.libp2p);
|
114
|
+
this.transactionStatus = new TransactionStatusDialProtocol(this.libp2p);
|
76
115
|
}
|
77
116
|
|
78
117
|
|
@@ -121,6 +160,14 @@ class Protocol extends EventEmitter {
|
|
121
160
|
this.libp2p.pubsub.publish(this.topic, encoded)
|
122
161
|
}
|
123
162
|
|
163
|
+
public sendTransaction(transaction: Transaction) {
|
164
|
+
const message = this.protocolMessages.find((m) => m.name === 'TransactionStatus')!
|
165
|
+
|
166
|
+
const encoded = rlp.encode([message.code, message.encode(transaction)]);
|
167
|
+
|
168
|
+
this.libp2p.pubsub.publish(this.topic, encoded)
|
169
|
+
}
|
170
|
+
|
124
171
|
async requestSignatures(data: ISignatureRequest, peerIds?: string[]) {
|
125
172
|
try {
|
126
173
|
peerIds = peerIds || peerPool.activePeerIds;
|
@@ -133,6 +180,15 @@ class Protocol extends EventEmitter {
|
|
133
180
|
return []
|
134
181
|
}
|
135
182
|
}
|
183
|
+
|
184
|
+
async requestTransactionStatus(transactionHash: string, peerId: string) {
|
185
|
+
try {
|
186
|
+
return await this.transactionStatus.send(transactionHash, peerId);
|
187
|
+
} catch (error) {
|
188
|
+
console.log(error);
|
189
|
+
return null
|
190
|
+
}
|
191
|
+
}
|
136
192
|
}
|
137
193
|
|
138
194
|
export const protocol = new Protocol();
|
@@ -0,0 +1,82 @@
|
|
1
|
+
import { BaseTask } from "./BaseTask";
|
2
|
+
import Logger from '@/logger';
|
3
|
+
import spawnAsync from 'await-spawn';
|
4
|
+
import { spawn } from 'child_process'
|
5
|
+
import config from "@/config";
|
6
|
+
import wait from "waait";
|
7
|
+
import packageJson from "../../package.json";
|
8
|
+
|
9
|
+
const currentVersion = packageJson.version;
|
10
|
+
const tag = config.staging ? 'dev' : 'latest';
|
11
|
+
|
12
|
+
class AutoUpdateTask extends BaseTask {
|
13
|
+
pollIntervalMs: number = 60 * 10 * 1000
|
14
|
+
|
15
|
+
constructor() {
|
16
|
+
super({
|
17
|
+
logger: new Logger("AutoUpdateTask"),
|
18
|
+
})
|
19
|
+
}
|
20
|
+
|
21
|
+
prePollHandler(): boolean {
|
22
|
+
return config.autoUpdate && !config.isLeadNode();
|
23
|
+
}
|
24
|
+
|
25
|
+
async getInstalledVersion() {
|
26
|
+
try {
|
27
|
+
const stdout = await spawnAsync('npm', ['-g', 'ls', '--depth=0', '--json'])
|
28
|
+
return JSON.parse(stdout.toString()).dependencies[packageJson.name].version
|
29
|
+
} catch (error) {
|
30
|
+
this.logger.error(error)
|
31
|
+
return currentVersion
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
async getLatestVersion() {
|
36
|
+
try {
|
37
|
+
const stdout = await spawnAsync('npm', ['view', `${packageJson.name}@${tag}`, 'version'])
|
38
|
+
return stdout.toString().trim()
|
39
|
+
} catch (error) {
|
40
|
+
this.logger.error(error)
|
41
|
+
return currentVersion
|
42
|
+
}
|
43
|
+
}
|
44
|
+
|
45
|
+
async pollHandler() {
|
46
|
+
const version = await this.getLatestVersion()
|
47
|
+
|
48
|
+
if (version === currentVersion) {
|
49
|
+
return;
|
50
|
+
}
|
51
|
+
|
52
|
+
this.logger.warn(`New version ${version} available.`)
|
53
|
+
|
54
|
+
this.logger.info('Updating...')
|
55
|
+
|
56
|
+
await spawnAsync('npm', ['-g', 'install', `@instadapp/interop-x@${tag}`, '-f']);
|
57
|
+
|
58
|
+
await wait(5000)
|
59
|
+
|
60
|
+
if (version !== await this.getInstalledVersion()) {
|
61
|
+
this.logger.warn(`failed to install ${version}, retrying in 5 minutes`)
|
62
|
+
return;
|
63
|
+
}
|
64
|
+
|
65
|
+
this.logger.warn(`Installed version ${version}`)
|
66
|
+
this.logger.warn(`Restarting...`)
|
67
|
+
|
68
|
+
|
69
|
+
// TODO: its restarting in the bg, but it should be in the fg
|
70
|
+
const subprocess = spawn(process.argv[0], process.argv.slice(1), {
|
71
|
+
cwd: process.cwd(),
|
72
|
+
stdio: "inherit",
|
73
|
+
// shell: process.env.SHELL,
|
74
|
+
});
|
75
|
+
|
76
|
+
subprocess.unref();
|
77
|
+
|
78
|
+
process.exit()
|
79
|
+
}
|
80
|
+
}
|
81
|
+
|
82
|
+
export default AutoUpdateTask;
|
package/src/tasks/BaseTask.ts
CHANGED
@@ -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 "
|
4
|
+
import Logger from "@/logger";
|
6
5
|
|
7
6
|
export interface IBaseTask {
|
8
7
|
pollCheck(): Promise<void>
|
@@ -20,6 +19,7 @@ export class BaseTask extends EventEmitter implements IBaseTask {
|
|
20
19
|
started: boolean = false
|
21
20
|
pollIntervalMs: number = 10 * 1000
|
22
21
|
leadNodeOnly: boolean = false
|
22
|
+
exceptLeadNode: boolean = false
|
23
23
|
|
24
24
|
public constructor({ logger }: { logger?: Logger }) {
|
25
25
|
super()
|
@@ -38,7 +38,7 @@ export class BaseTask extends EventEmitter implements IBaseTask {
|
|
38
38
|
await this.pollHandler()
|
39
39
|
}
|
40
40
|
} catch (err) {
|
41
|
-
this.logger.error(`poll check error
|
41
|
+
this.logger.error(`poll check error:\n${err.message}\ntrace: ${err.stack}`)
|
42
42
|
}
|
43
43
|
|
44
44
|
await this.postPollHandler()
|
@@ -46,11 +46,20 @@ export class BaseTask extends EventEmitter implements IBaseTask {
|
|
46
46
|
}
|
47
47
|
|
48
48
|
prePollHandler(): boolean {
|
49
|
-
if
|
50
|
-
|
49
|
+
if(config.isMaintenanceMode()){
|
50
|
+
this.logger.warn('Maintenance mode is enabled. Skipping task.')
|
51
|
+
return false
|
51
52
|
}
|
52
53
|
|
53
|
-
|
54
|
+
if (this.exceptLeadNode) {
|
55
|
+
return !config.isLeadNode();
|
56
|
+
}
|
57
|
+
|
58
|
+
if (this.leadNodeOnly) {
|
59
|
+
return config.isLeadNode()
|
60
|
+
}
|
61
|
+
|
62
|
+
return true
|
54
63
|
}
|
55
64
|
|
56
65
|
async pollHandler() {
|
@@ -0,0 +1,232 @@
|
|
1
|
+
import { BaseTask } from "../BaseTask";
|
2
|
+
import Logger from '@/logger';
|
3
|
+
import { BigNumber, ethers } from "ethers";
|
4
|
+
import abi from "@/abi";
|
5
|
+
import { Transaction } from "@/db";
|
6
|
+
import { buildSignatureBytes, getContract, getRpcProviderUrl, Signature } from "@/utils";
|
7
|
+
import { addresses } from "@/constants";
|
8
|
+
import { ChainId } from "@/types";
|
9
|
+
import config from "@/config";
|
10
|
+
import { GnosisSafe } from "@/typechain";
|
11
|
+
import { Op } from "sequelize";
|
12
|
+
import wait from "waait";
|
13
|
+
import { peerPool, protocol } from "@/net";
|
14
|
+
import { LogDescription } from "ethers/lib/utils";
|
15
|
+
import { buildGnosisData } from "@/gnosis";
|
16
|
+
|
17
|
+
const generateGnosisTransaction = async (transactionData: any, safeContract: GnosisSafe) => {
|
18
|
+
console.log(transactionData);
|
19
|
+
|
20
|
+
let isExecuted = await safeContract.dataHashes(
|
21
|
+
await safeContract.getTransactionHash(
|
22
|
+
transactionData.to,
|
23
|
+
transactionData.value,
|
24
|
+
transactionData.data,
|
25
|
+
transactionData.operation,
|
26
|
+
transactionData.safeTxGas,
|
27
|
+
transactionData.baseGas,
|
28
|
+
transactionData.gasPrice,
|
29
|
+
transactionData.gasToken,
|
30
|
+
transactionData.refundReceiver,
|
31
|
+
transactionData.nonce
|
32
|
+
)
|
33
|
+
)
|
34
|
+
|
35
|
+
while (isExecuted == 1) {
|
36
|
+
transactionData.safeTxGas = BigNumber.from(String(transactionData.safeTxGas)).add(1).toString()
|
37
|
+
|
38
|
+
isExecuted = await safeContract.dataHashes(
|
39
|
+
await safeContract.getTransactionHash(
|
40
|
+
transactionData.to,
|
41
|
+
transactionData.value,
|
42
|
+
transactionData.data,
|
43
|
+
transactionData.operation,
|
44
|
+
transactionData.safeTxGas,
|
45
|
+
transactionData.baseGas,
|
46
|
+
transactionData.gasPrice,
|
47
|
+
transactionData.gasToken,
|
48
|
+
transactionData.refundReceiver,
|
49
|
+
transactionData.nonce
|
50
|
+
)
|
51
|
+
)
|
52
|
+
}
|
53
|
+
|
54
|
+
return transactionData
|
55
|
+
}
|
56
|
+
|
57
|
+
class ProcessWithdrawEvents extends BaseTask {
|
58
|
+
provider: ethers.providers.JsonRpcProvider;
|
59
|
+
chainId: ChainId;
|
60
|
+
leadNodeOnly = true
|
61
|
+
|
62
|
+
constructor({ chainId }: { chainId: ChainId }) {
|
63
|
+
super({
|
64
|
+
logger: new Logger("InteropXGateway::ProcessWithdrawEvents"),
|
65
|
+
})
|
66
|
+
this.chainId = chainId;
|
67
|
+
}
|
68
|
+
|
69
|
+
async pollHandler() {
|
70
|
+
const blockNumber = await this.provider.getBlockNumber()
|
71
|
+
|
72
|
+
const transaction = await Transaction.findOne({
|
73
|
+
where: {
|
74
|
+
status: 'pending',
|
75
|
+
sourceStatus: 'success',
|
76
|
+
targetStatus: 'uninitialised',
|
77
|
+
action: 'withdraw',
|
78
|
+
sourceCreatedAt: {
|
79
|
+
[Op.gte]: new Date(Date.now() - 12 * 60 * 60 * 1000),
|
80
|
+
},
|
81
|
+
targetDelayUntil: {
|
82
|
+
[Op.or]: {
|
83
|
+
[Op.is]: null,
|
84
|
+
[Op.lt]: new Date(),
|
85
|
+
}
|
86
|
+
},
|
87
|
+
sourceBlockNumber: {
|
88
|
+
[Op.lt]: blockNumber - 12,
|
89
|
+
},
|
90
|
+
sourceChainId: this.chainId,
|
91
|
+
}
|
92
|
+
})
|
93
|
+
|
94
|
+
if (!transaction) {
|
95
|
+
return;
|
96
|
+
}
|
97
|
+
|
98
|
+
console.log(`Processing transaction ${transaction.transactionHash}`);
|
99
|
+
|
100
|
+
transaction.targetStatus = 'pending';
|
101
|
+
await transaction.save();
|
102
|
+
|
103
|
+
// refresh event data?
|
104
|
+
|
105
|
+
const targetChainProvider = new ethers.providers.JsonRpcProvider(
|
106
|
+
getRpcProviderUrl(transaction.targetChainId as ChainId)
|
107
|
+
);
|
108
|
+
|
109
|
+
const targetWallet = new ethers.Wallet(config.privateKey!, targetChainProvider);
|
110
|
+
|
111
|
+
const safeAddress = addresses[transaction.targetChainId].gnosisSafe;
|
112
|
+
|
113
|
+
|
114
|
+
const safeContract = getContract<GnosisSafe>(
|
115
|
+
safeAddress,
|
116
|
+
abi.gnosisSafe,
|
117
|
+
targetWallet
|
118
|
+
)
|
119
|
+
|
120
|
+
const ownersThreshold = await safeContract.getThreshold();
|
121
|
+
await wait(10000);
|
122
|
+
|
123
|
+
let gnosisTx = await generateGnosisTransaction({
|
124
|
+
baseGas: "0",
|
125
|
+
data: await buildGnosisData(transaction),
|
126
|
+
gasPrice: "0",
|
127
|
+
gasToken: "0x0000000000000000000000000000000000000000",
|
128
|
+
nonce: '0',
|
129
|
+
operation: "1",
|
130
|
+
refundReceiver: "0x0000000000000000000000000000000000000000",
|
131
|
+
safeAddress: safeAddress,
|
132
|
+
safeTxGas: "79668",
|
133
|
+
to: addresses[transaction.targetChainId].multisend,
|
134
|
+
value: "0",
|
135
|
+
}, safeContract);
|
136
|
+
|
137
|
+
const owners = await safeContract.getOwners().then(owners => owners.map(owner => owner.toLowerCase()));
|
138
|
+
|
139
|
+
const ownerPeerIds = peerPool.activePeers.filter(peer => owners.includes(peer.publicAddress.toLowerCase())).map(peer => peer.id)
|
140
|
+
|
141
|
+
console.log(`Collecting signatures for execution ${transaction.transactionHash}`)
|
142
|
+
|
143
|
+
console.log(ownerPeerIds);
|
144
|
+
|
145
|
+
const signatures = await protocol.requestSignatures({
|
146
|
+
type: 'source',
|
147
|
+
transactionHash: transaction.transactionHash,
|
148
|
+
safeTxGas: gnosisTx.safeTxGas,
|
149
|
+
safeNonce: gnosisTx.nonce
|
150
|
+
}, ownerPeerIds)
|
151
|
+
|
152
|
+
|
153
|
+
const validSignatures = signatures.filter(s => !!s.data && s.data !== '0x') as Signature[];
|
154
|
+
|
155
|
+
console.log({ signatures, validSignatures, ownersThreshold: ownersThreshold.toString() });
|
156
|
+
|
157
|
+
if (validSignatures.length === 0 || ownersThreshold.gt(validSignatures.length)) {
|
158
|
+
await transaction.save();
|
159
|
+
transaction.targetDelayUntil = new Date(Date.now() + 30 * 1000);
|
160
|
+
transaction.targetStatus = 'uninitialised'
|
161
|
+
|
162
|
+
await transaction.save();
|
163
|
+
const errorMessage = signatures.find(s => !!s.error)?.error;
|
164
|
+
throw new Error(`Not enough signatures` + (errorMessage ? `: ${errorMessage}` : ''));
|
165
|
+
}
|
166
|
+
|
167
|
+
|
168
|
+
console.log(`Executing transaction for execution ${transaction.transactionHash}`)
|
169
|
+
|
170
|
+
const { data: txData } = await safeContract.populateTransaction.execTransaction(
|
171
|
+
gnosisTx.to,
|
172
|
+
gnosisTx.value,
|
173
|
+
gnosisTx.data,
|
174
|
+
gnosisTx.operation,
|
175
|
+
gnosisTx.safeTxGas,
|
176
|
+
gnosisTx.baseGas,
|
177
|
+
gnosisTx.gasPrice,
|
178
|
+
gnosisTx.gasToken,
|
179
|
+
gnosisTx.refundReceiver,
|
180
|
+
buildSignatureBytes(validSignatures)
|
181
|
+
);
|
182
|
+
|
183
|
+
console.log({
|
184
|
+
from: targetWallet.address,
|
185
|
+
gasPrice: BigNumber.from(120 * 10 ** 9).toString(),
|
186
|
+
to: safeAddress,
|
187
|
+
data: txData,
|
188
|
+
})
|
189
|
+
|
190
|
+
|
191
|
+
const txSent = await targetWallet.sendTransaction({
|
192
|
+
from: targetWallet.address,
|
193
|
+
gasPrice: BigNumber.from(120 * 10 ** 9),
|
194
|
+
to: safeAddress,
|
195
|
+
data: txData,
|
196
|
+
})
|
197
|
+
|
198
|
+
const receipt = await txSent.wait();
|
199
|
+
|
200
|
+
const parsedLogs: LogDescription[] = [];
|
201
|
+
|
202
|
+
receipt.logs.forEach((log) => {
|
203
|
+
try {
|
204
|
+
parsedLogs.push(safeContract.interface.parseLog(log));
|
205
|
+
} catch (e) { }
|
206
|
+
});
|
207
|
+
|
208
|
+
if (parsedLogs.find(e => e.name === 'ExecutionSuccess')) {
|
209
|
+
console.log('ExecutionSuccess')
|
210
|
+
transaction.targetStatus = 'success'
|
211
|
+
transaction.targetTransactionHash = txSent.hash
|
212
|
+
transaction.status = 'success'
|
213
|
+
await transaction.save();
|
214
|
+
} else {
|
215
|
+
console.log('ExecutionFailure')
|
216
|
+
transaction.targetStatus = 'failed'
|
217
|
+
transaction.targetTransactionHash = txSent.hash
|
218
|
+
transaction.status = 'failed'
|
219
|
+
await transaction.save();
|
220
|
+
}
|
221
|
+
}
|
222
|
+
|
223
|
+
async start(): Promise<void> {
|
224
|
+
this.provider = new ethers.providers.JsonRpcProvider(
|
225
|
+
getRpcProviderUrl(this.chainId)
|
226
|
+
);
|
227
|
+
|
228
|
+
await super.start()
|
229
|
+
}
|
230
|
+
}
|
231
|
+
|
232
|
+
export default ProcessWithdrawEvents;
|