Skip to content

Commit

Permalink
unified - bf_interact
Browse files Browse the repository at this point in the history
  • Loading branch information
BiancaIalangi committed May 30, 2024
1 parent c81a6c4 commit 155b621
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
gateway = 'https://testnet-gateway.multiversx.com'
gateway = 'https://devnet-gateway.multiversx.com'
72 changes: 36 additions & 36 deletions contracts/feature-tests/basic-features/interact/src/bf_interact.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
#![allow(deprecated)] // TODO: unified syntax

mod bf_interact_cli;
mod bf_interact_config;
mod bf_interact_state;

use basic_features::{
storage_direct_load::ProxyTrait as _, storage_direct_store::ProxyTrait as _, ProxyTrait,
};
use basic_features::basic_features_proxy;
use bf_interact_config::Config;
use bf_interact_state::State;
use clap::Parser;
Expand Down Expand Up @@ -36,7 +32,7 @@ async fn main() {
#[allow(unused)]
struct BasicFeaturesInteract {
interactor: Interactor,
wallet_address: Address,
wallet_address: Bech32Address,
code_expr: BytesValue,
state: State,
large_storage_payload: Vec<u8>,
Expand All @@ -57,7 +53,7 @@ impl BasicFeaturesInteract {

Self {
interactor,
wallet_address,
wallet_address: wallet_address.into(),
code_expr,
state: State::load_state(),
large_storage_payload: Vec::new(),
Expand All @@ -74,56 +70,60 @@ impl BasicFeaturesInteract {
}

async fn set_state(&mut self) {
println!("wallet address: {}", bech32::encode(&self.wallet_address));
self.interactor
.retrieve_account(&Bech32Address::from(&self.wallet_address))
.await;
println!("wallet address: {}", self.wallet_address);
self.interactor.retrieve_account(&self.wallet_address).await;
}

async fn deploy(&mut self) {
self.set_state().await;

let (new_address, _) = self
let new_address = self
.interactor
.sc_deploy_get_result::<_, IgnoreValue>(
ScDeployStep::new()
.call(self.state.default_contract().init())
.from(&self.wallet_address)
.code(&self.code_expr)
.gas_limit("4,000,000")
.expect(TxExpect::ok().additional_error_message("deploy failed: ")),
)
.tx()
.from(&self.wallet_address)
.typed(basic_features_proxy::BasicFeaturesProxy)
.init()
.code(&self.code_expr)
.gas(NumExpr("4,000,000"))
.returns(ReturnsNewBech32Address)
.prepare_async()
.run()
.await;

let new_address_bech32 = bech32::encode(&new_address);
println!("new address: {new_address_bech32}");
println!("new address: {new_address}");

let new_address_expr = format!("bech32:{new_address_bech32}");
self.state.set_bf_address(&new_address_expr);
self.state.set_bf_address(new_address);
}

async fn set_large_storage(&mut self, value: &[u8]) {
self.interactor
.sc_call(
ScCallStep::new()
.call(self.state.bf_contract().store_bytes(value))
.from(&self.wallet_address)
.gas_limit("600,000,000")
.expect(
TxExpect::ok()
.additional_error_message("performing store_bytes failed with: "),
),
)
.tx()
.from(&self.wallet_address)
.to(self.state.bf_contract())
.gas(NumExpr("600,000,000"))
.typed(basic_features_proxy::BasicFeaturesProxy)
.store_bytes(value)
.prepare_async()
.run()
.await;

println!("successfully performed store_bytes");
}

async fn print_length(&mut self) {
let data: Vec<u8> = self
let data_raw = self
.interactor
.quick_query(self.state.bf_contract().load_bytes())
.query()
.to(self.state.bf_contract())
.typed(basic_features_proxy::BasicFeaturesProxy)
.load_bytes()
.returns(ReturnsResult)
.prepare_async()
.run()
.await;

let data = data_raw.to_vec();

println!("retrieved data length: {}", data.len());
if data != self.large_storage_payload {
println!("WARNING! Payload mismatch!");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
use crate::{ContractInfo, StaticApi};
use multiversx_sc_snippets::imports::Bech32Address;
use serde::{Deserialize, Serialize};
use std::{
io::{Read, Write},
path::Path,
};

const DEFAULT_CONTRACT_ADDRESS: &str =
"0x0000000000000000000000000000000000000000000000000000000000000000";

/// State file
const STATE_FILE: &str = "state.toml";

pub type BasicFeaturesContract = ContractInfo<basic_features::Proxy<StaticApi>>;

/// Multisig Interact state
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct State {
bf_address: Option<String>,
bf_address: Option<Bech32Address>,
}

impl State {
Expand All @@ -33,22 +28,15 @@ impl State {
}

/// Sets the contract address
pub fn set_bf_address(&mut self, address: &str) {
self.bf_address = Some(String::from(address));
pub fn set_bf_address(&mut self, address: Bech32Address) {
self.bf_address = Some(address);
}

/// Returns the contract
pub fn bf_contract(&self) -> BasicFeaturesContract {
BasicFeaturesContract::new(
self.bf_address
.clone()
.expect("basic-features contract not yet deployed"),
)
}

/// Returns the adder contract with default address
pub fn default_contract(&self) -> BasicFeaturesContract {
BasicFeaturesContract::new(DEFAULT_CONTRACT_ADDRESS)
pub fn bf_contract(&self) -> &Bech32Address {
self.bf_address
.as_ref()
.expect("basic-features contract not yet deployed")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

multiversx_sc::imports!();

pub mod basic_features_proxy;
pub mod big_num_methods;
pub mod big_num_operators;
pub mod block_info_features;
Expand Down
131 changes: 131 additions & 0 deletions contracts/feature-tests/basic-features/src/basic_features_proxy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
use multiversx_sc::proxy_imports::*;

pub struct BasicFeaturesProxy;

impl<Env, From, To, Gas> TxProxyTrait<Env, From, To, Gas> for BasicFeaturesProxy
where
Env: TxEnv,
From: TxFrom<Env>,
To: TxTo<Env>,
Gas: TxGas<Env>,
{
type TxProxyMethods = BasicFeaturesProxyMethods<Env, From, To, Gas>;

fn proxy_methods(self, tx: Tx<Env, From, To, (), Gas, (), ()>) -> Self::TxProxyMethods {
BasicFeaturesProxyMethods { wrapped_tx: tx }
}
}

pub struct BasicFeaturesProxyMethods<Env, From, To, Gas>
where
Env: TxEnv,
From: TxFrom<Env>,
To: TxTo<Env>,
Gas: TxGas<Env>,
{
wrapped_tx: Tx<Env, From, To, (), Gas, (), ()>,
}

#[rustfmt::skip]
impl<Env, From, Gas> BasicFeaturesProxyMethods<Env, From, (), Gas>
where
Env: TxEnv,
Env::Api: VMApi,
From: TxFrom<Env>,
Gas: TxGas<Env>,
{
pub fn init(
self,
) -> TxTypedDeploy<Env, From, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_deploy()
.original_result()
}
}

#[rustfmt::skip]
impl<Env, From, To, Gas> BasicFeaturesProxyMethods<Env, From, To, Gas>
where
Env: TxEnv,
Env::Api: VMApi,
From: TxFrom<Env>,
To: TxTo<Env>,
Gas: TxGas<Env>,
{
pub fn load_bytes(
self,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ManagedBuffer<Env::Api>> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("load_bytes")
.original_result()
}

pub fn store_bytes<
Arg0: ProxyArg<ManagedBuffer<Env::Api>>,
>(
self,
bi: Arg0,
) -> TxTypedCall<Env, From, To, NotPayable, Gas, ()> {
self.wrapped_tx
.payment(NotPayable)
.raw_call("store_bytes")
.argument(&bi)
.original_result()
}
}

#[type_abi]
#[derive(TopEncode, TopDecode)]
pub struct CodecErrorTestType {}

#[rustfmt::skip]
#[type_abi]
#[derive(NestedEncode, NestedDecode, TopEncode, TopDecode)]
pub enum ExampleEnumWithFields {
Unit,
Newtype(u32),
Tuple(u32, u32),
Struct {
a: u32,
},
}

#[type_abi]
#[derive(NestedEncode, NestedDecode, TopEncode, TopDecode)]
pub enum ExampleEnumSimple {
Variant0,
Variant1,
Variant2,
}

#[type_abi]
#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode)]
pub struct TokenAttributesStruct<Api>
where
Api: ManagedTypeApi,
{
pub field_biguint: BigUint<Api>,
pub field_u64: u64,
pub field_vec_u32: ManagedVec<Api, u32>,
}

#[type_abi]
#[derive(TopEncode, TopDecode)]
pub struct RgbColor {
pub r: u8,
pub g: u8,
pub b: u8,
}

#[type_abi]
#[derive(NestedEncode, NestedDecode, TopEncode, TopDecode, PartialEq, Eq, Debug, Clone)]
pub struct ExampleStructManaged<Api>
where
Api: ManagedTypeApi,
{
pub big_uint: BigUint<Api>,
pub int: u32,
pub bytes: ManagedBuffer<Api>,
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

#[derive(TypeAbi, TopEncode, TopDecode)]
#[type_abi]
#[derive(TopEncode, TopDecode)]
pub struct RgbColor {
r: u8,
g: u8,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, TypeAbi)]
#[type_abi]
#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode)]
pub struct TokenAttributesStruct<M: ManagedTypeApi> {
field_biguint: BigUint<M>,
field_u64: u64,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
multiversx_sc::derive_imports!();

/// Copied from multiversx-sc serialization tests.
#[derive(NestedEncode, NestedDecode, TopEncode, TopDecode, TypeAbi)]
#[type_abi]
#[derive(NestedEncode, NestedDecode, TopEncode, TopDecode)]
pub enum ExampleEnumSimple {
/// Variant 0 doc comment.
/// This will show up in the ABI.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
multiversx_sc::derive_imports!();

/// Copied from multiversx-sc serialization tests.
#[derive(NestedEncode, NestedDecode, TopEncode, TopDecode, TypeAbi)]
#[type_abi]
#[derive(NestedEncode, NestedDecode, TopEncode, TopDecode)]
pub enum ExampleEnumWithFields {
Unit,
Newtype(u32),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use multiversx_sc::{

multiversx_sc::derive_imports!();

#[derive(
NestedEncode, NestedDecode, TopEncode, TopDecode, TypeAbi, PartialEq, Eq, Debug, Clone,
)]
#[type_abi]
#[derive(NestedEncode, NestedDecode, TopEncode, TopDecode, PartialEq, Eq, Debug, Clone)]
pub struct ExampleStructManaged<M: ManagedTypeApi> {
pub big_uint: BigUint<M>,
pub int: u32,
Expand Down

0 comments on commit 155b621

Please sign in to comment.