Skip to content

Commit

Permalink
[add]: register-roles, use-instructions, work-with-numeric-assets
Browse files Browse the repository at this point in the history
  • Loading branch information
nxsaken committed Apr 10, 2024
1 parent a4af32b commit 5a53957
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 3 deletions.
38 changes: 37 additions & 1 deletion src/cookbook/register-roles.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,40 @@ head:

# How to Register a Role

TODO
The minimal case is an empty role (without any permission tokens):

```rust
fn register_new_role(
role_name: &str,
iroha_client: &Client
) {
let role_id = RoleId::from_str(role_name).unwrap();
let role = iroha_data_model::role::Role::new(role_id);
let register_role = Register::role(role);
iroha_client.submit(register_role).unwrap();
}
```

Permission tokens may be added to a role. In the following example,
a predefined `CanUnregisterDomain` permission token is used.

You can also define your own permission tokens,
see [Define Custom Permission Tokens](define-custom-permission-tokens.md).

```rust
fn register_new_role_with_permission(
role_name: &str,
domain_id: DomainId,
iroha_client: &Client
) {
let role_id = RoleId::from_str(role_name).unwrap();
let can_unregister_domain = PermissionToken::new(
"CanUnregisterDomain".parse().unwrap(),
&json!({ "domain_id": domain_id }),
);
let role = iroha_data_model::role::Role::new(role_id)
.add_permission(can_unregister_domain);
let register_role = Register::role(role);
iroha_client.submit(register_role).unwrap();
}
```
18 changes: 17 additions & 1 deletion src/cookbook/use-instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,20 @@ head:

# How to Use Iroha Special Instructions

TODO
Building and submitting an instruction:

```rust
fn use_instruction(
client: &Client,
roses: AssetDefinitionId,
alice: AccountId,
) {
// build an instruction
let mint_roses_for_alice = Mint::asset_numeric(
42_u32,
AssetId::new(roses, alice)
);
// submit the instruction
client.submit(mint_roses_for_alice).unwrap();
}
```
50 changes: 49 additions & 1 deletion src/cookbook/work-with-numeric-assets.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,52 @@ head:

# How to Work with Numeric Assets

TODO
Minting roses for Alice:

```rust
fn mint_numeric_asset(
client: &Client,
roses: AssetDefinitionId,
alice: AccountId,
) {
let mint_roses_for_alice = Mint::asset_numeric(
42_u32,
AssetId::new(roses, alice)
);
client.submit(mint_roses_for_alice).unwrap();
}
```

Burning Alice's roses:

```rust
fn burn_numeric_asset(
client: &Client,
roses: AssetDefinitionId,
alice: AccountId,
) {
let burn_roses_of_alice = Mint::asset_numeric(
8_u32,
AssetId::new(roses, alice)
);
client.submit(burn_roses_of_alice).unwrap();
}
```

Transferring Alice's roses to Mouse:

```rust
fn transfer_numeric_asset(
client: &Client,
roses: AssetDefinitionId,
alice: AccountId,
mouse: AccountId,
) {
let transfer_roses_from_alice_to_mouse = Transfer::asset_numeric(
AssetId::new(roses, alice),
13_u32,
mouse
);
client.submit(transfer_roses_from_alice_to_mouse).unwrap();
}
```

0 comments on commit 5a53957

Please sign in to comment.