Next, let's take a look at the Toy Example, when we will implement the contract and how we will use it.
The example I picked up is a contract named Greeter,that Hardhat generates for us. Greeter will have a function that we can choose to use when we interact with it. greet function which will return a greeting that has been set and will have setGreeting, when we call it, it must pass the greeting value to set the greeting translator, Now when we set the greeting and then we call With the greet command again, the contract returns a greeting.
We will go back to the vs code page and open file the Greeter.sol, which will be the example contract I mentioned earlier.
Next, let's walk through the basics of deploying this contract and how we can deploy it to our localhost network. Here we will show you the file sample-script.js located in the script folder. This file can be generated by hardhat for us. This is a script that when we run it, the script will lead Greeter to deploy, noting that line 17 is where we set a reference to the Greeter contract, and line 18 is where we deploy this contract. Go to the network and it will tell we that we have finished deploying and that we deploy to any address. Here we can go play. When deploying we can use this script or we want to use our console.
An example of a running script we will use the example to run it in the console using the npx hardhat run command and select the folder we want to run its script here, the deploy.js file. In the script folder will be the npx hardhat run script/deploy.js command. And we'll need to select the network to deploy our contract to, followed by the command –network localhost, which is the network of the hardhat we're running, and we'll get the command we will deploy: npx. hardhat run script/deploy.js –network localhost and press enter
Now that we're done, we'll get back the contract address of the Greeter that we deployed to. Once we've got the contract address of the Greeter, copy it and see if we want to connect the contract without writing code.
After copied the contract address, go back to Laika's web page and click Add a new collection.
Next, click on the three dots after the collection we just created and add a method, then click on the new method we just created.
And let’s take the contract address of the Greeter that we have copied and pasted in the address field.
where the first function we will call is function greet, its name is greet, then we enter a Method Name that greet.
The function greet doesn't need any parameters but it wants a return to be a string, so we don't put parameters, just add return and select string. Here, the greet function we wrote here doesn't need to be entered, we just leave it blank and then click send.
After click send we get a response as a result where we can see that name is an empty string with type as a string and we get a value of hello, Hardhat. This will show that we have successfully talked to the contract.
Next, we will try another function, function setGreeting. Let’s do the same thing as function greet, but rename the method to setGreeting , but the setGreeting function is a function that we need to fire to the contract, so the type of the method we need to set. The value is nonpayable so that we can write a contract.
By function setGreeting is a method that it has to create a transaction, but it's a transaction that we don't have to payable so here we select nonpayable. In this function, we need parameters, just press plus to add parameters and choose type as string and put parameters name as _greeting and enter the value Hola! Hardhat and then click send.
After pressing send a metamask will pop up for us to create the transaction because we have chosen it as nonpayable, then click confirm.
After waiting for a while, we will get a response as the result.
Here, if anyone wants to observe more details, they can go to see where we run local chain
Now, we have changed “Hello hardhat” to “Hola Hardhat”, we can also run the greet function one more time to see if the result has really changed.