0

I am trying to set values to the following contract public state variables from a frontend javascript code. But it seems not working.

Contract public state variables:

// Current state of the auction.
address public highestBidder;
uint public highestBid;

In the below Javascript code, I get the values for address and amount from UI and assign to highestBidder & highestBid. But it seems not working. How do I do this?

// Submit bid details, triggered by the "Bid" button
SimpleAuction.prototype.startAuction = function() {
    console.log("inside startAuction");
    var that = this;
    console.log("inside startAuction");
    // Gets form input values
    var address = $("#enter-address").val();
    var amount = $("#enter-amount").val();
    console.log(amount);

    // Validates address using utility function
    if (!isValidAddress(address)) {
        console.log("Invalid address");
        return;
    }
    that.highestBidder = address;
    
     
    // Validate amount using utility function
    if (!isValidAmount(amount)) {
        console.log("Invalid amount");
        return;
    }
    that.highestBid = amount;

This code fixed the issue. Passing address and amount as a transaction object in the bid() function in JS

// Submit bid details, triggered by the "Bid" button
SimpleAuction.prototype.startAuction = function() {
    console.log("inside startAuction");
    var that = this;
    console.log("inside startAuction");
    // Gets form input values
    var address = $("#enter-address").val();
    var amount = $("#enter-amount").val();
    console.log(amount);

    // Validates address using utility function
    if (!isValidAddress(address)) {
        console.log("Invalid address");
        return;
    }
     
    // Validate amount using utility function
    if (!isValidAmount(amount)) {
        console.log("Invalid amount");
        return;
    }
    
    
    // Calls the public `bid` function from the smart contract
    this.instance.bid(
         {  
            from: address,
            value: amount,
            gas: 100000,
            gasPrice: 100000,
            gasLimit: 100000
        },
       
        function(error, txHash) {
            if (error) {
                console.log(error);
            }
            // If success, wait for confirmation of transaction,
            // then clear form values
            else {
                that.waitForReceipt(txHash, function(receipt) {
                    if (receipt.status) {
                        $("#enter-address").val("");
                        $("#enter-amount").val("");
                    } else {
                        console.log("error");
                    }
                });
            }
        }
    );
};

1 Answer 1

0

You're trying to update the state of the contract and in this case it is required that you have to sign transaction. Take a look at web3 sendTransaction method over here. When you take a look at the method you will notice parameter transactionObject include data string which is the function ABI of your smart contract method. You can get function abi by using encodeABI.

Then at your smart contract just by creating a variable the following way address public highestBidder; does not mean that this state variable can be updated. You have to create a setter method or include the setter method logic into another method, example:

function setHighestBidder(address _highestBidder) {
    highestBidder = _highestBidder;
}
3
  • Thanks for the response! What I am trying to do is develop a UI for the Simple Auction contract in Solidity docs - solidity.readthedocs.io/en/v0.6.11/solidity-by-example.html. The contract works fine when I run from remix. The bid() function is payable but does not accept arguments. I am not sure how to pass the bid amount value to the contract from my UI. That's where I am stuck.
    – sijo0703
    Jul 20, 2020 at 16:19
  • Ok, show us your front end connection to the smart contract so we can see where you are stuck at the moment. Jul 21, 2020 at 4:19
  • I was able to fix the issue by passing the transaction object with address and amount in the JS function bid(). Updated the code fixed above. I am still little confused because the smart contract function bid() does not accept arguments and how can I pass a transaction object in the corresponding JS function?
    – sijo0703
    Jul 21, 2020 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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