Code Monkey home page Code Monkey logo

icon-sdk-rust's Introduction

ICONation logo

ICON SDK for Rust

License: MIT

This is an SDK to communicate with the ICON blockchain, built for Rust.

Disclaimer: I cannot guarantee optimal performance of this software. It is provided as is and without any assurances. Use it at your own risk.

Features

  • Wallet management
  • Read data from the blockchain
  • Send ICX transactions
  • Full IRC2 token support
  • Perform SCORE calls
  • Transaction builder

Installation

To use the SDK in your Rust project, add the following to your Cargo.toml:

[dependencies]
icon-sdk = "1.2.0"

Testing

To run tests, ensure you have Rust installed and run:

cargo test

Usage

Get block information

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);
}

Send 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),
    }
}

Use the testnet

// 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()));

Use the transaction builder

See icon_service.rs to see how to use the transaction builder.

icon-sdk-rust's People

Contributors

mitsosf avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.