Skip to content

Commit

Permalink
feat: create dev account with deposit on sandbox
Browse files Browse the repository at this point in the history
feat: test
  • Loading branch information
VladasZ committed Feb 12, 2024
1 parent 97d6449 commit a6ece95
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 41 deletions.
65 changes: 40 additions & 25 deletions workspaces/src/network/sandbox.rs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -126,17 +130,8 @@ impl TopLevelAccountCreator for Sandbox {
id: AccountId,
sk: SecretKey,
) -> Result<Execution<Account>> {
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(
Expand Down Expand Up @@ -164,6 +159,26 @@ impl TopLevelAccountCreator for Sandbox {
details: ExecutionFinalResult::from_view(outcome),
})
}

async fn create_tla_with_deposit(
&self,
worker: Worker<dyn Network>,
id: AccountId,
sk: SecretKey,
deposit: NearToken,
) -> Result<Execution<Account>> {
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 {
Expand Down
33 changes: 22 additions & 11 deletions workspaces/src/network/testnet.rs
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -121,6 +122,16 @@ impl TopLevelAccountCreator for Testnet {
details: ExecutionFinalResult::from_view(outcome),
})
}

async fn create_tla_with_deposit(
&self,
_worker: Worker<dyn Network>,
_id: AccountId,
_sk: SecretKey,
_deposit: NearToken,
) -> Result<Execution<Account>> {
unimplemented!("Creating accounts with deposin is not supported on Testnet")
}
}

impl NetworkClient for Testnet {
Expand Down
48 changes: 43 additions & 5 deletions workspaces/src/network/variants.rs
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -31,6 +35,14 @@ pub trait TopLevelAccountCreator {
sk: SecretKey,
wasm: &[u8],
) -> Result<Execution<Contract>>;

async fn create_tla_with_deposit(
&self,
worker: Worker<dyn Network>,
id: AccountId,
sk: SecretKey,
deposit: NearToken,
) -> Result<Execution<Account>>;
}

// NOTE: Not all networks/runtimes will have the ability to be able to do dev_deploy.
Expand All @@ -54,6 +66,24 @@ where
Ok(res)
}

pub async fn create_tla_with_deposit(
&self,
id: AccountId,
sk: SecretKey,
deposit: NearToken,
) -> Result<Execution<Account>> {
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,
Expand Down Expand Up @@ -84,6 +114,14 @@ where
Ok(account.into_result()?)
}

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?;
Ok(account.into_result()?)
}

pub async fn dev_deploy(&self, wasm: &[u8]) -> Result<Contract> {
let (id, sk) = self.dev_generate().await;
let contract = self.create_tla_and_deploy(id.clone(), sk, wasm).await?;
Expand Down
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 a6ece95

Please sign in to comment.