Skip to content

Commit

Permalink
Enhance coin retrieval logic in Iota client (#1472)
Browse files Browse the repository at this point in the history
* Enhance coin retrieval logic in Iota client

- Introduced a constant `MINIMUM_BALANCE` to define the minimum balance required for transactions.
- Updated `get_coin_for_transaction` method to iterate through paginated coin data, selecting the coin with the highest balance that meets the minimum requirement.
- Improved error handling to provide clearer messages when no suitable coins are found.

This change optimizes the process of selecting coins for transactions, ensuring that only coins with sufficient balance are considered.

* chore: remove unecessary comments

* chore: simplify the coin logic

* chore: review comments

* Update identity_iota_core/src/rebased/client/full_client.rs

Co-authored-by: Enrico Marconi <31142849+UMR1352@users.noreply.github.com>

* Update minimum balance requirement in Iota client

---------

Co-authored-by: Enrico Marconi <31142849+UMR1352@users.noreply.github.com>
  • Loading branch information
itsyaasir and UMR1352 authored Dec 4, 2024
1 parent a30610c commit 4912994
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions identity_iota_core/src/rebased/client/full_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use async_trait::async_trait;
use fastcrypto::ed25519::Ed25519PublicKey;
use fastcrypto::traits::ToFromBytes;
use identity_verification::jwk::Jwk;
use iota_sdk::rpc_types::Coin;
use iota_sdk::rpc_types::IotaExecutionStatus;
use iota_sdk::rpc_types::IotaObjectData;
use iota_sdk::rpc_types::IotaObjectDataFilter;
Expand Down Expand Up @@ -47,6 +48,9 @@ use crate::rebased::Error;
use super::get_object_id_from_did;
use super::IdentityClientReadOnly;

/// The minimum balance required to execute a transaction.
pub(crate) const MINIMUM_BALANCE: u64 = 1_000_000_000;

/// A signature which is used to sign transactions.
pub struct IotaKeySignature {
/// The public key of the signature.
Expand Down Expand Up @@ -253,18 +257,39 @@ impl<S> IdentityClient<S> {
Ok(budget)
}

async fn get_coin_for_transaction(&self) -> Result<iota_sdk::rpc_types::Coin, Error> {
let coins = self
.coin_read_api()
.get_coins(self.sender_address(), None, None, None)
.await
.map_err(|err| Error::GasIssue(format!("could not get coins; {err}")))?;
async fn get_coin_for_transaction(&self) -> Result<Coin, Error> {
const LIMIT: usize = 10;
let mut cursor = None;

loop {
let coins = self
.coin_read_api()
.get_coins(self.sender_address(), None, cursor, Some(LIMIT))
.await?;

let Some(coin) = coins.data.into_iter().max_by_key(|coin| coin.balance) else {
return Err(Error::GasIssue(format!(
"no coins found for address {}",
self.sender_address()
)));
};

if coin.balance >= MINIMUM_BALANCE {
return Ok(coin);
}

if !coins.has_next_page {
break;
}

cursor = coins.next_cursor;
}

coins
.data
.into_iter()
.next()
.ok_or_else(|| Error::GasIssue("could not find coins".to_string()))
Err(Error::GasIssue(format!(
"no coin found with minimum required balance of {} for address {}",
MINIMUM_BALANCE,
self.sender_address()
)))
}

async fn get_transaction_data(
Expand Down

0 comments on commit 4912994

Please sign in to comment.