- Docs
- Developers
- Transaction & query execution
Transaction and query execution
The following examples demonstrate the execution of simple queries and transactions. Each example assumes that you have deployed the increment contract on the Constantine testnet. To learn how to deploy the increment contract, see Deploying your contract on chain.
You will also need to install the arch3.js and axios packages:
npm install --save @archwayhq/arch3.jsnpm i axios
Querying
A query action is used to retrieve information from the blockchain without modifying the state of the chain. Since it is a read-only operation, queries do not require transaction fees.
Querying a contract (using arch3.js)
mainnet
testnet
import { ArchwayClient } from '@archwayhq/arch3.js';const client = await ArchwayClient.connect('https://rpc.mainnet.archway.io');const contractAddress = 'archway1ce97k929shkfzp633edt34hhv3uaqlkgsu3j4xqwjlg2fmg8y5hsw4lewj';const msg = { get_count: {},};const { count } = await client.queryContractSmart( contractAddress, msg);console.log("Counter: ", count);
Querying a contract (using a HTTP client)
The following request queries a smart contract using the Axios HTTP client.
mainnet
testnet
const axios = require('axios');const contractAddress = 'archway1ce97k929shkfzp633edt34hhv3uaqlkgsu3j4xqwjlg2fmg8y5hsw4lewj'; // replace with your contract's addressconst query = { "custom_query": { "data": "" } }; // replace with your queryaxios.get(`https://api.mainnet.archway.io/cosmwasm/wasm/v1/contract/${contractAddress}/smart`, { params: { query: query, },}).then(function (response) { console.log(response.data);}).catch(function (error) { console.log(error);});
You should see something similar to the following:
{ "address": "archway1ce97k929shkfzp633edt34hhv3uaqlkgsu3j4xqwjlg2fmg8y5hsw4lewj", "contract_info": { "code_id": "64", "creator": "archway1e2ntjy39x4gqn3tqu09ztjpjp4hf9q6u47w30u", "admin": "archway1hlsd2tgjxalap5gslxz4g4t0f0yr9nwne98uyu", "label": "testdev", "created": null, "ibc_port_id": "", "extension": null }}
Querying core protocol (using arch3.js)
The following request retrieves a list of currently active validators in the Archway network using the ArchwayClient from arch3.js.
mainnet
testnet
import { ArchwayClient, SigningArchwayClient } from '@archwayhq/arch3.js';const signingClient = await ArchwayClient.connect('https://rpc.mainnet.archway.io');const validators = await signingClient.tmClient.validatorsAll();console.log(validators);
Querying core protocol (using a HTTP client)
The following request retrieves a list of currently active validators in the Archway network using the Axios HTTP client.
mainnet
testnet
import axios from 'axios';let apiEnpoint = 'https://api.mainnet.archway.io';axios.get(apiEnpoint + '/cosmos/staking/v1beta1/validators') .then(response => { console.log(response.data); }) .catch(error => { console.log(error); });
Transaction
A transaction operation is used to create, sign, and broadcast messages that modify the state of the blockchain. A fee is required when executing transactions.
The following transaction increments the counter in the deployed increment contract.
mainnet
testnet
import { SigningArchwayClient } from '@archwayhq/arch3.js';import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing';const network = { chainId: 'archway-1', endpoint: 'https://rpc.mainnet.archway.io', prefix: 'archway',};// This is an incomplete mnemonic used for demo purposes only. Please, never hard code your seed phrases in your codeconst mnemonic = 'add your mnemonic here ...';const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix: network.prefix });const accounts = await wallet.getAccounts();const client = await SigningArchwayClient.connectWithSigner(network.endpoint, wallet);const contractAddress = 'archway1ce97k929shkfzp633edt34hhv3uaqlkgsu3j4xqwjlg2fmg8y5hsw4lewj';const msg = { increment: {},};const { transactionHash } = await client.execute( accounts[0].address, contractAddress, msg, "auto");console.log("Transaction Hash: ", transactionHash);