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

add mint with referral funcionality #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
27 changes: 20 additions & 7 deletions src/BaseGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Royalty.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Base64.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "./lib/Split.sol";

contract BaseGen is ERC721, ERC721Burnable, ERC721Royalty, Ownable {
uint256 private _nextTokenId;
Expand Down Expand Up @@ -47,7 +48,7 @@ contract BaseGen is ERC721, ERC721Burnable, ERC721Royalty, Ownable {
_contractURI = contractURI_;
_maxSupply = maxSupply_;
_receiver = receiver_;
_setDefaultRoyalty(receiver_, 5e2); //
_setDefaultRoyalty(receiver_, 5e2);
}

function _baseURI() internal view override returns (string memory) {
Expand Down Expand Up @@ -126,6 +127,10 @@ contract BaseGen is ERC721, ERC721Burnable, ERC721Royalty, Ownable {
}

function safeBatchMint(address to, uint256 quantity) public payable {
mintWithReferral(to, quantity, address(0));
}

function mintWithReferral(address to, uint256 quantity, address referrer) public payable {
if (quantity == 0) {
revert MintQuantityCannotBeZero();
}
Expand All @@ -143,7 +148,7 @@ contract BaseGen is ERC721, ERC721Burnable, ERC721Royalty, Ownable {
revert InsufficientFunds(totalCost, msg.value);
}

_splitPayment(totalCost);
_splitPayment(msg.value, referrer);

for (uint256 i = 0; i < quantity; i++) {
_safeMint(to, tokenId++);
Expand All @@ -153,13 +158,21 @@ contract BaseGen is ERC721, ERC721Burnable, ERC721Royalty, Ownable {
}

function _splitPayment(uint256 amount) private {
_splitPayment(amount, address(0));
}

function _splitPayment(uint256 amount, address referrer) private {
uint256 receiverAmount = amount / 2;
if (_receiver != address(0)) {
uint256 splitAmount = amount / 2;
payable(_receiver).transfer(splitAmount);
payable(owner()).transfer(splitAmount);
} else {
payable(owner()).transfer(amount);
payable(_receiver).transfer(receiverAmount);
}
if (referrer != address(0) && referrer != _receiver) {
(, uint256 referrerAmount) = royaltyInfo(0, amount);
if (referrerAmount > 0 && referrerAmount < address(this).balance) {
payable(referrer).transfer(referrerAmount);
}
}
payable(owner()).transfer(address(this).balance);
}

function tokenURI(uint256 tokenId) public view override(ERC721) returns (string memory) {
Expand Down
58 changes: 58 additions & 0 deletions test/BaseGenReferral.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import "forge-std/Test.sol";
import "../src/BaseGen.sol";

contract BaseGenReferralTest is Test {
BaseGen public instance;
uint256 public constant pricePerMint = 0.0015 ether;
uint256 public constant quantity = 10;
address public initialOwner;
address public receiver;
uint256 public maxSupply = 20;

function setUp() public {
initialOwner = vm.addr(1);
receiver = vm.addr(2);
string memory name = "Generative";
string memory symbol = "GEN";
string memory contractURI = "ipfs://";
string memory baseURI = "https://data.kodadot.xyz/base/"; // suffixed with /?hash=
instance = new BaseGen(initialOwner, name, symbol, contractURI, baseURI, maxSupply, receiver);
}

function testMintWithReferral() public {
address tokenOwner = vm.addr(3);
address referrer = vm.addr(4);

// Calculate the total cost for minting
uint256 totalCost = pricePerMint * quantity;
vm.deal(tokenOwner, totalCost);

// Assert initial state
assertEq(instance.totalSupply(), 0);

// Prank msg.sender as initialOwner to simulate the mint transaction
vm.prank(tokenOwner);

// Send sufficient funds to mint
instance.mintWithReferral{value: totalCost}(tokenOwner, quantity, referrer);

// Assert that totalSupply is updated correctly
assertEq(instance.totalSupply(), quantity);

// Assert the owner of the newly minted tokens
for (uint256 i = 0; i < quantity; i++) {
assertEq(instance.ownerOf(i), tokenOwner);
}

// Assert that the contract balance is transferred correctly
uint256 receiverBalance = totalCost / 2;
assertEq(receiver.balance, receiverBalance);
uint256 referrerBalance = totalCost / 20;
assertEq(referrer.balance, referrerBalance);
assertEq(initialOwner.balance, receiverBalance- referrerBalance);
}

}