From 64123432a4073da4f6faa6d52427f46ab8d7c755 Mon Sep 17 00:00:00 2001 From: Phuong Nguyen Date: Mon, 9 Jan 2023 16:54:31 -0800 Subject: [PATCH 1/3] Added destination account-id for import_contract call --- workspaces/src/rpc/patch.rs | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/workspaces/src/rpc/patch.rs b/workspaces/src/rpc/patch.rs index 9868d921..79cc1b21 100644 --- a/workspaces/src/rpc/patch.rs +++ b/workspaces/src/rpc/patch.rs @@ -27,9 +27,12 @@ pub struct ImportContractTransaction<'a> { initial_balance: Option, block_ref: Option, + + /// AccountId if specified, will be the destination account to clone the contract to. + into_account_id: Option, } -impl<'a, 'b> ImportContractTransaction<'a> { +impl<'a> ImportContractTransaction<'a> { pub(crate) fn new( account_id: &'a AccountId, from_network: Worker, @@ -42,6 +45,7 @@ impl<'a, 'b> ImportContractTransaction<'a> { import_data: false, initial_balance: None, block_ref: None, + into_account_id: None, } } @@ -81,17 +85,27 @@ impl<'a, 'b> ImportContractTransaction<'a> { self } + /// Sets the destination [`AccountId`] where the import will be transacted to. + /// This function is provided so users can import to a different [`AccountId`] + /// than the one initially provided to import from. + pub fn dest_account_id(mut self, account_id: &AccountId) -> Self { + self.into_account_id = Some(account_id.clone()); + self + } + /// Process the transaction, and return the result of the execution. pub async fn transact(self) -> crate::result::Result { - let account_id = self.account_id.clone(); + let from_account_id = self.account_id; + let into_account_id = self.into_account_id.as_ref().unwrap_or(&from_account_id); + let sk = SecretKey::from_seed(KeyType::ED25519, DEV_ACCOUNT_SEED); let pk = sk.public_key(); - let signer = InMemorySigner::from_secret_key(account_id.clone(), sk); + let signer = InMemorySigner::from_secret_key(from_account_id.clone(), sk); let block_ref = self.block_ref.unwrap_or_else(BlockReference::latest); let mut account_view = self .from_network - .view_account(&account_id) + .view_account(from_account_id) .block_reference(block_ref.clone()) .await? .into_near_account(); @@ -101,11 +115,11 @@ impl<'a, 'b> ImportContractTransaction<'a> { let mut records = vec![ StateRecord::Account { - account_id: account_id.clone(), + account_id: into_account_id.clone(), account: account_view.clone(), }, StateRecord::AccessKey { - account_id: account_id.clone(), + account_id: into_account_id.clone(), public_key: pk.clone().into(), access_key: AccessKey::full_access(), }, @@ -114,11 +128,11 @@ impl<'a, 'b> ImportContractTransaction<'a> { if account_view.code_hash() != near_primitives::hash::CryptoHash::default() { let code = self .from_network - .view_code(&account_id) + .view_code(&from_account_id) .block_reference(block_ref.clone()) .await?; records.push(StateRecord::Contract { - account_id: account_id.clone(), + account_id: into_account_id.clone(), code, }); } @@ -126,12 +140,12 @@ impl<'a, 'b> ImportContractTransaction<'a> { if self.import_data { records.extend( self.from_network - .view_state(&account_id) + .view_state(&from_account_id) .block_reference(block_ref) .await? .into_iter() .map(|(key, value)| StateRecord::Data { - account_id: account_id.clone(), + account_id: into_account_id.clone(), data_key: key, value, }), From eac58888c2ed394e2db36d85f4c7b54947024e24 Mon Sep 17 00:00:00 2001 From: Phuong Nguyen Date: Mon, 9 Jan 2023 17:08:26 -0800 Subject: [PATCH 2/3] Fix clippy --- workspaces/src/rpc/patch.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/workspaces/src/rpc/patch.rs b/workspaces/src/rpc/patch.rs index 79cc1b21..9219547e 100644 --- a/workspaces/src/rpc/patch.rs +++ b/workspaces/src/rpc/patch.rs @@ -96,11 +96,11 @@ impl<'a> ImportContractTransaction<'a> { /// Process the transaction, and return the result of the execution. pub async fn transact(self) -> crate::result::Result { let from_account_id = self.account_id; - let into_account_id = self.into_account_id.as_ref().unwrap_or(&from_account_id); + let into_account_id = self.into_account_id.as_ref().unwrap_or(from_account_id); let sk = SecretKey::from_seed(KeyType::ED25519, DEV_ACCOUNT_SEED); let pk = sk.public_key(); - let signer = InMemorySigner::from_secret_key(from_account_id.clone(), sk); + let signer = InMemorySigner::from_secret_key(into_account_id.clone(), sk); let block_ref = self.block_ref.unwrap_or_else(BlockReference::latest); let mut account_view = self @@ -128,7 +128,7 @@ impl<'a> ImportContractTransaction<'a> { if account_view.code_hash() != near_primitives::hash::CryptoHash::default() { let code = self .from_network - .view_code(&from_account_id) + .view_code(from_account_id) .block_reference(block_ref.clone()) .await?; records.push(StateRecord::Contract { @@ -140,7 +140,7 @@ impl<'a> ImportContractTransaction<'a> { if self.import_data { records.extend( self.from_network - .view_state(&from_account_id) + .view_state(from_account_id) .block_reference(block_ref) .await? .into_iter() From fd890b54d2aad37316fea9abf62b56d0f7c9596a Mon Sep 17 00:00:00 2001 From: Phuong Nguyen Date: Mon, 9 Jan 2023 17:29:46 -0800 Subject: [PATCH 3/3] rebased --- workspaces/src/error/impls.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/workspaces/src/error/impls.rs b/workspaces/src/error/impls.rs index 432c2de6..e5bb81b9 100644 --- a/workspaces/src/error/impls.rs +++ b/workspaces/src/error/impls.rs @@ -6,14 +6,6 @@ use crate::result::ExecutionFailure; use super::{Error, ErrorKind, ErrorRepr, RpcErrorCode, SandboxErrorCode}; impl ErrorKind { - pub(crate) fn full(self, msg: T, error: E) -> Error - where - E: Into>, - T: Into>, - { - Error::full(self, msg, error) - } - pub(crate) fn custom(self, error: E) -> Error where E: Into>,