0

The wallet address that is sent through event differs from the one stored in contract

Hi, I have a contract that is deployed to development network through truffle. I trigger function that looks like this:

    struct Round {
        bool isValue;
        uint32 id;
        RoundState state;
        address[] addresses;
        RoundBet[] bets;
        mapping(address => bool) betUsers;
        mapping(address => uint256) userBets;
        uint256 winTicket;
        uint256 amount;
        uint256 lastTicket;
        address winner;
    }
.....

    event roundBet(
        address user,
        uint256 amount,
        uint256 start,
        uint256 end
    );

......
function test() payable public {
        Round storage round = roundsHistory[currentRound];
        require(round.isValue == true);
        require(round.state == RoundState.started);
        require(msg.value >= MIN_BET);
        uint256 amount = msg.value - msg.value % MIN_STEP;
        if(!round.betUsers[msg.sender]){
            round.addresses.push(msg.sender);
            round.betUsers[msg.sender] = true;
        }
        round.userBets[msg.sender] += amount;
        uint256 sticket = round.lastTicket + 1;
        uint256 eticket = sticket + amount;
        uint256 length = round.bets.push(RoundBet(true, sticket, eticket, msg.sender, amount));

        round.amount += amount;
        round.lastTicket = eticket;

        if(round.addresses.length == 2){
            round.state = RoundState.running;
            emit roundTimerStart(currentRound);
        }
        emit roundBet(msg.sender,amount, sticket, eticket);
}

AS you can see I emit roundBet event at the end of function call. The problem is that value of "user" of that event differs from msg.sender that is stored in round.addresses(values stored in round.addresses - is currect and the one emited - is wrong)

1099511627776
  • 176
  • 1
  • 18

2 Answers2

1

If you are using Metamask, keep in mind that it does not switch the account set as msg.sender to the contract, it seems to use the first account (0) to sign every transaction.

We encountered the same problem during a school project.

SomeDutchGuy
  • 2,249
  • 4
  • 16
  • 42
1

First of all about platform. It was tron not ethereum. May be in ethereum there is no such issue. So what I did:

  1. I do not pass address in event. I save it in internal structure
  2. In event I do pass index of saved address in structure
  3. I've wrote a separate method that returns address (and other usefull info) from internal structures by its index.

So by using this workaround I was able to get needed information from contract.

1099511627776
  • 176
  • 1
  • 18