generated from proofoftom/buidler-waffle-typechain-quasar
-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #674 from clrfund/feat/bulkload-simple-recipients
Script to bulkload users into the the simple user registry
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |