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

Clean up wallet syncing code #2108

Merged
merged 46 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
1badfa3
const fn
Alex6323 Mar 1, 2024
bb673e2
sync options
Alex6323 Mar 1, 2024
9b77ea1
Merge branch '2.0' into cleanup-wallet-syncing
Alex6323 Mar 13, 2024
8134473
move instead of clone
Alex6323 Mar 14, 2024
264b2d9
combinators
Alex6323 Mar 14, 2024
010e276
rm unnecessary cloning 1
Alex6323 Mar 14, 2024
abe5f40
rm unnecessary cloning 2
Alex6323 Mar 14, 2024
c5e38fb
slices; impl Into<Option>
Alex6323 Mar 14, 2024
55b5a2f
refactor
Alex6323 Mar 14, 2024
8591c86
get rid of output_data
Alex6323 Mar 14, 2024
7a158a6
Merge branch '2.0' into cleanup-wallet-syncing
Alex6323 Mar 14, 2024
38afbb8
nits
Alex6323 Mar 14, 2024
1cb8c67
clippy
Alex6323 Mar 14, 2024
52e0afb
nit
Alex6323 Mar 14, 2024
99561d7
nest
Alex6323 Mar 15, 2024
afe5ada
nit
Alex6323 Mar 15, 2024
e7e592f
update python binding
Alex6323 Mar 15, 2024
0cc5e29
update nodejs binding
Alex6323 Mar 15, 2024
2f1c6e9
update core binding
Alex6323 Mar 15, 2024
dea3c64
rm output_data.py
Alex6323 Mar 15, 2024
863c7d1
nit
Alex6323 Mar 15, 2024
d035c55
suggestion
Alex6323 Mar 15, 2024
be7dcbb
rename local var
Alex6323 Mar 15, 2024
8dcb842
fix tests and clippy
Alex6323 Mar 15, 2024
62a9541
ci-doc
Alex6323 Mar 15, 2024
119fad0
rm HashSet piping of foundry output ids
Alex6323 Mar 15, 2024
ca13a6c
rm TODO
Alex6323 Mar 15, 2024
fec6cfd
fix nodejs how-to
Alex6323 Mar 15, 2024
b923a00
Merge branch '2.0' into cleanup-wallet-syncing
Alex6323 Mar 19, 2024
d12be48
Merge branch '2.0' into cleanup-wallet-syncing
Alex6323 Mar 22, 2024
0159dc9
remove todo
Alex6323 Mar 22, 2024
94de389
Merge branch '2.0' into cleanup-wallet-syncing
Mar 22, 2024
48b6661
Merge branch '2.0' into cleanup-wallet-syncing
Alex6323 Mar 27, 2024
e91b2af
undo rename
Alex6323 Apr 15, 2024
ea08631
Merge branch 'upstream/2.0' into cleanup-wallet-syncing
Alex6323 Apr 15, 2024
c3f0ae6
Merge branch '2.0' into cleanup-wallet-syncing
Alex6323 Apr 17, 2024
94419c4
Merge branch 'upstream/2.0' into cleanup-wallet-syncing
Alex6323 Apr 26, 2024
d7f4443
rust: cleanup
Alex6323 Apr 29, 2024
9e0e4e3
python: cleanup
Alex6323 Apr 29, 2024
f69965a
nodejs: cleanup
Alex6323 Apr 29, 2024
346ab5f
Merge branch '2.0' into cleanup-wallet-syncing
Alex6323 Apr 29, 2024
38b7bfc
rust: more cleanup
Alex6323 Apr 29, 2024
5de120c
Merge branch '2.0' into cleanup-wallet-syncing
Alex6323 Apr 30, 2024
a9100c3
...
Alex6323 Apr 30, 2024
b745c52
....
Alex6323 Apr 30, 2024
6412d7c
.....
Alex6323 Apr 30, 2024
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
12 changes: 6 additions & 6 deletions bindings/core/src/method/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use crate::OmittedDebug;
#[non_exhaustive]
pub enum WalletMethod {
/// Returns the accounts of the wallet.
/// Expected response: [`OutputsData`](crate::Response::OutputsData)
/// Expected response: [`OutputsWithExtendedMetadata`](crate::Response::OutputsWithExtendedMetadata)
Accounts,
/// Backup storage. Password must be the current one, when Stronghold is used as SecretManager.
/// Expected response: [`Ok`](crate::Response::Ok)
Expand Down Expand Up @@ -139,8 +139,8 @@ pub enum WalletMethod {
/// Expected response: [`Transaction`](crate::Response::Transaction)
#[serde(rename_all = "camelCase")]
GetIncomingTransaction { transaction_id: TransactionId },
/// Get the [`OutputData`](iota_sdk::wallet::types::OutputData) of an output stored in the wallet.
/// Expected response: [`OutputData`](crate::Response::OutputData)
/// Get the [`OutputWithExtendedMetadata`](iota_sdk::wallet::types::OutputWithExtendedMetadata) of an output stored
/// in the wallet. Expected response: [`OutputWithExtendedMetadata`](crate::Response::OutputWithExtendedMetadata)
#[serde(rename_all = "camelCase")]
GetOutput { output_id: OutputId },
// /// Expected response: [`ParticipationEvent`](crate::Response::ParticipationEvent)
Expand Down Expand Up @@ -199,13 +199,13 @@ pub enum WalletMethod {
bip_path: Option<Bip44>,
},
/// Returns the implicit accounts of the wallet.
/// Expected response: [`OutputsData`](crate::Response::OutputsData)
/// Expected response: [`OutputsWithExtendedMetadata`](crate::Response::OutputsWithExtendedMetadata)
ImplicitAccounts,
/// Returns all incoming transactions of the wallet.
/// Expected response: [`Transactions`](crate::Response::Transactions)
IncomingTransactions,
/// Returns all outputs of the wallet.
/// Expected response: [`OutputsData`](crate::Response::OutputsData)
/// Expected response: [`OutputsWithExtendedMetadata`](crate::Response::OutputsWithExtendedMetadata)
#[serde(rename_all = "camelCase")]
Outputs { filter_options: Option<FilterOptions> },
/// Returns all pending transactions of the wallet.
Expand Down Expand Up @@ -452,7 +452,7 @@ pub enum WalletMethod {
/// Expected response: [`Transactions`](crate::Response::Transactions)
Transactions,
/// Returns all unspent outputs of the wallet
/// Expected response: [`OutputsData`](crate::Response::OutputsData)
/// Expected response: [`OutputsWithExtendedMetadata`](crate::Response::OutputsWithExtendedMetadata)
#[serde(rename_all = "camelCase")]
UnspentOutputs { filter_options: Option<FilterOptions> },
/// Emits an event for testing if the event system is working
Expand Down
12 changes: 7 additions & 5 deletions bindings/core/src/method_handler/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ pub(crate) async fn call_wallet_method_internal(
method: WalletMethod,
) -> Result<Response, crate::Error> {
let response = match method {
WalletMethod::Accounts => Response::OutputsData(wallet.ledger().await.accounts().cloned().collect()),
WalletMethod::Accounts => {
Response::OutputsWithExtendedMetadata(wallet.ledger().await.accounts().cloned().collect())
}
#[cfg(feature = "stronghold")]
WalletMethod::BackupToStrongholdSnapshot { destination, password } => {
wallet.backup_to_stronghold_snapshot(destination, password).await?;
Expand Down Expand Up @@ -138,7 +140,7 @@ pub(crate) async fn call_wallet_method_internal(
|transaction| Response::Transaction(Some(Box::new(TransactionWithMetadataDto::from(transaction)))),
),
WalletMethod::GetOutput { output_id } => {
Response::OutputData(wallet.ledger().await.get_output(&output_id).cloned().map(Box::new))
Response::OutputWithExtendedMetadata(wallet.ledger().await.get_output(&output_id).cloned().map(Box::new))
}
// #[cfg(feature = "participation")]
// WalletMethod::GetParticipationEvent { event_id } => {
Expand Down Expand Up @@ -203,7 +205,7 @@ pub(crate) async fn call_wallet_method_internal(
Response::PreparedTransaction(data)
}
WalletMethod::ImplicitAccounts => {
Response::OutputsData(wallet.ledger().await.implicit_accounts().cloned().collect())
Response::OutputsWithExtendedMetadata(wallet.ledger().await.implicit_accounts().cloned().collect())
}
WalletMethod::IncomingTransactions => Response::Transactions(
wallet
Expand All @@ -216,7 +218,7 @@ pub(crate) async fn call_wallet_method_internal(
),
WalletMethod::Outputs { filter_options } => {
let wallet_ledger = wallet.ledger().await;
Response::OutputsData(if let Some(filter) = filter_options {
Response::OutputsWithExtendedMetadata(if let Some(filter) = filter_options {
wallet_ledger.filtered_outputs(filter).cloned().collect()
} else {
wallet_ledger.outputs().values().cloned().collect()
Expand Down Expand Up @@ -420,7 +422,7 @@ pub(crate) async fn call_wallet_method_internal(
),
WalletMethod::UnspentOutputs { filter_options } => {
let wallet_ledger = wallet.ledger().await;
Response::OutputsData(if let Some(filter) = filter_options {
Response::OutputsWithExtendedMetadata(if let Some(filter) = filter_options {
wallet_ledger.filtered_unspent_outputs(filter).cloned().collect()
} else {
wallet_ledger.unspent_outputs().values().cloned().collect()
Expand Down
6 changes: 3 additions & 3 deletions bindings/core/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use iota_sdk::{
},
utils::serde::string,
wallet::{
types::{Balance, OutputData, TransactionWithMetadataDto},
types::{Balance, OutputWithExtendedMetadata, TransactionWithMetadataDto},
PreparedCreateDelegationTransaction, PreparedCreateNativeTokenTransaction,
},
};
Expand Down Expand Up @@ -302,11 +302,11 @@ pub enum Response {
OutputIds(Vec<OutputId>),
/// Response for:
/// - [`GetOutput`](crate::method::WalletMethod::GetOutput)
OutputData(Option<Box<OutputData>>),
OutputWithExtendedMetadata(Option<Box<OutputWithExtendedMetadata>>),
/// Response for:
/// - [`Outputs`](crate::method::WalletMethod::Outputs),
/// - [`UnspentOutputs`](crate::method::WalletMethod::UnspentOutputs)
OutputsData(Vec<OutputData>),
OutputsWithExtendedMetadata(Vec<OutputWithExtendedMetadata>),
/// Response for:
/// - [`PrepareBurn`](crate::method::WalletMethod::PrepareBurn),
/// - [`PrepareClaimOutputs`](crate::method::WalletMethod::PrepareClaimOutputs)
Expand Down
2 changes: 1 addition & 1 deletion bindings/core/tests/combined.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async fn create_wallet() -> Result<(), Error> {
.await;

match response {
Response::OutputsData(_) => {}
Response::OutputsWithExtendedMetadata(_) => {}
_ => panic!("unexpected response {response:?}"),
}

Expand Down
12 changes: 5 additions & 7 deletions bindings/nodejs/examples/how_tos/wallet/consolidate-outputs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import { CommonOutput, Utils, Wallet, initLogger } from '@iota/sdk';
import { CommonOutput, Wallet, initLogger } from '@iota/sdk';

// This example uses secrets in environment variables for simplicity which should not be done in production.
require('dotenv').config({ path: '.env' });
Expand Down Expand Up @@ -46,11 +46,10 @@ async function run() {
const outputs = await wallet.unspentOutputs();
console.log('Outputs BEFORE consolidation:');

outputs.forEach(({ output, address }, i) => {
outputs.forEach(({ output }, i) => {
console.log(`OUTPUT #${i}`);
console.log(
'- address: %s\n- amount: %d\n- native token: %s',
Utils.addressToBech32(address, 'rms'),
'- amount: %d\n- native token: %s',
output.getAmount(),
output instanceof CommonOutput
? (output as CommonOutput).getNativeToken() ?? []
Expand Down Expand Up @@ -80,11 +79,10 @@ async function run() {

// Outputs after consolidation
console.log('Outputs AFTER consolidation:');
outputs.forEach(({ output, address }, i) => {
outputs.forEach(({ output }, i) => {
console.log(`OUTPUT #${i}`);
console.log(
'- address: %s\n- amount: %d\n- native tokens: %s',
Utils.addressToBech32(address, 'rms'),
'- amount: %d\n- native tokens: %s',
output.getAmount(),
output instanceof CommonOutput
? (output as CommonOutput).getNativeToken()
Expand Down
10 changes: 5 additions & 5 deletions bindings/nodejs/lib/types/wallet/event.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

import type { OutputData } from './output';
import type { OutputWithExtendedMetadata } from './output';
import { InclusionState } from './transaction';
import { InputSigningData, Remainder } from '../client';
import { Transaction, SignedTransactionPayload, TransactionId } from '../block';
Expand Down Expand Up @@ -57,7 +57,7 @@ class LedgerAddressGenerationWalletEvent extends WalletEvent {
* A 'new output' wallet event.
*/
class NewOutputWalletEvent extends WalletEvent {
output: OutputData;
output: OutputWithExtendedMetadata;
transaction?: SignedTransactionPayload;
transactionInputs?: OutputResponse[];

Expand All @@ -67,7 +67,7 @@ class NewOutputWalletEvent extends WalletEvent {
* @param transactionInputs The inputs for the transaction that created the output. Might be pruned and not available.
*/
constructor(
output: OutputData,
output: OutputWithExtendedMetadata,
transaction?: SignedTransactionPayload,
transactionInputs?: OutputResponse[],
) {
Expand All @@ -82,12 +82,12 @@ class NewOutputWalletEvent extends WalletEvent {
* A 'spent output' wallet event.
*/
class SpentOutputWalletEvent extends WalletEvent {
output: OutputData;
output: OutputWithExtendedMetadata;

/**
* @param output The spent output.
*/
constructor(output: OutputData) {
constructor(output: OutputWithExtendedMetadata) {
super(WalletEventType.SpentOutput);
this.output = output;
}
Expand Down
30 changes: 12 additions & 18 deletions bindings/nodejs/lib/types/wallet/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
// SPDX-License-Identifier: Apache-2.0

import { Type } from 'class-transformer';
import { Address, AddressDiscriminator } from '../block/address';
import { Output, OutputDiscriminator, OutputId } from '../block/output';
import { OutputMetadataResponse } from '../models/api';
import { OutputIdProof, OutputMetadataResponse } from '../models/api';

/** Output to claim */
export enum OutputsToClaim {
Expand All @@ -15,28 +14,23 @@ export enum OutputsToClaim {
All = 'All',
}

/** An output with metadata */
export class OutputData {
/** The identifier of an Output */
outputId!: OutputId;
/** The metadata of the output */
metadata!: OutputMetadataResponse;
/** The actual Output */
/** An output with extended metadata */
export class OutputWithExtendedMetadata {
Thoralf-M marked this conversation as resolved.
Show resolved Hide resolved
/** The output itself */
@Type(() => Output, {
discriminator: OutputDiscriminator,
})
output!: Output;
/** Associated account address */
@Type(() => Address, {
discriminator: AddressDiscriminator,
})
address!: Address;
/** Network ID */
/** The metadata of the output */
metadata!: OutputMetadataResponse;
/** The output ID proof */
OutputIdProof!: OutputIdProof;
/** The corresponding output ID */
outputId!: OutputId;
/** The network ID the output belongs to */
networkId!: string;
/** Remainder */
/** Whether the output represents a remainder amount */
remainder!: boolean;
/** BIP32 path */
chain?: Segment[];
}

/** A Segment of the BIP32 path*/
Expand Down
48 changes: 31 additions & 17 deletions bindings/nodejs/lib/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
FilterOptions,
CreateNativeTokenParams,
MintNftParams,
OutputData,
OutputWithExtendedMetadata,
OutputParams,
OutputsToClaim,
TransactionWithMetadata,
Expand Down Expand Up @@ -303,13 +303,15 @@ export class Wallet {
*
* @returns The accounts of the wallet.
*/
async accounts(): Promise<OutputData[]> {
async accounts(): Promise<OutputWithExtendedMetadata[]> {
const response = await this.methodHandler.callMethod({
name: 'accounts',
});

const parsed = JSON.parse(response) as Response<OutputData[]>;
return plainToInstance(OutputData, parsed.payload);
const parsed = JSON.parse(response) as Response<
OutputWithExtendedMetadata[]
>;
return plainToInstance(OutputWithExtendedMetadata, parsed.payload);
}

/**
Expand Down Expand Up @@ -701,17 +703,19 @@ export class Wallet {
/**
* Get the data for an output.
* @param outputId The output to get.
* @returns The `OutputData`.
* @returns The `OutputWithExtendedMetadata`.
*/
async getOutput(outputId: OutputId): Promise<OutputData> {
async getOutput(outputId: OutputId): Promise<OutputWithExtendedMetadata> {
const response = await this.methodHandler.callMethod({
name: 'getOutput',
data: {
outputId,
},
});
const parsed = JSON.parse(response) as Response<OutputData>;
return plainToInstance(OutputData, parsed.payload);
const parsed = JSON.parse(
response,
) as Response<OutputWithExtendedMetadata>;
return plainToInstance(OutputWithExtendedMetadata, parsed.payload);
}

/**
Expand Down Expand Up @@ -873,14 +877,18 @@ export class Wallet {
* @param filterOptions Options to filter the to be returned outputs.
* @returns The outputs with metadata.
*/
async outputs(filterOptions?: FilterOptions): Promise<OutputData[]> {
async outputs(
filterOptions?: FilterOptions,
): Promise<OutputWithExtendedMetadata[]> {
const response = await this.methodHandler.callMethod({
name: 'outputs',
data: { filterOptions },
});

const parsed = JSON.parse(response) as Response<OutputData[]>;
return plainToInstance(OutputData, parsed.payload);
const parsed = JSON.parse(response) as Response<
OutputWithExtendedMetadata[]
>;
return plainToInstance(OutputWithExtendedMetadata, parsed.payload);
}

/**
Expand Down Expand Up @@ -951,13 +959,15 @@ export class Wallet {
*
* @returns The implicit accounts of the wallet.
*/
async implicitAccounts(): Promise<OutputData[]> {
async implicitAccounts(): Promise<OutputWithExtendedMetadata[]> {
const response = await this.methodHandler.callMethod({
name: 'implicitAccounts',
});

const parsed = JSON.parse(response) as Response<OutputData[]>;
return plainToInstance(OutputData, parsed.payload);
const parsed = JSON.parse(response) as Response<
OutputWithExtendedMetadata[]
>;
return plainToInstance(OutputWithExtendedMetadata, parsed.payload);
}

/**
Expand Down Expand Up @@ -996,13 +1006,17 @@ export class Wallet {
* @param filterOptions Options to filter the to be returned outputs.
* @returns The outputs with metadata.
*/
async unspentOutputs(filterOptions?: FilterOptions): Promise<OutputData[]> {
async unspentOutputs(
filterOptions?: FilterOptions,
): Promise<OutputWithExtendedMetadata[]> {
const response = await this.methodHandler.callMethod({
name: 'unspentOutputs',
data: { filterOptions },
});
const parsed = JSON.parse(response) as Response<OutputData[]>;
return plainToInstance(OutputData, parsed.payload);
const parsed = JSON.parse(response) as Response<
OutputWithExtendedMetadata[]
>;
return plainToInstance(OutputWithExtendedMetadata, parsed.payload);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion bindings/python/iota_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@
from .types.native_token import *
from .types.node_info import *
from .types.output import *
from .types.output_data import *
from .types.output_id import *
from .types.output_id_proof import *
from .types.output_metadata import *
from .types.output_params import *
from .types.output_with_extended_metadata import *
from .types.payload import *
from .types.send_params import *
from .types.slot import *
Expand Down
Loading
Loading