#cosmos #blockchain #cosmos-rust-wallet

已删除 crw-client

cosmos-rust-wallet的客户端包,用于从基于cosmos区块链的链中广播交易和查询数据

0.1.0 2021年3月24日

#201 in #cosmos

Apache-2.0

18KB
269

客户端包

客户端包负责查询和向基于cosmos-sdk的链(更新到stargate)进行广播操作。

链客户端

pub struct ChainClient {
    pub node_info: NodeInfo,
    pub grpc_addr: String,
    pub lcd_addr: String,
}

pub struct NodeInfo {
    pub id: String,
    pub network: String,
}

获取账户数据

fn get_account_data_example() {
    let grpc_endpoint = "http://localhost:9090";
    let lcd_endpoint = "http://localhost:1317";
    let address = "desmos1rc4jrjyxyq0qpv7sn5ex9tr0kt0chrdq3x66ah";
    let node_info = get_node_info(lcd_endpoint.to_string())
        .await
        .unwrap()
        .node_info;

    let chain_client = ChainClient::new(
        node_info,
        lcd_endpoint.to_string(),
        grpc_endpoint.to_string(),
    );

    let account = chain_client.get_account_data(address.to_string()).await.unwrap();
}

广播交易

fn broadcast_tx_example() {
    let wallet = Wallet::from_mnemonic(...).unwrap();
    let chain_client = ChainClient::new(...);
    let msg = MsgSend {...};
    let fee = Fee {...};

    let mut msg_bytes = Vec::new();
    prost::Message::encode(&msg, &mut msg_bytes).unwrap();

    let proto_msg = Msg(Any {
        type_url: "/cosmos.bank.v1beta1.Msg/Send".to_string(),
        value: msg_bytes,
    });
    
    let msgs = vec![proto_msg];

    let account = chain_client
        .get_account_data(wallet.bech32_address.clone())
        .await
        .unwrap();

    let tx_signed_bytes = wallet
        .sign_tx(
            account,
            chain_client.node_info.network.clone(),
            msgs,
            fee,
            None,
            0,
        )
        .unwrap();

    let transaction_response = chain_client
        .broadcast_tx(tx_signed_bytes, BroadcastMode::Block)
        .await
        .unwrap();
}

依赖关系

~14–27MB
~429K SLoC