-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathupdateStartingBlock.js
75 lines (62 loc) · 2.64 KB
/
updateStartingBlock.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
require('dotenv').config();
const ethers = require('ethers');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const fs = require('fs');
// Parse command line arguments
const argv = yargs(hideBin(process.argv)).options({
'starting-blocknumber': { type: 'number', default: 0 },
}).argv;
// Environment variables
const rpcEndpoint = process.env.L1_ETH_RPC_HTTP;
const privateKey = process.env.MR_PROVER_KEY;
const contractAddress = process.env.OP_L2OO_ADDR;
// Check if required environment variables are provided
if (!rpcEndpoint || !privateKey || !contractAddress) {
console.error('Error: One or more required environment variables (L1_ETH_RPC_HTTP, MR_PROVER_KEY, OP_L2OO_ADDR) are missing or empty.');
process.exit(1); // Exit the script with an error code
}
async function main() {
const provider = new ethers.getDefaultProvider(rpcEndpoint);
const wallet = new ethers.Wallet(privateKey, provider);
const contractABI = [
{
"type":"function",
"name":"updateStartingBlock",
"inputs":[
{"name":"_startingBlockNumber","type":"uint256","internalType":"uint256"},
{"name":"_startingTimestamp","type":"uint256","internalType":"uint256"}
],
"outputs":[],
"stateMutability":"nonpayable"
}
];
const contract = new ethers.Contract(contractAddress, contractABI, wallet);
const blockNumber = argv['starting-blocknumber'] || 0;
// Fetch the block for timestamp
const block = await provider.getBlock('latest');
const blockTime = block.timestamp;
// Execute the contract function
try {
console.log(`Updating starting block (number=${blockNumber}, time=${blockTime})...`);
const tx = await contract.updateStartingBlock(blockNumber, blockTime);
console.log(`Transaction hash: ${tx.hash}`);
// Wait for 2 confirmations
await tx.wait(2);
console.log('Transaction confirmed');
// Update the JSON file
updateJsonFile(blockNumber, blockTime, block.hash);
} catch (error) {
console.error(`Error executing contract function: ${error.message}`);
}
}
function updateJsonFile(startingBlockNumber, startingTimestamp, blockHash) {
const filePath = './assets/deploy-config.json';
const json = JSON.parse(fs.readFileSync(filePath, 'utf8'));
json.l2OutputOracleStartingBlockNumber = startingBlockNumber;
json.l2OutputOracleStartingTimestamp = startingTimestamp;
json.l1StartingBlockTag = blockHash; // Update with the latest block hash
fs.writeFileSync(filePath, JSON.stringify(json, null, 2)); // Write back with nice formatting
console.log(`Updated ${filePath} with new starting block and timestamp.`);
}
main().catch(console.error);