Skip to content

Commit 5849d1b

Browse files
committed
Refactor to reduce code size and avoid memory structs
1 parent cd831e1 commit 5849d1b

File tree

5 files changed

+73
-106
lines changed

5 files changed

+73
-106
lines changed

contracts/ERC1155Pods.sol

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import "./TokenPodsLib.sol";
99
import "./libs/ReentrancyGuard.sol";
1010

1111
abstract contract ERC1155Pods is ERC1155, IERC1155Pods, ReentrancyGuardExt {
12-
using TokenPodsLib for TokenPodsLib.Info;
12+
using TokenPodsLib for TokenPodsLib.Data;
1313
using ReentrancyGuardLib for ReentrancyGuardLib.Data;
1414

1515
error ZeroPodsLimit();
@@ -29,43 +29,39 @@ abstract contract ERC1155Pods is ERC1155, IERC1155Pods, ReentrancyGuardExt {
2929
}
3030

3131
function hasPod(address account, address pod, uint256 id) public view virtual returns(bool) {
32-
return _info(id).hasPod(account, pod);
32+
return _pods[id].hasPod(account, pod);
3333
}
3434

3535
function podsCount(address account, uint256 id) public view virtual returns(uint256) {
36-
return _info(id).podsCount(account);
36+
return _pods[id].podsCount(account);
3737
}
3838

3939
function podAt(address account, uint256 index, uint256 id) public view virtual returns(address) {
40-
return _info(id).podAt(account, index);
40+
return _pods[id].podAt(account, index);
4141
}
4242

4343
function pods(address account, uint256 id) public view virtual returns(address[] memory) {
44-
return _info(id).pods(account);
44+
return _pods[id].pods(account);
4545
}
4646

4747
function balanceOf(address account, uint256 id) public nonReentrantView(_guard) view override(IERC1155, ERC1155) virtual returns(uint256) {
4848
return super.balanceOf(account, id);
4949
}
5050

5151
function podBalanceOf(address pod, address account, uint256 id) public nonReentrantView(_guard) view returns(uint256) {
52-
return _info(id).podBalanceOf(account, pod, super.balanceOf(msg.sender, id));
52+
return _pods[id].podBalanceOf(account, pod, super.balanceOf(msg.sender, id));
5353
}
5454

5555
function addPod(address pod, uint256 id) public virtual {
56-
if (_info(id).addPod(msg.sender, pod, balanceOf(msg.sender, id)) > podsLimit) revert PodsLimitReachedForAccount();
56+
if (_pods[id].addPod(msg.sender, pod, balanceOf(msg.sender, id), podCallGasLimit) > podsLimit) revert PodsLimitReachedForAccount();
5757
}
5858

5959
function removePod(address pod, uint256 id) public virtual {
60-
_info(id).removePod(msg.sender, pod, balanceOf(msg.sender, id));
60+
_pods[id].removePod(msg.sender, pod, balanceOf(msg.sender, id), podCallGasLimit);
6161
}
6262

6363
function removeAllPods(uint256 id) public virtual {
64-
_info(id).removeAllPods(msg.sender, balanceOf(msg.sender, id));
65-
}
66-
67-
function _info(uint256 id) private view returns(TokenPodsLib.Info memory) {
68-
return TokenPodsLib.makeInfo(_pods[id], podCallGasLimit);
64+
_pods[id].removeAllPods(msg.sender, balanceOf(msg.sender, id), podCallGasLimit);
6965
}
7066

7167
// ERC1155 Overrides
@@ -82,7 +78,7 @@ abstract contract ERC1155Pods is ERC1155, IERC1155Pods, ReentrancyGuardExt {
8278

8379
unchecked {
8480
for (uint256 i = 0; i < ids.length; i++) {
85-
_info(ids[i]).updateBalancesWithTokenId(from, to, amounts[i], ids[i]);
81+
_pods[i].updateBalancesWithTokenId(from, to, amounts[i], ids[i], podCallGasLimit);
8682
}
8783
}
8884
}

contracts/ERC20Pods.sol

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import "./TokenPodsLib.sol";
1010
import "./libs/ReentrancyGuard.sol";
1111

1212
abstract contract ERC20Pods is ERC20, IERC20Pods, ReentrancyGuardExt {
13-
using TokenPodsLib for TokenPodsLib.Info;
13+
using TokenPodsLib for TokenPodsLib.Data;
1414
using ReentrancyGuardLib for ReentrancyGuardLib.Data;
1515

1616
error ZeroPodsLimit();
@@ -30,49 +30,45 @@ abstract contract ERC20Pods is ERC20, IERC20Pods, ReentrancyGuardExt {
3030
}
3131

3232
function hasPod(address account, address pod) public view virtual returns(bool) {
33-
return _info().hasPod(account, pod);
33+
return _pods.hasPod(account, pod);
3434
}
3535

3636
function podsCount(address account) public view virtual returns(uint256) {
37-
return _info().podsCount(account);
37+
return _pods.podsCount(account);
3838
}
3939

4040
function podAt(address account, uint256 index) public view virtual returns(address) {
41-
return _info().podAt(account, index);
41+
return _pods.podAt(account, index);
4242
}
4343

4444
function pods(address account) public view virtual returns(address[] memory) {
45-
return _info().pods(account);
45+
return _pods.pods(account);
4646
}
4747

4848
function balanceOf(address account) public nonReentrantView(_guard) view override(IERC20, ERC20) virtual returns(uint256) {
4949
return super.balanceOf(account);
5050
}
5151

5252
function podBalanceOf(address pod, address account) public nonReentrantView(_guard) view virtual returns(uint256) {
53-
return _info().podBalanceOf(account, pod, super.balanceOf(account));
53+
return _pods.podBalanceOf(account, pod, super.balanceOf(account));
5454
}
5555

5656
function addPod(address pod) public virtual {
57-
if (_info().addPod(msg.sender, pod, balanceOf(msg.sender)) > podsLimit) revert PodsLimitReachedForAccount();
57+
if (_pods.addPod(msg.sender, pod, balanceOf(msg.sender), podCallGasLimit) > podsLimit) revert PodsLimitReachedForAccount();
5858
}
5959

6060
function removePod(address pod) public virtual {
61-
_info().removePod(msg.sender, pod, balanceOf(msg.sender));
61+
_pods.removePod(msg.sender, pod, balanceOf(msg.sender), podCallGasLimit);
6262
}
6363

6464
function removeAllPods() public virtual {
65-
_info().removeAllPods(msg.sender, balanceOf(msg.sender));
66-
}
67-
68-
function _info() private view returns(TokenPodsLib.Info memory) {
69-
return TokenPodsLib.makeInfo(_pods, podCallGasLimit);
65+
_pods.removeAllPods(msg.sender, balanceOf(msg.sender), podCallGasLimit);
7066
}
7167

7268
// ERC20 Overrides
7369

7470
function _afterTokenTransfer(address from, address to, uint256 amount) internal nonReentrant(_guard) override virtual {
7571
super._afterTokenTransfer(from, to, amount);
76-
_info().updateBalances(from, to, amount);
72+
_pods.updateBalances(from, to, amount, podCallGasLimit);
7773
}
7874
}

contracts/ERC721Pods.sol

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import "./TokenPodsLib.sol";
1111
import "./libs/ReentrancyGuard.sol";
1212

1313
abstract contract ERC721Pods is ERC721, IERC721Pods, ReentrancyGuardExt {
14-
using TokenPodsLib for TokenPodsLib.Info;
14+
using TokenPodsLib for TokenPodsLib.Data;
1515
using ReentrancyGuardLib for ReentrancyGuardLib.Data;
1616

1717
error ZeroPodsLimit();
@@ -31,49 +31,45 @@ abstract contract ERC721Pods is ERC721, IERC721Pods, ReentrancyGuardExt {
3131
}
3232

3333
function hasPod(address account, address pod) public view virtual returns(bool) {
34-
return _info().hasPod(account, pod);
34+
return _pods.hasPod(account, pod);
3535
}
3636

3737
function podsCount(address account) public view virtual returns(uint256) {
38-
return _info().podsCount(account);
38+
return _pods.podsCount(account);
3939
}
4040

4141
function podAt(address account, uint256 index) public view virtual returns(address) {
42-
return _info().podAt(account, index);
42+
return _pods.podAt(account, index);
4343
}
4444

4545
function pods(address account) public view virtual returns(address[] memory) {
46-
return _info().pods(account);
46+
return _pods.pods(account);
4747
}
4848

4949
function balanceOf(address account) public nonReentrantView(_guard) view override(IERC721, ERC721) virtual returns(uint256) {
5050
return super.balanceOf(account);
5151
}
5252

5353
function podBalanceOf(address pod, address account) public nonReentrantView(_guard) view virtual returns(uint256) {
54-
return _info().podBalanceOf(account, pod, super.balanceOf(account));
54+
return _pods.podBalanceOf(account, pod, super.balanceOf(account));
5555
}
5656

5757
function addPod(address pod) public virtual {
58-
if (_info().addPod(msg.sender, pod, balanceOf(msg.sender)) > podsLimit) revert PodsLimitReachedForAccount();
58+
if (_pods.addPod(msg.sender, pod, balanceOf(msg.sender), podCallGasLimit) > podsLimit) revert PodsLimitReachedForAccount();
5959
}
6060

6161
function removePod(address pod) public virtual {
62-
_info().removePod(msg.sender, pod, balanceOf(msg.sender));
62+
_pods.removePod(msg.sender, pod, balanceOf(msg.sender), podCallGasLimit);
6363
}
6464

6565
function removeAllPods() public virtual {
66-
_info().removeAllPods(msg.sender, balanceOf(msg.sender));
67-
}
68-
69-
function _info() private view returns(TokenPodsLib.Info memory) {
70-
return TokenPodsLib.makeInfo(_pods, podCallGasLimit);
66+
_pods.removeAllPods(msg.sender, balanceOf(msg.sender), podCallGasLimit);
7167
}
7268

7369
// ERC721 Overrides
7470

7571
function _afterTokenTransfer(address from, address to, uint256 firstTokenId, uint256 batchSize) internal nonReentrant(_guard) override virtual {
7672
super._afterTokenTransfer(from, to, firstTokenId, batchSize);
77-
_info().updateBalances(from, to, batchSize);
73+
_pods.updateBalances(from, to, batchSize, podCallGasLimit);
7874
}
7975
}

contracts/Pod.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
pragma solidity ^0.8.0;
44

55
import "./interfaces/IPod.sol";
6+
import "./interfaces/IPodWithId.sol";
67
import "./interfaces/IERC20Pods.sol";
78

8-
abstract contract Pod is IPod {
9+
abstract contract Pod is IPod, IPodWithId {
910
error AccessDenied();
1011

1112
IERC20Pods public immutable token;

0 commit comments

Comments
 (0)