Skip to content

Commit

Permalink
feat: batch deposit script (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
pinebit authored Feb 18, 2025
1 parent 731c363 commit a97d3df
Showing 1 changed file with 63 additions and 13 deletions.
76 changes: 63 additions & 13 deletions script/DepositScript.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,81 @@
pragma solidity ^0.8.19;

import "forge-std/Script.sol";
import "forge-std/console.sol";
import {ObolValidatorManager} from "src/ovm/ObolValidatorManager.sol";

//
// This script calls deposit() on a ObolValidatorManager contract.
// To run this script, the following environment variables must be set:
// - PRIVATE_KEY: the private key of the account that will deploy the contract
// Example usage:
// forge script script/DepositScript.s.sol --sig "run(address)" \
// --rpc-url https://rpc.pectra-devnet-5.ethpandaops.io/ --broadcast "<ovm_address>"
// forge script script/DepositScript.s.sol --sig "run(address,string)" -vvv \
// --rpc-url https://rpc.pectra-devnet-5.ethpandaops.io/ --broadcast "<ovm_address>" "<deposit_file_path>"
//
contract DepositScript is Script {
function run(address ovmAddress) external {
uint256 privKey = vm.envUint("PRIVATE_KEY");
struct DepositData {
// fields must be sorted alphabetically
uint256 amount;
string deposit_cli_version;
string deposit_data_root;
string deposit_message_root;
string fork_version;
string network_name;
string pubkey;
string signature;
string withdrawal_credentials;
}

vm.startBroadcast(privKey);
function run(address ovmAddress, string memory depositFilePath) external {
uint256 privKey = vm.envUint("PRIVATE_KEY");

ObolValidatorManager ovm = ObolValidatorManager(payable(ovmAddress));
console.log("Reading deposit data from file: %s", depositFilePath);

// Update these values from the deposit_data json file
bytes memory pubkey = hex"b9224aa98e2ad166bb4bf31109dfab36a938f5bf7d88c79971227d4ee03d35fd21cea4fc1aa5f87c199e82ceab521842";
bytes memory withdrawal_credentials = hex"01000000000000000000000002a362103abde4c712c27d626195a2a5e442b253";
bytes memory signature = hex"a7c34d447dfdfd71fc43f1605e20545072d2d9ad8df365432686e8482e7b1aadc90a76fc0fbc96d9f359636dcd6902911654bf18e4d71b06c86815b0aef3bd0f187526960e6d92c4a8e9321c62aa4e96440b47b4b827dc9d24eedaca9d4c13df";
bytes32 deposit_data_root = hex"20144d020e7638d583970f708c8abc7c0066b73cf08a23425a6ce48a1222c513";
ovm.deposit{value: 32 ether}(pubkey, withdrawal_credentials, signature, deposit_data_root);
string memory file = vm.readFile(depositFilePath);
bytes memory parsedJson = vm.parseJson(file);
DepositData[] memory depositDatas = abi.decode(parsedJson, (DepositData[]));

vm.stopBroadcast();
console.log("Number of deposit records: %d", depositDatas.length);

vm.startBroadcast(privKey);

uint256 totalAmount;

for (uint256 j = 0; j < depositDatas.length; j++) {
DepositData memory depositData = depositDatas[j];

console.log("Deposit at index %d for amount of %d ether:", j, depositData.amount / 1 gwei);
console.log(" PK: %s", depositData.pubkey);
console.log(" WC: %s", depositData.withdrawal_credentials);

totalAmount += depositData.amount;
}

console.log("Total amount will be deposited: %d ether", totalAmount / 1 gwei);
require(totalAmount > address(this).balance, "You don't have enough balance to deposit");

ObolValidatorManager ovm = ObolValidatorManager(payable(ovmAddress));
console.log("Currently staked amount: %d ether", ovm.amountOfPrincipalStake() / 1 ether);

// Executing deposits...
for (uint256 j = 0; j < depositDatas.length; j++) {
DepositData memory depositData = depositDatas[j];

console.log("Depositing %s for amount of %d ether", depositData.pubkey, depositData.amount / 1 gwei);

bytes memory pubkey = vm.parseBytes(depositData.pubkey);
bytes memory withdrawal_credentials = vm.parseBytes(depositData.withdrawal_credentials);
bytes memory signature = vm.parseBytes(depositData.signature);
bytes32 deposit_data_root = vm.parseBytes32(depositData.deposit_data_root);
ovm.deposit{value: depositData.amount}(pubkey, withdrawal_credentials, signature, deposit_data_root);

console.log("Deposit successful for amount: %d ether", depositData.amount / 1 gwei);
}

console.log("All deposits executed successfully.");

vm.stopBroadcast();
}
}

// forge script script/DepositScript.s.sol --sig "run(address,string)" --rpc-url https://rpc.pectra-devnet-6.ethpandaops.io/ --broadcast "0xAC1598a535a2Bd49a0B9779Ae45E958b3893589c" "./script/data/deposit-data.json"

0 comments on commit a97d3df

Please sign in to comment.