Building on top of Ethereum has never been easier.Frameworks such as the ever popular and make it very easy for developers to quickly deploy contracts and interact with them. Truffle-suite Embark These frameworks unfortunately is best suited for testing and experimentation. What happen’s if we want to have a server connect to a blockchain, and then have it interact with Smart Contracts? Enter Web3. Web3 is a wrapper for Ethereum’s JSON-RPC API. Despite being an abstraction library, it still offers great flexibility and a large deal of complexity. In this tutorial, we’ll be developing on top of the network. Quorum is , so you’ll find that this guide will work perfectly well with Ethereum as well. I choose Quorum specifically because transactions are near instant, and i don’t need to refill my wallet with test ethers. All transactions are free, so we do not need to get test Ethers. Quorum extremely similar to Ethereum We’ll split this guide into two sections: and Contract Deployment Contract Interaction. Creating your Quorum Network At Chainstack, you can create your own Quorum Node easily. We have a free trial for 14 days, so hop on over to our to get started. console If you don’t want to use Quorum, you can connect to your own Ethereum node via Geth or Parity, but you can deploy your own node on Chainstack as well for free :) Disclaimer (I work for Chainstack) Contract Deployment Let’s prepare a simple Smart Contract called Hello.sol pragma solidity ; contract Hello { text; ; constructor( memory _text) { text = _text; } { text; } { text = _newText; } { ; } } 0.5 .0 string ( ) event received address _received string public function ( ) view ( ) getText public returns memory string return function ( ) changeText memory _newText string public function ( ) payable pay public emit ( ) received msg.sender The contract accepts String argument to deploy it. You can also call to get back the string that you set during deployment. To change the text, call and supply it with your new string :) Hello getText() changeText() Finally, the contract has a payable function called A payable function is a function that can be sent Ethers to, and then the function is executed. We’ll see how all of this is done using . pay(). Web3js We’ll also assume that you know how to compile and get the bytecode and ABI of the contract. An easy way to do so if to use REMIX, and copy the bytecode and ABI that it provides. Let’s start by creating a contract instance: Contract = web3.eth.Contract(abi) const You have to supply the ABI so Web3 knows the specifications of each function in the Contract. Next, deploy the contract. .deploy({ : contractData.bytecode, :[title], }) data arguments The deploy function accepts an object. This object contains two things, the bytecode of the contract (which you obtained via REMIX) and the arguments for the constructor. While this function is called , it actually doesn’t deploy the contract to the blockchain. We’ll have to use to finalize this process: .deploy() .send() .send({ : address, : , gasPrice: , }) from gas 470000000 //9000000 for Ethereum 0 // 3000 for Ethereum (3000 wei) also takes in three key objects: .send() 1. From Address 2. Gas Limit (or just Gas) 3. Gas Price Let’s chain .deploy() and .send() of this together: contract.deploy({ : contractData.bytecode, :[title], }) .send({ : address, : , : , }) data arguments from gas 470000000 gasPrice 0 This will return a promise, which contains the transaction receipt for the deployment of the contract. Hello In summary, to deploy a contract you need to: 1. Get the bytecode and ABI of the contract 2. Create a contract instance 3. Include the bytecode and constructor arguments in . deploy() 4. Send it to the blockchain using .send() Once deployed, the transaction receipt contains the address of the Smart Contract. We’ll need this in the next section. Contract Interaction Contract Interaction is slightly more complicated. The contract has three functions, , and getText() changeText() pay() Let’s get an instance of the smart contract so your javascript file can interact with it. contract = Web3.eth.Contract(ABI,contractAddress) const We’ll have to supply the ABI so web3 knows how to interact with the contract. This time, we supply the contractAddress, so Web3 knows where to send our calls to. With the contract instance, we can now access the function: getText() text = Contract.methods.getText().call({}) const await The important thing here is the object. The contract Object exists as a nested Object in javascript, and the function calls are stored in the object. .methods methods Since ) is a view function, it does not make any changes to the blockchain. Thus, we chain the the function with . also returns the value in the returns() argument of the function in the Smart Contract, and the transaction receipt. getText( .call({}) call() not How about the function? Take a look: changeText() Contract.methods.changeText( ).send({ : accounts, : , : }) 'New Text' from gas 470000 gasPrice 0 For non-view functions, or rather functions which changes the state of the blockchain, we have to chain it with . Recall how we used during contract deployment. This makes sense because deploying the contract is also changing the state of the blockchain. You’ll get the transaction receipt as the response. .send() .send() The final function is a function that accepts ether. Let’s see how we can do this using Web3. pay() Contract.methods.pay().send({ : accounts, : , : gasPrice: }) from gas 470000 value 1000000000000000000 // in WEI, which is equivalent to 1 ether 0 We still chain it with the function, but this time we include the value parameter in the object to be sent. This value has to be in wei, and not Ether. So do your conversions accordingly. send() But wait, what about payable functions that returns a value? How do we return value but also send it some Ether? The answer is to use first, and then . .send() .call() That’s it! You’re now an expert in Web3 :P Don’t hesitate to drop me a message. Explore Chainstack Website: Console: www.chainstack.com console.chainstack.com