From 6957fe482f85119abf5acc53b43610ade4f1b231 Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Tue, 24 Sep 2024 15:07:06 +0200 Subject: [PATCH 1/8] sdk-wbg crate impl with dapp example, wasm target compatible interactor --- .gitignore | 1 + Cargo.toml | 3 + contracts/examples/adder/interact/Cargo.toml | 1 + .../examples/multisig/interact/Cargo.toml | 1 + .../basic-features/interact/Cargo.toml | 1 + .../composability/interact/Cargo.toml | 1 + framework/snippets/Cargo.toml | 6 +- framework/snippets/src/account_tool.rs | 4 +- framework/snippets/src/imports.rs | 5 +- framework/snippets/src/interactor.rs | 6 +- .../interactor_scenario/interactor_sc_call.rs | 2 +- .../interactor_sc_deploy.rs | 2 +- .../interactor_vm_query.rs | 2 +- framework/snippets/src/interactor_sender.rs | 2 +- framework/snippets/src/lib.rs | 7 +- .../src/multi/interactor_multi_sc_exec.rs | 2 +- .../src/multi/interactor_multi_sc_process.rs | 2 +- .../snippets/src/multi/interactor_step.rs | 2 +- framework/snippets/src/network_response.rs | 2 +- framework/snippets/src/test_wallets.rs | 2 +- .../tests/test_tx_deployed_address.rs | 4 +- .../tests/test_tx_issued_token_identifier.rs | 2 +- .../tests/test_tx_multi_contract_sc_result.rs | 2 +- .../tests/test_tx_multiple_sc_results.rs | 2 +- framework/snippets/tests/test_tx_sc_result.rs | 2 +- sdk-wbg/README.md | 3 + sdk-wbg/core/Cargo.toml | 42 + sdk-wbg/core/README.md | 5 + .../core/examples/ping-pong-dapp/Cargo.toml | 20 + .../core/examples/ping-pong-dapp/index.html | 12 + .../ping-pong-dapp/interactor/Cargo.toml | 13 + .../ping-pong-dapp/interactor/interactor.rs | 57 + .../examples/ping-pong-dapp/interactor/lib.rs | 2 + .../interactor/ping-pong-egld.wasm | Bin 0 -> 6567 bytes .../core/examples/ping-pong-dapp/readme.md | 9 + .../ping-pong-dapp/src/components/button.rs | 18 + .../ping-pong-dapp/src/components/footer.rs | 15 + .../ping-pong-dapp/src/components/mod.rs | 7 + .../src/components/network_status.rs | 17 + .../examples/ping-pong-dapp/src/context.rs | 66 + .../core/examples/ping-pong-dapp/src/main.rs | 31 + .../examples/ping-pong-dapp/src/pages/home.rs | 115 + .../examples/ping-pong-dapp/src/pages/mod.rs | 3 + .../ping-pong-dapp/src/requests/mod.rs | 3 + .../ping-pong-dapp/src/requests/proxy.rs | 264 ++ .../ping-pong-dapp/src/requests/query.rs | 18 + .../src/requests/transaction.rs | 68 + .../examples/ping-pong-dapp/src/routes.rs | 16 + .../examples/ping-pong-dapp/src/styles.css | 29 + .../core/examples/ping-pong-dapp/trunk.toml | 7 + .../edwards25519/completed_group_element.rs | 42 + .../core/src/crypto/edwards25519/constant.rs | 3654 +++++++++++++++++ .../edwards25519/extended_group_element.rs | 103 + .../src/crypto/edwards25519/field_element.rs | 637 +++ sdk-wbg/core/src/crypto/edwards25519/mod.rs | 848 ++++ .../pre_computed_group_element.rs | 50 + .../edwards25519/projective_group_element.rs | 24 + sdk-wbg/core/src/crypto/mod.rs | 3 + sdk-wbg/core/src/crypto/private_key.rs | 154 + sdk-wbg/core/src/crypto/public_key.rs | 73 + sdk-wbg/core/src/data/account.rs | 31 + sdk-wbg/core/src/data/account_storage.rs | 16 + sdk-wbg/core/src/data/address.rs | 107 + sdk-wbg/core/src/data/esdt.rs | 38 + sdk-wbg/core/src/data/hyperblock.rs | 38 + sdk-wbg/core/src/data/keystore.rs | 61 + sdk-wbg/core/src/data/mod.rs | 11 + sdk-wbg/core/src/data/network_config.rs | 47 + sdk-wbg/core/src/data/network_economics.rs | 33 + sdk-wbg/core/src/data/network_status.rs | 35 + sdk-wbg/core/src/data/transaction.rs | 292 ++ sdk-wbg/core/src/data/vm.rs | 113 + sdk-wbg/core/src/gateway.rs | 15 + sdk-wbg/core/src/gateway/gateway_account.rs | 106 + sdk-wbg/core/src/gateway/gateway_block.rs | 63 + sdk-wbg/core/src/gateway/gateway_network.rs | 61 + sdk-wbg/core/src/gateway/gateway_proxy.rs | 15 + sdk-wbg/core/src/gateway/gateway_tx.rs | 191 + .../core/src/gateway/gateway_tx_retrieve.rs | 104 + sdk-wbg/core/src/lib.rs | 5 + sdk-wbg/core/src/utils.rs | 15 + sdk-wbg/core/src/wallet.rs | 368 ++ sdk-wbg/core/tests/alice.pem | 5 + sdk-wbg/core/tests/wallet_test.rs | 116 + tools/interactor-system-func-calls/Cargo.toml | 1 + 85 files changed, 8354 insertions(+), 27 deletions(-) create mode 100644 sdk-wbg/README.md create mode 100644 sdk-wbg/core/Cargo.toml create mode 100644 sdk-wbg/core/README.md create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/Cargo.toml create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/index.html create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/interactor/Cargo.toml create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/interactor/interactor.rs create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/interactor/lib.rs create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/interactor/ping-pong-egld.wasm create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/readme.md create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/src/components/button.rs create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/src/components/footer.rs create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/src/components/mod.rs create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/src/components/network_status.rs create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/src/context.rs create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/src/main.rs create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/src/pages/home.rs create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/src/pages/mod.rs create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/src/requests/mod.rs create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/src/requests/proxy.rs create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/src/requests/query.rs create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/src/requests/transaction.rs create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/src/routes.rs create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/src/styles.css create mode 100644 sdk-wbg/core/examples/ping-pong-dapp/trunk.toml create mode 100644 sdk-wbg/core/src/crypto/edwards25519/completed_group_element.rs create mode 100644 sdk-wbg/core/src/crypto/edwards25519/constant.rs create mode 100644 sdk-wbg/core/src/crypto/edwards25519/extended_group_element.rs create mode 100644 sdk-wbg/core/src/crypto/edwards25519/field_element.rs create mode 100644 sdk-wbg/core/src/crypto/edwards25519/mod.rs create mode 100644 sdk-wbg/core/src/crypto/edwards25519/pre_computed_group_element.rs create mode 100644 sdk-wbg/core/src/crypto/edwards25519/projective_group_element.rs create mode 100644 sdk-wbg/core/src/crypto/mod.rs create mode 100644 sdk-wbg/core/src/crypto/private_key.rs create mode 100644 sdk-wbg/core/src/crypto/public_key.rs create mode 100644 sdk-wbg/core/src/data/account.rs create mode 100644 sdk-wbg/core/src/data/account_storage.rs create mode 100644 sdk-wbg/core/src/data/address.rs create mode 100644 sdk-wbg/core/src/data/esdt.rs create mode 100644 sdk-wbg/core/src/data/hyperblock.rs create mode 100644 sdk-wbg/core/src/data/keystore.rs create mode 100644 sdk-wbg/core/src/data/mod.rs create mode 100644 sdk-wbg/core/src/data/network_config.rs create mode 100644 sdk-wbg/core/src/data/network_economics.rs create mode 100644 sdk-wbg/core/src/data/network_status.rs create mode 100644 sdk-wbg/core/src/data/transaction.rs create mode 100644 sdk-wbg/core/src/data/vm.rs create mode 100644 sdk-wbg/core/src/gateway.rs create mode 100644 sdk-wbg/core/src/gateway/gateway_account.rs create mode 100644 sdk-wbg/core/src/gateway/gateway_block.rs create mode 100644 sdk-wbg/core/src/gateway/gateway_network.rs create mode 100644 sdk-wbg/core/src/gateway/gateway_proxy.rs create mode 100644 sdk-wbg/core/src/gateway/gateway_tx.rs create mode 100644 sdk-wbg/core/src/gateway/gateway_tx_retrieve.rs create mode 100644 sdk-wbg/core/src/lib.rs create mode 100644 sdk-wbg/core/src/utils.rs create mode 100644 sdk-wbg/core/src/wallet.rs create mode 100644 sdk-wbg/core/tests/alice.pem create mode 100644 sdk-wbg/core/tests/wallet_test.rs diff --git a/.gitignore b/.gitignore index 8295450d89..00a4be02e9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ # Generated by Cargo # will have compiled files and executables **/target/** +**/dist # Remove Cargo.lock from the framework, but not from the wasm contracts. # In contracts it helps with tracing builds. diff --git a/Cargo.toml b/Cargo.toml index 651456eebd..cab4a379fd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,9 @@ members = [ "sdk/core", "sdk/scenario-format", + "sdk-wbg/core", + "sdk-wbg/core/examples/ping-pong-dapp", + "tools/mxpy-snippet-generator", "tools/payload-macro-generator", # "tools/plotter", diff --git a/contracts/examples/adder/interact/Cargo.toml b/contracts/examples/adder/interact/Cargo.toml index b9c18b391b..0663180c81 100644 --- a/contracts/examples/adder/interact/Cargo.toml +++ b/contracts/examples/adder/interact/Cargo.toml @@ -13,6 +13,7 @@ path = "src/basic_interact.rs" clap = { version = "4.4.7", features = ["derive"] } serde = { version = "1.0", features = ["derive"] } toml = "0.8.6" +tokio = { version = "1.24" } [dependencies.adder] path = ".." diff --git a/contracts/examples/multisig/interact/Cargo.toml b/contracts/examples/multisig/interact/Cargo.toml index 7d6914bd60..e161ee4a42 100644 --- a/contracts/examples/multisig/interact/Cargo.toml +++ b/contracts/examples/multisig/interact/Cargo.toml @@ -13,6 +13,7 @@ path = "src/multisig_interact.rs" clap = { version = "4.4.7", features = ["derive"] } serde = { version = "1.0", features = ["derive"] } toml = "0.8.6" +tokio = { version = "1.24" } [dependencies.multisig] path = ".." diff --git a/contracts/feature-tests/basic-features/interact/Cargo.toml b/contracts/feature-tests/basic-features/interact/Cargo.toml index 69597ba7e3..735d08b890 100644 --- a/contracts/feature-tests/basic-features/interact/Cargo.toml +++ b/contracts/feature-tests/basic-features/interact/Cargo.toml @@ -12,6 +12,7 @@ path = "src/bf_interact.rs" [dependencies] clap = { version = "4.4.7", features = ["derive"] } serde = { version = "1.0", features = ["derive"] } +tokio = { version = "1.24" } toml = "0.8.6" [dependencies.basic-features] diff --git a/contracts/feature-tests/composability/interact/Cargo.toml b/contracts/feature-tests/composability/interact/Cargo.toml index e06ae3c915..b6c02ee16b 100644 --- a/contracts/feature-tests/composability/interact/Cargo.toml +++ b/contracts/feature-tests/composability/interact/Cargo.toml @@ -12,6 +12,7 @@ path = "src/comp_interact_main.rs" [dependencies] clap = { version = "4.4.7", features = ["derive"] } serde = { version = "1.0", features = ["derive"] } +tokio = { version = "1.24" } toml = "0.8.6" [dependencies.multiversx-sc] diff --git a/framework/snippets/Cargo.toml b/framework/snippets/Cargo.toml index 43ca798a69..6d9ad95fc2 100644 --- a/framework/snippets/Cargo.toml +++ b/framework/snippets/Cargo.toml @@ -14,7 +14,7 @@ keywords = ["multiversx", "blockchain", "contract", "snippets"] categories = ["cryptography::cryptocurrencies"] [dependencies] -tokio = { version = "1.24", features = ["full"] } +gloo-timers = { version = "0.3.0", features = ["futures"] } hex = "0.4" base64 = "0.22" log = "0.4.17" @@ -29,9 +29,9 @@ path = "../scenario" version = "0.23.0" path = "../../sdk/scenario-format" -[dependencies.multiversx-sdk] +[dependencies.multiversx-sdk-wbg] version = "=0.6.0" -path = "../../sdk/core" +path = "../../sdk-wbg/core" [dev-dependencies] serde_json = "1.0" diff --git a/framework/snippets/src/account_tool.rs b/framework/snippets/src/account_tool.rs index b10baf4a80..fad13ab314 100644 --- a/framework/snippets/src/account_tool.rs +++ b/framework/snippets/src/account_tool.rs @@ -3,7 +3,7 @@ use multiversx_sc_scenario::{ imports::Bech32Address, scenario_model::{Account, BytesKey, BytesValue, Scenario, SetStateStep, Step}, }; -use multiversx_sdk::{ +use multiversx_sdk_wbg::{ data::{address::Address, esdt::EsdtBalance}, gateway::GatewayProxy, }; @@ -67,7 +67,7 @@ pub async fn retrieve_account_as_scenario_set_state( } fn set_account( - account: multiversx_sdk::data::account::Account, + account: multiversx_sdk_wbg::data::account::Account, account_storage: HashMap, account_esdt: HashMap, account_esdt_roles: HashMap>, diff --git a/framework/snippets/src/imports.rs b/framework/snippets/src/imports.rs index a22c969887..3bdfaf062a 100644 --- a/framework/snippets/src/imports.rs +++ b/framework/snippets/src/imports.rs @@ -4,7 +4,8 @@ pub use crate::{ dns_address_for_name, test_wallets, Interactor, InteractorPrepareAsync, StepBuffer, }; -pub use multiversx_sdk::{data::keystore::InsertPassword, wallet::Wallet}; +pub use multiversx_sdk_wbg::{data::keystore::InsertPassword, wallet::Wallet}; pub use env_logger; -pub use tokio; +pub use gloo_timers; +// pub use tokio; diff --git a/framework/snippets/src/interactor.rs b/framework/snippets/src/interactor.rs index bb38873afe..d4b685f157 100644 --- a/framework/snippets/src/interactor.rs +++ b/framework/snippets/src/interactor.rs @@ -4,7 +4,7 @@ use multiversx_sc_scenario::{ multiversx_sc::types::Address, scenario_model::AddressValue, }; -use multiversx_sdk::{ +use multiversx_sdk_wbg::{ data::{address::Address as ErdrsAddress, network_config::NetworkConfig}, gateway::GatewayProxy, wallet::Wallet, @@ -61,7 +61,9 @@ impl Interactor { pub async fn sleep(&mut self, duration: Duration) { self.waiting_time_ms += duration.as_millis() as u64; - tokio::time::sleep(duration).await; + gloo_timers::future::sleep(duration).await; + + // tokio::time::sleep(duration).await; } pub async fn with_tracer>(mut self, path: P) -> Self { diff --git a/framework/snippets/src/interactor_scenario/interactor_sc_call.rs b/framework/snippets/src/interactor_scenario/interactor_sc_call.rs index 6802304ae9..de75ff16b1 100644 --- a/framework/snippets/src/interactor_scenario/interactor_sc_call.rs +++ b/framework/snippets/src/interactor_scenario/interactor_sc_call.rs @@ -5,7 +5,7 @@ use multiversx_sc_scenario::{ scenario::ScenarioRunner, scenario_model::{ScCallStep, SetStateStep, TxCall}, }; -use multiversx_sdk::{data::transaction::Transaction, utils::base64_encode}; +use multiversx_sdk_wbg::{data::transaction::Transaction, utils::base64_encode}; impl Interactor { pub async fn sc_call(&mut self, mut sc_call_step: S) diff --git a/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs b/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs index 59de249e83..83bcd10f94 100644 --- a/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs +++ b/framework/snippets/src/interactor_scenario/interactor_sc_deploy.rs @@ -5,7 +5,7 @@ use multiversx_sc_scenario::{ mandos_system::ScenarioRunner, scenario_model::{ScDeployStep, SetStateStep}, }; -use multiversx_sdk::{ +use multiversx_sdk_wbg::{ data::{address::Address as ErdrsAddress, transaction::Transaction}, utils::base64_encode, }; diff --git a/framework/snippets/src/interactor_scenario/interactor_vm_query.rs b/framework/snippets/src/interactor_scenario/interactor_vm_query.rs index aa9f87efb7..24839d5c0a 100644 --- a/framework/snippets/src/interactor_scenario/interactor_vm_query.rs +++ b/framework/snippets/src/interactor_scenario/interactor_vm_query.rs @@ -8,7 +8,7 @@ use multiversx_sc_scenario::{ multiversx_sc::{abi::TypeAbiFrom, codec::TopDecodeMulti, types::ContractCall}, scenario_model::{ScQueryStep, TxResponse}, }; -use multiversx_sdk::{data::vm::VmValueRequest, utils::base64_decode}; +use multiversx_sdk_wbg::{data::vm::VmValueRequest, utils::base64_decode}; impl Interactor { pub async fn sc_query(&mut self, mut step: S) -> &mut Self diff --git a/framework/snippets/src/interactor_sender.rs b/framework/snippets/src/interactor_sender.rs index 86d6abcada..8bff6b42dc 100644 --- a/framework/snippets/src/interactor_sender.rs +++ b/framework/snippets/src/interactor_sender.rs @@ -1,6 +1,6 @@ use log::debug; use multiversx_sc_scenario::multiversx_sc::types::Address; -use multiversx_sdk::{data::transaction::Transaction, wallet::Wallet}; +use multiversx_sdk_wbg::{data::transaction::Transaction, wallet::Wallet}; use crate::{address_h256_to_erdrs, Interactor}; diff --git a/framework/snippets/src/lib.rs b/framework/snippets/src/lib.rs index 7d3b52c696..3ffbabbc33 100644 --- a/framework/snippets/src/lib.rs +++ b/framework/snippets/src/lib.rs @@ -17,9 +17,10 @@ pub use interactor_tx::*; pub use log; pub use multi::*; pub use multiversx_sc_scenario::{self, multiversx_sc}; -pub use multiversx_sdk as erdrs; // TODO: remove -pub use multiversx_sdk as sdk; -pub use tokio; +pub use multiversx_sdk_wbg as erdrs; // TODO: remove +pub use multiversx_sdk_wbg as sdk; +// pub use tokio; +pub use gloo_timers; /// Imports normally needed in interactors, grouped together. pub mod imports; diff --git a/framework/snippets/src/multi/interactor_multi_sc_exec.rs b/framework/snippets/src/multi/interactor_multi_sc_exec.rs index 8ac6a4eba9..0b8eee4891 100644 --- a/framework/snippets/src/multi/interactor_multi_sc_exec.rs +++ b/framework/snippets/src/multi/interactor_multi_sc_exec.rs @@ -1,6 +1,6 @@ use super::interactor_multi_sc_process::{update_nonces_and_sign_tx, SenderSet, Txs}; use crate::{network_response, Interactor, InteractorStep, StepBuffer}; -use multiversx_sdk::data::transaction::Transaction; +use multiversx_sdk_wbg::data::transaction::Transaction; impl Interactor { pub async fn multi_sc_exec(&mut self, mut buffer: StepBuffer<'_>) { diff --git a/framework/snippets/src/multi/interactor_multi_sc_process.rs b/framework/snippets/src/multi/interactor_multi_sc_process.rs index 3364a4d5ee..f03785f510 100644 --- a/framework/snippets/src/multi/interactor_multi_sc_process.rs +++ b/framework/snippets/src/multi/interactor_multi_sc_process.rs @@ -1,6 +1,6 @@ use crate::{multiversx_sc::types::Address, Interactor, Sender}; use futures::future::join_all; -use multiversx_sdk::data::transaction::{Transaction, TransactionOnNetwork}; +use multiversx_sdk_wbg::data::transaction::{Transaction, TransactionOnNetwork}; use std::collections::HashSet; pub(crate) type Txs = Vec; diff --git a/framework/snippets/src/multi/interactor_step.rs b/framework/snippets/src/multi/interactor_step.rs index be172bbb82..8b8d0fbc71 100644 --- a/framework/snippets/src/multi/interactor_step.rs +++ b/framework/snippets/src/multi/interactor_step.rs @@ -2,7 +2,7 @@ use multiversx_sc_scenario::{ mandos_system::ScenarioRunner, scenario_model::{AddressValue, ScCallStep, ScDeployStep, TxResponse}, }; -use multiversx_sdk::data::transaction::Transaction; +use multiversx_sdk_wbg::data::transaction::Transaction; use crate::Interactor; diff --git a/framework/snippets/src/network_response.rs b/framework/snippets/src/network_response.rs index 0140dd839e..e77525fa16 100644 --- a/framework/snippets/src/network_response.rs +++ b/framework/snippets/src/network_response.rs @@ -3,7 +3,7 @@ use multiversx_sc_scenario::{ multiversx_chain_vm::crypto_functions::keccak256, scenario_model::{Log, TxResponse, TxResponseStatus}, }; -use multiversx_sdk::{ +use multiversx_sdk_wbg::{ data::transaction::{ApiSmartContractResult, Events, TransactionOnNetwork}, utils::base64_decode, }; diff --git a/framework/snippets/src/test_wallets.rs b/framework/snippets/src/test_wallets.rs index 341f93ef29..2b79cc948d 100644 --- a/framework/snippets/src/test_wallets.rs +++ b/framework/snippets/src/test_wallets.rs @@ -1,4 +1,4 @@ -use multiversx_sdk::wallet::Wallet; +use multiversx_sdk_wbg::wallet::Wallet; fn test_wallet(pem_file_contents: &str) -> Wallet { Wallet::from_pem_file_contents(pem_file_contents.to_string()).unwrap() diff --git a/framework/snippets/tests/test_tx_deployed_address.rs b/framework/snippets/tests/test_tx_deployed_address.rs index 377a0e779f..b518219de3 100644 --- a/framework/snippets/tests/test_tx_deployed_address.rs +++ b/framework/snippets/tests/test_tx_deployed_address.rs @@ -1,6 +1,6 @@ use multiversx_sc_scenario::imports::Address; use multiversx_sc_snippets::network_response; -use multiversx_sdk::data::transaction::{TransactionInfo, TransactionOnNetwork}; +use multiversx_sdk_wbg::data::transaction::{TransactionInfo, TransactionOnNetwork}; #[test] fn test_deployed_address() { @@ -55,7 +55,7 @@ fn test_deployed_address() { .transaction; let tx_response = network_response::parse_tx_response(tx_on_network); let opt_address = tx_response.new_deployed_address.map(|e| { - multiversx_sdk::data::address::Address::from_bytes(*e.as_array()) + multiversx_sdk_wbg::data::address::Address::from_bytes(*e.as_array()) .to_bech32_string() .unwrap() }); diff --git a/framework/snippets/tests/test_tx_issued_token_identifier.rs b/framework/snippets/tests/test_tx_issued_token_identifier.rs index 3539c4e757..f90bb7d207 100644 --- a/framework/snippets/tests/test_tx_issued_token_identifier.rs +++ b/framework/snippets/tests/test_tx_issued_token_identifier.rs @@ -1,5 +1,5 @@ use multiversx_sc_snippets::network_response; -use multiversx_sdk::data::transaction::{TransactionInfo, TransactionOnNetwork}; +use multiversx_sdk_wbg::data::transaction::{TransactionInfo, TransactionOnNetwork}; #[test] fn test_process_issued_token_identifier_fungible() { diff --git a/framework/snippets/tests/test_tx_multi_contract_sc_result.rs b/framework/snippets/tests/test_tx_multi_contract_sc_result.rs index a9b64d8941..7cc145552a 100644 --- a/framework/snippets/tests/test_tx_multi_contract_sc_result.rs +++ b/framework/snippets/tests/test_tx_multi_contract_sc_result.rs @@ -1,5 +1,5 @@ use multiversx_sc_snippets::network_response; -use multiversx_sdk::data::transaction::{TransactionInfo, TransactionOnNetwork}; +use multiversx_sdk_wbg::data::transaction::{TransactionInfo, TransactionOnNetwork}; #[test] fn test_with_multi_contract_same_shard_tx_that_has_no_sc_result() { diff --git a/framework/snippets/tests/test_tx_multiple_sc_results.rs b/framework/snippets/tests/test_tx_multiple_sc_results.rs index 4fee781b7f..9415ce9576 100644 --- a/framework/snippets/tests/test_tx_multiple_sc_results.rs +++ b/framework/snippets/tests/test_tx_multiple_sc_results.rs @@ -1,5 +1,5 @@ use multiversx_sc_snippets::network_response::{self, is_out_scr}; -use multiversx_sdk::data::transaction::{TransactionInfo, TransactionOnNetwork}; +use multiversx_sdk_wbg::data::transaction::{TransactionInfo, TransactionOnNetwork}; #[test] fn test_transaction_multiple_sc_results() { diff --git a/framework/snippets/tests/test_tx_sc_result.rs b/framework/snippets/tests/test_tx_sc_result.rs index b3809eb2f8..039ff98d73 100644 --- a/framework/snippets/tests/test_tx_sc_result.rs +++ b/framework/snippets/tests/test_tx_sc_result.rs @@ -1,5 +1,5 @@ use multiversx_sc_snippets::network_response; -use multiversx_sdk::data::transaction::{TransactionInfo, TransactionOnNetwork}; +use multiversx_sdk_wbg::data::transaction::{TransactionInfo, TransactionOnNetwork}; #[test] fn test_with_tx_that_has_sc_result() { diff --git a/sdk-wbg/README.md b/sdk-wbg/README.md new file mode 100644 index 0000000000..2dab196709 --- /dev/null +++ b/sdk-wbg/README.md @@ -0,0 +1,3 @@ +# MultiversX tools and SDKs + +This directory contains various tools and SDKs written in Rust for the MultiversX ecosystem specifically for the WASM targets, compatible with `wasm-bindgen`. diff --git a/sdk-wbg/core/Cargo.toml b/sdk-wbg/core/Cargo.toml new file mode 100644 index 0000000000..2eb5fac0b5 --- /dev/null +++ b/sdk-wbg/core/Cargo.toml @@ -0,0 +1,42 @@ +[package] +name = "multiversx-sdk-wbg" +version = "0.6.0" +edition = "2021" + +authors = ["MultiversX "] +license = "MIT" +readme = "README.md" +repository = "https://github.com/multiversx/mx-sdk-rs" +homepage = "https://multiversx.com/" +documentation = "https://docs.multiversx.com/" +description = "SDK for interacting with the MultiversX blockchain for WASM environments" +categories = ["cryptography::cryptocurrencies", "api-bindings"] +keywords = ["multiversx", "blockchain", "sdk", "api", "wasm", "wasm-bindgen"] + +[dependencies] +rand = { version = "0.8.5" } +getrandom = { version = "0.2.15", features = ["js"] } +wasm-bindgen = { version = "0.2.93" } +wasm-bindgen-futures = { version = "0.4.43" } +gloo-net = { version = "0.6.0" } +gloo-timers = { version = "0.3.0", features = ["futures"] } +serde = { version = "1.0.130", features = ["derive"] } +serde_json = { version = "1.0.68", features = ["preserve_order"] } +serde_repr = "0.1.8" +anyhow = "1.0.44" +bip39 = { version = "2.0.0", features = ["rand"] } +sha2 = "0.10.8" +sha3 = "0.10.8" +hmac = { version = "0.12.1", features = ["std"] } +hex = "0.4.3" +base64 = "0.22" +pbkdf2 = { version = "0.12.2", default-features = false } +zeroize = "1.4.2" +bech32 = "0.11" +itertools = "0.13.0" +pem = "3.0.2" +log = "0.4.17" +scrypt = "0.11" +aes = "0.8" +ctr = "0.9.2" +uuid = { version = "1.10.0", features = ["v4"] } diff --git a/sdk-wbg/core/README.md b/sdk-wbg/core/README.md new file mode 100644 index 0000000000..adaf311e41 --- /dev/null +++ b/sdk-wbg/core/README.md @@ -0,0 +1,5 @@ +# MultiversX SDK for Rust + +General purpose collection of tools & SDKs to interact with the MultiversX blockchain from Rust projects, wasm-bindgen compatible. + +More examples in `./examples`. diff --git a/sdk-wbg/core/examples/ping-pong-dapp/Cargo.toml b/sdk-wbg/core/examples/ping-pong-dapp/Cargo.toml new file mode 100644 index 0000000000..940efd99be --- /dev/null +++ b/sdk-wbg/core/examples/ping-pong-dapp/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "ping-pong-dapp" +version = "0.1.0" +edition = "2021" + +[dependencies] +console_log = "0.2" +log = "0.4" +serde = "1.0.210" +serde_json = "1.0" +wasm-bindgen-futures = "0.4" +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" diff --git a/sdk-wbg/core/examples/ping-pong-dapp/index.html b/sdk-wbg/core/examples/ping-pong-dapp/index.html new file mode 100644 index 0000000000..162a2a7a50 --- /dev/null +++ b/sdk-wbg/core/examples/ping-pong-dapp/index.html @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/sdk-wbg/core/examples/ping-pong-dapp/interactor/Cargo.toml b/sdk-wbg/core/examples/ping-pong-dapp/interactor/Cargo.toml new file mode 100644 index 0000000000..57820be136 --- /dev/null +++ b/sdk-wbg/core/examples/ping-pong-dapp/interactor/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "interactor" +version = "0.1.0" +edition = "2021" + +[lib] +path = "lib.rs" + +[dependencies] +serde = { version = "1.0", features = ["derive"] } + +[dependencies.multiversx-sc-snippets] +path = "../../../../../framework/snippets" diff --git a/sdk-wbg/core/examples/ping-pong-dapp/interactor/interactor.rs b/sdk-wbg/core/examples/ping-pong-dapp/interactor/interactor.rs new file mode 100644 index 0000000000..4db907e042 --- /dev/null +++ b/sdk-wbg/core/examples/ping-pong-dapp/interactor/interactor.rs @@ -0,0 +1,57 @@ +use imports::{Address, Bech32Address, BytesValue}; +use multiversx_sc_snippets::*; +use serde::{Deserialize, Serialize}; + +const GATEWAY: &str = sdk::gateway::DEVNET_GATEWAY; +const CONTRACT_ADDRESS: &str = "erd1qqqqqqqqqqqqqpgq6tqvj5f59xrgxwrtwy30elgpu7l4zrv6d8ssnjdwxq"; +const PING_PONG_CODE: &[u8] = include_bytes!("ping-pong-egld.wasm"); + +#[derive(Debug, Default, Serialize, Deserialize)] +pub struct Config { + gateway: String, + contract_address: String, +} + +impl Config { + // Deserializes state from file + pub fn new() -> Self { + Config { + gateway: GATEWAY.to_string(), + contract_address: CONTRACT_ADDRESS.to_string(), + } + } + + /// Sets the contract address + pub fn set_address(&mut self, address: Bech32Address) { + self.contract_address = address.to_string() + } + + /// Returns the contract address + pub fn current_address(&self) -> &String { + &self.contract_address + } +} + +pub struct ContractInteract { + pub interactor: Interactor, + pub wallet_address: Address, + pub contract_code: BytesValue, + pub config: Config, +} + +impl ContractInteract { + pub async fn new() -> Self { + let config = Config::new(); + let mut interactor = Interactor::new(&config.gateway).await; + let wallet_address = interactor.register_wallet(test_wallets::mike()); + + let contract_code = BytesValue::from(PING_PONG_CODE); + + ContractInteract { + interactor, + wallet_address, + contract_code, + config, + } + } +} diff --git a/sdk-wbg/core/examples/ping-pong-dapp/interactor/lib.rs b/sdk-wbg/core/examples/ping-pong-dapp/interactor/lib.rs new file mode 100644 index 0000000000..cf8d1d5292 --- /dev/null +++ b/sdk-wbg/core/examples/ping-pong-dapp/interactor/lib.rs @@ -0,0 +1,2 @@ +pub mod interactor; +pub use interactor::ContractInteract; diff --git a/sdk-wbg/core/examples/ping-pong-dapp/interactor/ping-pong-egld.wasm b/sdk-wbg/core/examples/ping-pong-dapp/interactor/ping-pong-egld.wasm new file mode 100644 index 0000000000000000000000000000000000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BRttbVWxEd?q7UlJ0%)P+&P)JFM#~@jUMNlDoV4xP9x|!bKgJ6vEox zSkpz_L%J5NLOi-hjEb$=a;<))rim=9G;kHTrmvLZrD7wzQj3?Ow(`2#YR1quE-Y#4 zUMO9Sin`gV;&QwZ>q2tPnAi?tM0zcXi^9X6*O1ETB)Gxg+i;*h>DD^7k%##D);{YkkMP2 literal 0 HcmV?d00001 diff --git a/sdk-wbg/core/examples/ping-pong-dapp/readme.md b/sdk-wbg/core/examples/ping-pong-dapp/readme.md new file mode 100644 index 0000000000..032b2c3d29 --- /dev/null +++ b/sdk-wbg/core/examples/ping-pong-dapp/readme.md @@ -0,0 +1,9 @@ +# Ping pong dapp example + +## Prerequisites +- trunk + +```bash +cargo install trunk +``` +Run this example locally with `trunk serve --open`. \ No newline at end of file diff --git a/sdk-wbg/core/examples/ping-pong-dapp/src/components/button.rs b/sdk-wbg/core/examples/ping-pong-dapp/src/components/button.rs new file mode 100644 index 0000000000..cdb898bd59 --- /dev/null +++ b/sdk-wbg/core/examples/ping-pong-dapp/src/components/button.rs @@ -0,0 +1,18 @@ +use yew::prelude::*; + +#[derive(Properties, PartialEq)] +pub struct ButtonProps { + pub name: String, + pub class_name: String, + pub button_type: String, + pub on_click: Callback, +} + +#[function_component(Button)] +pub fn button(props: &ButtonProps) -> Html { + html! { + + } +} diff --git a/sdk-wbg/core/examples/ping-pong-dapp/src/components/footer.rs b/sdk-wbg/core/examples/ping-pong-dapp/src/components/footer.rs new file mode 100644 index 0000000000..e5d7066c55 --- /dev/null +++ b/sdk-wbg/core/examples/ping-pong-dapp/src/components/footer.rs @@ -0,0 +1,15 @@ +use yew::prelude::*; +use yew_icons::{Icon, IconId}; + +#[function_component(Footer)] +pub fn footer() -> Html { + html! { +
+

+ { "Made with " } + + { " by the MultiversX team" } +

+
+ } +} diff --git a/sdk-wbg/core/examples/ping-pong-dapp/src/components/mod.rs b/sdk-wbg/core/examples/ping-pong-dapp/src/components/mod.rs new file mode 100644 index 0000000000..f55f57e4d0 --- /dev/null +++ b/sdk-wbg/core/examples/ping-pong-dapp/src/components/mod.rs @@ -0,0 +1,7 @@ +pub mod button; +pub mod footer; +pub mod network_status; + +pub use button::Button; +pub use footer::Footer; +pub use network_status::NetworkStatusComponent; diff --git a/sdk-wbg/core/examples/ping-pong-dapp/src/components/network_status.rs b/sdk-wbg/core/examples/ping-pong-dapp/src/components/network_status.rs new file mode 100644 index 0000000000..74f085f23c --- /dev/null +++ b/sdk-wbg/core/examples/ping-pong-dapp/src/components/network_status.rs @@ -0,0 +1,17 @@ +use yew::prelude::*; + +use crate::context::ConfigContext; + +#[function_component(NetworkStatusComponent)] +pub fn network_status() -> Html { + let context = use_context::().unwrap(); + let response = format!("{:?}", &context.network_status); + + html! { +
+

+ {&response} +

+
+ } +} diff --git a/sdk-wbg/core/examples/ping-pong-dapp/src/context.rs b/sdk-wbg/core/examples/ping-pong-dapp/src/context.rs new file mode 100644 index 0000000000..78f71590d8 --- /dev/null +++ b/sdk-wbg/core/examples/ping-pong-dapp/src/context.rs @@ -0,0 +1,66 @@ +use crate::requests::query; + +use html::ChildrenProps; +use multiversx_sc_snippets::sdk::data::network_status::NetworkStatus; +use yew::prelude::*; + +#[derive(Clone, Debug, PartialEq)] +pub struct ConfigContext { + pub network_status: NetworkStatus, + pub set_network_status: Callback, +} + +pub async fn refresh_context() -> NetworkStatus { + log::info!("refreshing context"); + query::get_network_status().await.unwrap_or_default() +} + +impl Default for ConfigContext { + fn default() -> Self { + ConfigContext { + network_status: NetworkStatus::default(), + set_network_status: Callback::noop(), + } + } +} + +#[function_component(ConfigProvider)] +pub fn config_provider(props: &ChildrenProps) -> Html { + let network_status = use_state(NetworkStatus::default); + + // Clone the state here for use in the callback + let set_network_status = { + let network_status = network_status.clone(); + Callback::from(move |new_status: NetworkStatus| { + network_status.set(new_status); + }) + }; + + // Clone the callback for async usage in the effect + let set_network_status_async = set_network_status.clone(); + + // refresh context on component mount + use_effect_with_deps( + move |_| { + wasm_bindgen_futures::spawn_local(async move { + let new_status = refresh_context().await; + + // Emit the new status inside the async block + set_network_status_async.emit(new_status); + }); + || () // no cleanup fn + }, + (), // empty dependency array, run once on mount + ); + + let context = ConfigContext { + network_status: (*network_status).clone(), + set_network_status, + }; + + html! { + context={context}> + { for props.children.iter() } + > + } +} diff --git a/sdk-wbg/core/examples/ping-pong-dapp/src/main.rs b/sdk-wbg/core/examples/ping-pong-dapp/src/main.rs new file mode 100644 index 0000000000..552e2593be --- /dev/null +++ b/sdk-wbg/core/examples/ping-pong-dapp/src/main.rs @@ -0,0 +1,31 @@ +use components::Footer; +use context::ConfigProvider; +use log::Level; +use routes::{switch, Route}; +use yew::prelude::*; +use yew_router::{BrowserRouter, Switch}; +mod components; +mod context; +mod pages; +mod requests; +mod routes; + +#[function_component(App)] +fn app() -> Html { + html! { + + + render={switch} /> +