Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace first_account_id by first_block_issuer_account_id #2184

Merged
merged 2 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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?;
DaughterOfMars marked this conversation as resolved.
Show resolved Hide resolved
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
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
Loading