In order to go through this tutorial you'll need to have a React app created. You can find how to do it here. 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 an application.
Bootstrap the application.
Create contract client
Create List tokens component
Get tokens
Mint token
Grip an application
As you may know the first thing that we need to do is Grip our application, in this case our app is in src/main.js. This is how our main.js should look like.
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import {
gripApp,
getKeplrAccountProvider
} from '@stakeordie/griptape.js';
const restUrl = 'https://api.pulsar.griptapejs.com';
const provider = getKeplrAccountProvider();
function runApp() {
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root')
);
}
gripApp(restUrl, provider, runApp);
We assumed you are using Keplr as wallet. Griptape has only support with Keplr at this point. The Griptape team is working looking froward to support as many wallets as the Cosmos Ecosystem requires.
Bootstrap your application
Now that we have your gripped our application we need to bootstrap it. Bootstrap creates a signing client able to encrypt and decrypt transactions. If don't bootstrap the app we won't be able to get the wallet address and execute messages.
Now on, let's move to start working in src/App.js. As our first step we need to import bootstrap api from @stakeordie/griptape.js. Copy the example below.
import { bootstrap } from '@stakeordie/griptape.js';
Right next to it just create a button and add the bootstrap function.
now we need to create a contract client definition in order to execute and query our contract. Let's create a folder contracts and inside this folder a file called minting.js and start working on this file. First we going to import some APIs needed from Griptape.
import {
createContractClient,
snip721Def
} from '@stakeordie/griptape.js';
Then we create a contract client object and export it:
In order to have a clean code we are going to create a dumb component to show a list of tokens. Create a folder components and inside of it create a file called TokenList.js and write something like the following.
Now let's move to App.js and import some APIs and our Token List component.
import React, { useState, useEffect } from "react";
import {
bootstrap,
viewingKeyManager,
onAccountAvailable
} from '@stakeordie/griptape.js';
import { minting } from './contracts/minting';
import TokenList from "./components/TokenList";
And then we are going to create some stores with useState for rendering our info.
var [loading, setLoading] = useState(false);
var [loadingMint, setLoadingMint] = useState(false);
var [loadingTokens, setLoadingTokens] = useState(false);
var [viewingKey, setViewingKey] = useState('');
var [nftList, setNftList] = useState([]);
var [isConnected, setIsConnected] = useState(false);
After that we set up a event handler for when our app has an account available.
Now finally we are going to create a function to query our tokens and same way for create a viewing key, since is neccesary for querying the private state of our contract.
now we want to create a new token, in order to create a new token you need to be a minter for your contract, for this contract we have for example there is no restrictions but you need to consider this for your contracts. with this said let's create a function to mint a new token.
const mint = async () => {
var date = Date.now();
const extension = {
name: `Example ${date}`,
description: "test",
image: 'https://i.picsum.photos/id/586/200/300.jpg?hmac=Ugf94OPRVzdbHxLu5sunf4PTa53u3gDVzdsh5jFCwQE'
}
setLoadingMint(true);
try {
// mintNft receives token_id,owner,public_metadata, ...
await minting.mintNft(null,null,{extension});
} catch (e) {
// ignore for now
} finally {
setLoadingMint(false);
}
}
When minting a new token all fields are optional, in this case we want to set some public metadata so we passed in some null values for token_id and owner,
Finally we should need to show the info and buttons in html: