reba-client 0.1.0

Reba client
Documentation
use std::env;
use std::process::exit;

use hex::decode;
use reba_client::client::RelayClient;
use secp256k1::SecretKey;
use web3::signing::{Key,
                    SecretKeyRef};
use web3::transports::Http;
use web3::types::TransactionParameters;
use web3::Web3;

const AVAX_API_URL: &str = "https://api.avax.network/ext/bc/C/rpc";

// gets the specified environment variable
// returns an empty string if it does not exist
fn get_env(key: &str) -> String
{
    match env::var(key)
    {
        Ok(val) => val,
        Err(_e) => String::new()
    }
}
#[tokio::main]
async fn main()
{
    // New HTTP provider. The provider is used to get information needed
    // to send a valid TX when sending via the Relay.
    let provider = Http::new(AVAX_API_URL).unwrap();

    // New Web3 instance using the HTTP provider
    let web3 = Web3::new(provider);

    let pk_str = get_env("PK");
    let private_key_bytes: &[u8] = pk_str.as_bytes();

    let sk = SecretKey::from_slice(&decode(private_key_bytes).unwrap()).unwrap();

    let pk = SecretKeyRef::new(&sk);

    let address = pk.address();


    let mut client = RelayClient::new(address, &pk).unwrap();

    let tx = TransactionParameters { value: 0.into(),
                                     nonce: None,
                                     to: Some(address),
                                     chain_id: 43114.into(), // Cam said this may fix it, still nothing.
                                     ..Default::default() };

    let signed_tx = match web3.accounts().sign_transaction(tx, &sk).await
    {
        Ok(signed_tx) => signed_tx,
        Err(e) =>
        {
            println!("Error signing transaction: {:?}", e);
            exit(1);
        }
    };

    println!("Signed tx hash: {:x?}", signed_tx.transaction_hash);

    // this is not working. TX sends successfully, however it doesn't
    // get to the nodes. Does not appear on snowtrace.
    match client.send_tx(signed_tx)
    {
        Ok(_) => println!("Sent transaction successfully."),
        Err(e) =>
        {
            println!("Error sending transaction: {:?}", e);
            exit(1);
        }
    };
}