Skip to content

Commit

Permalink
Do not require index because there is only one airdrop active
Browse files Browse the repository at this point in the history
  • Loading branch information
javier123454321 committed Jan 3, 2022
1 parent 35ab501 commit 0ffd82a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
16 changes: 8 additions & 8 deletions contracts/SimpleToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ contract SimpleToken is ERC20, AccessControl {

struct Airdrop {
bytes32 merkleRoot;
bool isFinished;
bool isComplete;
uint256 claimPeriodEnds;
BitMaps.BitMap claimed;
}
Expand Down Expand Up @@ -64,7 +64,7 @@ contract SimpleToken is ERC20, AccessControl {
function newAirdrop(bytes32 _merkleRoot, uint256 _timeLimit) public onlyRole(DEFAULT_ADMIN_ROLE) returns (uint256 airdropId) {
airdropId = numberOfAirdrops;
if(numberOfAirdrops > 0) {
require(airdrops[numberOfAirdrops - 1].isFinished, "Airdrop currently active, creation failed");
require(airdrops[numberOfAirdrops - 1].isComplete, "Airdrop currently active, creation failed");
}
Airdrop storage _drop = airdrops[airdropId];
_drop.merkleRoot = _merkleRoot;
Expand All @@ -79,11 +79,11 @@ contract SimpleToken is ERC20, AccessControl {

/**
* @dev Uses merkle proofs to verify that the amount is equivalent to the user's claim
* @param airdropIndex the index of the airdrop map
* @param claimAmount this must be calculated off chain and can be verified with the merkleProof
* @param merkleProof calculated using MerkleProof.js
*/
function claimTokens(uint256 airdropIndex, uint256 claimAmount, bytes32[] calldata merkleProof) external {
function claimTokens(uint256 claimAmount, bytes32[] calldata merkleProof) external {
uint256 airdropIndex = numberOfAirdrops - 1;
bytes32 leaf = keccak256(abi.encodePacked(msg.sender, claimAmount));
(bool valid, uint256 claimIndex) = MerkleProof.verify(merkleProof, airdrops[airdropIndex].merkleRoot, leaf);
require(valid, "Failed to verify proof");
Expand All @@ -95,9 +95,9 @@ contract SimpleToken is ERC20, AccessControl {
_transfer(address(this), msg.sender, claimAmount);
}

function getAirdropInfo(uint256 _index) public view returns (bytes32 root, uint256 claimPeriodEnds, bool isFinished) {
function getAirdropInfo(uint256 _index) public view returns (bytes32 root, uint256 claimPeriodEnds, bool isComplete) {
root = airdrops[_index].merkleRoot;
isFinished = airdrops[_index].isFinished;
isComplete = airdrops[_index].isComplete;
claimPeriodEnds = airdrops[_index].claimPeriodEnds;
}

Expand All @@ -109,7 +109,7 @@ contract SimpleToken is ERC20, AccessControl {
require(numberOfAirdrops > 0, "No airdrops active");
uint256 claimPeriodEnds = airdrops[numberOfAirdrops - 1].claimPeriodEnds;
require(block.timestamp > claimPeriodEnds, "Airdrop claim period still active");
airdrops[numberOfAirdrops - 1].isFinished = true;
airdrops[numberOfAirdrops - 1].isComplete = true;
emit AirdropComplete(numberOfAirdrops - 1);
}

Expand All @@ -119,7 +119,7 @@ contract SimpleToken is ERC20, AccessControl {
*/
function sweepTokens(address _destination) external onlyRole(DEFAULT_ADMIN_ROLE) {
require(numberOfAirdrops > 0, "No airdrops active");
require(airdrops[numberOfAirdrops - 1].isFinished, "Cannot sweep until airdrop is finished");
require(airdrops[numberOfAirdrops - 1].isComplete, "Cannot sweep until airdrop is finished");
uint256 amountToSweep = balanceOf(address(this));
_transfer(address(this), _destination, amountToSweep);
emit Sweep(_destination, amountToSweep);
Expand Down
14 changes: 7 additions & 7 deletions test/SimpleToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,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(0, expectedBalance, proof);
await simpleToken.connect(addresses[0]).claimTokens(expectedBalance, proof);
expect(await simpleToken.balanceOf(await addresses[0].getAddress())).to.equal(expectedBalance);
})

Expand All @@ -124,7 +124,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 expect(simpleToken.connect(addresses[0]).claimTokens(0, expectedBalance, proof))
await expect(simpleToken.connect(addresses[0]).claimTokens(expectedBalance, proof))
.to.emit(simpleToken, 'Claimed')
.withArgs(await addresses[0].getAddress(), expectedBalance);
})
Expand All @@ -135,9 +135,9 @@ 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(0, expectedBalance, proof);
await simpleToken.connect(addresses[0]).claimTokens(expectedBalance, proof);
await expect(
simpleToken.connect(addresses[0]).claimTokens(0, expectedBalance, proof)
simpleToken.connect(addresses[0]).claimTokens(expectedBalance, proof)
).to.be.revertedWith("Tokens already claimed for this airdrop");
expect(await simpleToken.balanceOf(await addresses[0].getAddress())).to.equal(expectedBalance);
})
Expand All @@ -162,9 +162,9 @@ describe("SimpleToken", () => {
const root = merkleTree.getHexRoot()
await simpleToken.connect(admin1).newAirdrop(root, BigNumber.from("100000000000"))
})
it("should set isFinished to false on creation", async () => {
const { isFinished } = await simpleToken.connect(addresses[0]).getAirdropInfo(0);
expect(isFinished).to.equal(false);
it("should set isComplete to false on creation", async () => {
const { isComplete } = await simpleToken.connect(addresses[0]).getAirdropInfo(0);
expect(isComplete).to.equal(false);
})

it("should not allow you to create a new airdrop if the previous one is not finished", async () => {
Expand Down

0 comments on commit 0ffd82a

Please sign in to comment.