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
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:
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