Skip to content

Commit

Permalink
🧹 Tidy and balance % checker
Browse files Browse the repository at this point in the history
  • Loading branch information
z0r0z committed Jun 2, 2024
1 parent 480bc67 commit e6d1f10
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
46 changes: 24 additions & 22 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -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)
18 changes: 17 additions & 1 deletion src/IE.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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:]);
}

Expand Down Expand Up @@ -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
Expand Down
24 changes: 21 additions & 3 deletions test/IE.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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");
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit e6d1f10

Please sign in to comment.