Skip to content

Commit

Permalink
Pass arguments to deploy/call
Browse files Browse the repository at this point in the history
  • Loading branch information
pmikolajczyk41 committed Jun 28, 2023
1 parent 5ffe100 commit e66c440
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 40 deletions.
2 changes: 2 additions & 0 deletions drink-cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ pub enum CliCommand {
Deploy {
#[clap(long, default_value = "new")]
constructor: String,
args: Vec<String>,
#[clap(long, default_values_t = Vec::<u8>::new(), value_delimiter = ',')]
salt: Vec<u8>,
},
Call {
message: String,
args: Vec<String>,
},
}

Expand Down
34 changes: 17 additions & 17 deletions drink-cli/src/executor/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::{env, fs, path::PathBuf};

use contract_transcode::ContractMessageTranscoder;
use drink::contract_api::ContractApi;
use sp_core::blake2_256;

use crate::app_state::{AppState, Contract};

Expand All @@ -26,7 +25,7 @@ pub fn build(app_state: &mut AppState) {
}
}

pub fn deploy(app_state: &mut AppState, constructor: String, salt: Vec<u8>) {
pub fn deploy(app_state: &mut AppState, constructor: String, args: Vec<String>, salt: Vec<u8>) {
// Get raw contract bytes
let Some((contract_name, contract_file)) = find_wasm_blob() else {
app_state.print_error("Failed to find contract file");
Expand All @@ -53,10 +52,13 @@ pub fn deploy(app_state: &mut AppState, constructor: String, salt: Vec<u8>) {
};

// Try deploying
let result =
app_state
.sandbox
.deploy_contract(contract_bytes, compute_selector(&constructor), salt);
let Ok(data) = transcode.encode(&constructor, args) else {
app_state.print_error("Failed to encode call data.");
return;
};
let result = app_state
.sandbox
.deploy_contract(contract_bytes, data, salt);
app_state.print_contract_action(&result);

// Check if call has been executed successfully
Expand Down Expand Up @@ -87,16 +89,19 @@ pub fn deploy(app_state: &mut AppState, constructor: String, salt: Vec<u8>) {
app_state.print("Contract deployed successfully");
}

pub fn call(app_state: &mut AppState, message: String) {
let Some(account_id) = app_state.contracts.current_contract()
.map(|c| c.address.clone()) else {
pub fn call(app_state: &mut AppState, message: String, args: Vec<String>) {
let Some(contract) = app_state.contracts.current_contract() else {
app_state.print_error("No deployed contract");
return;
};

let result = app_state
.sandbox
.call_contract(account_id, compute_selector(&message));
let account_id = contract.address.clone();
let Ok(data) = contract.transcode.encode(&message, args) else {
app_state.print_error("Failed to encode call data");
return;
};

let result = app_state.sandbox.call_contract(account_id, data);
app_state.print_contract_action(&result);

match result.result {
Expand Down Expand Up @@ -150,8 +155,3 @@ fn find_wasm_blob() -> Option<(String, PathBuf)> {

Some((raw_name, file.path()))
}

fn compute_selector(name: &str) -> Vec<u8> {
let name = name.as_bytes();
blake2_256(name)[..4].to_vec()
}
8 changes: 6 additions & 2 deletions drink-cli/src/executor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,12 @@ pub fn execute(app_state: &mut AppState) -> Result<()> {
CliCommand::AddTokens { recipient, value } => add_tokens(app_state, recipient, value),

CliCommand::Build => contract::build(app_state),
CliCommand::Deploy { constructor, salt } => contract::deploy(app_state, constructor, salt),
CliCommand::Call { message } => contract::call(app_state, message),
CliCommand::Deploy {
constructor,
args,
salt,
} => contract::deploy(app_state, constructor, args, salt),
CliCommand::Call { message, args } => contract::call(app_state, message, args),
}

Ok(())
Expand Down
20 changes: 5 additions & 15 deletions drink/src/contract_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@ pub trait ContractApi {
fn deploy_contract(
&mut self,
contract_bytes: Vec<u8>,
selector: Vec<u8>,
data: Vec<u8>,
salt: Vec<u8>,
) -> ContractInstantiateResult<AccountId32, u128>;

fn call_contract(
&mut self,
address: AccountId32,
selector: Vec<u8>,
) -> ContractExecResult<u128>;
fn call_contract(&mut self, address: AccountId32, data: Vec<u8>) -> ContractExecResult<u128>;
}

pub const GAS_LIMIT: Weight = Weight::from_parts(100_000_000_000, 3 * 1024 * 1024);
Expand All @@ -25,11 +21,9 @@ impl ContractApi for Sandbox {
fn deploy_contract(
&mut self,
contract_bytes: Vec<u8>,
selector: Vec<u8>,
data: Vec<u8>,
salt: Vec<u8>,
) -> ContractInstantiateResult<AccountId32, u128> {
let mut data = selector;
data.extend_from_slice(&[1; 32]);
self.externalities.execute_with(|| {
Contracts::bare_instantiate(
ALICE,
Expand All @@ -44,19 +38,15 @@ impl ContractApi for Sandbox {
})
}

fn call_contract(
&mut self,
address: AccountId32,
selector: Vec<u8>,
) -> ContractExecResult<u128> {
fn call_contract(&mut self, address: AccountId32, data: Vec<u8>) -> ContractExecResult<u128> {
self.externalities.execute_with(|| {
Contracts::bare_call(
ALICE,
address,
0,
GAS_LIMIT,
None,
selector,
data,
true,
Determinism::Enforced,
)
Expand Down
6 changes: 3 additions & 3 deletions examples/counter/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ mod counter {
}

#[ink(message)]
pub fn bump(&mut self) {
pub fn bump(&mut self, by: u32) {
debug_println!("Previous value: `{}`", self.value);
self.value += 1;
debug_println!("Bumped to: `{}`", self.value);
self.value += by;
debug_println!("Bumped to: `{}`", self.value);
}

#[ink(message)]
Expand Down
6 changes: 3 additions & 3 deletions examples/flipper/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ mod flipper {

impl Flipper {
#[ink(constructor)]
pub fn new() -> Self {
Self { value: false }
pub fn new(init: bool) -> Self {
Self { value: init }
}

#[ink(message)]
pub fn flip(&mut self) {
debug_println!("Previous value: `{}`", self.value);
self.value = !self.value;
debug_println!("Flipped to: `{}`", self.value);
debug_println!("Flipped to: `{}`", self.value);
}

#[ink(message)]
Expand Down

0 comments on commit e66c440

Please sign in to comment.