Skip to content

Commit

Permalink
Merge pull request #541 from yawn-c111/feature/referral-point
Browse files Browse the repository at this point in the history
update: referral point
  • Loading branch information
yu23ki14 authored Mar 17, 2024
2 parents fb3d12c + 8f9757b commit 99ffef1
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
7 changes: 5 additions & 2 deletions hardhat/contracts/IMintPoint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ pragma solidity ^0.8.19;
interface IMintPoint {

event Register(address indexed account, uint256 indexed id);
event Mint(address indexed account, uint256 indexed id);
event MintByMinter(address indexed account, uint256 indexed id);
event MintForReferral(address indexed from, address indexed to, uint256 indexed id);
event Transfer(address indexed operator, address indexed from, address indexed to, uint256[] ids, uint256[] values, uint256 timestamp);


function supportsInterface(bytes4 interfaceId) external view returns (bool);

function register() external;

function mint(address to, uint256 id, uint256 amount) external;
function mintByMinter(address to, uint256 id, uint256 amount) external;

function mintForReferral(address to, uint256 amount) external;

function burn(uint256 tokenId, uint256 amount) external;
}
53 changes: 50 additions & 3 deletions hardhat/contracts/MintPoint.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,34 @@ pragma solidity ^0.8.19;
import "@openzeppelin/contracts/utils/Base64.sol";
import {ERC1155Upgradeable, ERC1155SupplyUpgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC1155/extensions/ERC1155SupplyUpgradeable.sol";
import {AccessControlEnumerableUpgradeable} from "@openzeppelin/contracts-upgradeable/access/AccessControlEnumerableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/metatx/MinimalForwarderUpgradeable.sol";
import "./ERC2771ContextUpgradeable.sol";
import {IMintPoint} from "./IMintPoint.sol";

contract MintPoint is
ERC1155SupplyUpgradeable,
AccessControlEnumerableUpgradeable,
ERC2771ContextUpgradeable,
IMintPoint
{
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
uint256 public tokenIds;
string public name;
string public symbol;

function initialize() public initializer {
function initialize(MinimalForwarderUpgradeable _trustedForwarder) public initializer {
__ERC1155_init("");
__ERC1155Supply_init();
__AccessControlEnumerable_init();
__ERC2771Context_init(address(_trustedForwarder));

_grantRole(DEFAULT_ADMIN_ROLE, _msgSender());
_grantRole(MINTER_ROLE, _msgSender());

name = "MintPoint";
symbol = "MNT";
_register();
_register();
}

function supportsInterface(
Expand All @@ -51,15 +56,25 @@ contract MintPoint is
return super.isApprovedForAll(account, operator);
}

function mint(
function mintByMinter(
address to,
uint256 tokenId,
uint256 amount
) external onlyRole(MINTER_ROLE) {
require(tokenId < tokenIds, "MintPoint: tokenId is not registered.");
_mint(to, tokenId, amount, "");

emit Mint(to, tokenId);
emit MintByMinter(to, tokenId);
}

function mintForReferral(address to, uint256 amount) external {
require(amount < balanceOf(_msgSender(), 0), "MintPoint: not enough.");

_burn(_msgSender(), 0, amount);

_mint(to, 1, amount, "");

emit MintForReferral(_msgSender(), to, 1);
}

function register() external onlyRole(DEFAULT_ADMIN_ROLE) {
Expand All @@ -82,4 +97,36 @@ contract MintPoint is
);
_burn(_msgSender(), tokenId, amount);
}

function _msgSender()
internal
view
virtual
override(ContextUpgradeable, ERC2771ContextUpgradeable)
returns (address sender)
{
if (isTrustedForwarder(msg.sender)) {
// The assembly code is more direct than the Solidity version using `abi.decode`.
/// @solidity memory-safe-assembly
assembly {
sender := shr(96, calldataload(sub(calldatasize(), 20)))
}
} else {
return super._msgSender();
}
}

function _msgData()
internal
view
virtual
override(ContextUpgradeable, ERC2771ContextUpgradeable)
returns (bytes calldata)
{
if (isTrustedForwarder(msg.sender)) {
return msg.data[:msg.data.length - 20];
} else {
return super._msgData();
}
}
}
4 changes: 2 additions & 2 deletions hardhat/scripts/helper/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ export const deployForwarder = async () => {
return deployedForwarder;
};

export const deployMintPoint = async () => {
export const deployMintPoint = async (params: { forwarderAddress: string }) => {
const mintPoint = await ethers.getContractFactory("MintPoint");
const deployedMintPoint: MintPoint = (await upgrades.deployProxy(
mintPoint,
[],
[params.forwarderAddress],
{
initializer: "initialize",
}
Expand Down

0 comments on commit 99ffef1

Please sign in to comment.