Skip to content

Commit

Permalink
Add batch deposit script
Browse files Browse the repository at this point in the history
  • Loading branch information
k1rill-fedoseev committed Oct 25, 2021
1 parent a1c7e9e commit 2812065
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.env
.git/
.idea/
build/
coverage/
node_modules/
coverage.json
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"@openzeppelin/contracts": "^4.3.2",
"@truffle/hdwallet-provider": "^1.4.2",
"dotenv": "^10.0.0",
"truffle": "^5.4.3"
"truffle": "^5.4.3",
"web3": "^1.6.0"
},
"devDependencies": {
"chai": "^4.3.4",
Expand Down
10 changes: 10 additions & 0 deletions scripts/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DEPLOYMENT_ACCOUNT_PRIVATE_KEY=0000000000000000000000000000000000000000000000000000000000000000

RPC_URL=https://xdai.poa.network

BATCH_SIZE=128
N=256
OFFSET=0

TOKEN_ADDRESS=0x0000000000000000000000000000000000000000
DEPOSIT_CONTRACT_ADDRESS=0x0000000000000000000000000000000000000000
13 changes: 13 additions & 0 deletions scripts/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM node:14-alpine

WORKDIR /app

COPY package.json yarn.lock ./

RUN yarn install

COPY . .

RUN yarn compile

ENTRYPOINT ["node", "./scripts/deposit.js"]
57 changes: 57 additions & 0 deletions scripts/deposit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const Web3 = require('web3')

const { abi } = require('../build/contracts/IERC677.json')

const { RPC_URL, DEPLOYMENT_ACCOUNT_PRIVATE_KEY, BATCH_SIZE, N, OFFSET, TOKEN_ADDRESS, DEPOSIT_CONTRACT_ADDRESS } = process.env

const web3 = new Web3(RPC_URL)
const { address } = web3.eth.accounts.wallet.add(DEPLOYMENT_ACCOUNT_PRIVATE_KEY)

const depositData = require(process.argv[2])

const batchSize = parseInt(BATCH_SIZE, 10)
const offset = parseInt(OFFSET, 10)
const n = parseInt(N, 10)
async function main() {
const token = new web3.eth.Contract(abi, TOKEN_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 (!deposits.every(d => d.amount === 32000000000)) {
console.log('Amount should be exactly 32 tokens for batch deposits')
return
}

console.log(`Sending ${Math.ceil(deposits.length / batchSize)} deposit transactions for ${deposits.length} deposits in batches of ${batchSize} events`)
let nonce = await web3.eth.getTransactionCount(address)
let count = 0
let data = '0x' + wc
for (let i = 0; i < deposits.length; i++) {
const deposit = deposits[i]
data += deposit.pubkey
data += deposit.signature
data += deposit.deposit_data_root
count++

if (count === batchSize || i === deposits.length - 1) {
const amount = web3.utils.toBN(32 * count).mul(web3.utils.toBN('1000000000000000000'))
const call = token.methods.transferAndCall(DEPOSIT_CONTRACT_ADDRESS, amount, data)
const gas = await call.estimateGas({ from: address })
const receipt = await call.send({
from: address,
nonce: nonce++,
gas: Math.ceil(gas * 1.5),
})
console.log(`\t${count} next deposits: ${receipt.transactionHash}`)
data = '0x' + wc
count = 0
}
}
}

main()
Loading

0 comments on commit 2812065

Please sign in to comment.