Skip to content

Commit

Permalink
feat: add support for make-it-rain command (#3830)
Browse files Browse the repository at this point in the history
Description
---
Add support for make-it-rain command to collectibles.

Motivation and Context
---
Good for testing as well.

How Has This Been Tested?
---
Manually.
  • Loading branch information
Cifko authored Feb 23, 2022
1 parent da59c85 commit 0322402
Show file tree
Hide file tree
Showing 10 changed files with 420 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,62 @@ pub(crate) async fn asset_wallets_create(
asset_public_key: String,
state: tauri::State<'_, ConcurrentAppState>,
app: tauri::AppHandle,
) -> Result<(), Status> {
inner_asset_wallets_create(asset_public_key, state.inner(), app).await
}

#[tauri::command]
pub(crate) async fn asset_wallets_get_balance(
asset_public_key: String,
state: tauri::State<'_, ConcurrentAppState>,
) -> Result<u64, Status> {
inner_asset_wallets_get_balance(asset_public_key, state.inner()).await
}

#[tauri::command]
pub(crate) async fn asset_wallets_get_unspent_amounts(
state: tauri::State<'_, ConcurrentAppState>,
) -> Result<Vec<u64>, Status> {
inner_asset_wallets_get_unspent_amounts(state.inner()).await
}

#[tauri::command]
pub(crate) async fn asset_wallets_list(
state: tauri::State<'_, ConcurrentAppState>,
) -> Result<Vec<AssetRow>, Status> {
inner_asset_wallets_list(state.inner()).await
}

#[tauri::command]
pub(crate) async fn asset_wallets_create_address(
asset_public_key: String,
state: tauri::State<'_, ConcurrentAppState>,
) -> Result<AddressRow, Status> {
inner_asset_wallets_create_address(asset_public_key, state.inner()).await
}

#[tauri::command]
pub(crate) async fn asset_wallets_get_latest_address(
asset_public_key: String,
state: tauri::State<'_, ConcurrentAppState>,
) -> Result<AddressRow, Status> {
inner_asset_wallets_get_latest_address(asset_public_key, state.inner()).await
}

#[tauri::command]
pub(crate) async fn asset_wallets_send_to(
asset_public_key: String,
amount: u64,
to_address: String,
state: tauri::State<'_, ConcurrentAppState>,
) -> Result<(), Status> {
inner_asset_wallets_send_to(asset_public_key, amount, to_address, state.inner()).await
}

pub(crate) async fn inner_asset_wallets_create(
asset_public_key: String,
state: &ConcurrentAppState,
app: tauri::AppHandle,
) -> Result<(), Status> {
let wallet_id = state
.current_wallet_id()
Expand Down Expand Up @@ -119,10 +175,9 @@ pub(crate) async fn asset_wallets_create(
Ok(())
}

#[tauri::command]
pub(crate) async fn asset_wallets_get_balance(
pub(crate) async fn inner_asset_wallets_get_balance(
asset_public_key: String,
state: tauri::State<'_, ConcurrentAppState>,
state: &ConcurrentAppState,
) -> Result<u64, Status> {
debug!(
target: LOG_TARGET,
Expand Down Expand Up @@ -175,19 +230,17 @@ pub(crate) async fn asset_wallets_get_balance(
Ok(total)
}

#[tauri::command]
pub(crate) async fn asset_wallets_get_unspent_amounts(
state: tauri::State<'_, ConcurrentAppState>,
pub(crate) async fn inner_asset_wallets_get_unspent_amounts(
state: &ConcurrentAppState,
) -> Result<Vec<u64>, Status> {
let mut client = state.create_wallet_client().await;
client.connect().await?;
let result = client.get_unspent_amounts().await?;
Ok(result.amount)
}

#[tauri::command]
pub(crate) async fn asset_wallets_list(
state: tauri::State<'_, ConcurrentAppState>,
pub(crate) async fn inner_asset_wallets_list(
state: &ConcurrentAppState,
) -> Result<Vec<AssetRow>, Status> {
let wallet_id = state
.current_wallet_id()
Expand All @@ -202,10 +255,9 @@ pub(crate) async fn asset_wallets_list(
Ok(result)
}

#[tauri::command]
pub(crate) async fn asset_wallets_create_address(
pub(crate) async fn inner_asset_wallets_create_address(
asset_public_key: String,
state: tauri::State<'_, ConcurrentAppState>,
state: &ConcurrentAppState,
) -> Result<AddressRow, Status> {
let wallet_id = state
.current_wallet_id()
Expand Down Expand Up @@ -241,10 +293,9 @@ pub(crate) async fn asset_wallets_create_address(
Ok(address)
}

#[tauri::command]
pub(crate) async fn asset_wallets_get_latest_address(
pub(crate) async fn inner_asset_wallets_get_latest_address(
asset_public_key: String,
state: tauri::State<'_, ConcurrentAppState>,
state: &ConcurrentAppState,
) -> Result<AddressRow, Status> {
let wallet_id = state
.current_wallet_id()
Expand All @@ -266,12 +317,11 @@ pub(crate) async fn asset_wallets_get_latest_address(
)
}

#[tauri::command]
pub(crate) async fn asset_wallets_send_to(
pub(crate) async fn inner_asset_wallets_send_to(
asset_public_key: String,
amount: u64,
to_address: String,
state: tauri::State<'_, ConcurrentAppState>,
state: &ConcurrentAppState,
) -> Result<(), Status> {
let wallet_id = state
.current_wallet_id()
Expand Down
167 changes: 107 additions & 60 deletions applications/tari_collectibles/src-tauri/src/commands/assets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,103 @@ pub(crate) async fn assets_create(
template_ids: Vec<u32>,
template_parameters: Vec<TemplateParameter>,
state: tauri::State<'_, ConcurrentAppState>,
) -> Result<String, Status> {
inner_assets_create(
name,
description,
image,
template_ids,
template_parameters,
state.inner(),
)
.await
}

#[tauri::command]
pub(crate) async fn assets_list_owned(
state: tauri::State<'_, ConcurrentAppState>,
) -> Result<Vec<AssetInfo>, Status> {
inner_assets_list_owned(state.inner()).await
}

// TODO: remove and use better serializer
#[derive(Debug)]
struct AssetMetadata {
name: String,
description: String,
image: String,
}

trait AssetMetadataDeserializer {
fn deserialize(&self, metadata: &[u8]) -> AssetMetadata;
}
trait AssetMetadataSerializer {
fn serialize(&self, model: &AssetMetadata) -> Vec<u8>;
}

struct V1AssetMetadataSerializer {}

impl AssetMetadataDeserializer for V1AssetMetadataSerializer {
fn deserialize(&self, metadata: &[u8]) -> AssetMetadata {
let m = String::from_utf8(Vec::from(metadata)).unwrap();
let mut m = m
.as_str()
.split('|')
.map(|s| s.to_string())
.collect::<Vec<String>>()
.into_iter();
let name = m.next();
let description = m.next();
let image = m.next();

AssetMetadata {
name: name.unwrap_or_else(|| "".to_string()),
description: description.unwrap_or_else(|| "".to_string()),
image: image.unwrap_or_else(|| "".to_string()),
}
}
}

impl AssetMetadataSerializer for V1AssetMetadataSerializer {
fn serialize(&self, model: &AssetMetadata) -> Vec<u8> {
let str = format!("{}|{}|{}", model.name, model.description, model.image);

str.into_bytes()
}
}

#[tauri::command]
pub(crate) async fn assets_list_registered_assets(
offset: u64,
count: u64,
state: tauri::State<'_, ConcurrentAppState>,
) -> Result<Vec<RegisteredAssetInfo>, Status> {
inner_assets_list_registered_assets(offset, count, state.inner()).await
}

#[tauri::command]
pub(crate) async fn assets_create_initial_checkpoint(
asset_pub_key: String,
state: tauri::State<'_, ConcurrentAppState>,
) -> Result<(), Status> {
inner_assets_create_initial_checkpoint(asset_pub_key, state.inner()).await
}

#[tauri::command]
pub(crate) async fn assets_get_registration(
asset_pub_key: String,
state: tauri::State<'_, ConcurrentAppState>,
) -> Result<RegisteredAssetInfo, Status> {
inner_assets_get_registration(asset_pub_key, state.inner()).await
}

pub(crate) async fn inner_assets_create(
name: String,
description: String,
image: String,
template_ids: Vec<u32>,
template_parameters: Vec<TemplateParameter>,
state: &ConcurrentAppState,
) -> Result<String, Status> {
let wallet_id = state
.current_wallet_id()
Expand Down Expand Up @@ -134,9 +231,8 @@ pub(crate) async fn assets_create(
Ok(res)
}

#[tauri::command]
pub(crate) async fn assets_list_owned(
state: tauri::State<'_, ConcurrentAppState>,
pub(crate) async fn inner_assets_list_owned(
state: &ConcurrentAppState,
) -> Result<Vec<AssetInfo>, Status> {
let mut client = state.create_wallet_client().await;
client.connect().await?;
Expand All @@ -155,57 +251,10 @@ pub(crate) async fn assets_list_owned(
)
}

// TODO: remove and use better serializer
#[derive(Debug)]
struct AssetMetadata {
name: String,
description: String,
image: String,
}

trait AssetMetadataDeserializer {
fn deserialize(&self, metadata: &[u8]) -> AssetMetadata;
}
trait AssetMetadataSerializer {
fn serialize(&self, model: &AssetMetadata) -> Vec<u8>;
}

struct V1AssetMetadataSerializer {}

impl AssetMetadataDeserializer for V1AssetMetadataSerializer {
fn deserialize(&self, metadata: &[u8]) -> AssetMetadata {
let m = String::from_utf8(Vec::from(metadata)).unwrap();
let mut m = m
.as_str()
.split('|')
.map(|s| s.to_string())
.collect::<Vec<String>>()
.into_iter();
let name = m.next();
let description = m.next();
let image = m.next();

AssetMetadata {
name: name.unwrap_or_else(|| "".to_string()),
description: description.unwrap_or_else(|| "".to_string()),
image: image.unwrap_or_else(|| "".to_string()),
}
}
}

impl AssetMetadataSerializer for V1AssetMetadataSerializer {
fn serialize(&self, model: &AssetMetadata) -> Vec<u8> {
let str = format!("{}|{}|{}", model.name, model.description, model.image);

str.into_bytes()
}
}

#[tauri::command]
pub(crate) async fn assets_list_registered_assets(
pub(crate) async fn inner_assets_list_registered_assets(
offset: u64,
count: u64,
state: tauri::State<'_, ConcurrentAppState>,
state: &ConcurrentAppState,
) -> Result<Vec<RegisteredAssetInfo>, Status> {
let mut client = state.connect_base_node_client().await?;
let assets = client.list_registered_assets(offset, count).await?;
Expand Down Expand Up @@ -235,10 +284,9 @@ pub(crate) async fn assets_list_registered_assets(
.collect()
}

#[tauri::command]
pub(crate) async fn assets_create_initial_checkpoint(
asset_public_key: String,
state: tauri::State<'_, ConcurrentAppState>,
pub(crate) async fn inner_assets_create_initial_checkpoint(
asset_pub_key: String,
state: &ConcurrentAppState,
) -> Result<(), Status> {
let mmr = MerkleMountainRange::<Blake256, _>::new(MemBackendVec::new());

Expand All @@ -251,7 +299,7 @@ pub(crate) async fn assets_create_initial_checkpoint(

// create asset reg checkpoint
client
.create_initial_asset_checkpoint(&asset_public_key, merkle_root)
.create_initial_asset_checkpoint(&asset_pub_key, merkle_root)
.await?;

Ok(())
Expand All @@ -274,10 +322,9 @@ pub(crate) async fn assets_create_committee_definition(
Ok(())
}

#[tauri::command]
pub(crate) async fn assets_get_registration(
pub(crate) async fn inner_assets_get_registration(
asset_pub_key: String,
state: tauri::State<'_, ConcurrentAppState>,
state: &ConcurrentAppState,
) -> Result<RegisteredAssetInfo, Status> {
let mut client = state.connect_base_node_client().await?;
let asset_pub_key = PublicKey::from_hex(&asset_pub_key)?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ use tari_common_types::types::PublicKey;
#[tauri::command]
pub(crate) async fn next_asset_public_key(
state: tauri::State<'_, ConcurrentAppState>,
) -> Result<PublicKey, Status> {
inner_next_asset_public_key(state.inner()).await
}

pub(crate) async fn inner_next_asset_public_key(
state: &ConcurrentAppState,
) -> Result<PublicKey, Status> {
let wallet_id = state
.current_wallet_id()
Expand Down
4 changes: 4 additions & 0 deletions applications/tari_collectibles/src-tauri/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ pub mod wallets;

#[tauri::command]
pub async fn create_db(state: tauri::State<'_, ConcurrentAppState>) -> Result<(), String> {
inner_create_db(state.inner()).await
}

pub async fn inner_create_db(state: &ConcurrentAppState) -> Result<(), String> {
let _db = state
.create_db()
.await
Expand Down
Loading

0 comments on commit 0322402

Please sign in to comment.