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();