use std::sync::atomic::{AtomicUsize, Ordering};
use near_kit::sandbox::{SANDBOX_ROOT_ACCOUNT, SandboxConfig};
use near_kit::*;
static COUNTER: AtomicUsize = AtomicUsize::new(0);
fn unique_account() -> AccountId {
let n = COUNTER.fetch_add(1, Ordering::Relaxed);
format!("basic{}.{}", n, SANDBOX_ROOT_ACCOUNT)
.parse()
.unwrap()
}
#[tokio::test]
async fn test_balance_query() {
let sandbox = SandboxConfig::shared().await;
let near = sandbox.client();
let balance = near.balance(SANDBOX_ROOT_ACCOUNT).await.unwrap();
assert!(balance.total.as_yoctonear() > 0);
println!("{} balance: {}", SANDBOX_ROOT_ACCOUNT, balance);
}
#[tokio::test]
async fn test_account_query() {
let sandbox = SandboxConfig::shared().await;
let near = sandbox.client();
let account = near.account(SANDBOX_ROOT_ACCOUNT).await.unwrap();
assert!(account.storage_usage > 0);
println!(
"{} storage: {} bytes",
SANDBOX_ROOT_ACCOUNT, account.storage_usage
);
}
#[tokio::test]
async fn test_account_exists() {
let sandbox = SandboxConfig::shared().await;
let near = sandbox.client();
assert!(near.account_exists(SANDBOX_ROOT_ACCOUNT).await.unwrap());
assert!(
!near
.account_exists("definitely-does-not-exist-12345.sandbox")
.await
.unwrap()
);
}
#[tokio::test]
async fn test_access_keys_query() {
let sandbox = SandboxConfig::shared().await;
let near = sandbox.client();
let keys = near.access_keys(SANDBOX_ROOT_ACCOUNT).await.unwrap();
assert!(!keys.keys.is_empty());
println!(
"{} has {} access keys",
SANDBOX_ROOT_ACCOUNT,
keys.keys.len()
);
}
#[tokio::test]
async fn test_balance_with_finality() {
let sandbox = SandboxConfig::shared().await;
let near = sandbox.client();
let balance = near
.balance(SANDBOX_ROOT_ACCOUNT)
.finality(Finality::Optimistic)
.await
.unwrap();
assert!(balance.total.as_yoctonear() > 0);
}
#[tokio::test]
async fn test_rpc_status() {
let sandbox = SandboxConfig::shared().await;
let near = sandbox.client();
let status = near.rpc().status().await.unwrap();
assert!(!status.chain_id.is_empty());
println!(
"Sandbox chain_id: {}, block height: {}",
status.chain_id, status.sync_info.latest_block_height
);
}
#[tokio::test]
async fn test_gas_price() {
let sandbox = SandboxConfig::shared().await;
let near = sandbox.client();
let gas_price = near.rpc().gas_price(None).await.unwrap();
assert!(gas_price.as_u128() > 0);
println!("Current gas price: {} yoctoNEAR", gas_price.gas_price);
}
#[tokio::test]
async fn test_create_and_query_account() {
let sandbox = SandboxConfig::shared().await;
let near = sandbox.client();
let new_account_key = SecretKey::generate_ed25519();
let new_account_id = unique_account();
near.transaction(&new_account_id)
.create_account()
.transfer(NearToken::from_near(10))
.add_full_access_key(new_account_key.public_key())
.send()
.wait_until(Final)
.await
.unwrap();
let account = near.account(&new_account_id).await.unwrap();
assert!(account.amount.as_near() >= 9);
assert!(near.account_exists(&new_account_id).await.unwrap());
let keys = near.access_keys(&new_account_id).await.unwrap();
assert_eq!(keys.keys.len(), 1);
}