-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator_setup.ts
More file actions
183 lines (162 loc) · 7.93 KB
/
validator_setup.ts
File metadata and controls
183 lines (162 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const { AppLogger } = require('props-lib-logger');
const PrivateKeyProvider = require('truffle-privatekey-provider');
const Web3 = require('web3');
import axios from 'axios';
import config from '../config';
import Utils from '../utils/utils';
import { utils } from 'mocha';
export default class ValidatorSetup {
web3 = new Web3(config.settings.ethereum.uri);
/**
* Setup the validator on ethereum props rewards contract using Gnosis Safe Wallet
*
* @param _pk validator's private key
* @param _multiSigAddress string application name
* @param _name string application name
* @param _rewardsAddress string rewards wallet address
* @param _sidechainAddress string sidechain wallet address
* @param _network string either mainnet or rinkeby - defaults to rinkeby
*/
async setupViaSafe(pk:string, _multiSigAddress:string, _name:string, _rewardsAddress:string, _sidechainAddress:string, _network: string = 'rinkeby') {
let provider;
if (config.settings.ethereum.localhost_test_contract.length > 0) {
provider = new PrivateKeyProvider(pk, 'http://localhost:8545');
} else {
provider = new PrivateKeyProvider(pk, config.settings.ethereum.uri);
}
const web3 = new Web3(provider);
const account = web3.eth.accounts.privateKeyToAccount(`0x${pk}`);
web3.eth.accounts.wallet.add(account);
web3.eth.defaultAccount = account.address;
const tokenContractAddress = config.settings.ethereum.localhost_test_contract.length
> 0 ? config.settings.ethereum.localhost_test_contract : config.settings.ethereum.token_address;
const TokenContract = new web3.eth.Contract(JSON.parse(Utils.abi()), tokenContractAddress);
const multisigWalletABI = require('./GnosisSafe.json');
const multiSigContractInstance = new web3.eth.Contract(multisigWalletABI.abi, _multiSigAddress);
const encodedData = await TokenContract.methods.updateEntity(1, web3.utils.asciiToHex(_name), _rewardsAddress, _sidechainAddress,
).encodeABI();
const zeroAddress = Utils.zeroAddress();
const nonce = await multiSigContractInstance.methods.nonce().call();
const txHash = await multiSigContractInstance.methods.getTransactionHash(tokenContractAddress, 0, encodedData, 0, 0, 0, 0, zeroAddress, zeroAddress, nonce).call();
console.log(`config.settings.ethereum.uri=${config.settings.ethereum.uri}`);
console.log(`tokenContractAddress=${tokenContractAddress}`);
console.log(`config.settings.ethereum.entity_setup_multisig_gas=${config.settings.ethereum.entity_setup_multisig_gas}`);
console.log(`this.web3.utils.toWei(config.settings.ethereum.gas_price, 'gwei')=${this.web3.utils.toWei(config.settings.ethereum.gas_price, 'gwei')}`);
console.log(`account=${account.address},nonce=${nonce},txHash=${txHash},encodedData=${encodedData}`);
try {
await multiSigContractInstance.methods.approveHash(txHash,
).send(
{ from: account.address,
gas: config.settings.ethereum.entity_setup_multisig_gas, gasPrice: this.web3.utils.toWei(config.settings.ethereum.gas_price, 'gwei'),
}).then((receipt) => {
console.log(`receipt=${JSON.stringify(receipt)}`);
}).catch((err) => {
console.log(`err=${JSON.stringify(err)}`);
throw err;
});
} catch (error) {
throw error;
}
const sigs = `0x000000000000000000000000${account.address.replace(
'0x',
'',
)}000000000000000000000000000000000000000000000000000000000000000001`;
const url = _network.toLowerCase() === 'mainnet' ? `https://safe-transaction.gnosis.io/api/v1/safes/${_multiSigAddress}/transactions/` : `https://safe-transaction.rinkeby.gnosis.io/api/v1/safes/${_multiSigAddress}/transactions/`;
const body = {
to: web3.utils.toChecksumAddress(tokenContractAddress),
value: 0,
data: encodedData,
operation: 0,
nonce,
safeTxGas: 0,
baseGas: 0,
gasPrice: 0,
gasToken: zeroAddress,
refundReceiver: zeroAddress,
contractTransactionHash: txHash,
transactionHash: null,
sender: web3.utils.toChecksumAddress(account.address),
origin: null,
sigs,
};
const response = await axios.post(url, body);
console.log(`response=${JSON.stringify(response.data)}, status=${response.status}`);
}
/**
* Setup the validator on ethereum props rewards contract using Gnosis MultiSig Wallet
*
* @param _pk validator's private key
* @param _multiSigAddress string application name
* @param _name string application name
* @param _rewardsAddress string rewards wallet address
* @param _sidechainAddress string sidechain wallet address
*/
async setupViaMultiSig(pk:string, _multiSigAddress:string, _name:string, _rewardsAddress:string, _sidechainAddress:string) {
let provider;
if (config.settings.ethereum.localhost_test_contract.length > 0) {
provider = new PrivateKeyProvider(pk, 'http://localhost:8545');
} else {
provider = new PrivateKeyProvider(pk, config.settings.ethereum.uri);
}
const web3 = new Web3(provider);
const account = web3.eth.accounts.privateKeyToAccount(`0x${pk}`);
console.log(account.address);
const tokenContractAddress = config.settings.ethereum.localhost_test_contract.length
> 0 ? config.settings.ethereum.localhost_test_contract : config.settings.ethereum.token_address;
const TokenContract = new web3.eth.Contract(JSON.parse(Utils.abi()), tokenContractAddress);
const multisigWalletABI = require('./MultiSigWallet.json');
const multiSigContractInstance = new web3.eth.Contract(multisigWalletABI.abi, _multiSigAddress);
const encodedData = await TokenContract.methods.updateEntity(1, web3.utils.asciiToHex(_name), _rewardsAddress, _sidechainAddress,
).encodeABI();
try {
await multiSigContractInstance.methods.submitTransaction(
tokenContractAddress,
0,
encodedData,
).send(
{ from: account.address,
gas: config.settings.ethereum.entity_setup_multisig_gas, gasPrice: this.web3.utils.toWei(config.settings.ethereum.gas_price, 'gwei'),
}).then((receipt) => {
console.log(`receipt=${JSON.stringify(receipt)}`);
}).catch((err) => {
console.log(`err=${JSON.stringify(err)}`);
throw err;
});
} catch (error) {
throw error;
}
}
/**
* Setup the validator on ethereum props rewards contract
*
* @param _name string validator name
* @param _rewardsAddress string rewards wallet address
* @param _sidechainAddress string sidechain wallet address
*/
async setup(_name:string, _rewardsAddress:string, _sidechainAddress:string) {
let provider;
if (config.settings.ethereum.localhost_test_contract.length > 0) {
provider = new PrivateKeyProvider(config.settings.ethereum.validator_pk, 'http://localhost:8545');
} else {
provider = new PrivateKeyProvider(config.settings.ethereum.validator_pk, config.settings.ethereum.uri);
}
const web3 = new Web3(provider);
const account = web3.eth.accounts.privateKeyToAccount(`0x${config.settings.ethereum.validator_pk}`);
console.log(account.address);
const TokenContract = new web3.eth.Contract(JSON.parse(Utils.abi()),config.settings.ethereum.localhost_test_contract.length > 0 ? config.settings.ethereum.localhost_test_contract : config.settings.ethereum.token_address);
try {
await TokenContract.methods.updateEntity(1, web3.utils.asciiToHex(_name), _rewardsAddress, _sidechainAddress).send(
{ from: account.address,
gas: config.settings.ethereum.entity_setup_gas, gasPrice: this.web3.utils.toWei(config.settings.ethereum.gas_price, 'gwei'),
}).then((receipt) => {
console.log(`receipt=${JSON.stringify(receipt)}`);
}).catch((err) => {
console.log(`err=${JSON.stringify(err)}`);
throw err;
});
} catch (error) {
console.log(`error=${JSON.stringify(error)}`);
throw error;
}
}
}