2

I am trying to build a Dapp which includes functionality to deposit funds to a contract and withdraw them. The functions look like this

function fundContract() public payable {
        require (msg.sender == users[msg.sender].addr);
        users[msg.sender].balance += msg.value;
        contractBalance+=msg.value;

    }


function withdraw (uint amount) public onlyUser() payable{

        require (msg.sender == users[msg.sender].addr);
        require (amount > 0);
        require (amount <= users[msg.sender].balance);

        msg.sender.transfer(amount);

        users[msg.sender].balance -= amount;
        contractBalance-= amount;
    }

I understand that there will be a function call to withdraw() specifying the amount parameter from the front end using web3.js send() command, but how do I provide the msg.value for the fundContract method from the viewpoint of a Dapp. Remix allows specifying the value in the message , but I can't find a way to do so for a Dapp.

2
  • 1
    Whatever library you're using, there will be a way to attach ether to a transaction. Let us know what library, and we can point you to the relevant documentation. Web3.js is a likely candidate for what you're going to use, and there are two significant versions: 0.20.x and 1.0 beta.
    – user19510
    Commented Sep 12, 2018 at 17:10
  • @smarx I am building on Qtum and using QtumJS which is similar to web3.js The answer by Sam makes it clear how to use it with web3 and I found a similar way with QtumJS. Thank you Commented Sep 12, 2018 at 17:43

1 Answer 1

0

You should use the value field, then call the contract send method.

https://web3js.readthedocs.io/en/1.0/web3-eth-contract.html#methods-mymethod-send

myContract.methods.fundContract().send({value: 'AMOUNT IN WEI'})

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.