Skip to content

Commit

Permalink
Merge pull request #674 from clrfund/feat/bulkload-simple-recipients
Browse files Browse the repository at this point in the history
Script to bulkload users into the the simple user registry
  • Loading branch information
yuetloo authored Jun 14, 2023
2 parents 7890100 + 8afab42 commit cebf251
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
1 change: 1 addition & 0 deletions contracts/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ import './fetchRound'
import './mergeAllocations'
import './setDurations'
import './deploySponsor'
import './loadUsers'
86 changes: 86 additions & 0 deletions contracts/tasks/loadUsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { task } from 'hardhat/config'
import { Contract, utils, ContractReceipt } from 'ethers'
import fs from 'fs'

/*
* Script to bulkload users into the simple user registry.
* File path can be relative or absolute path.
* The script can only be run by the owner of the simple user registry
*
* Sample usage:
*
* yarn hardhat load-users --file-path addresses.txt --user-registry <address> --network goerli
*/

/**
* Add a user to the Simple user registry
*
* @param registry Simple user registry contract
* @param address User wallet address
* @returns transaction receipt
*/
async function addUser(
registry: Contract,
address: string
): Promise<ContractReceipt> {
const tx = await registry.addUser(address)
const receipt = await tx.wait()
return receipt
}

/**
* Load users in the file into the simple user registry
*
* @param registry Simple user registry contract
* @param filePath The path of the file containing the addresses
*/
async function loadFile(registry: Contract, filePath: string) {
let content: string | null = null
try {
content = fs.readFileSync(filePath, 'utf8')
} catch (err) {
console.error('Failed to read file', filePath, err)
return
}

const addresses: string[] = []
content.split(/\r?\n/).forEach(async (address) => {
addresses.push(address)
})

for (let i = 0; i < addresses.length; i++) {
const address = addresses[i]
const isValidAddress = Boolean(address) && utils.isAddress(address)
if (isValidAddress) {
console.log('Adding address', address)
try {
const result = await addUser(registry, address)
if (result.status !== 1) {
throw new Error(
`Transaction ${result.transactionHash} failed with status ${result.status}`
)
}
} catch (err) {
console.error('Failed to add address', address, err)
break
}
} else {
console.warn('Skipping invalid address', address)
}
}
}

task('load-users', 'Bulkload recipients into the simple user registry')
.addParam('userRegistry', 'The simple user registry contract address')
.addParam(
'filePath',
'The path of the file containing addresses separated by newline'
)
.setAction(async ({ userRegistry, filePath }, { ethers }) => {
const registry = await ethers.getContractAt(
'SimpleUserRegistry',
userRegistry
)

await loadFile(registry, filePath)
})

0 comments on commit cebf251

Please sign in to comment.