Skip to content

Commit

Permalink
feat: adds address arg to mint
Browse files Browse the repository at this point in the history
  • Loading branch information
jatZama committed Mar 6, 2024
1 parent 240ff4c commit e98e977
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ jobs:
npm run fhevm:start &> fhevm.log &
tail -f fhevm.log | sed '/Starting JSON WebSocket server/ q'
npm run test
npm run fhevm:stop || true
npm run fhevm:stop || true
2 changes: 1 addition & 1 deletion .github/workflows/testmock.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ jobs:
- name: "npm CI test"
run: |
# sometimes not created and is not tailed
npm run test:mock
npm run test:mock
2 changes: 1 addition & 1 deletion contracts/DAO/Comp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ contract Comp is EncryptedERC20, Ownable2Step {
* @notice Construct a new Comp token
*/
constructor() EncryptedERC20("Compound", "COMP") Ownable(msg.sender) {
_mint(1000000);
_mint(1000000, msg.sender);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions contracts/token/ERC20/EncryptedERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ abstract contract EncryptedERC20 is Reencrypt, EncryptedErrors {
}

// Increase sender's balance by the given `amount`.
function _mint(uint64 amount) internal virtual {
balances[msg.sender] = TFHE.add(balances[msg.sender], amount); // overflow impossible because of next line
function _mint(uint64 amount, address to) internal virtual {
balances[to] = TFHE.add(balances[to], amount); // overflow impossible because of next line
_totalSupply = _totalSupply + amount;
emit Mint(msg.sender, amount);
emit Mint(to, amount);
}

// Transfers an encrypted amount from the message sender address to the `to` address.
Expand Down
4 changes: 2 additions & 2 deletions contracts/token/ERC20/extensions/EncryptedERC20Mintable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ contract EncryptedERC20Mintable is Ownable2Step, EncryptedERC20 {
) Ownable(owner) EncryptedERC20(name_, symbol_) {}

// Increase owner's balance by the given `mintedAmount`.
function mint(uint64 mintedAmount) public virtual onlyOwner {
_mint(mintedAmount);
function mint(uint64 mintedAmount, address to) public virtual onlyOwner {
_mint(mintedAmount, to);
}
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@
"access": "public"
},
"scripts": {
"clean": "rimraf ./artifacts ./cache ./coverage ./types ./coverage.json && pnpm typechain",
"clean": "rimraf ./artifacts ./cache ./coverage ./types ./coverage.json && npm run typechain",
"compile": "cross-env TS_NODE_TRANSPILE_ONLY=true hardhat compile",
"deploy:contracts": "hardhat deploy",
"docgen": "hardhat docgen",
"lint": "pnpm lint:sol && pnpm lint:ts && pnpm prettier:check",
"lint": "npm run lint:sol && npm run lint:ts && npm run prettier:check",
"lint:sol": "solhint --max-warnings 10 \"contracts/**/*.sol\"",
"lint:ts": "eslint --ignore-path ./.eslintignore --ext .js,.ts .",
"postinstall": "DOTENV_CONFIG_PATH=./.env.example pnpm typechain",
"postinstall": "DOTENV_CONFIG_PATH=./.env.example npm run typechain",
"prettier:check": "prettier --check \"**/*.{js,json,md,sol,ts,yml}\"",
"prettier:write": "prettier --write \"**/*.{js,json,md,sol,ts,yml}\"",
"typechain": "cross-env TS_NODE_TRANSPILE_ONLY=true hardhat typechain",
Expand Down
22 changes: 12 additions & 10 deletions test/encryptedERC20/EncryptedERC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe("EncryptedERC20", function () {
});

it("should mint the contract", async function () {
const transaction = await this.erc20.mint(1000);
const transaction = await this.erc20.mint(1000, this.signers.alice.address);
await transaction.wait();
// Call the method
const token = this.instances.alice.getPublicKey(this.contractAddress) || {
Expand All @@ -46,18 +46,20 @@ describe("EncryptedERC20", function () {
it("non-owner should be unable to mint", async function () {
if (network.name == "hardhat") {
// mocked mode
await expect(this.erc20.connect(this.signers.bob).mint(1000))
await expect(this.erc20.connect(this.signers.bob).mint(1000, this.signers.alice.address))
.to.be.revertedWithCustomError(this.erc20, "OwnableUnauthorizedAccount")
.withArgs(this.signers.bob.address);
} else {
// fhevm-mode
const tx = await this.erc20.connect(this.signers.bob).mint(1000, { gasLimit: 1_000_000n });
const tx = await this.erc20
.connect(this.signers.bob)
.mint(1000, this.signers.alice.address, { gasLimit: 1_000_000n });
await expect(tx.wait()).to.throw;
}
});

it("should transfer tokens between two users", async function () {
const transaction = await this.erc20.mint(10000);
const transaction = await this.erc20.mint(10000, this.signers.alice.address);
await transaction.wait();

const encryptedTransferAmount = this.instances.alice.encrypt64(1337);
Expand Down Expand Up @@ -90,7 +92,7 @@ describe("EncryptedERC20", function () {
});

it("should only be able to read hiw own balance", async function () {
const transaction = await this.erc20.mint(10000);
const transaction = await this.erc20.mint(10000, this.signers.alice.address);
await transaction.wait();
const tokenAlice = this.instances.alice.getPublicKey(this.contractAddress)!;
const encryptedBalanceAlice = await this.erc20.balanceOf(
Expand All @@ -116,7 +118,7 @@ describe("EncryptedERC20", function () {

it("balanceOfMe should recover own's balance handle", async function () {
expect(await this.erc20.balanceOfMe()).to.be.eq(0n); // Alice's initial handle is 0
const transaction = await this.erc20.mint(1000);
const transaction = await this.erc20.mint(1000, this.signers.alice.address);
await transaction.wait();
if (network.name == "hardhat") {
// mocked mode
Expand All @@ -130,7 +132,7 @@ describe("EncryptedERC20", function () {
});

it("should not transfer tokens between two users", async function () {
const transaction = await this.erc20.mint(1000);
const transaction = await this.erc20.mint(1000, this.signers.alice.address);
await transaction.wait();

const encryptedTransferAmount = this.instances.alice.encrypt64(1337);
Expand Down Expand Up @@ -163,7 +165,7 @@ describe("EncryptedERC20", function () {
});

it("should be able to transferFrom only if allowance is sufficient", async function () {
const transaction = await this.erc20.mint(10000);
const transaction = await this.erc20.mint(10000, this.signers.alice.address);
await transaction.wait();

const encryptedAllowanceAmount = this.instances.alice.encrypt64(1337);
Expand Down Expand Up @@ -219,7 +221,7 @@ describe("EncryptedERC20", function () {
});

it("only spender and owner could read their allowance", async function () {
const transaction = await this.erc20.mint(10000);
const transaction = await this.erc20.mint(10000, this.signers.alice.address);
await transaction.wait();

const encryptedAllowanceAmount = this.instances.alice.encrypt64(1337);
Expand Down Expand Up @@ -284,7 +286,7 @@ describe("EncryptedERC20", function () {

it("should handle errors correctly", async function () {
// case 1 succesful transfer
const transaction = await this.erc20.mint(10000);
const transaction = await this.erc20.mint(10000, this.signers.alice.address);
await transaction.wait();
let encryptedTransferAmount = this.instances.alice.encrypt64(1337);
const tx = await this.erc20["transfer(address,bytes)"](this.signers.bob.address, encryptedTransferAmount);
Expand Down

0 comments on commit e98e977

Please sign in to comment.