Skip to content

Commit

Permalink
[add]: transfer-group-assets
Browse files Browse the repository at this point in the history
[edit]: update obsolete examples

Signed-off-by: Nurzhan Sakén <[email protected]>
  • Loading branch information
nxsaken committed Apr 18, 2024
1 parent bc1a100 commit 8de8655
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 76 deletions.
16 changes: 8 additions & 8 deletions src/cookbook/create-transactions.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "Create transactions | Cookbook"
title: "Create Transactions | Cookbook"
head:
- - meta
- name: description
Expand All @@ -12,7 +12,7 @@ head:
# How to Create a Transaction

```rust
fn create_transactions(iroha: &Client) {
fn create_transaction(iroha: &Client) -> SignedTransaction {
// Prepare the instructions you want to execute
let alice = AccountId::from_str("alice@wonderland").unwrap();
let register_alice = {
Expand Down Expand Up @@ -40,12 +40,12 @@ fn create_transactions(iroha: &Client) {

// Build a transaction with the prepared instructions and empty metadata
// on behalf of the current account configured with the client
let signed_tx = iroha.build_transaction(
iroha.build_transaction(
instructions,
UnlimitedMetadata::default()
);

// Submit the transaction
iroha.submit_transaction(&signed_tx).unwrap();
)
}
```
```

See [Submit Transactions](submit-transactions.md) to learn how to submit
the resulting `SignedTransaction`.
8 changes: 2 additions & 6 deletions src/cookbook/register-asset-definitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,8 @@ fn define_numeric_asset(iroha: &Client) {
)
);
let define_gold = Register::asset_definition(
// equivalent to `AssetDefinition::numeric(gold)`
AssetDefinition::new(
"gold#wonderland".parse().unwrap(),
// allow arbitrary numeric values
AssetValueType::Numeric(NumericSpec::unconstrained()),
)
// allow arbitrary numeric values
AssetDefinition::numeric("gold#wonderland".parse().unwrap())
);
iroha.submit_all([
define_roses.into(),
Expand Down
15 changes: 7 additions & 8 deletions src/cookbook/register-domains.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ head:
# How to Register a Domain

```rust
//First we need to define a domain id (name)
let domain_id = DomainId::new("disneyland".parse().unwrap());
//Then we need to define a new domain object
let domain: NewDomain = Domain::new(domain_id);
//After, define a Register expression for the new domain
let expression = RegisterExpr::new(domain);
//And finally we need to send the transaction
iroha_client.submit_blocking(expression).unwrap();
fn register_domain(iroha: &Client) {
let wonderland = DomainId::from_str("wonderland").unwrap();
let register_wonderland = Register::domain(
Domain::new(wonderland)
);
iroha.submit(register_wonderland).unwrap();
}
```
39 changes: 17 additions & 22 deletions src/cookbook/submit-transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,23 @@ head:

# How to Submit a Transaction

```rust
//Precondition: the Client variable must be initialized

//First we need to build an instruction expression that we want to submit
//The instruction expression structures have a self-explanatory name
//and you can easily understand what each of them does.
//As an example:
//* BurnExp - burns asset quantity
//* MintExpr - mints asset quantity
//* RegisterExpr - registers any Registrable object like
//** Account, Asset, Asset definition, Trigger etc.
//As an example we will take the MintExpr

let asset_definition_id = AssetDefinitionId::from_str("coolAsset#wonderland").unwrap();
let asset_id: AssetId = AssetId::new(asset_definition_id, AccountId::from_str("alice@wonderland").unwrap());
let asset_quantity: Fixed = Fixed::from_str("7").unwrap();
See [Create Transactions](create-transactions.md) to learn how to create
a `SignedTransaction` used in the examples below.

let some_expression = MintExpr::new(asset_quantity, asset_id);
```rust
fn submit_transaction_do_not_wait_for_approval(
iroha: &Client,
transaction: &SignedTransaction
) {
// If the transaction is rejected, the method panics
iroha.submit_transaction(transaction).unwrap()
}

//The client module in the iroha_client crate has 2 methods for the transaction submission
//* submit() - sends a transaction without waiting for approval
//* submit_blocking() - sends a transaction and waits for approving.
//If the transaction will be rejected, the method will panic
iroha_client.submit_blocking(some_expression).unwrap();
fn submit_transaction_and_wait_for_approval(
iroha: &Client,
transaction: &SignedTransaction
) {
// If the transaction is rejected, the method panics
iroha.submit_transaction_blocking(transaction).unwrap()
}
```
29 changes: 28 additions & 1 deletion src/cookbook/transfer-group-assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,33 @@ head:

# How to Transfer a Group of Assets

TODO
Transferring multiple assets atomically involves combining multiple
Transfer instructions in a single transaction.

```rust
fn transfer_group_of_assets(iroha: &Client) {
let alice = "alice@wonderland".parse().unwrap();
let mouse = "mouse@wonderland".parse().unwrap();
let transfer_roses_from_alice_to_mouse = Transfer::asset_numeric(
AssetId::new("rose#wonderland".parse().unwrap(), alice.clone()),
numeric!(1),
mouse.clone(),
);
let transfer_coins_from_alice_to_mouse = Transfer::asset_numeric(
AssetId::new("coin#wonderland".parse().unwrap(), alice.clone()),
numeric!(0.99),
mouse.clone(),
);
let transfer_hat_from_alice_to_mouse = Transfer::asset_store(
AssetId::new("hat#wonderland".parse().unwrap(), alice),
mouse,
);
let transfers: [TransferBox; 3] = [
transfer_roses_from_alice_to_mouse,
transfer_coins_from_alice_to_mouse,
transfer_hat_from_alice_to_mouse,
];
iroha.submit_all(transfers).unwrap();
}
```
<!-- related issue: https://github.com/hyperledger/iroha-2-docs/issues/369 -->
16 changes: 8 additions & 8 deletions src/cookbook/unregister-accounts.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ head:

# How to Unregister Accounts

Only domain owners and accounts with an appropriate permission token
can unregister another account.

```rust
//Note:
// Only domain owner or account with appropriate permission token can unregister another account
//First we need to define an account id that we want to unregister
let account_id = AccountId::from_str("artem@disneyland").unwrap();
//Then we need to define an Unregister expression
let expression = UnregisterExpr::new(account_id);
//And finally we need to send the transaction
iroha_client.submit_blocking(expression).unwrap();
fn unregister_account(iroha: &Client) {
let alice = AccountId::from_str("alice@wonderland").unwrap();
let unregister_alice = Unregister::account(alice);
iroha.submit(unregister_alice).unwrap();
}
```
14 changes: 7 additions & 7 deletions src/cookbook/unregister-asset-definitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ head:
# How to Unregister an Asset Definition

```rust
//First we need to define an asset definition id that we want to unregister
let asset_definition_id: AssetDefinitionId = AssetDefinitionId::from_str("wondercoins#wonderland").unwrap();
//Then we need to define an Unregister expression with the asset definition id
let expression: UnregisterExpr = UnregisterExpr::new(asset_definition_id);
//And finally we need to send the transaction
iroha_client.submit_blocking(expression).unwrap();
```
fn undefine_asset(
iroha: &Client,
) {
let hats = AssetDefinitionId::from_str("hat#outfit").unwrap();
iroha.submit(Unregister::asset_definition(hats)).unwrap();
}
```
17 changes: 7 additions & 10 deletions src/cookbook/unregister-assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ head:
# How to Unregister an Asset

```rust
//First we need to define an account id which has an asset that we want to unregister
let account_id = AccountId::from_str("alina@wonderland").unwrap();
//Then we need to define an asset definition id that we want to unregister
let asset_definition_id: AssetDefinitionId = AssetDefinitionId::from_str("romancoin#wonderland").unwrap();
//After, define the asset id as a new object that consists of asset definition id and account id
let asset_id: AssetId = AssetId::new(asset_definition_id, account_id);
// Define an Unregister expression with the asset id
let expression: UnregisterExpr = UnregisterExpr::new(asset_id);
//And finally we need to send the transaction
iroha_client.submit_blocking(expression).unwrap()
fn unregister_asset(
iroha: &Client,
) {
let roses_of_alice = AssetId::from_str("rose##alice@wonderland").unwrap();
let unregister_roses_of_alice = Unregister::asset(roses_of_alice);
iroha.submit(unregister_roses_of_alice).unwrap();
}
```
11 changes: 5 additions & 6 deletions src/cookbook/unregister-domains.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ head:
# How to Unregister a Domain

```rust
//First we need to define a domain id (name) that we want to unregister
let domain_id = DomainId::new("disneyland".parse().unwrap());
//Then we need to define an Unregister expression for the domain
let expression = UnregisterExpr::new(domain_id);
//And finally we need to send the transaction
iroha_client.submit_blocking(expression).unwrap();
fn unregister_domain(iroha: &Client) {
let wonderland = DomainId::from_str("wonderland").unwrap();
let unregister_wonderland = Unregister::domain(wonderland);
iroha.submit(unregister_wonderland).unwrap();
}
```

0 comments on commit 8de8655

Please sign in to comment.