Struct safe_network::client::client_api::Client[][src]

pub struct Client { /* fields omitted */ }
Expand description

Client object

Implementations

Read the contents of a blob from the network. The contents might be spread across different chunks in the network. This function invokes the self-encryptor and returns the data that was initially stored.

Takes position and len arguments which specify the start position and the length of bytes to be read. Passing None to position reads the data from the beginning. Passing None to length reads the full length of the data.

Examples

Get data

use safe_network::client::Client;
use safe_network::types::ChunkAddress;
use xor_name::XorName;
let head_chunk = ChunkAddress::Public(XorName::random());
let client = Client::new(None, None, bootstrap_contacts).await?;

// grab the random head of the blob from the network
let _data = client.read_blob(head_chunk, None, None).await?;

Store data in public chunks on the network.

This performs self encrypt on the data itself and returns a single address pointing to the head chunk of the blob, and with which the data can be read. It performs data storage as well as all necessary payment validation and checks against the client’s AT2 actor.

Examples

Store data

use safe_network::client::Client;
use safe_network::types::Token;
use std::str::FromStr;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let mut client = Client::new(None, None, bootstrap_contacts).await?;
let data = b"some data".to_vec();
// grab the random head of the blob from the network
let _address = client.store_public_blob(&data).await?;

Store data in private chunks on the network.

This performs self encrypt on the data itself and returns a single address pointing to the head chunk of the blob, and with which the data can be read. It performs data storage as well as all necessary payment validation and checks against the client’s AT2 actor.

Examples

Store data

use safe_network::client::Client;
use safe_network::types::Token;
use std::str::FromStr;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let mut client = Client::new(None, None, bootstrap_contacts).await?;
let data = b"some data".to_vec();
// grab the random head of the blob from the network
let fetched_data = client.store_private_blob(&data).await?;

println!( "{:?}", fetched_data ); // prints "some data"

Delete blob can only be performed on private chunks. But on those private chunks this will remove the data from the network.

Examples

Remove data

use safe_network::client::Client;
use safe_network::types::Token;
use std::str::FromStr;

// Let's use an existing client, with a pre-existing balance to be used for write payments.
let mut client = Client::new(None, None, bootstrap_contacts).await?;
let data = b"some private data".to_vec();
let address = client.store_private_blob(&data).await?;

let _ = client.delete_blob(address).await?;

// Now when we attempt to retrieve the blob, we should get an error

match client.read_blob(address, None, None).await {
    Err(error) => eprintln!("Expected error getting blob {:?}", error),
    _ => return Err(anyhow::anyhow!("Should not have been able to retrieve this blob"))
};

Uses self_encryption to generate an encrypted Blob serialized data map, without connecting and/or writing to the network.

Store a new sequenced Map

Examples

use safe_network::client::Client;
use safe_network::types::{ Keypair, Token, MapAction, MapPermissionSet, MapSeqValue, MapSeqEntries};
use rand::rngs::OsRng;
use std::collections::BTreeMap;
use xor_name::XorName;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let id = Keypair::new_ed25519(&mut OsRng);
let mut client = Client::new(Some(id), None, bootstrap_contacts).await?;
let name = XorName::random();
let tag = 15001;
let mut entries = MapSeqEntries::default();
let mut permissions = BTreeMap::default();
let permission_set = MapPermissionSet::new().allow(MapAction::Read);
let _ = permissions.insert(client.public_key(), permission_set);
let _ = entries.insert(b"key".to_vec(), MapSeqValue { data: b"value".to_vec(), version: 0 });
let owner = client.public_key();
let _ = client.store_seq_map(name, tag, owner, Some(entries), Some(permissions)).await?;

Store a new unsequenced Map

Examples

use safe_network::client::Client;
use safe_network::types::{ Keypair, Token, MapAction, MapPermissionSet, MapUnseqEntries};
use rand::rngs::OsRng;
use std::collections::BTreeMap;
use xor_name::XorName;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let id = Keypair::new_ed25519(&mut OsRng);
let mut client = Client::new(Some(id), None, bootstrap_contacts).await?;
let name = XorName::random();
let tag = 15001;
let mut entries = MapUnseqEntries::default();
let mut permissions = BTreeMap::default();
let permission_set = MapPermissionSet::new().allow(MapAction::Read);
let _ = permissions.insert(client.public_key(), permission_set);
let _ = entries.insert(b"key".to_vec(), b"value".to_vec());
let owner = client.public_key();
let _ = client.store_unseq_map(name, tag, owner, Some(entries), Some(permissions)).await?;

Delete Map

Examples

use safe_network::client::Client;
use safe_network::types::{ Keypair, Token, MapAction, MapPermissionSet, MapUnseqEntries};
use rand::rngs::OsRng;
use std::collections::BTreeMap;
use xor_name::XorName;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let id = Keypair::new_ed25519(&mut OsRng);
let mut client = Client::new(Some(id), None, bootstrap_contacts).await?;
let name = XorName::random();
let tag = 15001;
let mut entries = MapUnseqEntries::default();
let mut permissions = BTreeMap::default();
let permission_set = MapPermissionSet::new().allow(MapAction::Read);
let _ = permissions.insert(client.public_key(), permission_set);
let _ = entries.insert(b"key".to_vec(), b"value".to_vec());
let owner = client.public_key();
let address = client.store_unseq_map(name, tag, owner, Some(entries.clone()), Some(permissions)).await?;
let _ = client.delete_map(address).await?;

Delete map user permission

Set map user permissions

Mutate map user entries

Fetch map data from the network

Examples

use safe_network::client::Client;
use safe_network::types::{ Keypair, Token, MapAction, MapPermissionSet, MapUnseqEntries};
use std::collections::BTreeMap;
use xor_name::XorName;
use rand::rngs::OsRng;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let id = Keypair::new_ed25519(&mut OsRng);
let mut client = Client::new(Some(id), None, bootstrap_contacts).await?;
let name = XorName::random();
let tag = 15001;
let mut entries = MapUnseqEntries::default();
let mut permissions = BTreeMap::default();
let permission_set = MapPermissionSet::new().allow(MapAction::Read);
let _ = permissions.insert(client.public_key(), permission_set);
let _ = entries.insert(b"key".to_vec(), b"value".to_vec());
let owner = client.public_key();
let address = client.store_unseq_map(name, tag, owner, Some(entries.clone()), Some(permissions)).await?;
let _ = client.get_map(address).await?;

Fetch the value for a given key in a map

Examples

use safe_network::client::Client;
use safe_network::types::{ Keypair, Token, MapAction, MapValue, MapPermissionSet, MapUnseqEntries};
use std::collections::BTreeMap;
use xor_name::XorName;
use rand::rngs::OsRng;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let id = Keypair::new_ed25519(&mut OsRng);
let mut client = Client::new(Some(id), None, bootstrap_contacts).await?;
let name = XorName::random();
let tag = 15001;
let mut entries = MapUnseqEntries::default();
let _ = entries.insert(b"beep".to_vec(), b"boop".to_vec() );
let mut permissions = BTreeMap::default();
let permission_set = MapPermissionSet::new().allow(MapAction::Read);
let _ = permissions.insert(client.public_key(), permission_set);
let _ = entries.insert(b"key".to_vec(), b"value".to_vec());
let owner = client.public_key();
let address = client.store_unseq_map(name, tag, owner, Some(entries.clone()), Some(permissions)).await?;
let received_value = match client.get_map_value(address, b"beep".to_vec()).await? {
    MapValue::Unseq(value) => value,
    _ => panic!("Exptected an unsequenced map")
};
assert_eq!(received_value, b"boop".to_vec());

Get a shell (bare bones) version of Map from the network.

Get a current version of Map from the network.

Examples

use safe_network::client::Client;
use safe_network::types::{ Keypair, Token, MapAction, MapPermissionSet, MapUnseqEntries};
use rand::rngs::OsRng;
use std::collections::BTreeMap;
use xor_name::XorName;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let id = Keypair::new_ed25519(&mut OsRng);
let mut client = Client::new(Some(id), None, bootstrap_contacts).await?;
let name = XorName::random();
let tag = 15001;
let mut entries = MapUnseqEntries::default();
let _ = entries.insert(b"beep".to_vec(), b"boop".to_vec() );
let mut permissions = BTreeMap::default();
let permission_set = MapPermissionSet::new().allow(MapAction::Read);
let _ = permissions.insert(client.public_key(), permission_set);
let _ = entries.insert(b"key".to_vec(), b"value".to_vec());
let owner = client.public_key();
let address = client.store_unseq_map(name, tag, owner, Some(entries.clone()), Some(permissions)).await?;
let version = client.get_map_version(address).await?;
assert_eq!(version, 0);

Mutates sequenced Map entries in bulk

Mutates unsequenced Map entries in bulk

Return a complete list of entries in Map.

Return a complete list of entries in Map.

Return a list of keys in Map stored on the network.

Return a list of values in a Sequenced Mutable Data

Returns a list of values in an Unsequenced Mutable Data

Return the permissions set for a particular user

Return a list of permissions in Map stored on the network.

Updates or inserts a permissions set for a user

Updates or inserts a permissions set for a user

Sends an ownership transfer request.

Create a Private Register onto the Network

Creates a private Register on the network which can then be written to. Private data can be removed from the network at a later date.

A tag must be supplied. A xorname must be supplied, this can be random or deterministic as per your apps needs.

Create a Public Register onto the Network

Creates a public Register on the network which can then be written to. Public data can not be removed from the network at a later date.

A tag must be supplied. A xorname must be supplied, this can be random or deterministic as per your apps needs.

Delete Register

You’re only able to delete a PrivateRegister. Public data can no be removed from the network.

Write to Register

Public or private isn’t important for writing, though the data you write will be Public or Private according to the type of the targeted Register.

Get a Register from the Network

Get the last data entry from a Register data.

pub async fn get_register_entry(
    &self,
    address: Address,
    hash: EntryHash
) -> Result<Entry, Error>

Get an entry from a Register on the Network by its hash

Get the owner of a Register.

Get the set of Permissions in a Register for a specific user.

Get the Policy of a Register.

Create Private Sequence Data on to the Network

Creates a private sequence on the network which can be appended to. Private data can be removed from the network at a later date.

A tag must be supplied. A xorname must be supplied, this can be random or deterministic as per your apps needs.

Examples

Store data

use safe_network::client::Client;
use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions};
use std::collections::BTreeMap;
use xor_name::XorName;
use rand::rngs::OsRng;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let id = Keypair::new_ed25519(&mut OsRng);
let mut client = Client::new(Some(id), None, bootstrap_contacts).await?;
let name = XorName::random();
let tag = 10;
let owner = client.public_key();
let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new();

// Set the access permissions
let _ = perms.insert(
   owner,
   SequencePrivatePermissions::new(true, true),
);

// The returned address can then be used to `append` data to.
let _address = client.store_private_sequence(None, name, tag, owner, perms).await?;

Create Public Sequence Data into the Network

Creates a public sequence on the network which can be appended to. Public data can not be removed from the network at a later date.

A tag must be supplied. A xorname must be supplied, this can be random or deterministic as per your apps needs.

Examples

Store data

use safe_network::client::Client;
use safe_network::types::{Keypair, SequenceUser, Token, SequencePublicPermissions};
use std::collections::BTreeMap;
use xor_name::XorName;
use rand::rngs::OsRng;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let id = Keypair::new_ed25519(&mut OsRng);
let mut client = Client::new(Some(id), None, bootstrap_contacts).await?;
let name = XorName::random();
let tag = 10;
let owner = client.public_key();
let mut perms = BTreeMap::<SequenceUser, SequencePublicPermissions>::new();

// Set the access permissions
let _ = perms.insert(
   SequenceUser::Key(owner),
   SequencePublicPermissions::new(true),
);

// The returned address can then be used to `append` data to.
let _address = client.store_public_sequence(None, name, tag, owner, perms).await?;

Delete sequence

You’re only able to delete a PrivateSequence. Public data can no be removed from the network.

Examples

Delete data

use safe_network::client::Client;
use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions};
use std::collections::BTreeMap;
use xor_name::XorName;
use rand::rngs::OsRng;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let id = Keypair::new_ed25519(&mut OsRng);
let mut client = Client::new(Some(id), None, bootstrap_contacts).await?;
let name = XorName::random();
let tag = 10;
let owner = client.public_key();
let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new();

// Set the access permissions
let _ = perms.insert(
   owner,
   SequencePrivatePermissions::new(true, true),
);

// The returned address can then be used to `append` data to.
let address = client.store_private_sequence(None, name, tag, owner, perms).await?;

client.delete_sequence(address).await?;

Append to Sequence

Public or private isn’t important for append. You can append to either (though the data you append will be Public or Private).

Examples

use safe_network::client::Client;
use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions};
use std::collections::BTreeMap;
use xor_name::XorName;
use rand::rngs::OsRng;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let id = Keypair::new_ed25519(&mut OsRng);
let mut client = Client::new(Some(id), None, bootstrap_contacts).await?;
let name = XorName::random();
let tag = 10;
let owner = client.public_key();
let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new();

// Set the access permissions
let _ = perms.insert(
   owner,
   SequencePrivatePermissions::new(true, true),
);

// The returned address can then be used to `append` data to.
let address = client.store_private_sequence(None, name, tag, owner, perms).await?;

client.append_to_sequence(address, b"New Entry Value".to_vec()).await?;

Get Sequence Data from the Network

Examples

use safe_network::client::Client;
use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions};
use std::collections::BTreeMap;
use xor_name::XorName;
use rand::rngs::OsRng;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let id = Keypair::new_ed25519(&mut OsRng);
let mut client = Client::new(Some(id), None, bootstrap_contacts).await?;
let name = XorName::random();
let tag = 10;
let owner = client.public_key();
let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new();

// Set the access permissions
let _ = perms.insert(
   owner,
   SequencePrivatePermissions::new(true, true),
);

// The returned address can then be used to `append` data to.
let address = client.store_private_sequence(None, name, tag, owner, perms).await?;

let _data = client.get_sequence(address).await?;
pub async fn get_sequence_last_entry(
    &self,
    address: SequenceAddress
) -> Result<(u64, SequenceEntry), Error>

Get the last data entry from a Sequence Data.

Examples

use safe_network::client::Client;
use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions};
use std::collections::BTreeMap;
use xor_name::XorName;
use rand::rngs::OsRng;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let id = Keypair::new_ed25519(&mut OsRng);
let mut client = Client::new(Some(id), None, bootstrap_contacts).await?;
let name = XorName::random();
let tag = 10;
let owner = client.public_key();
let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new();

// Set the access permissions
let _ = perms.insert(
   owner,
   SequencePrivatePermissions::new(true, true),
);

// The returned address can then be used to `append` data to.
let address = client.store_private_sequence(None, name, tag, owner, perms).await?;
client.append_to_sequence(address, b"New Entry Value".to_vec()).await?;
client.append_to_sequence(address, b"Another New Entry Value".to_vec()).await?;

// Now we can retrieve the alst entry in the sequence:
let (_position, last_entry) = client.get_sequence_last_entry(address).await?;

assert_eq!(last_entry, b"Another New Entry Value".to_vec());
pub async fn get_sequence_entry(
    &self,
    address: SequenceAddress,
    index_from_start: u64
) -> Result<SequenceEntry, Error>

Get Sequence Data from the Network at a specific version

Examples

use safe_network::client::Client;
use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions};
use std::collections::BTreeMap;
use xor_name::XorName;
use rand::rngs::OsRng;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let id = Keypair::new_ed25519(&mut OsRng);
let mut client = Client::new(Some(id), None, bootstrap_contacts).await?;
let name = XorName::random();
let tag = 10;
let owner = client.public_key();
let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new();

// Set the access permissions
let _ = perms.insert(
   owner,
   SequencePrivatePermissions::new(true, true),
);

// The returned address can then be used to `append` data to.
let address = client.store_private_sequence(None, name, tag, owner, perms).await?;
client.append_to_sequence(address, b"New Entry Value".to_vec()).await?;
client.append_to_sequence(address, b"Another New Entry Value".to_vec()).await?;

// Now we can retrieve the alst entry in the sequence:
let entry_v1 = client.get_sequence_entry(address, 1).await?;

assert_eq!(entry_v1, b"Another New Entry Value".to_vec());

Get a set of Entries for the requested range from a Sequence.

Examples

use safe_network::client::Client;
use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions, SequenceIndex};
use std::collections::BTreeMap;
use xor_name::XorName;
use rand::rngs::OsRng;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let id = Keypair::new_ed25519(&mut OsRng);
let mut client = Client::new(Some(id), None, bootstrap_contacts).await?;
let name = XorName::random();
let tag = 10;
let owner = client.public_key();
let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new();

// Set the access permissions
let _ = perms.insert(
   owner,
   SequencePrivatePermissions::new(true, true),
);

// The returned address can then be used to `append` data to.
let address = client.store_private_sequence(None, name, tag, owner, perms).await?;
client.append_to_sequence(address, b"New Entry Value".to_vec()).await?;
client.append_to_sequence(address, b"Another New Entry Value".to_vec()).await?;
client.append_to_sequence(address, b"Third Entry Value".to_vec()).await?;

// Now we can retrieve the alst entry in the sequence:
let entries = client.get_sequence_range(address, (SequenceIndex::FromStart(1), SequenceIndex::FromEnd(0) )).await?;

assert_eq!(entries[0], b"Another New Entry Value".to_vec());
assert_eq!(entries[1], b"Third Entry Value".to_vec());

Get the owner of a Sequence.

Examples

use safe_network::client::Client;
use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions};
use std::collections::BTreeMap;
use xor_name::XorName;
use rand::rngs::OsRng;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let id = Keypair::new_ed25519(&mut OsRng);
let mut client = Client::new(Some(id), None, bootstrap_contacts).await?;
let name = XorName::random();
let tag = 10;
let owner = client.public_key();
let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new();

// Set the access permissions
let _ = perms.insert(
   owner,
   SequencePrivatePermissions::new(true, true),
);

// The returned address can then be used to `append` data to.
let address = client.store_private_sequence(None, name, tag, owner, perms).await?;

let seq_owner = client.get_sequence_owner(address).await?;
assert_eq!(seq_owner, owner);

Get the set of Permissions of a Public Sequence.

Examples

use safe_network::client::Client;
use safe_network::types::{Keypair, Token, SequenceUser,SequencePublicPermissions};
use std::collections::BTreeMap;
use xor_name::XorName;
use rand::rngs::OsRng;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let id = Keypair::new_ed25519(&mut OsRng);
let mut client = Client::new(Some(id), None, bootstrap_contacts).await?;
let name = XorName::random();
let tag = 10;
let owner = client.public_key();
let mut perms = BTreeMap::<SequenceUser, SequencePublicPermissions>::new();

// Set the access permissions
let _ = perms.insert(
   SequenceUser::Key(owner),
   SequencePublicPermissions::new(true),
);

// The returned address can then be used to `append` data to.
let address = client.store_public_sequence(None, name, tag, owner, perms).await?;

let _permissions = client.get_sequence_public_permissions_for_user(address, owner).await?;

Get the set of Permissions of a Private Sequence.

Examples

use safe_network::client::Client;
use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions};
use std::collections::BTreeMap;
use xor_name::XorName;
use rand::rngs::OsRng;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let id = Keypair::new_ed25519(&mut OsRng);
let mut client = Client::new(Some(id), None, bootstrap_contacts).await?;
let name = XorName::random();
let tag = 10;
let owner = client.public_key();
let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new();

// Set the access permissions
let _ = perms.insert(
   owner,
   SequencePrivatePermissions::new(true, true),
);

// The returned address can then be used to `append` data to.
let address = client.store_private_sequence(None, name, tag, owner, perms).await?;

let _permissions = client.get_sequence_private_permissions_for_user(address, owner).await?;

Get the set of Permissions for a specific user in a Sequence.

Examples

use safe_network::client::Client;
use safe_network::types::{Keypair, PublicKey, Token, SequencePrivatePermissions};
use std::collections::BTreeMap;
use xor_name::XorName;
use rand::rngs::OsRng;
// Let's use an existing client, with a pre-existing balance to be used for write payments.
let id = Keypair::new_ed25519(&mut OsRng);
let mut client = Client::new(Some(id), None, bootstrap_contacts).await?;
let name = XorName::random();
let tag = 10;
let owner = client.public_key();
let mut perms = BTreeMap::<PublicKey, SequencePrivatePermissions>::new();

// Set the access permissions
let _ = perms.insert(
   owner,
   SequencePrivatePermissions::new(true, true),
);

// The returned address can then be used to `append` data to.
let address = client.store_private_sequence(None, name, tag, owner, perms).await?;

let _permissions = client.get_sequence_public_permissions_for_user(address, owner).await?;

Handle all token transfers and Write API requests for a given ClientId.

Get the current known account balance from the local actor. (ie. Without querying the network)

Examples

Create a random client

use safe_network::client::Client;
use std::str::FromStr;
use safe_network::types::Token;
let client = Client::new(None, None, bootstrap_contacts).await?;
// now we check the local balance
let some_balance = client.get_local_balance().await;
assert_eq!(some_balance, Token::from_str("0")?);

Send token to another PublicKey.

If the PublicKey does not exist as a balance on the network it will be created with the send amount.

Examples

Send token to a PublickKey. (This test uses “simulated payouts” to generate test token. This of course would not be avaiable on a live network.)

use safe_network::client::Client;
use safe_network::types::{PublicKey, Token};
use std::str::FromStr;
// A random sk, to send token to
let sk = bls::SecretKey::random();
let pk = PublicKey::from(sk.public_key());
// Next we create a random client.
let mut client = Client::new(None, None, bootstrap_contacts).await?;
let target_balance = Token::from_str("100")?;
// And trigger a simulated payout to our client's PublicKey, so we have token to send.
let _ = client.trigger_simulated_farming_payout(target_balance).await?;

// Now we have 100 token at our balance, we can send it elsewhere:
let (count, sending_pk) = client.send_tokens( pk, target_balance ).await?;

// Finally, we can see that the token has arrived:
let received_balance = client.get_balance_for(pk).await?;

assert_eq!(1, count);
assert_ne!(pk, sending_pk);
assert_eq!(received_balance, target_balance);

Handle all token transfers and Write API requests for a given ClientId.

Simulate a farming payout & add a balance to the client’s PublicKey.

Useful for testing to generate initial balances needed for sending transfer requests, which is in turn required for performing write operations.

This also keeps the client transfer actor up to date.

Examples

Add 100 token to a client

use safe_network::client::Client;
use safe_network::types::{Keypair, Token};
use std::str::FromStr;
use rand::rngs::OsRng;
let id = Keypair::new_ed25519(&mut OsRng);
// Start our client
let mut client = Client::new(Some(id), None, bootstrap_contacts).await?;
let target_balance = Token::from_str("100")?;
let _ = client.trigger_simulated_farming_payout(target_balance).await?;

let balance = client.get_balance().await?;
assert_eq!(balance, target_balance);

Get the client’s current coin balance from the network

Examples

Retrieve an existing balance

use safe_network::client::Client;
use safe_network::types::{Keypair, Token};
use rand::rngs::OsRng;
use std::str::FromStr;
// Let's check the balance of a client with a random id.
// (It should have 0 balance)
let id = Keypair::new_ed25519(&mut OsRng);
let client = Client::new(Some(id), None, bootstrap_contacts).await?;
let initial_balance = Token::from_str("0")?;
let balance = client.get_balance().await?;
assert_eq!(balance, initial_balance);

Get balance for a Public Key on the network.

Examples

Retrieve an existing balance

use safe_network::client::Client;
use safe_network::types::{Keypair, Token};
use std::str::FromStr;
use rand::rngs::OsRng;
// Let's check the balance of a client with a random id.
let id = Keypair::new_ed25519(&mut OsRng);
let pk = id.public_key();

// And we use a random client to do this
let client = Client::new(None, None, bootstrap_contacts).await?;
let initial_balance = Token::from_str("0")?;
let balance = client.get_balance_for(pk).await?;
assert_eq!(balance, initial_balance);

Retrieve the history of the account from the network and apply to our local client’s AT2 actor.

Examples

Retrieving an existing balance history

use safe_network::client::Client;
use safe_network::types::Keypair;
use rand::rngs::OsRng;
// Let's check the balance of a random client.
// And we use a random client id to do this
let id = Keypair::new_ed25519(&mut OsRng);
let client = Client::new(Some(id), None, bootstrap_contacts).await?;
// Upon calling, history is retrieved and applied to the local AT2 actor.
let _ = client.get_history().await?;

Fetch latest StoreCost for given number of bytes from the network.

Easily manage connections to/from The Safe Network with the client and its APIs. Use a random client for read-only or one-time operations. Supply an existing, SecretKey which holds a SafeCoin balance to be able to perform write operations.

Create a Safe Network client instance. Either for an existing SecretKey (in which case) the client will attempt to retrieve the history of the key’s balance in order to be ready for any token operations. Or if no SecreteKey is passed, a random keypair will be used, which provides a client that can only perform Read operations (at least until the client’s SecretKey receives some token).

Examples

Create a random client

use safe_network::client::Client;


let client = Client::new(None, None, bootstrap_contacts).await?;
// Now for example you can perform read operations:
let _some_balance = client.get_balance().await?;

Sets up a listener for Cmd Errors that may need transfer debits to be resent to elders

Return the client’s FullId.

Useful for retrieving the PublicKey or KeyPair in the event you need to sign something

Examples

use safe_network::client::Client;
let client = Client::new(None, None, bootstrap_contacts).await?;
let _keypair = client.keypair();

Return the client’s PublicKey.

Examples

use safe_network::client::Client;
let client = Client::new(None, None, bootstrap_contacts).await?;
let _pk = client.public_key();

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.