-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(protocol): fix metadata retrieval in vaults (#17003)
Co-authored-by: dantaik <[email protected]>
- Loading branch information
Showing
6 changed files
with
97 additions
and
76 deletions.
There are no files selected for viewing
This file contains 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 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,32 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.24; | ||
|
||
library LibBytes { | ||
// Taken from: | ||
// https://github.com/0xPolygonHermez/zkevm-contracts/blob/main/contracts/PolygonZkEVMBridge.sol#L835-L860 | ||
/// @notice Function to convert returned data to string | ||
/// returns 'NOT_VALID_ENCODING' as fallback value. | ||
function toString(bytes memory _data) internal pure returns (string memory) { | ||
if (_data.length >= 64) { | ||
return abi.decode(_data, (string)); | ||
} else if (_data.length == 32) { | ||
// Since the strings on bytes32 are encoded left-right, check the first zero in the data | ||
uint256 nonZeroBytes; | ||
while (nonZeroBytes < 32 && _data[nonZeroBytes] != 0) { | ||
++nonZeroBytes; | ||
} | ||
|
||
// If the first one is 0, we do not handle the encoding | ||
if (nonZeroBytes == 0) return ""; | ||
|
||
// Create a byte array with nonZeroBytes length | ||
bytes memory bytesArray = new bytes(nonZeroBytes); | ||
for (uint256 i; i < nonZeroBytes; ++i) { | ||
bytesArray[i] = _data[i]; | ||
} | ||
return string(bytesArray); | ||
} else { | ||
return ""; | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -6,6 +6,17 @@ import "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol"; | |
import "../bridge/IBridge.sol"; | ||
import "../common/EssentialContract.sol"; | ||
import "../common/LibStrings.sol"; | ||
import "../libs/LibBytes.sol"; | ||
|
||
/// @title INameSymbol | ||
/// @notice Interface for contracts that provide name() and symbol() | ||
/// functions. These functions may not be part of the official interface but are | ||
/// used by some contracts. | ||
/// @custom:security-contact [email protected] | ||
interface INameSymbol { | ||
function name() external view returns (string memory); | ||
function symbol() external view returns (string memory); | ||
} | ||
|
||
/// @title BaseVault | ||
/// @notice This abstract contract provides a base implementation for vaults. | ||
|
@@ -16,6 +27,8 @@ abstract contract BaseVault is | |
IMessageInvocable, | ||
IERC165Upgradeable | ||
{ | ||
using LibBytes for bytes; | ||
|
||
uint256[50] private __gap; | ||
|
||
error VAULT_INVALID_TO_ADDR(); | ||
|
@@ -58,4 +71,16 @@ abstract contract BaseVault is | |
function checkToAddress(address _to) internal view { | ||
if (_to == address(0) || _to == address(this)) revert VAULT_INVALID_TO_ADDR(); | ||
} | ||
|
||
function safeSymbol(address _token) internal view returns (string memory symbol_) { | ||
(bool success, bytes memory data) = | ||
address(_token).staticcall(abi.encodeCall(INameSymbol.symbol, ())); | ||
return success ? data.toString() : ""; | ||
} | ||
|
||
function safeName(address _token) internal view returns (string memory) { | ||
(bool success, bytes memory data) = | ||
address(_token).staticcall(abi.encodeCall(INameSymbol.name, ())); | ||
return success ? data.toString() : ""; | ||
} | ||
} |
This file contains 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 |
---|---|---|
|
@@ -8,19 +8,6 @@ import "../common/LibStrings.sol"; | |
import "./BaseNFTVault.sol"; | ||
import "./BridgedERC1155.sol"; | ||
|
||
/// @title IERC1155NameAndSymbol | ||
/// @notice Interface for ERC1155 contracts that provide name() and symbol() | ||
/// functions. These functions may not be part of the official interface but are | ||
/// used by some contracts. | ||
/// @custom:security-contact [email protected] | ||
interface IERC1155NameAndSymbol { | ||
/// @notice Returns the name of the token. | ||
function name() external view returns (string memory); | ||
|
||
/// @notice Returns the symbol of the token. | ||
function symbol() external view returns (string memory); | ||
} | ||
|
||
/// @title ERC1155Vault | ||
/// @dev Labeled in AddressResolver as "erc1155_vault" | ||
/// @notice This vault holds all ERC1155 tokens that users have deposited. | ||
|
@@ -257,16 +244,9 @@ contract ERC1155Vault is BaseNFTVault, ERC1155ReceiverUpgradeable { | |
ctoken_ = CanonicalNFT({ | ||
chainId: uint64(block.chainid), | ||
addr: _op.token, | ||
symbol: "", | ||
name: "" | ||
symbol: safeSymbol(_op.token), | ||
name: safeName(_op.token) | ||
}); | ||
IERC1155NameAndSymbol t = IERC1155NameAndSymbol(_op.token); | ||
try t.name() returns (string memory _name) { | ||
ctoken_.name = _name; | ||
} catch { } | ||
try t.symbol() returns (string memory _symbol) { | ||
ctoken_.symbol = _symbol; | ||
} catch { } | ||
|
||
IERC1155(_op.token).safeBatchTransferFrom({ | ||
from: msg.sender, | ||
|
This file contains 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 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