1

Basically I need to get the hash from the function in the original contract, and for that the function makeBet() requires some input. The function looks like this:

pragma solidity >=0.8.0 <0.9.0;

contract Bet {

    address creator;

    // Bet status
    enum BetStatus { LONG, SHORT, LONG_NGMI, SHORT_NGMI }
    event Received(address, uint);

    // Game structure
    struct Game {
        uint256 betAmount;
        string coin;
        uint256 guess;
        BetStatus status;
        address maker;
        address taker;
        string expiry;
    }

    constructor() public payable {
        creator = msg.sender;
    }

     event GameCreated (
        uint newGameIndex,
        uint amountBetted,
        uint guess,
        address by
    );

    mapping (uint256 => Game) public activeGames;

     receive() external payable {
      emit Received(msg.sender, msg.value);
    }

    event BetMade (bytes32);

    function makeBet(string memory coin, uint guess, string memory expiry, uint betStatus) public payable {

      Game memory newGame = Game(msg.value, coin, guess, BetStatus(0), msg.sender, address(0), expiry);

      newGame.status = BetStatus(betStatus);

      bytes32 hash = keccak256(abi.encodePacked(msg.sender, msg.value, block.timestamp, coin, guess, expiry, newGame.status));

      activeGames[uint256(hash)] = newGame;

      emit BetMade(hash);
    }

As the original contract is finished, and it is my first finished smart contract, I also want to do some proper testing before deploying it. Here is my Bet_test.sol contract:

contract TestBet {
    
  Bet bet;

  function beforeAll() public {
    bet = new Bet();
  }
/// #value: 100
    function getHash() public returns (bytes32) {
        bet.makeBet("ETH", 5000, "tomorrow", 0);
        return hash;
    }
}

This however returns an error when I am trying to test it, saying that 'member "coin" not found or not visible'. That is probably due to the fact that it is a local variable in the original function. How do I access it? I am a bit lost as a first-timer.

2 Answers 2

0

In the original contract the function first argument is of type string. But in your test your Sending ETH as argument which not natively a string either you surround the value with "" to make it a string or create a variable like:

string ETH="ETH";
bet.makeBet(ETH, 5000, tomorrow, 0);

OR bet.makeBet("ETH",5000,tomorrow,0); Don't forget to use the same methodology for the third parameter as well because that has the same problem as the first argument.

7
  • Well that does not change much - this still returns an error and is not working as a test.
    – Teo
    Jun 29, 2021 at 9:45
  • can you share the latest updated code Jun 29, 2021 at 10:15
  • I updated the code with your code, and I also added Bet mapping at the top, for you to better understand the structure. Basically in the test I need to get the hash, which I will need to use later to check if I get correct bet outcomes.
    – Teo
    Jun 29, 2021 at 10:44
  • From your updated question I understand that your original error is resolved and a new one is generated, from your updated code, bet.makeBet.coin is not a valid way to access the local scoped variable, either modify the code and return the value from the function or create another event and test the code through that event. Jun 29, 2021 at 13:05
  • Ok I updated the main code once again, now the original contract's code is complete. Basically I need to get the hash from the test input. How do I achieve that?
    – Teo
    Jun 29, 2021 at 15:06
0

It doesn’t look like you’ve defined Game. I suggest you define the struct before defining your mapping that uses it. Is that in a part of missing code here?

4
  • Also, what version of Solidity are you using? Events typically need to be defined before using the emit keyword up after the mapping.
    – maxgrok
    Jun 29, 2021 at 13:24
  • Also also, try testing it as storage for the variables, not memory first. That might fix your error in your tests.
    – maxgrok
    Jun 29, 2021 at 13:26
  • Yes you were right, Game struct was missing, I added it in the original contract. You can also now see the version of Solidity that I use. now the original contract's code is complete. Basically I need to get the hash from the test input. How do I achieve that?
    – Teo
    Jun 29, 2021 at 15:07
  • It might be helpful to deploy this in remix.ethereum.org and play with the makeBet function to see if it emits the proper hash before writing tests.
    – maxgrok
    Jun 30, 2021 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.