From fcb76cc88b26193232e68013f844e3ea04f09900 Mon Sep 17 00:00:00 2001 From: Phuong Nguyen Date: Tue, 11 Jan 2022 23:10:38 +0000 Subject: [PATCH 1/5] Refactor (dev_)deploy to consistently take in &[u8] --- examples/src/ref_finance.rs | 2 +- workspaces/Cargo.toml | 1 + workspaces/src/network/mainnet.rs | 2 +- workspaces/src/network/mod.rs | 6 +++--- workspaces/src/network/sandbox.rs | 4 ++-- workspaces/src/network/testnet.rs | 5 +++-- workspaces/src/worker/impls.rs | 2 +- workspaces/tests/deploy.rs | 2 +- workspaces/tests/patch_state.rs | 2 +- 9 files changed, 14 insertions(+), 12 deletions(-) diff --git a/examples/src/ref_finance.rs b/examples/src/ref_finance.rs index 252d1cdd..76e07102 100644 --- a/examples/src/ref_finance.rs +++ b/examples/src/ref_finance.rs @@ -180,7 +180,7 @@ async fn create_custom_ft( worker: &Worker, ) -> anyhow::Result { let ft: Contract = worker - .dev_deploy(std::fs::read(FT_CONTRACT_FILEPATH)?) + .dev_deploy(&std::fs::read(FT_CONTRACT_FILEPATH)?) .await?; // Initialize our FT contract with owner metadata and total supply available diff --git a/workspaces/Cargo.toml b/workspaces/Cargo.toml index 9ae5a78f..b76efa97 100644 --- a/workspaces/Cargo.toml +++ b/workspaces/Cargo.toml @@ -31,6 +31,7 @@ near-primitives = "0.5" near-jsonrpc-primitives = "0.5" near-jsonrpc-client = { version = "0.1", features = ["sandbox"] } near-sandbox-utils = "0.1" +# near-sandbox-utils = { path = "../../../sandbox/crate" } [build-dependencies] near-sandbox-utils = "0.1" diff --git a/workspaces/src/network/mainnet.rs b/workspaces/src/network/mainnet.rs index 0e139661..49718f31 100644 --- a/workspaces/src/network/mainnet.rs +++ b/workspaces/src/network/mainnet.rs @@ -43,7 +43,7 @@ impl TopLevelAccountCreator for Mainnet { &self, _id: AccountId, _sk: SecretKey, - _wasm: Vec, + _wasm: &[u8], ) -> anyhow::Result> { panic!("Unsupported for now: https://github.com/near/workspaces-rs/issues/18"); } diff --git a/workspaces/src/network/mod.rs b/workspaces/src/network/mod.rs index c6a61da5..e59708ff 100644 --- a/workspaces/src/network/mod.rs +++ b/workspaces/src/network/mod.rs @@ -45,7 +45,7 @@ pub trait TopLevelAccountCreator { &self, id: AccountId, sk: SecretKey, - wasm: Vec, + wasm: &[u8], ) -> anyhow::Result>; } @@ -57,7 +57,7 @@ pub trait AllowDevAccountCreation {} pub trait DevAccountDeployer { async fn dev_generate(&self) -> (AccountId, SecretKey); async fn dev_create_account(&self) -> anyhow::Result; - async fn dev_deploy(&self, wasm: Vec) -> anyhow::Result; + async fn dev_deploy(&self, wasm: &[u8]) -> anyhow::Result; } #[async_trait] @@ -87,7 +87,7 @@ where account.into() } - async fn dev_deploy(&self, wasm: Vec) -> anyhow::Result { + async fn dev_deploy(&self, wasm: &[u8]) -> anyhow::Result { let (id, sk) = self.dev_generate().await; let contract = self.create_tla_and_deploy(id.clone(), sk, wasm).await?; contract.into() diff --git a/workspaces/src/network/sandbox.rs b/workspaces/src/network/sandbox.rs index 1281efb7..31363b76 100644 --- a/workspaces/src/network/sandbox.rs +++ b/workspaces/src/network/sandbox.rs @@ -86,12 +86,12 @@ impl TopLevelAccountCreator for Sandbox { &self, id: AccountId, sk: SecretKey, - wasm: Vec, + wasm: &[u8], ) -> anyhow::Result> { let root_signer = self.root_signer(); let outcome = self .client - .create_account_and_deploy(&root_signer, &id, sk.public_key(), DEFAULT_DEPOSIT, wasm) + .create_account_and_deploy(&root_signer, &id, sk.public_key(), DEFAULT_DEPOSIT, wasm.into()) .await?; let signer = InMemorySigner::from_secret_key(id.clone(), sk); diff --git a/workspaces/src/network/testnet.rs b/workspaces/src/network/testnet.rs index 9f3b26fe..3c267b28 100644 --- a/workspaces/src/network/testnet.rs +++ b/workspaces/src/network/testnet.rs @@ -65,12 +65,13 @@ impl TopLevelAccountCreator for Testnet { &self, id: AccountId, sk: SecretKey, - wasm: Vec, + wasm: &[u8], ) -> anyhow::Result> { let signer = InMemorySigner::from_secret_key(id.clone(), sk.clone()); let account = self.create_tla(id.clone(), sk).await?; let account = account.into_result()?; - let outcome = self.client.deploy(&signer, &id, wasm).await?; + + let outcome = self.client.deploy(&signer, &id, wasm.into()).await?; Ok(CallExecution { result: Contract::account(account), diff --git a/workspaces/src/worker/impls.rs b/workspaces/src/worker/impls.rs index cf182515..41b65e50 100644 --- a/workspaces/src/worker/impls.rs +++ b/workspaces/src/worker/impls.rs @@ -41,7 +41,7 @@ where &self, id: AccountId, sk: SecretKey, - wasm: Vec, + wasm: &[u8], ) -> anyhow::Result> { self.workspace.create_tla_and_deploy(id, sk, wasm).await } diff --git a/workspaces/tests/deploy.rs b/workspaces/tests/deploy.rs index 9a5f6e6c..423ebc90 100644 --- a/workspaces/tests/deploy.rs +++ b/workspaces/tests/deploy.rs @@ -32,7 +32,7 @@ fn expected() -> NftMetadata { async fn test_dev_deploy() -> anyhow::Result<()> { let worker = workspaces::sandbox(); let wasm = std::fs::read(NFT_WASM_FILEPATH)?; - let contract = worker.dev_deploy(wasm).await?; + let contract = worker.dev_deploy(&wasm).await?; let _result = contract .call(&worker, "new_default_meta") diff --git a/workspaces/tests/patch_state.rs b/workspaces/tests/patch_state.rs index f755ff78..966e7774 100644 --- a/workspaces/tests/patch_state.rs +++ b/workspaces/tests/patch_state.rs @@ -21,7 +21,7 @@ async fn view_status_state( worker: Worker, ) -> anyhow::Result<(AccountId, StatusMessage)> { let wasm = std::fs::read(STATUS_MSG_WASM_FILEPATH)?; - let contract = worker.dev_deploy(wasm).await.unwrap(); + let contract = worker.dev_deploy(&wasm).await.unwrap(); contract .call(&worker, "set_status") From 45f03856b55d22fff2dc1bdcc0ba09dd438f2e95 Mon Sep 17 00:00:00 2001 From: Phuong Nguyen Date: Tue, 11 Jan 2022 23:11:22 +0000 Subject: [PATCH 2/5] Update examples --- examples/src/nft.rs | 2 +- examples/src/spooning.rs | 2 +- examples/src/status_message.rs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/src/nft.rs b/examples/src/nft.rs index 69d94a23..1a12a9d8 100644 --- a/examples/src/nft.rs +++ b/examples/src/nft.rs @@ -8,7 +8,7 @@ const NFT_WASM_FILEPATH: &str = "./examples/res/non_fungible_token.wasm"; async fn main() -> anyhow::Result<()> { let worker = workspaces::sandbox(); let wasm = std::fs::read(NFT_WASM_FILEPATH)?; - let contract = worker.dev_deploy(wasm).await?; + let contract = worker.dev_deploy(&wasm).await?; let outcome = contract .call(&worker, "new_default_meta") diff --git a/examples/src/spooning.rs b/examples/src/spooning.rs index 8f21300d..e34f006c 100644 --- a/examples/src/spooning.rs +++ b/examples/src/spooning.rs @@ -43,7 +43,7 @@ async fn deploy_status_contract( msg: &str, ) -> anyhow::Result { let wasm = std::fs::read(STATUS_MSG_WASM_FILEPATH)?; - let contract = worker.dev_deploy(wasm).await?; + let contract = worker.dev_deploy(&wasm).await?; // This will `call` into `set_status` with the message we want to set. contract diff --git a/examples/src/status_message.rs b/examples/src/status_message.rs index 9be1b671..3fd3ca49 100644 --- a/examples/src/status_message.rs +++ b/examples/src/status_message.rs @@ -7,7 +7,7 @@ const STATUS_MSG_WASM_FILEPATH: &str = "./examples/res/status_message.wasm"; async fn main() -> anyhow::Result<()> { let worker = workspaces::sandbox(); let wasm = std::fs::read(STATUS_MSG_WASM_FILEPATH)?; - let contract = worker.dev_deploy(wasm).await?; + let contract = worker.dev_deploy(&wasm).await?; let outcome = contract .call(&worker, "set_status") From 68945f66f1b5313f6ad20797786cab5e4474a296 Mon Sep 17 00:00:00 2001 From: Phuong Nguyen Date: Wed, 12 Jan 2022 01:56:30 +0000 Subject: [PATCH 3/5] Format --- workspaces/src/network/sandbox.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/workspaces/src/network/sandbox.rs b/workspaces/src/network/sandbox.rs index 31363b76..156a4ddf 100644 --- a/workspaces/src/network/sandbox.rs +++ b/workspaces/src/network/sandbox.rs @@ -91,7 +91,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.clone(), sk); From 1a0deed3afae2fc37e89c68321b8542e239ebf58 Mon Sep 17 00:00:00 2001 From: Phuong Nguyen Date: Wed, 12 Jan 2022 02:06:46 +0000 Subject: [PATCH 4/5] Update account.deploy --- workspaces/src/network/account.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/workspaces/src/network/account.rs b/workspaces/src/network/account.rs index 270e09a4..f0c89ae6 100644 --- a/workspaces/src/network/account.rs +++ b/workspaces/src/network/account.rs @@ -93,10 +93,10 @@ impl Account { /// Deploy contract code or WASM bytes to the account, and return us a new /// [`Contract`] object that we can use to interact with the contract. - pub async fn deploy>( + pub async fn deploy( &self, worker: &Worker, - wasm: U, + wasm: &[u8], ) -> anyhow::Result> { let outcome = worker .client() From bd85f47abc4f6b77382e4a8debffd0e2a35ff670 Mon Sep 17 00:00:00 2001 From: Phuong Nguyen Date: Wed, 12 Jan 2022 10:28:31 -0800 Subject: [PATCH 5/5] Update workspaces/Cargo.toml Co-authored-by: Austin Abell --- workspaces/Cargo.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/workspaces/Cargo.toml b/workspaces/Cargo.toml index b76efa97..9ae5a78f 100644 --- a/workspaces/Cargo.toml +++ b/workspaces/Cargo.toml @@ -31,7 +31,6 @@ near-primitives = "0.5" near-jsonrpc-primitives = "0.5" near-jsonrpc-client = { version = "0.1", features = ["sandbox"] } near-sandbox-utils = "0.1" -# near-sandbox-utils = { path = "../../../sandbox/crate" } [build-dependencies] near-sandbox-utils = "0.1"