Skip to content

Commit 05dad32

Browse files
committed
feat: optimize IntraBlockTokenTracking _spotSend()
1 parent 9896ace commit 05dad32

5 files changed

Lines changed: 34 additions & 33 deletions

File tree

src/HyperCoreToken.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {PrecompileLib} from "hyper-evm-lib/PrecompileLib.sol";
1111
/// On burning side, any token holder can call it to burn them at 1:1 rate, the same amount will be receive at core
1212
contract HyperCoreToken is ERC20("HyperCoreUSDC", "HCUSDC") {
1313
/// @dev Wallet factory
14-
IWalletFactory public immutable walletFactory;
14+
IWalletFactory public immutable WALLET_FACTORY;
1515

1616
/// @dev Core enabler fee (1 USDC)
1717
uint64 public constant ENABLER_FEE = 1e8;
@@ -26,7 +26,7 @@ contract HyperCoreToken is ERC20("HyperCoreUSDC", "HCUSDC") {
2626
error HCT_ReceiverNotEnabled();
2727

2828
constructor(address walletFactory_) {
29-
walletFactory = IWalletFactory(walletFactory_);
29+
WALLET_FACTORY = IWalletFactory(walletFactory_);
3030
}
3131

3232
/**
@@ -66,7 +66,7 @@ contract HyperCoreToken is ERC20("HyperCoreUSDC", "HCUSDC") {
6666
}
6767

6868
modifier onlyWallet() {
69-
if (!walletFactory.isWallet(msg.sender)) revert HCT_OnlyWallet();
69+
if (!WALLET_FACTORY.isWallet(msg.sender)) revert HCT_OnlyWallet();
7070
_;
7171
}
7272
}

src/IntraBlockTokenTracking.sol

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,35 @@ abstract contract IntraBlockTokenTracking {
1111
/// @dev last block interaction
1212
uint256 lastUsedBlock;
1313

14-
/// @dev trowed at invalid amount
15-
error IBTT_InvalidAmount();
16-
1714
/// @dev throwed if the balance is not enough
1815
error IBTT_InsufficientBalance();
1916

2017
/// @dev throwed when tries to send zero amount
2118
error IBTT_ZeroAmount();
2219

23-
function getBalance() external view returns (uint64) {
24-
return _getBalance();
20+
/**
21+
* @notice Get the available balance at the current block state
22+
*/
23+
function getAvailableBalance() external view returns (uint64) {
24+
return _getAvailableBalance();
2525
}
2626

27-
function _getBalance() internal view returns (uint64 balance) {
27+
/**
28+
* @notice Get the available balance at the current block state
29+
*/
30+
function _getAvailableBalance() internal view returns (uint64 balance) {
2831
PrecompileLib.SpotBalance memory spotBalance = PrecompileLib.spotBalance(address(this), 0);
2932
uint64 total = spotBalance.total;
3033
uint64 usedAmount = _getUsedAmount();
3134

32-
if (usedAmount > total) return 0;
35+
// total can't be > usedAmount
36+
// check on that in the _spotSend()
3337
return total - usedAmount;
3438
}
3539

40+
/**
41+
* @notice Add amount used in the current block
42+
*/
3643
function _addUsedAmount(uint64 amount) internal {
3744
if (lastUsedBlock != block.number) {
3845
usedTokenAmounts = amount;
@@ -42,6 +49,9 @@ abstract contract IntraBlockTokenTracking {
4249
}
4350
}
4451

52+
/**
53+
* @notice Get used amount
54+
*/
4555
function _getUsedAmount() internal view returns (uint64) {
4656
return lastUsedBlock == block.number ? usedTokenAmounts : 0;
4757
}
@@ -54,17 +64,14 @@ abstract contract IntraBlockTokenTracking {
5464
function _spotSend(address to, uint64 amount) internal {
5565
if (amount == 0) revert IBTT_ZeroAmount();
5666

57-
bool activated;
58-
uint64 balance = _getBalance();
59-
60-
if (PrecompileLib.coreUserExists(to)) {
61-
activated = true;
62-
}
63-
67+
// enabler fees not included
6468
CoreWriterLib.spotSend(to, 0, amount);
6569

70+
// get the balance - used amount in the same block
71+
uint64 balance = _getAvailableBalance();
72+
6673
// add 1USDC as fee to enable the recipient at core spot
67-
amount = activated ? amount : amount + 1e8;
74+
amount = PrecompileLib.coreUserExists(to) ? amount : amount + 1e8;
6875

6976
if (amount > balance) revert IBTT_InsufficientBalance();
7077

src/Wallet.sol

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {IHyperCoreToken} from "./interfaces/IHyperCoreToken.sol";
88
/// @dev Wallet used to mint coreToken at evm, transferring spot usdc to coreToken's address at spot
99
contract Wallet is Initializable, IntraBlockTokenTracking {
1010
/// @dev Core token
11-
address public immutable coreToken;
11+
address public immutable CORE_TOKEN;
1212

1313
/// @dev Wallet owner (immutable)
1414
address public owner;
@@ -20,13 +20,13 @@ contract Wallet is Initializable, IntraBlockTokenTracking {
2020
error W_OnlyOwner();
2121

2222
/// @dev emitted at mint
23-
event Mint(address to, uint64 amount);
23+
event Mint(address indexed to, uint64 amount);
2424

2525
/// @dev emitted when a user withdraw
26-
event Withdraw(address to, uint64 amount);
26+
event Withdraw(address indexed to, uint64 amount);
2727

2828
constructor(address coreToken_) {
29-
coreToken = coreToken_;
29+
CORE_TOKEN = coreToken_;
3030
}
3131

3232
/**
@@ -59,15 +59,12 @@ contract Wallet is Initializable, IntraBlockTokenTracking {
5959
* @param amount amount to mint
6060
*/
6161
function _mintToken(address to, uint64 amount) internal onlyOwner {
62-
// check if there is enough balance at core
63-
uint64 balance = _getBalance();
64-
if (amount > balance) revert W_NotEnoughAmount();
65-
6662
// transfer token to coreToken address at core
67-
_spotSend(coreToken, amount);
63+
// assume core token is enabled at core
64+
_spotSend(CORE_TOKEN, amount);
6865

6966
// mint token at evm
70-
IHyperCoreToken(coreToken).mint(to, amount);
67+
IHyperCoreToken(CORE_TOKEN).mint(to, amount);
7168

7269
emit Mint(to, amount);
7370
}

test/BaseTest.t.sol

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {MockSpotBalance} from "./mocks/MockSpotBalance.sol";
77
import {MockCoreUserExists} from "./mocks/MockCoreUserExists.sol";
88

99
import {HLConstants} from "hyper-evm-lib/common/HLConstants.sol";
10-
//import {CoreWriterLib} from "hyper-evm-lib/CoreWriterLib.sol";
1110

1211
abstract contract BaseTest is Test {
1312
MockSpotBalance internal spotBalancePrecompile;

test/Wallet.t.sol

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

4-
import {HLConstants} from "hyper-evm-lib/common/HLConstants.sol";
5-
64
import {BaseTest} from "./BaseTest.t.sol";
75
import {Wallet} from "src/Wallet.sol";
86
import {WalletFactory} from "src/WalletFactory.sol";
@@ -23,7 +21,7 @@ contract WalletTest is BaseTest {
2321
// deploy factory
2422
factory = new WalletFactory();
2523
wallet = Wallet(factory.createWallet(user));
26-
hcUsdc = HyperCoreToken(wallet.coreToken());
24+
hcUsdc = HyperCoreToken(wallet.CORE_TOKEN());
2725

2826
_setCore();
2927
}
@@ -33,7 +31,7 @@ contract WalletTest is BaseTest {
3331

3432
assertEq(factory.isWallet(address(wallet)), true);
3533

36-
assertEq(address(hcUsdc.walletFactory()), address(factory));
34+
assertEq(address(hcUsdc.WALLET_FACTORY()), address(factory));
3735
assertEq(hcUsdc.decimals(), 8);
3836
}
3937

0 commit comments

Comments
 (0)