“服务提供商”是指为终端用户提供服务、且这些服务涉及某种形式与 Cosmos Hub 交互的实体。更具体地说,本文档重点关注与代币相关的交互。 服务提供商应当作为其终端用户连接区块链的可信接入点。本服务提供商章节不适用于希望提供轻客户端功能的钱包构建者。 本文档介绍:

连接选项

连接到 Cosmos Hub 时,主要需要考虑四种技术:
  • 全节点:与区块链交互。
  • REST 服务器:用于处理 HTTP 调用。
  • REST API:使用 REST 服务器提供的可用端点。
  • GRPC:通过 gRPC 连接到 Cosmos Hub。

运行全节点

什么是全节点?

全节点是与区块链状态保持同步的网络节点。它通过 RESTful API 向其他人提供区块链数据,也可以通过暴露接口提供数据库副本中的数据。全节点会与区块链中的其他节点保持同步,并将状态存储到磁盘上。如果被查询的区块不在本地磁盘中,全节点可以去区块链上找到该查询数据所在的位置。

安装与配置

本节介绍运行并与 Cosmos Hub 全节点交互的步骤。 首先,你需要安装软件。 也可以考虑运行你自己的 Cosmos Hub 全节点。

命令行界面

命令行界面(CLI)是访问 Cosmos Hub 并使用 gaia 的最强大工具。 要使用 CLI,你必须在本地机器上安装最新版 gaia。 将你的版本与最新发布版本进行比较。
gaiad version --long

可用命令

运行 gaiad 命令后,会显示所有可用的 CLI 命令:
gaiad
Stargate Cosmos Hub App

Usage:
  gaiad [command]

Available Commands:

  add-genesis-account Add a genesis account to genesis.json
  collect-gentxs      Collect genesis txs and output a genesis.json file
  debug               Tool for helping with debugging your application
  export              Export state to JSON
  gentx               Generate a genesis tx carrying a self delegation
  help                Help about any command
  init                Initialize private validator, p2p, genesis, and application configuration files
  keys                Manage your application's keys
  migrate             Migrate genesis to a specified target version
  query               Querying subcommands
  start               Run the full node
  status              Query remote node for status
  tendermint          Tendermint subcommands
  testnet             Initialize files for a gaia testnet
  tx                  Transactions subcommands
  unsafe-reset-all    Resets the blockchain database, removes address book files, and resets data/priv_validator_state.json to the genesis state
  validate-genesis    validates the genesis file at the default location or at the location passed as an arg
  version             Print the application binary version information

Flags:
  -h, --help                help for gaiad
      --home string         directory for config and data (default "/Users/tobias/.gaia")
      --log_format string   The logging format (json|plain) (default "plain")
      --log_level string    The logging level (trace|debug|info|warn|error|fatal|panic) (default "info")
      --trace               print out full stack trace on errors

Use "gaiad [command] --help" for more information about a command.
对于显示出的每个命令,你都可以使用 --help 标志获取更多信息。
gaiad query --help
Usage:
  gaiad query [flags]
  gaiad query [command]

Aliases:
  query, q

Available Commands:
  account                  Query for account by address
  auth                     Querying commands for the auth module
  bank                     Querying commands for the bank module
  block                    Get verified data for a the block at given height
  distribution             Querying commands for the distribution module
  evidence                 Query for evidence by hash or for all (paginated) submitted evidence
  gov                      Querying commands for the governance module
  ibc                      Querying commands for the IBC module
  ibc-transfer             IBC fungible token transfer query subcommands
  mint                     Querying commands for the minting module
  params                   Querying commands for the params module
  slashing                 Querying commands for the slashing module
  staking                  Querying commands for the staking module
  tendermint-validator-set Get the full tendermint validator set at given height
  tx                       Query for a transaction by hash in a committed block
  txs                      Query for paginated transactions that match a set of events
  upgrade                  Querying commands for the upgrade module

Flags:
      --chain-id string   The network chain ID
  -h, --help              help for query

Global Flags:
      --home string         directory for config and data (default "/Users/tobias/.gaia")
      --log_format string   The logging format (json|plain) (default "plain")
      --log_level string    The logging level (trace|debug|info|warn|error|fatal|panic) (default "info")
      --trace               print out full stack trace on errors

Use "gaiad query [command] --help" for more information about a command.

远程访问 gaiad

如果你选择远程访问全节点和 gaiad,则需要有一个正在运行的全节点,并且在本地机器上安装 gaia。 gaiad 是一个工具,使你能够与运行在 Cosmos Hub 网络上的节点交互,无论该节点是否由你自己运行。 要在本地机器上设置 gaiad 并连接到现有的全节点,请使用以下命令:
gaiad config <flag> <value>
首先,设置你要连接的全节点地址:
gaiad config node <host>:<port

// example: gaiad config node https://77.87.106.33:26657 (note: this is a placeholder)
如果你是在本地运行自己的全节点,请使用 tcp://localhost:26657 作为地址。 最后,设置你要交互的区块链的 chain-id:
gaiad config chain-id cosmoshub-4
接下来,学习如何使用 CLI 命令与全节点交互。 无论是将其作为远程控制使用,还是在本地机器上运行节点时,你都可以执行这些命令。

创建密钥对

默认密钥类型是 secp256k1 elliptic curve。使用 gaiad keys 命令可以列出密钥并生成新密钥。
gaiad keys add <your_key_name>
系统会要求你为这个密钥对创建一个密码(至少 8 个字符)。随后将返回以下信息:
  • NAME:你的密钥名称
  • TYPE:你的密钥类型,始终为 local。
  • ADDRESS:你的地址,用于接收资金。
  • PUBKEY:你的公钥,对验证者有用。
  • MNEMONIC:24 个单词组成的助记词。请将这组助记词保存在安全的地方。如果你忘记密码,恢复私钥时需要用到这组助记词。助记词会显示在输出的最后。
你可以通过输入以下命令查看所有可用密钥:
gaiad keys list
使用 --recover 标志可以向密钥环中添加一个通过助记词导入的密钥。
gaiad keys add <your_key_name> --recover

检查你的账户

你可以使用 query account 命令查看你的账户。
gaiad query account <YOUR_ADDRESS>
它会显示你的账户类型、账户编号、公钥以及当前账户序列号。
'@type': /cosmos.auth.v1beta1.BaseAccount
account_number: "xxxx"
address: cosmosxxxx
pub_key:
  '@type': /cosmos.crypto.secp256k1.PubKey
  key: xxx
sequence: "x"

检查你的余额

使用以下命令查询账户余额:
gaiad query bank balances <YOUR_ADDRESS>
响应中包含 balances 和 pagination 两个键。 每个 balances 条目都包含持有的 amount,并关联一个 denom 标识符。 常见的 $ATOM 代币使用 uatom 作为 denom 标识,其中 1 uatom 等于 0.000001 ATOM。
balances:
- amount: "12345678"
  denom: uatom
pagination:
  next_key: null
  total: "0"
当你查询一个尚未收到任何代币的账户时,balances 条目会显示为空数组。
balances: []
pagination:
  next_key: null
  total: "0"

使用 CLI 发送代币

使用 CLI 发送代币:
gaiad tx bank send [from_key_or_address] [to_address] [amount] [flags]
参数:
  • <from_key_or_address>:发送账户的密钥名称或地址。
  • <to_address>:接收方地址。
  • <amount>:该参数接受 <value|coinName> 格式,例如 1000000uatom。
标志:
  • --chain-id:该标志允许你指定链的 ID。不同测试网链和主网链使用不同的 ID。
  • --gas-prices:该标志允许你指定为交易支付的 gas 价格。格式例如 0.0025uatom

REST API

REST API 文档列出了所有可用端点,你可以使用它们与你的全节点交互。了解如何在你的全节点上启用 REST API。

监听传入交易

推荐的监听传入交易方式是通过以下 HTTP 端点定期查询区块链: /cosmos/bank/v1beta1/balances/{address}
‘Service Providers’ are defined as entities that provide services for end-users that involve some form of interaction with the Cosmos Hub. More specifically, this document is focused on interactions with tokens. Service Providers are expected to act as trusted points of contact to the blockchain for their end-users. This Service Providers section does not apply to wallet builders that want to provide Light Client functionalities. This document describes:

Connection Options

There are four main technologies to consider to connect to the Cosmos Hub:
  • Full Nodes: Interact with the blockchain.
  • REST Server: Serves for HTTP calls.
  • REST API: Use available endpoints for the REST Server.
  • GRPC: Connect to the Cosmos Hub using gRPC.

Running a Full Node

What is a Full Node?

A Full Node is a network node that syncs up with the state of the blockchain. It provides blockchain data to others by using RESTful APIs, a replica of the database by exposing data with interfaces. A Full Node keeps in syncs with the rest of the blockchain nodes and stores the state on disk. If the full node does not have the queried block on disk the full node can go find the blockchain where the queried data lives.

Installation and Configuration

This section describes the steps to run and interact with a full node for the Cosmos Hub. First, you need to install the software. Consider running your own Cosmos Hub Full Node.

Command-Line Interface

The command-line interface (CLI) is the most powerful tool to access the Cosmos Hub and use gaia. To use the CLI, you must install the latest version of gaia on your machine. Compare your version with the latest release version
gaiad version --long

Available Commands

All available CLI commands are shown when you run the gaiad command:
gaiad
Stargate Cosmos Hub App

Usage:
  gaiad [command]

Available Commands:

  add-genesis-account Add a genesis account to genesis.json
  collect-gentxs      Collect genesis txs and output a genesis.json file
  debug               Tool for helping with debugging your application
  export              Export state to JSON
  gentx               Generate a genesis tx carrying a self delegation
  help                Help about any command
  init                Initialize private validator, p2p, genesis, and application configuration files
  keys                Manage your application's keys
  migrate             Migrate genesis to a specified target version
  query               Querying subcommands
  start               Run the full node
  status              Query remote node for status
  tendermint          Tendermint subcommands
  testnet             Initialize files for a gaia testnet
  tx                  Transactions subcommands
  unsafe-reset-all    Resets the blockchain database, removes address book files, and resets data/priv_validator_state.json to the genesis state
  validate-genesis    validates the genesis file at the default location or at the location passed as an arg
  version             Print the application binary version information

Flags:
  -h, --help                help for gaiad
      --home string         directory for config and data (default "/Users/tobias/.gaia")
      --log_format string   The logging format (json|plain) (default "plain")
      --log_level string    The logging level (trace|debug|info|warn|error|fatal|panic) (default "info")
      --trace               print out full stack trace on errors

Use "gaiad [command] --help" for more information about a command.
For each displayed command, you can use the --help flag to get further information.
gaiad query --help
Usage:
  gaiad query [flags]
  gaiad query [command]

Aliases:
  query, q

Available Commands:
  account                  Query for account by address
  auth                     Querying commands for the auth module
  bank                     Querying commands for the bank module
  block                    Get verified data for a the block at given height
  distribution             Querying commands for the distribution module
  evidence                 Query for evidence by hash or for all (paginated) submitted evidence
  gov                      Querying commands for the governance module
  ibc                      Querying commands for the IBC module
  ibc-transfer             IBC fungible token transfer query subcommands
  mint                     Querying commands for the minting module
  params                   Querying commands for the params module
  slashing                 Querying commands for the slashing module
  staking                  Querying commands for the staking module
  tendermint-validator-set Get the full tendermint validator set at given height
  tx                       Query for a transaction by hash in a committed block
  txs                      Query for paginated transactions that match a set of events
  upgrade                  Querying commands for the upgrade module

Flags:
      --chain-id string   The network chain ID
  -h, --help              help for query

Global Flags:
      --home string         directory for config and data (default "/Users/tobias/.gaia")
      --log_format string   The logging format (json|plain) (default "plain")
      --log_level string    The logging level (trace|debug|info|warn|error|fatal|panic) (default "info")
      --trace               print out full stack trace on errors

Use "gaiad query [command] --help" for more information about a command.

Remote Access to gaiad

When choosing to remote access a Full Node and gaiad, you need a Full Node running and gaia installed on your local machine. gaiad is the tool that enables you to interact with the node that runs on the Cosmos Hub network, whether you run it yourself or not. To set up gaiad on a local machine and connect to an existing full node, use the following command:
gaiad config <flag> <value>
First, set up the address of the full node you want to connect to:
gaiad config node <host>:<port

// example: gaiad config node https://77.87.106.33:26657 (note: this is a placeholder)
If you run your own full node locally, use tcp://localhost:26657 as the address. Finally, set the chain-id of the blockchain you want to interact with:
gaiad config chain-id cosmoshub-4
Next, learn to use CLI commands to interact with the full node. You can run these commands as remote control or when you are running it on your local machine.

Create a Key Pair

The default key is secp256k1 elliptic curve. Use the gaiad keys command to list the keys and generate a new key.
gaiad keys add <your_key_name>
You will be asked to create a password (at least 8 characters) for this key-pair. This will return the information listed below:
  • NAME: Name of your key
  • TYPE: Type of your key, always local.
  • ADDRESS: Your address. Used to receive funds.
  • PUBKEY: Your public key. Useful for validators.
  • MNEMONIC: 24-word phrase. Save this mnemonic somewhere safe. This phrase is required to recover your private key in case you forget the password. The mnemonic is displayed at the end of the output.
You can see all available keys by typing:
gaiad keys list
Use the --recover flag to add a key that imports a mnemonic to your keyring.
gaiad keys add <your_key_name> --recover

Check your Account

You can view your account by using the query account command.
gaiad query account <YOUR_ADDRESS>
It will display your account type, account number, public key and current account sequence.
'@type': /cosmos.auth.v1beta1.BaseAccount
account_number: "xxxx"
address: cosmosxxxx
pub_key:
  '@type': /cosmos.crypto.secp256k1.PubKey
  key: xxx
sequence: "x"

Check your Balance

Query the account balance with the command:
gaiad query bank balances <YOUR_ADDRESS>
The response contains keys balances and pagination. Each balances entry contains an amount held, connected to a denom identifier. The typical $ATOM token is identified by the denom uatom. Where 1 uatom is 0.000001 ATOM.
balances:
- amount: "12345678"
  denom: uatom
pagination:
  next_key: null
  total: "0"
When you query an account that has not received any token yet, the balances entry is shown as an empty array.
balances: []
pagination:
  next_key: null
  total: "0"

Send Coins Using the CLI

To send coins using the CLI:
gaiad tx bank send [from_key_or_address] [to_address] [amount] [flags]
Parameters:
  • <from_key_or_address>: Key name or address of sending account.
  • <to_address>: Address of the recipient.
  • <amount>: This parameter accepts the format <value|coinName>, such as 1000000uatom.
Flags:
  • --chain-id: This flag allows you to specify the id of the chain. There are different ids for different testnet chains and mainnet chains.
  • --gas-prices: This flag allows you to specify the gas prices you pay for the transaction. The format is used as 0.0025uatom

REST API

The REST API documents list all the available endpoints that you can use to interact with your full node. Learn how to enable the REST API on your full node.

Listen for Incoming Transactions

The recommended way to listen for incoming transactions is to periodically query the blockchain by using the following HTTP endpoint: /cosmos/bank/v1beta1/balances/{address}