Are you an LLM? Read llms.txt for a summary of the docs, or llms-full.txt for the full context.
Skip to content

Encoding

Platform-neutral JSON encoding utilities for CosmWasm message fields.

import { Json } from 'schemos'

Json.toBytes

Encode a JSON-serializable object to UTF-8 bytes.

Json.toBytes(value: Record<string, unknown>): Uint8Array

Use for MsgExecuteContract.msg and queryData fields in telescope SDKs:

import { Json } from 'schemos'
import { MsgExecuteContract } from 'osmojs/cosmwasm/wasm/v1/tx'
// Json is re-exported from the main schemos entrypoint
 
const envelope = cw20Msg('transfer', { amount: '1000', recipient: 'osmo1...' })
 
MsgExecuteContract.fromPartial({
  sender,
  contract,
  msg: Json.toBytes(envelope),
  funds: [],
})

Json.toBase64

Encode a JSON-serializable object to a base64 string.

Json.toBase64(value: Record<string, unknown>): string

Use for CosmWasm Binary fields — e.g., the msg field in cw20 send (hook message to receiving contract):

import { Json } from 'schemos'
 
cw20Msg('send', {
  amount: '1000',
  contract: 'osmo1hook...',
  msg: Json.toBase64({ execute_swap: { offer_asset: 'uosmo' } }),
})

Json.fromBytes

Decode UTF-8 bytes to a JSON object.

Json.fromBytes(bytes: Uint8Array): unknown

Use for decoding query responses from telescope RPC:

import { Json } from 'schemos'
 
const res = await rpc.cosmwasm.wasm.v1.smartContractState({ address, queryData })
const result = Json.fromBytes(res.data)

Implementation Notes

  • Uses TextEncoder/TextDecoder for UTF-8 (platform-neutral — works in Node.js and browsers)
  • Base64 encoding uses a lookup table approach adapted from ox (MIT, wevm LLC)
  • No dependency on Node.js Buffer