-
Notifications
You must be signed in to change notification settings - Fork 12.4k
ERC20Bridgable (ERC-7802) #5735
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
56f1a34
Add ERC20 bridgable
Amxx 905d0fa
pragma
Amxx c4d2e38
Update draft-ERC20Bridgeable.sol
Amxx 7a4d113
nits
ernestognw ed5de69
Add to interface docs
arr00 e972ed2
Update contracts/token/ERC20/extensions/draft-ERC20Bridgeable.sol
ernestognw 97e67ac
up
ernestognw 19fc7ab
Apply suggestions from code review
arr00 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-solidity': minor | ||
| --- | ||
|
|
||
| `ERC20Bridgeable`: Implementation of ERC-7802 that makes an ERC-20 compatible with crosschain bridges. |
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,30 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity >=0.6.2; | ||
|
|
||
| import {IERC165} from "./IERC165.sol"; | ||
|
|
||
| /// @title IERC7802 | ||
| /// @notice Defines the interface for crosschain ERC20 transfers. | ||
| interface IERC7802 is IERC165 { | ||
| /// @notice Emitted when a crosschain transfer mints tokens. | ||
| /// @param to Address of the account tokens are being minted for. | ||
| /// @param amount Amount of tokens minted. | ||
| /// @param sender Address of the caller (msg.sender) who invoked crosschainMint. | ||
| event CrosschainMint(address indexed to, uint256 amount, address indexed sender); | ||
|
|
||
| /// @notice Emitted when a crosschain transfer burns tokens. | ||
| /// @param from Address of the account tokens are being burned from. | ||
| /// @param amount Amount of tokens burned. | ||
| /// @param sender Address of the caller (msg.sender) who invoked crosschainBurn. | ||
| event CrosschainBurn(address indexed from, uint256 amount, address indexed sender); | ||
|
|
||
| /// @notice Mint tokens through a crosschain transfer. | ||
| /// @param _to Address to mint tokens to. | ||
| /// @param _amount Amount of tokens to mint. | ||
| function crosschainMint(address _to, uint256 _amount) external; | ||
|
|
||
| /// @notice Burn tokens through a crosschain transfer. | ||
| /// @param _from Address to burn tokens from. | ||
| /// @param _amount Amount of tokens to burn. | ||
| function crosschainBurn(address _from, uint256 _amount) external; | ||
| } | ||
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,26 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| pragma solidity ^0.8.20; | ||
|
|
||
| import {ERC20, ERC20Bridgeable} from "../../token/ERC20/extensions/draft-ERC20Bridgeable.sol"; | ||
|
|
||
| abstract contract ERC20BridgeableMock is ERC20Bridgeable { | ||
| address private _bridge; | ||
|
|
||
| error OnlyTokenBridge(); | ||
| event OnlyTokenBridgeFnCalled(address caller); | ||
|
|
||
| constructor(address bridge) { | ||
| _bridge = bridge; | ||
| } | ||
|
|
||
| function onlyTokenBridgeFn() external onlyTokenBridge { | ||
| emit OnlyTokenBridgeFnCalled(msg.sender); | ||
| } | ||
|
|
||
| function _checkTokenBridge(address sender) internal view override { | ||
| if (sender != _bridge) { | ||
| revert OnlyTokenBridge(); | ||
| } | ||
| } | ||
| } |
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
50 changes: 50 additions & 0 deletions
50
contracts/token/ERC20/extensions/draft-ERC20Bridgeable.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,50 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| pragma solidity ^0.8.20; | ||
|
|
||
| import {ERC20} from "../ERC20.sol"; | ||
| import {ERC165, IERC165} from "../../../utils/introspection/ERC165.sol"; | ||
| import {IERC7802} from "../../../interfaces/draft-IERC7802.sol"; | ||
|
|
||
| /** | ||
| * @dev ERC20 extension that implements the standard token interface according to | ||
| * https://eips.ethereum.org/EIPS/eip-7802[ERC-7802]. | ||
| */ | ||
| abstract contract ERC20Bridgeable is ERC20, ERC165, IERC7802 { | ||
| /// @dev Modifier to restrict access to the token bridge. | ||
| modifier onlyTokenBridge() { | ||
| // Token bridge should never be impersonated using a relayer/forwarder. Using msg.sender is preferable to | ||
| // _msgSender() for security reasons. | ||
| _checkTokenBridge(msg.sender); | ||
| _; | ||
| } | ||
|
|
||
| /// @inheritdoc ERC165 | ||
| function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) { | ||
| return interfaceId == type(IERC7802).interfaceId || super.supportsInterface(interfaceId); | ||
| } | ||
|
|
||
| /** | ||
| * @dev See {IERC7802-crosschainMint}. Emits a {IERC7802-CrosschainMint} event. | ||
| */ | ||
| function crosschainMint(address to, uint256 value) public virtual override onlyTokenBridge { | ||
| _mint(to, value); | ||
| emit CrosschainMint(to, value, _msgSender()); | ||
| } | ||
|
|
||
| /** | ||
| * @dev See {IERC7802-crosschainBurn}. Emits a {IERC7802-CrosschainBurn} event. | ||
| */ | ||
| function crosschainBurn(address from, uint256 value) public virtual override onlyTokenBridge { | ||
| _burn(from, value); | ||
| emit CrosschainBurn(from, value, _msgSender()); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Checks if the caller is a trusted token bridge. MUST revert otherwise. | ||
| * | ||
| * Developers should implement this function using an access control mechanism that allows | ||
| * customizing the list of allowed senders. Consider using {AccessControl} or {AccessManaged}. | ||
| */ | ||
| function _checkTokenBridge(address caller) internal virtual; | ||
| } |
ernestognw marked this conversation as resolved.
Show resolved
Hide resolved
|
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,89 @@ | ||
| const { ethers } = require('hardhat'); | ||
| const { expect } = require('chai'); | ||
| const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers'); | ||
|
|
||
| const { shouldBehaveLikeERC20 } = require('../ERC20.behavior.js'); | ||
| const { shouldSupportInterfaces } = require('../../../utils/introspection/SupportsInterface.behavior'); | ||
|
|
||
| const name = 'My Token'; | ||
| const symbol = 'MTKN'; | ||
| const initialSupply = 100n; | ||
|
|
||
| async function fixture() { | ||
| const [other, bridge, ...accounts] = await ethers.getSigners(); | ||
|
|
||
| const token = await ethers.deployContract('$ERC20BridgeableMock', [name, symbol, bridge]); | ||
| await token.$_mint(accounts[0], initialSupply); | ||
|
|
||
| return { bridge, other, accounts, token }; | ||
| } | ||
|
|
||
| describe('ERC20Bridgeable', function () { | ||
| beforeEach(async function () { | ||
| Object.assign(this, await loadFixture(fixture)); | ||
| }); | ||
|
|
||
| describe('onlyTokenBridgeFn', function () { | ||
| it('reverts when called by non-bridge', async function () { | ||
| await expect(this.token.onlyTokenBridgeFn()).to.be.revertedWithCustomError(this.token, 'OnlyTokenBridge'); | ||
| }); | ||
|
|
||
| it('does not revert when called by bridge', async function () { | ||
| await expect(this.token.connect(this.bridge).onlyTokenBridgeFn()) | ||
| .to.emit(this.token, 'OnlyTokenBridgeFnCalled') | ||
| .withArgs(this.bridge); | ||
| }); | ||
| }); | ||
|
|
||
| describe('crosschainMint', function () { | ||
| it('reverts when called by non-bridge', async function () { | ||
| await expect(this.token.crosschainMint(this.other, 100n)).to.be.revertedWithCustomError( | ||
| this.token, | ||
| 'OnlyTokenBridge', | ||
| ); | ||
| }); | ||
|
|
||
| it('mints amount provided by the bridge when calling crosschainMint', async function () { | ||
| const amount = 100n; | ||
| await expect(this.token.connect(this.bridge).crosschainMint(this.other, amount)) | ||
| .to.emit(this.token, 'CrosschainMint') | ||
| .withArgs(this.other, amount, this.bridge) | ||
| .to.emit(this.token, 'Transfer') | ||
| .withArgs(ethers.ZeroAddress, this.other, amount); | ||
|
|
||
| await expect(this.token.balanceOf(this.other)).to.eventually.equal(amount); | ||
| }); | ||
| }); | ||
|
|
||
| describe('crosschainBurn', function () { | ||
| it('reverts when called by non-bridge', async function () { | ||
| await expect(this.token.crosschainBurn(this.other, 100n)).to.be.revertedWithCustomError( | ||
| this.token, | ||
| 'OnlyTokenBridge', | ||
| ); | ||
| }); | ||
|
|
||
| it('burns amount provided by the bridge when calling crosschainBurn', async function () { | ||
| const amount = 100n; | ||
| await this.token.$_mint(this.other, amount); | ||
|
|
||
| await expect(this.token.connect(this.bridge).crosschainBurn(this.other, amount)) | ||
| .to.emit(this.token, 'CrosschainBurn') | ||
| .withArgs(this.other, amount, this.bridge) | ||
| .to.emit(this.token, 'Transfer') | ||
| .withArgs(this.other, ethers.ZeroAddress, amount); | ||
|
|
||
| await expect(this.token.balanceOf(this.other)).to.eventually.equal(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe('ERC165', function () { | ||
| shouldSupportInterfaces({ | ||
| ERC7802: ['crosschainMint(address,uint256)', 'crosschainBurn(address,uint256)'], | ||
| }); | ||
| }); | ||
|
|
||
| describe('ERC20 behavior', function () { | ||
| shouldBehaveLikeERC20(initialSupply); | ||
| }); | ||
| }); |
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.
Lots of non-dev tags here--won't be parsed for documentation.
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.
Yes, it's true we have been not too strict with that for interfaces and we try to keep them as defined in their corresponding ERCs (which was the case for 7802 as far as I remember). I would be in favor of rendering
@paramand@returnsin the interfaces page only.