From 55d9c4765bab6cf1a86070ef613c0cac158c4c9d Mon Sep 17 00:00:00 2001 From: adaki2004 Date: Thu, 31 Aug 2023 10:18:06 +0200 Subject: [PATCH 1/8] Combine new and old to make upgradeable --- .../protocol/contracts/tokenvault/ERC20Vault.sol | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/protocol/contracts/tokenvault/ERC20Vault.sol b/packages/protocol/contracts/tokenvault/ERC20Vault.sol index 867a17152d1..5d648dea960 100644 --- a/packages/protocol/contracts/tokenvault/ERC20Vault.sol +++ b/packages/protocol/contracts/tokenvault/ERC20Vault.sol @@ -330,13 +330,21 @@ contract ERC20Vault is private returns (address btoken) { - ProxiedBridgedERC20 bridgedToken = new ProxiedBridgedERC20(); + address bridgedToken = Create2Upgradeable.deploy({ + amount: 0, // amount of Ether to send + salt: keccak256( + bytes.concat( + bytes32(ctoken.chainId), bytes32(uint256(uint160(ctoken.addr))) + ) + ), + bytecode: type(ProxiedBridgedERC20).creationCode + }); btoken = LibVaultUtils.deployProxy( address(bridgedToken), owner(), bytes.concat( - bridgedToken.init.selector, + ProxiedBridgedERC20(bridgedToken).init.selector, abi.encode( address(_addressManager), ctoken.addr, From f36f9d36145f0bc134fdff8f02de4048038342ca Mon Sep 17 00:00:00 2001 From: adaki2004 Date: Thu, 31 Aug 2023 10:18:38 +0200 Subject: [PATCH 2/8] indentation From 6f3d60bdf119f8c29c31daa4c18b9f73e3ef0a1b Mon Sep 17 00:00:00 2001 From: adaki2004 Date: Fri, 1 Sep 2023 09:47:59 +0200 Subject: [PATCH 3/8] Adjust to integration tests --- .../contracts/tokenvault/ERC1155Vault.sol | 26 +++++++++++++++---- .../contracts/tokenvault/ERC20Vault.sol | 26 +++++++++++++++---- .../contracts/tokenvault/ERC721Vault.sol | 10 +++++-- 3 files changed, 50 insertions(+), 12 deletions(-) diff --git a/packages/protocol/contracts/tokenvault/ERC1155Vault.sol b/packages/protocol/contracts/tokenvault/ERC1155Vault.sol index 5e42f772709..57f1ff8d669 100644 --- a/packages/protocol/contracts/tokenvault/ERC1155Vault.sol +++ b/packages/protocol/contracts/tokenvault/ERC1155Vault.sol @@ -261,6 +261,10 @@ contract ERC1155Vault is BaseNFTVault, ERC1155ReceiverUpgradeable { || super.supportsInterface(interfaceId); } + /// @dev Encodes sending bridged or canonical ERC1155 tokens to the user. + /// @param user The user's address. + /// @param opt BridgeTransferOp data. + /// @return msgData Encoded message data. function _encodeDestinationCall( address user, BridgeTransferOp memory opt @@ -314,7 +318,9 @@ contract ERC1155Vault is BaseNFTVault, ERC1155ReceiverUpgradeable { ); } - /// @dev Returns the contract address per given canonical token. + /// @dev Retrieve or deploy a bridged ERC1155 token contract. + /// @param ctoken CanonicalNFT data. + /// @return btoken Address of the bridged token contract. function _getOrDeployBridgedToken(CanonicalNFT memory ctoken) private returns (address btoken) @@ -325,18 +331,28 @@ contract ERC1155Vault is BaseNFTVault, ERC1155ReceiverUpgradeable { } } - /// @dev Deploys a new BridgedNFT contract and initializes it. This must be - /// called before the first time a btoken is sent to this chain. + /// @dev Deploy a new BridgedNFT contract and initialize it. + /// This must be called before the first time a bridged token is sent to + /// this chain. + /// @param ctoken CanonicalNFT data. + /// @return btoken Address of the deployed bridged token contract. function _deployBridgedToken(CanonicalNFT memory ctoken) private returns (address btoken) { - ProxiedBridgedERC1155 bridgedToken = new ProxiedBridgedERC1155(); + bytes32 salt = keccak256(abi.encode(ctoken)); + + address bridgedToken = Create2Upgradeable.deploy({ + amount: 0, // amount of Ether to send + salt: salt, + bytecode: type(ProxiedBridgedERC1155).creationCode + }); + btoken = LibVaultUtils.deployProxy( address(bridgedToken), owner(), bytes.concat( - bridgedToken.init.selector, + ProxiedBridgedERC1155(bridgedToken).init.selector, abi.encode( address(_addressManager), ctoken.addr, diff --git a/packages/protocol/contracts/tokenvault/ERC20Vault.sol b/packages/protocol/contracts/tokenvault/ERC20Vault.sol index 5d648dea960..ff695adde1f 100644 --- a/packages/protocol/contracts/tokenvault/ERC20Vault.sol +++ b/packages/protocol/contracts/tokenvault/ERC20Vault.sol @@ -268,6 +268,16 @@ contract ERC20Vault is return interfaceId == type(IRecallableMessageSender).interfaceId; } + /// @dev Encodes sending bridged or canonical ERC20 tokens to the user. + /// @param user The user's address. + /// @param token The token address. + /// @param to To address. + /// @param amount Amount to be sent. + /// @return msgData Encoded message data. + /// @return _amount Token amount left in the contract. (Todo Daniel: why we + /// calcualte the left amount ? Why is it necessary ? It seems ambigous that + /// in one case we just return the amount while in other case we calculate + /// the left tokens.) function _encodeDestinationCall( address user, address token, @@ -315,6 +325,9 @@ contract ERC20Vault is ); } + /// @dev Retrieve or deploy a bridged ERC20 token contract. + /// @param ctoken CanonicalERC20 data. + /// @return btoken Address of the bridged token contract. function _getOrDeployBridgedToken(CanonicalERC20 calldata ctoken) private returns (address btoken) @@ -326,17 +339,20 @@ contract ERC20Vault is } } + /// @dev Deploy a new BridgedERC20 contract and initialize it. + /// This must be called before the first time a bridged token is sent to + /// this chain. + /// @param ctoken CanonicalERC20 data. + /// @return btoken Address of the deployed bridged token contract. function _deployBridgedToken(CanonicalERC20 calldata ctoken) private returns (address btoken) { + bytes32 salt = keccak256(abi.encode(ctoken)); + address bridgedToken = Create2Upgradeable.deploy({ amount: 0, // amount of Ether to send - salt: keccak256( - bytes.concat( - bytes32(ctoken.chainId), bytes32(uint256(uint160(ctoken.addr))) - ) - ), + salt: salt, bytecode: type(ProxiedBridgedERC20).creationCode }); diff --git a/packages/protocol/contracts/tokenvault/ERC721Vault.sol b/packages/protocol/contracts/tokenvault/ERC721Vault.sol index bf3925405e1..249ce4262cf 100644 --- a/packages/protocol/contracts/tokenvault/ERC721Vault.sol +++ b/packages/protocol/contracts/tokenvault/ERC721Vault.sol @@ -281,13 +281,19 @@ contract ERC721Vault is BaseNFTVault, IERC721Receiver, IERC165Upgradeable { private returns (address btoken) { - ProxiedBridgedERC721 bridgedToken = new ProxiedBridgedERC721(); + bytes32 salt = keccak256(abi.encode(ctoken)); + + address bridgedToken = Create2Upgradeable.deploy({ + amount: 0, // amount of Ether to send + salt: salt, + bytecode: type(ProxiedBridgedERC721).creationCode + }); btoken = LibVaultUtils.deployProxy( address(bridgedToken), owner(), bytes.concat( - bridgedToken.init.selector, + ProxiedBridgedERC721(bridgedToken).init.selector, abi.encode( address(_addressManager), ctoken.addr, From a290366efee5aa23f1b6c12e2623c3660749fe9c Mon Sep 17 00:00:00 2001 From: Daniel Wang <99078276+dantaik@users.noreply.github.com> Date: Fri, 1 Sep 2023 14:08:20 +0000 Subject: [PATCH 4/8] Update ERC721Vault.sol --- packages/protocol/contracts/tokenvault/ERC721Vault.sol | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/protocol/contracts/tokenvault/ERC721Vault.sol b/packages/protocol/contracts/tokenvault/ERC721Vault.sol index 249ce4262cf..70b2fa029e5 100644 --- a/packages/protocol/contracts/tokenvault/ERC721Vault.sol +++ b/packages/protocol/contracts/tokenvault/ERC721Vault.sol @@ -281,11 +281,9 @@ contract ERC721Vault is BaseNFTVault, IERC721Receiver, IERC165Upgradeable { private returns (address btoken) { - bytes32 salt = keccak256(abi.encode(ctoken)); - address bridgedToken = Create2Upgradeable.deploy({ amount: 0, // amount of Ether to send - salt: salt, + salt: keccak256(abi.encode(ctoken)), bytecode: type(ProxiedBridgedERC721).creationCode }); From cf5e11ea0d324c3b9d46c74b2ee8c797d0729222 Mon Sep 17 00:00:00 2001 From: Daniel Wang <99078276+dantaik@users.noreply.github.com> Date: Fri, 1 Sep 2023 14:08:46 +0000 Subject: [PATCH 5/8] Update ERC20Vault.sol --- packages/protocol/contracts/tokenvault/ERC20Vault.sol | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/protocol/contracts/tokenvault/ERC20Vault.sol b/packages/protocol/contracts/tokenvault/ERC20Vault.sol index ff695adde1f..d97900a3c60 100644 --- a/packages/protocol/contracts/tokenvault/ERC20Vault.sol +++ b/packages/protocol/contracts/tokenvault/ERC20Vault.sol @@ -348,11 +348,9 @@ contract ERC20Vault is private returns (address btoken) { - bytes32 salt = keccak256(abi.encode(ctoken)); - address bridgedToken = Create2Upgradeable.deploy({ amount: 0, // amount of Ether to send - salt: salt, + salt: keccak256(abi.encode(ctoken)), bytecode: type(ProxiedBridgedERC20).creationCode }); From e1d9e06e6675f7196d6a2f853a4f55ff23255ff4 Mon Sep 17 00:00:00 2001 From: Daniel Wang <99078276+dantaik@users.noreply.github.com> Date: Fri, 1 Sep 2023 14:09:14 +0000 Subject: [PATCH 6/8] Update ERC1155Vault.sol --- packages/protocol/contracts/tokenvault/ERC1155Vault.sol | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/protocol/contracts/tokenvault/ERC1155Vault.sol b/packages/protocol/contracts/tokenvault/ERC1155Vault.sol index 57f1ff8d669..d8ebe768317 100644 --- a/packages/protocol/contracts/tokenvault/ERC1155Vault.sol +++ b/packages/protocol/contracts/tokenvault/ERC1155Vault.sol @@ -340,11 +340,9 @@ contract ERC1155Vault is BaseNFTVault, ERC1155ReceiverUpgradeable { private returns (address btoken) { - bytes32 salt = keccak256(abi.encode(ctoken)); - address bridgedToken = Create2Upgradeable.deploy({ amount: 0, // amount of Ether to send - salt: salt, + salt: keccak256(abi.encode(ctoken)), bytecode: type(ProxiedBridgedERC1155).creationCode }); From fff496e6979d0de26da5dcef681f5635ff995912 Mon Sep 17 00:00:00 2001 From: Daniel Wang Date: Sun, 3 Sep 2023 00:04:37 +0800 Subject: [PATCH 7/8] Update LibProving.sol --- .../protocol/contracts/L1/libs/LibProving.sol | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/packages/protocol/contracts/L1/libs/LibProving.sol b/packages/protocol/contracts/L1/libs/LibProving.sol index 7658ec0bee0..a8052604405 100644 --- a/packages/protocol/contracts/L1/libs/LibProving.sol +++ b/packages/protocol/contracts/L1/libs/LibProving.sol @@ -166,14 +166,19 @@ library LibProving { pure returns (bytes32 instance) { - if (evidence.prover == LibUtils.ORACLE_PROVER) return 0; - else return keccak256(abi.encode( - evidence.metaHash, - evidence.parentHash, - evidence.blockHash, - evidence.signalRoot, - evidence.graffiti, - evidence.prover - )); + if (evidence.prover == LibUtils.ORACLE_PROVER) { + return 0; + } else { + return keccak256( + abi.encode( + evidence.metaHash, + evidence.parentHash, + evidence.blockHash, + evidence.signalRoot, + evidence.graffiti, + evidence.prover + ) + ); + } } } From 05aef61b06b0cb189ca2fbd56b3765695818773f Mon Sep 17 00:00:00 2001 From: Daniel Wang Date: Sun, 3 Sep 2023 00:04:47 +0800 Subject: [PATCH 8/8] Update ERC20Vault.sol --- .../contracts/tokenvault/ERC20Vault.sol | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/packages/protocol/contracts/tokenvault/ERC20Vault.sol b/packages/protocol/contracts/tokenvault/ERC20Vault.sol index d97900a3c60..45b1eb84f21 100644 --- a/packages/protocol/contracts/tokenvault/ERC20Vault.sol +++ b/packages/protocol/contracts/tokenvault/ERC20Vault.sol @@ -274,10 +274,9 @@ contract ERC20Vault is /// @param to To address. /// @param amount Amount to be sent. /// @return msgData Encoded message data. - /// @return _amount Token amount left in the contract. (Todo Daniel: why we - /// calcualte the left amount ? Why is it necessary ? It seems ambigous that - /// in one case we just return the amount while in other case we calculate - /// the left tokens.) + /// @return _balanceChange User token balance actual change after the token + /// transfer. This value is calculated so we do not assume token balance + /// change is the amount of token transfered away. function _encodeDestinationCall( address user, address token, @@ -285,7 +284,7 @@ contract ERC20Vault is uint256 amount ) private - returns (bytes memory msgData, uint256 _amount) + returns (bytes memory msgData, uint256 _balanceChange) { CanonicalERC20 memory ctoken; @@ -294,7 +293,7 @@ contract ERC20Vault is ctoken = bridgedToCanonical[token]; assert(ctoken.addr != address(0)); IMintableERC20(token).burn(msg.sender, amount); - _amount = amount; + _balanceChange = amount; } else { // If it's a canonical token ERC20Upgradeable t = ERC20Upgradeable(token); @@ -308,7 +307,7 @@ contract ERC20Vault is if (token == resolve("taiko_token", true)) { IMintableERC20(token).burn(msg.sender, amount); - _amount = amount; + _balanceChange = amount; } else { uint256 _balance = t.balanceOf(address(this)); t.transferFrom({ @@ -316,12 +315,12 @@ contract ERC20Vault is to: address(this), amount: amount }); - _amount = t.balanceOf(address(this)) - _balance; + _balanceChange = t.balanceOf(address(this)) - _balance; } } msgData = abi.encodeWithSelector( - ERC20Vault.receiveToken.selector, ctoken, user, to, _amount + ERC20Vault.receiveToken.selector, ctoken, user, to, _balanceChange ); }