Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contracts upgrade 0.54.0 #128

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
284 changes: 108 additions & 176 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions contracts/adder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ publish = false
path = "src/adder.rs"

[dependencies.multiversx-sc]
version = "0.53.2"
version = "0.54.3"

[dev-dependencies.multiversx-sc-scenario]
version = "0.53.2"
version = "0.54.3"
13 changes: 11 additions & 2 deletions contracts/adder/interact/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[[bin]]
name = "basic-interact"
path = "src/basic_interact.rs"
path = "src/basic_interact_main.rs"

[package]
name = "basic-interact"
Expand All @@ -9,6 +9,9 @@ authors = ["Ovidiu Stinga <[email protected]>"]
edition = "2021"
publish = false

[lib]
path = "src/basic_interact.rs"

[dependencies]
toml = "0.8.6"

Expand All @@ -20,8 +23,14 @@ features = ["derive"]
version = "1.0"
features = ["derive"]

[dependencies.tokio]
version = "1.24"

[dependencies.adder]
path = ".."

[dependencies.multiversx-sc-snippets]
version = "0.53.2"
version = "0.54.3"

[features]
chain-simulator-tests = []
6 changes: 5 additions & 1 deletion contracts/adder/interact/config.toml
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
gateway = 'https://devnet-gateway.multiversx.com'
# chain_type = 'simulator'
# gateway_uri = 'http://localhost:8085'

chain_type = 'real'
gateway_uri = 'https://devnet-gateway.multiversx.com'
177 changes: 135 additions & 42 deletions contracts/adder/interact/src/basic_interact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,32 @@
mod basic_interact_config;
mod basic_interact_state;

use crate::basic_interact_state::State;
use adder::adder_proxy;
use basic_interact_config::Config;
use basic_interact_state::State;
pub use basic_interact_config::Config;
use clap::Parser;

use multiversx_sc_snippets::imports::*;

const INTERACTOR_SCENARIO_TRACE_PATH: &str = "interactor_trace.scen.json";

#[tokio::main]
async fn main() {
const ADDER_CODE_PATH: MxscPath = MxscPath::new("../output/adder.mxsc.json");

pub async fn adder_cli() {
env_logger::init();

let mut basic_interact = AdderInteract::init().await;
let config = Config::load_config();

let mut basic_interact = AdderInteract::init(config).await;

let cli = basic_interact_cli::InteractCli::parse();
match &cli.command {
Some(basic_interact_cli::InteractCliCommand::Add(args)) => {
basic_interact.add(args.value).await;
if args.count == 1 {
basic_interact.add(args.value).await;
} else {
basic_interact.multi_add(args.value, args.count).await;
}
}
Some(basic_interact_cli::InteractCliCommand::Deploy) => {
basic_interact.deploy().await;
Expand All @@ -29,50 +36,62 @@
basic_interact.feed_contract_egld().await;
}
Some(basic_interact_cli::InteractCliCommand::MultiDeploy(args)) => {
basic_interact.multi_deploy(&args.count).await;
basic_interact.multi_deploy(args.count).await;
}
Some(basic_interact_cli::InteractCliCommand::Sum) => {
basic_interact.print_sum().await;
let sum = basic_interact.get_sum().await;
println!("sum: {sum}");
}
Some(basic_interact_cli::InteractCliCommand::Upgrade(args)) => {
let owner_address = basic_interact.adder_owner_address.clone();
basic_interact
.upgrade(args.value, &owner_address, None)
.await
}
None => {}
}
}

#[allow(unused)]
struct AdderInteract {
interactor: Interactor,
wallet_address: Bech32Address,
adder_code: BytesValue,
state: State,
pub struct AdderInteract {
pub interactor: Interactor,
pub adder_owner_address: Bech32Address,
pub wallet_address: Bech32Address,
pub state: State,
}

impl AdderInteract {
async fn init() -> Self {
let config = Config::load_config();
let mut interactor = Interactor::new(config.gateway())
pub async fn init(config: Config) -> Self {
let mut interactor = Interactor::new(config.gateway_uri(), config.use_chain_simulator())

Check failure on line 65 in contracts/adder/interact/src/basic_interact.rs

View workflow job for this annotation

GitHub Actions / Contracts / Rust tests

this function takes 1 argument but 2 arguments were supplied

Check failure on line 65 in contracts/adder/interact/src/basic_interact.rs

View workflow job for this annotation

GitHub Actions / Contracts / Test Coverage

this function takes 1 argument but 2 arguments were supplied

Check failure on line 65 in contracts/adder/interact/src/basic_interact.rs

View workflow job for this annotation

GitHub Actions / Contracts / Wasm tests

this function takes 1 argument but 2 arguments were supplied

Check failure on line 65 in contracts/adder/interact/src/basic_interact.rs

View workflow job for this annotation

GitHub Actions / clippy

[clippy] contracts/adder/interact/src/basic_interact.rs#L65

error[E0061]: this function takes 1 argument but 2 arguments were supplied --> contracts/adder/interact/src/basic_interact.rs:65:30 | 65 | let mut interactor = Interactor::new(config.gateway_uri(), config.use_chain_simulator()) | ^^^^^^^^^^^^^^^ ---------------------------- unexpected argument #2 of type `bool` | note: associated function defined here --> /home/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/multiversx-sc-snippets-0.54.3/src/interactor/interactor_base.rs:40:18 | 40 | pub async fn new(gateway_uri: &str) -> Self { | ^^^ help: remove the extra argument | 65 - let mut interactor = Interactor::new(config.gateway_uri(), config.use_chain_simulator()) 65 + let mut interactor = Interactor::new(config.gateway_uri()) |
Raw output
contracts/adder/interact/src/basic_interact.rs:65:68:e:error[E0061]: this function takes 1 argument but 2 arguments were supplied
  --> contracts/adder/interact/src/basic_interact.rs:65:30
   |
65 |         let mut interactor = Interactor::new(config.gateway_uri(), config.use_chain_simulator())
   |                              ^^^^^^^^^^^^^^^                       ---------------------------- unexpected argument #2 of type `bool`
   |
note: associated function defined here
  --> /home/runner/.cargo/registry/src/index.crates.io-6f17d22bba15001f/multiversx-sc-snippets-0.54.3/src/interactor/interactor_base.rs:40:18
   |
40 |     pub async fn new(gateway_uri: &str) -> Self {
   |                  ^^^
help: remove the extra argument
   |
65 -         let mut interactor = Interactor::new(config.gateway_uri(), config.use_chain_simulator())
65 +         let mut interactor = Interactor::new(config.gateway_uri())
   |


__END__
.await
.with_tracer(INTERACTOR_SCENARIO_TRACE_PATH)
.await;
let wallet_address = interactor.register_wallet(test_wallets::mike());
let adder_code = BytesValue::interpret_from(
"mxsc:../output/adder.mxsc.json",
&InterpreterContext::default(),
);

interactor.set_current_dir_from_workspace("contracts/adder/interact");

let adder_owner_address = interactor.register_wallet(test_wallets::alice()).await;
let wallet_address = interactor.register_wallet(test_wallets::frank()).await;

// generate blocks until ESDTSystemSCAddress is enabled
interactor.generate_blocks_until_epoch(1).await.unwrap();

Self {
interactor,
adder_owner_address: adder_owner_address.into(),
wallet_address: wallet_address.into(),
adder_code,
state: State::load_state(),
}
}

async fn set_state(&mut self) {
pub async fn set_state(&mut self) {
println!("wallet address: {}", self.wallet_address);
self.interactor
.retrieve_account(&self.adder_owner_address)
.await;
self.interactor.retrieve_account(&self.wallet_address).await;
}

async fn deploy(&mut self) {
pub async fn deploy(&mut self) {
// warning: multi deploy not yet fully supported
// only works with last deployed address

Expand All @@ -81,21 +100,22 @@
let new_address = self
.interactor
.tx()
.from(&self.wallet_address)
.from(&self.adder_owner_address)
.gas(6_000_000)
.typed(adder_proxy::AdderProxy)
.init(0u32)
.code(&self.adder_code)
.code(ADDER_CODE_PATH)
.code_metadata(CodeMetadata::UPGRADEABLE)
.returns(ReturnsNewBech32Address)
.prepare_async()
.run()
.await;

println!("new address: {new_address}");
self.state.set_adder_address(new_address);
}

async fn multi_deploy(&mut self, count: &u8) {
if *count == 0 {
pub async fn multi_deploy(&mut self, count: usize) {
if count == 0 {
println!("count must be greater than 0");
return;
}
Expand All @@ -104,13 +124,13 @@
println!("deploying {count} contracts...");

let mut buffer = self.interactor.homogenous_call_buffer();
for _ in 0..*count {
for _ in 0..count {
buffer.push_tx(|tx| {
tx.from(&self.wallet_address)
.typed(adder_proxy::AdderProxy)
.init(0u32)
.code(&self.adder_code)
.gas(NumExpr("70,000,000"))
.code(ADDER_CODE_PATH)
.gas(6_000_000)
.returns(ReturnsNewBech32Address)
});
}
Expand All @@ -127,43 +147,116 @@
}
}

async fn feed_contract_egld(&mut self) {
pub async fn multi_add(&mut self, value: u32, count: usize) {
self.set_state().await;
println!("calling contract {count} times...");

let mut buffer = self.interactor.homogenous_call_buffer();
for _ in 0..count {
buffer.push_tx(|tx| {
tx.from(&self.wallet_address)
.to(self.state.current_adder_address())
.typed(adder_proxy::AdderProxy)
.add(value)
.returns(ReturnsGasUsed)
.gas(6_000_000)
});
}

let gas_used = buffer.run().await;
let gas_used_sum = gas_used.iter().sum::<u64>();

println!(
"successfully performed add {count} times, total gas used: {}, avg gas used: {}",
gas_used_sum,
gas_used_sum / count as u64
);
}

pub async fn feed_contract_egld(&mut self) {
self.interactor
.tx()
.from(&self.wallet_address)
.to(self.state.current_adder_address())
.egld(NumExpr("0,050000000000000000"))
.prepare_async()
.run()
.await;
}

async fn add(&mut self, value: u64) {
pub async fn add(&mut self, value: u32) {
self.interactor
.tx()
.from(&self.wallet_address)
.to(self.state.current_adder_address())
.gas(6_000_000)
.typed(adder_proxy::AdderProxy)
.add(value)
.prepare_async()
.run()
.await;

println!("successfully performed add");
}

async fn print_sum(&mut self) {
let sum = self
.interactor
pub async fn get_sum(&mut self) -> RustBigUint {
self.interactor
.query()
.to(self.state.current_adder_address())
.typed(adder_proxy::AdderProxy)
.sum()
.returns(ReturnsResultUnmanaged)
.prepare_async()
.run()
.await;
.await
}

pub async fn upgrade(
&mut self,
new_value: u32,
sender: &Bech32Address,
expected_result: Option<(u64, &str)>,
) {
match expected_result {
Some((code, msg)) => {
let response = self
.interactor
.tx()
.from(sender)
.to(self.state.current_adder_address())
.gas(6_000_000)
.typed(adder_proxy::AdderProxy)
.upgrade(new_value)
.code_metadata(CodeMetadata::UPGRADEABLE)
.code(ADDER_CODE_PATH)
.returns(ExpectError(code, msg))
.run()
.await;

println!("response: {response:?}");
}
None => {
self.interactor
.tx()
.from(sender)
.to(self.state.current_adder_address())
.gas(6_000_000)
.typed(adder_proxy::AdderProxy)
.upgrade(new_value)
.code_metadata(CodeMetadata::UPGRADEABLE)
.code(ADDER_CODE_PATH)
.run()
.await;

let sum = self
.interactor
.query()
.to(self.state.current_adder_address())
.typed(adder_proxy::AdderProxy)
.sum()
.returns(ReturnsResultUnmanaged)
.run()
.await;

println!("sum: {sum}");
assert_eq!(sum, RustBigUint::from(new_value));
}
}
}
}
21 changes: 17 additions & 4 deletions contracts/adder/interact/src/basic_interact_cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,31 @@ pub enum InteractCliCommand {
MultiDeploy(MultiDeployArgs),
#[command(name = "sum", about = "Print sum")]
Sum,
#[command(name = "upgrade", about = "Upgrade contract")]
Upgrade(UpgradeArgs),
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct AddArgs {
/// The value to add
#[arg(short = 'v', long = "value", verbatim_doc_comment)]
pub value: u64,
#[arg(short = 'v', long = "value")]
pub value: u32,

/// Repeat this number of times
#[arg(short = 'c', long = "count", default_value = "1")]
pub count: usize,
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct UpgradeArgs {
/// The value to add
#[arg(short = 'v', long = "value")]
pub value: u32,
}

#[derive(Default, Clone, PartialEq, Eq, Debug, Args)]
pub struct MultiDeployArgs {
/// The number of contracts to deploy
#[arg(short = 'c', long = "count", verbatim_doc_comment)]
pub count: u8,
#[arg(short = 'c', long = "count")]
pub count: usize,
}
Loading
Loading