In this tutorial we show you how to create viewing keys for authenticating a SNIP-20 contract. When you finish this Hello Viewing Keys tutorial you will have a web app connected to pulsar-2 with the ability to query the sSCRT balance using viewing keys, creating viewing keys and connecting the app to Keplr.
In order to go through this tutorial you'll need to have a Vue 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
We highly recommend have done Hello, Contracts and Hello, Griptape before starting this tutorial for higher understanding of Griptape.
Getting started
This tutorial consist of these steps.
Grip an application.
Creating contract definition.
Import necessary Griptape APIs and Contract Definition.
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 { createApp } from 'vue'
import App from './App.vue'
import {
gripApp,
getKeplrAccountProvider
} from '@stakeordie/griptape.js';
const restUrl = 'https://api.pulsar.griptapejs.com';
const provider = getKeplrAccountProvider();
function runApp() {
createApp(App)
.mount('#app')
}
gripApp(restUrl, provider, runApp);
Creating contract definition
Let's create a folder named contracts inside contracts, let's create a file called sscrt.js. You can copy and paste the next command.
mkdir contracts && touch ./contracts/sscrt.js
Importing APIs in src/contracts/sscrt.js
Now let's move and start working in src/contracts/sscrt.js, we need to import some APIs from Griptape.
import {
createContractClient,
snip20Def
} from '@stakeordie/griptape.js';
Brief explanation of Griptape APIs imported.
createContractClient : Help us create an object based on a definition passed in as a parameter.
snip20Def : Is a pre-defined contract definition following Secret Network reference.
Creating contract
Finally we just need to create our contract and export it.
Okay, now that we have Grip our app and created our contract definition, let's import some APIs in src/App.vue.
import {
viewingKeyManager,
coinConvert,
bootstrap,
onAccountAvailable
} from '@stakeordie/griptape.js';
import { sscrt } from './contracts/sscrt';
viewingKeyManager help us create and get viewing key in a very easy way. We also import our contract sscrt we just created in a section before.
Bootstrap app
Before bootstrapping our app, we are going to define what our data object should look like and use one of our events APIs, onAccountAvailable explained in tutorials before (more info in Hello, Events).
viewingKeyManager is one of the APIs created for viewing key management, this has many internal functions such as get,add and set. get receives one parameter the identifier of the contract we want to get the viewing key, could be the contract address like in this case or the contract id. add and set receive two parameters a contract in this case sscrt and a viewing key, we will see a example later.
Now, let's create a simple function to connect the app.
With Griptape creating a permit is very easy, viewingKeyManager is an API that has many functions such as add which receives two parameters, the first is a contract, in this case the one we imported before (sscrt), and the second parameter is a string (viewing key).
We are going to create a simple util function to create a viewing key. like the following.
export default {
// ...data
//...mounted
methods:{
// ... more methods
async createViewingKey() {
this.loading = true;
try {
// Execute `create_viewing_key` message on sscrt contract.
const result = await sscrt.createViewingKey();
// Validate if response is empty.
if (result.isEmpty()) return;
// In case is not empty, parse the result.
const { create_viewing_key: { key } } = result.parse();
// Check if there's already a viewing key.
const currentKey = viewingKeyManager.get(sscrt.at);
// If there is, update the viewing key using the `set`
// function. Otherwise, add it.
if (currentKey) {
viewingKeyManager.set(sscrt, key);
} else {
viewingKeyManager.add(sscrt, key);
}
// Update UI.
this.viewingKey = key;
} catch (e) {
// ignore for now
} finally {
this.loading = false;
}
},
}
}
Get Balance
In order to get the balance of a SNIP-20 you must provide a viewing key and as you see in the example below we don't pass in any viewing key, Griptape already does it internally, like magic!
export default {
// ...data
//...mounted
methods:{
// ... more methods
async getBalance() {
// Get the viewing key from the manager.
const key = viewingKeyManager.get(sscrt.at);
// Do nothing if we don't have a viewing key.
if (!key) return;
// In case we have a viewing key, fetch the balance.
const { balance: { amount } } = await sscrt.getBalance();
const balance = coinConvert(amount, '6', 'human');
this.balance = balance;
}
}
}