Skip to content

Commit

Permalink
pack stage struct to save storage/gas (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
mi-yu authored Oct 5, 2022
1 parent 018ad8c commit bd5b454
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 40 deletions.
14 changes: 7 additions & 7 deletions contracts/ERC721M.sol
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ contract ERC721M is IERC721M, ERC721AQueryable, Ownable, ReentrancyGuard {

function updateStage(
uint256 index,
uint256 price,
uint80 price,
uint32 walletLimit,
bytes32 merkleRoot,
uint256 maxStageSupply,
uint24 maxStageSupply,
uint64 startTimeUnixSeconds,
uint64 endTimeUnixSeconds
) external onlyOwner {
Expand Down Expand Up @@ -241,7 +241,7 @@ contract ERC721M is IERC721M, ERC721AQueryable, Ownable, ReentrancyGuard {
function mint(
uint32 qty,
bytes32[] calldata proof,
uint256 timestamp,
uint64 timestamp,
bytes calldata signature
) external payable nonReentrant {
_mintInternal(qty, msg.sender, proof, timestamp, signature);
Expand All @@ -251,7 +251,7 @@ contract ERC721M is IERC721M, ERC721AQueryable, Ownable, ReentrancyGuard {
uint32 qty,
address to,
bytes32[] calldata proof,
uint256 timestamp,
uint64 timestamp,
bytes calldata signature
) external payable nonReentrant {
if (_crossmintAddress == address(0)) revert CrossmintAddressNotSet();
Expand All @@ -266,7 +266,7 @@ contract ERC721M is IERC721M, ERC721AQueryable, Ownable, ReentrancyGuard {
uint32 qty,
address to,
bytes32[] calldata proof,
uint256 timestamp,
uint64 timestamp,
bytes calldata signature
) internal canMint hasSupply(qty) {
if (_activeStage >= _mintStages.length) revert InvalidStage();
Expand Down Expand Up @@ -422,12 +422,12 @@ contract ERC721M is IERC721M, ERC721AQueryable, Ownable, ReentrancyGuard {
revert InvalidStage();
}

function _assertValidTimestamp(uint256 timestamp) internal view {
function _assertValidTimestamp(uint64 timestamp) internal view {
if (timestamp < block.timestamp - MIN_STAGE_INTERVAL_SECONDS)
revert TimestampExpired();
}

function _assertValidStartAndEndTimestamp(uint256 start, uint256 end)
function _assertValidStartAndEndTimestamp(uint64 start, uint64 end)
internal
pure
{
Expand Down
16 changes: 8 additions & 8 deletions contracts/IERC721M.sol
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ interface IERC721M {
error WithdrawFailed();

struct MintStageInfo {
uint256 price;
uint80 price;
uint32 walletLimit; // 0 for unlimited
bytes32 merkleRoot; // 0x0 for no presale enforced
uint256 maxStageSupply; // 0 for unlimited
uint256 startTimeUnixSeconds;
uint256 endTimeUnixSeconds;
uint24 maxStageSupply; // 0 for unlimited
uint64 startTimeUnixSeconds;
uint64 endTimeUnixSeconds;
}

event UpdateStage(
uint256 stage,
uint256 price,
uint80 price,
uint32 walletLimit,
bytes32 merkleRoot,
uint256 maxStageSupply,
uint256 startTimeUnixSeconds,
uint256 endTimeUnixSeconds
uint24 maxStageSupply,
uint64 startTimeUnixSeconds,
uint64 endTimeUnixSeconds
);

event SetCosigner(address cosigner);
Expand Down
2 changes: 1 addition & 1 deletion cosign-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ router.post(

const cosigner = getCosigner();
const digest = keccak256(
['address', 'address', 'uint32', 'address', 'uint256'],
['address', 'address', 'uint32', 'address', 'uint64'],
[
payload.collectionContract.toLowerCase(),
payload.minter,
Expand Down
21 changes: 11 additions & 10 deletions scripts/setStages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export interface ISetStagesParams {

interface StageConfig {
price: string;
startDate: number;
endDate: number;
walletLimit?: number;
maxSupply?: number;
whitelistPath?: string;
Expand All @@ -22,11 +24,6 @@ export const setStages = async (
const stagesConfig = JSON.parse(
fs.readFileSync(args.stages, 'utf-8'),
) as StageConfig[];
const prices = stagesConfig.map((stage) =>
ethers.BigNumber.from(ethers.utils.parseEther(stage.price)),
);
const walletLimits = stagesConfig.map((stage) => stage.walletLimit ?? 0);
const maxSupplies = stagesConfig.map((stage) => stage.maxSupply ?? 0);
const ERC721M = await ethers.getContractFactory('ERC721M');
const contract = ERC721M.attach(args.contract);
const merkleRoots = await Promise.all(
Expand All @@ -49,11 +46,15 @@ export const setStages = async (
}),
);
const tx = await contract.setStages(
prices,
walletLimits,
merkleRoots,
maxSupplies,
{ gasLimit: 300_000 },
stagesConfig.map((s, i) => ({
price: ethers.utils.parseEther(s.price),
maxStageSupply: s.maxSupply ?? 0,
walletLimit: s.walletLimit ?? 0,
merkleRoot: merkleRoots[i],
startTimeUnixSeconds: Math.floor(new Date(s.startDate).getTime() / 1000),
endTimeUnixSeconds: Math.floor(new Date(s.endDate).getTime() / 1000),
})),
{ gasLimit: 500_000 },
);
console.log(`Submitted tx ${tx.hash}`);

Expand Down
28 changes: 14 additions & 14 deletions test/erc721m.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,14 @@ describe('ERC721M', function () {
let [stageInfo, walletMintedCount] = await contract.getStageInfo(0);
expect(stageInfo.price).to.equal(ethers.utils.parseEther('0.5'));
expect(stageInfo.walletLimit).to.equal(3);
expect(stageInfo.maxStageSupply.toNumber()).to.equal(5);
expect(stageInfo.maxStageSupply).to.equal(5);
expect(stageInfo.merkleRoot).to.equal(ethers.utils.hexZeroPad('0x1', 32));
expect(walletMintedCount).to.equal(0);

[stageInfo, walletMintedCount] = await contract.getStageInfo(1);
expect(stageInfo.price).to.equal(ethers.utils.parseEther('0.6'));
expect(stageInfo.walletLimit).to.equal(4);
expect(stageInfo.maxStageSupply.toNumber()).to.equal(10);
expect(stageInfo.maxStageSupply).to.equal(10);
expect(stageInfo.merkleRoot).to.equal(ethers.utils.hexZeroPad('0x2', 32));
expect(walletMintedCount).to.equal(0);

Expand All @@ -174,7 +174,7 @@ describe('ERC721M', function () {
[stageInfo, walletMintedCount] = await contract.getStageInfo(0);
expect(stageInfo.price).to.equal(ethers.utils.parseEther('0.6'));
expect(stageInfo.walletLimit).to.equal(4);
expect(stageInfo.maxStageSupply.toNumber()).to.equal(0);
expect(stageInfo.maxStageSupply).to.equal(0);
expect(stageInfo.merkleRoot).to.equal(ethers.utils.hexZeroPad('0x3', 32));
expect(walletMintedCount).to.equal(0);

Expand All @@ -201,7 +201,7 @@ describe('ERC721M', function () {
[stageInfo, walletMintedCount] = await contract.getStageInfo(1);
expect(stageInfo.price).to.equal(ethers.utils.parseEther('0.7'));
expect(stageInfo.walletLimit).to.equal(5);
expect(stageInfo.maxStageSupply.toNumber()).to.equal(5);
expect(stageInfo.maxStageSupply).to.equal(5);
expect(stageInfo.merkleRoot).to.equal(ethers.utils.hexZeroPad('0x4', 32));
expect(walletMintedCount).to.equal(0);
});
Expand Down Expand Up @@ -231,14 +231,14 @@ describe('ERC721M', function () {
let [stageInfo, walletMintedCount] = await contract.getStageInfo(0);
expect(stageInfo.price).to.equal(ethers.utils.parseEther('0.5'));
expect(stageInfo.walletLimit).to.equal(3);
expect(stageInfo.maxStageSupply.toNumber()).to.equal(5);
expect(stageInfo.maxStageSupply).to.equal(5);
expect(stageInfo.merkleRoot).to.equal(ethers.utils.hexZeroPad('0x1', 32));
expect(walletMintedCount).to.equal(0);

[stageInfo, walletMintedCount] = await contract.getStageInfo(1);
expect(stageInfo.price).to.equal(ethers.utils.parseEther('0.6'));
expect(stageInfo.walletLimit).to.equal(4);
expect(stageInfo.maxStageSupply.toNumber()).to.equal(10);
expect(stageInfo.maxStageSupply).to.equal(10);
expect(stageInfo.merkleRoot).to.equal(ethers.utils.hexZeroPad('0x2', 32));
expect(walletMintedCount).to.equal(0);

Expand Down Expand Up @@ -270,15 +270,15 @@ describe('ERC721M', function () {
[stageInfo, walletMintedCount] = await contract.getStageInfo(0);
expect(stageInfo.price).to.equal(ethers.utils.parseEther('0.1'));
expect(stageInfo.walletLimit).to.equal(13);
expect(stageInfo.maxStageSupply.toNumber()).to.equal(15);
expect(stageInfo.maxStageSupply).to.equal(15);
expect(stageInfo.merkleRoot).to.equal(ethers.utils.hexZeroPad('0x9', 32));
expect(walletMintedCount).to.equal(0);

// Stage 2 is unchanged.
[stageInfo, walletMintedCount] = await contract.getStageInfo(1);
expect(stageInfo.price).to.equal(ethers.utils.parseEther('0.6'));
expect(stageInfo.walletLimit).to.equal(4);
expect(stageInfo.maxStageSupply.toNumber()).to.equal(10);
expect(stageInfo.maxStageSupply).to.equal(10);
expect(stageInfo.merkleRoot).to.equal(ethers.utils.hexZeroPad('0x2', 32));
expect(walletMintedCount).to.equal(0);
});
Expand Down Expand Up @@ -368,7 +368,7 @@ describe('ERC721M', function () {
const [stageInfo, walletMintedCount] = await contract.getStageInfo(0);
expect(stageInfo.price).to.equal(ethers.utils.parseEther('0.5'));
expect(stageInfo.walletLimit).to.equal(3);
expect(stageInfo.maxStageSupply.toNumber()).to.equal(5);
expect(stageInfo.maxStageSupply).to.equal(5);
expect(stageInfo.merkleRoot).to.equal(ethers.utils.hexZeroPad('0x1', 32));
expect(walletMintedCount).to.equal(0);
});
Expand Down Expand Up @@ -652,7 +652,7 @@ describe('ERC721M', function () {
);
const [stageInfo, walletMintedCount, stagedMintedCount] =
await readonlyContract.getStageInfo(0);
expect(stageInfo.maxStageSupply.toNumber()).to.equal(100);
expect(stageInfo.maxStageSupply).to.equal(100);
expect(walletMintedCount).to.equal(1);
expect(stagedMintedCount.toNumber()).to.equal(1);
});
Expand Down Expand Up @@ -692,7 +692,7 @@ describe('ERC721M', function () {
);
const [stageInfo, walletMintedCount, stagedMintedCount] =
await readonlyContract.getStageInfo(0);
expect(stageInfo.maxStageSupply.toNumber()).to.equal(100);
expect(stageInfo.maxStageSupply).to.equal(100);
expect(walletMintedCount).to.equal(1);
expect(stagedMintedCount.toNumber()).to.equal(1);
});
Expand Down Expand Up @@ -922,7 +922,7 @@ describe('ERC721M', function () {
let [stageInfo, walletMintedCount, stagedMintedCount] =
await contract.getStageInfo(0);

expect(stageInfo.maxStageSupply.toNumber()).to.equal(5);
expect(stageInfo.maxStageSupply).to.equal(5);
expect(walletMintedCount).to.equal(5);
expect(stagedMintedCount.toNumber()).to.equal(5);

Expand Down Expand Up @@ -951,7 +951,7 @@ describe('ERC721M', function () {
});
[stageInfo, walletMintedCount, stagedMintedCount] =
await contract.getStageInfo(1);
expect(stageInfo.maxStageSupply.toNumber()).to.equal(10);
expect(stageInfo.maxStageSupply).to.equal(10);
expect(walletMintedCount).to.equal(8);
expect(stagedMintedCount.toNumber()).to.equal(8);

Expand Down Expand Up @@ -1106,7 +1106,7 @@ describe('ERC721M', function () {

const [stageInfo, walletMintedCount, stagedMintedCount] =
await ownerConn.getStageInfo(0);
expect(stageInfo.maxStageSupply.toNumber()).to.equal(100);
expect(stageInfo.maxStageSupply).to.equal(100);
expect(walletMintedCount).to.equal(0);
expect(stagedMintedCount.toNumber()).to.equal(1);
});
Expand Down

0 comments on commit bd5b454

Please sign in to comment.