Skip to content

Commit

Permalink
Revert "refactor(protocol): add batch transfer and burn for BridgedER…
Browse files Browse the repository at this point in the history
…C721 (#17058)"

This reverts commit cfc8fa8.
  • Loading branch information
dantaik committed May 9, 2024
1 parent 4d851fc commit 5c7e26b
Show file tree
Hide file tree
Showing 22 changed files with 194 additions and 111 deletions.
2 changes: 1 addition & 1 deletion packages/protocol/contracts/L1/TaikoData.sol
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ library TaikoData {
mapping(
uint64 blockId_mod_blockRingBufferSize
=> mapping(uint32 transitionId => TransitionState ts)
) transitions;
) transitions;
// Ring buffer for Ether deposits
bytes32 __reserve1;
SlotA slotA; // slot 5
Expand Down
31 changes: 12 additions & 19 deletions packages/protocol/contracts/tokenvault/BridgedERC1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ pragma solidity 0.8.24;
import "@openzeppelin/contracts-upgradeable/token/ERC1155/ERC1155Upgradeable.sol";
import "../common/EssentialContract.sol";
import "../common/LibStrings.sol";
import "./IBridgedERC1155.sol";
import "./LibBridgedToken.sol";

/// @title BridgedERC1155
/// @notice Contract for bridging ERC1155 tokens across different chains.
/// @custom:security-contact security@taiko.xyz
contract BridgedERC1155 is EssentialContract, ERC1155Upgradeable {
contract BridgedERC1155 is
EssentialContract,
IBridgedERC1155,
IBridgedERC1155Init,
ERC1155Upgradeable
{
/// @notice Address of the source token contract.
address public srcToken;

Expand All @@ -27,13 +33,7 @@ contract BridgedERC1155 is EssentialContract, ERC1155Upgradeable {
error BTOKEN_INVALID_PARAMS();
error BTOKEN_INVALID_TO_ADDR();

/// @notice Initializes the contract.
/// @param _owner The owner of this contract. msg.sender will be used if this value is zero.
/// @param _addressManager The address of the {AddressManager} contract.
/// @param _srcToken Address of the source token.
/// @param _srcChainId Source chain ID.
/// @param _symbol Symbol of the bridged token.
/// @param _name Name of the bridged token.
/// @inheritdoc IBridgedERC1155Init
function init(
address _owner,
address _addressManager,
Expand Down Expand Up @@ -61,10 +61,7 @@ contract BridgedERC1155 is EssentialContract, ERC1155Upgradeable {
name = _name;
}

/// @dev Mints tokens.
/// @param _to Address to receive the minted tokens.
/// @param _tokenIds ID of the token to mint.
/// @param _amounts Amount of tokens to mint.
/// @inheritdoc IBridgedERC1155
function mintBatch(
address _to,
uint256[] calldata _tokenIds,
Expand All @@ -78,9 +75,7 @@ contract BridgedERC1155 is EssentialContract, ERC1155Upgradeable {
_mintBatch(_to, _tokenIds, _amounts, "");
}

/// @dev Batch burns tokens.
/// @param _ids Array of IDs of the tokens to burn.
/// @param _amounts Amount of tokens to burn respectively.
/// @inheritdoc IBridgedERC1155
function burnBatch(
uint256[] calldata _ids,
uint256[] calldata _amounts
Expand All @@ -93,10 +88,8 @@ contract BridgedERC1155 is EssentialContract, ERC1155Upgradeable {
_burnBatch(msg.sender, _ids, _amounts);
}

/// @notice Gets the canonical token's address and chain ID.
/// @return The canonical token's address.
/// @return The canonical token's chain ID.
function canonical() public view returns (address, uint256) {
/// @inheritdoc IBridgedERC1155
function canonical() external view returns (address, uint256) {
return (srcToken, srcChainId);
}

Expand Down
41 changes: 13 additions & 28 deletions packages/protocol/contracts/tokenvault/BridgedERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import "./LibBridgedToken.sol";
/// @notice An upgradeable ERC20 contract that represents tokens bridged from
/// another chain.
/// @custom:security-contact security@taiko.xyz
contract BridgedERC20 is EssentialContract, IBridgedERC20, ERC20Upgradeable {
contract BridgedERC20 is EssentialContract, IBridgedERC20, IBridgedERC20Init, ERC20Upgradeable {
/// @dev Slot 1.
address public srcToken;

Expand Down Expand Up @@ -50,14 +50,7 @@ contract BridgedERC20 is EssentialContract, IBridgedERC20, ERC20Upgradeable {
error BTOKEN_INVALID_TO_ADDR();
error BTOKEN_MINT_DISALLOWED();

/// @notice Initializes the contract.
/// @param _owner The owner of this contract. msg.sender will be used if this value is zero.
/// @param _addressManager The address of the {AddressManager} contract.
/// @param _srcToken The source token address.
/// @param _srcChainId The source chain ID.
/// @param _decimals The number of decimal places of the source token.
/// @param _symbol The symbol of the token.
/// @param _name The name of the token.
/// @inheritdoc IBridgedERC20Init
function init(
address _owner,
address _addressManager,
Expand All @@ -81,9 +74,7 @@ contract BridgedERC20 is EssentialContract, IBridgedERC20, ERC20Upgradeable {
__srcDecimals = _decimals;
}

/// @notice Start or stop migration to/from a specified contract.
/// @param _migratingAddress The address migrating 'to' or 'from'.
/// @param _migratingInbound If false then signals migrating 'from', true if migrating 'into'.
/// @inheritdoc IBridgedERC20
function changeMigrationStatus(
address _migratingAddress,
bool _migratingInbound
Expand All @@ -102,9 +93,7 @@ contract BridgedERC20 is EssentialContract, IBridgedERC20, ERC20Upgradeable {
emit MigrationStatusChanged(_migratingAddress, _migratingInbound);
}

/// @notice Mints tokens to the specified account.
/// @param _account The address of the account to receive the tokens.
/// @param _amount The amount of tokens to mint.
/// @inheritdoc IBridgedERC20
function mint(address _account, uint256 _amount) external whenNotPaused nonReentrant {
// mint is disabled while migrating outbound.
if (isMigratingOut()) revert BTOKEN_MINT_DISALLOWED();
Expand All @@ -121,9 +110,7 @@ contract BridgedERC20 is EssentialContract, IBridgedERC20, ERC20Upgradeable {
_mint(_account, _amount);
}

/// @notice Burns tokens in case of 'migrating out' from msg.sender (EOA) or from the ERC20Vault
/// if bridging back to canonical token.
/// @param _amount The amount of tokens to burn.
/// @inheritdoc IBridgedERC20
function burn(uint256 _amount) external whenNotPaused nonReentrant {
if (isMigratingOut()) {
// Outbound migration
Expand All @@ -140,16 +127,8 @@ contract BridgedERC20 is EssentialContract, IBridgedERC20, ERC20Upgradeable {
_burn(msg.sender, _amount);
}

/// @notice Gets the number of decimal places of the token.
/// @return The number of decimal places of the token.
function decimals() public view override returns (uint8) {
return __srcDecimals;
}

/// @notice Gets the canonical token's address and chain ID.
/// @return The canonical token's address.
/// @return The canonical token's chain ID.
function canonical() public view returns (address, uint256) {
/// @inheritdoc IBridgedERC20
function canonical() external view returns (address, uint256) {
return (srcToken, srcChainId);
}

Expand All @@ -159,6 +138,12 @@ contract BridgedERC20 is EssentialContract, IBridgedERC20, ERC20Upgradeable {
return OwnableUpgradeable.owner();
}

/// @notice Gets the number of decimal places of the token.
/// @return The number of decimal places of the token.
function decimals() public view override returns (uint8) {
return __srcDecimals;
}

function isMigratingOut() public view returns (bool) {
return migratingAddress != address(0) && !migratingInbound;
}
Expand Down
36 changes: 12 additions & 24 deletions packages/protocol/contracts/tokenvault/BridgedERC721.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ pragma solidity 0.8.24;
import "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol";
import "../common/EssentialContract.sol";
import "../common/LibStrings.sol";
import "./IBridgedERC721.sol";
import "./LibBridgedToken.sol";

/// @title BridgedERC721
/// @notice Contract for bridging ERC721 tokens across different chains.
/// @custom:security-contact security@taiko.xyz
contract BridgedERC721 is EssentialContract, ERC721Upgradeable {
contract BridgedERC721 is
EssentialContract,
IBridgedERC721,
IBridgedERC721Init,
ERC721Upgradeable
{
/// @notice Address of the source token contract.
address public srcToken;

Expand All @@ -22,13 +28,7 @@ contract BridgedERC721 is EssentialContract, ERC721Upgradeable {
error BTOKEN_INVALID_TO_ADDR();
error BTOKEN_INVALID_BURN();

/// @notice Initializes the contract.
/// @param _owner The owner of this contract. msg.sender will be used if this value is zero.
/// @param _addressManager The address of the {AddressManager} contract.
/// @param _srcToken Address of the source token.
/// @param _srcChainId Source chain ID.
/// @param _symbol Symbol of the bridged token.
/// @param _name Name of the bridged token.
/// @inheritdoc IBridgedERC721Init
function init(
address _owner,
address _addressManager,
Expand All @@ -49,9 +49,7 @@ contract BridgedERC721 is EssentialContract, ERC721Upgradeable {
srcChainId = _srcChainId;
}

/// @dev Mints tokens.
/// @param _account Address to receive the minted token.
/// @param _tokenId ID of the token to mint.
/// @inheritdoc IBridgedERC721
function mint(
address _account,
uint256 _tokenId
Expand All @@ -64,8 +62,7 @@ contract BridgedERC721 is EssentialContract, ERC721Upgradeable {
_safeMint(_account, _tokenId);
}

/// @dev Burns tokens.
/// @param _tokenId ID of the token to burn.
/// @inheritdoc IBridgedERC721
function burn(uint256 _tokenId)
external
whenNotPaused
Expand All @@ -80,10 +77,8 @@ contract BridgedERC721 is EssentialContract, ERC721Upgradeable {
_burn(_tokenId);
}

/// @notice Gets the source token and source chain ID being bridged.
/// @return The source token's address.
/// @return The source token's chain ID.
function source() public view returns (address, uint256) {
/// @inheritdoc IBridgedERC721
function canonical() external view returns (address, uint256) {
return (srcToken, srcChainId);
}

Expand All @@ -97,13 +92,6 @@ contract BridgedERC721 is EssentialContract, ERC721Upgradeable {
return LibBridgedToken.buildURI(srcToken, srcChainId, Strings.toString(_tokenId));
}

/// @notice Gets the canonical token's address and chain ID.
/// @return The canonical token's address.
/// @return The canonical token's chain ID.
function canonical() public view returns (address, uint256) {
return (srcToken, srcChainId);
}

function _beforeTokenTransfer(
address _from,
address _to,
Expand Down
20 changes: 8 additions & 12 deletions packages/protocol/contracts/tokenvault/ERC1155Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC1155/utils/ERC1155ReceiverUpgradeable.sol";
import "../libs/LibAddress.sol";
import "../common/LibStrings.sol";
import "./IBridgedERC1155.sol";
import "./BaseNFTVault.sol";
import "./BridgedERC1155.sol";

/// @title ERC1155Vault
/// @dev Labeled in AddressResolver as "erc1155_vault"
Expand Down Expand Up @@ -220,7 +220,7 @@ contract ERC1155Vault is BaseNFTVault, ERC1155ReceiverUpgradeable {
} else {
// Token does not live on this chain
token = _getOrDeployBridgedToken(ctoken);
BridgedERC1155(token).mintBatch(to, tokenIds, amounts);
IBridgedERC1155(token).mintBatch(to, tokenIds, amounts);
}
}

Expand All @@ -238,10 +238,10 @@ contract ERC1155Vault is BaseNFTVault, ERC1155ReceiverUpgradeable {
CanonicalNFT storage _ctoken = bridgedToCanonical[_op.token];
if (_ctoken.addr != address(0)) {
ctoken_ = _ctoken;
BridgedERC1155(_op.token).safeBatchTransferFrom(
IERC1155(_op.token).safeBatchTransferFrom(
msg.sender, address(this), _op.tokenIds, _op.amounts, ""
);
BridgedERC1155(_op.token).burnBatch(_op.tokenIds, _op.amounts);
IBridgedERC1155(_op.token).burnBatch(_op.tokenIds, _op.amounts);
} else {
// is a ctoken token, meaning, it lives on this chain
ctoken_ = CanonicalNFT({
Expand All @@ -251,13 +251,9 @@ contract ERC1155Vault is BaseNFTVault, ERC1155ReceiverUpgradeable {
name: safeName(_op.token)
});

IERC1155(_op.token).safeBatchTransferFrom({
from: msg.sender,
to: address(this),
ids: _op.tokenIds,
amounts: _op.amounts,
data: ""
});
IERC1155(_op.token).safeBatchTransferFrom(
msg.sender, address(this), _op.tokenIds, _op.amounts, ""
);
}
}
msgData_ = abi.encodeCall(
Expand Down Expand Up @@ -286,7 +282,7 @@ contract ERC1155Vault is BaseNFTVault, ERC1155ReceiverUpgradeable {
/// @return btoken_ Address of the deployed bridged token contract.
function _deployBridgedToken(CanonicalNFT memory _ctoken) private returns (address btoken_) {
bytes memory data = abi.encodeCall(
BridgedERC1155.init,
IBridgedERC1155Init.init,
(owner(), addressManager, _ctoken.addr, _ctoken.chainId, _ctoken.symbol, _ctoken.name)
);

Expand Down
2 changes: 1 addition & 1 deletion packages/protocol/contracts/tokenvault/ERC20Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ contract ERC20Vault is BaseVault {
/// @return btoken Address of the deployed bridged token contract.
function _deployBridgedToken(CanonicalERC20 memory ctoken) private returns (address btoken) {
bytes memory data = abi.encodeCall(
BridgedERC20.init,
IBridgedERC20Init.init,
(
owner(),
addressManager,
Expand Down
16 changes: 6 additions & 10 deletions packages/protocol/contracts/tokenvault/ERC721Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
import "../libs/LibAddress.sol";
import "../common/LibStrings.sol";
import "./IBridgedERC721.sol";
import "./BaseNFTVault.sol";
import "./BridgedERC721.sol";

/// @title ERC721Vault
/// @notice This vault holds all ERC721 tokens that users have deposited. It also manages
Expand Down Expand Up @@ -178,7 +178,7 @@ contract ERC721Vault is BaseNFTVault, IERC721Receiver {
} else {
token_ = _getOrDeployBridgedToken(_ctoken);
for (uint256 i; i < _tokenIds.length; ++i) {
BridgedERC721(token_).mint(_to, _tokenIds[i]);
IBridgedERC721(token_).mint(_to, _tokenIds[i]);
}
}
}
Expand All @@ -197,10 +197,8 @@ contract ERC721Vault is BaseNFTVault, IERC721Receiver {
if (_ctoken.addr != address(0)) {
ctoken_ = _ctoken;
for (uint256 i; i < _op.tokenIds.length; ++i) {
BridgedERC721(_op.token).safeTransferFrom(
msg.sender, address(this), _op.tokenIds[i]
);
BridgedERC721(_op.token).burn(_op.tokenIds[i]);
IERC721(_op.token).safeTransferFrom(msg.sender, address(this), _op.tokenIds[i]);
IBridgedERC721(_op.token).burn(_op.tokenIds[i]);
}
} else {
ctoken_ = CanonicalNFT({
Expand All @@ -211,9 +209,7 @@ contract ERC721Vault is BaseNFTVault, IERC721Receiver {
});

for (uint256 i; i < _op.tokenIds.length; ++i) {
ERC721Upgradeable(_op.token).safeTransferFrom(
msg.sender, address(this), _op.tokenIds[i]
);
IERC721(_op.token).safeTransferFrom(msg.sender, address(this), _op.tokenIds[i]);
}
}
}
Expand Down Expand Up @@ -244,7 +240,7 @@ contract ERC721Vault is BaseNFTVault, IERC721Receiver {
/// @return btoken_ Address of the deployed bridged token contract.
function _deployBridgedToken(CanonicalNFT memory _ctoken) private returns (address btoken_) {
bytes memory data = abi.encodeCall(
BridgedERC721.init,
IBridgedERC721Init.init,
(owner(), addressManager, _ctoken.addr, _ctoken.chainId, _ctoken.symbol, _ctoken.name)
);

Expand Down
Loading

0 comments on commit 5c7e26b

Please sign in to comment.