Skip to content

Commit

Permalink
Unlocks: fix inconsistent parameter naming
Browse files Browse the repository at this point in the history
  • Loading branch information
kyriediculous committed Sep 5, 2023
1 parent dd70e18 commit 634c654
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
8 changes: 4 additions & 4 deletions src/unlocks/Renderer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ contract Renderer is Initializable, UUPSUpgradeable, OwnableUpgradeable {

/**
* @notice Returns the JSON metadata for a given unlock
* @param id ID of the unlock
* @param tokenId ID of the unlock token
*/
function json(uint256 id) external view returns (string memory) {
Metadata memory data = Unlocks(msg.sender).getMetadata(id);
function json(uint256 tokenId) external view returns (string memory) {
Metadata memory data = Unlocks(msg.sender).getMetadata(tokenId);

return string(
abi.encodePacked(
Expand Down Expand Up @@ -100,7 +100,7 @@ contract Renderer is Initializable, UUPSUpgradeable, OwnableUpgradeable {
'</text><text x="10" y="60">',
data.maturity.toString(),
'</text><text x="10" y="80">',
data.tokenId.toString(),
data.unlockId.toString(),
"</text>",
"</svg>"
)
Expand Down
54 changes: 31 additions & 23 deletions src/unlocks/Unlocks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pragma solidity >=0.8.19;
struct Metadata {
uint256 amount;
uint256 maturity;
uint256 tokenId;
uint256 unlockId;
string symbol;
string name;
address validator;
Expand All @@ -37,7 +37,7 @@ contract Unlocks is ERC721 {
Registry private immutable registry;
Renderer private immutable renderer;

error NotOwnerOf(uint256 id, address owner, address sender);
error NotOwnerOf(uint256 tokenId, address owner, address sender);
error NotTenderizer(address sender);
error InvalidID();

Expand All @@ -55,36 +55,44 @@ contract Unlocks is ERC721 {
* @notice Creates a new unlock token
* @dev Only callable by a Tenderizer
* @param receiver Address of the receiver
* @param id ID of the unlock
* @param unlockId ID of the unlock
* @return tokenId ID of the created token
*/
function createUnlock(address receiver, uint256 id) external virtual isValidTenderizer(msg.sender) returns (uint256 tokenId) {
if (id >= 1 << 96) revert InvalidID();
tokenId = _encodeTokenId(msg.sender, uint96(id));
function createUnlock(
address receiver,
uint256 unlockId
)
external
virtual
isValidTenderizer(msg.sender)
returns (uint256 tokenId)
{
if (unlockId >= 1 << 96) revert InvalidID();
tokenId = _encodeTokenId(msg.sender, uint96(unlockId));
_safeMint(receiver, tokenId);
}

/**
* @notice Burns an unlock token
* @dev Only callable by a Tenderizer
* @param owner Owner of the token
* @param id ID of the unlock
* @param unlockId ID of the unlock
*/
function useUnlock(address owner, uint256 id) external virtual isValidTenderizer(msg.sender) {
if (id >= 1 << 96) revert InvalidID();
uint256 tokenId = _encodeTokenId(msg.sender, uint96(id));
if (ownerOf(tokenId) != owner) revert NotOwnerOf(id, ownerOf(tokenId), owner);
function useUnlock(address owner, uint256 unlockId) external virtual isValidTenderizer(msg.sender) {
if (unlockId >= 1 << 96) revert InvalidID();
uint256 tokenId = _encodeTokenId(msg.sender, uint96(unlockId));
if (ownerOf(tokenId) != owner) revert NotOwnerOf(unlockId, ownerOf(tokenId), owner);
_burn(tokenId);
}

/**
* @notice Returns the tokenURI of an unlock token
* @param id ID of the unlock token
* @param tokenId ID of the unlock token
* @return tokenURI of the unlock token
*/
function tokenURI(uint256 id) public view virtual override returns (string memory) {
require(_ownerOf[id] != address(0), "non-existent token");
return renderer.json(id);
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
require(ownerOf(tokenId) != address(0), "non-existent token");
return renderer.json(tokenId);
}

/**
Expand All @@ -93,13 +101,13 @@ contract Unlocks is ERC721 {
* @return metadata of the unlock token
*/
function getMetadata(uint256 tokenId) external view returns (Metadata memory metadata) {
(address tenderizer, uint256 id) = _decodeTokenId(tokenId);
(address tenderizer, uint256 unlockId) = _decodeTokenId(tokenId);
address asset = Tenderizer(tenderizer).asset();

return Metadata({
amount: Tenderizer(tenderizer).previewWithdraw(id),
maturity: Tenderizer(tenderizer).unlockMaturity(id),
tokenId: id,
amount: Tenderizer(tenderizer).previewWithdraw(unlockId),
maturity: Tenderizer(tenderizer).unlockMaturity(unlockId),
unlockId: unlockId,
symbol: ERC20(asset).symbol(),
name: ERC20(asset).name(),
validator: Tenderizer(tenderizer).validator()
Expand All @@ -110,12 +118,12 @@ contract Unlocks is ERC721 {
if (!registry.isTenderizer(sender)) revert NotTenderizer(sender);
}

function _encodeTokenId(address tenderizer, uint96 id) internal pure virtual returns (uint256) {
return uint256(bytes32(abi.encodePacked(tenderizer, id)));
function _encodeTokenId(address tenderizer, uint96 unlockId) internal pure virtual returns (uint256) {
return uint256(bytes32(abi.encodePacked(tenderizer, unlockId)));
}

function _decodeTokenId(uint256 tokenId) internal pure virtual returns (address tenderizer, uint96 id) {
function _decodeTokenId(uint256 tokenId) internal pure virtual returns (address tenderizer, uint96 unlockId) {
bytes32 a = bytes32(tokenId);
return (address(bytes20(a)), uint96(bytes12(a << 160)));
(tenderizer, unlockId) = (address(bytes20(a)), uint96(bytes12(a << 160)));
}
}
2 changes: 1 addition & 1 deletion test/unlocks/Renderer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ contract RendererTest is Test {
address private validator = vm.addr(4);
uint256 private id = 1;
Metadata private metadata =
Metadata({ amount: 100, maturity: 1000, tokenId: id, symbol: "GRT", name: "Graph", validator: validator });
Metadata({ amount: 100, maturity: 1000, unlockId: id, symbol: "GRT", name: "Graph", validator: validator });
RendererV1 private rendererV1;

bytes32 internal constant IMPL_SLOT = bytes32(uint256(keccak256("eip1967.proxy.implementation")) - 1);
Expand Down
4 changes: 2 additions & 2 deletions test/unlocks/Unlocks.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ contract UnlockTest is Test {
}

function test_tokenURI_RevertIfIdDoesntExist() public {
vm.expectRevert("non-existent token");
vm.expectRevert("NOT_MINTED");
unlocks.tokenURI(1);
}

Expand All @@ -152,7 +152,7 @@ contract UnlockTest is Test {

Metadata memory d = unlocks.getMetadata(tokenId);

assertEq(d.tokenId, lockId);
assertEq(d.unlockId, lockId);
assertEq(d.amount, 1 ether);
assertEq(d.maturity, block.timestamp);
assertEq(d.symbol, "TEST");
Expand Down

0 comments on commit 634c654

Please sign in to comment.