0

I am taking a look at Solidity, because the concept of smart contracts is starting to interest me.

I see how Solidity can be used to code smart contracts. But the language does for example not contain a library to code a user interface.

So that means that Solidity gets it data from an external source?

Let's examine the Open Auction example from the docs.

/// Bid on the auction with the value sent
    /// together with this transaction.
    /// The value will only be refunded if the
    /// auction is not won.
    function bid() external payable {
        // No arguments are necessary, all
        // information is already part of
        // the transaction. The keyword payable
        // is required for the function to
        // be able to receive Ether.

        // Revert the call if the bidding
        // period is over.
        if (block.timestamp > auctionEndTime)
            revert AuctionAlreadyEnded();

        // If the bid is not higher, send the
        // money back (the revert statement
        // will revert all changes in this
        // function execution including
        // it having received the money).
        if (msg.value <= highestBid)
            revert BidNotHighEnough(highestBid);

        if (highestBid != 0) {
            // Sending back the money by simply using
            // highestBidder.send(highestBid) is a security risk
            // because it could execute an untrusted contract.
            // It is always safer to let the recipients
            // withdraw their money themselves.
            pendingReturns[highestBidder] += highestBid;
        }
        highestBidder = msg.sender;
        highestBid = msg.value;
        emit HighestBidIncreased(msg.sender, msg.value);
    }

This function allows the user to do a bid on the auction. But nowhere asks the user to enter the value he wants to bid using a user-input. I only see msg.value. A quick Google search tells me that msg is a global variable which lets me access the request send to the smart contract and msg.value is the content of that request.

My question is: How does one send a message to the smart contract? If I'd deploy the Open Auction smart contract on Ethereum, could I use HTTP-requests to send a message to the contract?

If I had the following HTML interface:

<form>
    <label>Value you want to bid:</label>
    <input type="number" name="value_to_bid" value="">

    <input type="submit" name="submit_bid" value="bid">
</form>

which is simply a user input asking the user for the value he wants to bid on the auction. How would I send the entered value to a Ethereum smart contract (in theory, not asking for code)?

TL;DR: I don't understand how the theory behind how one would communicate with a smart contract.

2 Answers 2

2

Welcome to the space. Solidity does not have any way to interact with it directly rather you interact with it using ABI (application binary interface) which you get when you compile your solidity contract into bytecode (which is deployed to the blockchain). The easiest way to interact with your contract is to use libraries such as Ethersjs and web3js.

The best place to start interacting with your contract is to first use the remix IDE before using these other tools.

4
  • Thanks for showing Web3js! Looks easy to use. Two questions though: 1. I see also Web3j for Java, and Web3.py for Python exists. Does that mean that there is also a way to use 'the pure version' of the protocol to connect to interact with a smart contract. Like in another language you could you use the library or implementation of a API, or call the API using pure HTTP-requests/cURL. Is there a kind of 'cURL' to access smart contracts?
    – O'Niel
    Jan 7, 2022 at 8:51
  • 2. Could I use Web3js on a Node.js server. And make normal HTTP requests to that Node.js server which would then relay the request to Ethereum in the Web3JS form?
    – O'Niel
    Jan 7, 2022 at 8:52
  • 1. There exists no way to interact directly with the protocol you must use these layers of abstractions (the libraries). Never tried before but you can only do so if you run an Ethereum node.
    – kelche
    Jan 7, 2022 at 9:21
  • 2. The libraries are mostly used in nodejs servers to call the public functions from the deployed smart contracts.
    – kelche
    Jan 7, 2022 at 9:24
1

There are a few different frameworks for connecting with a smart contract. One of which (the one that I use) is hardhat/ethers.

It allows you to quite easily connect or deploy smart contracts and refer back to them.

To connect to an already deployed contract:

Auction = await ethers.getContractFactory("Auction");
auction = await Auction.attach(<insert contract address here as a string>);

Now, using "auction", you can check on any public value or access any external/public function on that contract!

This is my first reply to someone else's question, so hope this helps :)

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.