Skip to content

Commit

Permalink
Add init, update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ltyu committed Mar 25, 2024
1 parent 1bbf245 commit 3c4700a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 22 deletions.
8 changes: 8 additions & 0 deletions solidity/contracts/token/HypERC721Collateral.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ contract HypERC721Collateral is TokenRouter {
wrappedToken = IERC721(erc721);
}

function initialize(
address _hook,
address _interchainSecurityModule,
address _owner
) public virtual initializer {
_MailboxClient_initialize(_hook, _interchainSecurityModule, _owner);
}

function ownerOf(uint256 _tokenId) external view returns (address) {
return IERC721(wrappedToken).ownerOf(_tokenId);
}
Expand Down
105 changes: 83 additions & 22 deletions solidity/test/token/HypERC721.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ pragma solidity ^0.8.13;
@@@@@@@@@ @@@@@@@@*/

import "forge-std/Test.sol";
import {TransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";
import {IERC721Receiver} from "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
import {ERC721URIStorageUpgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC721/extensions/ERC721URIStorageUpgradeable.sol";

import {TestMailbox} from "../../contracts/test/TestMailbox.sol";
import {TestPostDispatchHook} from "../../contracts/test/TestPostDispatchHook.sol";
Expand All @@ -26,9 +29,6 @@ import {HypERC721Collateral} from "../../contracts/token/HypERC721Collateral.sol
import {HypERC721URIStorage} from "../../contracts/token/extensions/HypERC721URIStorage.sol";
import {HypERC721URICollateral} from "../../contracts/token/extensions/HypERC721URICollateral.sol";

import {IERC721Receiver} from "@openzeppelin/contracts/token/ERC721/IERC721Receiver.sol";
import {ERC721URIStorageUpgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC721/extensions/ERC721URIStorageUpgradeable.sol";

abstract contract HypTokenTest is Test, IERC721Receiver {
using TypeCasts for address;

Expand All @@ -38,6 +38,7 @@ abstract contract HypTokenTest is Test, IERC721Receiver {

address internal constant ALICE = address(0x1);
address internal constant BOB = address(0x2);
address internal constant PROXY_ADMIN = address(0x37);
uint32 internal constant ORIGIN = 11;
uint32 internal constant DESTINATION = 22;
uint256 internal constant TRANSFER_ID = 0;
Expand Down Expand Up @@ -77,19 +78,42 @@ abstract contract HypTokenTest is Test, IERC721Receiver {

function _deployRemoteToken(bool isCollateral) internal {
if (isCollateral) {
remoteToken = new HypERC721Collateral(
HypERC721Collateral implementation = new HypERC721Collateral(
address(remotePrimaryToken),
address(remoteMailbox)
);
TransparentUpgradeableProxy proxy = new TransparentUpgradeableProxy(
address(implementation),
PROXY_ADMIN,
abi.encodeWithSelector(
HypERC721Collateral.initialize.selector,
address(0),
address(0),
address(this)
)
);
remoteToken = HypERC721Collateral(address(proxy));
remotePrimaryToken.transferFrom(
address(this),
address(remoteToken),
0
); // need for processing messages
} else {
HypERC721 erc721 = new HypERC721(address(remoteMailbox));
erc721.initialize(0, NAME, SYMBOL);
remoteToken = TokenRouter(address(erc721));
HypERC721 implementation = new HypERC721(address(remoteMailbox));
TransparentUpgradeableProxy proxy = new TransparentUpgradeableProxy(
address(implementation),
PROXY_ADMIN,
abi.encodeWithSelector(
HypERC721.initialize.selector,
0,
NAME,
SYMBOL,
address(0),
address(0),
address(this)
)
);
remoteToken = TokenRouter(address(proxy));
}
remoteToken.enrollRemoteRouter(
ORIGIN,
Expand Down Expand Up @@ -151,48 +175,67 @@ contract HypERC721Test is HypTokenTest {
function setUp() public virtual override {
super.setUp();

localToken = new HypERC721(address(localMailbox));
hyp721 = HypERC721(address(localToken));

hyp721.initialize(INITIAL_SUPPLY, NAME, SYMBOL);
HypERC721 implementation = new HypERC721(address(localMailbox));
TransparentUpgradeableProxy proxy = new TransparentUpgradeableProxy(
address(implementation),
PROXY_ADMIN,
abi.encodeWithSelector(
HypERC721.initialize.selector,
INITIAL_SUPPLY,
NAME,
SYMBOL,
address(0),
address(0),
address(this)
)
);
localToken = HypERC721(address(proxy));
hyp721 = HypERC721(address(proxy));

hyp721.enrollRemoteRouter(
DESTINATION,
address(remoteToken).addressToBytes32()
);
}

function testInitialize_revert_ifAlreadyInitialized() public {
function testHypERC721Test_Initialize_revert_ifAlreadyInitialized() public {
vm.expectRevert("Initializable: contract is already initialized");
hyp721.initialize(INITIAL_SUPPLY, NAME, SYMBOL);
hyp721.initialize(
INITIAL_SUPPLY,
NAME,
SYMBOL,
address(0),
address(0),
address(this)
);
}

function testTotalSupply() public {
function testHypERC721Test_TotalSupply() public {
assertEq(hyp721.balanceOf(address(this)), INITIAL_SUPPLY);
}

function testOwnerOf() public {
function testHypERC721Test_OwnerOf() public {
assertEq(hyp721.ownerOf(0), address(this));
}

function testLocalTransfer() public {
function testHypERC721Test_LocalTransfer() public {
hyp721.transferFrom(address(this), ALICE, 0);
assertEq(hyp721.balanceOf(address(this)), INITIAL_SUPPLY - 1);
assertEq(hyp721.balanceOf(ALICE), 1);
}

function testLocalYTransfer_revert_invalidTokenId() public {
function testHypERC721Test_LocalYTransfer_revert_invalidTokenId() public {
vm.expectRevert("ERC721: invalid token ID");
hyp721.transferFrom(address(this), ALICE, INITIAL_SUPPLY);
}

function testRemoteTransfer(bool isCollateral) public {
function testHypERC721Test_RemoteTransfer(bool isCollateral) public {
_deployRemoteToken(isCollateral);
_performRemoteTransfer(25000, 0);
assertEq(hyp721.balanceOf(address(this)), INITIAL_SUPPLY - 1);
}

function testRemoteTransfer_revert_unowned() public {
function testHypERC721Test_RemoteTransfer_revert_unowned() public {
hyp721.transferFrom(address(this), BOB, 1);

_deployRemoteToken(false);
Expand All @@ -201,7 +244,7 @@ contract HypERC721Test is HypTokenTest {
assertEq(hyp721.balanceOf(address(this)), INITIAL_SUPPLY - 1);
}

function testRemoteTransfer_revert_invalidTokenId() public {
function testHypERC721Test_RemoteTransfer_revert_invalidTokenId() public {
_deployRemoteToken(false);
vm.expectRevert("ERC721: invalid token ID");
_performRemoteTransfer(25000, INITIAL_SUPPLY);
Expand All @@ -228,7 +271,14 @@ contract HypERC721URIStorageTest is HypTokenTest {
localToken = new MockHypERC721URIStorage(address(localMailbox));
hyp721Storage = MockHypERC721URIStorage(address(localToken));

hyp721Storage.initialize(INITIAL_SUPPLY, NAME, SYMBOL);
hyp721Storage.initialize(
INITIAL_SUPPLY,
NAME,
SYMBOL,
address(0),
address(0),
address(this)
);
hyp721Storage.setTokenURI(0, URI);
hyp721Storage.enrollRemoteRouter(
DESTINATION,
Expand All @@ -254,10 +304,21 @@ contract HypERC721CollateralTest is HypTokenTest {
function setUp() public override {
super.setUp();

localToken = new HypERC721Collateral(
HypERC721Collateral implementation = new HypERC721Collateral(
address(localPrimaryToken),
address(localMailbox)
);
TransparentUpgradeableProxy proxy = new TransparentUpgradeableProxy(
address(implementation),
PROXY_ADMIN,
abi.encodeWithSelector(
HypERC721Collateral.initialize.selector,
address(0),
address(0),
address(this)
)
);
localToken = HypERC721Collateral(address(proxy));
hyp721Collateral = HypERC721Collateral(address(localToken));

hyp721Collateral.enrollRemoteRouter(
Expand Down

0 comments on commit 3c4700a

Please sign in to comment.