-
Notifications
You must be signed in to change notification settings - Fork 23
Support compliance modules for confidential RWAs #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
james-toussaint
wants to merge
57
commits into
master
Choose a base branch
from
feature/confidential-rwa-compliance
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
57 commits
Select commit
Hold shift + click to select a range
61729c2
Init `ERC7984Rwa` extension.
james-toussaint 639e5a2
Fix typos
james-toussaint 85546dd
Add agent role
james-toussaint 0029ad7
Update spelling
james-toussaint b2174ea
Add pausable & roles tests
james-toussaint ce8286c
Add mint/burn/force/transfer tests
james-toussaint 6cb98a5
Remove tmp freezable
james-toussaint 46d6800
Merge remote-tracking branch 'origin/master' into feature/confidentia…
james-toussaint d2562aa
Name ERC7984Rwa
james-toussaint f58c1f3
Name confidential
james-toussaint 76b21ae
Move RWA test
james-toussaint 127aff5
Test with & without proof
james-toussaint 84af687
Rwa mock uses freezable
james-toussaint b0d5ffa
Check transferred amounts in tests
james-toussaint 6fb7f97
Bypass hardhat fhevm behaviour
james-toussaint 0cb0208
Add support interface test
james-toussaint 90ebfa0
Add should not force transfer if anyone
james-toussaint c5d07fe
Move some modifiers to mock
james-toussaint e484066
Update doc
james-toussaint 4ec0bd1
Merge remote-tracking branch 'origin/master' into feature/confidentia…
james-toussaint 64c6c9b
Swap items in doc
james-toussaint 1ad9ccd
Add suggestions
james-toussaint 628d143
Remove lint annotation
james-toussaint 0b1d87c
Update test name
james-toussaint b6b6827
Add restriction to ERC7984Rwa
james-toussaint 3185336
Move gates
james-toussaint b4f8c03
Remove ExpectedPause error
james-toussaint 28973a2
Rename block functions
james-toussaint 0facae5
Rename fixture
james-toussaint 2d0ef0e
Force transfer with all update effects
james-toussaint 5451777
Update set frozen doc
james-toussaint 3065002
Refactor event checks in freezable tests
james-toussaint 4a2e41e
Init compliance modules for confidential RWAs
james-toussaint 3946b30
Add abstract compliance modules
james-toussaint 4debb47
Compliance implements interface
james-toussaint 86d5250
Add post transfer hook
james-toussaint 30ca7df
Typo
james-toussaint 57ab500
Init investor cap module
james-toussaint ed06057
Move rwa compliance contracts
james-toussaint 4b4d558
Support confidential rwa module
james-toussaint 4c45948
Rename rwa mock functions
james-toussaint 9974fae
Immutable token in balance cap module
james-toussaint 3332581
Switch to always-on/transfer-only compliance modules
james-toussaint c81b703
Typo
james-toussaint 7d438c3
Use enum for compliance module type
james-toussaint fea22af
Enable token handles access to modules
james-toussaint b74c5ba
Increase coverage on modular compliance flow
james-toussaint b964c8b
Should not post update investors if not compliant
james-toussaint 3be8b00
Rename to `ModularCompliance` & `ComplianceModule`
james-toussaint 1bc4c3c
Add balance cap module tests
james-toussaint 1336085
Add max investor tests
james-toussaint 35b0771
Merge remote-tracking branch 'origin' into feature/confidential-rwa
james-toussaint 4f04222
Use agent for operations
james-toussaint 9e56fa5
Merge remote-tracking branch 'origin/feature/confidential-rwa' into f…
james-toussaint 020fe73
Merge remote-tracking branch 'origin' into feature/confidential-rwa-c…
james-toussaint 53ec926
Restore restricted and freezable
james-toussaint d3240e1
Update styling
james-toussaint File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'openzeppelin-confidential-contracts': minor | ||
--- | ||
|
||
`ERC7984RwaModularCompliance`: Support compliance modules for confidential RWAs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.24; | ||
|
||
import {SepoliaConfig} from "@fhevm/solidity/config/ZamaConfig.sol"; | ||
import {FHE, euint64} from "@fhevm/solidity/lib/FHE.sol"; | ||
import {ERC7984RwaBalanceCapModule} from "../../token/ERC7984/extensions/rwa/ERC7984RwaBalanceCapModule.sol"; | ||
|
||
contract ERC7984RwaBalanceCapModuleMock is ERC7984RwaBalanceCapModule, SepoliaConfig { | ||
event AmountEncrypted(euint64 amount); | ||
|
||
constructor(address compliance) ERC7984RwaBalanceCapModule(compliance) {} | ||
|
||
function createEncryptedAmount(uint64 amount) public returns (euint64 encryptedAmount) { | ||
FHE.allowThis(encryptedAmount = FHE.asEuint64(amount)); | ||
FHE.allow(encryptedAmount, msg.sender); | ||
emit AmountEncrypted(encryptedAmount); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.24; | ||
|
||
import {SepoliaConfig} from "@fhevm/solidity/config/ZamaConfig.sol"; | ||
import {FHE, ebool, euint64} from "@fhevm/solidity/lib/FHE.sol"; | ||
import {ERC7984RwaComplianceModule} from "../../token/ERC7984/extensions/rwa/ERC7984RwaComplianceModule.sol"; | ||
|
||
// solhint-disable func-name-mixedcase | ||
contract ERC7984RwaModularComplianceModuleMock is ERC7984RwaComplianceModule, SepoliaConfig { | ||
bool private _compliant = false; | ||
|
||
event PostTransfer(); | ||
event PreTransfer(); | ||
|
||
constructor(address compliance) ERC7984RwaComplianceModule(compliance) {} | ||
|
||
function $_setCompliant() public { | ||
_compliant = true; | ||
} | ||
|
||
function $_unsetCompliant() public { | ||
_compliant = false; | ||
} | ||
|
||
function _isCompliantTransfer( | ||
address /*from*/, | ||
address /*to*/, | ||
euint64 /*encryptedAmount*/ | ||
) internal override returns (ebool) { | ||
emit PreTransfer(); | ||
return FHE.asEbool(_compliant); | ||
} | ||
|
||
function _postTransfer(address /*from*/, address /*to*/, euint64 /*encryptedAmount*/) internal override { | ||
emit PostTransfer(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.24; | ||
|
||
import {SepoliaConfig} from "@fhevm/solidity/config/ZamaConfig.sol"; | ||
import {ERC7984RwaInvestorCapModule} from "../../token/ERC7984/extensions/rwa/ERC7984RwaInvestorCapModule.sol"; | ||
|
||
contract ERC7984RwaInvestorCapModuleMock is ERC7984RwaInvestorCapModule, SepoliaConfig { | ||
constructor(address token, uint64 maxInvestor) ERC7984RwaInvestorCapModule(token, maxInvestor) {} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {ERC7984} from "../../token/ERC7984/ERC7984.sol"; | ||
import {ERC7984Rwa} from "../../token/ERC7984/extensions/ERC7984Rwa.sol"; | ||
import {ERC7984RwaModularCompliance} from "../../token/ERC7984/extensions/rwa/ERC7984RwaModularCompliance.sol"; | ||
|
||
contract ERC7984RwaModularComplianceMock is ERC7984RwaModularCompliance, SepoliaConfig { | ||
constructor( | ||
string memory name, | ||
string memory symbol, | ||
string memory tokenUri, | ||
address admin | ||
) ERC7984Rwa(admin) ERC7984(name, symbol, tokenUri) {} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
contracts/token/ERC7984/extensions/rwa/ERC7984RwaBalanceCapModule.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.27; | ||
|
||
import {FHE, ebool, euint64, externalEuint64} from "@fhevm/solidity/lib/FHE.sol"; | ||
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol"; | ||
import {IERC7984} from "../../../../interfaces/IERC7984.sol"; | ||
import {FHESafeMath} from "../../../../utils/FHESafeMath.sol"; | ||
import {ERC7984RwaComplianceModule} from "./ERC7984RwaComplianceModule.sol"; | ||
|
||
/** | ||
* @dev A transfer compliance module for confidential Real World Assets (RWAs) which limits the balance of an investor. | ||
*/ | ||
abstract contract ERC7984RwaBalanceCapModule is ERC7984RwaComplianceModule { | ||
using EnumerableSet for *; | ||
|
||
euint64 private _maxBalance; | ||
|
||
event MaxBalanceSet(euint64 newMaxBalance); | ||
|
||
constructor(address token) ERC7984RwaComplianceModule(token) { | ||
_token = token; | ||
} | ||
|
||
/// @dev Sets max balance of an investor with proof. | ||
function setMaxBalance(externalEuint64 maxBalance, bytes calldata inputProof) public virtual onlyTokenAdmin { | ||
euint64 maxBalance_ = FHE.fromExternal(maxBalance, inputProof); | ||
FHE.allowThis(_maxBalance = maxBalance_); | ||
emit MaxBalanceSet(maxBalance_); | ||
} | ||
|
||
/// @dev Sets max balance of an investor. | ||
function setMaxBalance(euint64 maxBalance) public virtual onlyTokenAdmin { | ||
FHE.allowThis(_maxBalance = maxBalance); | ||
emit MaxBalanceSet(maxBalance); | ||
} | ||
|
||
/// @dev Gets max balance of an investor. | ||
function getMaxBalance() public view virtual returns (euint64) { | ||
return _maxBalance; | ||
} | ||
|
||
/// @dev Internal function which checks if a transfer is compliant. | ||
function _isCompliantTransfer( | ||
address /*from*/, | ||
address to, | ||
euint64 encryptedAmount | ||
) internal override returns (ebool compliant) { | ||
if (to == address(0)) { | ||
return FHE.asEbool(true); // if burning | ||
} | ||
euint64 balance = IERC7984(_token).confidentialBalanceOf(to); | ||
_getTokenHandleAllowance(balance); | ||
_getTokenHandleAllowance(encryptedAmount); | ||
(ebool increased, euint64 futureBalance) = FHESafeMath.tryIncrease(balance, encryptedAmount); | ||
compliant = FHE.and(increased, FHE.le(futureBalance, _maxBalance)); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
contracts/token/ERC7984/extensions/rwa/ERC7984RwaComplianceModule.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.27; | ||
|
||
import {FHE, ebool, euint64} from "@fhevm/solidity/lib/FHE.sol"; | ||
import {IERC7984Rwa, IERC7984RwaComplianceModule} from "../../../../interfaces/IERC7984Rwa.sol"; | ||
import {HandleAccessManager} from "../../../../utils/HandleAccessManager.sol"; | ||
|
||
/** | ||
* @dev A contract which allows to build a transfer compliance module for confidential Real World Assets (RWAs). | ||
*/ | ||
abstract contract ERC7984RwaComplianceModule is IERC7984RwaComplianceModule, HandleAccessManager { | ||
address internal immutable _token; | ||
|
||
/// @dev The sender is not the token. | ||
error SenderNotToken(address account); | ||
/// @dev The sender is not the token admin. | ||
error SenderNotTokenAdmin(address account); | ||
/// @dev The sender is not a token agent. | ||
error SenderNotTokenAgent(address account); | ||
/// @dev The sender is not the token admin or a token agent. | ||
error SenderNotTokenAdminOrTokenAgent(address account); | ||
|
||
/// @dev Throws if called by any account other than the token. | ||
modifier onlyToken() { | ||
require(msg.sender == _token, SenderNotToken(msg.sender)); | ||
_; | ||
} | ||
|
||
/// @dev Throws if called by any account other than the token admin. | ||
modifier onlyTokenAdmin() { | ||
require(IERC7984Rwa(_token).isAdmin(msg.sender), SenderNotTokenAdmin(msg.sender)); | ||
_; | ||
} | ||
|
||
/// @dev Throws if called by any account other than a token agent. | ||
modifier onlyTokenAgent() { | ||
require(IERC7984Rwa(_token).isAgent(msg.sender), SenderNotTokenAgent(msg.sender)); | ||
_; | ||
} | ||
|
||
/// @dev Throws if called by any account other than the token admin or a token agent. | ||
modifier onlyTokenAdminOrTokenAgent() { | ||
require(IERC7984Rwa(_token).isAdminOrAgent(msg.sender), SenderNotTokenAdminOrTokenAgent(msg.sender)); | ||
_; | ||
} | ||
|
||
constructor(address token) { | ||
_token = token; | ||
} | ||
|
||
/// @inheritdoc IERC7984RwaComplianceModule | ||
function isModule() public pure override returns (bytes4) { | ||
return this.isModule.selector; | ||
} | ||
|
||
/// @inheritdoc IERC7984RwaComplianceModule | ||
function isCompliantTransfer( | ||
address from, | ||
address to, | ||
euint64 encryptedAmount | ||
) public virtual onlyToken returns (ebool compliant) { | ||
FHE.allow(compliant = _isCompliantTransfer(from, to, encryptedAmount), msg.sender); | ||
} | ||
|
||
/// @inheritdoc IERC7984RwaComplianceModule | ||
function postTransfer(address from, address to, euint64 encryptedAmount) public virtual onlyToken { | ||
_postTransfer(from, to, encryptedAmount); | ||
} | ||
|
||
/// @dev Internal function which checks if a transfer is compliant. | ||
function _isCompliantTransfer( | ||
address /*from*/, | ||
address /*to*/, | ||
euint64 /*encryptedAmount*/ | ||
) internal virtual returns (ebool); | ||
|
||
/// @dev Internal function which performs operation after transfer. | ||
function _postTransfer(address /*from*/, address /*to*/, euint64 /*encryptedAmount*/) internal virtual { | ||
// default to no-op | ||
} | ||
|
||
/// @dev Allow modules to get access to token handles during transaction. | ||
function _getTokenHandleAllowance(euint64 handle) internal virtual { | ||
_getTokenHandleAllowance(handle, false); | ||
} | ||
|
||
/// @dev Allow modules to get access to token handles. | ||
function _getTokenHandleAllowance(euint64 handle, bool persistent) internal virtual { | ||
if (FHE.isInitialized(handle)) { | ||
HandleAccessManager(_token).getHandleAllowance(euint64.unwrap(handle), address(this), persistent); | ||
} | ||
} | ||
|
||
function _validateHandleAllowance(bytes32 handle) internal view override onlyTokenAdminOrTokenAgent {} | ||
} |
71 changes: 71 additions & 0 deletions
71
contracts/token/ERC7984/extensions/rwa/ERC7984RwaInvestorCapModule.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.27; | ||
|
||
import {FHE, ebool, euint64} from "@fhevm/solidity/lib/FHE.sol"; | ||
import {IERC7984} from "../../../../interfaces/IERC7984.sol"; | ||
import {ERC7984RwaComplianceModule} from "./ERC7984RwaComplianceModule.sol"; | ||
|
||
/** | ||
* @dev A transfer compliance module for confidential Real World Assets (RWAs) which limits the number of investors. | ||
*/ | ||
abstract contract ERC7984RwaInvestorCapModule is ERC7984RwaComplianceModule { | ||
uint64 private _maxInvestor; | ||
euint64 private _investors; | ||
|
||
event MaxInvestorSet(uint64 maxInvestor); | ||
|
||
constructor(address token, uint64 maxInvestor) ERC7984RwaComplianceModule(token) { | ||
_maxInvestor = maxInvestor; | ||
} | ||
|
||
/// @dev Sets max number of investors. | ||
function setMaxInvestor(uint64 maxInvestor) public virtual onlyTokenAdmin { | ||
_maxInvestor = maxInvestor; | ||
emit MaxInvestorSet(maxInvestor); | ||
} | ||
|
||
/// @dev Gets max number of investors. | ||
function getMaxInvestor() public view virtual returns (uint64) { | ||
return _maxInvestor; | ||
} | ||
|
||
/// @dev Gets current number of investors. | ||
function getCurrentInvestor() public view virtual returns (euint64) { | ||
return _investors; | ||
} | ||
|
||
/// @dev Internal function which checks if a transfer is compliant. | ||
function _isCompliantTransfer( | ||
address /*from*/, | ||
address to, | ||
euint64 encryptedAmount | ||
) internal override returns (ebool compliant) { | ||
euint64 balance = IERC7984(_token).confidentialBalanceOf(to); | ||
_getTokenHandleAllowance(balance); | ||
_getTokenHandleAllowance(encryptedAmount); | ||
compliant = FHE.or( | ||
FHE.or( | ||
FHE.asEbool(to == address(0)), // return true if burning | ||
FHE.eq(encryptedAmount, FHE.asEuint64(0)) // or zero amount | ||
), | ||
FHE.or( | ||
FHE.gt(balance, FHE.asEuint64(0)), // or already investor | ||
FHE.lt(_investors, FHE.asEuint64(_maxInvestor)) // or not reached max investors limit | ||
) | ||
); | ||
} | ||
|
||
/// @dev Internal function which performs operation after transfer. | ||
function _postTransfer(address /*from*/, address to, euint64 encryptedAmount) internal override { | ||
euint64 balance = IERC7984(_token).confidentialBalanceOf(to); | ||
_getTokenHandleAllowance(balance); | ||
_getTokenHandleAllowance(encryptedAmount); | ||
if (!FHE.isInitialized(_investors)) { | ||
_investors = FHE.asEuint64(0); | ||
} | ||
_investors = FHE.select(FHE.eq(balance, encryptedAmount), FHE.add(_investors, FHE.asEuint64(1)), _investors); | ||
_investors = FHE.select(FHE.eq(balance, FHE.asEuint64(0)), FHE.sub(_investors, FHE.asEuint64(1)), _investors); | ||
FHE.allowThis(_investors); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
🧩 Analysis chain
Verify redundant assignment.
Line 22 assigns
_token = token
, but the parent constructorERC7984RwaComplianceModule(token)
is called on line 21. If the parent constructor already sets_token
, this assignment is redundant.Check the parent contract to confirm:
🏁 Script executed:
Length of output: 0
🏁 Script executed:
Length of output: 764
Remove redundant assignment in constructor
The parent ERC7984RwaComplianceModule constructor already sets
_token
, so the child’s_token = token;
can be removed.🤖 Prompt for AI Agents