Skip to content

Instantly share code, notes, and snippets.

@niklasad1
Created October 15, 2018 16:02
Show Gist options
  • Save niklasad1/43690f39a07e59f92fd4a8ea9709385b to your computer and use it in GitHub Desktop.
Save niklasad1/43690f39a07e59f92fd4a8ea9709385b to your computer and use it in GitHub Desktop.
extern crate web3;
#[macro_use]
extern crate hex_literal;
use web3::futures::Future;
use web3::types::*;
fn main() {
read_only();
account();
test_blocks();
test_state();
}
fn create_call_req(from: Option<Address>) -> CallRequest {
CallRequest {
from,
to: 5.into(),
gas: Some(100_000.into()),
gas_price: None,
value: Some(5_000_000.into()),
data: Some(vec![1, 2, 3].into()),
}
}
fn read_only() {
let block_hex = hex!("563c77e3d5580028497c1b6106945d16ce60ffff");
let a: Address = block_hex.into();
let (_eloop, transport) = web3::transports::Http::new("http://localhost:8555").unwrap();
let web3 = web3::Web3::new(transport);
println!(
"eth_estimateGas: {:?}",
web3.eth()
.estimate_gas(create_call_req(None), Some(BlockNumber::Latest))
.wait()
.unwrap()
);
println!(
"eth_call: {:?}",
web3.eth()
.call(create_call_req(None), Some(BlockNumber::Latest))
.wait()
.unwrap()
);
println!(
"(invalid account) eth_estimateGas: {:?}",
web3.eth()
.estimate_gas(create_call_req(Some(a)), Some(BlockNumber::Latest))
.wait()
);
println!(
"(invalid account) eth_call: {:?}",
web3.eth()
.call(create_call_req(Some(a)), Some(BlockNumber::Latest))
.wait()
);
println!(
"eth_getBalance: {:?}",
web3.eth()
.balance(0.into(), Some(BlockNumber::Latest))
.wait()
.unwrap()
);
println!(
"(invalid account) eth_getBalance: {:?}",
web3.eth()
.balance(a, Some(BlockNumber::Latest))
.wait()
.unwrap()
);
}
fn account() {
let (_eloop, transport) = web3::transports::Http::new("http://localhost:8555").unwrap();
let web3 = web3::Web3::new(transport);
println!("eth_accounts: {:?}", web3.eth().accounts().wait().unwrap());
println!("eth_coinbase: {:?}", web3.eth().coinbase().wait().unwrap());
}
fn test_blocks() {
let (_eloop, transport) = web3::transports::Http::new("http://localhost:8555").unwrap();
let web3 = web3::Web3::new(transport);
let block_number = web3.eth().block_number().wait().unwrap();
println!("eth_blockNumber: {}", block_number);
println!(
"eth_getBlockByNumber 0: {:?}",
web3.eth().block(BlockId::from(0)).wait().unwrap()
);
let tx_hash: H256 = hex!("a17b45b011dad358740ccb65383e0900b086643d15192060f3983160492cca15").into();
println!(
"eth_getTransactionCountByHash: {:?}",
web3.eth()
.block_transaction_count(BlockId::Hash(tx_hash))
.wait()
);
println!(
"eth_getTransactionCountByNumber 0: {:?}",
web3.eth()
.block_transaction_count(BlockId::Number(BlockNumber::Latest))
.wait()
);
}
fn test_state() {
let (_eloop, transport) = web3::transports::Http::new("http://localhost:8555").unwrap();
let web3 = web3::Web3::new(transport);
let inval_addr: H160 = hex!("563c77e3d5580028497c1b6106945d16ce60ffff").into();
let parity_addr: H160 = hex!("0010f94b296A852aAac52EA6c5Ac72e03afD032D").into();
// requires state pruning
println!(
"eth_getCode: {:?}",
web3.eth().code(parity_addr, None).wait().unwrap()
);
println!(
"eth_getCode: {:?}",
web3.eth().code(inval_addr, None).wait().unwrap()
);
println!(
"eth_getTransactionReceipt: {:?}",
web3.eth().transaction_receipt(0.into()).wait()
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment