Skip to content

Commit

Permalink
feat: assume 43-bytes-length RUNE addy
Browse files Browse the repository at this point in the history
  • Loading branch information
gomesalexandre committed Apr 9, 2024
1 parent a7e8a04 commit 5a545b4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
3 changes: 2 additions & 1 deletion foundry/src/FoxStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ contract FoxStaking is
}

function stake(uint256 amount, string memory runeAddress) external whenNotPaused whenStakingUnpaused {
require(bytes(runeAddress).length > 0, "Rune address cannot be empty");
require(bytes(runeAddress).length == 43, "Rune address must be 43 characters");
require(amount > 0, "FOX amount to stake must be greater than 0");
foxToken.safeTransferFrom(msg.sender, address(this), amount);

Expand Down Expand Up @@ -131,6 +131,7 @@ contract FoxStaking is
}

function setRuneAddress(string memory runeAddress) external {
require(bytes(runeAddress).length == 43, "Rune address must be 43 characters");
StakingInfo storage info = stakingInfo[msg.sender];
string memory oldRuneAddress = info.runeAddress;
info.runeAddress = runeAddress;
Expand Down
31 changes: 22 additions & 9 deletions foundry/test/FoxStaking.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ contract FOXStakingTestRuneAddress is Test {
function testCanSetRuneAddress() public {
vm.startPrank(user);

string memory newRuneAddress = "thorFooBarBaz";
string memory newRuneAddress = "thor17gw75axcnr8747pkanye45pnrwk7p9c3cqncsv";

foxStaking.setRuneAddress(newRuneAddress);

Expand All @@ -52,6 +52,17 @@ contract FOXStakingTestRuneAddress is Test {

vm.stopPrank();
}

function cannotStakeWithInvalidLengthRuneAddress() public {
vm.startPrank(user);

string memory invalidLengthRuneAddress = "thor1234";

vm.expectRevert("Rune address must be 42 characters");
foxStaking.stake(1e18, invalidLengthRuneAddress);

vm.stopPrank();
}
}

contract FOXStakingTestOwnership is Test {
Expand Down Expand Up @@ -107,7 +118,7 @@ contract FOXStakingTestStaking is Test {
function testCanStakeAfterUnpausingStake() public {
address user = address(0xBABE);
uint256 amount = 1000;
string memory runeAddress = "runeAddress";
string memory runeAddress = "thor17gw75axcnr8747pkanye45pnrwk7p9c3cqncsv";

foxToken.makeItRain(user, amount);

Expand Down Expand Up @@ -155,7 +166,7 @@ contract FOXStakingTestStaking is Test {

address user = address(0xBABE);
uint256 amount = 1000;
string memory runeAddress = "runeAddress";
string memory runeAddress = "thor17gw75axcnr8747pkanye45pnrwk7p9c3cqncsv";


(uint256 stakingBalance_before, uint256 unstakingBalance_before, , ) = foxStaking.stakingInfo(user);
Expand Down Expand Up @@ -184,7 +195,7 @@ contract FOXStakingTestStaking is Test {

function testStake_cannotStakeZero() public {
address user = address(0xD00D);
string memory runeAddress = "runeAddress";
string memory runeAddress = "thor17gw75axcnr8747pkanye45pnrwk7p9c3cqncsv";

vm.startPrank(user);

Expand Down Expand Up @@ -219,7 +230,7 @@ contract FOXStakingTestStaking is Test {
vm.assertEq(unstakingBalance, 0);

// Try to stake with empty rune address
vm.expectRevert("Rune address cannot be empty");
vm.expectRevert("Rune address must be 43 characters");
foxStaking.stake(1e18, "");

// Check user staking balances are unchanged
Expand All @@ -233,6 +244,7 @@ contract FOXStakingTestStaking is Test {

// "e2e" staking test for multiple users
function testStaking() public {
string memory baseRuneAddress = "thor17gw75axcnr8747pkanye45pnrwk7p9c3cqncs"; // 42 chars - so we can append another to make it 43
address[] memory users = new address[](3);
users[0] = address(0xBABE);
users[1] = address(0xC0DE);
Expand All @@ -245,8 +257,9 @@ contract FOXStakingTestStaking is Test {

// Simulate each user staking FOX tokens
for (uint256 i = 0; i < users.length; i++) {
// Unique mock address per user
string memory runeAddress = string(abi.encodePacked("runeAddress", Strings.toString(i)));
// Pseudo-random RUNE addy - takes the base one above and changes the last char each iteration
string memory indexChar = Strings.toString(i % 10);
string memory runeAddress = string(abi.encodePacked(baseRuneAddress, indexChar));
// Free FOX tokens for each user
foxToken.makeItRain(users[i], amounts[i]);
// https://book.getfoundry.sh/cheatcodes/start-prank
Expand All @@ -270,7 +283,7 @@ contract FOXStakingTestUnstake is Test {
address user = address(0xBEEF);
uint256 amount = 1000;

string constant runeAddress = "thorFooBarBaz";
string constant runeAddress = "thor17gw75axcnr8747pkanye45pnrwk7p9c3cqncsv";

function setUp() public {
foxToken = new MockFOXToken();
Expand Down Expand Up @@ -487,7 +500,7 @@ contract FOXStakingTestWithdraw is Test {
address user = address(0xBEEF);
uint256 amount = 1000;

string constant runeAddress = "thorFooBarBaz";
string constant runeAddress = "thor17gw75axcnr8747pkanye45pnrwk7p9c3cqncsv";

function setUp() public {
foxToken = new MockFOXToken();
Expand Down

0 comments on commit 5a545b4

Please sign in to comment.