0

I am trying to call a function in contract from web3js but variable's value is not getting updated

My code is

contract Bet {
address owner = 0x3618b7fFA11F5eE1c439f2B0C547a0e7feD7FE44;
uint totalPot = 0;
uint pot1 = 0;
uint pot2 = 0;
uint addressLUTIndex = 0;
bool gameOpen = true;

struct Player {
    uint choice;
    uint amount;
    bool isset;
}

mapping(address => Player) pot;
mapping(uint => address) playerLUT;

function () public payable {
    betforChoice(msg.sender, 1);     //auto bet for choice 1
    placeBet(msg.sender, msg.value);
}

function Bet() public payable {
    placeBet(msg.sender, msg.value);
}

function placeBet(address iplayer, uint amount) internal {
    if (pot[iplayer].isset) {
        totalPot += amount;
        pot[msg.sender].amount = amount;
        if (pot[iplayer].choice == 1) {
            pot1 += amount;
        } else {
            pot2 += amount;
        }
    }
}

modifier canSetChoice(address player, uint choice) {
    require(!pot[player].isset && (choice == 1 || choice == 2) && gameOpen);
    _;
}

function betforChoice(address player, uint user_choice) canSetChoice(player, user_choice) public {
    pot[player].choice = user_choice;
    pot[player].isset = true;
    playerLUT[addressLUTIndex] = player;
    addressLUTIndex++;

}

modifier onlyOwnerChoice(uint choice) {
    require(msg.sender == owner && (choice == 1 || choice == 2) && gameOpen);
    _;
}

function setWinner(uint owner_choice) onlyOwnerChoice(owner_choice) public {
    uint winningFactor = 0;
    uint dealerComission = 10;
...
}

modifier onlyOwner() {
    require(msg.sender == owner);
    _;
}

function getPotSize() public view onlyOwner returns(uint)  {
    return totalPot;
}
function getPotSize1() public view onlyOwner returns(uint)  {
    return pot1;
}
function getPotSize2() public view onlyOwner returns(uint)  {
    return pot2;
}

}

Whenever I call the function betforChoice the variables inside are not getting updated. What could be the issue?

1 Answer 1

0

I tried your code in Remix. I added the following function to view the attributes of a player in the pot:

function getAttributes (address _player) constant returns (uint,uint,bool) {
    return (pot[_player].choice,pot[_player].amount,pot[_player].isset);
}

When calling it with the address of the player I just used when calling betforChoice it returns the right values.

Perhaps it works out for you as well. Please let me know if not...

Hope it helps!

2
  • the isset variable is still false after i call betforchoice, your getAttributes function returned {"0":"0","1":"0","2":false} Feb 2, 2018 at 11:39
  • you are right, looks like a problem with my web3 implementation. thanks for the response. Feb 2, 2018 at 11:52

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.