Skip to content

Commit 7d17dbd

Browse files
committed
Add confidential to balanceOf and totalSupply functions (#93)
* Rename token functions * add changeset
1 parent 6ee48d2 commit 7d17dbd

10 files changed

+49
-44
lines changed

.changeset/nasty-camels-attack.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'openzeppelin-confidential-contracts': minor
3+
---
4+
5+
`IConfidentialFungibleToken`: Prefix `totalSupply` and `balanceOf` functions with confidential.

contracts/governance/utils/VotesConfidential.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ abstract contract VotesConfidential is Nonces, EIP712, IERC6372 {
9696
* @dev Returns the current total supply of votes as an encrypted uint64 (euint64). Must be implemented
9797
* by the derived contract.
9898
*/
99-
function totalSupply() public view virtual returns (euint64);
99+
function confidentialTotalSupply() public view virtual returns (euint64);
100100

101101
/// @dev Returns the delegate that `account` has chosen.
102102
function delegates(address account) public view virtual returns (address) {
@@ -149,11 +149,11 @@ abstract contract VotesConfidential is Nonces, EIP712, IERC6372 {
149149
* @dev Transfers, mints, or burns voting units. To register a mint, `from` should be zero. To register a burn, `to`
150150
* should be zero. Total supply of voting units will be adjusted with mints and burns.
151151
*
152-
* WARNING: Must be called after {totalSupply} is updated.
152+
* WARNING: Must be called after {confidentialTotalSupply} is updated.
153153
*/
154154
function _transferVotingUnits(address from, address to, euint64 amount) internal virtual {
155155
if (from == address(0) || to == address(0)) {
156-
_push(_totalCheckpoints, totalSupply());
156+
_push(_totalCheckpoints, confidentialTotalSupply());
157157
}
158158
_moveDelegateVotes(delegates(from), delegates(to), amount);
159159
}

contracts/interfaces/IConfidentialFungibleToken.sol

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ interface IConfidentialFungibleToken {
2121
* Accounts with access to the encrypted amount `encryptedAmount` that is also accessible to this contract
2222
* should be able to disclose the amount. This functionality is implementation specific.
2323
*/
24-
event EncryptedAmountDisclosed(euint64 indexed encryptedAmount, uint64 amount);
24+
event AmountDisclosed(euint64 indexed encryptedAmount, uint64 amount);
2525

2626
/// @dev Returns the name of the token.
2727
function name() external view returns (string memory);
@@ -35,11 +35,11 @@ interface IConfidentialFungibleToken {
3535
/// @dev Returns the token URI.
3636
function tokenURI() external view returns (string memory);
3737

38-
/// @dev Returns the encrypted total supply of the token.
39-
function totalSupply() external view returns (euint64);
38+
/// @dev Returns the confidential total supply of the token.
39+
function confidentialTotalSupply() external view returns (euint64);
4040

41-
/// @dev Returns the encrypted balance of the account `account`.
42-
function balanceOf(address account) external view returns (euint64);
41+
/// @dev Returns the confidential balance of the account `account`.
42+
function confidentialBalanceOf(address account) external view returns (euint64);
4343

4444
/// @dev Returns true if `spender` is currently an operator for `holder`.
4545
function isOperator(address holder, address spender) external view returns (bool);

contracts/mocks/ConfidentialFungibleTokenMock.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ contract ConfidentialFungibleTokenMock is ConfidentialFungibleToken, SepoliaConf
1919

2020
function _update(address from, address to, euint64 amount) internal virtual override returns (euint64 transferred) {
2121
transferred = super._update(from, to, amount);
22-
FHE.allow(totalSupply(), _OWNER);
22+
FHE.allow(confidentialTotalSupply(), _OWNER);
2323
}
2424

2525
function $_mint(

contracts/mocks/ConfidentialFungibleTokenVotesMock.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ abstract contract ConfidentialFungibleTokenVotesMock is ConfidentialFungibleToke
2626
return super.clock();
2727
}
2828

29-
function totalSupply()
29+
function confidentialTotalSupply()
3030
public
3131
view
3232
virtual
3333
override(ConfidentialFungibleToken, ConfidentialFungibleTokenVotes)
3434
returns (euint64)
3535
{
36-
return super.totalSupply();
36+
return super.confidentialTotalSupply();
3737
}
3838

3939
function _update(

contracts/token/ConfidentialFungibleToken.sol

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ abstract contract ConfidentialFungibleToken is IConfidentialFungibleToken {
8484
}
8585

8686
/// @inheritdoc IConfidentialFungibleToken
87-
function totalSupply() public view virtual returns (euint64) {
87+
function confidentialTotalSupply() public view virtual returns (euint64) {
8888
return _totalSupply;
8989
}
9090

9191
/// @inheritdoc IConfidentialFungibleToken
92-
function balanceOf(address account) public view virtual returns (euint64) {
92+
function confidentialBalanceOf(address account) public view virtual returns (euint64) {
9393
return _balances[account];
9494
}
9595

@@ -203,7 +203,7 @@ abstract contract ConfidentialFungibleToken is IConfidentialFungibleToken {
203203
}
204204

205205
/**
206-
* @dev Discloses an encrypted amount `encryptedAmount` publicly via an {IConfidentialFungibleToken-EncryptedAmountDisclosed}
206+
* @dev Discloses an encrypted amount `encryptedAmount` publicly via an {IConfidentialFungibleToken-AmountDisclosed}
207207
* event. The caller and this contract must be authorized to use the encrypted amount on the ACL.
208208
*
209209
* NOTE: This is an asynchronous operation where the actual decryption happens off-chain and
@@ -231,7 +231,7 @@ abstract contract ConfidentialFungibleToken is IConfidentialFungibleToken {
231231

232232
euint64 requestHandle = _requestHandles[requestId];
233233
require(FHE.isInitialized(requestHandle), ConfidentialFungibleTokenInvalidGatewayRequest(requestId));
234-
emit EncryptedAmountDisclosed(requestHandle, amount);
234+
emit AmountDisclosed(requestHandle, amount);
235235

236236
_requestHandles[requestId] = euint64.wrap(0);
237237
}

contracts/token/extensions/ConfidentialFungibleTokenVotes.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import {VotesConfidential} from "./../../governance/utils/VotesConfidential.sol"
77
import {ConfidentialFungibleToken} from "./../ConfidentialFungibleToken.sol";
88

99
abstract contract ConfidentialFungibleTokenVotes is ConfidentialFungibleToken, VotesConfidential {
10-
function totalSupply()
10+
function confidentialTotalSupply()
1111
public
1212
view
1313
virtual
1414
override(VotesConfidential, ConfidentialFungibleToken)
1515
returns (euint64)
1616
{
17-
return super.totalSupply();
17+
return super.confidentialTotalSupply();
1818
}
1919

2020
function _update(address from, address to, euint64 amount) internal virtual override returns (euint64 transferred) {
@@ -24,6 +24,6 @@ abstract contract ConfidentialFungibleTokenVotes is ConfidentialFungibleToken, V
2424
}
2525

2626
function _getVotingUnits(address account) internal view virtual override returns (euint64) {
27-
return balanceOf(account);
27+
return confidentialBalanceOf(account);
2828
}
2929
}

test/token/ConfidentialFungibleToken.test.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,16 @@ describe('ConfidentialFungibleToken', function () {
4848
});
4949
});
5050

51-
describe('balanceOf', function () {
51+
describe('confidentialBalanceOf', function () {
5252
it('handle can be reencryped by owner', async function () {
53-
const balanceOfHandleHolder = await this.token.balanceOf(this.holder);
53+
const balanceOfHandleHolder = await this.token.confidentialBalanceOf(this.holder);
5454
await expect(
5555
fhevm.userDecryptEuint(FhevmType.euint64, balanceOfHandleHolder, this.token.target, this.holder),
5656
).to.eventually.equal(1000);
5757
});
5858

5959
it('handle cannot be reencryped by non-owner', async function () {
60-
const balanceOfHandleHolder = await this.token.balanceOf(this.holder);
60+
const balanceOfHandleHolder = await this.token.confidentialBalanceOf(this.holder);
6161
await expect(
6262
fhevm.userDecryptEuint(FhevmType.euint64, balanceOfHandleHolder, this.token.target, this.accounts[0]),
6363
).to.be.rejectedWith(generateReencryptionErrorMessage(balanceOfHandleHolder, this.accounts[0].address));
@@ -78,13 +78,13 @@ describe('ConfidentialFungibleToken', function () {
7878
['$_mint(address,bytes32,bytes)'](this.holder, encryptedInput.handles[0], encryptedInput.inputProof);
7979
}
8080

81-
const balanceOfHandleHolder = await this.token.balanceOf(this.holder);
81+
const balanceOfHandleHolder = await this.token.confidentialBalanceOf(this.holder);
8282
await expect(
8383
fhevm.userDecryptEuint(FhevmType.euint64, balanceOfHandleHolder, this.token.target, this.holder),
8484
).to.eventually.equal(existingUser ? 2000 : 1000);
8585

8686
// Check total supply
87-
const totalSupplyHandle = await this.token.totalSupply();
87+
const totalSupplyHandle = await this.token.confidentialTotalSupply();
8888
await expect(
8989
fhevm.userDecryptEuint(FhevmType.euint64, totalSupplyHandle, this.token.target, this.holder),
9090
).to.eventually.equal(existingUser ? 2000 : 1000);
@@ -121,13 +121,13 @@ describe('ConfidentialFungibleToken', function () {
121121
.connect(this.holder)
122122
['$_burn(address,bytes32,bytes)'](this.holder, encryptedInput.handles[0], encryptedInput.inputProof);
123123

124-
const balanceOfHandleHolder = await this.token.balanceOf(this.holder);
124+
const balanceOfHandleHolder = await this.token.confidentialBalanceOf(this.holder);
125125
await expect(
126126
fhevm.userDecryptEuint(FhevmType.euint64, balanceOfHandleHolder, this.token.target, this.holder),
127127
).to.eventually.equal(sufficientBalance ? 600 : 1000);
128128

129129
// Check total supply
130-
const totalSupplyHandle = await this.token.totalSupply();
130+
const totalSupplyHandle = await this.token.confidentialTotalSupply();
131131
await expect(
132132
fhevm.userDecryptEuint(FhevmType.euint64, totalSupplyHandle, this.token.target, this.holder),
133133
).to.eventually.equal(sufficientBalance ? 600 : 1000);
@@ -286,8 +286,8 @@ describe('ConfidentialFungibleToken', function () {
286286
expect(transferEvent.args[1]).to.equal(this.recipient.address);
287287

288288
const transferAmountHandle = transferEvent.args[2];
289-
const holderBalanceHandle = await this.token.balanceOf(this.holder);
290-
const recipientBalanceHandle = await this.token.balanceOf(this.recipient);
289+
const holderBalanceHandle = await this.token.confidentialBalanceOf(this.holder);
290+
const recipientBalanceHandle = await this.token.confidentialBalanceOf(this.recipient);
291291

292292
await expect(
293293
fhevm.userDecryptEuint(FhevmType.euint64, transferAmountHandle, this.token.target, this.holder),
@@ -341,14 +341,14 @@ describe('ConfidentialFungibleToken', function () {
341341
}
342342

343343
it('full balance', async function () {
344-
const fullBalanceHandle = await this.token.balanceOf(this.holder);
344+
const fullBalanceHandle = await this.token.confidentialBalanceOf(this.holder);
345345

346346
await callTransfer(this.token, this.holder, this.recipient, fullBalanceHandle);
347347

348348
await expect(
349349
fhevm.userDecryptEuint(
350350
FhevmType.euint64,
351-
await this.token.balanceOf(this.recipient),
351+
await this.token.confidentialBalanceOf(this.recipient),
352352
this.token.target,
353353
this.recipient,
354354
),
@@ -365,7 +365,7 @@ describe('ConfidentialFungibleToken', function () {
365365
.connect(this.holder)
366366
['$_mint(address,bytes32,bytes)'](this.recipient, encryptedInput.handles[0], encryptedInput.inputProof);
367367

368-
const recipientBalanceHandle = await this.token.balanceOf(this.recipient);
368+
const recipientBalanceHandle = await this.token.confidentialBalanceOf(this.recipient);
369369
await expect(callTransfer(this.token, this.holder, this.recipient, recipientBalanceHandle))
370370
.to.be.revertedWithCustomError(this.token, 'ConfidentialFungibleTokenUnauthorizedUseOfEncryptedAmount')
371371
.withArgs(recipientBalanceHandle, this.holder);
@@ -375,7 +375,7 @@ describe('ConfidentialFungibleToken', function () {
375375
describe('without operator approval', function () {
376376
beforeEach(async function () {
377377
await this.token.connect(this.holder).setOperator(this.operator.address, 0);
378-
await allowHandle(hre, this.holder, this.operator, await this.token.balanceOf(this.holder));
378+
await allowHandle(hre, this.holder, this.operator, await this.token.confidentialBalanceOf(this.holder));
379379
});
380380

381381
it('should revert', async function () {
@@ -384,7 +384,7 @@ describe('ConfidentialFungibleToken', function () {
384384
this.token,
385385
this.holder,
386386
this.recipient,
387-
await this.token.balanceOf(this.holder),
387+
await this.token.confidentialBalanceOf(this.holder),
388388
this.operator,
389389
),
390390
)
@@ -442,7 +442,7 @@ describe('ConfidentialFungibleToken', function () {
442442
await expect(
443443
fhevm.userDecryptEuint(
444444
FhevmType.euint64,
445-
await this.token.balanceOf(this.holder),
445+
await this.token.confidentialBalanceOf(this.holder),
446446
this.token.target,
447447
this.holder,
448448
),
@@ -509,7 +509,7 @@ describe('ConfidentialFungibleToken', function () {
509509
'0x',
510510
);
511511

512-
const balanceOfHandle = await this.token.balanceOf(this.recipient);
512+
const balanceOfHandle = await this.token.confidentialBalanceOf(this.recipient);
513513
await expect(
514514
fhevm.userDecryptEuint(FhevmType.euint64, balanceOfHandle, this.token.target, this.recipient),
515515
).to.eventually.equal(1000);
@@ -526,7 +526,7 @@ describe('ConfidentialFungibleToken', function () {
526526
});
527527

528528
it('user balance', async function () {
529-
const holderBalanceHandle = await this.token.balanceOf(this.holder);
529+
const holderBalanceHandle = await this.token.confidentialBalanceOf(this.holder);
530530

531531
await this.token.connect(this.holder).discloseEncryptedAmount(holderBalanceHandle);
532532

@@ -556,15 +556,15 @@ describe('ConfidentialFungibleToken', function () {
556556
});
557557

558558
it("other user's balance", async function () {
559-
const holderBalanceHandle = await this.token.balanceOf(this.holder);
559+
const holderBalanceHandle = await this.token.confidentialBalanceOf(this.holder);
560560

561561
await expect(this.token.connect(this.recipient).discloseEncryptedAmount(holderBalanceHandle))
562562
.to.be.revertedWithCustomError(this.token, 'ConfidentialFungibleTokenUnauthorizedUseOfEncryptedAmount')
563563
.withArgs(holderBalanceHandle, this.recipient);
564564
});
565565

566566
it('invalid signature reverts', async function () {
567-
const holderBalanceHandle = await this.token.balanceOf(this.holder);
567+
const holderBalanceHandle = await this.token.confidentialBalanceOf(this.holder);
568568
await this.token.connect(this.holder).discloseEncryptedAmount(holderBalanceHandle);
569569

570570
await expect(this.token.connect(this.holder).finalizeDiscloseEncryptedAmount(0, 0, [])).to.be.reverted;
@@ -576,7 +576,7 @@ describe('ConfidentialFungibleToken', function () {
576576
await fhevm.awaitDecryptionOracle();
577577

578578
// Check that event was correctly emitted
579-
const eventFilter = this.token.filters.EncryptedAmountDisclosed();
579+
const eventFilter = this.token.filters.AmountDisclosed();
580580
const discloseEvent = (await this.token.queryFilter(eventFilter))[0];
581581
expect(discloseEvent.args[0]).to.equal(expectedHandle);
582582
expect(discloseEvent.args[1]).to.equal(expectedAmount);

test/token/extensions/ConfidentialFungibleTokenVotes.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ describe('ConfidentialFungibleTokenVotes', function () {
181181
const afterTransferBlock = await ethers.provider.getBlockNumber();
182182

183183
// Burn total balance
184-
const amountToBurn = await this.token.balanceOf(this.holder);
184+
const amountToBurn = await this.token.confidentialBalanceOf(this.holder);
185185
await this.token.$_burn(this.holder, amountToBurn);
186186
const afterBurnBlock = await ethers.provider.getBlockNumber();
187187
await mine();

test/token/extensions/ConfidentialFungibleWrapper.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('ConfidentialFungibleTokenWrapper', function () {
4444
}
4545

4646
await expect(this.token.balanceOf(this.holder)).to.eventually.equal(ethers.parseUnits('900', 18));
47-
const wrappedBalanceHandle = await this.wrapper.balanceOf(this.holder.address);
47+
const wrappedBalanceHandle = await this.wrapper.confidentialBalanceOf(this.holder.address);
4848
await expect(
4949
fhevm.userDecryptEuint(FhevmType.euint64, wrappedBalanceHandle, this.wrapper.target, this.holder),
5050
).to.eventually.equal(ethers.parseUnits('100', 9));
@@ -62,7 +62,7 @@ describe('ConfidentialFungibleTokenWrapper', function () {
6262
await expect(this.token.balanceOf(this.holder)).to.eventually.equal(
6363
ethers.parseUnits('1000', 18) - ethers.parseUnits('10', 9),
6464
);
65-
const wrappedBalanceHandle = await this.wrapper.balanceOf(this.holder.address);
65+
const wrappedBalanceHandle = await this.wrapper.confidentialBalanceOf(this.holder.address);
6666
await expect(
6767
fhevm.userDecryptEuint(FhevmType.euint64, wrappedBalanceHandle, this.wrapper.target, this.holder),
6868
).to.eventually.equal(10);
@@ -81,7 +81,7 @@ describe('ConfidentialFungibleTokenWrapper', function () {
8181
);
8282

8383
await expect(this.token.balanceOf(this.holder)).to.eventually.equal(ethers.parseUnits('900', 18));
84-
const wrappedBalanceHandle = await this.wrapper.balanceOf(this.recipient.address);
84+
const wrappedBalanceHandle = await this.wrapper.confidentialBalanceOf(this.recipient.address);
8585
await expect(
8686
fhevm.userDecryptEuint(FhevmType.euint64, wrappedBalanceHandle, this.wrapper.target, this.recipient),
8787
).to.eventually.equal(ethers.parseUnits('100', 9));
@@ -129,7 +129,7 @@ describe('ConfidentialFungibleTokenWrapper', function () {
129129
it('unwrap full balance', async function () {
130130
await this.wrapper
131131
.connect(this.holder)
132-
.unwrap(this.holder, this.holder, await this.wrapper.balanceOf(this.holder.address));
132+
.unwrap(this.holder, this.holder, await this.wrapper.confidentialBalanceOf(this.holder.address));
133133
await fhevm.awaitDecryptionOracle();
134134

135135
await expect(this.token.balanceOf(this.holder)).to.eventually.equal(ethers.parseUnits('1000', 18));
@@ -218,7 +218,7 @@ describe('ConfidentialFungibleTokenWrapper', function () {
218218
});
219219

220220
it('with a value not allowed to sender', async function () {
221-
const totalSupplyHandle = await this.wrapper.totalSupply();
221+
const totalSupplyHandle = await this.wrapper.confidentialTotalSupply();
222222

223223
await expect(this.wrapper.connect(this.holder).unwrap(this.holder, this.holder, totalSupplyHandle))
224224
.to.be.revertedWithCustomError(this.wrapper, 'ConfidentialFungibleTokenUnauthorizedUseOfEncryptedAmount')

0 commit comments

Comments
 (0)