Skip to content

Commit

Permalink
refactor: code optimization and style adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
pseudoyu committed Jul 25, 2023
1 parent c28c142 commit a38f0e9
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 45 deletions.
5 changes: 3 additions & 2 deletions contracts/interfaces/ITipsWithConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@ interface ITipsWithConfig {
/**
* @notice Initializes the ITipsWithConfig.
* @param web3Entry_ Address of web3Entry.
* @param token_ Address of token.
*/
function initialize(address web3Entry_, address token_) external;
function initialize(address web3Entry_) external;

/**
* @notice Set the tips config for character.
* @param fromCharacterId The from character ID.
* @param toCharacterId The to character ID.
* @param token The tip token address.
* @param amount The amount of token.
* @param interval The interval of tips with periodical config.
* @param expiration The expiration of tips with periodical config.
*/
function setTipsConfig4Character(
uint256 fromCharacterId,
uint256 toCharacterId,
address token,
uint256 amount,
uint256 interval,
uint256 expiration
Expand Down
41 changes: 13 additions & 28 deletions contracts/misc/TipsWIthConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {Initializable} from "@openzeppelin/contracts/proxy/utils/Initializable.s
* to the to character.
*/
contract TipsWithConfig is ITipsWithConfig, Initializable {
using SafeERC20 for IERC20;

// structs
struct TipsConfig {
uint256 id;
Expand Down Expand Up @@ -98,31 +100,23 @@ contract TipsWithConfig is ITipsWithConfig, Initializable {
/**
* @notice Initialize the contract, setting web3Entry address and token address.
* @param web3Entry_ Address of web3Entry.
* @param token_ Address of token.
*/
function initialize(address web3Entry_, address token_) external override initializer {
function initialize(address web3Entry_) external override initializer {
_web3Entry = web3Entry_;
_token = token_;

// register interfaces
ERC1820_REGISTRY.setInterfaceImplementer(
address(this),
TOKENS_RECIPIENT_INTERFACE_HASH,
address(this)
);
}

/// @inheritdoc ITipsWithConfig
function setTipsConfig4Character(
uint256 fromCharacterId,
uint256 toCharacterId,
address token,
uint256 amount,
uint256 interval,
uint256 expiration
) external override {
TipsConfig memory config;
require(interval > 0, "TipsWithConfig: interval must be greater than 0");
uint256 tipConfigId = _tipsConfigIds[fromCharacterId][toCharacterId];
config = _tipsConfigs[tipConfigId];
TipsConfig storage config = _tipsConfigs[tipConfigId];

if (tipConfigId == 0) {
// if tipConfigId is 0, create a new config
Expand All @@ -133,7 +127,7 @@ contract TipsWithConfig is ITipsWithConfig, Initializable {
_tipsConfigIds[fromCharacterId][toCharacterId] = config.id;
} else {
// if tipConfigId is not 0, update the config
config.token = _token;
config.token = token;
config.amount = amount;
config.expiration = expiration;
config.interval = interval;
Expand Down Expand Up @@ -180,7 +174,7 @@ contract TipsWithConfig is ITipsWithConfig, Initializable {

/// @inheritdoc ITipsWithConfig
function triggerTips4Character(uint256 tipConfigId) external override {
TipsConfig memory config = _tipsConfigs[tipConfigId];
TipsConfig storage config = _tipsConfigs[tipConfigId];

require(config.redeemedTimes < config.tipTimes, "TipsWithConfig: all tips redeemed");

Expand Down Expand Up @@ -252,11 +246,7 @@ contract TipsWithConfig is ITipsWithConfig, Initializable {
// send token
address from = IERC721(_web3Entry).ownerOf(fromCharacterId);
address to = IERC721(_web3Entry).ownerOf(toCharacterId);
bool success = IERC20(_token).transferFrom(from, to, unRedeemedAmount);

if (!success) {
revert("TipsWithConfig: transfer token failed");
}
IERC20(_token).safeTransferFrom(from, to, unRedeemedAmount);

return (availableTipTimes, unRedeemedAmount);
}

Check failure

Code scanning / Slither

Arbitrary `from` in transferFrom High

Expand All @@ -275,7 +265,7 @@ contract TipsWithConfig is ITipsWithConfig, Initializable {
uint256 expiration
)
{
TipsConfig memory config = _tipsConfigs[tipConfigId];
TipsConfig storage config = _tipsConfigs[tipConfigId];
return (
config.fromCharacterId,
config.toCharacterId,
Expand All @@ -289,11 +279,12 @@ contract TipsWithConfig is ITipsWithConfig, Initializable {
function _calculateUnredeemedTimesAndAmount(
uint256 tipConfigId
) internal view returns (uint256, uint256) {
TipsConfig memory config = _tipsConfigs[tipConfigId];
TipsConfig storage config = _tipsConfigs[tipConfigId];
uint256 elapsed = block.timestamp - config.startTime;

uint256 cycles = elapsed / config.interval;

// When user tip for a character, instant count once
uint256 availableTipTimes = cycles + 1;

if (availableTipTimes > config.tipTimes) {
Expand All @@ -309,13 +300,7 @@ contract TipsWithConfig is ITipsWithConfig, Initializable {
uint256 expireTime,
uint256 interval
) internal pure returns (uint256) {
uint256 totalInterval = expireTime - startTime;

if (interval == 0) {
return 0;
}

uint256 intervals = totalInterval / interval;
uint256 intervals = expireTime - startTime / interval;

return intervals + 1;
}
Expand Down
17 changes: 2 additions & 15 deletions test/misc/TipsWithConfig.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ contract TipsWithFeeTest is CommonTest {

// deploy and init Tips contract
_tips = new TipsWithConfig();
_tips.initialize(address(web3Entry), address(token));
_tips.initialize(address(web3Entry));

// create characters
_createCharacter(CHARACTER_HANDLE, alice);
Expand All @@ -57,26 +57,13 @@ contract TipsWithFeeTest is CommonTest {
function testSetupState() public {
// check status after initialization
assertEq(_tips.getWeb3Entry(), address(web3Entry));
assertEq(_tips.getToken(), address(token));
}

function testReinitializeFail() public {
vm.expectRevert(abi.encodePacked("Initializable: contract is already initialized"));
_tips.initialize(address(0x10), address(0x10));
_tips.initialize(address(0x10));

// check status
assertEq(_tips.getWeb3Entry(), address(web3Entry));
assertEq(_tips.getToken(), address(token));
}

// function testSetTipsConfig4Character(uint256 amount) public {
// vm.assume(amount < 1 ether && amount > 0);

// vm.prank(alice);
// _tips.setTipsConfig4Character(1, 2, amount, 3600, 1690365841);

// // expect events
// expectEmit(CheckAll);
// emit SetTipsConfig4Character(1, 1, 2, address(token), amount, 0, 3600, 1690365841, 0);
// }
}

0 comments on commit a38f0e9

Please sign in to comment.