Skip to content

Commit

Permalink
add separate additional data for anyonecanstake
Browse files Browse the repository at this point in the history
  • Loading branch information
OBorce committed Aug 26, 2024
1 parent bb4e3a7 commit b31700d
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 103 deletions.
36 changes: 24 additions & 12 deletions wallet/src/send_request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use common::primitives::{Amount, BlockHeight};
use crypto::vrf::VRFPublicKey;
use utils::ensure;
use wallet_types::partially_signed_transaction::{
PartiallySignedTransaction, UtxoAdditionalInfo, UtxoWithAdditionalInfo,
PartiallySignedTransaction, TokenAdditionalInfo, UtxoAdditionalInfo, UtxoWithAdditionalInfo,
};

use crate::account::currency_grouper::Currency;
Expand Down Expand Up @@ -322,6 +322,7 @@ impl SendRequest {
}
}

/// Find aditional data for TxOutput, mainly for UI purposes
fn find_additional_info(
utxo: &TxOutput,
additional_info: &BTreeMap<PoolOrTokenId, UtxoAdditionalInfo>,
Expand All @@ -331,17 +332,22 @@ fn find_additional_info(
| TxOutput::Htlc(value, _)
| TxOutput::Transfer(value, _)
| TxOutput::LockThenTransfer(value, _, _) => {
find_additional_info_output_value(value, additional_info)?
find_token_additional_info(value, additional_info)?.map(UtxoAdditionalInfo::TokenInfo)
}
TxOutput::AnyoneCanTake(data) => {
find_additional_info_output_value(data.as_ref().ask(), additional_info)?
let ask = find_token_additional_info(data.ask(), additional_info)?;
let give = find_token_additional_info(data.give(), additional_info)?;

Some(UtxoAdditionalInfo::AnyoneCanTake { ask, give })
}
TxOutput::IssueNft(_, data, _) => {
Some(UtxoAdditionalInfo::TokenInfo(TokenAdditionalInfo {
num_decimals: 0,
ticker: match data.as_ref() {
NftIssuance::V0(data) => data.metadata.ticker().clone(),
},
}))
}
TxOutput::IssueNft(_, data, _) => Some(UtxoAdditionalInfo::TokenInfo {
num_decimals: 0,
ticker: match data.as_ref() {
NftIssuance::V0(data) => data.metadata.ticker().clone(),
},
}),
TxOutput::CreateStakePool(_, data) => Some(UtxoAdditionalInfo::PoolInfo {
staker_balance: data.pledge(),
}),
Expand All @@ -358,17 +364,23 @@ fn find_additional_info(
Ok(additional_info)
}

fn find_additional_info_output_value(
fn find_token_additional_info(
value: &OutputValue,
additional_info: &BTreeMap<PoolOrTokenId, UtxoAdditionalInfo>,
) -> WalletResult<Option<UtxoAdditionalInfo>> {
) -> WalletResult<Option<TokenAdditionalInfo>> {
match value {
OutputValue::Coin(_) | OutputValue::TokenV0(_) => Ok(None),
OutputValue::TokenV1(token_id, _) => additional_info
.get(&PoolOrTokenId::TokenId(*token_id))
.ok_or(WalletError::MissingTokenAdditionalData(*token_id))
.cloned()
.map(Some),
.map(|data| match data {
UtxoAdditionalInfo::TokenInfo(data) => Ok(Some(data.clone())),
UtxoAdditionalInfo::PoolInfo { staker_balance: _ }
| UtxoAdditionalInfo::AnyoneCanTake { ask: _, give: _ } => {
Err(WalletError::MissmatchedTokenAdditionalData(*token_id))
}
})?,
}
}

Expand Down
14 changes: 8 additions & 6 deletions wallet/src/signer/trezor_signer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ use wallet_storage::{
use wallet_types::{
account_info::DEFAULT_ACCOUNT_INDEX,
partially_signed_transaction::{
PartiallySignedTransaction, UtxoAdditionalInfo, UtxoWithAdditionalInfo,
PartiallySignedTransaction, TokenAdditionalInfo, UtxoAdditionalInfo, UtxoWithAdditionalInfo,
},
signature_status::SignatureStatus,
AccountId,
Expand Down Expand Up @@ -718,16 +718,18 @@ fn to_trezor_output_value(
let mut token_value = MintlayerTokenOutputValue::new();
token_value.set_token_id(token_id.to_hash().as_bytes().to_vec());
match additional_info {
Some(UtxoAdditionalInfo::TokenInfo {
Some(UtxoAdditionalInfo::TokenInfo(TokenAdditionalInfo {
num_decimals,
ticker,
}) => {
})) => {
token_value.set_number_of_decimals(*num_decimals as u32);
token_value.set_token_ticker(ticker.clone());
}
Some(UtxoAdditionalInfo::PoolInfo { staker_balance: _ }) | None => {
return Err(SignerError::MissingUtxoExtraInfo)
}
Some(
UtxoAdditionalInfo::PoolInfo { staker_balance: _ }
| UtxoAdditionalInfo::AnyoneCanTake { ask: _, give: _ },
)
| None => return Err(SignerError::MissingUtxoExtraInfo),
}
value.token = Some(token_value).into();
value
Expand Down
65 changes: 22 additions & 43 deletions wallet/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ use wallet_storage::{
use wallet_types::account_info::{StandaloneAddressDetails, StandaloneAddresses};
use wallet_types::chain_info::ChainInfo;
use wallet_types::partially_signed_transaction::{
PartiallySignedTransaction, PartiallySignedTransactionCreationError, UtxoAdditionalInfo,
PartiallySignedTransaction, PartiallySignedTransactionCreationError, TokenAdditionalInfo,
UtxoAdditionalInfo,
};
use wallet_types::seed_phrase::SerializableSeedPhrase;
use wallet_types::signature_status::SignatureStatus;
Expand Down Expand Up @@ -243,6 +244,8 @@ pub enum WalletError {
MissingPoolAdditionalData(PoolId),
#[error("Missing additional data for Token {0}")]
MissingTokenAdditionalData(TokenId),
#[error("Missmatched additional data for token {0}")]
MissmatchedTokenAdditionalData(TokenId),
}

/// Result type used for the wallet
Expand Down Expand Up @@ -1505,13 +1508,7 @@ where
consolidate_fee_rate: FeeRate,
) -> WalletResult<SignedTransaction> {
let latest_median_time = self.latest_median_time;
let additional_utxo_infos = BTreeMap::from_iter([(
PoolOrTokenId::TokenId(token_info.token_id()),
UtxoAdditionalInfo::TokenInfo {
num_decimals: token_info.num_decimals(),
ticker: token_info.token_ticker().to_vec(),
},
)]);
let additional_utxo_infos = to_token_additional_info(token_info);
self.for_account_rw_unlocked_and_check_tx(
account_index,
&additional_utxo_infos,
Expand Down Expand Up @@ -1540,13 +1537,7 @@ where
consolidate_fee_rate: FeeRate,
) -> WalletResult<SignedTransaction> {
let latest_median_time = self.latest_median_time;
let additional_utxo_infos = BTreeMap::from_iter([(
PoolOrTokenId::TokenId(token_info.token_id()),
UtxoAdditionalInfo::TokenInfo {
num_decimals: token_info.num_decimals(),
ticker: token_info.token_ticker().to_vec(),
},
)]);
let additional_utxo_infos = to_token_additional_info(token_info);
self.for_account_rw_unlocked_and_check_tx(
account_index,
&additional_utxo_infos,
Expand All @@ -1573,13 +1564,7 @@ where
consolidate_fee_rate: FeeRate,
) -> WalletResult<SignedTransaction> {
let latest_median_time = self.latest_median_time;
let additional_utxo_infos = BTreeMap::from_iter([(
PoolOrTokenId::TokenId(token_info.token_id()),
UtxoAdditionalInfo::TokenInfo {
num_decimals: token_info.num_decimals(),
ticker: token_info.token_ticker().to_vec(),
},
)]);
let additional_utxo_infos = to_token_additional_info(token_info);
self.for_account_rw_unlocked_and_check_tx(
account_index,
&additional_utxo_infos,
Expand All @@ -1606,13 +1591,7 @@ where
consolidate_fee_rate: FeeRate,
) -> WalletResult<SignedTransaction> {
let latest_median_time = self.latest_median_time;
let additional_utxo_infos = BTreeMap::from_iter([(
PoolOrTokenId::TokenId(token_info.token_id()),
UtxoAdditionalInfo::TokenInfo {
num_decimals: token_info.num_decimals(),
ticker: token_info.token_ticker().to_vec(),
},
)]);
let additional_utxo_infos = to_token_additional_info(token_info);
self.for_account_rw_unlocked_and_check_tx(
account_index,
&additional_utxo_infos,
Expand All @@ -1639,13 +1618,7 @@ where
consolidate_fee_rate: FeeRate,
) -> WalletResult<SignedTransaction> {
let latest_median_time = self.latest_median_time;
let additional_utxo_infos = BTreeMap::from_iter([(
PoolOrTokenId::TokenId(token_info.token_id()),
UtxoAdditionalInfo::TokenInfo {
num_decimals: token_info.num_decimals(),
ticker: token_info.token_ticker().to_vec(),
},
)]);
let additional_utxo_infos = to_token_additional_info(token_info);
self.for_account_rw_unlocked_and_check_tx(
account_index,
&additional_utxo_infos,
Expand All @@ -1672,13 +1645,7 @@ where
consolidate_fee_rate: FeeRate,
) -> WalletResult<SignedTransaction> {
let latest_median_time = self.latest_median_time;
let additional_utxo_infos = BTreeMap::from_iter([(
PoolOrTokenId::TokenId(token_info.token_id()),
UtxoAdditionalInfo::TokenInfo {
num_decimals: token_info.num_decimals(),
ticker: token_info.token_ticker().to_vec(),
},
)]);
let additional_utxo_infos = to_token_additional_info(token_info);
self.for_account_rw_unlocked_and_check_tx(
account_index,
&additional_utxo_infos,
Expand Down Expand Up @@ -2070,6 +2037,18 @@ where
}
}

fn to_token_additional_info(
token_info: &UnconfirmedTokenInfo,
) -> BTreeMap<PoolOrTokenId, UtxoAdditionalInfo> {
BTreeMap::from_iter([(
PoolOrTokenId::TokenId(token_info.token_id()),
UtxoAdditionalInfo::TokenInfo(TokenAdditionalInfo {
num_decimals: token_info.num_decimals(),
ticker: token_info.token_ticker().to_vec(),
}),
)])
}

impl<B, P> Wallet<B, P>
where
B: storage::Backend + 'static,
Expand Down
8 changes: 4 additions & 4 deletions wallet/src/wallet/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2222,10 +2222,10 @@ fn issue_and_transfer_tokens(#[case] seed: Seed) {

let additional_info = BTreeMap::from_iter([(
PoolOrTokenId::TokenId(*token_id),
UtxoAdditionalInfo::TokenInfo {
UtxoAdditionalInfo::TokenInfo(TokenAdditionalInfo {
num_decimals: number_of_decimals,
ticker: token_ticker.clone(),
},
}),
)]);
let transfer_tokens_transaction = wallet
.create_transaction_to_addresses(
Expand Down Expand Up @@ -2573,10 +2573,10 @@ fn freeze_and_unfreeze_tokens(#[case] seed: Seed) {

let additional_info = BTreeMap::from_iter([(
PoolOrTokenId::TokenId(issued_token_id),
UtxoAdditionalInfo::TokenInfo {
UtxoAdditionalInfo::TokenInfo(TokenAdditionalInfo {
num_decimals: unconfirmed_token_info.num_decimals(),
ticker: unconfirmed_token_info.token_ticker().to_vec(),
},
}),
)]);
let transfer_tokens_transaction = wallet
.create_transaction_to_addresses(
Expand Down
16 changes: 14 additions & 2 deletions wallet/types/src/partially_signed_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,23 @@ pub enum PartiallySignedTransactionCreationError {
TxCreationError(#[from] TransactionCreationError),
}

#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode)]
pub struct TokenAdditionalInfo {
pub num_decimals: u8,
pub ticker: Vec<u8>,
}

/// Additional info for UTXOs
#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode)]
pub enum UtxoAdditionalInfo {
TokenInfo { num_decimals: u8, ticker: Vec<u8> },
PoolInfo { staker_balance: Amount },
TokenInfo(TokenAdditionalInfo),
PoolInfo {
staker_balance: Amount,
},
AnyoneCanTake {
ask: Option<TokenAdditionalInfo>,
give: Option<TokenAdditionalInfo>,
},
}

#[derive(Debug, Eq, PartialEq, Clone, Encode, Decode)]
Expand Down
63 changes: 32 additions & 31 deletions wallet/wallet-controller/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use futures::{
use node_comm::node_traits::NodeInterface;
use wallet::{account::currency_grouper::Currency, get_tx_output_destination, WalletError};
use wallet_types::partially_signed_transaction::{
PartiallySignedTransaction, UtxoAdditionalInfo, UtxoWithAdditionalInfo,
PartiallySignedTransaction, TokenAdditionalInfo, UtxoAdditionalInfo, UtxoWithAdditionalInfo,
};

use crate::{types::Balances, ControllerError, WalletType2};
Expand Down Expand Up @@ -131,6 +131,25 @@ fn pool_id_from_txo(utxo: &TxOutput) -> Option<PoolId> {
}
}

async fn fetch_token_extra_info<T>(
rpc_client: &T,
value: &OutputValue,
) -> Result<Option<TokenAdditionalInfo>, ControllerError<T>>
where
T: NodeInterface,
{
match value {
OutputValue::Coin(_) | OutputValue::TokenV0(_) => Ok(None),
OutputValue::TokenV1(token_id, _) => {
let info = fetch_token_info(rpc_client, *token_id).await?;
Ok(Some(TokenAdditionalInfo {
num_decimals: info.token_number_of_decimals(),
ticker: info.token_ticker().to_vec(),
}))
}
}
}

pub async fn fetch_utxo_extra_info<T>(
rpc_client: &T,
utxo: TxOutput,
Expand All @@ -142,36 +161,18 @@ where
TxOutput::Burn(value)
| TxOutput::Transfer(value, _)
| TxOutput::LockThenTransfer(value, _, _)
| TxOutput::Htlc(value, _) => match value {
OutputValue::Coin(_) | OutputValue::TokenV0(_) => {
Ok(UtxoWithAdditionalInfo::new(utxo, None))
}
OutputValue::TokenV1(token_id, _) => {
let info = fetch_token_info(rpc_client, *token_id).await?;
Ok(UtxoWithAdditionalInfo::new(
utxo,
Some(UtxoAdditionalInfo::TokenInfo {
num_decimals: info.token_number_of_decimals(),
ticker: info.token_ticker().to_vec(),
}),
))
}
},
TxOutput::AnyoneCanTake(order) => match order.ask() {
OutputValue::Coin(_) | OutputValue::TokenV0(_) => {
Ok(UtxoWithAdditionalInfo::new(utxo, None))
}
OutputValue::TokenV1(token_id, _) => {
let info = fetch_token_info(rpc_client, *token_id).await?;
Ok(UtxoWithAdditionalInfo::new(
utxo,
Some(UtxoAdditionalInfo::TokenInfo {
num_decimals: info.token_number_of_decimals(),
ticker: info.token_ticker().to_vec(),
}),
))
}
},
| TxOutput::Htlc(value, _) => {
let additional_info = fetch_token_extra_info(rpc_client, value)
.await?
.map(UtxoAdditionalInfo::TokenInfo);
Ok(UtxoWithAdditionalInfo::new(utxo, additional_info))
}
TxOutput::AnyoneCanTake(order) => {
let ask = fetch_token_extra_info(rpc_client, order.ask()).await?;
let give = fetch_token_extra_info(rpc_client, order.ask()).await?;
let additional_info = Some(UtxoAdditionalInfo::AnyoneCanTake { ask, give });
Ok(UtxoWithAdditionalInfo::new(utxo, additional_info))
}
TxOutput::ProduceBlockFromStake(_, pool_id) => {
let staker_balance = rpc_client
.get_staker_balance(*pool_id)
Expand Down
Loading

0 comments on commit b31700d

Please sign in to comment.