Oraichain
  • ORAICHAIN
    • Introduction
    • System Overview
      • AI Layer 1 for Data Economy & blockchain oracle services
      • Layer 2 Rollups and Subnetworks
      • Verifiable and trustless AI Execution
      • Protocol Messages
      • IBC Integration
    • Use Cases
    • Token Economics
  • DEVELOPER GUIDES
    • General
      • Blockchain details
    • CosmWasm Contracts
      • Compile a Contract
      • Deploy a Contract
      • Query a Contract
      • Send tokens to a Contract
      • Manage Contracts using design patterns
      • End-to-end CosmWasm testing with CW-simulate
    • Local testnet chain
    • Wallet
      • OWallet
    • Price Feed
      • CW Oracle Hub
    • VRF 2.0
      • Introduction to Oraichain VRF 2.0
      • Get a Random Value from API
        • Get VRF Value from different networks
        • Contract Addresses and Pricing
        • Validate a Group Signature
      • Security Remarks
      • API Reference
      • Get support from Oraichain team
    • OraiDEX
      • ORAIX Token
      • OBridge
        • OraiBTC
        • TON Bridge
          • TON Blockchain 101
      • Decentralization
    • Indexers
      • SubQuery
    • OraichainEVM
      • Getting started
      • Smart Contracts
        • Oraichain EVM Precompiled Contracts
        • Address
        • Bank
        • Wasm
        • Authz
      • Guides
        • Metamask
        • Remix
        • Foundry
        • Hardhat
        • Tools
        • Oraichain EVM RPC
  • GOVERNANCE
    • Privacy Policy
  • NODES & VALIDATORS
    • Networks
      • Joining Mainnet
        • Build Linux binary from source and become a Sentry Node Operator
        • Become a Validator
      • Joining Testnet
        • Become Testnet Fullnode From Source
        • StateSync Testnet
        • Faucet Testnet
    • Oraichain Tutorials
      • Migrate one Oraichain node to another
      • Cosmovisor
      • Update validator image
      • Tenderduty
      • Grafana
      • Tracking Unvoted Proposals
      • Tmtop
Powered by GitBook
On this page
  • PreRequisite
  • Upload Contract
  • Instantiate

Was this helpful?

Edit on GitHub
  1. DEVELOPER GUIDES
  2. CosmWasm Contracts

Deploy a Contract

PreviousCompile a ContractNextQuery a Contract

Last updated 1 month ago

Was this helpful?

PreRequisite

Make sure you follow the guide first for your project. Once you have a contract compiled in the artifacts/*.wasm directory, you are ready for this guide.

This guide will show you how to deploy and interact with a contract using CLI.

Upload Contract

You need to upload the contract via the CLI to chain. To do this, perform the following

TXFLAGS="--chain-id Oraichain --gas auto --gas-adjustment 1.5 --fees 1000orai --node https://rpc.orai.io"

oraid tx wasm store artifacts/CONRTACT_NAME.wasm \
    --from <KEY> $TXFLAGS -y

This will then return a transaction hash. With this data, you need to query it to get the code id of the contract on chain

oraid q tx 38330E909CD219B80927009DA37FD69D334D19B2AD4EC47456A24E85034F0085 --output json --node https://rpc.orai.io

This will return the data about the transaction, and give us the code id of our contract

{
  "height": "148",
  "txhash": "2A52A8B3201425A534052C1616C088271B90EBAFF3ECF4B9784420EF1D531B6D",
  "codespace": "",
  "code": 0,
  "data": "124E0A262F636F736D7761736D2E7761736D2E76312E4D736753746F7265436F6465526573706F6E7365122408011220795F644FE9F0CC5F5E2C48ADCE0F0C28B76AA8D3BF7DCF0104085A1B767248E1",
  "raw_log": "",
  "logs": [],
  "info": "",
  "gas_wanted": "1488045",
  "gas_used": "993511",
  "tx": {
    "@type": "/cosmos.tx.v1beta1.Tx",
    "body": {
      "messages": [
        {
          "@type": "/cosmwasm.wasm.v1.MsgStoreCode",
          "sender": "orai10vvuu8lgc70rrqdkr03jchtd4yk023gylsr84c",
          "wasm_byte_code": "FU4jZbtUYhyy2XsU2i1+2my2q3bepA0PSasjyNIKAftcBywqHisQMWnC1R8SlSl42jxxnwBcMu9N+a4jLIFkfkVc+RDglRa5LlTWs27naZIVsyRf4wdO1Pq2Kykjp0W6QCIcBRG0fKrPVFxAM3bCRXkOJiu1iodCkmMzuc1J5ESrRLsKQZOt3Oh17YhrQR5RyFB3inIym3OSoSLmZDqmZuu2Tw1Yphsdiw+cQILIJXdKMvwvCMjrXQBtlwcDEfkqGG9YARHySxEZIQ9cIy1xQe9aGEQxH4ReGcFhCcluiODtUPLOY2PL56HeI7k2QOSmkwQlWYKwHu3y0k7OpyPNnyOI/gUi2Y0gaqVvIURXsjEi/G+wuYAPonehX5bhTTJCMsT+TjebOzeSQnUQAQ90Q3hJE/LQso64JVqmB8LNtCIIUADFXC5pPgTQ3QCCkgRc/R4A18kXwwACCITQfOCS3F4v8nTIfyqBhNzrhfWwyQUbgAc2slJdWXiHB8A1MpA/QsBT9ICRyjs8XmgXv7fr/HsipoDXYwqYiilgaZ8CLuxT/k8AAAD//+oTrtn/FQIA",
          "instantiate_permission": null
        }
      ],
      "memo": "",
      "timeout_height": "0",
      "extension_options": [],
      "non_critical_extension_options": []
    },
    "auth_info": {
      "signer_infos": [
        {
          "public_key": {
            "@type": "/cosmos.crypto.secp256k1.PubKey",
            "key": "A6Le3gjpOFSjsX6mBy///SfMrmpKAYuCralBe30yc0jr"
          },
          "mode_info": { "single": { "mode": "SIGN_MODE_DIRECT" } },
          "sequence": "1"
        }
      ],
      "fee": {
        "amount": [{ "denom": "orai", "amount": "200" }],
        "gas_limit": "1488045",
        "payer": "",
        "granter": ""
      },
      "tip": null
    },
    "signatures": [
      "aU8Xty61MvidTNl5zbLY3r1882pAcvMnt4qYluZ3/zEhlHSvfELQszJcFdrNbsMU08IRwLMPQEPUM3aPs1pTUw=="
    ]
  },
  "timestamp": "2025-03-26T08:56:07Z",
  "events": [
    {
      "type": "coin_spent",
      "attributes": [
        {
          "key": "spender",
          "value": "orai10vvuu8lgc70rrqdkr03jchtd4yk023gylsr84c",
          "index": true
        },
        { "key": "amount", "value": "200orai", "index": true }
      ]
    },
    {
      "type": "coin_received",
      "attributes": [
        {
          "key": "receiver",
          "value": "orai17xpfvakm2amg962yls6f84z3kell8c5lr24r2w",
          "index": true
        },
        { "key": "amount", "value": "200orai", "index": true }
      ]
    },
    {
      "type": "transfer",
      "attributes": [
        {
          "key": "recipient",
          "value": "orai17xpfvakm2amg962yls6f84z3kell8c5lr24r2w",
          "index": true
        },
        {
          "key": "sender",
          "value": "orai10vvuu8lgc70rrqdkr03jchtd4yk023gylsr84c",
          "index": true
        },
        { "key": "amount", "value": "200orai", "index": true }
      ]
    },
    {
      "type": "message",
      "attributes": [
        {
          "key": "sender",
          "value": "orai10vvuu8lgc70rrqdkr03jchtd4yk023gylsr84c",
          "index": true
        }
      ]
    },
    {
      "type": "tx",
      "attributes": [{ "key": "fee", "value": "200orai", "index": true }]
    },
    {
      "type": "tx",
      "attributes": [
        {
          "key": "acc_seq",
          "value": "{\u0019�\u001f�Ǟ1��\u001b�,]m�,�E\u0004/1",
          "index": true
        }
      ]
    },
    {
      "type": "tx",
      "attributes": [
        {
          "key": "signature",
          "value": "aU8Xty61MvidTNl5zbLY3r1882pAcvMnt4qYluZ3/zEhlHSvfELQszJcFdrNbsMU08IRwLMPQEPUM3aPs1pTUw==",
          "index": true
        }
      ]
    },
    {
      "type": "message",
      "attributes": [
        {
          "key": "action",
          "value": "/cosmwasm.wasm.v1.MsgStoreCode",
          "index": true
        },
        {
          "key": "sender",
          "value": "orai10vvuu8lgc70rrqdkr03jchtd4yk023gylsr84c",
          "index": true
        },
        { "key": "module", "value": "wasm", "index": true },
        { "key": "msg_index", "value": "0", "index": true }
      ]
    },
    {
      "type": "store_code",
      "attributes": [
        {
          "key": "code_checksum",
          "value": "795f644fe9f0cc5f5e2c48adce0f0c28b76aa8d3bf7dcf0104085a1b767248e1",
          "index": true
        },
        { "key": "code_id", "value": "1", "index": true },
        { "key": "msg_index", "value": "0", "index": true }
      ]
    }
  ]
}

We can see events with type store_code shows the code_id being 1. If you wish the automate this return code in bash to a variable, you can

# ensure jq is installed
UPLOAD_TX_HASH=38330E909CD219B80927009DA37FD69D334D19B2AD4EC47456A24E85034F0085
CODE_ID=$(oraid q tx $UPLOAD_TX_HASH --output json | jq -r '.events[] | select(.type == "store_code") | .attributes[] | select(.key == "code_id") | .value') && echo "Code Id: $CODE_ID"

Instantiate

Ensure you change CODE_ID to match your code id from the store code

# Manual
CODE_ID=1
oraid tx wasm instantiate $CODE_ID  --label "some-contract" $FLAGS -y --admin <your-address-here>
# then query the tranasaction hash as we do above.

# Automated return of the contract address
CODE_ID=1
TX_INIT=$(oraid tx wasm instantiate "$CODE_ID" '{"count":0}' --label "contract" $FLAGS -y --admin <your-address-here> | jq -r '.txhash') && echo $TX_INIT
CONTRACT_ADDR=$($BINARY query tx $TX_INIT --output json | jq -r '.events[] | select(.type == "instantiate") | .attributes[] | select(.key == "_contract_address") | .value') && echo "CONTRACT_ADDR: $CONTRACT_ADDR"

With the code now being up on chain, we can now run logic to setup our own copy of the contract which we control. This will then give us a unique contract address for others to interact with in accordance with the contract logic. This example is from the .

compile a contract
cosmwasm/cw-template