chain

package
v0.0.30 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 6, 2025 License: MIT Imports: 35 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultHDPath        = "m/44'/118'/0/0/0"
	DefaultAddressPrefix = "cosmos"
)

Variables

This section is empty.

Functions

func PubToAddress

func PubToAddress(publicKey string) string

PubToAddress 公钥转换为地址

Types

type Client

type Client struct {

	// 核心对象
	Encoding  moduletestutil.TestEncodingConfig
	ClientCtx client.Context
	KeyRing   keyring.Keyring
	Config    Config
	// contains filtered or unexported fields
}

Client 区块链客户端

func New added in v0.0.25

func New(cfg Config) (*Client, error)

New 创建新的区块链客户端实例(多实例安全)

func (*Client) Account

func (c *Client) Account(address sdk.AccAddress) (client.Account, error)

func (*Client) AccountNumberSequence added in v0.0.25

func (c *Client) AccountNumberSequence(addr sdk.AccAddress) (uint64, uint64, error)

AccountNumberSequence 通过地址查询账户号/序列号(支持各种账户类型)

func (*Client) Address

func (c *Client) Address(name string) (sdk.AccAddress, error)

Address 通过 keyring 名称获取地址

func (*Client) Balance added in v0.0.25

func (c *Client) Balance(ctx context.Context, addr sdk.AccAddress, denom string) (sdk.Coin, error)

Balance 查询某地址某 denom 的余额

func (*Client) BroadcastRawTx added in v0.0.26

func (c *Client) BroadcastRawTx(ctx context.Context, txBytes []byte) (*txtypes.BroadcastTxResponse, error)

BroadcastRawTx 通过 gRPC 广播已签名交易字节

func (*Client) BroadcastTx added in v0.0.25

func (c *Client) BroadcastTx(
	ctx context.Context,
	senderName string,
	msgs ...sdk.Msg,
) (*txtypes.BroadcastTxResponse, error)

BroadcastTx 通过 gRPC 广播 自动签名一步到位:构建->签名->广播

func (*Client) Close added in v0.0.25

func (c *Client) Close() error

Close 优雅关闭

func (*Client) EncodeTxBytes added in v0.0.25

func (c *Client) EncodeTxBytes(txBuilder client.TxBuilder) ([]byte, error)

EncodeTxBytes 编码为 protobuf bytes

func (*Client) LatestHeight added in v0.0.25

func (c *Client) LatestHeight(ctx context.Context) (int64, error)

LatestHeight 获取最新区块高度

func (*Client) Ping added in v0.0.25

func (c *Client) Ping(timeout time.Duration) error

Ping 查询节点状态(确认 RPC/gRPC 通)

func (*Client) SendCoins added in v0.0.25

func (c *Client) SendCoins(
	ctx context.Context,
	signerName string,
	fromAddr, toAddr sdk.AccAddress,
	amounts ...string,
) (*txtypes.BroadcastTxResponse, error)

SendCoins 使用指定 signer 从 from -> to 转账 amounts: ["10stake","10token"]

func (*Client) SignTxWith added in v0.0.25

func (c *Client) SignTxWith(signerName string, txBuilder client.TxBuilder, accNum, seq uint64) error

SignTxWith 显式给定 accNum/seq 的签名(并发压测推荐用)

func (*Client) SimulateGas added in v0.0.25

func (c *Client) SimulateGas(ctx context.Context, txBuilder client.TxBuilder) (uint64, error)

SimulateGas 使用 gRPC TxService.Simulate 估算 gas(返回 GasUsed)

func (*Client) Status

func (c *Client) Status() (*coretypes.ResultStatus, error)

Status 区块链信息

func (*Client) TxBuilder added in v0.0.25

func (c *Client) TxBuilder(msgs ...sdk.Msg) (client.TxBuilder, error)

TxBuilder 构建 client.TxBuilder 并设置消息

func (*Client) TxFactory added in v0.0.25

func (c *Client) TxFactory() (sdktx.Factory, error)

TxFactory 返回一个带链配置的 *tx.Factory,便于构建/签名/广播

func (*Client) WaitForTx added in v0.0.25

func (c *Client) WaitForTx(ctx context.Context, txHash string, timeout, interval time.Duration) (*txtypes.GetTxResponse, error)

WaitForTx 轮询等待上链 timeout 例:30*time.Second;interval 例:500*time.Millisecond。

type Config added in v0.0.25

type Config struct {
	// 基础设置
	ChainID        string                // 链 ID, node id
	NodeRPC        string                // 节点 RPC 地址 e.g. "http://127.0.0.1:26657"
	NodeGRPC       string                // 节点 gRPC 地址 e.g. "127.0.0.1:9090"
	HomeDir        string                // 数据目录
	KeyringBackend string                // "test" | "file" | "os"
	BroadcastMode  txtypes.BroadcastMode // 广播模式

	// 发送交易相关设置
	Fee      string // e.g. "{amount0}{denomination},...,{amountN}{denominationN}"
	GasLimit uint64

	// gRPC 限制/保活
	GRPCMaxRecvBytes int
	GRPCMaxSendBytes int
	KeepaliveTime    time.Duration
	KeepaliveTimeout time.Duration

	// 调试 / 其他
	SkipConfirmation bool

	// 自定义模块注册(可选,覆盖默认:auth + bank)
	// 用于自定义 encoding config(注册更多模块)
	RegisterModules func() moduletestutil.TestEncodingConfig
}

Config 客户端的配置参数

func DefaultConfig added in v0.0.25

func DefaultConfig(chainid, homedir string, modules ...module.AppModuleBasic) Config

DefaultConfig 获取默认配置 modules yourmodule.AppModule{} (通过go.mod引入yourmodule "module/x/module/foo")

type KeyRing

type KeyRing struct {
	Address  string             `json:"address"`  // cosmos地址
	Mnemonic string             `json:"mnemonic"` // 24词助记词
	Private  *secp256k1.PrivKey `json:"private"`  // 私钥
	Public   *secp256k1.PubKey  `json:"public"`   // 公钥
	HDPath   string             `json:"hd_path"`  // HD路径
}

KeyRing 包含区块链账户的密钥信息

func NewDefaultKey

func NewDefaultKey() (KeyRing, error)

NewDefaultKey 创建一个使用默认前缀和HD路径的新密钥 返回生成的密钥信息和可能的错误

func NewKey

func NewKey(prefix, hdPath string) (KeyRing, error)

NewKey 根据指定的地址前缀和HD路径创建新的密钥对 prefix: 地址前缀,如"cosmos" hdPath: HD钱包路径,如"m/44'/118'/0/0/0" 返回生成的密钥信息和可能的错误

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL