Injective | TS & dApps Docs
DocumentationInjective TS
  • Overview
  • Getting Started
    • Technical Concepts
    • Application Concepts
      • Calculations
        • Min Price Tick Size
        • Min Quantity Tick Size
      • Networks
      • CosmJs Support
    • Assets
      • Creating Tokens
      • Denom Client (deprecated)
      • Injective Lists
    • Running examples
  • Wallets
    • Getting Started
    • Accounts
    • Wallet Connections
    • Wallet Strategy
    • Offchain (Arbitrary) Data
  • Querying
    • Getting Started
    • Chain
      • Auction
      • Auth
      • Bank
      • Distribution
      • Exchange
      • Governance
      • IBC
      • Mint
      • Insurance Funds
      • Oracle
      • Peggy
      • Permissions
      • Staking
      • Tendermint
      • Wasm
      • WasmX
      • Token Factory
    • Indexer
      • Account
      • Auction
      • Derivatives
      • Explorer
      • Insurance Funds
      • Markets
      • Leaderboard
      • Mito
      • Oracle
      • Portfolio
      • Spot
      • Web3Gw Transactions
      • Streaming
        • Account
        • Auction
        • Derivatives
        • Oracle
        • Portfolio
        • Spot
        • Explorer
    • Ethereum (GraphQL)
  • Transactions
    • Getting Started
    • Cosmos
      • Ledger through Keplr Wallet
    • Ethereum
      • Ethereum Ledger
    • MsgBroadcaster
    • Private Key
    • Web3 Gateway
  • Core Modules (& examples)
    • Getting Started
    • Auction
    • AuthZ
    • Bank
    • Distribution
    • Exchange
    • Feegrant
    • Governance
    • IBC
    • Insurance
    • Peggy
    • Permissions
    • Staking
    • Tokenfactory
    • Wasm
  • Smart Contracts
    • Cosmwasm
      • Injective Name Service
      • Neptune Service
      • CW20 to Bank & Market Order in One Transaction
  • Bridges
    • Getting Started
    • Ethereum
    • IBC
    • Wormhole
  • Building Dapps
    • Getting Started
    • Configuring Nuxt
    • Configuring React
    • dApps Examples
      • Smart Contract
      • DEX
      • Bridge
      • Simple HTML example with Webpack
Powered by GitBook
On this page
  • MsgExecuteContract (Transfer)
  • MsgExecuteContract (funds example)
  • MsgExecuteContractCompat
  1. Core Modules (& examples)

Wasm

PreviousTokenfactoryNextCosmwasm

Last updated 5 months ago

The wasm module is the heart of interacting with the wasm smart contracts deployed on the injective chain, here you can find a list of that are deployed on the Injective chain.

MsgUpdateCode and MsgStoreCode are not supported by Ethereum (ex: Metamask) wallets.

MsgExecuteContract (Transfer)

This message is used to execute contract function, below we will use the transfer message as an example.

import { MsgExecuteContract, MsgBroadcasterWithPk } from '@injectivelabs/sdk-ts'
import { Network } from '@injectivelabs/networks'

const injectiveAddress = 'inj1...'
const recipientAddress = 'inj2...'
const contractAddress = 'cw...'

const msg = MsgExecuteContract.fromJSON({
  contractAddress,
  sender: injectiveAddress,
  exec: {
    action: 'transfer',
    msg: {
      recipient: recipientAddress,
      amount: 100000,
    },
  },
})

const txHash = await new MsgBroadcasterWithPk({
  privateKey,
  network: Network.Mainnet,
}).broadcast({
  msgs: msg,
})

console.log(txHash)

MsgExecuteContract (funds example)

In some scenarios, depending on the smart contract's function we have to transfer tokens to the smart contract, following cosmwasm convention, we use the funds field to transfer tokens to the smart contract from the user's bank module.

Below is an example of how we can send the MsgExecuteContract using an test contract function.

import { MsgExecuteContract, MsgBroadcasterWithPk } from '@injectivelabs/sdk-ts'
import { INJ_DENOM } from '@injectivelabs/utils'
import { Network } from '@injectivelabs/networks'

const injectiveAddress = 'inj1...'
const contractAddress = 'cw...'

const msg = MsgExecuteContract.fromJSON({
  contractAddress,
  sender: injectiveAddress,
  exec: {
    action: 'test',
    funds: [
      {
        denom: 'inj',
        amount: new BigNumberInBase(1).toWei().toFixed(),
      },
    ],
  },
})

const txHash = await new MsgBroadcasterWithPk({
  privateKey,
  network: Network.Mainnet,
}).broadcast({
  msgs: msg,
})

console.log(txHash)

MsgExecuteContractCompat

There are some compatibility issues parsing the funds array and msgs object in the previous example with EIP712. Since MsgExecuteContract can't be properly converted to EIP712 and then signed by Ethereum wallets, we introduced MsgExecuteContractCompat which is fully compatible with EIP712.

Note: MsgExecuteContract and MsgExecuteContractCompat underlying messages are the same. MsgExecuteContractCompat is just EIP712 compatible.

Below is an example of how we can send the MsgExecuteContractCompact using an test contract function.

import {
  MsgBroadcasterWithPk,
  MsgExecuteContractCompat,
} from '@injectivelabs/sdk-ts'
import { INJ_DENOM } from '@injectivelabs/utils'
import { Network } from '@injectivelabs/networks'

const injectiveAddress = 'inj1...'
const contractAddress = 'cw...'

const msg = MsgExecuteContractCompat.fromJSON({
  contractAddress,
  sender: injectiveAddress,
  exec: {
    action: 'test',
    funds: [
      {
        denom: INJ_DENOM,
        amount: new BigNumberInBase(1).toWei().toFixed(),
      },
    ],
  },
})

const txHash = await new MsgBroadcasterWithPk({
  privateKey,
  network: Network.Mainnet,
}).broadcast({
  msgs: msg,
})

console.log(txHash)
smart contracts
CW20 spec