Web3.js – How to Configure a Payable Function Front End in DAPP Development

dappsjavascriptsolidityweb3js

I am trying add the front end part of a payable function using web3.js (And just vanilla JavaScript, no other libraries).

Here is the solidity function:

    function deposit() public payable {
        balance[msg.sender] += msg.value;
    }

Here is a picture of the front end:
DAPP Front End

I have created a contract for the withdrawal.

function withdraw() {
            amount = withdrawInput.value * 1000000000000000000;
            contract.withdraw(amount, function (err, result) {
                if (err) {
                    console.log('An error occured', err);
                    loadAccount();
                } else {
                    console.log('You have successfully withdrawn ' + amount / 1000000000000000000 +
                                ' ether to your wallet.');
                    loadAccount();
                };
            });
        };

But I am completely lost on how to set the transaction value to the amount set in the input.
Oh, if it helps the HTML element ID's important for the deposit function are "depositInput" (to set transaction value) and "depositButton" (To call the function).

Best Answer

You can specify the deposit value like this:

contract.deposit({'value': put_here_deposit_inputvalue_in_wei}, function (err, result){ }

Hope this help

Related Topic