Skip to content

Commit bc393d5

Browse files
committed
move asset and category structs into standalone file
1 parent d112518 commit bc393d5

9 files changed

Lines changed: 137 additions & 156 deletions

src/Asset.sol

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.0;
3+
4+
/**
5+
* @title Category
6+
* @dev Enum representation Asset category.
7+
*/
8+
enum Category {
9+
ERC20,
10+
ERC721,
11+
ERC1155,
12+
CryptoKitties
13+
}
14+
15+
/**
16+
* @title Asset
17+
* @param category Corresponding asset category.
18+
* @param assetAddress Address of the token contract defining the asset.
19+
* @param id TokenID of an NFT or 0.
20+
* @param amount Amount of fungible tokens or 0 -> 1.
21+
*/
22+
struct Asset {
23+
Category category;
24+
address assetAddress;
25+
uint256 id;
26+
uint256 amount;
27+
}

src/MultiToken.sol

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { ERC165Checker } from "openzeppelin/utils/introspection/ERC165Checker.so
1111
import { ICryptoKitties } from "multitoken/interfaces/ICryptoKitties.sol";
1212
import { IMultiTokenCategoryRegistry } from "multitoken/interfaces/IMultiTokenCategoryRegistry.sol";
1313

14+
import { Asset, Category } from "multitoken/Asset.sol";
15+
1416

1517
/**
1618
* @title MultiToken library
@@ -30,31 +32,6 @@ library MultiToken {
3032
*/
3133
uint8 public constant CATEGORY_NOT_REGISTERED = type(uint8).max;
3234

33-
/**
34-
* @title Category
35-
* @dev Enum representation Asset category.
36-
*/
37-
enum Category {
38-
ERC20,
39-
ERC721,
40-
ERC1155,
41-
CryptoKitties
42-
}
43-
44-
/**
45-
* @title Asset
46-
* @param category Corresponding asset category.
47-
* @param assetAddress Address of the token contract defining the asset.
48-
* @param id TokenID of an NFT or 0.
49-
* @param amount Amount of fungible tokens or 0 -> 1.
50-
*/
51-
struct Asset {
52-
Category category;
53-
address assetAddress;
54-
uint256 id;
55-
uint256 amount;
56-
}
57-
5835
/**
5936
* @notice Thrown when unsupported category is used.
6037
* @param categoryValue Value of the unsupported category.

src/Permit2MultiToken.sol

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ICryptoKitties } from "multitoken/interfaces/ICryptoKitties.sol";
1313
import { IMultiTokenCategoryRegistry } from "multitoken/interfaces/IMultiTokenCategoryRegistry.sol";
1414
import { IPermit2Like } from "multitoken/interfaces/IPermit2Like.sol";
1515

16+
import { Asset, Category } from "multitoken/Asset.sol";
1617

1718
/**
1819
* @title Permit2 MultiToken library
@@ -33,31 +34,6 @@ library Permit2MultiToken {
3334
*/
3435
uint8 public constant CATEGORY_NOT_REGISTERED = type(uint8).max;
3536

36-
/**
37-
* @title Category
38-
* @dev Enum representation Asset category.
39-
*/
40-
enum Category {
41-
ERC20,
42-
ERC721,
43-
ERC1155,
44-
CryptoKitties
45-
}
46-
47-
/**
48-
* @title Asset
49-
* @param category Corresponding asset category.
50-
* @param assetAddress Address of the token contract defining the asset.
51-
* @param id TokenID of an NFT or 0.
52-
* @param amount Amount of fungible tokens or 0 -> 1.
53-
*/
54-
struct Asset {
55-
Category category;
56-
address assetAddress;
57-
uint256 id;
58-
uint256 amount;
59-
}
60-
6137
/**
6238
* @notice Thrown when unsupported category is used.
6339
* @param categoryValue Value of the unsupported category.

test/fork/MultiToken.fork.t.sol

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { IERC1155 } from "openzeppelin/interfaces/IERC1155.sol";
99
import { ERC1155 } from "openzeppelin/token/ERC1155/ERC1155.sol";
1010
import { IERC20Permit } from "openzeppelin/token/ERC20/extensions/IERC20Permit.sol";
1111

12-
import { MultiToken, ICryptoKitties } from "multitoken/MultiToken.sol";
12+
import { MultiToken, Asset, Category, ICryptoKitties } from "multitoken/MultiToken.sol";
1313

1414

1515
abstract contract MultiTokenIntegrationTest is Test {
@@ -60,7 +60,7 @@ contract T1155 is ERC1155("test") {
6060
|*----------------------------------------------------------*/
6161

6262
contract MultiToken_Transfer_IntegrationTest is MultiTokenIntegrationTest {
63-
using MultiToken for MultiToken.Asset;
63+
using MultiToken for Asset;
6464

6565
// ERC20
6666

@@ -139,7 +139,7 @@ contract MultiToken_Transfer_IntegrationTest is MultiTokenIntegrationTest {
139139

140140
function test_transferERC20_whenCallerIsNotSource() external {
141141
bool success;
142-
MultiToken.Asset memory asset;
142+
Asset memory asset;
143143

144144
// WETH - transfer & transferFrom returning bool
145145
vm.prank(WETH); // Assuming WETH contract has at least `amount` tokens
@@ -269,7 +269,7 @@ contract MultiToken_Transfer_IntegrationTest is MultiTokenIntegrationTest {
269269
T1155 t1155 = new T1155();
270270
t1155.mint(chandler, id, amount);
271271

272-
MultiToken.Asset memory asset = MultiToken.ERC1155(address(t1155), id, 0);
272+
Asset memory asset = MultiToken.ERC1155(address(t1155), id, 0);
273273

274274
vm.prank(chandler);
275275
asset.approveAsset(address(this));
@@ -294,7 +294,7 @@ contract MultiToken_Transfer_IntegrationTest is MultiTokenIntegrationTest {
294294
T1155 t1155 = new T1155();
295295
t1155.mint(chandler, id, amount);
296296

297-
MultiToken.Asset memory asset = MultiToken.ERC1155(address(t1155), id, amount);
297+
Asset memory asset = MultiToken.ERC1155(address(t1155), id, amount);
298298

299299
vm.prank(chandler);
300300
asset.approveAsset(address(this));
@@ -337,7 +337,7 @@ contract MultiToken_Transfer_IntegrationTest is MultiTokenIntegrationTest {
337337
}
338338

339339
function test_transferCryptoKitties_whenCallerIsNotSource() external {
340-
MultiToken.Asset memory asset = MultiToken.CryptoKitties(CK, id);
340+
Asset memory asset = MultiToken.CryptoKitties(CK, id);
341341
address trueOwner = ICryptoKitties(CK).ownerOf(id);
342342
vm.prank(trueOwner);
343343
ICryptoKitties(CK).transfer(chandler, id);
@@ -367,7 +367,7 @@ contract MultiToken_Transfer_IntegrationTest is MultiTokenIntegrationTest {
367367
|*----------------------------------------------------------*/
368368

369369
contract MultiToken_Permit_IntegrationTest is MultiTokenIntegrationTest {
370-
using MultiToken for MultiToken.Asset;
370+
using MultiToken for Asset;
371371

372372

373373
function _signPermit() private returns (uint8 v, bytes32 r, bytes32 s) {
@@ -428,7 +428,7 @@ contract MultiToken_Permit_IntegrationTest is MultiTokenIntegrationTest {
428428
|*----------------------------------------------------------*/
429429

430430
contract MultiToken_Approve_IntegrationTest is MultiTokenIntegrationTest {
431-
using MultiToken for MultiToken.Asset;
431+
using MultiToken for Asset;
432432

433433

434434
function test_approveAmount_whenERC20() external {
@@ -472,7 +472,7 @@ contract MultiToken_Approve_IntegrationTest is MultiTokenIntegrationTest {
472472

473473
// CryptoKitties doesn't implement `getApproved` function. The approve is tested by a transfer.
474474
function test_approveId_whenCryptoKitties() external {
475-
MultiToken.Asset memory asset = MultiToken.CryptoKitties(CK, id);
475+
Asset memory asset = MultiToken.CryptoKitties(CK, id);
476476
address trueOwner = ICryptoKitties(CK).ownerOf(id);
477477
vm.prank(trueOwner);
478478
ICryptoKitties(CK).transfer(chandler, id);
@@ -500,7 +500,7 @@ contract MultiToken_Approve_IntegrationTest is MultiTokenIntegrationTest {
500500
|*----------------------------------------------------------*/
501501

502502
contract MultiToken_IsValid_IntegrationTest is MultiTokenIntegrationTest {
503-
using MultiToken for MultiToken.Asset;
503+
using MultiToken for Asset;
504504

505505
address t1155;
506506

@@ -514,121 +514,121 @@ contract MultiToken_IsValid_IntegrationTest is MultiTokenIntegrationTest {
514514

515515
function test_returnTrue_whenValidERC20() external {
516516
assertTrue(
517-
MultiToken.Asset(MultiToken.Category.ERC20, USDC, 0, amount).isValid()
517+
Asset(Category.ERC20, USDC, 0, amount).isValid()
518518
);
519519
}
520520

521521
function test_returnFalse_whenERC20_withNonZeroId() external {
522522
assertFalse(
523-
MultiToken.Asset(MultiToken.Category.ERC20, USDC, id, amount).isValid()
523+
Asset(Category.ERC20, USDC, id, amount).isValid()
524524
);
525525
}
526526

527527
function test_returnFalse_whenERC20_withERC721Category() external {
528528
assertFalse(
529-
MultiToken.Asset(MultiToken.Category.ERC721, USDC, id, 0).isValid()
529+
Asset(Category.ERC721, USDC, id, 0).isValid()
530530
);
531531
}
532532

533533
function test_returnFalse_whenERC20_withERC1155Category() external {
534534
assertFalse(
535-
MultiToken.Asset(MultiToken.Category.ERC1155, USDC, id, amount).isValid()
535+
Asset(Category.ERC1155, USDC, id, amount).isValid()
536536
);
537537
}
538538

539539
function test_returnFalse_whenERC20_withCryptoKittiesCategory() external {
540540
assertFalse(
541-
MultiToken.Asset(MultiToken.Category.CryptoKitties, USDC, id, 0).isValid()
541+
Asset(Category.CryptoKitties, USDC, id, 0).isValid()
542542
);
543543
}
544544

545545
// ERC721
546546

547547
function test_returnTrue_whenValidERC721() external {
548548
assertTrue(
549-
MultiToken.Asset(MultiToken.Category.ERC721, DOODLE, id, 0).isValid()
549+
Asset(Category.ERC721, DOODLE, id, 0).isValid()
550550
);
551551
}
552552

553553
function test_returnFalse_whenERC721_withNonZeroAmount() external {
554554
assertFalse(
555-
MultiToken.Asset(MultiToken.Category.ERC721, DOODLE, id, 1).isValid()
555+
Asset(Category.ERC721, DOODLE, id, 1).isValid()
556556
);
557557
}
558558

559559
function test_returnFalse_whenERC721_withERC20Category() external {
560560
assertFalse(
561-
MultiToken.Asset(MultiToken.Category.ERC20, DOODLE, 0, amount).isValid()
561+
Asset(Category.ERC20, DOODLE, 0, amount).isValid()
562562
);
563563
}
564564

565565
function test_returnFalse_whenERC721_withERC1155Category() external {
566566
assertFalse(
567-
MultiToken.Asset(MultiToken.Category.ERC1155, DOODLE, id, amount).isValid()
567+
Asset(Category.ERC1155, DOODLE, id, amount).isValid()
568568
);
569569
}
570570

571571
function test_returnFalse_whenERC721_withCryptoKittiesCategory() external {
572572
assertFalse(
573-
MultiToken.Asset(MultiToken.Category.CryptoKitties, DOODLE, id, 0).isValid()
573+
Asset(Category.CryptoKitties, DOODLE, id, 0).isValid()
574574
);
575575
}
576576

577577
// ERC1155
578578

579579
function test_returnTrue_whenValidERC1155() external {
580580
assertTrue(
581-
MultiToken.Asset(MultiToken.Category.ERC1155, t1155, id, amount).isValid()
581+
Asset(Category.ERC1155, t1155, id, amount).isValid()
582582
);
583583
}
584584

585585
function test_returnFalse_whenERC1155_withERC20Category() external {
586586
assertFalse(
587-
MultiToken.Asset(MultiToken.Category.ERC20, t1155, 0, amount).isValid()
587+
Asset(Category.ERC20, t1155, 0, amount).isValid()
588588
);
589589
}
590590

591591
function test_returnFalse_whenERC1155_withERC721Category() external {
592592
assertFalse(
593-
MultiToken.Asset(MultiToken.Category.ERC721, t1155, id, 0).isValid()
593+
Asset(Category.ERC721, t1155, id, 0).isValid()
594594
);
595595
}
596596

597597
function test_returnFalse_whenERC1155_withCryptoKittiesCategory() external {
598598
assertFalse(
599-
MultiToken.Asset(MultiToken.Category.CryptoKitties, t1155, id, 0).isValid()
599+
Asset(Category.CryptoKitties, t1155, id, 0).isValid()
600600
);
601601
}
602602

603603
// CryptoKitties
604604

605605
function test_returnTrue_whenValidCryptoKitties() external {
606606
assertTrue(
607-
MultiToken.Asset(MultiToken.Category.CryptoKitties, CK, id, 0).isValid()
607+
Asset(Category.CryptoKitties, CK, id, 0).isValid()
608608
);
609609
}
610610

611611
function test_returnFalse_whenCryptoKitties_withNonZeroAmount() external {
612612
assertFalse(
613-
MultiToken.Asset(MultiToken.Category.CryptoKitties, CK, id, 1).isValid()
613+
Asset(Category.CryptoKitties, CK, id, 1).isValid()
614614
);
615615
}
616616

617617
function test_returnFalse_whenCryptoKitties_withERC20Category() external {
618618
assertFalse(
619-
MultiToken.Asset(MultiToken.Category.ERC20, CK, 0, amount).isValid()
619+
Asset(Category.ERC20, CK, 0, amount).isValid()
620620
);
621621
}
622622

623623
function test_returnFalse_whenCryptoKitties_withERC721Category() external {
624624
assertFalse(
625-
MultiToken.Asset(MultiToken.Category.ERC721, CK, id, 0).isValid()
625+
Asset(Category.ERC721, CK, id, 0).isValid()
626626
);
627627
}
628628

629629
function test_returnFalse_whenCryptoKitties_withERC1155Category() external {
630630
assertFalse(
631-
MultiToken.Asset(MultiToken.Category.ERC1155, CK, id, amount).isValid()
631+
Asset(Category.ERC1155, CK, id, amount).isValid()
632632
);
633633
}
634634

test/fork/Permit2MultiToken.fork.t.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ pragma solidity 0.8.16;
44
import { Test } from "forge-std/Test.sol";
55

66
import {
7-
Permit2MultiToken,
7+
Permit2MultiToken, Asset, Category,
8+
89
IERC20, IPermit2Like
910
} from "multitoken/Permit2MultiToken.sol";
1011

1112
using Permit2MultiToken for address;
12-
using Permit2MultiToken for Permit2MultiToken.Asset;
13+
using Permit2MultiToken for Asset;
1314

1415
abstract contract Permit2MultiTokenIntegrationTest is Test {
1516

test/harness/MultiTokenHarness.sol

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity 0.8.16;
33

4-
import { MultiToken } from "multitoken/MultiToken.sol";
4+
import { MultiToken, Asset } from "multitoken/MultiToken.sol";
55

66

77
contract MultiTokenHarness {
88

9-
function transferAssetFrom(MultiToken.Asset memory asset, address source, address dest) external {
9+
function transferAssetFrom(Asset memory asset, address source, address dest) external {
1010
MultiToken.transferAssetFrom(asset, source, dest);
1111
}
1212

13-
function safeTransferAssetFrom(MultiToken.Asset memory asset, address source, address dest) external {
13+
function safeTransferAssetFrom(Asset memory asset, address source, address dest) external {
1414
MultiToken.safeTransferAssetFrom(asset, source, dest);
1515
}
1616

17-
function permit(MultiToken.Asset memory asset, address owner, address spender, bytes memory permitData) external {
17+
function permit(Asset memory asset, address owner, address spender, bytes memory permitData) external {
1818
MultiToken.permit(asset, owner, spender, permitData);
1919
}
2020

0 commit comments

Comments
 (0)