forked from rocket-pool/rocketpool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminipool.js
More file actions
181 lines (141 loc) · 5.9 KB
/
Copy pathminipool.js
File metadata and controls
181 lines (141 loc) · 5.9 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
import {
RocketMinipoolDelegate,
RocketMinipoolManager,
RocketMinipoolFactory,
RocketDAOProtocolSettingsMinipool,
RocketMinipoolStatus,
RocketNetworkPrices,
RocketNodeDeposit,
RocketDAOProtocolSettingsNode,
RocketStorage
} from '../_utils/artifacts';
import { getValidatorPubkey, getValidatorSignature, getDepositDataRoot } from '../_utils/beacon';
// Get the number of minipools a node has
export async function getNodeMinipoolCount(nodeAddress) {
const rocketMinipoolManager = await RocketMinipoolManager.deployed();
let count = await rocketMinipoolManager.getNodeMinipoolCount.call(nodeAddress);
return count;
}
// Get the number of minipools a node has in Staking status
export async function getNodeStakingMinipoolCount(nodeAddress) {
const rocketMinipoolManager = await RocketMinipoolManager.deployed();
let count = await rocketMinipoolManager.getNodeStakingMinipoolCount.call(nodeAddress);
return count;
}
// Get the number of minipools a node has in that are active
export async function getNodeActiveMinipoolCount(nodeAddress) {
const rocketMinipoolManager = await RocketMinipoolManager.deployed();
let count = await rocketMinipoolManager.getNodeActiveMinipoolCount.call(nodeAddress);
return count;
}
// Get the minimum required RPL stake for a minipool
export async function getMinipoolMinimumRPLStake() {
// Load contracts
const [
rocketDAOProtocolSettingsMinipool,
rocketNetworkPrices,
rocketDAOProtocolSettingsNode,
] = await Promise.all([
RocketDAOProtocolSettingsMinipool.deployed(),
RocketNetworkPrices.deployed(),
RocketDAOProtocolSettingsNode.deployed(),
]);
// Load data
let [depositUserAmount, minMinipoolStake, rplPrice] = await Promise.all([
rocketDAOProtocolSettingsMinipool.getHalfDepositUserAmount(),
rocketDAOProtocolSettingsNode.getMinimumPerMinipoolStake(),
rocketNetworkPrices.getRPLPrice(),
]);
// Calculate & return
return depositUserAmount.mul(minMinipoolStake).div(rplPrice);
}
let minipoolSalt = 1
// Create a minipool
export async function createMinipool(txOptions, salt = null) {
// Load contracts
const [
rocketMinipoolFactory,
rocketNodeDeposit,
rocketStorage,
] = await Promise.all([
RocketMinipoolFactory.deployed(),
RocketNodeDeposit.deployed(),
RocketStorage.deployed()
]);
// Get artifact and bytecode
const RocketMinipool = artifacts.require('RocketMinipool');
const contractBytecode = RocketMinipool.bytecode;
// Get deposit type from tx amount
const depositType = await rocketNodeDeposit.getDepositType(txOptions.value);
// Construct creation code for minipool deploy
const constructorArgs = web3.eth.abi.encodeParameters(['address', 'address', 'uint8'], [rocketStorage.address, txOptions.from, depositType]);
const deployCode = contractBytecode + constructorArgs.substr(2);
if(salt === null){
salt = minipoolSalt++;
}
// Calculate keccak(nodeAddress, salt)
const nodeSalt = web3.utils.soliditySha3(
{type: 'address', value: txOptions.from},
{type: 'uint256', value: salt}
)
// Calculate hash of deploy code
const bytecodeHash = web3.utils.soliditySha3(
{type: 'bytes', value: deployCode}
)
// Construct deterministic minipool address
const raw = web3.utils.soliditySha3(
{type: 'bytes1', value: '0xff'},
{type: 'address', value: rocketMinipoolFactory.address},
{type: 'bytes32', value: nodeSalt},
{type: 'bytes32', value: bytecodeHash}
)
const minipoolAddress = raw.substr(raw.length - 40);
let withdrawalCredentials = '0x010000000000000000000000' + minipoolAddress;
// Get validator deposit data
let depositData = {
pubkey: getValidatorPubkey(),
withdrawalCredentials: Buffer.from(withdrawalCredentials.substr(2), 'hex'),
amount: BigInt(16000000000), // gwei
signature: getValidatorSignature(),
};
let depositDataRoot = getDepositDataRoot(depositData);
// Make node deposit
await rocketNodeDeposit.deposit(web3.utils.toWei('0', 'ether'), depositData.pubkey, depositData.signature, depositDataRoot, salt, '0x' + minipoolAddress, txOptions);
return RocketMinipoolDelegate.at('0x' + minipoolAddress);
}
// Refund node ETH from a minipool
export async function refundMinipoolNodeETH(minipool, txOptions) {
await minipool.refund(txOptions);
}
// Progress a minipool to staking
export async function stakeMinipool(minipool, txOptions) {
// Get contracts
const rocketMinipoolManager = await RocketMinipoolManager.deployed()
// Get minipool validator pubkey
const validatorPubkey = await rocketMinipoolManager.getMinipoolPubkey(minipool.address);
// Get minipool withdrawal credentials
let withdrawalCredentials = await rocketMinipoolManager.getMinipoolWithdrawalCredentials.call(minipool.address);
// Get validator deposit data
let depositData = {
pubkey: Buffer.from(validatorPubkey.substr(2), 'hex'),
withdrawalCredentials: Buffer.from(withdrawalCredentials.substr(2), 'hex'),
amount: BigInt(16000000000), // gwei
signature: getValidatorSignature(),
};
let depositDataRoot = getDepositDataRoot(depositData);
// Stake
await minipool.stake(depositData.signature, depositDataRoot, txOptions);
}
// Submit a minipool withdrawable event
export async function submitMinipoolWithdrawable(minipoolAddress, txOptions) {
const rocketMinipoolStatus = await RocketMinipoolStatus.deployed();
await rocketMinipoolStatus.submitMinipoolWithdrawable(minipoolAddress, txOptions);
}
// Dissolve a minipool
export async function dissolveMinipool(minipool, txOptions) {
await minipool.dissolve(txOptions);
}
// Close a dissolved minipool and destroy it
export async function closeMinipool(minipool, txOptions) {
await minipool.close(txOptions);
}