Skip to content

Commit

Permalink
snippets-dapp separated from snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Sep 26, 2024
1 parent 45384b3 commit f6b687c
Show file tree
Hide file tree
Showing 89 changed files with 4,857 additions and 87 deletions.
18 changes: 17 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [
"framework/meta-lib",
"framework/scenario",
"framework/snippets",
"framework/snippets-dapp",
"framework/wasm-adapter",

"sdk/core",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use multiversx_sc_snippets::imports::*;

use super::*;
Expand All @@ -18,15 +20,15 @@ impl MultisigInteract {
self.feed_contract_egld().await;
self.issue_collection().await;
self.set_special_role().await;
self.interactor.sleep(15u32).await;
self.interactor.sleep(Duration::from_secs(15)).await;
self.create_items().await;
}

pub async fn issue_multisig_and_collection_with_all_roles_full(&mut self) {
self.deploy().await;
self.feed_contract_egld().await;
self.issue_collection_with_all_roles().await;
self.interactor.sleep(15u32).await;
self.interactor.sleep(Duration::from_secs(15)).await;
self.create_items().await;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::time::Duration;

use multiversx_sc_snippets::imports::*;

use super::*;
Expand All @@ -10,7 +12,7 @@ impl MultisigInteract {
self.deploy().await;
self.feed_contract_egld().await;
self.wrap_egld().await;
self.interactor.sleep(15u32).await;
self.interactor.sleep(Duration::from_secs(15)).await;
self.unwrap_egld().await;
}

Expand Down
7 changes: 2 additions & 5 deletions contracts/examples/ping-pong-egld/dapp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,5 @@ yew = { version = "0.20.0", features = ["csr"] }
yew_icons = { version = "0.7.3", features = ["BootstrapHeartFill"] }
yew-router = { version = "0.17.0" }

# [dependencies.interactor]
# path = "interactor"

[dependencies.multiversx-sc-snippets]
path = "../../../../framework/snippets"
[dependencies.multiversx-sc-snippets-dapp]
path = "../../../../framework/snippets-dapp"
2 changes: 1 addition & 1 deletion contracts/examples/ping-pong-egld/dapp/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::requests::query;

use html::ChildrenProps;
use multiversx_sc_snippets::sdk::data::network_status::NetworkStatus;
use multiversx_sc_snippets_dapp::sdk::data::network_status::NetworkStatus;
use yew::prelude::*;

#[derive(Clone, Debug, PartialEq)]
Expand Down
2 changes: 1 addition & 1 deletion contracts/examples/ping-pong-egld/dapp/src/interactor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use imports::{Address, Bech32Address, BytesValue};
use multiversx_sc_snippets::*;
use multiversx_sc_snippets_dapp::*;
use serde::{Deserialize, Serialize};

const GATEWAY: &str = sdk::gateway::DEVNET_GATEWAY;
Expand Down
3 changes: 1 addition & 2 deletions contracts/examples/ping-pong-egld/dapp/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
mod components;
mod context;
mod interactor;
mod pages;
mod requests;
mod routes;
mod interactor;


use components::Footer;
use context::ConfigProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#![allow(clippy::all)]

use multiversx_sc::proxy_imports::*;
use multiversx_sc_snippets::multiversx_sc;
use multiversx_sc_snippets_dapp::multiversx_sc;

pub struct PingPongProxy;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::interactor::ContractInteract;
use multiversx_sc_snippets::sdk::data::network_status::NetworkStatus;
use multiversx_sc_snippets_dapp::sdk::data::network_status::NetworkStatus;

pub async fn get_network_status() -> Result<NetworkStatus, String> {
let contract_interact = ContractInteract::new().await;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::interactor::ContractInteract;
use multiversx_sc_snippets::{
use multiversx_sc_snippets_dapp::{
imports::{
Bech32Address, BigUint, IgnoreValue, OptionalValue, ReturnsMessage,
ReturnsNewBech32Address, ReturnsStatus,
Expand Down
39 changes: 39 additions & 0 deletions framework/snippets-dapp/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "multiversx-sc-snippets-dapp"
version = "0.53.0"
edition = "2021"

authors = ["MultiversX <[email protected]>"]
license = "GPL-3.0-only"
readme = "README.md"
repository = "https://github.com/multiversx/mx-sdk-rs"
homepage = "https://multiversx.com/"
documentation = "https://docs.multiversx.com/"
description = "MultiversX framework for building smart contract interaction snippets"
keywords = ["multiversx", "blockchain", "contract", "snippets"]
categories = ["cryptography::cryptocurrencies"]

[dependencies]
web-sys = { version = "0.3.70" }
js-sys = { version = "0.3.70" }
wasm-bindgen-futures = { version = "0.4.43" }
hex = "0.4"
base64 = "0.22"
log = "0.4.17"
env_logger = "0.11"
futures = "0.3"

[dependencies.multiversx-sc-scenario]
version = "=0.53.0"
path = "../scenario"

[dependencies.multiversx-chain-scenario-format]
version = "0.23.0"
path = "../../sdk/scenario-format"

[dependencies.multiversx-sdk-wbg]
version = "=0.6.0"
path = "../../sdk-wbg/core"

[dev-dependencies]
serde_json = "1.0"
7 changes: 7 additions & 0 deletions framework/snippets-dapp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# MultiversX transaction snippets

Provides basic functionality for interacting with smart contracts directly from Rust.

It a base on top of which little interaction programs (or snippets) can be written.

It is largely a wrapper around `elrond-sdk-erdrs` and `multiversx-chain-vm`.
102 changes: 102 additions & 0 deletions framework/snippets-dapp/src/account_tool.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
use multiversx_chain_scenario_format::interpret_trait::IntoRaw;
use multiversx_sc_scenario::{
imports::Bech32Address,
scenario_model::{Account, BytesKey, BytesValue, Scenario, SetStateStep, Step},
};
use multiversx_sdk_wbg::{
data::{address::Address, esdt::EsdtBalance},
gateway::GatewayProxy,
};
use std::collections::{BTreeMap, HashMap};

/// Called directly from CLI, from `sc-meta`.
///
/// Retrieves an account data via the API,
/// then formats it as a scenario set state step.
pub async fn print_account_as_scenario_set_state(
api_string: String,
address_bech32_string: String,
) {
let api = GatewayProxy::new(api_string);
let address = Bech32Address::from_bech32_string(address_bech32_string);
let set_state = retrieve_account_as_scenario_set_state(&api, &address).await;
let scenario = build_scenario(set_state);
println!("{}", scenario.into_raw().to_json_string());
}

fn build_scenario(set_state: SetStateStep) -> Scenario {
Scenario {
name: None,
comment: None,
check_gas: None,
steps: vec![Step::SetState(set_state)],
}
}

pub async fn retrieve_account_as_scenario_set_state(
api: &GatewayProxy,
address: &Bech32Address,
) -> SetStateStep {
let sdk_address = Address::from_bech32_string(address.to_bech32_str()).unwrap();
let sdk_account = api.get_account(&sdk_address).await.unwrap();

let account_esdt = api
.get_account_esdt_tokens(&sdk_address)
.await
.unwrap_or_else(|err| {
panic!("failed to retrieve ESDT tokens for address {address}: {err}")
});
let account_esdt_roles = api
.get_account_esdt_roles(&sdk_address)
.await
.unwrap_or_else(|err| panic!("failed to retrieve ESDT roles for address {address}: {err}"));
let account_storage = api
.get_account_storage_keys(&sdk_address)
.await
.unwrap_or_else(|err| panic!("failed to retrieve storage for address {address}: {err}"));

let account_state = set_account(
sdk_account,
account_storage,
account_esdt,
account_esdt_roles,
);

let set_state_step = SetStateStep::new();
set_state_step.put_account(address, account_state)
}

fn set_account(
account: multiversx_sdk_wbg::data::account::Account,
account_storage: HashMap<String, String>,
account_esdt: HashMap<String, EsdtBalance>,
account_esdt_roles: HashMap<String, Vec<String>>,
) -> Account {
let mut account_state = Account::new()
.nonce(account.nonce)
.balance(account.balance.as_str())
.code(account.code);
account_state.username = Some(format!("str:{}", account.username.as_str()).into());
account_state.storage = convert_storage(account_storage);

for (_, esdt_balance) in account_esdt.iter() {
let token_id_expr = format!("str:{}", esdt_balance.token_identifier);
account_state =
account_state.esdt_balance(token_id_expr.as_str(), esdt_balance.balance.as_str());
}

for (token_id, esdt_roles) in account_esdt_roles {
let token_id_expr = format!("str:{token_id}");
account_state = account_state.esdt_roles(token_id_expr.as_str(), esdt_roles);
}

account_state
}

fn convert_storage(account_storage: HashMap<String, String>) -> BTreeMap<BytesKey, BytesValue> {
account_storage
.into_iter()
.filter(|(k, _)| !k.starts_with("454c524f4e44"))
.map(|(k, v)| (BytesKey::from(k.as_str()), BytesValue::from(v)))
.collect()
}
9 changes: 9 additions & 0 deletions framework/snippets-dapp/src/imports.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub use crate::multiversx_sc_scenario::imports::*;

pub use crate::{
dns_address_for_name, test_wallets, Interactor, InteractorPrepareAsync, StepBuffer,
};

pub use multiversx_sdk_wbg::{data::keystore::InsertPassword, wallet::Wallet};

pub use env_logger;
Loading

0 comments on commit f6b687c

Please sign in to comment.