0

Let's say there's a button in html that can claim winnings.

<button type="submit" (click)="claimWinnigs()">Claim Winnings</button>

A user with account address 0xf17...b732 claimed winnings by clicking the button. What I want here is I want that user to be able to withdraw eth from the contract address (0x8f...b90f). Metamask is the tool I'm using.

simplified solidity:

mapping(address => uint256) public balanceOf

function claimWinnings() returns (bool) {   
    balanceOf[msg.sender] = 20
    withdraw();

    return true;
}

function withdraw() public {
    uint256 balance = balanceOf[msg.sender];  
    balanceOf[msg.sender] -= balance;
    msg.sender.transfer(balance);

    LogWithdrawal(msg.sender, balance);
}    

Below is my angular web3 code:

claimWinnings(): Promise<any> { 
    return this.TestContract.deployed().then((instance) => {
        return claimWinnings({
            from: contractAddress,                               
            gas: 1000000
        });
    })
    .then((value) => {
        return value;
    })
    .catch((e) => {
        console.log(e);
    }); 
}

But when I execute this, I get an error

Unknown address - unable to sign transaction for this address: "0x8f...b90f"

I'm testing with Ganache fyi.

What am I doing wrong here? and how can the user receive ether from the contract?

  • 1
    You are calling claim winning from contract address (which doesn't have a private key to sign the transaction). You should call this function from EOA (from user's account who is claiming the withdrawal) – Prashant Prabhakar Singh Feb 27 '18 at 8:59
2

You should put the logic of the withdraw function to the claimWinnings function and make that publicly claimable by the (user!) address, that has a positive balance (put a require statement, if it is eligible to withdraw of course).

The user calling the claimWinnings function may then be identified by msg.sender.

You also mixed up the "from" address in your test, that should be one of your account addresses, e.g. account[0].

  • I had to change from address to the account address and it all worked. Thanks. – bbusdriver Feb 27 '18 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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