-
Notifications
You must be signed in to change notification settings - Fork 58
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
feat(erc1155): add custom ERC1155 contract for airdrop #262
Changes from all commits
5f9d572
b5831e7
898de67
bcc161f
31eb7fa
d7538d6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// SPDX-License-Identifier: AGPL-3.0-only | ||
pragma solidity ^0.8.0; | ||
|
||
import "@openzeppelin/contracts/access/Ownable.sol"; | ||
import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol"; | ||
|
||
/** | ||
* @title MintableERC1155 | ||
* @notice Ownable contract enabling owner to airdrop many recipients the same token ID at once | ||
*/ | ||
contract MintableERC1155 is ERC1155, Ownable { | ||
// Maps `tokenId` to metadata URI `tokenURI` | ||
mapping(uint256 => string) public _tokenURIs; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should add comment above all state vars including |
||
|
||
event Airdrop(address caller, uint256 tokenId, address[] recipients, uint256 amount); | ||
|
||
// We are passing an empty string as the `baseURI` because we use `_tokenURIs` instead | ||
// to allow for IPFS URIs. | ||
// solhint-disable-next-line | ||
constructor() ERC1155("") {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I might add a comment why you're passing in "" (because we opt to use the _tokenURIs mapping instead) |
||
|
||
/** | ||
* @notice Creates `amount` new tokens for `recipients` of token type `tokenId`. | ||
* @dev Call might run out of gas if `recipients` arg too long. Might need to chunk up the list. | ||
* @param recipients List of airdrop recipients. | ||
* @param tokenId Token type to airdrop. | ||
* @param amount Amount of token types to airdrop. | ||
*/ | ||
function airdrop( | ||
uint256 tokenId, | ||
address[] memory recipients, | ||
uint256 amount | ||
) public onlyOwner { | ||
for (uint256 i = 0; i < recipients.length; i++) { | ||
_mint(recipients[i], tokenId, amount, ""); | ||
} | ||
emit Airdrop(_msgSender(), tokenId, recipients, amount); | ||
} | ||
|
||
/** | ||
* @notice Sets the URI for token of type `tokenId` to `tokenURI`. | ||
* @param tokenId Token type to set `tokenURI` for. | ||
* @param tokenURI URI of token metadata. | ||
*/ | ||
function setTokenURI(uint256 tokenId, string memory tokenURI) external onlyOwner { | ||
require(bytes(_tokenURIs[tokenId]).length == 0, "uri already set"); | ||
|
||
_tokenURIs[tokenId] = tokenURI; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: emit an Event after changing state. Something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes good point. Actually in the spec we should emit There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perfect! |
||
emit URI(tokenURI, tokenId); | ||
} | ||
|
||
/** | ||
* @notice Returns metadata URI of token type `tokenId`. | ||
* @dev Instead of returning the same URI for *all* token types, we return the uri set by | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Add a @notice explaining this function. Can use |
||
* `setTokenURI` to allow IPFS URIs for all token types. | ||
* @param tokenId Token type to retrieve metadata URI for. | ||
*/ | ||
function uri(uint256 tokenId) public view override returns (string memory) { | ||
return _tokenURIs[tokenId]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import "hardhat-deploy"; | ||
import { HardhatRuntimeEnvironment } from "hardhat/types/runtime"; | ||
|
||
const func = async function (hre: HardhatRuntimeEnvironment) { | ||
const { deployments, getNamedAccounts } = hre; | ||
const { deploy } = deployments; | ||
|
||
const { deployer } = await getNamedAccounts(); | ||
|
||
await deploy("MintableERC1155", { | ||
from: deployer, | ||
log: true, | ||
skipIfAlreadyDeployed: true, | ||
args: [], | ||
}); | ||
}; | ||
|
||
module.exports = func; | ||
func.tags = ["MintableERC1155"]; |
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.
nit: add a
@title, @notice
comment above the Contract. Something like:@title MintableERC1155 @notice Ownable contract enabling owner to airdrop many recipients the same token ID at once