Skip to content
Draft
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
5 changes: 5 additions & 0 deletions .changeset/stupid-dogs-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-confidential-contracts': minor
---

`ConfidentialFungibleTokenERC20Events`: Extension of `ConfidentialFungibleToken` that emits ERC20 events on transfers.
17 changes: 17 additions & 0 deletions contracts/mocks/ConfidentialFungibleTokenERC20EventsMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import {SepoliaConfig} from "@fhevm/solidity/config/ZamaConfig.sol";
import {FHE, euint64, externalEuint64} from "@fhevm/solidity/lib/FHE.sol";
import {ConfidentialFungibleTokenERC20Events} from "./../token/extensions/ConfidentialFungibleTokenERC20Events.sol";

// solhint-disable func-name-mixedcase
abstract contract ConfidentialFungibleTokenERC20EventsMock is ConfidentialFungibleTokenERC20Events, SepoliaConfig {
function $_mint(
address to,
externalEuint64 encryptedAmount,
bytes calldata inputProof
) public returns (euint64 transferred) {
return _mint(to, FHE.fromExternal(encryptedAmount, inputProof));
}
}
2 changes: 2 additions & 0 deletions contracts/token/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ This set of interfaces, contracts, and utilities are all related to the evolving

- {ConfidentialFungibleToken}: Implementation of {IConfidentialFungibleToken}.
- {ConfidentialFungibleTokenERC20Wrapper}: Extension of {ConfidentialFungibleToken} which wraps an `ERC20` into a confidential token. The wrapper allows for free conversion in both directions at a fixed rate.
- {ConfidentialFungibleTokenERC20Events}: Extension of {ConfidentialFungibleToken} that emits ERC20 events on transfers.
- {ConfidentialFungibleTokenUtils}: A library that provides the on-transfer callback check used by {ConfidentialFungibleToken}.

== Core
{{ConfidentialFungibleToken}}

== Extensions
{{ConfidentialFungibleTokenERC20Wrapper}}
{{ConfidentialFungibleTokenERC20Events}}

== Utilities
{{ConfidentialFungibleTokenUtils}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {ConfidentialFungibleToken, euint64} from "../ConfidentialFungibleToken.sol";

/**
* @dev Extension of `ConfidentialFungibleToken` that emits ERC20 events on transfers. This
* can be useful for surfacing confidential transfers on applications that support ERC20 events such as Etherscan.
*
* NOTE: The ERC20 events emitted only have meaningful data for the `to` and `from` fields. The `amount` field
* is fixed to 1.
*/
abstract contract ConfidentialFungibleTokenERC20Events is ConfidentialFungibleToken {
function _update(address from, address to, euint64 amount) internal virtual override returns (euint64) {
emit IERC20.Transfer(from, to, 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to use 1 over something else ? I'd personally use 0. That way the arithmetics of inbound - outbound would always be 0, and can never go negative.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 value transfers are automatically hidden by Etherscan. Tried that first

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, thank you for this test.
Considering what you say, I feel adding Transfer events (with this dummy value of 1), might not be the best path for being compatible with Etherscan (since it looks a bit twisted).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't disagree but without any dedicated support from etherescan this is likely the only way. We could do something with ERC1155 or ERC721 too but I feel like that isn't desirable.

return super._update(from, to, amount);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { expect } from 'chai';
import { ethers, fhevm } from 'hardhat';

const name = 'ConfidentialFungibleToken';
const symbol = 'CFT';
const uri = 'https://example.com/metadata';

describe('ConfidentialFungibleTokenERC20Events', function () {
beforeEach(async function () {
const [holder] = await ethers.getSigners();

const token = await ethers.deployContract('$ConfidentialFungibleTokenERC20EventsMock', [name, symbol, uri]);
this.token = token;
this.holder = holder;
});

it('should emit ERC20 transfer event', async function () {
const encryptedInput = await fhevm
.createEncryptedInput(this.token.target, this.holder.address)
.add64(100)
.encrypt();

await expect(
this.token
.connect(this.holder)
['$_mint(address,bytes32,bytes)'](this.holder, encryptedInput.handles[0], encryptedInput.inputProof),
)
.to.emit(this.token, 'Transfer')
.withArgs(ethers.ZeroAddress, this.holder.address, 1);
});
});