Skip to content

Commit

Permalink
[Unlocks] feat: add percentage progress to Unlocks metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
kyriediculous committed Nov 30, 2023
1 parent 5bcf0b0 commit 5ba6eeb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
9 changes: 8 additions & 1 deletion src/unlocks/Unlocks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -27,6 +28,7 @@ pragma solidity >=0.8.19;
struct Metadata {
uint256 amount;
uint256 maturity;
uint256 progress;
uint256 unlockId;
string symbol;
string name;
Expand Down Expand Up @@ -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(),
Expand Down
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, 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);
Expand Down
12 changes: 10 additions & 2 deletions test/unlocks/Unlocks.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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 {
Expand Down Expand Up @@ -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")));
Expand All @@ -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);
Expand Down

0 comments on commit 5ba6eeb

Please sign in to comment.