-
Notifications
You must be signed in to change notification settings - Fork 11
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 #17 from fakenickels/feat/special-kittens-distributor
- Loading branch information
Showing
6 changed files
with
215 additions
and
17 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 |
---|---|---|
@@ -1,42 +1,66 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.4; | ||
import "@openzeppelin/contracts/access/Ownable.sol"; | ||
import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol"; | ||
import "./HonoraryKittens.sol"; | ||
import "./KittensHD.sol"; | ||
|
||
contract SpecialKittensDistributor is Ownable { | ||
contract SpecialKittensDistributor is Ownable, IERC721Receiver { | ||
HonoraryKittens honoraryKittens; | ||
|
||
KittensHD kittensHD; | ||
|
||
mapping(address => bool) public claims; | ||
mapping(uint256 => bool) public claimBitmap; | ||
|
||
constructor(address kittensHDAddress, address honoraryKittensAddress) { | ||
kittensHD = KittensHD(kittensHDAddress); | ||
honoraryKittens = HonoraryKittens(honoraryKittensAddress); | ||
} | ||
|
||
function claim() public { | ||
require(honoraryKittens.balanceOf(msg.sender) > 0); | ||
require(!claims[msg.sender], "You've already claimed"); | ||
require( | ||
honoraryKittens.balanceOf(msg.sender) > 0, | ||
"You don't have any honorary kittens" | ||
); | ||
|
||
// check if the sender already minted a kitten hd due to the KittensHD contract bug | ||
uint256 index = 0; | ||
while (true) { | ||
|
||
// for of kittenHD.balance of msg.sender | ||
for (uint256 index = 0; index < kittensHD.balanceOf(msg.sender); index++) { | ||
uint256 tokenId_ = kittensHD.tokenOfOwnerByIndex(msg.sender, index); | ||
require( | ||
// check if the honorary holder has already minted within the reserved range | ||
tokenId_ < 667 && tokenId_ > 687, | ||
tokenId_ < 667 || tokenId_ > 687, | ||
"You've already claimed your free HD" | ||
); | ||
index++; | ||
} | ||
|
||
claims[msg.sender] = true; | ||
// check if honorary kitten was already "spent" | ||
for ( | ||
uint256 index = 0; | ||
index < honoraryKittens.balanceOf(msg.sender); | ||
index++ | ||
) { | ||
uint256 tokenId_ = honoraryKittens.tokenOfOwnerByIndex(msg.sender, index); | ||
require(!claimBitmap[tokenId_], "You've already claimed this token"); | ||
claimBitmap[tokenId_] = true; | ||
} | ||
|
||
// call daoClaim from KittensHD and transfer to msg.sender | ||
uint256 tokenId = kittensHD.getGeneralMintCounter(); | ||
|
||
kittensHD.unpauseMinting(); | ||
kittensHD.daoAnyClaim(1); | ||
kittensHD.safeTransferFrom(address(this), msg.sender, tokenId); | ||
kittensHD.pauseMinting(); | ||
} | ||
|
||
function onERC721Received( | ||
address operator, | ||
address from, | ||
uint256 tokenId, | ||
bytes calldata data | ||
) external override returns (bytes4) { | ||
return this.onERC721Received.selector; | ||
} | ||
} |
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
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,36 @@ | ||
import React from "react"; | ||
import * as ethers from "ethers"; | ||
import Contract from "../../artifacts/contracts/SpecialKittensDistributor.sol/SpecialKittensDistributor.json"; | ||
|
||
export const useWeb3 = () => { | ||
const provider: React.MutableRefObject< | ||
ethers.providers.Web3Provider | undefined | ||
> = React.useRef(); | ||
const contract: React.MutableRefObject<any | undefined> = React.useRef(); | ||
|
||
React.useEffect(() => { | ||
provider.current = new ethers.providers.Web3Provider(window.ethereum); | ||
const signer = provider.current.getSigner(); | ||
contract.current = new ethers.Contract( | ||
process.env.NEXT_PUBLIC_SPECIAL_DISTRIBUTOR_ADDRESS as string, | ||
Contract.abi as any, | ||
signer | ||
); | ||
}); | ||
|
||
return [provider, contract]; | ||
}; | ||
|
||
export const useSpecialClaim = () => { | ||
const [, contract] = useWeb3(); | ||
|
||
const claimKittens = async () => { | ||
return contract.current.claim().then((res: any) => { | ||
return res.wait(); | ||
}); | ||
}; | ||
|
||
return { | ||
claimKittens, | ||
}; | ||
}; |
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,106 @@ | ||
const chai = require("chai"); | ||
const expect = chai.expect; | ||
const { solidity } = require("ethereum-waffle"); | ||
const merkleRoot = require("../airdrop/kittens-hd-rkitten-airdrop/merkle-root.json"); | ||
|
||
chai.use(solidity); | ||
|
||
describe("KittensHD minter", function () { | ||
it("should deploy and be able to claim", async function () { | ||
this.timeout(50000000000000000000000000000000000000); | ||
const [user] = await ethers.getSigners(); | ||
const deployer = "0x03D7D343bB9008d182FDB0B9100903f0952bf358"; | ||
await hre.network.provider.request({ | ||
method: "hardhat_impersonateAccount", | ||
params: [deployer], | ||
}); | ||
|
||
const signer = await ethers.getSigner(deployer); | ||
|
||
const Contract = await ethers.getContractFactory( | ||
"SpecialKittensDistributor" | ||
); | ||
|
||
const kittensHDAddress = "0xad956DF38D04A9A555E079Cf5f3fA59CB0a25DC9"; | ||
const honoraryKittensAddress = "0xE65469083B4f50d1EcD089584c671Bb1d23F9AC7"; | ||
|
||
console.log("Deploying contract..."); | ||
const contract = await Contract.deploy( | ||
kittensHDAddress, | ||
honoraryKittensAddress | ||
); | ||
|
||
console.log("Initializing kittens hd"); | ||
const kittensHDContract = await ethers.getContractAt( | ||
"KittensHD", | ||
kittensHDAddress, | ||
signer | ||
); | ||
|
||
// setup merkle distributor | ||
console.log("Granting role..."); | ||
await kittensHDContract.grantDAOMemberRole(contract.address); | ||
|
||
console.log("Pausing minting..."); | ||
await kittensHDContract.pauseMinting(); | ||
|
||
const honoraryHolderAddress = "0x0fdBAeCF59193e8Ff96d9b392B8C24E892AE2b26"; | ||
await hre.network.provider.request({ | ||
method: "hardhat_impersonateAccount", | ||
params: [honoraryHolderAddress], | ||
}); | ||
|
||
await network.provider.send("hardhat_setBalance", [ | ||
honoraryHolderAddress, | ||
"0xffffffffffffffff", | ||
]); | ||
|
||
const honoraryHolder = await ethers.getSigner(honoraryHolderAddress); | ||
|
||
const receipt = await contract | ||
.connect(honoraryHolder) | ||
.claim() | ||
.then((receipt) => receipt.hash) | ||
.catch((e) => e.message); | ||
|
||
expect(receipt).to.not.contain( | ||
`VM Exception while processing transaction: reverted with reason string` | ||
); | ||
expect(receipt).to.not.contain(`Transaction reverted:`); | ||
|
||
const balance = await kittensHDContract.balanceOf(honoraryHolderAddress); | ||
|
||
expect(balance).to.equal(1); | ||
|
||
const shouldFailReceipt = await contract | ||
.connect(honoraryHolder) | ||
.claim() | ||
.then((receipt) => receipt.hash) | ||
.catch((e) => e.message); | ||
// should not be able to claim twice for a honorary kitten ticket | ||
expect(shouldFailReceipt).to.contain(`You've already claimed this token`); | ||
// not honorary holder = fail | ||
expect( | ||
await contract | ||
.connect(user) | ||
.claim() | ||
.then((receipt) => receipt.hash) | ||
.catch((e) => e.message) | ||
).to.contain(`You don't have any honorary kittens`); | ||
|
||
const incidentMinterAddress = "0x9945daF5dCd39C9A3556d27c16af343765e5630C"; | ||
await hre.network.provider.request({ | ||
method: "hardhat_impersonateAccount", | ||
params: [incidentMinterAddress], | ||
}); | ||
|
||
const incidentMinter = await ethers.getSigner(incidentMinterAddress); | ||
expect( | ||
await contract | ||
.connect(incidentMinter) | ||
.claim() | ||
.then((receipt) => receipt.hash) | ||
.catch((e) => e.message) | ||
).to.contain(`You've already claimed your free HD`); | ||
}); | ||
}); |
1304687
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: