Skip to content

Commit

Permalink
ValorizeDAO#15: add merkle tree verification
Browse files Browse the repository at this point in the history
  • Loading branch information
javier123454321 committed Dec 21, 2021
1 parent 3947407 commit b39eb41
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
8 changes: 4 additions & 4 deletions contracts/SimpleToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ contract SimpleToken is ERC20, AccessControl {
bytes32 public merkleRoot;

event MerkleRootChanged(bytes32 merkleRoot);
event Claimed(address claimant, uint256 amount);

/**
* @dev Constructor.
Expand Down Expand Up @@ -55,11 +56,10 @@ contract SimpleToken is ERC20, AccessControl {
function claimTokens(uint256 amount, bytes32[] calldata merkleProof) external {
bytes32 leaf = keccak256(abi.encodePacked(msg.sender, amount));
(bool valid, uint256 index) = MerkleProof.verify(merkleProof, merkleRoot, leaf);
//require(valid, "Failed to verify proof");
//require(!isClaimed(index), "Tokens already claimed");
require(valid, "Failed to verify proof");

//emit Claim(msg.sender, amount);
emit Claimed(msg.sender, amount);

//_transfer(address(this), msg.sender, amount));
_transfer(address(this), msg.sender, amount);
}
}
18 changes: 10 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 11 additions & 4 deletions test/SimpleToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import { solidity } from "ethereum-waffle";
import { getAddress } from "@ethersproject/address";
import { SimpleToken } from './../typechain/SimpleToken.d';
import { SimpleTokenFactory } from './../typechain/SimpleTokenFactory';
import { MerkleTree } from "merkletreejs";
import { MerkleTree } from 'merkletreejs';
import { keccak_256 } from "js-sha3";



chai.use(solidity);

const { expect } = chai;
Expand Down Expand Up @@ -75,12 +77,17 @@ describe("SimpleToken", () => {
let merkleTree: MerkleTree
beforeEach(async () => {
await setupSimpleToken()
const nodeToHash = (baseNode: (String | BigNumber)[]) => ethers.utils.solidityKeccak256(['address', 'uint256'], [baseNode[0], baseNode[1]])
const hashingFunction = (hash: any) => { console.log({ hash }); ethers.utils.solidityKeccak256(['bytes32'], [hash]) }
const leaves = [
[await addresses[0].getAddress(), BigNumber.from("1000000000000000000000")],
[await addresses[1].getAddress(), BigNumber.from("2000000000000000000000")],
[await addresses[2].getAddress(), BigNumber.from("2000000000000000000000")],
].map(v => ethers.utils.solidityKeccak256(['address', 'uint256'], [v[0], v[1]]))
merkleTree = new MerkleTree(leaves, keccak_256)
].map(nodeToHash)
merkleTree = new MerkleTree(leaves, keccak_256, { sort: true })
const leaf = ethers.utils.solidityKeccak256(['address', 'uint256'], [await addresses[0].getAddress(), BigNumber.from("1000000000000000000000")])
const proof = merkleTree.getHexProof(leaf)
const root = merkleTree.getHexRoot()
})

it("should allow admin to set merkle Tree Root for airdrops", async () => {
Expand Down Expand Up @@ -114,7 +121,7 @@ describe("SimpleToken", () => {
const expectedBalance = BigNumber.from("1000000000000000000000")
const leaf = ethers.utils.solidityKeccak256(['address', 'uint256'], [await addresses[0].getAddress(), expectedBalance])
const proof = merkleTree.getHexProof(leaf)
await simpleToken.connect(addresses[0]).claimTokens(expectedBalance, proof);
const tx = await simpleToken.connect(addresses[0]).claimTokens(expectedBalance, proof);
await expect(await simpleToken.balanceOf(await addresses[0].getAddress())).to.equal(expectedBalance);
})
})
Expand Down

0 comments on commit b39eb41

Please sign in to comment.