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 npmnpm install && npm install @stakeordie/griptape.js# With yarnyarn && 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.
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.
exportdefault {// ...data//...mounted methods:{// ... more methodsasynccreateViewingKey() {this.loading =true;try {// Execute `create_viewing_key` message on sscrt contract.constresult=awaitsscrt.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.constcurrentKey=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!
exportdefault {// ...data//...mounted methods:{// ... more methodsasyncgetBalance() {// Get the viewing key from the manager.constkey=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 } } =awaitsscrt.getBalance();constbalance=coinConvert(amount,'6','human');this.balance = balance; } }}