HEllo Archway

2024-03-04
2021-11-07

This is a review of the basic operations for developing DApp on the Archway Network, a blockchain that uses the Cosmos SDK.

Environment

Host

  • OS: Windows 10 Home
  • CPU: Intel Core i7-7700 4C/8T
  • RAM: 32GB

Guest

(VirtualBox)

  • OS: Ubuntu 20.04
  • CPU: 1 Core
  • RAM: 16GB

Control the node version with nodenv

terminal

$ node -v
v14.15.0
$ npm -v
8.1.2
$ cargo version
cargo 1.55.0 (32da73ab1 2021-08-23)

Installation

Cargo

Also install the package to be able to use the cargo generate command.

terminal

$ cargo install cargo-generate --features vendored-openssl
$ cargo generate --version
cargo-generate 0.10.3

Simply set up the Rust environment on your Mac as shown below.

https://blog.mktia.com/rust-on-mac-m1

Archwayd

Install the Archway localnet. You can implement it from Docker Hub. If you check to see if the image exists, run the docker images command.

terminal

$ docker pull drewstaylor/archwayd:latest

Set up the alias to easily run the command.

.zshrc

alias archwayd="docker run drewstaylor/archwayd:latest archwayd"

Archway Developer CLI

terminal

$ npm i -g @archwayhq/cli

参考:Installation | Archway Network

Create the DApp

Use a template to create a template. If you skip this step, you will not be able to configure the network settings.

terminal

$ archway new
Creating new Archway dApp...

Configure environment (Y/N default: N)?: y

1. Testnet
2. Localhost
3. Mainnet

Select environment type (1-3 default: 1): 1

1. Constantine [stable]
2. Titus [nightly]

Select a testnet to use (1-2 default: 1): 1
Use Docker to run "archwayd" daemon (Y/N default: N)?: y
Use starter template? (Y/N default: N): y
1. Increment [https://github.com/drewstaylor/archway-template/blob/main/README.md]
Select starter template (1-1): 1
Name of project ("My Project") My Project

...

✨  Done! New project created path/to/my-project

Setup

Network

terminal

$ archway network
Printing network settings...


1. Testnet*
2. Localhost
3. Mainnet


Using:  Constantine Testnet
RPC:    https://rpc.constantine-1.archway.tech:443
Faucet: https://faucet.constantine-1.archway.tech


Migrate to another network? (Y/N default: N): 
Ok!

The configuration is set by referring to config.json in the DApp directory. If you try to run without this configuration file, an error will occur and you will not be able to proceed.

Error locating dApp config at path path/to/config.json. Please run this command from the root folder of an Archway project.

Account

terminal

$ archway accounts -add mywallet

Error adding wallet mywallet to keychain Error: spawn archwayd ENOENT

The above error occurs when the path for the first argument of child_process.spawn is not set properly.

The package specifies archwayd as the first argument, but if you are using a Docker image, the path will not work on the PC unless the above alias is set.

The same error occurs if you try to create an account before setting up the network.

Faucet

When Faucet is executed, you will be asked to enter the address.

terminal

$ archway faucet
Enter an address to receive Testnet funds (e.g. "archway1x35egm8883wzg2zwqkvcjp0j4g25p4hed4yjuv"; Or, hit <enter> to list accounts): archway1youraddress
Requesting faucet funds to account archway1youraddress...

{ transfers: [ { coin: '10000000uconst', status: 'ok' } ] }
Requested funds to archway1youraddress on network constantine-1 using faucet https://faucet.constantine-1.archway.tech 
Success! REQUEST SENT

Check the balancess of the account.

terminal

$ archwayd q bank balances archway1youraddress $ARCHWAY_NODE 
balances:
- amount: "10000000"
  denom: uconst
pagination: {}

Premission

By default, root privileges are required, so I changed the owner.

terminal

$ sudo chown <username>:<groupname> /var/tmp/.archwayd

If not set, the following error will occur during deployment.

Cannot copy "my_project.wasm" to "/var/tmp/.archwayd"

Deploy

Running the deploy command with the --dryrun option will compile the contract in the same way as cargo wasm.

terminal

$ archway deploy --dryrun

Without this option, optimized compile it using cosmwasm/rust-optimizer container and deploy to archway network. The arguments in config.json are used when compiling and deploying.

Deploy the default contract

Specify the required message to run the command with --args.

terminal

$ archway deploy --args '{"count":0}'
Building optimized wasm binary...

...

Ok!

Uploading optimized executable to constantine-1...

Send tx from which wallet in your keychain? (e.g. "main" or crtl+c to quit): mywallet
Enter keyring passphrase:
gas estimate: 1020770

confirm transaction before signing and broadcasting [y/N]:y

...

Downloading build artifact from constantine-1 and saving as "download.wasm"...

Successfully updated config file: path/to/my-project/config.json

Downloading wasm code to download.wasm
Integrity check Ok!

Deploying an instance of my-project to constantine-1...

Send tx from which wallet in your keychain? (e.g. "main" or crtl+c to quit): mywallet
Label this deployment? (e.g. "my deployment label", default: my-project 0.0.1): 

Warning: Rewards address in developer config currently unset.

Enter an address to receive developer rewards (e.g. "archway1x35egm8883wzg2zwqkvcjp0j4g25p4hed4yjuv"): archway1youraddress
Enter keyring passphrase:
gas estimate: 169967

...

Successfully updated config file: path/to/my-project/config.json

Deploy an NFT contract

terminal

$ git clone https://github.com/CosmWasm/cw-nfts.git
$ cd ./cw-nfts
$ git checkout v0.9.2

Since the deployment method is different from that of CosmWasm, we will deploy by overwriting the template. Remove and copy the existing examples, schema and src directories.

terminal

$ cd path/to/my-project
$ rm -rf ./<dir>
$ cp -r path/to/cw-mfts/contracts/cw721-base/<dir>

Add the required packages.

Cargo.toml

[dependencies]
cw0 = "0.9.1"
cw2 = "0.9.1"
cw721 = "0.9.2"

Since it has been compiled and verified that it completes normally, it is deployed to the constantine-1 testnet.

terminal

$ archway deploy --args '{"name":"<token name>","symbol":"<token symbol>","minter":"<minter's address>"}'

Interaction

Execute

Mint an NFT.

terminal

$ archway tx --args '{"mint":{"token_id":"1","owner":"archway1youraddress","token_uri":"https://example.com/nft/1"}}'

Query

Query the number of NFTs.

terminal

$ archway query contract-state smart --args '{"num_tokens":{}}'
Attempting query...

{"data":{"count":1}}

Ok!

Check the detail of the NFT with token_id.

terminal

$ archway query contract-state smart --args '{"nft_info":{"token_id":"1"}}'
Attempting query...

{"data":{"token_uri":"https://example.com/nft/1","extension":null}}

Ok!

Conclusion

When deploying with Archway CLI, the contract is compiled with the optimizer using a Docker container and instantiated. To run the contract and query, tx and query commands are provided.

While most projects using the Cosmos SDK integrate the CLI, Archway provides a separate CLI. Arguments are managed in config.js, eliminating the need to type in a variety of commands.

Reference