From 5ba6eebac659db393308f28e3cb8990c8fb8e24b Mon Sep 17 00:00:00 2001 From: kyriediculous Date: Thu, 30 Nov 2023 17:01:17 +0100 Subject: [PATCH] [Unlocks] feat: add percentage progress to Unlocks metadata --- src/unlocks/Unlocks.sol | 9 ++++++++- test/unlocks/Renderer.t.sol | 2 +- test/unlocks/Unlocks.t.sol | 12 ++++++++++-- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/unlocks/Unlocks.sol b/src/unlocks/Unlocks.sol index 2fe5dae..c2fa9ab 100644 --- a/src/unlocks/Unlocks.sol +++ b/src/unlocks/Unlocks.sol @@ -12,6 +12,7 @@ import { ERC721 } from "solmate/tokens/ERC721.sol"; import { ERC20 } from "solmate/tokens/ERC20.sol"; +import { Adapter } from "core/adapters/Adapter.sol"; import { Tenderizer } from "core/tenderizer/Tenderizer.sol"; import { Registry } from "core/registry/Registry.sol"; import { Renderer } from "core/unlocks/Renderer.sol"; @@ -27,6 +28,7 @@ pragma solidity >=0.8.19; struct Metadata { uint256 amount; uint256 maturity; + uint256 progress; uint256 unlockId; string symbol; string name; @@ -104,9 +106,14 @@ contract Unlocks is ERC721 { (address tenderizer, uint256 unlockId) = _decodeTokenId(tokenId); address asset = Tenderizer(tenderizer).asset(); + Adapter adapter = Tenderizer(tenderizer).adapter(); + uint256 maturity = Tenderizer(tenderizer).unlockMaturity(unlockId); + uint256 currentTime = adapter.currentTime(); + return Metadata({ amount: Tenderizer(tenderizer).previewWithdraw(unlockId), - maturity: Tenderizer(tenderizer).unlockMaturity(unlockId), + maturity: maturity, + progress: maturity > currentTime ? 100 - (maturity - currentTime) * 100 / adapter.unlockTime() : 100, unlockId: unlockId, symbol: ERC20(asset).symbol(), name: ERC20(asset).name(), diff --git a/test/unlocks/Renderer.t.sol b/test/unlocks/Renderer.t.sol index e5de115..532badd 100644 --- a/test/unlocks/Renderer.t.sol +++ b/test/unlocks/Renderer.t.sol @@ -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, unlockId: id, symbol: "GRT", name: "Graph", validator: validator }); + Metadata({ amount: 100, maturity: 1000, progress: 100, unlockId: id, symbol: "GRT", name: "Graph", validator: validator }); RendererV1 private rendererV1; bytes32 internal constant IMPL_SLOT = bytes32(uint256(keccak256("eip1967.proxy.implementation")) - 1); diff --git a/test/unlocks/Unlocks.t.sol b/test/unlocks/Unlocks.t.sol index ec77d4d..5b30311 100644 --- a/test/unlocks/Unlocks.t.sol +++ b/test/unlocks/Unlocks.t.sol @@ -14,6 +14,7 @@ pragma solidity >=0.8.19; import { Test, stdError } from "forge-std/Test.sol"; import { IERC20Metadata } from "core/interfaces/IERC20.sol"; +import { Adapter } from "core/adapters/Adapter.sol"; import { Renderer } from "core/unlocks/Renderer.sol"; import { Registry } from "core/registry/Registry.sol"; import { Tenderizer } from "core/tenderizer/Tenderizer.sol"; @@ -29,9 +30,11 @@ contract UnlockTest is Test { address private renderer = vm.addr(4); address private impostor = vm.addr(5); address private validator = vm.addr(6); + address private adapter = vm.addr(7); function setUp() public { unlocks = new Unlocks(registry, renderer); + vm.etch(adapter, bytes("code")); } function test_Metadata() public { @@ -142,8 +145,12 @@ contract UnlockTest is Test { vm.mockCall(registry, abi.encodeCall(Registry.isTenderizer, (tenderizer)), abi.encode(true)); uint256 tokenId = unlocks.createUnlock(msg.sender, lockId); + vm.mockCall(tenderizer, abi.encodeCall(TenderizerImmutableArgs.adapter, ()), abi.encode((adapter))); + vm.mockCall(adapter, abi.encodeCall(Adapter.currentTime, ()), abi.encode((block.number + 50))); + vm.mockCall(adapter, abi.encodeCall(Adapter.unlockTime, ()), abi.encode((100))); + vm.mockCall(tenderizer, abi.encodeCall(Tenderizer.previewWithdraw, (lockId)), abi.encode((1 ether))); - vm.mockCall(tenderizer, abi.encodeCall(Tenderizer.unlockMaturity, (lockId)), abi.encode((block.timestamp))); + vm.mockCall(tenderizer, abi.encodeCall(Tenderizer.unlockMaturity, (lockId)), abi.encode((block.number + 100))); vm.mockCall(tenderizer, abi.encodeCall(TenderizerImmutableArgs.validator, ()), abi.encode((validator))); vm.mockCall(tenderizer, abi.encodeCall(TenderizerImmutableArgs.asset, ()), abi.encode((asset))); vm.mockCall(asset, abi.encodeCall(IERC20Metadata.symbol, ()), abi.encode(("TEST"))); @@ -154,7 +161,8 @@ contract UnlockTest is Test { assertEq(d.unlockId, lockId); assertEq(d.amount, 1 ether); - assertEq(d.maturity, block.timestamp); + assertEq(d.maturity, block.number + 100); + assertEq(d.progress, 50); assertEq(d.symbol, "TEST"); assertEq(d.name, "Test Token"); assertEq(d.validator, validator);