From 290517db5cfa7189a7ea92be1ee18f8e40a2c380 Mon Sep 17 00:00:00 2001 From: PacificYield <173040337+PacificYield@users.noreply.github.com> Date: Tue, 29 Oct 2024 15:36:01 +0100 Subject: [PATCH] feat: Add mint function --- contracts/token/ERC20/EncryptedERC20.sol | 11 +++++++++++ .../token/ERC20/extensions/EncryptedERC20Mintable.sol | 4 +--- .../extensions/EncryptedERC20WithErrorsMintable.sol | 4 +--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/contracts/token/ERC20/EncryptedERC20.sol b/contracts/token/ERC20/EncryptedERC20.sol index c36bced..b4bc0f2 100644 --- a/contracts/token/ERC20/EncryptedERC20.sol +++ b/contracts/token/ERC20/EncryptedERC20.sol @@ -155,6 +155,17 @@ abstract contract EncryptedERC20 is IEncryptedERC20 { TFHE.allow(amount, spender); } + /** + * @dev It does not incorporate any overflow check. It must be implemented + * by the function calling it. + */ + function _unsafeMint(address account, euint64 amount) internal virtual { + _balances[msg.sender] = TFHE.add(_balances[account], amount); + TFHE.allowThis(_balances[account]); + TFHE.allow(_balances[account], account); + emit Transfer(address(0), account); + } + function _transfer(address from, address to, euint64 amount, ebool isTransferable) internal virtual { // Add to the balance of `to` and subract from the balance of `from`. euint64 transferValue = TFHE.select(isTransferable, amount, TFHE.asEuint64(0)); diff --git a/contracts/token/ERC20/extensions/EncryptedERC20Mintable.sol b/contracts/token/ERC20/extensions/EncryptedERC20Mintable.sol index 737b4ac..c9f1208 100644 --- a/contracts/token/ERC20/extensions/EncryptedERC20Mintable.sol +++ b/contracts/token/ERC20/extensions/EncryptedERC20Mintable.sol @@ -33,9 +33,7 @@ contract EncryptedERC20Mintable is Ownable2Step, EncryptedERC20 { * @param amount Amount of tokens to mint. */ function mint(uint64 amount) public onlyOwner { - _balances[msg.sender] = TFHE.add(_balances[msg.sender], amount); - TFHE.allow(_balances[msg.sender], address(this)); - TFHE.allow(_balances[msg.sender], msg.sender); + _unsafeMint(msg.sender, TFHE.asEuint64(amount)); /// @dev Since _totalSupply is not encrypted and _totalSupply >= balances[msg.sender], /// the next line contains an overflow check for the encrypted operation above. _totalSupply = _totalSupply + amount; diff --git a/contracts/token/ERC20/extensions/EncryptedERC20WithErrorsMintable.sol b/contracts/token/ERC20/extensions/EncryptedERC20WithErrorsMintable.sol index 20c1926..0317b54 100644 --- a/contracts/token/ERC20/extensions/EncryptedERC20WithErrorsMintable.sol +++ b/contracts/token/ERC20/extensions/EncryptedERC20WithErrorsMintable.sol @@ -33,9 +33,7 @@ contract EncryptedERC20WithErrorsMintable is Ownable2Step, EncryptedERC20WithErr * @param amount Amount of tokens to mint. */ function mint(uint64 amount) public onlyOwner { - _balances[msg.sender] = TFHE.add(_balances[msg.sender], amount); - TFHE.allow(_balances[msg.sender], address(this)); - TFHE.allow(_balances[msg.sender], msg.sender); + _unsafeMint(msg.sender, TFHE.asEuint64(amount)); /// @dev Since _totalSupply is not encrypted and _totalSupply >= balances[msg.sender], /// the next line contains an overflow check for the encrypted operation above. _totalSupply = _totalSupply + amount;