Struct flow_sdk::client::FlowClient [−][src]
pub struct FlowClient<T> { /* fields omitted */ }
Expand description
A gRPC client wrapper. Has utility functions for sending requests.
Implementations
Wraps the inner client to gain access to helper functions to send requests.
Retrieve the inner client from this instance.
Sends a request over the client.
pub fn ping<'grpc>(
&'grpc mut self
) -> Pin<Box<dyn Future<Output = Result<PingResponse, Inner::Error>> + 'grpc>> where
Inner: GrpcClient<PingRequest, PingResponse>,
pub fn ping<'grpc>(
&'grpc mut self
) -> Pin<Box<dyn Future<Output = Result<PingResponse, Inner::Error>> + 'grpc>> where
Inner: GrpcClient<PingRequest, PingResponse>,
Sends a ping over the network.
Examples found in repository
More examples
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::testnet().await?;
client.ping().await?;
let latest_block = client.latest_block(Seal::Sealed).await?;
let block_by_id = client.block_by_id(&latest_block.id).await?;
let block_by_height = client.block_by_height(latest_block.height).await?;
assert_eq!(latest_block, block_by_id);
assert_eq!(latest_block, block_by_height);
println!("OK: {:#?}", latest_block);
Ok(())
}
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::mainnet().await?;
client.ping().await?;
let ret = client
.execute_script_at_latest_block(
SIMPLE_SCRIPT,
[ValueRef::Int(cadence_json::BigInt::from(32))],
)
.await?
.parse()?;
println!("{:#?}", ret);
let ret = client
.execute_script_at_latest_block(COMPLEX_SCRIPT, [ValueRef::String("John Doe")])
.await?
.parse()?;
println!("{:#?}", ret);
Ok(())
}
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut net = TonicHyperFlowClient::testnet().await?;
let _ = net.ping().await?;
let latest_block_header = net.latest_block_header(Seal::Sealed).await?;
println!("{:?}", latest_block_header);
let Timestamp { nanos, seconds } = latest_block_header.timestamp;
println!("{}", Utc.timestamp(seconds, nanos as u32));
println!("----------------");
let block_header2 = net
.block_header_by_height(latest_block_header.height)
.await?;
println!("{:?}", block_header2);
let Timestamp { nanos, seconds } = block_header2.timestamp;
println!("{}", Utc.timestamp(seconds, nanos as u32));
assert_eq!(latest_block_header, block_header2);
Ok(())
}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::mainnet().await?;
client.ping().await?;
let mut latest_block = client.latest_block(Seal::Sealed).await?;
// traverse latest blocks until we find a collection guarantee.
let collection_guarrantee = loop {
if latest_block.collection_guarantees.is_empty() {
// Go to the next block
latest_block = client.block_by_id(&latest_block.parent_id).await?;
} else {
break latest_block.collection_guarantees.pop().unwrap();
}
};
// retrieve the collection by id.
let collection = client
.collection_by_id(&collection_guarrantee.collection_id)
.await?;
println!("OK: {:#?}", collection);
Ok(())
}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::mainnet().await?;
client.ping().await?;
let latest_block_height = client.latest_block_header(Seal::Sealed).await?.height;
let start_height = latest_block_height - 20;
println!(
"Searching for accounts created within the last 20 blocks ({}-{})...",
start_height, latest_block_height
);
for events in client
.events_for_height_range("flow.AccountCreated", start_height, latest_block_height)
.await?
.results
.iter()
{
if events.events.is_empty() {
continue;
}
println!(
"\nBlock #{} ({}):",
events.block_height,
hex::encode(&events.block_id)
);
for event in events.events.iter() {
let val = event.parse_payload()?;
println!(" - {:#?}", val);
}
}
Ok(())
}
pub fn events_for_height_range<'grpc, EventTy>(
&'grpc mut self,
ty: EventTy,
start_height: u64,
end_height: u64
) -> Pin<Box<dyn Future<Output = Result<EventsResponse, Inner::Error>> + 'grpc>> where
Inner: GrpcClient<GetEventsForHeightRangeRequest<EventTy>, EventsResponse>,
pub fn events_for_height_range<'grpc, EventTy>(
&'grpc mut self,
ty: EventTy,
start_height: u64,
end_height: u64
) -> Pin<Box<dyn Future<Output = Result<EventsResponse, Inner::Error>> + 'grpc>> where
Inner: GrpcClient<GetEventsForHeightRangeRequest<EventTy>, EventsResponse>,
Retrieves events with the specified type within the specified range.
Examples found in repository
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::mainnet().await?;
client.ping().await?;
let latest_block_height = client.latest_block_header(Seal::Sealed).await?.height;
let start_height = latest_block_height - 20;
println!(
"Searching for accounts created within the last 20 blocks ({}-{})...",
start_height, latest_block_height
);
for events in client
.events_for_height_range("flow.AccountCreated", start_height, latest_block_height)
.await?
.results
.iter()
{
if events.events.is_empty() {
continue;
}
println!(
"\nBlock #{} ({}):",
events.block_height,
hex::encode(&events.block_id)
);
for event in events.events.iter() {
let val = event.parse_payload()?;
println!(" - {:#?}", val);
}
}
Ok(())
}
More examples
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::mainnet().await?;
client.ping().await?;
let latest_block_height = client.latest_block_header(Seal::Sealed).await?.height;
let start_height = latest_block_height - 100;
println!(
"Searching for accounts created within the last 100 blocks ({}-{})...",
start_height, latest_block_height
);
let mut accounts = Vec::new();
for events in client
.events_for_height_range("flow.AccountCreated", start_height, latest_block_height)
.await?
.results
.iter()
{
for event in events.events.iter() {
let val: cadence_json::ValueOwned = serde_json::from_slice(&event.payload)?;
if let cadence_json::ValueOwned::Event(c) = val {
for field in c.fields.into_iter().filter(|f| f.name == "address") {
if let cadence_json::ValueOwned::Address(addr) = field.value {
accounts.push(client.account_at_latest_block(&addr.data).await?);
}
}
}
}
}
println!("Found {} accounts.", accounts.len());
if let Some(acc) = accounts.into_iter().max_by_key(|acc| acc.balance) {
println!(
"\nThe richest account is 0x{} with a balance of {}.",
hex::encode(acc.address),
acc.balance
);
}
Ok(())
}
pub fn events_for_blocks_by_ids<'grpc, EventTy, BlockIds>(
&'grpc mut self,
ty: EventTy,
block_ids: BlockIds
) -> Pin<Box<dyn Future<Output = Result<EventsResponse, Inner::Error>> + 'grpc>> where
Inner: GrpcClient<GetEventsForBlockIdsRequest<EventTy, BlockIds>, EventsResponse>,
pub fn events_for_blocks_by_ids<'grpc, EventTy, BlockIds>(
&'grpc mut self,
ty: EventTy,
block_ids: BlockIds
) -> Pin<Box<dyn Future<Output = Result<EventsResponse, Inner::Error>> + 'grpc>> where
Inner: GrpcClient<GetEventsForBlockIdsRequest<EventTy, BlockIds>, EventsResponse>,
Retrieves events with the specified type with the specified block ids.
pub fn execute_script_at_latest_block<'grpc, Script, Arguments>(
&'grpc mut self,
script: Script,
arguments: Arguments
) -> Pin<Box<dyn Future<Output = Result<ExecuteScriptResponse, Inner::Error>> + 'grpc>> where
Inner: GrpcClient<ExecuteScriptAtLatestBlockRequest<Script, Arguments>, ExecuteScriptResponse>,
pub fn execute_script_at_latest_block<'grpc, Script, Arguments>(
&'grpc mut self,
script: Script,
arguments: Arguments
) -> Pin<Box<dyn Future<Output = Result<ExecuteScriptResponse, Inner::Error>> + 'grpc>> where
Inner: GrpcClient<ExecuteScriptAtLatestBlockRequest<Script, Arguments>, ExecuteScriptResponse>,
Executes Cadence script at the latest block and returns the result.
Examples found in repository
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::mainnet().await?;
client.ping().await?;
let ret = client
.execute_script_at_latest_block(
SIMPLE_SCRIPT,
[ValueRef::Int(cadence_json::BigInt::from(32))],
)
.await?
.parse()?;
println!("{:#?}", ret);
let ret = client
.execute_script_at_latest_block(COMPLEX_SCRIPT, [ValueRef::String("John Doe")])
.await?
.parse()?;
println!("{:#?}", ret);
Ok(())
}
pub fn execute_script_at_block_id<'grpc, BlockId, Script, Arguments>(
&'grpc mut self,
block_id: BlockId,
script: Script,
arguments: Arguments
) -> Pin<Box<dyn Future<Output = Result<ExecuteScriptResponse, Inner::Error>> + 'grpc>> where
Inner: GrpcClient<ExecuteScriptAtBlockIdRequest<BlockId, Script, Arguments>, ExecuteScriptResponse>,
pub fn execute_script_at_block_id<'grpc, BlockId, Script, Arguments>(
&'grpc mut self,
block_id: BlockId,
script: Script,
arguments: Arguments
) -> Pin<Box<dyn Future<Output = Result<ExecuteScriptResponse, Inner::Error>> + 'grpc>> where
Inner: GrpcClient<ExecuteScriptAtBlockIdRequest<BlockId, Script, Arguments>, ExecuteScriptResponse>,
Executes Cadence script at a specific block height and returns the result.
pub fn execute_script_at_block_height<'grpc, Script, Arguments>(
&'grpc mut self,
block_height: u64,
script: Script,
arguments: Arguments
) -> Pin<Box<dyn Future<Output = Result<ExecuteScriptResponse, Inner::Error>> + 'grpc>> where
Inner: GrpcClient<ExecuteScriptAtBlockHeightRequest<Script, Arguments>, ExecuteScriptResponse>,
pub fn execute_script_at_block_height<'grpc, Script, Arguments>(
&'grpc mut self,
block_height: u64,
script: Script,
arguments: Arguments
) -> Pin<Box<dyn Future<Output = Result<ExecuteScriptResponse, Inner::Error>> + 'grpc>> where
Inner: GrpcClient<ExecuteScriptAtBlockHeightRequest<Script, Arguments>, ExecuteScriptResponse>,
Executes Cadence script at a specific block height and returns the result.
pub fn send_transaction<'grpc, Script, Arguments, ReferenceBlockId, ProposalKeyAddress, Payer, Authorizers, PayloadSignatures, EnvelopeSignatures>(
&'grpc mut self,
transaction: TransactionE<Script, Arguments, ReferenceBlockId, ProposalKeyAddress, Payer, Authorizers, PayloadSignatures, EnvelopeSignatures>
) -> Pin<Box<dyn Future<Output = Result<SendTransactionResponse, Inner::Error>> + 'grpc>> where
Inner: GrpcClient<SendTransactionRequest<Script, Arguments, ReferenceBlockId, ProposalKeyAddress, Payer, Authorizers, PayloadSignatures, EnvelopeSignatures>, SendTransactionResponse>,
pub fn send_transaction<'grpc, Script, Arguments, ReferenceBlockId, ProposalKeyAddress, Payer, Authorizers, PayloadSignatures, EnvelopeSignatures>(
&'grpc mut self,
transaction: TransactionE<Script, Arguments, ReferenceBlockId, ProposalKeyAddress, Payer, Authorizers, PayloadSignatures, EnvelopeSignatures>
) -> Pin<Box<dyn Future<Output = Result<SendTransactionResponse, Inner::Error>> + 'grpc>> where
Inner: GrpcClient<SendTransactionRequest<Script, Arguments, ReferenceBlockId, ProposalKeyAddress, Payer, Authorizers, PayloadSignatures, EnvelopeSignatures>, SendTransactionResponse>,
Sends a transaction over the network.
pub fn transaction_result_by_id<'grpc, Id>(
&'grpc mut self,
id: Id
) -> Pin<Box<dyn Future<Output = Result<TransactionResultResponse, Inner::Error>> + 'grpc>> where
Inner: GrpcClient<GetTransactionRequest<Id>, TransactionResultResponse>,
pub fn transaction_result_by_id<'grpc, Id>(
&'grpc mut self,
id: Id
) -> Pin<Box<dyn Future<Output = Result<TransactionResultResponse, Inner::Error>> + 'grpc>> where
Inner: GrpcClient<GetTransactionRequest<Id>, TransactionResultResponse>,
Retrieves a transaction’s result by its ID.
pub fn transaction_by_id<'grpc, Id>(
&'grpc mut self,
id: Id
) -> Map<Pin<Box<dyn Future<Output = Result<TransactionResponse, Inner::Error>> + 'grpc>>, fn(_: Result<TransactionResponse, Inner::Error>) -> Result<TransactionD, Inner::Error>> where
Inner: GrpcClient<GetTransactionRequest<Id>, TransactionResponse>,
pub fn transaction_by_id<'grpc, Id>(
&'grpc mut self,
id: Id
) -> Map<Pin<Box<dyn Future<Output = Result<TransactionResponse, Inner::Error>> + 'grpc>>, fn(_: Result<TransactionResponse, Inner::Error>) -> Result<TransactionD, Inner::Error>> where
Inner: GrpcClient<GetTransactionRequest<Id>, TransactionResponse>,
Retrieves a transaction by its ID.
pub fn account_at_latest_block<'grpc, Addr>(
&'grpc mut self,
address: Addr
) -> Map<Pin<Box<dyn Future<Output = Result<AccountResponse, Inner::Error>> + 'grpc>>, fn(_: Result<AccountResponse, Inner::Error>) -> Result<Account, Inner::Error>> where
Inner: GrpcClient<GetAccountAtLatestBlockRequest<Addr>, AccountResponse>,
pub fn account_at_latest_block<'grpc, Addr>(
&'grpc mut self,
address: Addr
) -> Map<Pin<Box<dyn Future<Output = Result<AccountResponse, Inner::Error>> + 'grpc>>, fn(_: Result<AccountResponse, Inner::Error>) -> Result<Account, Inner::Error>> where
Inner: GrpcClient<GetAccountAtLatestBlockRequest<Addr>, AccountResponse>,
Retrieves information about an account at the latest block.
Examples found in repository
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let stdin = stdin();
let mut stdin = stdin.lock();
let mut buf = String::new();
println!("Enter the account's address:");
stdin.read_line(&mut buf)?;
let addr = buf.trim();
let address: AddressOwned = addr.parse()?;
let mut net = TonicHyperFlowClient::mainnet().await?;
let account = net.account_at_latest_block(&address.data).await?;
let latest_block_height = net.latest_block_header(Seal::Sealed).await?.height;
let account1 = net
.account_at_block_height(&address.data, latest_block_height)
.await?;
println!("{:#?}", account);
assert_eq!(account, account1);
Ok(())
}
More examples
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::mainnet().await?;
client.ping().await?;
let latest_block_height = client.latest_block_header(Seal::Sealed).await?.height;
let start_height = latest_block_height - 100;
println!(
"Searching for accounts created within the last 100 blocks ({}-{})...",
start_height, latest_block_height
);
let mut accounts = Vec::new();
for events in client
.events_for_height_range("flow.AccountCreated", start_height, latest_block_height)
.await?
.results
.iter()
{
for event in events.events.iter() {
let val: cadence_json::ValueOwned = serde_json::from_slice(&event.payload)?;
if let cadence_json::ValueOwned::Event(c) = val {
for field in c.fields.into_iter().filter(|f| f.name == "address") {
if let cadence_json::ValueOwned::Address(addr) = field.value {
accounts.push(client.account_at_latest_block(&addr.data).await?);
}
}
}
}
}
println!("Found {} accounts.", accounts.len());
if let Some(acc) = accounts.into_iter().max_by_key(|acc| acc.balance) {
println!(
"\nThe richest account is 0x{} with a balance of {}.",
hex::encode(acc.address),
acc.balance
);
}
Ok(())
}
pub fn account_at_block_height<'grpc, Addr>(
&'grpc mut self,
address: Addr,
block_height: u64
) -> Map<Pin<Box<dyn Future<Output = Result<AccountResponse, Inner::Error>> + 'grpc>>, fn(_: Result<AccountResponse, Inner::Error>) -> Result<Account, Inner::Error>> where
Inner: GrpcClient<GetAccountAtBlockHeightRequest<Addr>, AccountResponse>,
pub fn account_at_block_height<'grpc, Addr>(
&'grpc mut self,
address: Addr,
block_height: u64
) -> Map<Pin<Box<dyn Future<Output = Result<AccountResponse, Inner::Error>> + 'grpc>>, fn(_: Result<AccountResponse, Inner::Error>) -> Result<Account, Inner::Error>> where
Inner: GrpcClient<GetAccountAtBlockHeightRequest<Addr>, AccountResponse>,
Retrieves information about an account at the specified block height.
Examples found in repository
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let stdin = stdin();
let mut stdin = stdin.lock();
let mut buf = String::new();
println!("Enter the account's address:");
stdin.read_line(&mut buf)?;
let addr = buf.trim();
let address: AddressOwned = addr.parse()?;
let mut net = TonicHyperFlowClient::mainnet().await?;
let account = net.account_at_latest_block(&address.data).await?;
let latest_block_height = net.latest_block_header(Seal::Sealed).await?.height;
let account1 = net
.account_at_block_height(&address.data, latest_block_height)
.await?;
println!("{:#?}", account);
assert_eq!(account, account1);
Ok(())
}
pub fn latest_block_header<'grpc>(
&'grpc mut self,
seal: Seal
) -> Map<Pin<Box<dyn Future<Output = Result<BlockHeaderResponse, Inner::Error>> + 'grpc>>, fn(_: Result<BlockHeaderResponse, Inner::Error>) -> Result<BlockHeader, Inner::Error>> where
Inner: GrpcClient<GetLatestBlockHeaderRequest, BlockHeaderResponse>,
pub fn latest_block_header<'grpc>(
&'grpc mut self,
seal: Seal
) -> Map<Pin<Box<dyn Future<Output = Result<BlockHeaderResponse, Inner::Error>> + 'grpc>>, fn(_: Result<BlockHeaderResponse, Inner::Error>) -> Result<BlockHeader, Inner::Error>> where
Inner: GrpcClient<GetLatestBlockHeaderRequest, BlockHeaderResponse>,
Retrieves header information of the latest block.
Examples found in repository
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let stdin = stdin();
let mut stdin = stdin.lock();
let mut buf = String::new();
println!("Enter the account's address:");
stdin.read_line(&mut buf)?;
let addr = buf.trim();
let address: AddressOwned = addr.parse()?;
let mut net = TonicHyperFlowClient::mainnet().await?;
let account = net.account_at_latest_block(&address.data).await?;
let latest_block_height = net.latest_block_header(Seal::Sealed).await?.height;
let account1 = net
.account_at_block_height(&address.data, latest_block_height)
.await?;
println!("{:#?}", account);
assert_eq!(account, account1);
Ok(())
}
More examples
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut net = TonicHyperFlowClient::testnet().await?;
let _ = net.ping().await?;
let latest_block_header = net.latest_block_header(Seal::Sealed).await?;
println!("{:?}", latest_block_header);
let Timestamp { nanos, seconds } = latest_block_header.timestamp;
println!("{}", Utc.timestamp(seconds, nanos as u32));
println!("----------------");
let block_header2 = net
.block_header_by_height(latest_block_header.height)
.await?;
println!("{:?}", block_header2);
let Timestamp { nanos, seconds } = block_header2.timestamp;
println!("{}", Utc.timestamp(seconds, nanos as u32));
assert_eq!(latest_block_header, block_header2);
Ok(())
}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::mainnet().await?;
client.ping().await?;
let latest_block_height = client.latest_block_header(Seal::Sealed).await?.height;
let start_height = latest_block_height - 20;
println!(
"Searching for accounts created within the last 20 blocks ({}-{})...",
start_height, latest_block_height
);
for events in client
.events_for_height_range("flow.AccountCreated", start_height, latest_block_height)
.await?
.results
.iter()
{
if events.events.is_empty() {
continue;
}
println!(
"\nBlock #{} ({}):",
events.block_height,
hex::encode(&events.block_id)
);
for event in events.events.iter() {
let val = event.parse_payload()?;
println!(" - {:#?}", val);
}
}
Ok(())
}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::mainnet().await?;
client.ping().await?;
let latest_block_height = client.latest_block_header(Seal::Sealed).await?.height;
let start_height = latest_block_height - 100;
println!(
"Searching for accounts created within the last 100 blocks ({}-{})...",
start_height, latest_block_height
);
let mut accounts = Vec::new();
for events in client
.events_for_height_range("flow.AccountCreated", start_height, latest_block_height)
.await?
.results
.iter()
{
for event in events.events.iter() {
let val: cadence_json::ValueOwned = serde_json::from_slice(&event.payload)?;
if let cadence_json::ValueOwned::Event(c) = val {
for field in c.fields.into_iter().filter(|f| f.name == "address") {
if let cadence_json::ValueOwned::Address(addr) = field.value {
accounts.push(client.account_at_latest_block(&addr.data).await?);
}
}
}
}
}
println!("Found {} accounts.", accounts.len());
if let Some(acc) = accounts.into_iter().max_by_key(|acc| acc.balance) {
println!(
"\nThe richest account is 0x{} with a balance of {}.",
hex::encode(acc.address),
acc.balance
);
}
Ok(())
}
pub fn block_header_by_height<'grpc>(
&'grpc mut self,
height: u64
) -> Map<Pin<Box<dyn Future<Output = Result<BlockHeaderResponse, Inner::Error>> + 'grpc>>, fn(_: Result<BlockHeaderResponse, Inner::Error>) -> Result<BlockHeader, Inner::Error>> where
Inner: GrpcClient<GetBlockHeaderByHeightRequest, BlockHeaderResponse>,
pub fn block_header_by_height<'grpc>(
&'grpc mut self,
height: u64
) -> Map<Pin<Box<dyn Future<Output = Result<BlockHeaderResponse, Inner::Error>> + 'grpc>>, fn(_: Result<BlockHeaderResponse, Inner::Error>) -> Result<BlockHeader, Inner::Error>> where
Inner: GrpcClient<GetBlockHeaderByHeightRequest, BlockHeaderResponse>,
Retrieves header information of a block specified by its height.
Examples found in repository
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut net = TonicHyperFlowClient::testnet().await?;
let _ = net.ping().await?;
let latest_block_header = net.latest_block_header(Seal::Sealed).await?;
println!("{:?}", latest_block_header);
let Timestamp { nanos, seconds } = latest_block_header.timestamp;
println!("{}", Utc.timestamp(seconds, nanos as u32));
println!("----------------");
let block_header2 = net
.block_header_by_height(latest_block_header.height)
.await?;
println!("{:?}", block_header2);
let Timestamp { nanos, seconds } = block_header2.timestamp;
println!("{}", Utc.timestamp(seconds, nanos as u32));
assert_eq!(latest_block_header, block_header2);
Ok(())
}
pub fn block_header_by_id<'grpc, Id>(
&'grpc mut self,
id: Id
) -> Map<Pin<Box<dyn Future<Output = Result<BlockHeaderResponse, Inner::Error>> + 'grpc>>, fn(_: Result<BlockHeaderResponse, Inner::Error>) -> Result<BlockHeader, Inner::Error>> where
Inner: GrpcClient<GetBlockHeaderByIdRequest<Id>, BlockHeaderResponse>,
pub fn block_header_by_id<'grpc, Id>(
&'grpc mut self,
id: Id
) -> Map<Pin<Box<dyn Future<Output = Result<BlockHeaderResponse, Inner::Error>> + 'grpc>>, fn(_: Result<BlockHeaderResponse, Inner::Error>) -> Result<BlockHeader, Inner::Error>> where
Inner: GrpcClient<GetBlockHeaderByIdRequest<Id>, BlockHeaderResponse>,
Retrieves header information of a block specified by its ID.
pub fn latest_block<'grpc>(
&'grpc mut self,
seal: Seal
) -> Map<Pin<Box<dyn Future<Output = Result<BlockResponse, Inner::Error>> + 'grpc>>, fn(_: Result<BlockResponse, Inner::Error>) -> Result<Block, Inner::Error>> where
Inner: GrpcClient<GetLatestBlockRequest, BlockResponse>,
pub fn latest_block<'grpc>(
&'grpc mut self,
seal: Seal
) -> Map<Pin<Box<dyn Future<Output = Result<BlockResponse, Inner::Error>> + 'grpc>>, fn(_: Result<BlockResponse, Inner::Error>) -> Result<Block, Inner::Error>> where
Inner: GrpcClient<GetLatestBlockRequest, BlockResponse>,
Retrieves full information of the latest block.
Examples found in repository
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::testnet().await?;
client.ping().await?;
let latest_block = client.latest_block(Seal::Sealed).await?;
let block_by_id = client.block_by_id(&latest_block.id).await?;
let block_by_height = client.block_by_height(latest_block.height).await?;
assert_eq!(latest_block, block_by_id);
assert_eq!(latest_block, block_by_height);
println!("OK: {:#?}", latest_block);
Ok(())
}
More examples
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::mainnet().await?;
client.ping().await?;
let mut latest_block = client.latest_block(Seal::Sealed).await?;
// traverse latest blocks until we find a collection guarantee.
let collection_guarrantee = loop {
if latest_block.collection_guarantees.is_empty() {
// Go to the next block
latest_block = client.block_by_id(&latest_block.parent_id).await?;
} else {
break latest_block.collection_guarantees.pop().unwrap();
}
};
// retrieve the collection by id.
let collection = client
.collection_by_id(&collection_guarrantee.collection_id)
.await?;
println!("OK: {:#?}", collection);
Ok(())
}
pub fn block_by_height<'grpc>(
&'grpc mut self,
height: u64
) -> Map<Pin<Box<dyn Future<Output = Result<BlockResponse, Inner::Error>> + 'grpc>>, fn(_: Result<BlockResponse, Inner::Error>) -> Result<Block, Inner::Error>> where
Inner: GrpcClient<GetBlockByHeightRequest, BlockResponse>,
pub fn block_by_height<'grpc>(
&'grpc mut self,
height: u64
) -> Map<Pin<Box<dyn Future<Output = Result<BlockResponse, Inner::Error>> + 'grpc>>, fn(_: Result<BlockResponse, Inner::Error>) -> Result<Block, Inner::Error>> where
Inner: GrpcClient<GetBlockByHeightRequest, BlockResponse>,
Retrieves full information of a block specified by its height.
Examples found in repository
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::testnet().await?;
client.ping().await?;
let latest_block = client.latest_block(Seal::Sealed).await?;
let block_by_id = client.block_by_id(&latest_block.id).await?;
let block_by_height = client.block_by_height(latest_block.height).await?;
assert_eq!(latest_block, block_by_id);
assert_eq!(latest_block, block_by_height);
println!("OK: {:#?}", latest_block);
Ok(())
}
pub fn block_by_id<'grpc, Id>(
&'grpc mut self,
id: Id
) -> Map<Pin<Box<dyn Future<Output = Result<BlockResponse, Inner::Error>> + 'grpc>>, fn(_: Result<BlockResponse, Inner::Error>) -> Result<Block, Inner::Error>> where
Inner: GrpcClient<GetBlockByIdRequest<Id>, BlockResponse>,
pub fn block_by_id<'grpc, Id>(
&'grpc mut self,
id: Id
) -> Map<Pin<Box<dyn Future<Output = Result<BlockResponse, Inner::Error>> + 'grpc>>, fn(_: Result<BlockResponse, Inner::Error>) -> Result<Block, Inner::Error>> where
Inner: GrpcClient<GetBlockByIdRequest<Id>, BlockResponse>,
Retrieves full information of a block specified by its ID.
Examples found in repository
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::testnet().await?;
client.ping().await?;
let latest_block = client.latest_block(Seal::Sealed).await?;
let block_by_id = client.block_by_id(&latest_block.id).await?;
let block_by_height = client.block_by_height(latest_block.height).await?;
assert_eq!(latest_block, block_by_id);
assert_eq!(latest_block, block_by_height);
println!("OK: {:#?}", latest_block);
Ok(())
}
More examples
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::mainnet().await?;
client.ping().await?;
let mut latest_block = client.latest_block(Seal::Sealed).await?;
// traverse latest blocks until we find a collection guarantee.
let collection_guarrantee = loop {
if latest_block.collection_guarantees.is_empty() {
// Go to the next block
latest_block = client.block_by_id(&latest_block.parent_id).await?;
} else {
break latest_block.collection_guarantees.pop().unwrap();
}
};
// retrieve the collection by id.
let collection = client
.collection_by_id(&collection_guarrantee.collection_id)
.await?;
println!("OK: {:#?}", collection);
Ok(())
}
pub fn collection_by_id<'grpc, Id>(
&'grpc mut self,
id: Id
) -> Map<Pin<Box<dyn Future<Output = Result<CollectionResponse, Inner::Error>> + 'grpc>>, fn(_: Result<CollectionResponse, Inner::Error>) -> Result<Collection, Inner::Error>> where
Inner: GrpcClient<GetCollectionByIdRequest<Id>, CollectionResponse>,
pub fn collection_by_id<'grpc, Id>(
&'grpc mut self,
id: Id
) -> Map<Pin<Box<dyn Future<Output = Result<CollectionResponse, Inner::Error>> + 'grpc>>, fn(_: Result<CollectionResponse, Inner::Error>) -> Result<Collection, Inner::Error>> where
Inner: GrpcClient<GetCollectionByIdRequest<Id>, CollectionResponse>,
Retrieves information of a collection specified by its ID.
Examples found in repository
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::mainnet().await?;
client.ping().await?;
let mut latest_block = client.latest_block(Seal::Sealed).await?;
// traverse latest blocks until we find a collection guarantee.
let collection_guarrantee = loop {
if latest_block.collection_guarantees.is_empty() {
// Go to the next block
latest_block = client.block_by_id(&latest_block.parent_id).await?;
} else {
break latest_block.collection_guarantees.pop().unwrap();
}
};
// retrieve the collection by id.
let collection = client
.collection_by_id(&collection_guarrantee.collection_id)
.await?;
println!("OK: {:#?}", collection);
Ok(())
}
Connects to a static endpoint URI.
Connects to the Mainnet access node provided by Dapper Labs.
Examples found in repository
More examples
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::mainnet().await?;
client.ping().await?;
let ret = client
.execute_script_at_latest_block(
SIMPLE_SCRIPT,
[ValueRef::Int(cadence_json::BigInt::from(32))],
)
.await?
.parse()?;
println!("{:#?}", ret);
let ret = client
.execute_script_at_latest_block(COMPLEX_SCRIPT, [ValueRef::String("John Doe")])
.await?
.parse()?;
println!("{:#?}", ret);
Ok(())
}
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let stdin = stdin();
let mut stdin = stdin.lock();
let mut buf = String::new();
println!("Enter the account's address:");
stdin.read_line(&mut buf)?;
let addr = buf.trim();
let address: AddressOwned = addr.parse()?;
let mut net = TonicHyperFlowClient::mainnet().await?;
let account = net.account_at_latest_block(&address.data).await?;
let latest_block_height = net.latest_block_header(Seal::Sealed).await?.height;
let account1 = net
.account_at_block_height(&address.data, latest_block_height)
.await?;
println!("{:#?}", account);
assert_eq!(account, account1);
Ok(())
}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::mainnet().await?;
client.ping().await?;
let mut latest_block = client.latest_block(Seal::Sealed).await?;
// traverse latest blocks until we find a collection guarantee.
let collection_guarrantee = loop {
if latest_block.collection_guarantees.is_empty() {
// Go to the next block
latest_block = client.block_by_id(&latest_block.parent_id).await?;
} else {
break latest_block.collection_guarantees.pop().unwrap();
}
};
// retrieve the collection by id.
let collection = client
.collection_by_id(&collection_guarrantee.collection_id)
.await?;
println!("OK: {:#?}", collection);
Ok(())
}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::mainnet().await?;
client.ping().await?;
let latest_block_height = client.latest_block_header(Seal::Sealed).await?.height;
let start_height = latest_block_height - 20;
println!(
"Searching for accounts created within the last 20 blocks ({}-{})...",
start_height, latest_block_height
);
for events in client
.events_for_height_range("flow.AccountCreated", start_height, latest_block_height)
.await?
.results
.iter()
{
if events.events.is_empty() {
continue;
}
println!(
"\nBlock #{} ({}):",
events.block_height,
hex::encode(&events.block_id)
);
for event in events.events.iter() {
let val = event.parse_payload()?;
println!(" - {:#?}", val);
}
}
Ok(())
}
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::mainnet().await?;
client.ping().await?;
let latest_block_height = client.latest_block_header(Seal::Sealed).await?.height;
let start_height = latest_block_height - 100;
println!(
"Searching for accounts created within the last 100 blocks ({}-{})...",
start_height, latest_block_height
);
let mut accounts = Vec::new();
for events in client
.events_for_height_range("flow.AccountCreated", start_height, latest_block_height)
.await?
.results
.iter()
{
for event in events.events.iter() {
let val: cadence_json::ValueOwned = serde_json::from_slice(&event.payload)?;
if let cadence_json::ValueOwned::Event(c) = val {
for field in c.fields.into_iter().filter(|f| f.name == "address") {
if let cadence_json::ValueOwned::Address(addr) = field.value {
accounts.push(client.account_at_latest_block(&addr.data).await?);
}
}
}
}
}
println!("Found {} accounts.", accounts.len());
if let Some(acc) = accounts.into_iter().max_by_key(|acc| acc.balance) {
println!(
"\nThe richest account is 0x{} with a balance of {}.",
hex::encode(acc.address),
acc.balance
);
}
Ok(())
}
Connects to the Testnet access node provided by Dapper Labs.
Examples found in repository
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut client = TonicHyperFlowClient::testnet().await?;
client.ping().await?;
let latest_block = client.latest_block(Seal::Sealed).await?;
let block_by_id = client.block_by_id(&latest_block.id).await?;
let block_by_height = client.block_by_height(latest_block.height).await?;
assert_eq!(latest_block, block_by_id);
assert_eq!(latest_block, block_by_height);
println!("OK: {:#?}", latest_block);
Ok(())
}
More examples
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let mut net = TonicHyperFlowClient::testnet().await?;
let _ = net.ping().await?;
let latest_block_header = net.latest_block_header(Seal::Sealed).await?;
println!("{:?}", latest_block_header);
let Timestamp { nanos, seconds } = latest_block_header.timestamp;
println!("{}", Utc.timestamp(seconds, nanos as u32));
println!("----------------");
let block_header2 = net
.block_header_by_height(latest_block_header.height)
.await?;
println!("{:?}", block_header2);
let Timestamp { nanos, seconds } = block_header2.timestamp;
println!("{}", Utc.timestamp(seconds, nanos as u32));
assert_eq!(latest_block_header, block_header2);
Ok(())
}
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
let stdin = stdin();
let mut stdin = stdin.lock();
let mut buf = String::new();
// Let's make a transaction!
// First, generate a keypair for us to use.
let secp = Secp256k1::signing_only();
// `EntropyRng` is a secure random number generator.
let mut rng = secp256k1::rand::rngs::EntropyRng::new();
let secret_key = SecretKey::new(&mut rng);
let public_key = PublicKey::from_secret_key(&secp, &secret_key);
// Print the public key as a hexadecimal.
println!("This is your public key:");
let public_key_bytes = public_key.serialize_uncompressed();
// There is a leading 0x04 which we don't need to emit
// https://bitcoin.stackexchange.com/a/3043
for ch in &public_key_bytes[1..] {
print!("{:02x}", *ch);
}
println!();
println!("Go to https://flow-faucet.vercel.app/, select secp256k1 and sha3 and create your testnet account.");
println!("Paste the address you get and press ENTER to continue");
stdin.read_line(&mut buf)?;
let addr = buf.trim();
let address: AddressOwned = addr.parse()?;
let net = TonicHyperFlowClient::testnet().await?;
let mut account = Account::<_, _>::new(net, &address.data, secret_key).await?;
let create_account = CreateAccountTransaction {
public_keys: &[public_key],
};
let create_account_header = create_account.to_header::<_, tiny_keccak::Sha3>(account.signer());
let res = account
.send_transaction_header(&create_account_header)
.await?;
println!(
"Just made {} to create another account :p",
hex::encode(&res.id)
);
let response = res.finalize(account.client()).await?;
match response {
Some(res) => {
for ev in res.events.iter() {
if ev.ty == "flow.AccountCreated" {
let payload = ev.parse_payload()?;
let addr = payload
.fields
.into_iter()
.find(|field| field.name == "address")
.map(|field| field.value)
.unwrap();
if let ValueOwned::Address(addr) = addr {
println!("Created account's address is: {}", addr);
}
}
}
}
None => {
panic!("The transaction did not get sealed... Perhaps the network is malfunctioning?")
}
}
Ok(())
}
Connects to a static endpoint URI. Does not connect until we try to send a request.
Note: You must have entered the tokio runtime context before calling this function.
You can do so by writing the code down below, or it will automatically be entered, if
you have an .await
before calling this. Consider using the async
functions instead.
let handle = tokio::runtime::Handle::current();
handle.enter();
Connects to an endpoint. Does not connect until we try to send a request.
Note: You must have entered the tokio runtime context before calling this function.
You can do so by writing the code down below, or it will automatically be entered, if
you have an .await
before calling this. Consider using the async
functions instead.
let handle = tokio::runtime::Handle::current();
handle.enter();
Builds a lazy connection to the Mainnet access node provided by Dapper Labs.
Note: You must have entered the tokio runtime context before calling this function.
You can do so by writing the code down below, or it will automatically be entered, if
you have an .await
before calling this. Consider using the async
functions instead.
let handle = tokio::runtime::Handle::current();
handle.enter();
Builds a lazy connection to the Testnet access node provided by Dapper Labs.
Note: You must have entered the tokio runtime context before calling this function.
You can do so by writing the code down below, or it will automatically be entered, if
you have an .await
before calling this. Consider using the async
functions instead.
let handle = tokio::runtime::Handle::current();
handle.enter();
Trait Implementations
Returns the “default value” for a type. Read more
Auto Trait Implementations
impl<T> RefUnwindSafe for FlowClient<T> where
T: RefUnwindSafe,
impl<T> Send for FlowClient<T> where
T: Send,
impl<T> Sync for FlowClient<T> where
T: Sync,
impl<T> Unpin for FlowClient<T> where
T: Unpin,
impl<T> UnwindSafe for FlowClient<T> where
T: UnwindSafe,
Blanket Implementations
Mutably borrows from an owned value. Read more
Wrap the input message T
in a tonic::Request
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more