Skip to content

Commit

Permalink
Fix batch deposit script for different BLS withdrawal credentials (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
k1rill-fedoseev authored Dec 21, 2021
1 parent 9f59f83 commit 702ad72
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
11 changes: 7 additions & 4 deletions scripts/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ RPC_URL=https://dai.poa.network
GAS_PRICE=1000000000

# number of deposits in one transaction, should be in range [1, 128]
# NOTE: put 1 here if withdrawal credentials were generated with the BLS12-381 keys
BATCH_SIZE=128
# total number of deposits to read from file
N=256
# index of the first deposit to read from file
OFFSET=0

# address of the wrapped SBC token
TOKEN_ADDRESS=0x0000000000000000000000000000000000000000
# address of the SBC deposit contract
DEPOSIT_CONTRACT_ADDRESS=0x0000000000000000000000000000000000000000
# address of the wrapped GBC token
TOKEN_ADDRESS=0x722fc4DAABFEaff81b97894fC623f91814a1BF68
# address of the GBC deposit contract
DEPOSIT_CONTRACT_ADDRESS=0x0B98057eA310F4d31F2a452B414647007d1645d9
# block where the deposit contract was deployed at
START_BLOCK_NUMBER=19469077
27 changes: 18 additions & 9 deletions scripts/deposit.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,28 @@ async function main() {
const depositContract = new web3.eth.Contract(depositABI, DEPOSIT_CONTRACT_ADDRESS)
const deposits = depositData.slice(offset, offset + n)

const wc = deposits[0].withdrawal_credentials
if (!deposits.every(d => d.withdrawal_credentials === wc)) {
console.log('Withdrawal credentials do not match')
return
if (batchSize > 1) {
for (let i = 0; i < deposits.length; i += batchSize) {
const wc = deposits[i].withdrawal_credentials
const endIndex = Math.min(i + batchSize, deposits.length)
if (!deposits.slice(i, endIndex).every(d => d.withdrawal_credentials === wc)) {
console.log(`Withdrawal credentials for batch [${i}..${endIndex - 1}] do not match`)
return
}
}
}

if (!deposits.every(d => d.amount === 32000000000)) {
console.log('Amount should be exactly 32 tokens for batch deposits')
return
}

const depositAmountBN = web3.utils.toBN(32).mul(web3.utils.toBN('1000000000000000000'))
const depositAmountBN = web3.utils.toBN(32).mul(web3.utils.toBN('1000000000000000000'))
const totalDepositAmountBN = depositAmountBN.muln(deposits.length)
const tokenBalance = await token.methods.balanceOf(address).call()

if (web3.utils.toBN(tokenBalance).lt(depositAmountBN)) {
console.log(`Token balance is not enough to cover all deposits, have ${tokenBalance}, required ${depositAmountBN.toString()}`)
if (web3.utils.toBN(tokenBalance).lt(totalDepositAmountBN)) {
console.log(`Token balance is not enough to cover all deposits, have ${tokenBalance}, required ${totalDepositAmountBN.toString()}`)
return
}

Expand All @@ -69,9 +75,12 @@ async function main() {
let balance = await web3.eth.getBalance(address).then(web3.utils.toBN)
let nonce = await web3.eth.getTransactionCount(address)
let count = 0
let data = '0x' + wc
let data = '0x'
for (let i = 0; i < deposits.length; i++) {
const deposit = deposits[i]
if (i % batchSize === 0) {
data += deposit.withdrawal_credentials
}
data += deposit.pubkey
data += deposit.signature
data += deposit.deposit_data_root
Expand Down Expand Up @@ -101,7 +110,7 @@ async function main() {
})
balance = balance.sub(web3.utils.toBN(GAS_PRICE).muln(receipt.gasUsed))
console.log(`\t${count} next deposits: ${receipt.transactionHash}`)
data = '0x' + wc
data = '0x'
count = 0
}
}
Expand Down

0 comments on commit 702ad72

Please sign in to comment.