#icons #blockchain #sdk #icx

icon-sdk

ICON(ICX) SDK for Rust

4个稳定版本

1.2.0 2024年3月20日
1.1.0 2024年3月15日
1.0.1 2024年3月14日

#2431 in 神奇豆

MIT许可证

30KB
548

ICONation logo

ICON SDK for Rust

License: MIT

这是一个用于与ICON区块链通信的SDK,专为Rust编写。

免责声明:我无法保证此软件的性能最优。它按原样提供,不提供任何保证。自行承担使用风险。

功能

  • 钱包管理
  • 从区块链读取数据
  • 发送ICX交易
  • 完全支持IRC2代币
  • 执行SCORE调用
  • 交易构建器

安装

要在Rust项目中使用SDK,请将以下内容添加到您的 Cargo.toml

[dependencies]
icon-sdk = "1.2.0"

测试

要运行测试,请确保已安装Rust,然后运行

cargo test

使用方法

获取区块信息

use icon_sdk::icon_service;

async fn main() {
    // Example: get last block
    let icon_service = icon_service::IconService::new(None);
    let last_block = icon_service.get_last_block().await;
    println!("{:?}", last_block);

    // Example: get block by height
    let block_by_height = icon_service.get_block_by_height("0x3").await;
    println!("{:?}", block_by_height);

    // Example: get block by hash
    let block_by_hash = icon_service.get_block_by_hash("0x123986e1c834632f6e65915c249d81cd01453ec915e3370d364d6df7be5e6c03").await;
    println!("{:?}", block_by_hash);
    
    // Example: get transaction result
    let transaction_result = icon_service.get_transaction_result("0x123986e1c834632f6e65915c249d81cd01453ec915e3370d364d6df7be5e6c03").await;
    println!("{:?}", transaction_result);
    
    // Example: get transaction by hash
    let transaction_by_hash = icon_service.get_transaction_by_hash("0x123986e1c834632f6e65915c249d81cd01453ec915e3370d364d6df7be5e6c03").await;
    println!("{:?}", transaction_by_hash);
    
    // Example: SCORE call
    let score_call = icon_service.call(
        "cx9ab3078e72c8d9017194d17b34b1a47b661945ca",
        json!({
            "method": "balanceOf",
            "params": {
                "_owner": "hx70e8eeb5d23ab18a828ec95f769db6d953e5f0fd"
            }
        })).await;
    println!("{:?}", score_call);
}

发送ICX

use icon_sdk::{icon_service, wallet::Wallet};

#[tokio::main]
async fn main() {
    let wallet = Wallet::new(None); //Or load a wallet from a private key

    let to = "hx9ab3078e72c8d9017194d17b34b1a47b661945ca";
    let value = "100"; // Amount to send in ICX or hex encoded value for tokens
    let version = "0x3"; 
    let nid = "0x3"; 
    let nonce = "0x1234";
    let step_limit = "0x186a0";
    let message = "Hello, ICON!";

    let icon_service = icon_service::IconService::new(None);
    // Send the transaction
    match icon_service.send_transaction(wallet, to, value, version, nid, nonce, step_limit).await {
        Ok(response) => println!("Transaction sent successfully: {:?}", response),
        Err(err) => eprintln!("Error sending transaction: {}", err),
    }
    
    // Send the transaction with a message
    match icon_service.send_transaction_with_message(wallet, to, value, version, nid, nonce, step_limit, message).await {
        Ok(response) => println!("Transaction sent successfully: {:?}", response),
        Err(err) => eprintln!("Error sending transaction: {}", err),
    }
}

使用测试网

// Lisbon testnet, make sure to also change the nid when needed
let icon_service = icon_service::IconService::new(Some("https://lisbon.net.solidwallet.io/api/v3".to_string()));

使用交易构建器

查看 icon_service.rs 了解如何使用交易构建器。

依赖项

~14–30MB
~377K SLoC