From 67adc92789b6e5b962fae1e42f1b4dc5e6d1d34a Mon Sep 17 00:00:00 2001 From: ChihYunChuang Date: Mon, 1 Apr 2024 09:56:44 +0800 Subject: [PATCH] cant add function --- script/Deploy.s.sol | 1 + src/SimpleStore.huff | 24 +++++++++++++++++++----- test/SimpleStore.t.sol | 6 ++++-- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/script/Deploy.s.sol b/script/Deploy.s.sol index 1b204c8..239501b 100644 --- a/script/Deploy.s.sol +++ b/script/Deploy.s.sol @@ -7,6 +7,7 @@ import "forge-std/Script.sol"; interface SimpleStore { function setValue(uint256) external; function getValue() external returns (uint256); + function add() external returns (uint256); } contract Deploy is Script { diff --git a/src/SimpleStore.huff b/src/SimpleStore.huff index f202687..f72b27d 100644 --- a/src/SimpleStore.huff +++ b/src/SimpleStore.huff @@ -1,6 +1,9 @@ + + /* Interface */ #define function setValue(uint256) nonpayable returns () #define function getValue() view returns (uint256) +#define function add() view returns (uint256) /* Storage Slots */ #define constant VALUE_LOCATION = FREE_STORAGE_POINTER() @@ -10,9 +13,6 @@ 0x04 calldataload // [value] [VALUE_LOCATION] // [ptr, value] sstore // [] - - // End Execution - stop } #define macro GET_VALUE() = takes (0) returns (0) { @@ -22,7 +22,19 @@ // Store value in memory. 0x00 mstore + + // Return value + 0x20 0x00 return +} +#define macro Add() = takes (0) returns (0) { + // Load value from storage. + [VALUE_LOCATION] // [ptr] + sload // [value] + + // Store value in memory. + 0x00 mstore + // Return value 0x20 0x00 return } @@ -31,7 +43,8 @@ // Identify which function is being called. 0x00 calldataload 0xE0 shr dup1 __FUNC_SIG(setValue) eq set jumpi - dup1 __FUNC_SIG(getValue) eq get jumpi + dup1 0x20965255 eq get jumpi + dup1 __FUNC_SIG(add) eq add jumpi 0x00 0x00 revert @@ -39,5 +52,6 @@ SET_VALUE() get: GET_VALUE() - + add: + Add() } \ No newline at end of file diff --git a/test/SimpleStore.t.sol b/test/SimpleStore.t.sol index 003763c..36e3ad4 100644 --- a/test/SimpleStore.t.sol +++ b/test/SimpleStore.t.sol @@ -15,9 +15,10 @@ contract SimpleStoreTest is Test { } /// @dev Ensure that you can set and get the value. - function testSetAndGetValue(uint256 value) public { + function testSetAndGetValue() public { + uint256 value = 7; simpleStore.setValue(value); - console.log(value); + console.log(simpleStore.add()); console.log(simpleStore.getValue()); assertEq(value, simpleStore.getValue()); } @@ -26,4 +27,5 @@ contract SimpleStoreTest is Test { interface SimpleStore { function setValue(uint256) external; function getValue() external returns (uint256); + function add( ) external returns (uint256); }