Skip to content

Commit

Permalink
Remove cloning of client
Browse files Browse the repository at this point in the history
  • Loading branch information
DOBEN committed Oct 13, 2023
1 parent bb110f9 commit 94c1859
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 70 deletions.
20 changes: 0 additions & 20 deletions templates/default/deploy-scripts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,6 @@ This project has boilerplate code to write deployment, initialization, and updat
Automatic scripts are useful to speed up the development and testing of your protocol on the chain.
In addition, scripts help to set up identical protocols on different chains easily. E.g. you can deploy your protocol to testnet or mainnet by just specifying a corresponding node connection when running the script.

# Setup

Option 1:

```
cargo concordium init
```

Option 2 (alternative command):

```
cargo generate --git https://github.com/Concordium/concordium-rust-smart-contracts.git
```

Any of the two commands will work and will give you several templates to choose from.

- Choose the `templates/default` and answer the questions to complete the setup process (answer `default` for the two questions if you want to run the below example).

At the end, you will have a Rust project setup with this boilerplate code included.

# Running The Script

Build and run the script from the deploy-scripts folder using
Expand Down
87 changes: 39 additions & 48 deletions templates/default/deploy-scripts/src/deployer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ pub struct ContractInitialized {

/// A struct containing connection and wallet information.
#[derive(Debug)]
pub struct Deployer {
pub struct Deployer<'a> {
/// The client to establish a connection to a Concordium node (V2 API).
pub client: v2::Client,
pub client: &'a mut v2::Client,
/// The account keys to be used for sending transactions.
pub key: WalletAccount,
}

impl Deployer {
impl<'a> Deployer<'a> {
/// A function to create a new deployer instance.
pub fn new(client: v2::Client, wallet_account_file: &Path) -> Result<Deployer, DeployError> {
pub fn new(
client: &'a mut v2::Client,
wallet_account_file: &Path,
) -> Result<Deployer<'a>, DeployError> {
let key_data = WalletAccount::from_json_file(wallet_account_file)?;

Ok(Deployer {
Expand All @@ -56,14 +59,14 @@ impl Deployer {
}

/// A function to check if a module exists on the chain.
pub async fn module_exists(&self, wasm_module: WasmModule) -> Result<bool, DeployError> {
let best_block = self.client.clone().get_block_finalization_summary(Best).await?;
pub async fn module_exists(&mut self, wasm_module: WasmModule) -> Result<bool, DeployError> {
let best_block = self.client.get_block_finalization_summary(Best).await?;

let best_block_hash = best_block.block_hash;

let module_ref = wasm_module.get_module_ref();

let module_src = self.client.clone().get_module_source(&module_ref, &best_block_hash).await;
let module_src = self.client.get_module_source(&module_ref, &best_block_hash).await;

match module_src {
Ok(_) => Ok(true),
Expand All @@ -84,7 +87,7 @@ impl Deployer {
/// can be given. If `None` is provided, the local time + 300 seconds is
/// used as a default expiry time.
pub async fn deploy_wasm_module(
&self,
&mut self,
wasm_module: WasmModule,
expiry: Option<TransactionTime>,
) -> Result<(Option<HashBytes<TransactionMarker>>, ModuleDeployed), DeployError> {
Expand All @@ -108,10 +111,9 @@ impl Deployer {
return Err(DeployError::NonceNotFinal);
}

let expiry = match expiry {
Some(expiry) => expiry,
None => TransactionTime::from_seconds((chrono::Utc::now().timestamp() + 300) as u64),
};
let expiry = expiry.unwrap_or(TransactionTime::from_seconds(
(chrono::Utc::now().timestamp() + 300) as u64,
));

let tx = deploy_module(&self.key, self.key.address, nonce.nonce, expiry, wasm_module);
let bi = transactions::BlockItem::AccountTransaction(tx);
Expand All @@ -125,7 +127,7 @@ impl Deployer {

println!("Sent tx: {tx_hash}");

let (_, block_item) = self.client.clone().wait_until_finalized(&tx_hash).await?;
let (_, block_item) = self.client.wait_until_finalized(&tx_hash).await?;

let module_deployed = self.parse_deploy_module_event(block_item)?;

Expand All @@ -147,7 +149,7 @@ impl Deployer {
/// expiry time for the transaction can be given. If `None` is provided,
/// the local time + 300 seconds is used as a default expiry time.
pub async fn init_contract(
&self,
&mut self,
payload: InitContractPayload,
energy: Option<Energy>,
expiry: Option<TransactionTime>,
Expand All @@ -160,17 +162,13 @@ impl Deployer {
return Err(DeployError::NonceNotFinal);
}

let energy = match energy {
Some(energy) => energy,
None => Energy {
energy: 5000,
},
};
let energy = energy.unwrap_or(Energy {
energy: 5000,
});

let expiry = match expiry {
Some(expiry) => expiry,
None => TransactionTime::from_seconds((chrono::Utc::now().timestamp() + 300) as u64),
};
let expiry = expiry.unwrap_or(TransactionTime::from_seconds(
(chrono::Utc::now().timestamp() + 300) as u64,
));

let tx = init_contract(&self.key, self.key.address, nonce.nonce, expiry, payload, energy);

Expand All @@ -185,7 +183,7 @@ impl Deployer {

println!("Sent tx: {tx_hash}");

let (_, block_item) = self.client.clone().wait_until_finalized(&tx_hash).await?;
let (_, block_item) = self.client.wait_until_finalized(&tx_hash).await?;

let contract_init = self.parse_contract_init_event(block_item)?;

Expand All @@ -208,7 +206,7 @@ impl Deployer {
/// `None` is provided, the local time + 300 seconds is used as a default
/// expiry time.
pub async fn update_contract(
&self,
&mut self,
update_payload: UpdateContractPayload,
energy: Option<GivenEnergy>,
expiry: Option<TransactionTime>,
Expand All @@ -225,17 +223,13 @@ impl Deployer {
payload: update_payload,
};

let expiry = match expiry {
Some(expiry) => expiry,
None => TransactionTime::from_seconds((chrono::Utc::now().timestamp() + 300) as u64),
};
let expiry = expiry.unwrap_or(TransactionTime::from_seconds(
(chrono::Utc::now().timestamp() + 300) as u64,
));

let energy = match energy {
Some(energy) => energy,
None => GivenEnergy::Absolute(Energy {
energy: 50000,
}),
};
let energy = energy.unwrap_or(GivenEnergy::Absolute(Energy {
energy: 50000,
}));

let tx = transactions::send::make_and_sign_transaction(
&self.key,
Expand All @@ -256,7 +250,7 @@ impl Deployer {

println!("Sent tx: {tx_hash}");

let (_, block_item) = self.client.clone().wait_until_finalized(&tx_hash).await?;
let (_, block_item) = self.client.wait_until_finalized(&tx_hash).await?;

self.parse_contract_update_event(block_item)?;

Expand All @@ -275,22 +269,19 @@ impl Deployer {
/// be given. If `None` is provided, 50000 energy is used as a default
/// max_energy value.
pub async fn estimate_energy(
&self,
&mut self,
payload: UpdateContractPayload,
max_energy: Option<Energy>,
) -> Result<Energy, DeployError> {
println!("\nEstimating energy....");

let best_block = self.client.clone().get_block_finalization_summary(Best).await?;
let best_block = self.client.get_block_finalization_summary(Best).await?;

let best_block_hash = best_block.block_hash;

let max_energy = match max_energy {
Some(energy) => energy,
None => Energy {
energy: 50000,
},
};
let max_energy = max_energy.unwrap_or(Energy {
energy: 50000,
});

let context = ContractContext {
invoker: Some(Address::Account(self.key.address)),
Expand All @@ -301,7 +292,7 @@ impl Deployer {
energy: max_energy,
};

let result = self.client.clone().invoke_instance(&best_block_hash, &context).await?;
let result = self.client.invoke_instance(&best_block_hash, &context).await?;

match result.response {
InvokeContractResult::Failure {
Expand All @@ -328,10 +319,10 @@ impl Deployer {

/// A function to get the current nonce of the wallet account.
pub async fn get_nonce(
&self,
&mut self,
address: AccountAddress,
) -> Result<AccountNonceResponse, DeployError> {
let nonce = self.client.clone().get_next_account_sequence_number(&address).await?;
let nonce = self.client.get_next_account_sequence_number(&address).await?;
Ok(nonce)
}

Expand Down
4 changes: 2 additions & 2 deletions templates/default/deploy-scripts/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ struct App {
async fn main() -> Result<(), DeployError> {
let app: App = App::parse();

let concordium_client = v2::Client::new(app.url).await?;
let mut concordium_client = v2::Client::new(app.url).await?;

let deployer = Deployer::new(concordium_client, &app.key_file)?;
let mut deployer = Deployer::new(&mut concordium_client, &app.key_file)?;

let mut modules_deployed: Vec<ModuleDeployed> = Vec::new();

Expand Down

0 comments on commit 94c1859

Please sign in to comment.