Skip to content

Commit

Permalink
fix script to deposit
Browse files Browse the repository at this point in the history
  • Loading branch information
toshihiko-okubo committed Nov 12, 2024
1 parent bab0ac6 commit 5dcc926
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 48 deletions.
3 changes: 2 additions & 1 deletion tests/e2e/chains/ethereum/execution/genesis.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
"mergeNetsplitBlock": 0,
"shanghaiTime": 0,
"cancunTime": 0,
"terminalTotalDifficulty": 1,
"terminalTotalDifficulty": 0,
"terminalTotalDifficultyPassed": true,
"depositContractAddress": "0x4242424242424242424242424242424242424242",
"ethash": {}
},
"difficulty": "0x1",
Expand Down
119 changes: 72 additions & 47 deletions tests/e2e/chains/ethereum/tools/deposit/src/deposit.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Command } from 'commander'
import * as fs from 'fs'
import * as process from 'node:process'
import Web3 from 'web3'
import { Contract } from 'web3-eth-contract'
import { AbiItem } from 'web3-utils'
import * as dotenv from 'dotenv'
import DepositABI from '../json/deposit.json'
import DepositABI from './abi/deposit.json'

dotenv.config()
const program = new Command()
Expand All @@ -13,7 +15,6 @@ interface callParams {
privateKey: string
depositContractAddr: string
keyDir: string
mode: string
}

const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000'
Expand Down Expand Up @@ -51,63 +52,87 @@ const checkArgs = (): callParams | undefined => {
privateKey: process.env.PRIVATE_KEY || '',
depositContractAddr: process.env.DEPOSIT_CONTRACT_ADDRESS || '',
keyDir: process.env.KEY_DIR || '',
mode: opts.mode,
}
return callParams
}

const main = async (): Promise<void> => {
const args = checkArgs()
if (args === undefined) throw 'args is invalid'
console.log(`command: ${args.mode}`)

try {
const web3 = new Web3(args.nodeURL)

// PRIVATE_KEY variable defined
web3.eth.accounts.wallet.add(args.privateKey)
const account = web3.eth.accounts.wallet[0].address

switch (args.mode) {
case 'deposit': {
const contractAbi: AbiItem[] = DepositABI as AbiItem[]
const deposit = new web3.eth.Contract(
contractAbi,
args.depositContractAddr
const web3 = new Web3(args.nodeURL)

// PRIVATE_KEY variable defined
web3.eth.accounts.wallet.add(args.privateKey)
const account = web3.eth.accounts.wallet[0].address

const contractAbi: AbiItem[] = DepositABI as AbiItem[]
const deposit: Contract = new web3.eth.Contract(
contractAbi,
args.depositContractAddr
)

// Read deposit_data-*.json from ${KEY_DIR}
const path = `${process.cwd()}/${args.keyDir}`
const jsons = fs.readdirSync(path)
const target = jsons.filter((json) => json.includes('deposit_data-'))
if (target.length !== 1)
throw new Error('deposit_data json file is not found')

const depositData = JSON.parse(
fs.readFileSync(`${path}/${target[0]}`, 'utf8')
)

// call deposit
for (const data of depositData) {
const params = {
pubKey: `0x${data.pubkey}`,
credentials: `0x${data.withdrawal_credentials}`,
signature: `0x${data.signature}`,
depositDataRoot: `0x${data.deposit_data_root}`,
}
console.log(`${JSON.stringify(params, null, 2)}`)

while(true){
try{
await callDeposit(
deposit,
account,
params.pubKey,
params.credentials,
params.signature,
params.depositDataRoot
)

const callDeposit = async (
pubkey: string,
withdrawalCredentials: string,
signature: string,
depositDataRoot: string
) => {
console.log(`call deposit function: pubkey: ${pubkey}`)

const result = await deposit.methods
.deposit(pubkey, withdrawalCredentials, signature, depositDataRoot)
.send({ from: account, gasLimit: 90000, value: 32000000000000000000 });
console.log(result)
}

// Read deposit_data-*.json from ${KEY_DIR}
const path = `${process.cwd()}/${args.keyDir}`
const jsons = fs.readdirSync(path)
const target = jsons.filter(json => json.includes('deposit_data-'))
if (target.length !== 1) throw new Error('deposit_data json file is not found');

const depositData = JSON.parse(fs.readFileSync(`${path}/${target[0]}`, 'utf8'))

// call deposit
for (const data of depositData) {
await callDeposit(`0x${data.pubkey}`, `0x${data.withdrawal_credentials}`, `0x${data.signature}`, `0x${data.deposit_data_root}`)
}
break
} catch (err) {
console.log(err)
await sleep(1000)
}
}
} catch (e) {
console.error(e)
}
}

void main()
const sleep = (ms: number) => new Promise((res) => setTimeout(res, ms));


const callDeposit = async (
depositContract: Contract,
account: string,
pubkey: string,
withdrawalCredentials: string,
signature: string,
depositDataRoot: string
) => {
console.log(`call deposit function: pubkey: ${pubkey}`)

const result = await depositContract.methods
.deposit(pubkey, withdrawalCredentials, signature, depositDataRoot)
.send({ from: account, gasLimit: 90000, value: 32000000000000000000 })
console.log(result)
return result;
}

main().catch((err) => {
console.error(err)
process.exit(1)
})

0 comments on commit 5dcc926

Please sign in to comment.