Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: Reorganize exports and modules exposed #102

Merged
merged 7 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/src/ref_finance.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::{collections::HashMap, convert::TryInto};

use near_units::{parse_gas, parse_near};
use workspaces::{prelude::*, BlockHeight, DevNetwork, Sandbox};
use workspaces::network::Sandbox;
use workspaces::prelude::*;
use workspaces::{Account, AccountId, Contract, Network, Worker};
use workspaces::{BlockHeight, DevNetwork};

const FT_CONTRACT_FILEPATH: &str = "./examples/res/fungible_token.wasm";

Expand Down
11 changes: 6 additions & 5 deletions workspaces/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ mod cargo;
#[cfg(feature = "unstable")]
pub use cargo::compile_project;

mod network;
mod rpc;
mod types;
mod worker;

pub mod network;
pub mod operations;
pub mod prelude;
pub mod result;

pub use network::result;
pub use network::transaction::Function;
pub use network::Sandbox;
pub use network::{Account, AccountDetails, Block, Contract, DevNetwork, Network};
pub use network::variants::{DevNetwork, Network};
pub use types::account::{Account, AccountDetails, Contract};
pub use types::block::Block;
pub use types::{AccessKey, AccountId, BlockHeight, CryptoHash, InMemorySigner};
pub use worker::{
mainnet, mainnet_archival, sandbox, testnet, testnet_archival, with_mainnet,
Expand Down
3 changes: 1 addition & 2 deletions workspaces/src/network/mainnet.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::network::Info;
use crate::network::{NetworkClient, NetworkInfo};
use crate::network::{Info, NetworkClient, NetworkInfo};
use crate::rpc::client::Client;
use std::path::PathBuf;

Expand Down
100 changes: 13 additions & 87 deletions workspaces/src/network/mod.rs
Original file line number Diff line number Diff line change
@@ -1,94 +1,20 @@
mod account;
mod block;
//! All builtin network types and traits.
//!
//! Currently the builtin network types are [`Mainnet`], [`Testnet`], and [`Sandbox`].

mod info;
mod mainnet;
pub mod result;
mod sandbox;
mod server;
mod testnet;
pub(crate) mod variants;

pub mod transaction;

use async_trait::async_trait;

pub(crate) use crate::network::info::Info;
use crate::rpc::client::Client;
use crate::types::{AccountId, KeyType, SecretKey};

pub use crate::network::account::{Account, AccountDetails, Contract};
pub use crate::network::block::Block;
pub use crate::network::mainnet::Mainnet;
pub use crate::network::result::{CallExecution, CallExecutionDetails, ViewResultDetails};
pub use crate::network::sandbox::Sandbox;
pub use crate::network::testnet::Testnet;

pub(crate) const DEV_ACCOUNT_SEED: &str = "testificate";

pub trait NetworkClient {
fn client(&self) -> &Client;
}

pub trait NetworkInfo {
fn info(&self) -> &Info;
}

#[async_trait]
pub trait TopLevelAccountCreator {
async fn create_tla(
&self,
id: AccountId,
sk: SecretKey,
) -> anyhow::Result<CallExecution<Account>>;

async fn create_tla_and_deploy(
&self,
id: AccountId,
sk: SecretKey,
wasm: &[u8],
) -> anyhow::Result<CallExecution<Contract>>;
}

// NOTE: Not all networks/runtimes will have the ability to be able to do dev_deploy.
// This trait acts as segmented boundary for only specific networks such as sandbox and testnet.
pub trait AllowDevAccountCreation {}

#[async_trait]
pub trait DevAccountDeployer {
async fn dev_generate(&self) -> (AccountId, SecretKey);
async fn dev_create_account(&self) -> anyhow::Result<Account>;
async fn dev_deploy(&self, wasm: &[u8]) -> anyhow::Result<Contract>;
}

#[async_trait]
impl<T> DevAccountDeployer for T
where
T: TopLevelAccountCreator + NetworkInfo + AllowDevAccountCreation + Send + Sync,
{
async fn dev_generate(&self) -> (AccountId, SecretKey) {
let id = crate::rpc::tool::random_account_id();
let sk = SecretKey::from_seed(KeyType::ED25519, DEV_ACCOUNT_SEED);
(id, sk)
}

async fn dev_create_account(&self) -> anyhow::Result<Account> {
let (id, sk) = self.dev_generate().await;
let account = self.create_tla(id.clone(), sk).await?;
account.into()
}

async fn dev_deploy(&self, wasm: &[u8]) -> anyhow::Result<Contract> {
let (id, sk) = self.dev_generate().await;
let contract = self.create_tla_and_deploy(id.clone(), sk, wasm).await?;
contract.into()
}
}

pub trait Network: NetworkInfo + NetworkClient + Send + Sync {}

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: AllowDevAccountCreation + Network + TopLevelAccountCreator {}
pub(crate) use variants::DEV_ACCOUNT_SEED;

// Implemented by default if we have `AllowDevAccountCreation`
impl<T> DevNetwork for T where T: AllowDevAccountCreation + Network + TopLevelAccountCreator {}
pub use self::info::Info;
pub use self::mainnet::Mainnet;
pub use self::sandbox::Sandbox;
pub use self::testnet::Testnet;
pub use self::variants::{
AllowDevAccountCreation, DevAccountDeployer, NetworkClient, NetworkInfo, TopLevelAccountCreator,
};
9 changes: 3 additions & 6 deletions workspaces/src/network/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ use near_jsonrpc_client::methods::sandbox_fast_forward::RpcSandboxFastForwardReq
use near_jsonrpc_client::methods::sandbox_patch_state::RpcSandboxPatchStateRequest;
use near_primitives::state_record::StateRecord;

use super::{
Account, AllowDevAccountCreation, CallExecution, Contract, NetworkClient, NetworkInfo,
TopLevelAccountCreator,
};

use super::{AllowDevAccountCreation, NetworkClient, NetworkInfo, TopLevelAccountCreator};
use crate::network::server::SandboxServer;
use crate::network::Info;
use crate::result::CallExecution;
use crate::rpc::client::Client;
use crate::rpc::patch::ImportContractTransaction;
use crate::types::{AccountId, Balance, InMemorySigner, SecretKey};
use crate::{Network, Worker};
use crate::{Account, Contract, Network, Worker};

// Constant taken from nearcore crate to avoid dependency
pub(crate) const NEAR_BASE: Balance = 1_000_000_000_000_000_000_000_000;
Expand Down
9 changes: 3 additions & 6 deletions workspaces/src/network/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@ use url::Url;
use near_primitives::views::{ExecutionStatusView, FinalExecutionStatus};

use crate::network::Info;
use crate::network::{
Account, AllowDevAccountCreation, CallExecution, CallExecutionDetails, NetworkClient,
NetworkInfo, TopLevelAccountCreator,
};
use crate::result::ExecutionOutcome;
use crate::network::{AllowDevAccountCreation, NetworkClient, NetworkInfo, TopLevelAccountCreator};
use crate::result::{CallExecution, CallExecutionDetails, ExecutionOutcome};
use crate::rpc::{client::Client, tool};
use crate::types::{AccountId, InMemorySigner, SecretKey};
use crate::{Contract, CryptoHash};
use crate::{Account, Contract, CryptoHash};

const RPC_URL: &str = "https://rpc.testnet.near.org";
const HELPER_URL: &str = "https://helper.testnet.near.org";
Expand Down
77 changes: 77 additions & 0 deletions workspaces/src/network/variants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use crate::network::Info;
use crate::result::CallExecution;
use crate::rpc::client::Client;
use crate::types::{AccountId, KeyType, SecretKey};
use crate::{Account, Contract};
use async_trait::async_trait;

pub(crate) const DEV_ACCOUNT_SEED: &str = "testificate";

pub trait NetworkClient {
fn client(&self) -> &Client;
}

pub trait NetworkInfo {
fn info(&self) -> &Info;
}

#[async_trait]
pub trait TopLevelAccountCreator {
async fn create_tla(
&self,
id: AccountId,
sk: SecretKey,
) -> anyhow::Result<CallExecution<Account>>;

async fn create_tla_and_deploy(
&self,
id: AccountId,
sk: SecretKey,
wasm: &[u8],
) -> anyhow::Result<CallExecution<Contract>>;
}

// NOTE: Not all networks/runtimes will have the ability to be able to do dev_deploy.
// This trait acts as segmented boundary for only specific networks such as sandbox and testnet.
pub trait AllowDevAccountCreation {}

#[async_trait]
pub trait DevAccountDeployer {
async fn dev_generate(&self) -> (AccountId, SecretKey);
async fn dev_create_account(&self) -> anyhow::Result<Account>;
async fn dev_deploy(&self, wasm: &[u8]) -> anyhow::Result<Contract>;
}

#[async_trait]
impl<T> DevAccountDeployer for T
where
T: TopLevelAccountCreator + NetworkInfo + AllowDevAccountCreation + Send + Sync,
{
async fn dev_generate(&self) -> (AccountId, SecretKey) {
let id = crate::rpc::tool::random_account_id();
let sk = SecretKey::from_seed(KeyType::ED25519, DEV_ACCOUNT_SEED);
(id, sk)
}

async fn dev_create_account(&self) -> anyhow::Result<Account> {
let (id, sk) = self.dev_generate().await;
let account = self.create_tla(id.clone(), sk).await?;
account.into()
}

async fn dev_deploy(&self, wasm: &[u8]) -> anyhow::Result<Contract> {
let (id, sk) = self.dev_generate().await;
let contract = self.create_tla_and_deploy(id.clone(), sk, wasm).await?;
contract.into()
}
}

pub trait Network: NetworkInfo + NetworkClient + Send + Sync {}

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 {}

// Implemented by default if we have `AllowDevAccountCreation`
impl<T> DevNetwork for T where T: TopLevelAccountCreator + AllowDevAccountCreation + Network {}
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use std::convert::TryInto;

use near_crypto::KeyType;
use near_primitives::transaction::{
Action, AddKeyAction, CreateAccountAction, DeleteAccountAction, DeleteKeyAction,
DeployContractAction, FunctionCallAction, StakeAction, TransferAction,
};
use near_primitives::views::FinalExecutionOutcomeView;
//! All operation types that are generated/used when making transactions or view calls.

use crate::network::{CallExecution, CallExecutionDetails, Network, ViewResultDetails};
use crate::result::{CallExecution, CallExecutionDetails, ViewResultDetails};
use crate::rpc::client::{
send_batch_tx_and_retry, Client, DEFAULT_CALL_DEPOSIT, DEFAULT_CALL_FN_GAS,
};
use crate::types::{AccessKey, AccountId, Balance, Gas, InMemorySigner, PublicKey, SecretKey};
use crate::worker::Worker;
use crate::Account;
use crate::Network;
use near_crypto::KeyType;
use near_primitives::transaction::{
Action, AddKeyAction, CreateAccountAction, DeleteAccountAction, DeleteKeyAction,
DeployContractAction, FunctionCallAction, StakeAction, TransferAction,
};
use near_primitives::views::FinalExecutionOutcomeView;
use std::convert::TryInto;

const MAX_GAS: Gas = 300_000_000_000_000;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Result and execution types from results of RPC calls to the network.

use near_account_id::AccountId;
use near_primitives::views::{
CallResult, ExecutionOutcomeWithIdView, ExecutionStatusView, FinalExecutionOutcomeView,
Expand Down
2 changes: 1 addition & 1 deletion workspaces/src/rpc/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use near_primitives::views::{
QueryRequest,
};

use crate::network::ViewResultDetails;
use crate::result::ViewResultDetails;
use crate::rpc::tool;
use crate::types::{AccountId, InMemorySigner, PublicKey, Signer};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use near_primitives::views::AccountView;
use crate::types::{AccountId, Balance, InMemorySigner};
use crate::{CryptoHash, Network, Worker};

use super::transaction::{CallTransaction, CreateAccountTransaction, Transaction};
use super::{CallExecution, CallExecutionDetails, ViewResultDetails};
use crate::operations::{CallTransaction, CreateAccountTransaction, Transaction};
use crate::result::{CallExecution, CallExecutionDetails, ViewResultDetails};

pub struct Account {
pub(crate) id: AccountId,
Expand Down
File renamed without changes.
3 changes: 3 additions & 0 deletions workspaces/src/types.rs → workspaces/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
pub(crate) mod account;
pub(crate) mod block;

/// Types copied over from near_primitives since those APIs are not yet stable.
/// and internal libraries like near-jsonrpc-client requires specific versions
/// of these types which shouldn't be exposed either.
Expand Down
15 changes: 6 additions & 9 deletions workspaces/src/worker/impls.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
use std::collections::HashMap;

use async_trait::async_trait;
use near_primitives::types::{Balance, StoreKey};

use crate::network::{
Account, AllowDevAccountCreation, Block, CallExecution, CallExecutionDetails, Contract,
NetworkClient, NetworkInfo, TopLevelAccountCreator, ViewResultDetails,
};
use crate::network::{AllowDevAccountCreation, NetworkClient, NetworkInfo, TopLevelAccountCreator};
use crate::network::{Info, Sandbox};
use crate::result::{CallExecution, CallExecutionDetails, ViewResultDetails};
use crate::rpc::client::{Client, DEFAULT_CALL_DEPOSIT, DEFAULT_CALL_FN_GAS};
use crate::rpc::patch::ImportContractTransaction;
use crate::types::{AccountId, Gas, InMemorySigner, SecretKey};
use crate::worker::Worker;
use crate::{Account, Block, Contract};
use crate::{AccountDetails, Network};
use async_trait::async_trait;
use near_primitives::types::{Balance, StoreKey};
use std::collections::HashMap;

impl<T> Clone for Worker<T> {
fn clone(&self) -> Self {
Expand Down
3 changes: 2 additions & 1 deletion workspaces/src/worker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ mod impls;

use std::sync::Arc;

use crate::network::{Mainnet, Network, Sandbox, Testnet};
use crate::network::{Mainnet, Sandbox, Testnet};
use crate::Network;

pub struct Worker<T> {
workspace: Arc<T>,
Expand Down
2 changes: 1 addition & 1 deletion workspaces/tests/batch_tx.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde_json::json;
use test_log::test;
use workspaces::operations::Function;
use workspaces::prelude::*;
use workspaces::Function;

#[test(tokio::test)]
async fn test_batch_tx() -> anyhow::Result<()> {
Expand Down