Skip to content

Commit 96301a5

Browse files
committed
Reuse referenced tokens util in API Server
1 parent aae5e37 commit 96301a5

File tree

5 files changed

+70
-101
lines changed

5 files changed

+70
-101
lines changed

api-server/scanner-lib/src/blockchain_state/mod.rs

Lines changed: 28 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use common::{
3131
config::ChainConfig,
3232
make_order_id,
3333
output_value::OutputValue,
34-
tokens::{make_token_id, IsTokenFrozen, TokenId, TokenIssuance},
34+
tokens::{get_referenced_token_ids, make_token_id, IsTokenFrozen, TokenId, TokenIssuance},
3535
transaction::OutPointSourceId,
3636
AccountCommand, AccountNonce, AccountSpending, Block, DelegationId, Destination, GenBlock,
3737
Genesis, OrderData, OrderId, PoolId, SignedTransaction, Transaction, TxInput, TxOutput,
@@ -594,63 +594,33 @@ async fn calculate_tx_fee_and_collect_token_info<T: ApiServerStorageWrite>(
594594
let input_tasks: FuturesOrdered<_> =
595595
tx.inputs().iter().map(|input| fetch_utxo(input, db_tx)).collect();
596596
let input_utxos: Vec<Option<TxOutput>> = input_tasks.try_collect().await?;
597-
let token_ids = {
598-
let mut token_ids = BTreeSet::new();
599-
for (inp, utxo) in tx.inputs().iter().zip(input_utxos.iter()) {
600-
match inp {
601-
TxInput::Utxo(_) => match utxo.as_ref().expect("must be present") {
602-
TxOutput::Transfer(v, _)
603-
| TxOutput::LockThenTransfer(v, _, _)
604-
| TxOutput::Htlc(v, _) => match v {
605-
OutputValue::TokenV1(token_id, _) => {
606-
token_ids.insert(*token_id);
607-
}
608-
OutputValue::Coin(_) | OutputValue::TokenV0(_) => {}
609-
},
610-
TxOutput::IssueNft(token_id, _, _) => {
611-
token_ids.insert(*token_id);
612-
}
613-
TxOutput::CreateStakePool(_, _)
614-
| TxOutput::Burn(_)
615-
| TxOutput::DataDeposit(_)
616-
| TxOutput::DelegateStaking(_, _)
617-
| TxOutput::CreateDelegationId(_, _)
618-
| TxOutput::IssueFungibleToken(_)
619-
| TxOutput::ProduceBlockFromStake(_, _)
620-
| TxOutput::CreateOrder(_) => {}
621-
},
622-
TxInput::Account(_) => {}
623-
TxInput::AccountCommand(_, cmd) => match cmd {
624-
AccountCommand::MintTokens(token_id, _)
625-
| AccountCommand::FreezeToken(token_id, _)
626-
| AccountCommand::UnmintTokens(token_id)
627-
| AccountCommand::UnfreezeToken(token_id)
628-
| AccountCommand::LockTokenSupply(token_id)
629-
| AccountCommand::ChangeTokenMetadataUri(token_id, _)
630-
| AccountCommand::ChangeTokenAuthority(token_id, _) => {
631-
token_ids.insert(*token_id);
632-
}
633-
AccountCommand::ConcludeOrder(order_id)
634-
| AccountCommand::FillOrder(order_id, _, _) => {
635-
let order = db_tx.get_order(*order_id).await?.expect("must exist");
636-
match order.ask_currency {
637-
CoinOrTokenId::Coin => {}
638-
CoinOrTokenId::TokenId(id) => {
639-
token_ids.insert(id);
640-
}
641-
};
642-
match order.give_currency {
643-
CoinOrTokenId::Coin => {}
644-
CoinOrTokenId::TokenId(id) => {
645-
token_ids.insert(id);
646-
}
647-
};
648-
}
649-
},
650-
};
651-
}
652-
token_ids
653-
};
597+
598+
let token_ids: BTreeSet<_> = tx
599+
.inputs()
600+
.iter()
601+
.zip(input_utxos.iter())
602+
.filter_map(|(inp, utxo)| match inp {
603+
TxInput::Utxo(_) => Some(get_referenced_token_ids(
604+
utxo.as_ref().expect("must be present"),
605+
)),
606+
TxInput::Account(_) => None,
607+
TxInput::AccountCommand(_, cmd) => match cmd {
608+
AccountCommand::MintTokens(token_id, _)
609+
| AccountCommand::FreezeToken(token_id, _)
610+
| AccountCommand::UnmintTokens(token_id)
611+
| AccountCommand::UnfreezeToken(token_id)
612+
| AccountCommand::LockTokenSupply(token_id)
613+
| AccountCommand::ChangeTokenMetadataUri(token_id, _)
614+
| AccountCommand::ChangeTokenAuthority(token_id, _) => {
615+
Some(BTreeSet::from_iter([*token_id]))
616+
}
617+
AccountCommand::ConcludeOrder(_) | AccountCommand::FillOrder(_, _, _) => None,
618+
},
619+
})
620+
.fold(BTreeSet::new(), |mut x, mut y| {
621+
x.append(&mut y);
622+
x
623+
});
654624

655625
let token_tasks: FuturesOrdered<_> = token_ids
656626
.iter()

common/src/chain/tokens/tokens_utils.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16+
use std::collections::BTreeSet;
17+
1618
use super::{TokenData, TokenId};
1719
use crate::{
1820
chain::{output_value::OutputValue, AccountCommand, TxInput, TxOutput},
@@ -88,3 +90,33 @@ pub fn is_token_or_nft_issuance(output: &TxOutput) -> bool {
8890
TxOutput::IssueFungibleToken(_) | TxOutput::IssueNft(_, _, _) => true,
8991
}
9092
}
93+
94+
/// Get any referenced token by this output
95+
/// ignore tokens V0
96+
pub fn get_referenced_token_ids(output: &TxOutput) -> BTreeSet<TokenId> {
97+
match output {
98+
TxOutput::Transfer(v, _)
99+
| TxOutput::LockThenTransfer(v, _, _)
100+
| TxOutput::Burn(v)
101+
| TxOutput::Htlc(v, _) => referenced_token_id(v),
102+
| TxOutput::CreateOrder(data) => {
103+
let mut tokens = referenced_token_id(data.ask());
104+
tokens.extend(referenced_token_id(data.give()));
105+
tokens
106+
}
107+
TxOutput::CreateStakePool(_, _)
108+
| TxOutput::ProduceBlockFromStake(_, _)
109+
| TxOutput::CreateDelegationId(_, _)
110+
| TxOutput::DelegateStaking(_, _)
111+
| TxOutput::DataDeposit(_)
112+
| TxOutput::IssueFungibleToken(_) => BTreeSet::new(),
113+
TxOutput::IssueNft(token_id, _, _) => BTreeSet::from_iter([*token_id]),
114+
}
115+
}
116+
117+
fn referenced_token_id(v: &OutputValue) -> BTreeSet<TokenId> {
118+
match v {
119+
OutputValue::Coin(_) | OutputValue::TokenV0(_) => BTreeSet::new(),
120+
OutputValue::TokenV1(token_id, _) => BTreeSet::from_iter([*token_id]),
121+
}
122+
}

wallet/src/account/output_cache/mod.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ use common::{
2626
output_value::OutputValue,
2727
stakelock::StakePoolData,
2828
tokens::{
29-
make_token_id, IsTokenFreezable, IsTokenUnfreezable, RPCFungibleTokenInfo,
30-
RPCIsTokenFrozen, RPCNonFungibleTokenInfo, RPCTokenTotalSupply, TokenId, TokenIssuance,
31-
TokenTotalSupply,
29+
get_referenced_token_ids, make_token_id, IsTokenFreezable, IsTokenUnfreezable,
30+
RPCFungibleTokenInfo, RPCIsTokenFrozen, RPCNonFungibleTokenInfo, RPCTokenTotalSupply,
31+
TokenId, TokenIssuance, TokenTotalSupply,
3232
},
3333
AccountCommand, AccountNonce, AccountSpending, DelegationId, Destination, GenBlock,
3434
OrderId, OutPointSourceId, PoolId, Transaction, TxInput, TxOutput, UtxoOutPoint,
@@ -49,10 +49,7 @@ use wallet_types::{
4949
AccountWalletTxId, BlockInfo, WalletTx,
5050
};
5151

52-
use crate::{
53-
destination_getters::get_all_tx_output_destinations, send_request::get_referenced_token_ids,
54-
WalletError, WalletResult,
55-
};
52+
use crate::{destination_getters::get_all_tx_output_destinations, WalletError, WalletResult};
5653

5754
pub type UtxoWithTxOutput<'a> = (UtxoOutPoint, &'a TxOutput);
5855

wallet/src/send_request/mod.rs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
use std::collections::{BTreeMap, BTreeSet};
16+
use std::collections::BTreeMap;
1717
use std::mem::take;
1818

1919
use common::address::Address;
@@ -394,36 +394,6 @@ fn find_token_additional_info(
394394
}
395395
}
396396

397-
/// Get any referenced token by this output
398-
/// ignore tokens V0
399-
pub fn get_referenced_token_ids(output: &TxOutput) -> BTreeSet<TokenId> {
400-
match output {
401-
TxOutput::Transfer(v, _)
402-
| TxOutput::LockThenTransfer(v, _, _)
403-
| TxOutput::Burn(v)
404-
| TxOutput::Htlc(v, _) => referenced_token_id(v),
405-
| TxOutput::CreateOrder(data) => {
406-
let mut tokens = referenced_token_id(data.ask());
407-
tokens.extend(referenced_token_id(data.give()));
408-
tokens
409-
}
410-
TxOutput::CreateStakePool(_, _)
411-
| TxOutput::ProduceBlockFromStake(_, _)
412-
| TxOutput::CreateDelegationId(_, _)
413-
| TxOutput::DelegateStaking(_, _)
414-
| TxOutput::DataDeposit(_)
415-
| TxOutput::IssueFungibleToken(_) => BTreeSet::new(),
416-
TxOutput::IssueNft(token_id, _, _) => BTreeSet::from_iter([*token_id]),
417-
}
418-
}
419-
420-
fn referenced_token_id(v: &OutputValue) -> BTreeSet<TokenId> {
421-
match v {
422-
OutputValue::Coin(_) | OutputValue::TokenV0(_) => BTreeSet::new(),
423-
OutputValue::TokenV1(token_id, _) => BTreeSet::from_iter([*token_id]),
424-
}
425-
}
426-
427397
pub enum SelectedInputs {
428398
Utxos(Vec<UtxoOutPoint>),
429399
Inputs(Vec<(UtxoOutPoint, TxOutput)>),

wallet/wallet-controller/src/synced_controller.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ use common::{
2323
output_value::OutputValue,
2424
signature::inputsig::arbitrary_message::ArbitraryMessageSignature,
2525
tokens::{
26-
IsTokenFreezable, IsTokenUnfreezable, Metadata, RPCFungibleTokenInfo, RPCTokenInfo,
27-
TokenId, TokenIssuance, TokenIssuanceV1, TokenTotalSupply,
26+
get_referenced_token_ids, IsTokenFreezable, IsTokenUnfreezable, Metadata,
27+
RPCFungibleTokenInfo, RPCTokenInfo, TokenId, TokenIssuance, TokenIssuanceV1,
28+
TokenTotalSupply,
2829
},
2930
ChainConfig, DelegationId, Destination, OrderId, PoolId, RpcOrderInfo, SignedTransaction,
3031
SignedTransactionIntent, Transaction, TxOutput, UtxoOutPoint,
@@ -48,9 +49,8 @@ use wallet::{
4849
account::{CoinSelectionAlgo, TransactionToSign, UnconfirmedTokenInfo},
4950
destination_getters::{get_tx_output_destination, HtlcSpendingCondition},
5051
send_request::{
51-
get_referenced_token_ids, make_address_output, make_address_output_token,
52-
make_create_delegation_output, make_data_deposit_output, PoolOrTokenId, SelectedInputs,
53-
StakePoolDataArguments,
52+
make_address_output, make_address_output_token, make_create_delegation_output,
53+
make_data_deposit_output, PoolOrTokenId, SelectedInputs, StakePoolDataArguments,
5454
},
5555
wallet::WalletPoolsFilter,
5656
wallet_events::WalletEvents,

0 commit comments

Comments
 (0)