diff --git a/README.md b/README.md index 026a912..51b710c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ -# Iroha 2 examples -This repository contains a number of examples for different SDKs. -It'll contain the demos that should work with the current `dev` branch and are automatically tested with CI. +## Experimenting with transfer +This specific repository attempts to use Iroha 2 Rust SDK to create transfers, preferably signed transfers. diff --git a/Rust/configs/demo/config.json b/Rust/configs/demo/config.json new file mode 100644 index 0000000..70c9f26 --- /dev/null +++ b/Rust/configs/demo/config.json @@ -0,0 +1,21 @@ +{ + "PUBLIC_KEY": "ed01207233BFC89DCBD68C19FDE6CE6158225298EC1131B6A130D1AEB454C1AB5183C0", + "PRIVATE_KEY": { + "digest_function": "ed25519", + "payload": "9ac47abf59b356e0bd7dcbbbb4dec080e302156a48ca907e47cb6aea1d32719e7233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0" + }, + "ACCOUNT_ID": "admin@sos", + "BASIC_AUTH": { + "web_login": "mad_hatter", + "password": "ilovetea" + }, + "TORII_API_URL": "http://127.0.0.1:8080/", + "TORII_TELEMETRY_URL": "http://127.0.0.1:8180/", + "TRANSACTION_TIME_TO_LIVE_MS": 100000, + "TRANSACTION_STATUS_TIMEOUT_MS": 15000, + "TRANSACTION_LIMITS": { + "max_instruction_number": 4096, + "max_wasm_size_bytes": 4194304 + }, + "ADD_TRANSACTION_NONCE": false +} \ No newline at end of file diff --git a/Rust/configs/demo/genesis.json b/Rust/configs/demo/genesis.json new file mode 100644 index 0000000..829af35 --- /dev/null +++ b/Rust/configs/demo/genesis.json @@ -0,0 +1,116 @@ +{ + "transactions": [ + [ + { + "Register": { + "NewDomain": { + "id": "sos", + "logo": null, + "metadata": { + "key": { + "String": "value" + } + } + } + } + }, + { + "Register": { + "NewAccount": { + "id": "admin@sos", + "signatories": [ + "ed01207233BFC89DCBD68C19FDE6CE6158225298EC1131B6A130D1AEB454C1AB5183C0" + ], + "metadata": { + "key": { + "String": "value" + } + } + } + } + }, + { + "Register": { + "NewAssetDefinition": { + "id": "e_coin#sos", + "value_type": "Quantity", + "mintable": "Infinitely", + "logo": null, + "metadata": {} + } + } + }, + { + "Grant": { + "PermissionToken": { + "definition_id": "can_set_parameters", + "params": {} + }, + "destination_id": { + "AccountId": "admin@sos" + } + } + }, + { + "Sequence": [ + { + "NewParameter": { + "Parameter": "?MaxTransactionsInBlock=512" + } + }, + { + "NewParameter": { + "Parameter": "?BlockTime=2000" + } + }, + { + "NewParameter": { + "Parameter": "?CommitTimeLimit=4000" + } + }, + { + "NewParameter": { + "Parameter": "?TransactionLimits=4096,4194304_TL" + } + }, + { + "NewParameter": { + "Parameter": "?WSVAssetMetadataLimits=1048576,4096_ML" + } + }, + { + "NewParameter": { + "Parameter": "?WSVAssetDefinitionMetadataLimits=1048576,4096_ML" + } + }, + { + "NewParameter": { + "Parameter": "?WSVAccountMetadataLimits=1048576,4096_ML" + } + }, + { + "NewParameter": { + "Parameter": "?WSVDomainMetadataLimits=1048576,4096_ML" + } + }, + { + "NewParameter": { + "Parameter": "?WSVIdentLengthLimits=1,128_LL" + } + }, + { + "NewParameter": { + "Parameter": "?WASMFuelLimit=23000000" + } + }, + { + "NewParameter": { + "Parameter": "?WASMMaxMemory=524288000" + } + } + ] + } + ] + ], + "validator": "./validator.wasm" +} \ No newline at end of file diff --git a/Rust/configs/demo/validator.wasm b/Rust/configs/demo/validator.wasm new file mode 100644 index 0000000..cdb40f9 Binary files /dev/null and b/Rust/configs/demo/validator.wasm differ diff --git a/Rust/configs/prometheus.yml b/Rust/configs/prometheus.yml new file mode 100644 index 0000000..811a010 --- /dev/null +++ b/Rust/configs/prometheus.yml @@ -0,0 +1,12 @@ +global: + scrape_interval: 15s + evaluation_interval: 15s + +rule_files: + # - "first.rules" + # - "second.rules" + +scrape_configs: + - job_name: prometheus + static_configs: + - targets: ['localhost:8180'] diff --git a/Rust/docker-compose.yml b/Rust/docker-compose.yml new file mode 100644 index 0000000..a0470b7 --- /dev/null +++ b/Rust/docker-compose.yml @@ -0,0 +1,21 @@ +version: '3.8' +services: + iroha0: + image: ffakenz/iroha-node:dev + environment: + IROHA_PUBLIC_KEY: ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB + IROHA_PRIVATE_KEY: '{"digest_function":"ed25519","payload":"8f4c15e5d664da3f13778801d23d4e89b76e94c1b94b389544168b6cb894f84f8ba62848cf767d72e7f7f4b9d2d7ba07fee33760f79abe5597a51520e292a0cb"}' + TORII_P2P_ADDR: iroha0:1337 + TORII_API_URL: iroha0:8080 + TORII_TELEMETRY_URL: iroha0:8180 + IROHA_GENESIS_ACCOUNT_PUBLIC_KEY: ed01204164BF554923ECE1FD412D241036D863A6AE430476C898248B8237D77534CFC4 + IROHA_GENESIS_ACCOUNT_PRIVATE_KEY: '{"digest_function":"ed25519","payload":"82b3bde54aebeca4146257da0de8d59d8e46d5fe34887dcd8072866792fcb3ad4164bf554923ece1fd412d241036d863a6ae430476c898248b8237d77534cfc4"}' + SUMERAGI_TRUSTED_PEERS: '[{"address":"iroha0:1337","public_key":"ed01208BA62848CF767D72E7F7F4B9D2D7BA07FEE33760F79ABE5597A51520E292A0CB"}]' + ports: + - 1337:1337 + - 8080:8080 + - 8180:8180 + volumes: + - ./configs/demo:/config + init: true + command: iroha --submit-genesis diff --git a/Rust/examples/experimenting-with-transfer.rs b/Rust/examples/experimenting-with-transfer.rs new file mode 100644 index 0000000..b6c7f10 --- /dev/null +++ b/Rust/examples/experimenting-with-transfer.rs @@ -0,0 +1,73 @@ +use eyre::Result; +use iroha_2_examples::load_client; +use iroha_client::client::Client; +use iroha_data_model::prelude::*; +use std::str::FromStr; + +fn get_key_from_user() -> Result { + let (public_key, _) = iroha_crypto::KeyPair::generate()?.into(); + Ok(public_key) +} + +fn register_account(client: &Client, account_name: &str, domain_name: &str) -> Result { + let account_id: AccountId = format!("{}@{}", account_name, domain_name).parse()?; + let public_key = get_key_from_user()?; + let register = RegisterBox::new(Account::new(account_id.clone(), [public_key])); + client.submit_blocking(register)?; + Ok(account_id) +} + +fn mint_to_account(client: &Client, asset_definition_id: &AssetDefinitionId, account_id: &AccountId, value: f64) -> Result<()> { + let asset_id = AssetId::new(asset_definition_id.clone(), account_id.clone()); + let mint: InstructionBox = + MintBox::new(value.try_to_value()?, IdBox::AssetId(asset_id.clone())).into(); + client.submit_blocking(mint)?; + Ok(()) +} + +fn print_asset(client: &Client, asset_id: &AssetId) -> Result<()> { + let asset: Asset = client.request(iroha_data_model::query::asset::model::FindAssetById { + id: asset_id.clone().into(), + })?; + println!("Asset data: {:?}", asset); + Ok(()) +} + +fn main() -> Result<()> { + let client: Client = load_client("configs/demo/config.json")?; + + // Create and register a Domain + let domain_name = "unreasonable"; + let domain_id: DomainId = domain_name.parse()?; + let register: InstructionBox = RegisterBox::new(Domain::new(domain_id)).into(); + client.submit_blocking(register)?; + + // Register accounts + let user_account_id = register_account(&client, "user", domain_name)?; + let user2_account_id = register_account(&client, "user2", domain_name)?; + + // Register the time asset + let time_definition_id = AssetDefinitionId::from_str("time#unreasonable")?; + let register_time: InstructionBox = RegisterBox::new(AssetDefinition::fixed(time_definition_id.clone())).into(); + client.submit_blocking(register_time)?; + + // Mint to accounts and print asset + mint_to_account(&client, &time_definition_id, &user_account_id, 12.34)?; + print_asset(&client, &AssetId::new(time_definition_id.clone(), user_account_id.clone()))?; + + mint_to_account(&client, &time_definition_id, &user2_account_id, 12.34)?; + print_asset(&client, &AssetId::new(time_definition_id.clone(), user2_account_id.clone()))?; + + // Transfer asset between accounts + let asset_id_from = AssetId::new(time_definition_id.clone(), user_account_id); + let asset_id_to = AssetId::new(time_definition_id, user2_account_id); + let transfer = TransferBox::new( + IdBox::AssetId(asset_id_from), + Value::Numeric(1_f64.try_into()?), + IdBox::AssetId(asset_id_to) + ); + println!("Preparing to transfer 1 time from user to user2..."); + client.submit_blocking(transfer)?; + + Ok(()) +}