Skip to content

Commit 8f5088f

Browse files
Merge branch 'rc/v0.53' into system-func-calls-interactor
2 parents e711f65 + 5724b7d commit 8f5088f

File tree

5 files changed

+117
-44
lines changed

5 files changed

+117
-44
lines changed

contracts/examples/adder/interact/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Pem files are used for interactions, but shouldn't be committed
22
*.pem
3+
*.json
34
!adder-owner.pem
45

56
# Temporary storage of deployed contract address, so we can preserve the context between executions.

contracts/examples/adder/interact/src/basic_interact.rs

+88-37
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ async fn main() {
4141
basic_interact.print_sum().await;
4242
},
4343
Some(basic_interact_cli::InteractCliCommand::Upgrade(args)) => {
44-
basic_interact.upgrade(args.value).await
44+
let owner_address = basic_interact.adder_owner_address.clone();
45+
basic_interact
46+
.upgrade(args.value, &owner_address, None)
47+
.await
4548
},
4649
None => {},
4750
}
@@ -66,8 +69,14 @@ impl AdderInteract {
6669
let adder_owner_address =
6770
interactor.register_wallet(Wallet::from_pem_file("adder-owner.pem").unwrap());
6871
// PASSWORD: "alice"
69-
let wallet_address = interactor
70-
.register_wallet(Wallet::from_keystore_secret("alice.json", "alice").unwrap());
72+
// InsertPassword::Plaintext("alice".to_string()) || InsertPassword::StandardInput
73+
let wallet_address = interactor.register_wallet(
74+
Wallet::from_keystore_secret(
75+
"alice.json",
76+
InsertPassword::Plaintext("alice".to_string()),
77+
)
78+
.unwrap(),
79+
);
7180

7281
Self {
7382
interactor,
@@ -95,7 +104,7 @@ impl AdderInteract {
95104
.interactor
96105
.tx()
97106
.from(&self.adder_owner_address)
98-
.gas(3_000_000)
107+
.gas(6_000_000)
99108
.typed(adder_proxy::AdderProxy)
100109
.init(0u32)
101110
.code(ADDER_CODE_PATH)
@@ -125,7 +134,7 @@ impl AdderInteract {
125134
.typed(adder_proxy::AdderProxy)
126135
.init(0u32)
127136
.code(ADDER_CODE_PATH)
128-
.gas(3_000_000)
137+
.gas(6_000_000)
129138
.returns(ReturnsNewBech32Address)
130139
});
131140
}
@@ -153,7 +162,7 @@ impl AdderInteract {
153162
.to(self.state.current_adder_address())
154163
.typed(adder_proxy::AdderProxy)
155164
.add(value)
156-
.gas(3_000_000)
165+
.gas(6_000_000)
157166
});
158167
}
159168

@@ -178,7 +187,7 @@ impl AdderInteract {
178187
.tx()
179188
.from(&self.wallet_address)
180189
.to(self.state.current_adder_address())
181-
.gas(3_000_000)
190+
.gas(6_000_000)
182191
.typed(adder_proxy::AdderProxy)
183192
.add(value)
184193
.prepare_async()
@@ -203,45 +212,87 @@ impl AdderInteract {
203212
println!("sum: {sum}");
204213
}
205214

206-
async fn upgrade(&mut self, new_value: u32) {
207-
let response = self
208-
.interactor
209-
.tx()
210-
.from(&self.wallet_address)
211-
.to(self.state.current_adder_address())
212-
.gas(3_000_000)
213-
.typed(adder_proxy::AdderProxy)
214-
.upgrade(BigUint::from(new_value))
215-
.code_metadata(CodeMetadata::UPGRADEABLE)
216-
.code(ADDER_CODE_PATH)
217-
.returns(ReturnsResultUnmanaged)
218-
.prepare_async()
219-
.run()
220-
.await;
221-
222-
let sum = self
223-
.interactor
224-
.query()
225-
.to(self.state.current_adder_address())
226-
.typed(adder_proxy::AdderProxy)
227-
.sum()
228-
.returns(ReturnsResultUnmanaged)
229-
.prepare_async()
230-
.run()
231-
.await;
232-
assert_eq!(sum, RustBigUint::from(new_value));
215+
async fn upgrade(
216+
&mut self,
217+
new_value: u32,
218+
sender: &Bech32Address,
219+
expected_result: Option<(u64, &str)>,
220+
) {
221+
match expected_result {
222+
Some((code, msg)) => {
223+
let response = self
224+
.interactor
225+
.tx()
226+
.from(sender)
227+
.to(self.state.current_adder_address())
228+
.gas(6_000_000)
229+
.typed(adder_proxy::AdderProxy)
230+
.upgrade(new_value)
231+
.code_metadata(CodeMetadata::UPGRADEABLE)
232+
.code(ADDER_CODE_PATH)
233+
.returns(ExpectError(code, msg))
234+
.prepare_async()
235+
.run()
236+
.await;
237+
238+
println!("response: {response:?}");
239+
},
240+
None => {
241+
self.interactor
242+
.tx()
243+
.from(sender)
244+
.to(self.state.current_adder_address())
245+
.gas(6_000_000)
246+
.typed(adder_proxy::AdderProxy)
247+
.upgrade(new_value)
248+
.code_metadata(CodeMetadata::UPGRADEABLE)
249+
.code(ADDER_CODE_PATH)
250+
.prepare_async()
251+
.run()
252+
.await;
233253

234-
println!("response: {response:?}");
254+
let sum = self
255+
.interactor
256+
.query()
257+
.to(self.state.current_adder_address())
258+
.typed(adder_proxy::AdderProxy)
259+
.sum()
260+
.returns(ReturnsResultUnmanaged)
261+
.prepare_async()
262+
.run()
263+
.await;
264+
265+
assert_eq!(sum, RustBigUint::from(new_value));
266+
},
267+
}
235268
}
236269
}
237270

238271
#[tokio::test]
239272
#[ignore = "run on demand"]
240-
async fn test() {
273+
async fn upgrade_test() {
241274
let mut basic_interact = AdderInteract::init().await;
275+
let wallet_address = basic_interact.wallet_address.clone();
276+
let adder_owner_address = basic_interact.adder_owner_address.clone();
277+
let error_not_owner = (4, "upgrade is allowed only for owner");
242278

243279
basic_interact.deploy().await;
244280
basic_interact.add(1u32).await;
245281

246-
basic_interact.upgrade(7u32).await;
282+
// Sum will be 1
283+
basic_interact.print_sum().await;
284+
285+
basic_interact
286+
.upgrade(7u32, &adder_owner_address, None)
287+
.await;
288+
289+
// Sum will be the updated value of 7
290+
basic_interact.print_sum().await;
291+
292+
basic_interact
293+
.upgrade(10u32, &wallet_address, Some(error_not_owner))
294+
.await;
295+
296+
// Sum will remain 7
297+
basic_interact.print_sum().await;
247298
}

framework/snippets/src/imports.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ pub use crate::{
44
dns_address_for_name, test_wallets, Interactor, InteractorPrepareAsync, StepBuffer,
55
};
66

7-
pub use multiversx_sdk::wallet::Wallet;
7+
pub use multiversx_sdk::{
8+
wallet::Wallet,
9+
data::keystore::InsertPassword,
10+
};
811

912
pub use env_logger;
1013
pub use tokio;

sdk/core/src/data/keystore.rs

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ pub enum WalletError {
1212
InvalidKdf,
1313
InvalidCipher,
1414
}
15+
16+
#[derive(Debug)]
17+
pub enum InsertPassword {
18+
Plaintext(String),
19+
StandardInput,
20+
}
21+
1522
#[derive(Debug, Serialize, Deserialize)]
1623
pub struct CryptoParams {
1724
pub iv: String,

sdk/core/src/wallet.rs

+17-6
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,24 @@ impl Wallet {
156156
(private_key_str, public_key_str)
157157
}
158158

159-
pub fn from_keystore_secret(file_path: &str, password: &str) -> Result<Self> {
160-
let decyption_params = Self::validate_keystore_password(file_path, password.to_string())
161-
.unwrap_or_else(|e| {
162-
panic!("Error: {:?}", e);
163-
});
159+
pub fn from_keystore_secret(file_path: &str, insert_password: InsertPassword) -> Result<Self> {
160+
let decryption_params = match insert_password {
161+
InsertPassword::Plaintext(password) => {
162+
Self::validate_keystore_password(file_path, password.to_string()).unwrap_or_else(
163+
|e| {
164+
panic!("Error: {:?}", e);
165+
},
166+
)
167+
},
168+
InsertPassword::StandardInput => {
169+
Self::validate_keystore_password(file_path, Self::get_keystore_password())
170+
.unwrap_or_else(|e| {
171+
panic!("Error: {:?}", e);
172+
})
173+
},
174+
};
164175
let priv_key = PrivateKey::from_hex_str(
165-
hex::encode(Self::decrypt_secret_key(decyption_params)).as_str(),
176+
hex::encode(Self::decrypt_secret_key(decryption_params)).as_str(),
166177
)?;
167178
Ok(Self { priv_key })
168179
}

0 commit comments

Comments
 (0)