Skip to content

Commit

Permalink
Add prepare_claim_outputs() (#1622)
Browse files Browse the repository at this point in the history
* Factor out prepare_claim_outputs()

Signed-off-by: Tadeusz „tadzik” Sośnierz <[email protected]>

* Add node bindings for prepareClaimOutputs()

Signed-off-by: Tadeusz „tadzik” Sośnierz <[email protected]>

* Add Python bindings for prepare_claim_outputs()

Signed-off-by: Tadeusz „tadzik” Sośnierz <[email protected]>

* Fix an argument name

Signed-off-by: Tadeusz „tadzik” Sośnierz <[email protected]>

* Add PrepareClaimOutputs to AccountMethod bindings

Signed-off-by: Tadeusz „tadzik” Sośnierz <[email protected]>

* Fixup the nodejs bindings to prepareClaimOutputs()

Signed-off-by: Tadeusz „tadzik” Sośnierz <[email protected]>

* Fixup Python bindings to prepareClaimOutputs()

Signed-off-by: Tadeusz „tadzik” Sośnierz <[email protected]>

* Add Python and Typescript examples for prepareClaimOutputs()

Signed-off-by: Tadeusz „tadzik” Sośnierz <[email protected]>

* Fix the explorer URL in consolidate_outputs() example

Signed-off-by: Tadeusz „tadzik” Sośnierz <[email protected]>

* typo

Signed-off-by: Tadeusz „tadzik” Sośnierz <[email protected]>

* Fix typescript formatting

Signed-off-by: Tadeusz „tadzik” Sośnierz <[email protected]>

* Fix rust formatting

Signed-off-by: Tadeusz „tadzik” Sośnierz <[email protected]>

* Remove new examples, per suggestion

Signed-off-by: Tadeusz „tadzik” Sośnierz <[email protected]>

* sigh

Signed-off-by: Tadeusz „tadzik” Sośnierz <[email protected]>

* Add changelogs for prepare_claim_outputs()

Signed-off-by: Tadeusz „tadzik” Sośnierz <[email protected]>

* Update bindings/nodejs/CHANGELOG.md

Co-authored-by: Thoralf-M <[email protected]>

---------

Signed-off-by: Tadeusz „tadzik” Sośnierz <[email protected]>
Co-authored-by: Thoralf-M <[email protected]>
  • Loading branch information
tadzik and Thoralf-M authored Nov 16, 2023
1 parent b0231e1 commit bd7c08f
Show file tree
Hide file tree
Showing 12 changed files with 100 additions and 52 deletions.
4 changes: 4 additions & 0 deletions bindings/core/src/method/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ pub enum AccountMethod {
burn: BurnDto,
options: Option<TransactionOptionsDto>,
},
/// Claim outputs.
/// Expected response: [`PreparedTransaction`](crate::Response::PreparedTransaction)
#[serde(rename_all = "camelCase")]
PrepareClaimOutputs { output_ids_to_claim: Vec<OutputId> },
/// Consolidate outputs.
/// Expected response: [`PreparedTransaction`](crate::Response::PreparedTransaction)
PrepareConsolidateOutputs { params: ConsolidationParams },
Expand Down
4 changes: 4 additions & 0 deletions bindings/core/src/method_handler/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ pub(crate) async fn call_account_method_internal(account: &Account, method: Acco
.await?;
Response::PreparedTransaction(PreparedTransactionDataDto::from(&data))
}
AccountMethod::PrepareClaimOutputs { output_ids_to_claim } => {
let data = account.prepare_claim_outputs(output_ids_to_claim).await?;
Response::PreparedTransaction(PreparedTransactionDataDto::from(&data))
}
AccountMethod::PrepareConsolidateOutputs { params } => {
let data = account.prepare_consolidate_outputs(params).await?;
Response::PreparedTransaction(PreparedTransactionDataDto::from(&data))
Expand Down
1 change: 1 addition & 0 deletions bindings/core/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ pub enum Response {
OutputsData(Vec<OutputDataDto>),
/// Response for:
/// - [`PrepareBurn`](crate::method::AccountMethod::PrepareBurn),
/// - [`PrepareClaimOutputs`](crate::method::AccountMethod::PrepareClaimOutputs)
/// - [`PrepareConsolidateOutputs`](crate::method::AccountMethod::PrepareConsolidateOutputs)
/// - [`PrepareCreateAliasOutput`](crate::method::AccountMethod::PrepareCreateAliasOutput)
/// - [`PrepareDecreaseVotingPower`](crate::method::AccountMethod::PrepareDecreaseVotingPower)
Expand Down
1 change: 1 addition & 0 deletions bindings/nodejs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- `Utils:verifyTransactionSemantic()`;
- `Account::prepareClaimOutputs()` method;

### Fixed

Expand Down
7 changes: 7 additions & 0 deletions bindings/nodejs/lib/types/wallet/bridge/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ export type __PrepareBurnMethod__ = {
};
};

export type __PrepareClaimOutputsMethod__ = {
name: 'prepareClaimOutputs';
data: {
outputIdsToClaim: OutputId[];
};
};

export type __ClaimOutputsMethod__ = {
name: 'claimOutputs';
data: {
Expand Down
2 changes: 2 additions & 0 deletions bindings/nodejs/lib/types/wallet/bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
__BuildFoundryOutputMethod__,
__BuildNftOutputMethod__,
__PrepareBurnMethod__,
__PrepareClaimOutputsMethod__,
__ClaimOutputsMethod__,
__PrepareConsolidateOutputsMethod__,
__PrepareCreateAliasOutputMethod__,
Expand Down Expand Up @@ -87,6 +88,7 @@ export type __AccountMethod__ =
| __BuildNftOutputMethod__
| __PrepareBurnMethod__
| __ClaimOutputsMethod__
| __PrepareClaimOutputsMethod__
| __PrepareConsolidateOutputsMethod__
| __PrepareCreateAliasOutputMethod__
| __DeregisterParticipationEventMethod__
Expand Down
23 changes: 20 additions & 3 deletions bindings/nodejs/lib/wallet/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,17 +242,34 @@ export class Account {
* @returns The resulting transaction.
*/
async claimOutputs(outputIds: OutputId[]): Promise<Transaction> {
return (await this.prepareClaimOutputs(outputIds)).send();
}

/**
* Claim basic or nft outputs that have additional unlock conditions
* to their `AddressUnlockCondition` from the account.
* @param outputIds The outputs to claim.
* @returns The prepared transaction.
*/
async prepareClaimOutputs(
outputIds: OutputId[],
): Promise<PreparedTransaction> {
const response = await this.methodHandler.callAccountMethod(
this.meta.index,
{
name: 'claimOutputs',
name: 'prepareClaimOutputs',
data: {
outputIdsToClaim: outputIds,
},
},
);
const parsed = JSON.parse(response) as Response<Transaction>;
return plainToInstance(Transaction, parsed.payload);
const parsed = JSON.parse(
response,
) as Response<PreparedTransactionData>;
return new PreparedTransaction(
plainToInstance(PreparedTransactionData, parsed.payload),
this,
);
}

/**
Expand Down
1 change: 1 addition & 0 deletions bindings/python/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

- `Utils:verify_transaction_semantic()`;
- `Account::prepare_claim_outputs()` method;

## 1.1.1 - 2023-10-31

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
block_id = account.retry_transaction_until_included(transaction.transactionId)

print(
f'Transaction included: {os.environ["EXPLORER_ID"]}/block/{block_id}'
f'Transaction included: {os.environ["EXPLORER_URL"]}/block/{block_id}'
)

# Sync account
Expand Down
10 changes: 8 additions & 2 deletions bindings/python/iota_sdk/wallet/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,8 +564,14 @@ def claim_outputs(
self, output_ids_to_claim: List[OutputId]) -> Transaction:
"""Claim outputs.
"""
return Transaction.from_dict(self._call_account_method(
'claimOutputs', {
return self.prepare_claim_outputs(output_ids_to_claim).send()

def prepare_claim_outputs(
self, output_ids_to_claim: List[OutputId]) -> PreparedTransaction:
"""Claim outputs.
"""
return PreparedTransaction(self, self._call_account_method(
'prepareClaimOutputs', {
'outputIdsToClaim': output_ids_to_claim
}
))
Expand Down
6 changes: 6 additions & 0 deletions sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security -->

## Unreleased - 2023-MM-DD

### Added

- `Account::prepare_claim_outputs` method;

## 1.1.2 - 2023-10-26

### Added
Expand Down
91 changes: 45 additions & 46 deletions sdk/src/wallet/account/operations/output_claiming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::{HashMap, HashSet};
use serde::{Deserialize, Serialize};

use crate::{
client::secret::SecretManage,
client::{api::PreparedTransactionData, secret::SecretManage},
types::block::{
address::Address,
output::{
Expand Down Expand Up @@ -186,37 +186,44 @@ where
I::IntoIter: Send,
{
log::debug!("[OUTPUT_CLAIMING] claim_outputs");
let basic_outputs = self.get_basic_outputs_for_additional_inputs().await?;
self.claim_outputs_internal(output_ids_to_claim, basic_outputs)
.await
.map_err(|error| {
// Map InsufficientStorageDepositAmount error here because it's the result of InsufficientFunds in this
// case and then easier to handle
match error {
crate::wallet::Error::Block(block_error) => match *block_error {
crate::types::block::Error::InsufficientStorageDepositAmount { amount, required } => {
crate::wallet::Error::InsufficientFunds {
available: amount,
required,
}
let prepared_transaction = self.prepare_claim_outputs(output_ids_to_claim).await.map_err(|error| {
// Map InsufficientStorageDepositAmount error here because it's the result of InsufficientFunds in this
// case and then easier to handle
match error {
crate::wallet::Error::Block(block_error) => match *block_error {
crate::types::block::Error::InsufficientStorageDepositAmount { amount, required } => {
crate::wallet::Error::InsufficientFunds {
available: amount,
required,
}
_ => crate::wallet::Error::Block(block_error),
},
_ => error,
}
})
}
_ => crate::wallet::Error::Block(block_error),
},
_ => error,
}
})?;

let claim_tx = self.sign_and_submit_transaction(prepared_transaction, None).await?;

log::debug!(
"[OUTPUT_CLAIMING] Claiming transaction created: block_id: {:?} tx_id: {:?}",
claim_tx.block_id,
claim_tx.transaction_id
);
Ok(claim_tx)
}

/// Try to claim basic outputs that have additional unlock conditions to their [AddressUnlockCondition].
pub(crate) async fn claim_outputs_internal<I: IntoIterator<Item = OutputId> + Send>(
pub async fn prepare_claim_outputs<I: IntoIterator<Item = OutputId> + Send>(
&self,
output_ids_to_claim: I,
mut possible_additional_inputs: Vec<OutputData>,
) -> crate::wallet::Result<Transaction>
) -> crate::wallet::Result<PreparedTransactionData>
where
I::IntoIter: Send,
{
log::debug!("[OUTPUT_CLAIMING] claim_outputs_internal");
log::debug!("[OUTPUT_CLAIMING] prepare_claim_outputs");

let mut possible_additional_inputs = self.get_basic_outputs_for_additional_inputs().await?;

let current_time = self.client().get_time_checked().await?;
let rent_structure = self.client().get_rent_structure().await?;
Expand Down Expand Up @@ -406,29 +413,21 @@ where
})?;
}

let claim_tx = self
.finish_transaction(
outputs_to_send,
Some(TransactionOptions {
custom_inputs: Some(
outputs_to_claim
.iter()
.map(|o| o.output_id)
// add additional inputs
.chain(additional_inputs)
.collect::<Vec<OutputId>>(),
),
..Default::default()
}),
)
.await?;

log::debug!(
"[OUTPUT_CLAIMING] Claiming transaction created: block_id: {:?} tx_id: {:?}",
claim_tx.block_id,
claim_tx.transaction_id
);
Ok(claim_tx)
self.prepare_transaction(
outputs_to_send,
Some(TransactionOptions {
custom_inputs: Some(
outputs_to_claim
.iter()
.map(|o| o.output_id)
// add additional inputs
.chain(additional_inputs)
.collect::<Vec<OutputId>>(),
),
..Default::default()
}),
)
.await
}
}

Expand Down

0 comments on commit bd7c08f

Please sign in to comment.