Skip to content

Commit

Permalink
Refactor to reduce code size and avoid memory structs
Browse files Browse the repository at this point in the history
  • Loading branch information
k06a committed Dec 25, 2022
1 parent cd831e1 commit 8efff87
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 107 deletions.
24 changes: 10 additions & 14 deletions contracts/ERC1155Pods.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import "./TokenPodsLib.sol";
import "./libs/ReentrancyGuard.sol";

abstract contract ERC1155Pods is ERC1155, IERC1155Pods, ReentrancyGuardExt {
using TokenPodsLib for TokenPodsLib.Info;
using TokenPodsLib for TokenPodsLib.Data;
using ReentrancyGuardLib for ReentrancyGuardLib.Data;

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

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

function podsCount(address account, uint256 id) public view virtual returns(uint256) {
return _info(id).podsCount(account);
return _pods[id].podsCount(account);
}

function podAt(address account, uint256 index, uint256 id) public view virtual returns(address) {
return _info(id).podAt(account, index);
return _pods[id].podAt(account, index);
}

function pods(address account, uint256 id) public view virtual returns(address[] memory) {
return _info(id).pods(account);
return _pods[id].pods(account);
}

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

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

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

function removePod(address pod, uint256 id) public virtual {
_info(id).removePod(msg.sender, pod, balanceOf(msg.sender, id));
_pods[id].removePod(msg.sender, pod, balanceOf(msg.sender, id), podCallGasLimit);
}

function removeAllPods(uint256 id) public virtual {
_info(id).removeAllPods(msg.sender, balanceOf(msg.sender, id));
}

function _info(uint256 id) private view returns(TokenPodsLib.Info memory) {
return TokenPodsLib.makeInfo(_pods[id], podCallGasLimit);
_pods[id].removeAllPods(msg.sender, balanceOf(msg.sender, id), podCallGasLimit);
}

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

unchecked {
for (uint256 i = 0; i < ids.length; i++) {
_info(ids[i]).updateBalancesWithTokenId(from, to, amounts[i], ids[i]);
_pods[i].updateBalancesWithTokenId(from, to, amounts[i], ids[i], podCallGasLimit);
}
}
}
Expand Down
24 changes: 10 additions & 14 deletions contracts/ERC20Pods.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import "./TokenPodsLib.sol";
import "./libs/ReentrancyGuard.sol";

abstract contract ERC20Pods is ERC20, IERC20Pods, ReentrancyGuardExt {
using TokenPodsLib for TokenPodsLib.Info;
using TokenPodsLib for TokenPodsLib.Data;
using ReentrancyGuardLib for ReentrancyGuardLib.Data;

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

function hasPod(address account, address pod) public view virtual returns(bool) {
return _info().hasPod(account, pod);
return _pods.hasPod(account, pod);
}

function podsCount(address account) public view virtual returns(uint256) {
return _info().podsCount(account);
return _pods.podsCount(account);
}

function podAt(address account, uint256 index) public view virtual returns(address) {
return _info().podAt(account, index);
return _pods.podAt(account, index);
}

function pods(address account) public view virtual returns(address[] memory) {
return _info().pods(account);
return _pods.pods(account);
}

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

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

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

function removePod(address pod) public virtual {
_info().removePod(msg.sender, pod, balanceOf(msg.sender));
_pods.removePod(msg.sender, pod, balanceOf(msg.sender), podCallGasLimit);
}

function removeAllPods() public virtual {
_info().removeAllPods(msg.sender, balanceOf(msg.sender));
}

function _info() private view returns(TokenPodsLib.Info memory) {
return TokenPodsLib.makeInfo(_pods, podCallGasLimit);
_pods.removeAllPods(msg.sender, balanceOf(msg.sender), podCallGasLimit);
}

// ERC20 Overrides

function _afterTokenTransfer(address from, address to, uint256 amount) internal nonReentrant(_guard) override virtual {
super._afterTokenTransfer(from, to, amount);
_info().updateBalances(from, to, amount);
_pods.updateBalances(from, to, amount, podCallGasLimit);
}
}
24 changes: 10 additions & 14 deletions contracts/ERC721Pods.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import "./TokenPodsLib.sol";
import "./libs/ReentrancyGuard.sol";

abstract contract ERC721Pods is ERC721, IERC721Pods, ReentrancyGuardExt {
using TokenPodsLib for TokenPodsLib.Info;
using TokenPodsLib for TokenPodsLib.Data;
using ReentrancyGuardLib for ReentrancyGuardLib.Data;

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

function hasPod(address account, address pod) public view virtual returns(bool) {
return _info().hasPod(account, pod);
return _pods.hasPod(account, pod);
}

function podsCount(address account) public view virtual returns(uint256) {
return _info().podsCount(account);
return _pods.podsCount(account);
}

function podAt(address account, uint256 index) public view virtual returns(address) {
return _info().podAt(account, index);
return _pods.podAt(account, index);
}

function pods(address account) public view virtual returns(address[] memory) {
return _info().pods(account);
return _pods.pods(account);
}

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

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

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

function removePod(address pod) public virtual {
_info().removePod(msg.sender, pod, balanceOf(msg.sender));
_pods.removePod(msg.sender, pod, balanceOf(msg.sender), podCallGasLimit);
}

function removeAllPods() public virtual {
_info().removeAllPods(msg.sender, balanceOf(msg.sender));
}

function _info() private view returns(TokenPodsLib.Info memory) {
return TokenPodsLib.makeInfo(_pods, podCallGasLimit);
_pods.removeAllPods(msg.sender, balanceOf(msg.sender), podCallGasLimit);
}

// ERC721 Overrides

function _afterTokenTransfer(address from, address to, uint256 firstTokenId, uint256 batchSize) internal nonReentrant(_guard) override virtual {
super._afterTokenTransfer(from, to, firstTokenId, batchSize);
_info().updateBalances(from, to, batchSize);
_pods.updateBalances(from, to, batchSize, podCallGasLimit);
}
}
3 changes: 2 additions & 1 deletion contracts/Pod.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
pragma solidity ^0.8.0;

import "./interfaces/IPod.sol";
import "./interfaces/IPodWithId.sol";
import "./interfaces/IERC20Pods.sol";

abstract contract Pod is IPod {
abstract contract Pod is IPod, IPodWithId {
error AccessDenied();

IERC20Pods public immutable token;
Expand Down
Loading

0 comments on commit 8efff87

Please sign in to comment.