CosmJs
Step 1: Setting up your Development Environment
Before you start, make sure you have Node.js and npm (Node Package Manager) installed on your system. You can verify the installation by running the following commands:
Next, create a new Node.js project by running:
Install the necessary dependencies:
npm install @cosmjs/cosmwasm-stargate @cosmjs/proto-signing
Step 2: Connecting to Furya with CosmJS
In this section, we will create a script that connects to the Furya network using CosmJs.
import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { GasPrice } from "@cosmjs/stargate";
const rpc = 'https://rpc-mainnet.furya.xyz';
const mnemonic = 'your mnemonic here';
const prefix = 'furya';
const start = async () => {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix });
const [firstAccount] = await wallet.getAccounts();
const walletAddress = firstAccount.address;
const signingClient = await SigningCosmWasmClient.connectWithSigner(rpc, wallet, {
gasPrice: GasPrice.fromString("0.1uosmo"),
});
console.log(
"Connected to Furya, chain id:",
await signingClient.getChainId(),
", height:",
await signingClient.getHeight()
);
};
start();
Step 3: Querying Data
Now we will create a script to query smart contract data from the Furya network.
import { CosmWasmClient } from "@cosmjs/cosmwasm-stargate";
const rpc = 'https://rpc-furya.keplr.app';
const contractAddress = 'furya-contract-address-here';
const start = async () => {
try {
const client = new CosmWasmClient(rpc);
const contractQuery = await client.queryContractSmart(contractAddress, {
"custom_query": {
}
});
console.log('Contract Query Result:', contractQuery);
} catch (err) {
console.error(err);
}
};
start();
Step 4: Sign and Broadcast Transactions
Lastly, let’s create a script to sign broadcast transactions to the Furya network.
import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate";
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { GasPrice } from "@cosmjs/stargate";
const rpc = 'https://rpc-furya.keplr.app';
const mnemonic = 'your mnemonic here';
const contractAddress = 'furya-contract-address-here';
const prefix = 'furya';
const start = async () => {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix });
const [firstAccount] = await wallet.getAccounts();
const walletAddress = firstAccount.address;
const signingClient = await SigningCosmWasmClient.connectWithSigner(rpc, wallet, {
gasPrice: GasPrice.fromString("0.1uosmo"),
});
const msg = {
};
const result = await signingClient.execute(
walletAddress,
contractAddress,
msg,
"auto",
"",
[{ denom: "ufury", amount: "1000000" }]
);
console.log(JSON.stringify(result));
};
start();
Remember to replace your mnemonic here
and furya-contract-address-here
with actual values.
Step 5: IBC Transactions
In this step, we will create a script to facilitate Inter-Blockchain Communication (IBC) transactions, which allow for the transfer of tokens between different blockchains within the Cosmos ecosystem. Create a file named ibc-transaction.js and add the following generalized code:
import {
CosmWasmClient,
SigningCosmWasmClient,
} from "@cosmjs/cosmwasm-stargate";
import { coins, DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { GasPrice } from "@cosmjs/stargate";
import { SigningStargateClient } from "@cosmjs/stargate";
const rpc = 'YOUR_RPC_URL_HERE';
const mnemonic = 'YOUR_MNEMONIC_HERE';
const prefix = 'YOUR_CHAIN_PREFIX_HERE';
const start = async () => {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, {
prefix: prefix
});
const [firstAccount] = await wallet.getAccounts();
const walletAddress = firstAccount.address;
console.log('Wallet Address:', walletAddress);
const signingClient = await SigningStargateClient.connectWithSigner(
rpc,
wallet,
{ gasPrice: GasPrice.fromString("0.1YOUR_GAS_TOKEN_HERE") }
);
const recipient = 'RECIPIENT_ADDRESS_HERE';
const amount = {
denom: "YOUR_TOKEN_DENOMINATION_HERE",
amount: "YOUR_TOKEN_AMOUNT_HERE",
};
const timeoutTimestamp = Math.floor(Date.now() / 1000) + 60 * 60;
const tx = await signingClient.sendIbcTokens(
walletAddress,
recipient,
amount,
'YOUR_PORT_HERE',
'YOUR_CHANNEL_HERE',
undefined,
timeoutTimestamp,
"auto",
""
);
console.log(JSON.stringify(tx));
};
start();