Learn how to connect Secret Network contracts to a frontend.
SecretJS
Written in TypeScript and provided with type definitions.
Provides simple abstractions over core data structures.
Supports every possible message and transaction type.
Exposes every possible query type.
Handles input/output encryption/decryption for Secret Contracts.
Works in Node.js, modern web browsers and React Native.
SecretJS Templates
Connecting to Secret Network
Creating account using SecretJS
Query Secret Network
Submitting transactions
Using contracts
Wallets
SNIP-20 tokens
Consuming websocket contract events
Fullstack dApp tutorial
Getting Started with SecretJS
Learn how to install and use SecretJS.
Overview
In this tutorial you will learn how to use Secret.js to connect a web3 wallet, upload a contract, instantiate a contract, execute a contract, and query a contract, amongst other use cases! For the complete Secret.js docs, see here.
All of the following examples use the pulsar-3 testnet with LCD endpoint https://pulsar.lcd.secretnodes.com.
Public LCD endpoints can be found here for both mainnet and testnet.
Installation
Create a package.json file:
npm init -y
Then install secretJS:
npm install secretjs
Set up Secret Network Client
import { SecretNetworkClient, Wallet } from "secretjs";
const wallet = new Wallet("Your mnemonic words go here");
const secretjs = new SecretNetworkClient({
chainId: "pulsar-3",
url: "https://pulsar.lcd.secretnodes.com",
wallet: wallet,
walletAddress: wallet.address,
});
import { SecretNetworkClient, Wallet } from "secretjs";
import * as fs from "fs";
const wallet = new Wallet("Your mnemonic words go here");
const contract_wasm = fs.readFileSync("../contract/contract.wasm");
const secretjs = new SecretNetworkClient({
chainId: "pulsar-3",
url: "https://pulsar.lcd.secretnodes.com",
wallet: wallet,
walletAddress: wallet.address,
});
// Add your contract codeId here
let codeId = ""
// Add your contractCodeHash here
let contractCodeHash = ""
let instantiate_contract = async () => {
//instantiate message is empty in this example. If your contract needs to be instantiated with additional variables, be sure to include them.
const initMsg = {};
let tx = await secretjs.tx.compute.instantiateContract(
{
code_id: codeId,
sender: wallet.address,
code_hash: contractCodeHash,
init_msg: initMsg,
label: "Demo" + Math.ceil(Math.random() * 10000),
},
{
gasLimit: 400_000,
}
);
//Find the contract_address in the logs
const contractAddress = tx.arrayLog.find(
(log) => log.type === "message" && log.key === "contract_address"
).value;
console.log(contractAddress);
};
instantiate_contract();
Loading...
Loading...
Loading...
Contract Migration
Learn how to do contract migration with SecretJS.
Secret Network Client Setup
import { SecretNetworkClient, Wallet } from "secretjs";
const wallet = new Wallet("Your mnemonic words go here");
const secretjs = new SecretNetworkClient({
chainId: "pulsar-3",
url: "https://pulsar.lcd.secretnodes.com",
wallet: wallet,
walletAddress: wallet.address,
});
let permit = await secretjs.utils.accessControl.permit.sign(
accounts[0].address,
"secret-2",
"test",
["abcdef"],
["owner", "balance"],
false
);
// can also use the variant "verifyNoExcept"
let result = secretjs.utils.accessControl.permit.verify(
permit,
accounts[0].address,
"abcdef",
["owner"],
);
An allowance in the context of the Fee Grant module is a specified amount of tokens that a granter permits a grantee to use for paying transaction fees. It should be noted that there can be only one existing fee grant allowed for a grantee and granter and self grants are not allowed.
Granter and grantee
In the allowance mechanism, there are two key players:
The Granter: The granter is the account that provides the allowance. This account will be debited whenever the grantee pays transaction fees using the allowance.
The Grantee: The grantee is the account that receives the allowance. This account can use the allowance to pay for transaction fees.
Types of allowances
There are three allowance types that can be granted:
Basic Fee Allowance: This is a simple allowance model where a granter can specify a maximum limit up to which a grantee can use tokens to pay transaction fees. The allowance can also have an expiration date after which it can no longer be used.
Periodic Fee Allowance: This allowance model is more complex and versatile. It allows the granter to set a periodic reset of the allowance, creating a recurring "budget" for the grantee. This could be used to create a daily, weekly, or monthly allowance, for example.
Allowed Msg Allowance: Creates allowance only for specified message types.
Granting and revoking allowances
Granting an allowance involves the granter sending a special type of transaction that includes information about the grantee and the details of the allowance. Once the transaction is included in a block, the allowance is active and can be used by the grantee.
Revoking an allowance is also a simple process. The granter sends a revoke transaction that includes the address of the grantee. Once this transaction is processed, the grantee can no longer use the allowance to pay fees.
Handling allowance expiry and insufficient allowance
It's important to note that allowances may have an expiry date set by the granter. Once an allowance has expired, it can no longer be used to pay for transaction fees. Grantees must be aware of their allowance expiry dates and ensure that they use their allowances before they expire.
Similarly, if an allowance is exhausted (i.e., the fees for a transaction exceed the remaining allowance), the transaction will not be processed.