-
Notifications
You must be signed in to change notification settings - Fork 75
/
set-stake.js
144 lines (129 loc) · 5.02 KB
/
set-stake.js
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
/* eslint-disable no-console */
import { ethers } from 'ethers';
import { createRequire } from 'module';
import axios from 'axios';
import {
NODE_ENVIRONMENTS,
TRANSACTION_POLLING_TIMEOUT_MILLIS,
TRANSACTION_CONFIRMATIONS,
} from '../src/constants/constants.js';
import validateArguments from './utils.js';
const require = createRequire(import.meta.url);
const Staking = require('dkg-evm-module/abi/Staking.json');
const IdentityStorage = require('dkg-evm-module/abi/IdentityStorage.json');
const ERC20Token = require('dkg-evm-module/abi/Token.json');
const Hub = require('dkg-evm-module/abi/Hub.json');
const argv = require('minimist')(process.argv.slice(1), {
string: [
'stake',
'operationalWalletPrivateKey',
'managementWalletPrivateKey',
'hubContractAddress',
'gasPriceOracleLink',
],
});
const devEnvironment =
process.env.NODE_ENV === NODE_ENVIRONMENTS.DEVELOPMENT ||
process.env.NODE_ENV === NODE_ENVIRONMENTS.TEST;
async function getGasPrice(gasPriceOracleLink, hubContractAddress, provider) {
try {
if (!gasPriceOracleLink) {
if (
hubContractAddress === '0x6C861Cb69300C34DfeF674F7C00E734e840C29C0' ||
hubContractAddress === '0x144eDa5cbf8926327cb2cceef168A121F0E4A299' ||
hubContractAddress === '0xaBfcf2ad1718828E7D3ec20435b0d0b5EAfbDf2c'
) {
return provider.getGasPrice();
}
return devEnvironment ? undefined : 8;
}
let gasPrice;
const response = await axios.get(gasPriceOracleLink);
if (
gasPriceOracleLink === 'https://api.gnosisscan.io/api?module=proxy&action=eth_gasPrice'
) {
gasPrice = Number(response.data.result, 10);
} else if (
gasPriceOracleLink === 'https://blockscout.chiadochain.net/api/v1/gas-price-oracle'
) {
gasPrice = Math.round(response.data.average * 1e9);
} else {
gasPrice = Math.round(response.result * 1e9);
}
this.logger.debug(`Gas price: ${gasPrice}`);
return gasPrice;
} catch (error) {
return undefined;
}
}
async function setStake(
rpcEndpoint,
stake,
operationalWalletPrivateKey,
managementWalletPrivateKey,
hubContractAddress,
gasPriceOracleLink,
) {
const provider = new ethers.providers.JsonRpcProvider(rpcEndpoint);
const operationalWallet = new ethers.Wallet(operationalWalletPrivateKey, provider);
const managementWallet = new ethers.Wallet(managementWalletPrivateKey, provider);
const hubContract = new ethers.Contract(hubContractAddress, Hub, provider);
const stakingContractAddress = await hubContract.getContractAddress('Staking');
const stakingContract = new ethers.Contract(stakingContractAddress, Staking, managementWallet);
const identityStorageAddress = await hubContract.getContractAddress('IdentityStorage');
const identityStorage = new ethers.Contract(identityStorageAddress, IdentityStorage, provider);
const identityId = await identityStorage.getIdentityId(operationalWallet.address);
const tokenContractAddress = await hubContract.getContractAddress('Token');
const tokenContract = new ethers.Contract(tokenContractAddress, ERC20Token, managementWallet);
const stakeWei = ethers.utils.parseEther(stake);
const gasPrice = await getGasPrice(gasPriceOracleLink, hubContractAddress, provider);
let tx = await tokenContract.increaseAllowance(stakingContractAddress, stakeWei, {
gasPrice,
gasLimit: 500_000,
});
await provider.waitForTransaction(
tx.hash,
TRANSACTION_CONFIRMATIONS,
TRANSACTION_POLLING_TIMEOUT_MILLIS,
);
// TODO: Add ABI instead of hard-coded function definition
tx = await stakingContract['addStake(uint72,uint96)'](identityId, stakeWei, {
gasPrice: gasPrice ? gasPrice * 100 : undefined,
gasLimit: 3_000_000,
});
await provider.waitForTransaction(
tx.hash,
TRANSACTION_CONFIRMATIONS,
TRANSACTION_POLLING_TIMEOUT_MILLIS,
);
}
const expectedArguments = [
'rpcEndpoint',
'stake',
'operationalWalletPrivateKey',
'managementWalletPrivateKey',
'hubContractAddress',
];
if (validateArguments(argv, expectedArguments)) {
setStake(
argv.rpcEndpoint,
argv.stake,
argv.operationalWalletPrivateKey,
argv.managementWalletPrivateKey,
argv.hubContractAddress,
argv.gasPriceOracleLink,
)
.then(() => {
console.log('Set stake completed');
process.exit(0);
})
.catch((error) => {
console.log('Error while setting stake. Error: ', error);
process.exit(1);
});
} else {
console.log('Wrong arguments sent in script.');
console.log(
'Example: npm run set-stake -- --rpcEndpoint=<rpc_enpoint> --stake=<stake> --operationalWalletPrivateKey=<private_key> --managementWalletPrivateKey=<private_key> --hubContractAddress=<hub_contract_address>',
);
}