From e6d1f10740966c799668cd9f18f69be3ba6d0b7b Mon Sep 17 00:00:00 2001 From: ross <92001561+z0r0z@users.noreply.github.com> Date: Sun, 2 Jun 2024 13:42:08 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20Tidy=20and=20balance=20%=20check?= =?UTF-8?q?er?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gas-snapshot | 46 ++++++++++++++++++++++++---------------------- src/IE.sol | 18 +++++++++++++++++- test/IE.t.sol | 24 +++++++++++++++++++++--- 3 files changed, 62 insertions(+), 26 deletions(-) diff --git a/.gas-snapshot b/.gas-snapshot index 0e36ace..0f90c18 100644 --- a/.gas-snapshot +++ b/.gas-snapshot @@ -1,25 +1,27 @@ -IETest:testCommandSendETH() (gas: 77576) -IETest:testCommandSendETHRawAddr() (gas: 75289) -IETest:testCommandStakeETH() (gas: 147120) -IETest:testCommandSwapDAI() (gas: 138671) -IETest:testCommandSwapETH() (gas: 185596) -IETest:testCommandSwapForETH() (gas: 145586) -IETest:testCommandSwapUSDC() (gas: 171751) -IETest:testCommandSwapUSDCForWBTC() (gas: 196670) -IETest:testDeploy() (gas: 4000936) -IETest:testENSNameOwnership() (gas: 50696) -IETest:testPreviewCommandSendDecimals() (gas: 111460) -IETest:testPreviewCommandSendUSDC() (gas: 70114) -IETest:testPreviewSend() (gas: 56016) -IETest:testPreviewSendCommand() (gas: 69640) -IETest:testPreviewSendCommandRawAddr() (gas: 66822) -IETest:testPreviewSendRawAddr() (gas: 30017) +IETest:testCommandSendETH() (gas: 77527) +IETest:testCommandSendETHRawAddr() (gas: 75284) +IETest:testCommandStakeETH() (gas: 148215) +IETest:testCommandSwapDAI() (gas: 130008) +IETest:testCommandSwapETH() (gas: 155437) +IETest:testCommandSwapForETH() (gas: 137166) +IETest:testCommandSwapUSDC() (gas: 171664) +IETest:testCommandSwapUSDCForWBTC() (gas: 196641) +IETest:testDeploy() (gas: 4117277) +IETest:testENSNameOwnership() (gas: 50740) +IETest:testPreviewBalanceChangeDAI() (gas: 129875) +IETest:testPreviewBalanceChangeETH() (gas: 70179) +IETest:testPreviewCommandSendDecimals() (gas: 111362) +IETest:testPreviewCommandSendUSDC() (gas: 70065) +IETest:testPreviewSend() (gas: 56060) +IETest:testPreviewSendCommand() (gas: 69635) +IETest:testPreviewSendCommandRawAddr() (gas: 66817) +IETest:testPreviewSendRawAddr() (gas: 30061) IETest:testTokenAliasSetting() (gas: 10964) -IETest:testTranslateCommand() (gas: 10531) -IETest:testTranslateExecuteSend0_0_1ETH() (gas: 29102) -IETest:testTranslateExecuteSend0_1ETH() (gas: 28475) -IETest:testTranslateExecuteSend10USDC() (gas: 26870) -IETest:testTranslateExecuteSend1ETH() (gas: 30784) -IETest:testTranslateExecuteSend1Wei() (gas: 32438) +IETest:testTranslateCommand() (gas: 10575) +IETest:testTranslateExecuteSend0_0_1ETH() (gas: 29150) +IETest:testTranslateExecuteSend0_1ETH() (gas: 28479) +IETest:testTranslateExecuteSend10USDC() (gas: 26874) +IETest:testTranslateExecuteSend1ETH() (gas: 30788) +IETest:testTranslateExecuteSend1Wei() (gas: 32486) NAMITest:testFailRegister() (gas: 9532) NAMITest:testRegister() (gas: 59011) \ No newline at end of file diff --git a/src/IE.sol b/src/IE.sol index f451f3e..939e7c5 100644 --- a/src/IE.sol +++ b/src/IE.sol @@ -545,7 +545,12 @@ contract IE { /// ==================== COMMAND TRANSLATION ==================== /// /// @dev Translates an `intent` from raw `command()` calldata. - function translateCommand(bytes calldata callData) public pure returns (string memory intent) { + function translateCommand(bytes calldata callData) + public + pure + virtual + returns (string memory intent) + { return string(callData[4:]); } @@ -653,6 +658,17 @@ contract IE { /// ================== BALANCE & SUPPLY HELPERS ================== /// + /// @dev Returns resulting percentage change of ETH or token balance. + function previewBalanceChange(address user, string calldata intent) + public + view + virtual + returns (uint256 percentage) + { + (, uint256 amount,, address token,,) = previewCommand(intent); + return (amount * 100) / (token == ETH ? user.balance : _balanceOf(token, user)); + } + /// @dev Returns the balance of a named account in a named token. function whatIsTheBalanceOf(string calldata name, /*(bob)*/ /*in*/ string calldata token) public diff --git a/test/IE.t.sol b/test/IE.t.sol index b7d2080..26b4664 100644 --- a/test/IE.t.sol +++ b/test/IE.t.sol @@ -18,10 +18,13 @@ contract IETest is Test { address internal constant WSTETH = 0x5979D7b546E38E414F7E9822514be443A4800529; address internal constant RETH = 0xEC70Dcb4A1EFa46b8F2D97C310C9c4790ba5ffA8; - address internal constant VITALIK_DOT_ETH = 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789; + address internal constant SHIVANSHI_DOT_ETH = 0xCB0592589602B841BE035e1e64C2A5b1Ef006aa2; + address internal constant CATTIN_DOT_ETH = 0xA9D2BCF3AcB743340CdB1D858E529A23Cef37838; address internal constant Z0R0Z_DOT_ETH = 0x1C0Aa8cCD568d90d61659F060D1bFb1e6f855A20; address internal constant NANI_DOT_ETH = 0x7AF890Ca7262D6accdA5c9D24AC42e35Bb293188; + address internal constant ENTRY_POINT = 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789; + address internal constant USDC_WHALE = 0x62383739D68Dd0F844103Db8dFb05a7EdED5BBE6; address internal constant DAI_WHALE = 0x2d070ed1321871841245D8EE5B84bD2712644322; @@ -129,12 +132,12 @@ contract IETest is Test { } function testCommandSwapETH() public payable { - vm.prank(VITALIK_DOT_ETH); // Note: price might change in the future. + vm.prank(ENTRY_POINT); // Note: price might change in the future. ie.command{value: 1 ether}("swap 1 eth for 2800 dai"); } function testCommandStakeETH() public payable { - vm.prank(VITALIK_DOT_ETH); + vm.prank(ENTRY_POINT); ie.command{value: 1 ether}("stake 1 eth into lido"); } @@ -228,6 +231,21 @@ contract IETest is Test { string memory ret = ie.translateExecute(execData); assertEq(ret, intent); } + + function testPreviewBalanceChangeDAI() public payable { + string memory intent = "send 1 DAI to 0x1c0aa8ccd568d90d61659f060d1bfb1e6f855a20"; + uint256 percentageChange = ie.previewBalanceChange(SHIVANSHI_DOT_ETH, intent); + assertEq(percentageChange, 50); + intent = "send 2 DAI to 0x1c0aa8ccd568d90d61659f060d1bfb1e6f855a20"; + percentageChange = ie.previewBalanceChange(SHIVANSHI_DOT_ETH, intent); + assertEq(percentageChange, 100); + } + + function testPreviewBalanceChangeETH() public payable { + string memory intent = "send 0.4 ETH to 0x1c0aa8ccd568d90d61659f060d1bfb1e6f855a20"; + uint256 percentageChange = ie.previewBalanceChange(SHIVANSHI_DOT_ETH, intent); + assertEq(percentageChange, 40); + } } interface IERC20 {