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://api.pulsar3.scrttestnet.com.
Public LCD endpoints can be found here for both mainnet and testnet.
Installation
Create a package.json file:
npminit-y
Then install secretJS:
npminstallsecretjs
Set up Secret Network Client
import { SecretNetworkClient, Wallet } from"secretjs";constwallet=newWallet("Your mnemonic words go here");constsecretjs=newSecretNetworkClient({ chainId:"pulsar-3", url:"https://api.pulsar3.scrttestnet.com", wallet: wallet, walletAddress:wallet.address,});
import { SecretNetworkClient, Wallet } from"secretjs";import*as fs from"fs";constwallet=newWallet("Your mnemonic words go here");constcontract_wasm=fs.readFileSync("../contract/contract.wasm");constsecretjs=newSecretNetworkClient({ chainId:"pulsar-3", url:"https://api.pulsar3.scrttestnet.com", wallet: wallet, walletAddress:wallet.address,});// Add your contract codeId herelet codeId =""// Add your contractCodeHash herelet contractCodeHash =""letinstantiate_contract=async () => {//instantiate message is empty in this example. If your contract needs to be instantiated with additional variables, be sure to include them.constinitMsg= {};let tx =awaitsecretjs.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 logsconstcontractAddress=tx.arrayLog.find( (log) =>log.type ==="message"&&log.key ==="contract_address" ).value;console.log(contractAddress);};instantiate_contract();