diff --git a/workspaces/src/network/sandbox.rs b/workspaces/src/network/sandbox.rs index 1cea0ba1..fe09ac8f 100644 --- a/workspaces/src/network/sandbox.rs +++ b/workspaces/src/network/sandbox.rs @@ -1,22 +1,26 @@ -use std::path::PathBuf; -use std::str::FromStr; +use std::{path::PathBuf, str::FromStr}; use async_trait::async_trait; -use near_jsonrpc_client::methods::sandbox_fast_forward::RpcSandboxFastForwardRequest; -use near_jsonrpc_client::methods::sandbox_patch_state::RpcSandboxPatchStateRequest; +use near_jsonrpc_client::methods::{ + sandbox_fast_forward::RpcSandboxFastForwardRequest, + sandbox_patch_state::RpcSandboxPatchStateRequest, +}; use near_primitives::state_record::StateRecord; use near_sandbox_utils as sandbox; -use super::builder::{FromNetworkBuilder, NetworkBuilder}; -use super::server::ValidatorKey; -use super::{AllowDevAccountCreation, NetworkClient, NetworkInfo, TopLevelAccountCreator}; -use crate::error::SandboxErrorCode; -use crate::network::server::SandboxServer; -use crate::network::Info; -use crate::result::{Execution, ExecutionFinalResult, Result}; -use crate::rpc::client::Client; -use crate::types::{AccountId, InMemorySigner, NearToken, SecretKey}; -use crate::{Account, Contract, Network, Worker}; +use super::{ + builder::{FromNetworkBuilder, NetworkBuilder}, + server::ValidatorKey, + AllowDevAccountCreation, NetworkClient, NetworkInfo, TopLevelAccountCreator, +}; +use crate::{ + error::SandboxErrorCode, + network::{server::SandboxServer, Info}, + result::{Execution, ExecutionFinalResult, Result}, + rpc::client::Client, + types::{AccountId, InMemorySigner, NearToken, SecretKey}, + Account, Contract, Network, Worker, +}; // Constant taken from nearcore crate to avoid dependency const DEFAULT_DEPOSIT: NearToken = NearToken::from_near(100); @@ -126,17 +130,8 @@ impl TopLevelAccountCreator for Sandbox { id: AccountId, sk: SecretKey, ) -> Result> { - let root_signer = self.root_signer()?; - let outcome = self - .client() - .create_account(&root_signer, &id, sk.public_key(), DEFAULT_DEPOSIT) - .await?; - - let signer = InMemorySigner::from_secret_key(id, sk); - Ok(Execution { - result: Account::new(signer, worker), - details: ExecutionFinalResult::from_view(outcome), - }) + self.create_tla_with_deposit(worker, id, sk, DEFAULT_DEPOSIT) + .await } async fn create_tla_and_deploy( @@ -164,6 +159,26 @@ impl TopLevelAccountCreator for Sandbox { details: ExecutionFinalResult::from_view(outcome), }) } + + async fn create_tla_with_deposit( + &self, + worker: Worker, + id: AccountId, + sk: SecretKey, + deposit: NearToken, + ) -> Result> { + let root_signer = self.root_signer()?; + let outcome = self + .client() + .create_account(&root_signer, &id, sk.public_key(), deposit) + .await?; + + let signer = InMemorySigner::from_secret_key(id, sk); + Ok(Execution { + result: Account::new(signer, worker), + details: ExecutionFinalResult::from_view(outcome), + }) + } } impl NetworkClient for Sandbox { diff --git a/workspaces/src/network/testnet.rs b/workspaces/src/network/testnet.rs index 15ed8fb7..5645e380 100644 --- a/workspaces/src/network/testnet.rs +++ b/workspaces/src/network/testnet.rs @@ -1,19 +1,20 @@ -use std::path::PathBuf; -use std::str::FromStr; +use std::{path::PathBuf, str::FromStr}; use async_trait::async_trait; use near_gas::NearGas; -use url::Url; - use near_primitives::views::ExecutionStatusView; +use url::Url; -use crate::network::builder::{FromNetworkBuilder, NetworkBuilder}; -use crate::network::Info; -use crate::network::{AllowDevAccountCreation, NetworkClient, NetworkInfo, TopLevelAccountCreator}; -use crate::result::{Execution, ExecutionDetails, ExecutionFinalResult, ExecutionOutcome, Result}; -use crate::rpc::{client::Client, tool}; -use crate::types::{AccountId, InMemorySigner, NearToken, SecretKey}; -use crate::{Account, Contract, CryptoHash, Network, Worker}; +use crate::{ + network::{ + builder::{FromNetworkBuilder, NetworkBuilder}, + AllowDevAccountCreation, Info, NetworkClient, NetworkInfo, TopLevelAccountCreator, + }, + result::{Execution, ExecutionDetails, ExecutionFinalResult, ExecutionOutcome, Result}, + rpc::{client::Client, tool}, + types::{AccountId, InMemorySigner, NearToken, SecretKey}, + Account, Contract, CryptoHash, Network, Worker, +}; /// URL to the testnet RPC node provided by near.org. pub const RPC_URL: &str = "https://rpc.testnet.near.org"; @@ -121,6 +122,16 @@ impl TopLevelAccountCreator for Testnet { details: ExecutionFinalResult::from_view(outcome), }) } + + async fn create_tla_with_deposit( + &self, + _worker: Worker, + _id: AccountId, + _sk: SecretKey, + _deposit: NearToken, + ) -> Result> { + unimplemented!("Creating accounts with deposin is not supported on Testnet") + } } impl NetworkClient for Testnet { diff --git a/workspaces/src/network/variants.rs b/workspaces/src/network/variants.rs index eb228cd8..b0266fe2 100644 --- a/workspaces/src/network/variants.rs +++ b/workspaces/src/network/variants.rs @@ -1,9 +1,13 @@ -use crate::network::Info; -use crate::result::{Execution, Result}; -use crate::rpc::client::Client; -use crate::types::{AccountId, KeyType, SecretKey}; -use crate::{Account, Contract, Worker}; use async_trait::async_trait; +use near_token::NearToken; + +use crate::{ + network::Info, + result::{Execution, Result}, + rpc::client::Client, + types::{AccountId, KeyType, SecretKey}, + Account, Contract, Worker, +}; pub(crate) const DEV_ACCOUNT_SEED: &str = "testificate"; @@ -31,6 +35,14 @@ pub trait TopLevelAccountCreator { sk: SecretKey, wasm: &[u8], ) -> Result>; + + async fn create_tla_with_deposit( + &self, + worker: Worker, + id: AccountId, + sk: SecretKey, + deposit: NearToken, + ) -> Result>; } // NOTE: Not all networks/runtimes will have the ability to be able to do dev_deploy. @@ -54,6 +66,24 @@ where Ok(res) } + pub async fn create_tla_with_deposit( + &self, + id: AccountId, + sk: SecretKey, + deposit: NearToken, + ) -> Result> { + let res = self + .workspace + .create_tla_with_deposit(self.clone().coerce(), id, sk, deposit) + .await?; + + for callback in self.tx_callbacks.iter() { + callback(res.details.total_gas_burnt)?; + } + + Ok(res) + } + pub async fn create_tla_and_deploy( &self, id: AccountId, @@ -84,6 +114,14 @@ where Ok(account.into_result()?) } + pub async fn dev_create_account_with_deposit(&self, deposit: NearToken) -> Result { + let (id, sk) = self.dev_generate().await; + let account = self + .create_tla_with_deposit(id.clone(), sk, deposit) + .await?; + Ok(account.into_result()?) + } + pub async fn dev_deploy(&self, wasm: &[u8]) -> Result { let (id, sk) = self.dev_generate().await; let contract = self.create_tla_and_deploy(id.clone(), sk, wasm).await?; diff --git a/workspaces/tests/account.rs b/workspaces/tests/account.rs index 6f896cb1..a958f282 100644 --- a/workspaces/tests/account.rs +++ b/workspaces/tests/account.rs @@ -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(()) +}