Initia (Closed Testnet)
  • Reminder
    • ⛔Confidentiality Disclaimer
  • WELCOME TO CLOSED TESTNET
    • Welcome to Closed Testnet
  • About Initia
    • What is the Initia Platform?
      • Architecture
        • Layer 1
        • Layer 2
      • Aligning Initia (coming soon!)
    • InitiaDEX
    • Enshrined Liquidity and Staking
  • Developers
    • Initiad
      • Download Initiad
      • Using Initiad
    • Get INIT token
    • Build your own Minitia
      • Simple Minitia Setup
      • Connect Minitia to L1
        • OPinit Stack
          • OPinit Module
          • OPinit Bots
        • Relayer
          • Fetching Oracle Prices
    • Virtual Machines
      • MoveVM
        • Interact with CLI
      • WasmVM
    • Contracts
      • Move Contracts
      • CosmWasm Contracts
      • EVM Contracts (Coming Soon)
    • Oracles
  • Initia Developer Tutorials
    • 1. Create account
    • 2. Send Move coin
    • 3. Interchain Message
    • 4. Create your own Move coin
    • 5. Create your own Move NFT
    • 6. Build and Publish Contracts
      • Move Module
      • CosmWasm Contract
  • Core Applications
    • Core Applications
      • Wallet
      • Initia App
      • Usernames
      • Initia Scan
  • Node Operators
    • Running Initia Node
      • Prerequisites
      • Oracle
      • Boot an Initia Node
      • Connect to Initia Network
      • Cosmovisor Guide
    • Configuring Initia Validator
  • References and Tools
    • Move Language
    • Rust Language (CosmWasm)
    • Closed Testnet Chain Information
    • Chain Parameters
    • Initia.js
    • API Docs
    • API Docs (minimove)
    • API Docs (miniwasm)
    • Whitelisted Tokens and Pairs
Powered by GitBook
On this page
  • Step 0: clone initia-tutorials
  • Step 1: Build a module
  • Step 2: Publish a module
  • Step 3: Interact with published module
  1. Initia Developer Tutorials
  2. 6. Build and Publish Contracts

Move Module

This tutorial covers building, publishing and interacting with my own Move modules.

Step 0: clone initia-tutorials

In this tutorial we will use read_write module of initia-tutorials repository.

git clone git@github.com:initia-labs/initia-tutorials.git

Step 1: Build a module

Before building a module, module owner address must be modified to your own address. Open ./initia-tutorials/move/read_write/Move.toml and modify your_address. your_address must be a hex address.

How to get HEX address

> initiad keys parse [addr]
bytes: F64D24B10B0CE93CD428DF3AB9228ADB18B93CFE
human: init
import { AccAddress } from '@initia/initia.js';
console.log(AccAddress.toHex('[addr]'));
// 0x7b23641ee96425a5dbe9953bdd949da7f6c5fcb0
[package]
name = "read_write"
version = "0.0.0"

[dependencies]
InitiaStdlib = { git = "https://github.com/initia-labs/initiavm.git", subdir = "crates/../precompile/modules/initia_stdlib", rev = "main" }

[addresses]
std =  "0x1"
your_address = "{insert your hex address here}"

And build module with the CLI or builder.js

> initiad move build --path ./initia-tutorials/move/read_write
import { MoveBuilder } from '@initia/builder.js';

const path =
  '${path_to_initia_tutorials}/move/read_write';

async function buildModule() {
  const builder = new MoveBuilder(path, {});
  await builder.build();
}

buildModule();

Step 2: Publish a module

Let's publish read_write.mv module create in the previous step.

>initiad move deploy \
  --path ./initia-tutorials/move/read_write \
  --upgrade-policy COMPATIBLE \
  --from test-account \
  --gas auto --gas-adjustment 1.5 \
  --gas-prices 0.15uinit \
  --node [rpc-url]:[rpc-port] \
  --chain-id [chain-id]
import { LCDClient, MnemonicKey, MsgPublish, Wallet } from '@initia/initia.js';
import { MoveBuilder } from '@initia/builder.js';
import * as fs from 'fs';

const path =
  '${path_to_initia_tutorials}/move/read_write';

async function publishModule() {
  const lcd = new LCDClient('https://lcd.mahalo-1.initia.xyz', {
    gasPrices: '0.15uinit',
    gasAdjustment: '1.5',
  });

  const builder = new MoveBuilder(path, {});
  await builder.build();

  const key = new MnemonicKey({
    mnemonic:
      'beauty sniff protect ...',
  });
  const wallet = new Wallet(lcd, key);

  const codeBytes = fs.readFileSync(
    `${path}/build/read_write/bytecode_modules/read_write.mv`
  );
  // const codeBytes = await builder.get('read_write'); // or you can get with MoveBuilder

  const msgs = [
    new MsgPublish(key.accAddress, [codeBytes.toString('base64')], 1),
  ];

  // sign tx
  const signedTx = await wallet.createAndSignTx({ msgs });
  // send(broadcast) tx
  lcd.tx.broadcastSync(signedTx).then(res => console.log(res));
  // {
  //   height: 0,
  //   txhash: '162AA29DE237BD060EFEFFA862DBD07ECD1C562EBFDD965AD6C34DF856B53DC2',
  //   raw_log: '[]'
  // }
}

publishModule();

About upgrade policy

Policy
Description

ARBITRARY

Whether unconditional code upgrade with no compatibility check is allowed. This publication mode should only be used for modules which aren't shared with user others. The developer is responsible for not breaking memory layout of any resources he already stored on chain.

COMPATIBLE

Whether a compatibility check should be performed for upgrades. The check only passes if a new module has (a) the same public functions (b) for existing resources, no layout change.

IMMUTABLE

Whether the modules in the package are immutable and cannot be upgraded.


Step 3: Interact with published module

Let's interact with the published module.

> initiad query move view [addr] read_write read \
  --node [rpc-url]:[rpc-port]

data: '"initial content"'

> initiad tx move execute [addr] read_write write \
  --args "string:new_string" \
  --from [key-name] \
  --gas auto --gas-adjustment 1.5 --gas-prices 0.15uinit \
  --node [rpc-url]:[rpc-port] --chain-id [chain-id]

> initiad query move view [addr] read_write read \
  --node [rpc-url]:[rpc-port]

data: '"new_string"'
import {
  bcs,
  LCDClient,
  MnemonicKey,
  MsgExecute,
  Wallet,
} from '@initia/initia.js';

async function setNewContent() {
  const lcd = new LCDClient('[rest-url]', {
    gasPrices: '0.15uinit',
    gasAdjustment: '1.5',
  });

  const key = new MnemonicKey({
    mnemonic: 'beauty sniff protect ...',
  });
  const wallet = new Wallet(lcd, key);

  await lcd.move
    .viewFunction(key.accAddress, 'read_write', 'read')
    .then(res => console.log('before write:', res));

  const msgs = [
    new MsgExecute(
      key.accAddress,
      key.accAddress,
      'read_write',
      'write',
      [],
      [bcs.string().serialize('new_string').toBase64()]
    ),
  ];

  // sign tx
  const signedTx = await wallet.createAndSignTx({ msgs });
  // send(broadcast) tx
  await lcd.tx.broadcastSync(signedTx).then(res => console.log(res));
  // {
  //   height: 0,
  //   txhash: '162AA29DE237BD060EFEFFA862DBD07ECD1C562EBFDD965AD6C34DF856B53DC2',
  //   raw_log: '[]'
  // }

  await lcd.move
    .viewFunction(key.accAddress, 'read_write', 'read')
    .then(res => console.log('after write:', res));
}

setNewContent();
Previous6. Build and Publish ContractsNextCosmWasm Contract

Last updated 1 year ago