In this tutorial we are going to build an application, in which you will be able to connect to Keplr, learn to define a contract, also interact with it in a simple way to increment a counter and finally get the value of the counter.
Requirements
For this tutorial you will need to have a React app created. You can find how to do it . Also, install your dependencies and install Griptape:
# With npm
npm install && npm install @stakeordie/griptape.js
# With yarn
yarn && yarn add @stakeordie/griptape.js
Getting started
This tutorial consist of these steps:
Grip your application
Bootstrap the application
Create a contract definition
Build the application
Grip your application
Go to the src/index.js and import gripApp and getKeplrAccountProvider from @stakeordie/griptape.js package.
src/index.js
import {
gripApp,
getKeplrAccountProvider
} from "@stakeordie/griptape.js";
Boostrap the application
Open up src/App.js and add a button to bootstrap the application.
If we go to the src/contracts/counter.js file, you can see that the first thing we need to do is import createContractClient from @stakeordie/griptape.js. Thus begins the definition of our contract.
src/contracts/counter.js
import { createContractClient } from '@stakeordie/griptape.js';
After that, we need to create the contract definition called counterDef the contract definition includes the messages part, which is everything we are going to write in the blockchain and the queries part, basically everything we are going to read from the blockchain.
Finally, we are going to create and export our counterContract using the createContactClient API, which we are going to assign an id that can be the name you want counter in this case, we are also going to assign an address of instantiated contract on the blockchain and don't forget to assign the definition counterDef.
To start building our application, first we need to import the contract that we created a few steps before countercontract from './Contracts/Counter'. Then we need to import Bootstrap and onAccountavailable from "@ stakeordie / griptape.js".
src/App.js
import { counterContract } from './contracts/counter';
import {
bootstrap,
onAccountAvailable
} from "@stakeordie/griptape.js";
Now you can notice that we are using the onAccountAvailable event, it is within a useEffect to know when the user is connected. So, once our app is rendered, it will be asked through the event onAccountAvailable if we are connected. If this is the case we will assign true to the setIsConnected variable.
Now we are going to build the getCount function, which contains an asynchronous request to the contract counterContract in which it specifically requests the getCount query. Once we have the response of this request, we can assign the value to setCount state.
Now we are going to create the incrementCount function that asynchronously makes the incrementCount request to the counterContract contract and returns the result of it.