CLOSED Gitcoin: 3) Issue a Smart Contract Call to the Deployed Smart Contract
nervosnetwork
Layer 2, Rollups, EVM, Ethereum, CKB
This task is capped at maximum 175 submissions; so be in the first 175 valid submissions to get your payout!
# 3. Issue a Smart Contract Call to the Deployed Smart Contract
In this task we will learn how to make function calls to the smart contract that was deployed in the previous task. We will provide some simple example code that uses the popular Web3.js library to do so. This code will allow you to interact with your smart contract which is running on Nervos' [Layer 2](https://github.com/Kuzirashi/gw-gitcoin-instruction/tree/master/src/conceptual-explainers/structure.md#layer-1--layer-2) in a nearly identical way to if it was running on Ethereum.
> Note: You are free to use a different smart contract than is included with the instructions, but your smart contract include at least one function to read a value, and at least one function to write a value.
Your smart contract should operate just like it would on Ethereum, but in actuality, it will be running in an EVM environment provided by [Polyjuice](https://github.com/Kuzirashi/gw-gitcoin-instruction/tree/master/src/conceptual-explainers/frameworks.md#polyjuice). When combined with [Godwoken](https://github.com/Kuzirashi/gw-gitcoin-instruction/tree/master/src/conceptual-explainers/frameworks.md#godwoken), which provides a Layer 2 solution with optimistic rollups, total throughput performance is significantly higher. This means that all users will experience dramatically lower fees, and more reliable transaction confirmation than if they were using Ethereum.
## Task Instructions
> Note: Before starting the tasks, it is recommended that you review the [Task Submission](#task-submission) section so you know what materials you will need to provide to judges to review your task submission.
### Prerequisites
Before you begin on this task you must complete the [first](https://github.com/Kuzirashi/gw-gitcoin-instruction/tree/master/src/tasks/1.create.godwoken.account.md) and [second](https://github.com/Kuzirashi/gw-gitcoin-instruction/tree/master/src/tasks/2.deploy.eth.contract.md) tasks. If you have not completed them, please do so now.
You will need the private key from the **Ethereum** address that you used in the previous task. Make sure this is Ethereum private key for Layer 2, not the private key from your Nervos CKB Layer 1 address. If you do not have this, you can follow the instructions in [this tutorial](https://github.com/Kuzirashi/gw-gitcoin-instruction/tree/master/src/component-tutorials/5.extract.ethereum.private.key.md), to extract your private key from MetaMask.
This task requires the Gitcoin Task Instruction Examples repo (gw-gitcoin-instruction) which was setup in [task 2](https://github.com/Kuzirashi/gw-gitcoin-instruction/tree/master/src/tasks/2.deploy.eth.contract.md#2-clone-and-setup-the-gitcoin-task-instruction-examples). If you do not have this repo available for any reason, please set it up now.
### 1. Prepare the Smart Contract Address and ABI
In order to execute a function call on a smart contract, it must be deployed, and you must have the ABI that was generated when the code was originally compiled. "ABI" stands for Application Binary Interface, and it contains the information required by an application to interface and call functions on the smart contract.
In the [previous task](https://github.com/Kuzirashi/gw-gitcoin-instruction/tree/master/src/tasks/2.deploy.eth.contract.md), you compiled and deployed an Ethereum smart contract. You may be able to reuse that smart contract for this task. If it is no longer available, please revisit the [previous task](https://github.com/Kuzirashi/gw-gitcoin-instruction/tree/master/src/tasks/2.deploy.eth.contract.md) and complete it again.
The example smart contract from the previous task is `SimpleStorage.sol`, and the corresponding ABI value can be found in `2-deploy-contract/build/contracts/SimpleStorage.json` after the contract is compiled. Below is the ABI value which has been extracted from this file.
```json
[
{
"inputs": [],
"stateMutability": "payable",
"type": "constructor"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "x",
"type": "uint256"
}
],
"name": "set",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [],
"name": "get",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
]
```
The SimpleStorage contract has also been deployed to Testnet at the address below. You can optionally use this for testing purposes, but the judges will require you to use your own contract to complete this task.
```txt
0xC46e27169824290EcaEf6E14503C1a6DE72d41B0
```
### 2. Prepare and Run the Example Code to Call the Smart Contract
Next we will use the example code to make a function call in your smart contract. Open the file `gw-gitcoin-instruction/src/examples/3-call-contract/index.js` in an editor of your choosing, and find the `readCall()` and `writeCall()` functions.
The function `readCall()` will read a value from the smart contract without a state change. This does not require a transaction because no data is changing. The `writeCall()` function will write a new value to your smart contract. A transaction will be required because state changes can only occur through transactions. This behavior is the same as on the Ethereum chain.
Next, you need to update the values in `index.js` to match your private keys and smart contract.
#### Private Key
The first thing you will need to do is update `index.js` with your Ethereum private key. This private key will be used to make the function calls, and it should be the same Ethereum private key that funds were added to in the previous tasks. Make sure you use your **Ethereum** private key for Layer 2, not your Nervos CKB Layer 1 private key. Replace `` with this value. **Always make sure your private key is prefixed with "0x".**
```js
const ACCOUNT_PRIVATE_KEY = '';
```
#### ABI
Next, add your contract ABI to the script by replacing `` with the ABI value from the JSON file which was generated during compilation.
> Note: The `CONTRACT_ABI` constant is expecting an array with your ABI as index 0. Make sure this is a data structure, just like it is in `SimpleStorage.json`, and does not get input as a string.
```js
const CONTRACT_ABI = []; // Array
```
#### Contract Address
Replace `` with the address of the Ethereum contract you will be making calls to. This value should be a hex string that was returned when the after deploying the contract.
```js
const CONTRACT_ADDRESS = '';
```
#### Replace the Read Function Name
Locate `` within the `readCall()` function. This must be replaced with function name from your contract that is used for reading.
```js
const callResult = await contract.methods.().call({
from: account.address
});
```
#### Replace the Write Function Name
Locate `` within the `writeCall()` function. This must be replaced with function name from your contract that is used for writing.
```js
const callResult = await contract.methods.().call({
from: account.address,
gas: 6000000,
gasPrice: '0'
});
```
#### Run the Script
After all values have been replaced, use the following commands in a console to execute the script.
```sh
cd ~/projects/gw-gitcoin-instruction/src/examples/3-call-contract
node index.js
```
Example Output (click to expand)
```txt
β node index.js
Calling contract...
Read call result: 123
Write call transaction hash: 0x2a706f6cc50d4edc3e078920023872df82e03168992d2ea55308d441ab4933fb
Write call transaction receipt: {
transactionHash: '0x2a706f6cc50d4edc3e078920023872df82e03168992d2ea55308d441ab4933fb',
blockHash: '0xe6287bea27e792e94833219ad3412068d1aebc235c8c8f5621a6db46e0ed1681',
blockNumber: 20941,
transactionIndex: 0,
gasUsed: 20374,
cumulativeGasUsed: 20374,
logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
contractAddress: null,
status: true,
events: {}
}
Read call result: 888
```
If you've seen transaction hash and the transaction receipt then congratulations! You have successfully issued a smart contract write call on Nervos Layer 2.
## Task Submission
As a result of the contract call in the previous step, you should have received `Transaction hash` as part of the command output.
To complete the tasks, add the following materials to a document on your Github and submit for review by the judges (include the link in your Gitcoin submission):
1. A screenshot of the console output immediately after you have successfully issued a smart contract call.
2. The `transaction hash` from the console output (in text format).
3. The `contract address` that you called (in text format).
4. The `ABI` for contract you made a call on (in text format).
## Helpful Links
- [Introduction](https://github.com/Kuzirashi/gw-gitcoin-instruction/blob/master/src/introduction/introduction.md)
- [Task Setup and Requirements](https://github.com/Kuzirashi/gw-gitcoin-instruction/tree/master/src/task-setup-and-requirements)
- [Discord](https://discord.com/invite/AqGTUE9)
- [Broaden the Spectrum](https://gitcoin.co/hackathon/nervos/onboard)
- [Bounties List](https://gitcoin.co/hackathon/nervos/)
- [Content Directory](https://github.com/Kuzirashi/gw-gitcoin-instruction)