I'm using Rust with crate web3 0.15.0. (Is there a better library to use?)

My goal is to deploy contracts and call functions in them using programs I've written in Rust.

I have two simple functions, one that connects to the client and the other that deploys a contract, and a main function that calls the two of them. I get an error when I try to use the web3 library to deploy.

use web3::transports::WebSocket;
use web3::Web3;
use web3::api::Eth;
use web3::contract::Contract;

use std::path::Path;
use std::fs::File;
use std::io::Read;

async fn connect_ws () -> Web3<WebSocket>
    let transport = WebSocket::new ("ws://").await;
    Web3::new (transport.unwrap ())

async fn deploy (eth: Eth<WebSocket>)
    let abi_file = "../../solidity/test/SmallContract.abi";
    let bin_file = "../../solidity/test/SmallContract.bin";

    // make sure the files exist
    let required_files = [&abi_file, &bin_file];
    for rf in required_files.iter ()
        if !Path::new (rf).exists () { println! ("{} does not exist.\n", rf); std::process::exit (1); }

    // open the abi file
    let abi = File::open (&abi_file);
    if abi.is_err () { println! ("Failed to open {}\n", abi_file); std::process::exit (1); }

    // read the abi file
    let mut abi_data = String::new ();
    let bytes_read = abi.unwrap ().read_to_string (&mut abi_data);
    if bytes_read.is_err () { println! ("Failed to read from {}\n", abi_file); std::process::exit (1); }

    let builder = Contract::deploy (eth, abi_data.as_bytes ());
    match &builder
        Err (err) => { println! ("Contract::deploy returned an error:\n{}\n", err); std::process::exit (1); },
        Ok (_) => {}

async fn main ()
    let web3 = connect_ws ().await;
    deploy (web3.eth ()).await;

Contract::deploy returns an error. Apparently, it requires bytes instead of the actual text of the JSON.

The output from my program is:

Contract::deploy returned an error:
Serialization error: Invalid operation type. at line 1 column 891

The abi file is 891 ascii characters in length. The JSON is properly formatted. I've tried it with files produced by solc 0.8.2 and solc 0.7.6 and it does the same thing.

Am I doing something wrong? Does anyone know of any sample code to deploy a contract in Rust?

1 Answer 1


This is a known bug and has been reported as Issue #410.

There is also a list of open issues for the web3 crate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.