Giter VIP home page Giter VIP logo

rust-web3's Introduction

rust-web3

Ethereum and CITA JSON-RPC multi-transport client. Rust implementation of Web3.js library.

Examples1

extern crate tokio_core;
extern crate web3;

use web3::futures::Future;
use web3::types::{BlockId, BlockNumber};

const MAX_PARALLEL_REQUESTS: usize = 64;

fn main() {
    let mut event_loop = tokio_core::reactor::Core::new().unwrap();
    let web3 = web3::Web3::new(
        web3::transports::Http::with_event_loop(
            "http://localhost:1337",
            &event_loop.handle(),
            MAX_PARALLEL_REQUESTS,
        ).unwrap(),
    );

    //get height
    let block_number = web3.cita().block_number().map(|height| {
        println!("height: {:?}", height);
    });
    event_loop.run(block_number).unwrap();

    //get peer count
    let peer_count = web3.net().peer_count().map(|peer_count| {
        println!("peer_count: {:?}", peer_count);
    });
    event_loop.run(peer_count).unwrap();

    //get block
    let block = web3.cita()
        .block(BlockId::Number(BlockNumber::Latest))
        .map(|block| {
            println!("block: {:?}", block);
        });
    event_loop.run(block).unwrap();
}

example2: send_transaction

For more create contract information, you can see jsonrpc readme.md

extern crate cita_crypto;
extern crate protobuf;
extern crate rustc_hex;
extern crate tokio_core;
extern crate web3;

use cita_crypto::*;
use web3::futures::Future;
use web3::cita_types::{Transaction, UnverifiedTransaction};

//use std::str::FromStr;
use protobuf::core::Message;
use rustc_hex::FromHex;
use rustc_hex::ToHex;

const MAX_PARALLEL_REQUESTS: usize = 64;
const CONTRUCT_CODE: &str = "60606040523415600e57600080fd5b5b5b5b60948061001f6000396000f300\
                             60606040526000357c01000000000000000000000000000000000000000000\
                             00000000000000900463ffffffff1680635524107714603d575b600080fd5b\
                             3415604757600080fd5b605b6004808035906020019091905050605d565b00\
                             5b806000819055505b505600a165627a7a72305820c471b4376626da2540b2\
                             374e8b4110501051c426ff46814a6170ce9e219e49a80029";


fn main() {
    let mut event_loop = tokio_core::reactor::Core::new().unwrap();
    let web3 = web3::Web3::new(
        web3::transports::Http::with_event_loop(
            "http://localhost:1337",
            &event_loop.handle(),
            MAX_PARALLEL_REQUESTS,
        ).unwrap(),
    );

    //study create sendtransaction param
    let cita = web3.cita();
    let height = cita.block_number().map(|height| {
        println!("height: {:?}", height);
        height
    });


    let number = event_loop.run(height).unwrap();
    let key_pair = KeyPair::gen_keypair();

    //create contract
    let number = number.low_u64();
    println!("number: {:?}", number);
    let tx = generate_tx(
        CONTRUCT_CODE.to_string(),
        "".to_string(),
        key_pair.privkey(),
        number,
        2500,
    ).write_to_bytes()
        .unwrap()
        .to_hex();

    println!("hex = {:?}", tx);
    let tx = cita.send_transaction(tx).map(|tx_response| {
        println!("tx_response: {:?}", tx_response);
        tx_response
    });
    event_loop.run(tx).unwrap();
}

fn generate_tx(code: String, address: String, pv: &PrivKey, curh: u64, quota: u64) -> UnverifiedTransaction {
    let data = code.from_hex().unwrap();
    let mut tx = Transaction::new();
    tx.set_data(data);
    tx.set_to(address);
    tx.set_nonce("0".to_string());
    tx.set_valid_until_block(curh + 100);
    tx.set_quota(quota);
    tx.sign(*pv).take_transaction_with_sig()
}

If you want to deploy smart contracts you have written you can do something like this (make sure you have the solidity compiler installed):

solc -o build --bin --abi contracts/*.sol

The solidity compiler is generating the binary and abi code for the smart contracts in a directory called contracts and is being output to a directory called build.

For more see examples folder

TODO

General

  • More flexible API (accept Into<X>)
  • Contract calls (ABI encoding; debris/ethabi)
  • Batch Requests

Transports

  • HTTP transport
  • IPC transport
  • WebSockets transport

Types

  • Types for U256,H256,Address(H160)
  • Index type (numeric, encoded to hex)
  • Transaction type (Transaction from Parity)
  • Transaction receipt type (TransactionReceipt from Parity)
  • Block type (RichBlock from Parity)
  • Work type (Work from Parity)
  • Syncing type (SyncStats from Parity)

APIs

  • Eth: eth_*
  • Eth filters: eth_*
  • net_*
  • web3_*
  • personal_*
  • cita_*
  • traces_*

Parity-specific APIs

  • Parity read-only: parity_*

  • Parity accounts: parity_*

  • Parity set: parity_*

  • signer_*

  • Own APIs (Extendable)

let web3 = Web3::new(transport);
web3.api::<CustomNamespace>().custom_method().wait().unwrap()

Installation on Windows

Currently, Windows does not support IPC, which is enabled in the library by default. To complile, you need to disable IPC feature:

web3 = { version = "0.1.0", default-features = false, features = ["http"] }

rust-web3's People

Contributors

tomusdrw avatar rphmeier avatar suyanlong avatar debris avatar usd-yamazaki avatar regresscheck avatar hswick avatar

Watchers

James Cloos avatar  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.