Skip to content

Commit

Permalink
cant add function
Browse files Browse the repository at this point in the history
  • Loading branch information
cychuang0924 committed Apr 1, 2024
1 parent bb59e01 commit 67adc92
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
1 change: 1 addition & 0 deletions script/Deploy.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
24 changes: 19 additions & 5 deletions src/SimpleStore.huff
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -10,9 +13,6 @@
0x04 calldataload // [value]
[VALUE_LOCATION] // [ptr, value]
sstore // []

// End Execution
stop
}

#define macro GET_VALUE() = takes (0) returns (0) {
Expand All @@ -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
}
Expand All @@ -31,13 +43,15 @@
// 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

set:
SET_VALUE()
get:
GET_VALUE()

add:
Add()
}
6 changes: 4 additions & 2 deletions test/SimpleStore.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -26,4 +27,5 @@ contract SimpleStoreTest is Test {
interface SimpleStore {
function setValue(uint256) external;
function getValue() external returns (uint256);
function add( ) external returns (uint256);
}

0 comments on commit 67adc92

Please sign in to comment.