Skip to content

Commit

Permalink
[fix] hyperledger-iroha#161: Register and Mint Asset
Browse files Browse the repository at this point in the history
Signed-off-by: Sam H. Smith <sam.henning.smith@protonmail.com>
  • Loading branch information
SamHSmith committed Feb 5, 2024
1 parent 4a9b430 commit 84d0280
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 21 deletions.
12 changes: 4 additions & 8 deletions docs-recipes/2.3.client.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@
password,
api_url)

pyasset_id = iroha.AssetDefinitionId("pyasset", "wonderland")
alice = iroha.AccountId("alice", "wonderland")
bob = iroha.AccountId("bob", "wonderland")
domains = client.query_all_domains()

print("Listing all domains...")
for d in domains:
print(" - ", d,)

register = iroha.Instruction.register(iroha.NewAssetDefinition(pyasset_id, iroha.AssetValueType.QUANTITY))
mint = iroha.Instruction.mint(1024, iroha.AssetId(pyasset_id, alice))
transfer = iroha.Instruction.transfer(512, iroha.AssetId(pyasset_id, alice), bob)

client.submit_executable([register, mint, transfer])
53 changes: 53 additions & 0 deletions docs-recipes/5.register-and-mint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import iroha

key_pair = iroha.KeyPair.from_json("""
{
"public_key": "ed01207233BFC89DCBD68C19FDE6CE6158225298EC1131B6A130D1AEB454C1AB5183C0",
"private_key": {
"digest_function": "ed25519",
"payload": "9ac47abf59b356e0bd7dcbbbb4dec080e302156a48ca907e47cb6aea1d32719e7233bfc89dcbd68c19fde6ce6158225298ec1131b6a130d1aeb454c1ab5183c0"
}
}
""")

account_id = "alice@wonderland"
web_login = "mad_hatter"
password = "ilovetea"
api_url = "http://127.0.0.1:8080/"
telemetry_url = "http://127.0.0.1:8180/"

client = iroha.Client.create(
key_pair,
account_id,
web_login,
password,
api_url)

assets = client.query_all_assets_owned_by_account("alice@wonderland")

print("Listing all assets owned by alice@wonderland...")
for a in assets:
print(" - ", a,)


asset_definition_id = "time#wonderland"
asset_id = "time##alice@wonderland"

if "time##alice@wonderland" in assets:
print("'alice@wonderland' already has asset 'time'.")

register_definition = iroha.Instruction.register_asset_definition(asset_definition_id, "Quantity")

mint = iroha.Instruction.mint_asset(5, asset_id, "Quantity")

client.submit_executable([register_definition, mint])

while True:
assets = client.query_all_assets_owned_by_account("alice@wonderland")

if asset_id in assets:
break

print("Listing all assets owned by alice@wonderland...")
for a in assets:
print(" - ", a,)
22 changes: 22 additions & 0 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ impl Client {
}
Ok(items)
}

fn query_all_assets_owned_by_account(&self, account_id: &str) -> PyResult<Vec<String>> {
let query = iroha_data_model::query::prelude::FindAssetsByAccountId {
account_id: AccountId::from_str(account_id)
.map_err(|e| PyValueError::new_err(e.to_string()))?
.into(),
};

let val = self
.client
.request(query)
.map_err(|e| PyRuntimeError::new_err(format!("{e:?}")))?;

let mut items = Vec::new();
for item in val {
items.push(
item.map(|d| d.id.to_string())
.map_err(|e| PyRuntimeError::new_err(format!("{e:?}")))?,
);
}
Ok(items)
}
}

macro_rules! register_query {
Expand Down
56 changes: 43 additions & 13 deletions src/isi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use iroha_data_model::isi::{
};
use iroha_data_model::prelude::*;
use iroha_data_model::NumericValue;
use iroha_primitives::fixed::FixedPointOperationError;
use pyo3::{exceptions::PyValueError, prelude::*};

use std::str::FromStr;
Expand Down Expand Up @@ -61,7 +62,7 @@ impl PyInstruction {
}

#[staticmethod]
/// Create an instruction for registering a new domain.
/// Create an instruction for registering a new account.
fn register_account(
account_id: &str,
public_keys: Vec<PyPublicKey>,
Expand All @@ -77,6 +78,26 @@ impl PyInstruction {
))));
}

#[staticmethod]
/// Create an instruction for registering a new account.
fn register_asset_definition(
asset_definition_id: &str,
value_type: &str,
) -> PyResult<PyInstruction> {
let new_definition_object = NewAssetDefinition {
id: AssetDefinitionId::from_str(asset_definition_id)
.map_err(|e| PyValueError::new_err(e.to_string()))?,
value_type: AssetValueType::from_str(value_type)
.map_err(|e| PyValueError::new_err(e.to_string()))?,
mintable: Mintable::Infinitely,
logo: None,
metadata: Metadata::default(),
};
return Ok(PyInstruction(InstructionExpr::Register(RegisterExpr::new(
new_definition_object,
))));
}

#[staticmethod]
/// Create an instruction for un-registering an object.
/// Currently supported are: Account, AssetDefinition and Domain
Expand Down Expand Up @@ -174,22 +195,31 @@ impl PyInstruction {

#[staticmethod]
// Mint value to an Asset
fn mint(py: Python<'_>, value: PyObject, to: PyAssetId) -> PyResult<PyInstruction> {
let val = if let Ok(val) = value.extract::<u32>(py) {
NumericValue::U32(val)
} else if let Ok(val) = value.extract::<u128>(py) {
NumericValue::U128(val)
} else if let Ok(val) = value.extract::<f64>(py) {
let fixed = val.try_into().map_err(|e| {
PyValueError::new_err(format!("Couldn't convert {} to fixed: {}", val, e))
})?;
NumericValue::Fixed(fixed)
fn mint_asset(
py: Python<'_>,
value: PyObject,
asset_id: &str,
value_type: &str,
) -> PyResult<PyInstruction> {
let val = if value_type == "Quantity" {
NumericValue::U32(value.extract::<u32>(py)?)
} else if value_type == "BigQuantity" {
NumericValue::U128(value.extract::<u128>(py)?)
} else if value_type == "Fixed" {
NumericValue::Fixed(
value
.extract::<f64>(py)?
.try_into()
.map_err(|e: FixedPointOperationError| PyValueError::new_err(e.to_string()))?,
)
} else {
return Err(PyValueError::new_err("Invalid value to transfer"));
// TODO(Sam): Add Store variant because it's needed for metadata.
return Err(PyValueError::new_err("Value type is wrong. It needs to be one of these 4: Quantity, BigQuantity, Fixed or Store."));
};

Ok(PyInstruction(InstructionExpr::Mint(MintExpr::new(
val, to.0,
val,
AssetId::from_str(asset_id).map_err(|e| PyValueError::new_err(e.to_string()))?,
))))
}
}
Expand Down

0 comments on commit 84d0280

Please sign in to comment.