@apophis-sdk/cosmwasm

CosmWasm module on top of the Cosmos module.

The CosmWasm module serves primarily as a user-friendly and convenient wrapper for a set of predefined messages, allowing developers to interact with smart contracts more efficiently. It exposes a few methods to upload, deploy, and execute smart contracts, as well as the query subgroup implementing the various low-level queries that CosmWasm supports.

Example

import { Cosmos, LocalSigner, mw } from '@apophis-sdk/cosmos';
import { DefaultCosmWasmMiddlewares, CosmWasm } from '@apophis-sdk/cosmwasm';

mw.use(...DefaultCosmWasmMiddlewares);

const network = await Cosmos.getNetworkFromRegistry('neutrontestnet');
const signer = LocalSigner.fromMnemonic('...');

await Cosmos.ws(network).ready();

const response = await CosmWasm.query.smart({
  network,
  'neutron1...', // contract address
  // smart query messages are only JSON-formatted by convention, not by requirement
  // toBinary is a helper to serialize JSON messages
  // it's really just implemented using `TextEncoder`, `JSON`, and a marshaller
  CosmWasm.toBinary({
    config: {},
  }),
});
console.log(response);

await CosmWasm.execute(
  network,
  signer,
  'neutron1...', // contract address
  // same JSON convention here
  CosmWasm.toBinary({
    increment: {},
  }),
  [Cosmos.coin(1_000000n, 'untrn')], // optional native coin "funds" to pass along the call
);

Queries

There are 4 types of queries in Apophis. Three of these correspond directly to low-level queries supported by the blockchain.

  • Smart Queries are the queries a developer defines as part of their smart contract. They are typically JSON-encoded by convention, but a smart contract developer is technically free to choose any other format - though they should prepare for the backlash from the wider community. ;)

  • Raw Queries are extremely low-level and require knowledge of the storage layout of a smart contract. In CosmWasm, state is stored as pairs of binary keys and binary serialized values. As most smart contracts use cw-storage-plus, the format of these keys is typically the same, and Apophis can simplify raw storage lookup.

  • State Queries are related to Raw Queries. They facilitate iterating over the entire low-level storage of a smart contract with pagination. However, as items of lists and maps are stored in the same key-value store without abstraction, it's mainly useful for manually inspecting the state of a smart contract.

  • Contract Info Queries are high-level queries introduced by Apophis. Contract Info is specified as part of CW2, and Apophis retrieves this data through Raw Queries. However, CW2 is optional, so the query may fail.

Outlook

There is still work to be done on the CosmWasm integration. Namely, further abstraction of smart contracts and code generation for your smart contract messages, and AuthZ messages.

Last updated