DubheSuiTutorialsYour Frist Sui DappWriting System Logic and Publish

Writing-System-Logic-And-Publish

contracts/counter
β”œβ”€β”€ Move.toml
└── sources
    β”œβ”€β”€ codegen
    β”‚   β”œβ”€β”€ errors
    β”‚   β”‚   └── counter_error_invalid_increment.move
    β”‚   β”œβ”€β”€ events
    β”‚   β”‚   └── counter_event_increment.move
    β”‚   └── schemas
    β”‚       β”œβ”€β”€ counter.move
    β”‚       └── default
    β”‚           └── dapp
    β”‚               β”œβ”€β”€ metadata.move
    β”‚               β”œβ”€β”€ schema.move
    β”‚               └── system.move
    β”œβ”€β”€ scripts
    β”‚   β”œβ”€β”€ deploy_hook.move
    β”‚   └── migrate.move
    β”œβ”€β”€ systems
    β”‚
    └── tests
        β”œβ”€β”€ counter.move
        └── init.move

To begin, open a terminal or console at the location you plan to store your dapp. Use the pnpm create dubhe command to create an sui dapp by dubhe with the name 101:

touch contracts/counter/sources/systems/counter.move

After imports, we can check counter schema in the counter_schema.move file.

  // Copyright (c) Obelisk Labs, Inc.
  // SPDX-License-Identifier: Apache-2.0
  #[allow(unused_use)]
 
  /* Autogenerated file. Do not edit manually. */
 
  module counter::counter_schema {
  use std::ascii::String;
  use std::ascii::string;
  use sui::package::UpgradeCap;
  use std::type_name;
  use dubhe::storage_migration;
  use dubhe::storage_value::{Self, StorageValue};
  use dubhe::storage_map::{Self, StorageMap};
  use dubhe::storage_double_map::{Self, StorageDoubleMap};
  use sui::dynamic_field as df;
  use sui::sui::SUI;
  use sui::coin::Coin;
  use sui::balance::Balance;
 
  public struct Counter has key, store {
    id: UID,
  }
 
  public fun borrow_value(self: &Counter): &StorageValue<u32> {
    storage_migration::borrow_field(&self.id, b"value")
  }
 
  public(package) fun borrow_mut_value(self: &mut Counter): &mut StorageValue<u32> {
    storage_migration::borrow_mut_field(&mut self.id, b"value")
  }
 
  public(package) fun create(ctx: &mut TxContext): Counter {
    let mut id = object::new(ctx);
    storage_migration::add_field<StorageValue<u32>>(&mut id, b"value", storage_value::new());
    Counter { id }
  }
 
  public fun migrate(_counter: &mut Counter, _cap: &UpgradeCap) {}
 
  // ======================================== View Functions ========================================
 
  public fun get_value(self: &Counter): &u32 {
    self.borrow_value().borrow()
  }
 
  // =========================================================================================================
}
  • Part 1: Imports - Bring the counter schema that Dubhe CLI automatically generated for us in the previous section into the program space
module counter::counter_system {
    use counter::counter_schema::Counter;
}
  • Part 2: Data Change Method Definition - In the example, we write a counter for the simplest application, and we implement a method to increase it.
module counter::counter_system {
    use counter::counter_schema::Counter;
 
    public entry fun inc(counter: &mut Counter) {
        counter.borrow_mut_value().mutate!(|value| *value = *value + 1);
    }
}
  • Part 3: Data View method - Define a method to query the current value of the counter.
module counter::counter_system {
    use counter::counter_schema::Counter;
 
    public entry fun inc(counter: &mut Counter) {
        counter.borrow_mut_value().mutate!(|value| *value = *value + 1);
    }
 
    public fun get(counter: &Counter): u32 {
        counter.borrow_value().get()
    }
}

Build With Publish

using simple one command:

pnpm run setup:testnet

deploy

if show the error like this:

Error: Account balance is 0, need to get testnet coins

you can get testnet coins from the faucet to make sure you account has enough gas token to deploy your dapp.

pnpm run faucet testnet

the correct output should be like this: faucet

or follow the steps:

  • Part 1: account generate
pnpm run account:gen
  • Part 2: account balance check
pnpm run check-balance

if show the error like this:

Error: Account balance is 0, need to get testnet coins

you can get testnet coins from the faucet to make sure you account has enough gas token to deploy your dapp.

  • Part 3: faucet gas token from testnet
pnpm run faucet testnet
  • Part 4: deploy on testnet
pnpm run deploy:testnet