Skip to content

Commit

Permalink
Replace first_account_id by first_block_issuer_account_id as we are o…
Browse files Browse the repository at this point in the history
…nly interested in those;

Derive Eq because of clippy
  • Loading branch information
Thoralf-M committed Mar 15, 2024
1 parent 05cf681 commit 800b397
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 23 deletions.
22 changes: 12 additions & 10 deletions cli/src/wallet_cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,11 +443,12 @@ pub async fn address_command(wallet: &Wallet) -> Result<(), Error> {

// `allot-mana` command
pub async fn allot_mana_command(wallet: &Wallet, mana: u64, account_id: Option<AccountId>) -> Result<(), Error> {
let account_id = {
let wallet_ledger = wallet.ledger().await;
account_id
.or_else(|| wallet_ledger.first_account_id())
.ok_or(WalletError::AccountNotFound)?
let account_id = match account_id {
Some(account_id) => account_id,
None => wallet
.first_block_issuer_account_id()
.await?
.ok_or(WalletError::AccountNotFound)?,
};

let transaction = wallet.allot_mana([ManaAllotment::new(account_id, mana)?], None).await?;
Expand Down Expand Up @@ -626,11 +627,12 @@ pub async fn congestion_command(
account_id: Option<AccountId>,
work_score: Option<u32>,
) -> Result<(), Error> {
let account_id = {
let wallet_ledger = wallet.ledger().await;
account_id
.or_else(|| wallet_ledger.first_account_id())
.ok_or(WalletError::AccountNotFound)?
let account_id = match account_id {
Some(account_id) => account_id,
None => wallet
.first_block_issuer_account_id()
.await?
.ok_or(WalletError::AccountNotFound)?,
};

let congestion = wallet.client().get_account_congestion(&account_id, work_score).await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/client/node_api/core/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::{
pub(crate) static INFO_PATH: &str = "api/core/v3/info";

/// Contains the info and the url from the node (useful when multiple nodes are used)
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NodeInfoResponse {
/// The returned info
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/api/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct RoutesResponse {

/// Response of GET /api/core/v3/info.
/// General information about the node.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InfoResponse {
pub name: String,
Expand Down
44 changes: 39 additions & 5 deletions sdk/src/wallet/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ use crate::wallet::storage::{StorageManager, StorageOptions};
use crate::{
client::{
secret::{SecretManage, SecretManager},
verify_mnemonic, Client,
verify_mnemonic, Client, ClientError,
},
types::{
block::{
address::{Address, Bech32Address, Hrp, ImplicitAccountCreationAddress},
output::{AccountId, AnchorId, DelegationId, FoundryId, FoundryOutput, NftId, Output, OutputId, TokenId},
payload::signed_transaction::TransactionId,
protocol::ProtocolParameters,
slot::SlotIndex,
},
TryFromDto,
},
Expand Down Expand Up @@ -297,11 +298,19 @@ impl WalletLedger {
.filter(|output_data| output_data.output.is_account())
}

// Returns the first possible Account id, which can be an implicit account.
pub fn first_account_id(&self) -> Option<AccountId> {
// Returns the first possible unexpired block issuer Account id, which can be an implicit account.
pub fn first_block_issuer_account_id(&self, current_slot: SlotIndex) -> Option<AccountId> {
self.accounts()
.next()
.map(|o| o.output.as_account().account_id_non_null(&o.output_id))
.find_map(|o| {
let account = o.output.as_account();
account.features().block_issuer().and_then(|block_issuer| {
if block_issuer.expiry_slot() > current_slot {
Some(account.account_id_non_null(&o.output_id))
} else {
None
}
})
})
.or_else(|| self.implicit_accounts().next().map(|o| AccountId::from(&o.output_id)))
}

Expand Down Expand Up @@ -340,6 +349,31 @@ impl WalletLedger {
}

impl<S: 'static + SecretManage> Wallet<S> {
// Returns the first possible unexpired block issuer Account id, which can be an implicit account.
pub async fn first_block_issuer_account_id(&self) -> Result<Option<AccountId>, ClientError> {
let current_slot = self.client().get_slot_index().await?;
let wallet_ledger = self.ledger().await;
let account_id = wallet_ledger
.accounts()
.find_map(|o| {
let account = o.output.as_account();
account.features().block_issuer().and_then(|block_issuer| {
if block_issuer.expiry_slot() > current_slot {
Some(account.account_id_non_null(&o.output_id))
} else {
None
}
})
})
.or_else(|| {
wallet_ledger
.implicit_accounts()
.next()
.map(|o| AccountId::from(&o.output_id))
});
Ok(account_id)
}

/// Get the [`Output`] that minted a native token by the token ID. First try to get it
/// from the wallet, if it isn't in the wallet try to get it from the node
pub async fn get_foundry_output(&self, native_token_id: TokenId) -> Result<Output, WalletError> {
Expand Down
12 changes: 7 additions & 5 deletions sdk/src/wallet/operations/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@ where
// If an issuer ID is provided, use it; otherwise, use the first available account or implicit account.
let issuer_id = match issuer_id.into() {
Some(id) => id,
None => self
.ledger()
.await
.first_account_id()
.ok_or(WalletError::AccountNotFound)?,
None => {
let current_slot = self.client().get_slot_index().await?;
self.ledger()
.await
.first_block_issuer_account_id(current_slot)
.ok_or(WalletError::AccountNotFound)?
}
};

let unsigned_block = self.client().build_basic_block(issuer_id, payload).await?;
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/wallet/operations/transaction/build_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<S: 'static + SecretManage> Wallet<S> {

let slot_commitment_id = self.client().get_issuance().await?.latest_commitment.id();
if options.issuer_id.is_none() {
options.issuer_id = self.ledger().await.first_account_id();
options.issuer_id = self.first_block_issuer_account_id().await?;
}

#[cfg(feature = "events")]
Expand Down

0 comments on commit 800b397

Please sign in to comment.