Skip to content

Commit

Permalink
add batchMint supported
Browse files Browse the repository at this point in the history
  • Loading branch information
hvthhien committed May 22, 2024
1 parent a6e0c3a commit 769b9dc
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
14 changes: 12 additions & 2 deletions contracts/FeralfileToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,19 @@ contract FeralfileToken is ERC20, Authorizable {
constructor(string memory name_, string memory symbol_) ERC20(name_, symbol_) {}

/// @notice Mint new tokens to an owner
/// @param amount The amount of tokens to mint
/// @param owner The address of the owner to receive the minted tokens
function mintToken(uint256 amount, address owner) external onlyAuthorized {
/// @param amount The amount of tokens to mint
function mint(address owner, uint256 amount) external onlyAuthorized {
_mint(owner, amount);
}

/// @notice Mint tokens for multiple owners
/// @param owners An array of addresses to receive the minted tokens
/// @param amounts An array of amounts of tokens to mint for each respective owner
function batchMint(address[] calldata owners, uint256[] calldata amounts) external virtual onlyAuthorized {
require(owners.length == amounts.length, "FeralfileExhibitionV4: owners and amounts length mismatch");
for (uint256 i = 0; i < owners.length; i++) {
_mint(owners[i], amounts[i]);
}
}
}
29 changes: 28 additions & 1 deletion test/feralfile_token.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ contract('FeralfileToken', async (accounts) => {
const owner = accounts[1];
const amount = 24;

const tx = await this.contract.mintToken(amount, owner);
await this.contract.mint(owner, amount, { from: accounts[0] });

// total supply
const totalSupply = await this.contract.totalSupply();
Expand All @@ -31,4 +31,31 @@ contract('FeralfileToken', async (accounts) => {
const balance = await this.contract.balanceOf(owner);
assert.equal(balance, amount);
});

it("test mint tokens for multiple owners", async function () {
const owners = [accounts[2], accounts[3], accounts[4]];
const amounts = [50, 100, 150];

await this.contract.batchMint(owners, amounts, { from: accounts[0] });

const totalSupply = await this.contract.totalSupply();
assert.equal(totalSupply.toNumber(), 300, "Total supply does not match minted amount for multiple users");

for (let i = 0; i < owners.length; i++) {
const balance = await this.contract.balanceOf(owners[i]);
assert.equal(balance.toNumber(), amounts[i], `Balance of owner ${i + 1} does not match minted amount`);
}
});

it("test mint tokens for multiple owners mismatched array lengths", async function () {
const owners = [accounts[2], accounts[3]];
const amounts = [50, 100, 150];

try {
await this.contract.batchMint(owners, amounts, { from: accounts[0] });
assert.fail("Minting with mismatched array lengths should have thrown an error");
} catch (error) {
assert(error.message.includes("owners and amounts length mismatch"), "Expected Owners and amounts length mismatch error");
}
});
});

0 comments on commit 769b9dc

Please sign in to comment.