Skip to content

Commit

Permalink
Add docs on testing Merkle claims and improve the libs
Browse files Browse the repository at this point in the history
  • Loading branch information
gabmontes committed Apr 15, 2024
1 parent 040c486 commit 70bbc4f
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 32 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,39 @@ Then open the browser at http://localhost:3000.
1. Add it to the `Utilities` component so it appears in the home page.
1. Go from there!

## Testing

### Merkle claims

Go to `packages/merkle-box-lib` and create a list of recipients as a JSON file:

```json
[
{
"account": "0x0000000000000000000000000000000000000010",
"amount": "1000000000000000000"
},
{
"account": "0x0000000000000000000000000000000000000020",
"amount": "2000000000000000000"
}
]
```

Create the dataset:

```sh
node scripts/create-dataset.js recipients.json > dataset.json
```

And copy the data set file to the development web server at `site/public`.

Set `NODE_URL`, `MNEMONIC` and create the claim group:

```sh
node scripts/create-claim-group.js WETH http://localhost:3000/test20241231.json 2024-12-31
```

## End-to-end tests

Set the following environment variables: `BASE_NODE_URL`, `MNEMONIC`.
Expand Down
2 changes: 2 additions & 0 deletions packages/erc-20-lib/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const createErc20 = function (web3, address, options = {}) {
const totalSupply = () => contract.methods.totalSupply().call()

return {
getAddress: () => contract.options.address,

getInfo: () =>
Promise.all([
contract.methods.symbol().call(),
Expand Down
71 changes: 40 additions & 31 deletions packages/merkle-box-lib/scripts/create-claim-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,33 @@ const Web3 = require('web3')

const createMerkleBox = require('..')

const [token, datasetUrl, unlock] = process.argv.slice(2)

const provider = new HDWalletProvider({
addressIndex: Number.parseInt(process.env.ACCOUNT || '0'),
mnemonic: process.env.MNEMONIC || '',
numberOfAddresses: 1,
providerOrUrl: process.env.NODE_URL
})
const from = provider.getAddress(0)
const web3 = new Web3(provider)
const { ACCOUNT = '0', MNEMONIC = '', NODE_URL, PRIVATE_KEYS } = process.env

const memo = `datasetUri=${datasetUrl}`
const [token, datasetUrl, unlock] = process.argv.slice(2)

const toTimestamp = str =>
/^[0-9]+$/.test(str)
? Number.parseInt(str)
: Math.round(new Date(str).getTime() / 1000)
const getContractObjects = () =>
// @ts-ignore ts(2351)
new Web3(NODE_URL).eth.getChainId().then(function (chainId) {
const provider = new HDWalletProvider({
addressIndex: Number.parseInt(ACCOUNT),
chainId,
numberOfAddresses: 1,
providerOrUrl: NODE_URL,
...(PRIVATE_KEYS
? { privateKeys: PRIVATE_KEYS.split(',') }
: { mnemonic: MNEMONIC })
})
const from = provider.getAddress(0)
// @ts-ignore ts(2351)
const web3 = new Web3(provider)
const tokenAddress = token.startsWith('0x')
? token
: createErc20.util.tokenAddress(token, chainId)
const erc20 = createErc20(web3, tokenAddress, { from })
const merkleBoxAddress = createMerkleBox.addresses[chainId]
const merkleBox = createMerkleBox(web3, merkleBoxAddress, { from })
return { erc20, merkleBox, provider }
})

const parseDataSet = () =>
fetch(datasetUrl)
Expand All @@ -53,30 +63,29 @@ const parseDataSet = () =>
}
})

Promise.all([web3.eth.getChainId(), parseDataSet()])
.then(function ([chainId, { root, total }]) {
const tokenAddress = token.startsWith('0x')
? token
: createErc20.util.tokenAddress(token, chainId)
const erc20 = createErc20(web3, tokenAddress, { from })
const merkleBoxAddress = createMerkleBox.addresses[chainId]
const merkleBox = createMerkleBox(web3, merkleBoxAddress, { from })
return erc20
.approve(merkleBoxAddress, total)
const toTimestamp = str =>
/^[0-9]+$/.test(str)
? Number.parseInt(str)
: Math.round(new Date(str).getTime() / 1000)

Promise.all([getContractObjects(), parseDataSet()])
.then(([{ erc20, merkleBox, provider }, { root, total }]) =>
erc20
.approve(merkleBox.getAddress(), total)
.then(() =>
merkleBox.newClaimsGroup(
tokenAddress,
erc20.getAddress(),
total,
root,
toTimestamp(unlock),
memo
`datasetUri=${datasetUrl}`
)
)
})
.finally(function () {
provider.engine.stop()
})
)
.then(function (receipt) {
console.log(receipt.events.NewMerkle.returnValues)
})
.catch(console.error)
.finally(function () {
provider.engine.stop()
})
4 changes: 3 additions & 1 deletion packages/merkle-box-lib/scripts/create-dataset.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
/**
* Usage:
*
* node scripts/create-dataset.js ./recipients.json > dataset.json
* node scripts/create-dataset.js recipients.json > dataset.json
*/

/* eslint-disable no-console */

'use strict'

const createMerkleBox = require('..')
Expand Down
3 changes: 3 additions & 0 deletions packages/merkle-box-lib/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ const createMerkleBox = function (web3, address, options = {}) {
{ from, ...txOps }
)

const getAddress = () => merkleBox.options.address

return {
claim,
getAddress,
getHolding,
isClaimable,
newClaimsGroup
Expand Down

0 comments on commit 70bbc4f

Please sign in to comment.