add eth_balance and eth_accounts
This commit is contained in:
parent
3ed8b58ceb
commit
f8ca81ab22
|
@ -8,7 +8,8 @@ use std::convert::TryFrom;
|
||||||
async fn main() -> Result<(), failure::Error> {
|
async fn main() -> Result<(), failure::Error> {
|
||||||
// connect to the network
|
// connect to the network
|
||||||
let provider = HttpProvider::try_from("http://localhost:8545")?;
|
let provider = HttpProvider::try_from("http://localhost:8545")?;
|
||||||
let from = "784C1bA9846aB4CE78E9CFa27884E29dd31d593A".parse()?;
|
let accounts = provider.get_accounts().await?;
|
||||||
|
let from = accounts[0];
|
||||||
|
|
||||||
// craft the tx
|
// craft the tx
|
||||||
let tx = TransactionRequest {
|
let tx = TransactionRequest {
|
||||||
|
@ -21,6 +22,8 @@ async fn main() -> Result<(), failure::Error> {
|
||||||
nonce: None,
|
nonce: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let balance_before = provider.get_balance(from, None).await?;
|
||||||
|
|
||||||
// broadcast it via the eth_sendTransaction API
|
// broadcast it via the eth_sendTransaction API
|
||||||
let tx_hash = provider.send_transaction(tx).await?;
|
let tx_hash = provider.send_transaction(tx).await?;
|
||||||
|
|
||||||
|
@ -38,5 +41,11 @@ async fn main() -> Result<(), failure::Error> {
|
||||||
|
|
||||||
assert!(nonce2 < nonce1);
|
assert!(nonce2 < nonce1);
|
||||||
|
|
||||||
|
let balance_after = provider.get_balance(from, None).await?;
|
||||||
|
assert!(balance_after < balance_before);
|
||||||
|
|
||||||
|
println!("Balance before {}", balance_before);
|
||||||
|
println!("Balance after {}", balance_after);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,6 +46,11 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Gets the accounts on the node
|
||||||
|
pub async fn get_accounts(&self) -> Result<Vec<Address>, P::Error> {
|
||||||
|
self.0.request("eth_accounts", None::<()>).await
|
||||||
|
}
|
||||||
|
|
||||||
/// Gets the latest block number via the `eth_BlockNumber` API
|
/// Gets the latest block number via the `eth_BlockNumber` API
|
||||||
pub async fn get_block_number(&self) -> Result<U256, P::Error> {
|
pub async fn get_block_number(&self) -> Result<U256, P::Error> {
|
||||||
self.0.request("eth_blockNumber", None::<()>).await
|
self.0.request("eth_blockNumber", None::<()>).await
|
||||||
|
@ -71,6 +76,8 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
self.0.request("eth_sendRawTransaction", Some(rlp)).await
|
self.0.request("eth_sendRawTransaction", Some(rlp)).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Account state
|
||||||
|
|
||||||
pub async fn get_transaction_count(
|
pub async fn get_transaction_count(
|
||||||
&self,
|
&self,
|
||||||
from: Address,
|
from: Address,
|
||||||
|
@ -82,4 +89,14 @@ impl<P: JsonRpcClient> Provider<P> {
|
||||||
.request("eth_getTransactionCount", Some(&[from, block]))
|
.request("eth_getTransactionCount", Some(&[from, block]))
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn get_balance(
|
||||||
|
&self,
|
||||||
|
from: Address,
|
||||||
|
block: Option<BlockNumber>,
|
||||||
|
) -> Result<U256, P::Error> {
|
||||||
|
let from = utils::serialize(&from);
|
||||||
|
let block = utils::serialize(&block.unwrap_or(BlockNumber::Latest));
|
||||||
|
self.0.request("eth_getBalance", Some(&[from, block])).await
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue