Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix tests for claimable airdrops #362

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
214 changes: 214 additions & 0 deletions contracts/airdrop/BearAirdropERC1155Claimable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.11;

// ========== External imports ==========
import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";

import "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/MulticallUpgradeable.sol";

// ========== Internal imports ==========

import "../interfaces/airdrop/IAirdropERC1155Claimable.sol";

// ========== Features ==========
import "../extension/Ownable.sol";

import "../openzeppelin-presets/metatx/ERC2771ContextUpgradeable.sol";
import "../lib/MerkleProof.sol";
import { AirdropERC1155Claimable } from "./AirdropERC1155Claimable.sol";

contract BearAirdropERC1155Claimable is
Initializable,
Ownable,
ReentrancyGuardUpgradeable,
ERC2771ContextUpgradeable,
MulticallUpgradeable,
IAirdropERC1155Claimable
{
/*///////////////////////////////////////////////////////////////
State variables
//////////////////////////////////////////////////////////////*/

bytes32 private constant MODULE_TYPE = bytes32("BearAirdropERC1155Claimable");
uint256 private constant VERSION = 1;

/// @dev address of token being airdropped.
address public airdropTokenAddress;

/// @dev address of owner of tokens being airdropped.
address public tokenOwner;

/// @dev list of tokens to airdrop.
uint256[] public tokenIds;

/// @dev airdrop expiration timestamp.
uint256 public expirationTimestamp;

/*///////////////////////////////////////////////////////////////
Mappings
//////////////////////////////////////////////////////////////*/

/// @dev Mapping from tokenId and claimer address to total number of tokens claimed.
mapping(uint256 => mapping(address => uint256)) public supplyClaimedByWallet;

/// @dev general claim limit for a tokenId if claimer not in allowlist.
mapping(uint256 => uint256) public maxWalletClaimCount;

/// @dev number tokens available to claim for a tokenId.
mapping(uint256 => uint256) public availableAmount;

/// @dev mapping of tokenId to merkle root of the allowlist of addresses eligible to claim.
mapping(uint256 => bytes32) public merkleRoot;

address public immutable OldContract;

Check failure on line 64 in contracts/airdrop/BearAirdropERC1155Claimable.sol

View workflow job for this annotation

GitHub Actions / lint

Variable name must be in mixedCase

/*///////////////////////////////////////////////////////////////
Constructor + initializer logic
//////////////////////////////////////////////////////////////*/

constructor(address _oldContract) {
OldContract = _oldContract;
}

/// @dev Initiliazes the contract, like a constructor.
function initialize(
address _defaultAdmin,
address[] memory _trustedForwarders,
address _tokenOwner,
address _airdropTokenAddress,
uint256[] memory _tokenIds,
uint256[] memory _availableAmounts,
uint256,
uint256[] memory,
bytes32[] memory _merkleRoot
) external initializer {
_setupOwner(_defaultAdmin);
__ReentrancyGuard_init();
__ERC2771Context_init(_trustedForwarders);

tokenOwner = _tokenOwner;
airdropTokenAddress = _airdropTokenAddress;
tokenIds = _tokenIds;
expirationTimestamp = AirdropERC1155Claimable(OldContract).expirationTimestamp();

require(
_merkleRoot.length == _tokenIds.length && _availableAmounts.length == _tokenIds.length,
"length mismatch."
);

for (uint256 i = 0; i < _tokenIds.length; i++) {
merkleRoot[_tokenIds[i]] = _merkleRoot[i];
maxWalletClaimCount[_tokenIds[i]] = 0;
availableAmount[_tokenIds[i]] = _availableAmounts[i];
}
}

/*///////////////////////////////////////////////////////////////
Generic contract logic
//////////////////////////////////////////////////////////////*/

/// @dev Returns the type of the contract.
function contractType() external pure returns (bytes32) {
return MODULE_TYPE;
}

/// @dev Returns the version of the contract.
function contractVersion() external pure returns (uint8) {
return uint8(VERSION);
}

/*///////////////////////////////////////////////////////////////
Claim logic
//////////////////////////////////////////////////////////////*/

/**
* @notice Lets an account claim a given quantity of ERC1155 tokens.
*
* @param _receiver The receiver of the tokens to claim.
* @param _quantity The quantity of tokens to claim.
* @param _tokenId Token Id to claim.
* @param _proofs The proof of the claimer's inclusion in the merkle root allowlist
* of the claim conditions that apply.
* @param _proofMaxQuantityForWallet The maximum number of tokens an address included in an
* allowlist can claim.
*/
function claim(
address _receiver,
uint256 _quantity,
uint256 _tokenId,
bytes32[] calldata _proofs,
uint256 _proofMaxQuantityForWallet
) external nonReentrant {
address claimer = _msgSender();

verifyClaim(claimer, _quantity, _tokenId, _proofs, _proofMaxQuantityForWallet);

_transferClaimedTokens(_receiver, _quantity, _tokenId);

emit TokensClaimed(_msgSender(), _receiver, _tokenId, _quantity);
}

/// @dev Transfers the tokens being claimed.
function _transferClaimedTokens(
address _to,
uint256 _quantityBeingClaimed,
uint256 _tokenId
) internal {
// if transfer claimed tokens is called when `to != msg.sender`, it'd use msg.sender's limits.
// behavior would be similar to `msg.sender` mint for itself, then transfer to `_to`.
supplyClaimedByWallet[_tokenId][_msgSender()] += _quantityBeingClaimed;
availableAmount[_tokenId] -= _quantityBeingClaimed;

IERC1155(airdropTokenAddress).safeTransferFrom(tokenOwner, _to, _tokenId, _quantityBeingClaimed, "");
}

/// @dev Checks a request to claim tokens against the active claim condition's criteria.
function verifyClaim(
address _claimer,
uint256 _quantity,
uint256 _tokenId,
bytes32[] calldata _proofs,
uint256 _proofMaxQuantityForWallet
) public view {
uint256 supplyClaimedBefore = AirdropERC1155Claimable(OldContract).supplyClaimedByWallet(_tokenId, _claimer);
bool isOverride;

bytes32 mroot = merkleRoot[_tokenId];
if (mroot != bytes32(0)) {
(isOverride, ) = MerkleProof.verify(
_proofs,
mroot,
keccak256(abi.encodePacked(_claimer, _proofMaxQuantityForWallet))
);
}

uint256 supplyClaimedAlready = supplyClaimedByWallet[_tokenId][_claimer];

require(_quantity > 0, "Claiming zero tokens");
require(_quantity <= availableAmount[_tokenId], "exceeds available tokens.");

uint256 expTimestamp = expirationTimestamp;
require(expTimestamp == 0 || block.timestamp < expTimestamp, "airdrop expired.");

uint256 claimLimitForWallet = isOverride ? _proofMaxQuantityForWallet : maxWalletClaimCount[_tokenId];
require(_quantity + supplyClaimedAlready + supplyClaimedBefore <= claimLimitForWallet, "invalid quantity.");
}

/*///////////////////////////////////////////////////////////////
Miscellaneous
//////////////////////////////////////////////////////////////*/

/// @dev Returns whether owner can be set in the given execution context.
function _canSetOwner() internal view virtual override returns (bool) {
return _msgSender() == owner();
}

function _msgSender() internal view virtual override returns (address sender) {
return ERC2771ContextUpgradeable._msgSender();
}

function _msgData() internal view virtual override returns (bytes calldata) {
return ERC2771ContextUpgradeable._msgData();
}
}
2 changes: 1 addition & 1 deletion lib/ds-test
2 changes: 1 addition & 1 deletion lib/forge-std
Loading
Loading