Skip to content

Commit

Permalink
feat: test
Browse files Browse the repository at this point in the history
  • Loading branch information
VladasZ committed Feb 12, 2024
1 parent bbfc339 commit cc0dcf1
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 18 deletions.
47 changes: 34 additions & 13 deletions workspaces/src/network/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use std::{path::PathBuf, str::FromStr};

use async_trait::async_trait;
use near_jsonrpc_client::methods::{
sandbox_fast_forward::RpcSandboxFastForwardRequest, sandbox_patch_state::RpcSandboxPatchStateRequest,
sandbox_fast_forward::RpcSandboxFastForwardRequest,
sandbox_patch_state::RpcSandboxPatchStateRequest,
};
use near_primitives::state_record::StateRecord;
use near_sandbox_utils as sandbox;
Expand Down Expand Up @@ -42,28 +43,36 @@ impl Sandbox {
let path = home_dir.join("validator_key.json");
InMemorySigner::from_file(&path)
}
ValidatorKey::Known(account_id, secret_key) => {
Ok(InMemorySigner::from_secret_key(account_id.clone(), secret_key.clone()))
}
ValidatorKey::Known(account_id, secret_key) => Ok(InMemorySigner::from_secret_key(
account_id.clone(),
secret_key.clone(),
)),
}
}
pub(crate) async fn from_builder_with_version<'a>(build: NetworkBuilder<'a, Self>, version: &str) -> Result<Self> {
pub(crate) async fn from_builder_with_version<'a>(
build: NetworkBuilder<'a, Self>,
version: &str,
) -> Result<Self> {
// Check the conditions of the provided rpc_url and validator_key
let mut server = match (build.rpc_addr, build.validator_key) {
// Connect to a provided sandbox:
(Some(rpc_url), Some(validator_key)) => SandboxServer::connect(rpc_url, validator_key).await?,
(Some(rpc_url), Some(validator_key)) => {
SandboxServer::connect(rpc_url, validator_key).await?
}

// Spawn a new sandbox since rpc_url and home_dir weren't specified:
(None, None) => SandboxServer::run_new_with_version(version).await?,

// Missing inputted parameters for sandbox:
(Some(rpc_url), None) => {
return Err(SandboxErrorCode::InitFailure
.message(format!("Custom rpc_url={rpc_url} requires validator_key set.")));
return Err(SandboxErrorCode::InitFailure.message(format!(
"Custom rpc_url={rpc_url} requires validator_key set."
)));
}
(None, Some(validator_key)) => {
return Err(SandboxErrorCode::InitFailure
.message(format!("Custom validator_key={validator_key:?} requires rpc_url set.")));
return Err(SandboxErrorCode::InitFailure.message(format!(
"Custom validator_key={validator_key:?} requires rpc_url set."
)));
}
};

Expand Down Expand Up @@ -121,7 +130,8 @@ impl TopLevelAccountCreator for Sandbox {
id: AccountId,
sk: SecretKey,
) -> Result<Execution<Account>> {
self.create_tla_with_deposit(worker, id, sk, DEFAULT_DEPOSIT).await
self.create_tla_with_deposit(worker, id, sk, DEFAULT_DEPOSIT)
.await
}

async fn create_tla_and_deploy(
Expand All @@ -134,7 +144,13 @@ impl TopLevelAccountCreator for Sandbox {
let root_signer = self.root_signer()?;
let outcome = self
.client()
.create_account_and_deploy(&root_signer, &id, sk.public_key(), DEFAULT_DEPOSIT, wasm.into())
.create_account_and_deploy(
&root_signer,
&id,
sk.public_key(),
DEFAULT_DEPOSIT,
wasm.into(),
)
.await?;

let signer = InMemorySigner::from_secret_key(id, sk);
Expand Down Expand Up @@ -178,7 +194,12 @@ impl NetworkInfo for Sandbox {
}

impl Sandbox {
pub(crate) async fn patch_state(&self, contract_id: &AccountId, key: &[u8], value: &[u8]) -> Result<()> {
pub(crate) async fn patch_state(
&self,
contract_id: &AccountId,
key: &[u8],
value: &[u8],
) -> Result<()> {
let state = StateRecord::Data {
account_id: contract_id.to_owned(),
data_key: key.to_vec().into(),
Expand Down
22 changes: 17 additions & 5 deletions workspaces/src/network/variants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ pub trait NetworkInfo {

#[async_trait]
pub trait TopLevelAccountCreator {
async fn create_tla(&self, worker: Worker<dyn Network>, id: AccountId, sk: SecretKey)
-> Result<Execution<Account>>;
async fn create_tla(
&self,
worker: Worker<dyn Network>,
id: AccountId,
sk: SecretKey,
) -> Result<Execution<Account>>;

async fn create_tla_and_deploy(
&self,
Expand Down Expand Up @@ -50,7 +54,10 @@ where
T: DevNetwork + TopLevelAccountCreator + 'static,
{
pub async fn create_tla(&self, id: AccountId, sk: SecretKey) -> Result<Execution<Account>> {
let res = self.workspace.create_tla(self.clone().coerce(), id, sk).await?;
let res = self
.workspace
.create_tla(self.clone().coerce(), id, sk)
.await?;

for callback in self.tx_callbacks.iter() {
callback(res.details.total_gas_burnt)?;
Expand Down Expand Up @@ -109,7 +116,9 @@ where

pub async fn dev_create_account_with_deposit(&self, deposit: NearToken) -> Result<Account> {
let (id, sk) = self.dev_generate().await;
let account = self.create_tla_with_deposit(id.clone(), sk, deposit).await?;
let account = self
.create_tla_with_deposit(id.clone(), sk, deposit)
.await?;
Ok(account.into_result()?)
}

Expand All @@ -129,4 +138,7 @@ impl<T> Network for T where T: NetworkInfo + NetworkClient + Send + Sync {}
/// DevNetwork is a Network that can call into `dev_create` and `dev_deploy` to create developer accounts.
pub trait DevNetwork: TopLevelAccountCreator + AllowDevAccountCreation + Network + 'static {}

impl<T> DevNetwork for T where T: TopLevelAccountCreator + AllowDevAccountCreation + Network + 'static {}
impl<T> DevNetwork for T where
T: TopLevelAccountCreator + AllowDevAccountCreation + Network + 'static
{
}
13 changes: 13 additions & 0 deletions workspaces/tests/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,16 @@ async fn test_delete_account() -> anyhow::Result<()> {

Ok(())
}

#[test(tokio::test)]
async fn test_dev_account_with_deposit() -> anyhow::Result<()> {
let worker = near_workspaces::sandbox().await?;

let amount = NearToken::from_near(1_111_222);

let account = worker.dev_create_account_with_deposit(amount).await?;

assert_eq!(amount, account.view_account().await?.balance);

Ok(())
}

0 comments on commit cc0dcf1

Please sign in to comment.