From 4aac8377acd35ed018784546227c2001a497e7a9 Mon Sep 17 00:00:00 2001 From: Thoralf-M <46689931+Thoralf-M@users.noreply.github.com> Date: Wed, 6 Mar 2024 18:01:55 +0100 Subject: [PATCH] Cleanup bindings send methods (#2137) * Cleanup bindings send methods * format * Keep prepare_send * Revert comment change --------- Co-authored-by: DaughterOfMars --- bindings/core/src/method/wallet.rs | 29 +------------- bindings/core/src/method_handler/wallet.rs | 20 ---------- bindings/core/src/response.rs | 9 ++--- .../nodejs/lib/types/wallet/bridge/index.ts | 8 +--- .../nodejs/lib/types/wallet/bridge/wallet.ts | 21 +--------- bindings/nodejs/lib/wallet/wallet.ts | 40 ++++--------------- bindings/python/iota_sdk/wallet/wallet.py | 33 +++++---------- 7 files changed, 27 insertions(+), 133 deletions(-) diff --git a/bindings/core/src/method/wallet.rs b/bindings/core/src/method/wallet.rs index 2bf51e0d14..70e9997662 100644 --- a/bindings/core/src/method/wallet.rs +++ b/bindings/core/src/method/wallet.rs @@ -119,10 +119,6 @@ pub enum WalletMethod { /// Expected response: [`OutputIds`](crate::Response::OutputIds) #[serde(rename_all = "camelCase")] ClaimableOutputs { outputs_to_claim: OutputsToClaim }, - /// Claim outputs. - /// Expected response: [`SentTransaction`](crate::Response::SentTransaction) - #[serde(rename_all = "camelCase")] - ClaimOutputs { output_ids_to_claim: Vec }, // /// Removes a previously registered participation event from local storage. // /// Expected response: [`Ok`](crate::Response::Ok) // #[cfg(feature = "participation")] @@ -308,7 +304,7 @@ pub enum WalletMethod { #[serde(default)] transaction_options: Option, }, - /// Prepare to send base coins. + /// Prepare to send base coins to multiple addresses, or with additional parameters. /// Expected response: [`PreparedTransaction`](crate::Response::PreparedTransaction) PrepareSend { params: Vec, @@ -421,29 +417,6 @@ pub enum WalletMethod { /// Maximum attempts max_attempts: Option, }, - /// Send base coins. - /// Expected response: [`SentTransaction`](crate::Response::SentTransaction) - Send { - #[serde(with = "string")] - amount: u64, - address: Bech32Address, - #[serde(default)] - options: Option, - }, - /// Send base coins to multiple addresses, or with additional parameters. - /// Expected response: [`SentTransaction`](crate::Response::SentTransaction) - SendWithParams { - params: Vec, - #[serde(default)] - options: Option, - }, - /// Send outputs in a transaction. - /// Expected response: [`SentTransaction`](crate::Response::SentTransaction) - SendOutputs { - outputs: Vec, - #[serde(default)] - options: Option, - }, /// Set the alias of the wallet. /// Expected response: [`Ok`](crate::Response::Ok) SetAlias { alias: String }, diff --git a/bindings/core/src/method_handler/wallet.rs b/bindings/core/src/method_handler/wallet.rs index f7f1f5fb21..a4fbb1f626 100644 --- a/bindings/core/src/method_handler/wallet.rs +++ b/bindings/core/src/method_handler/wallet.rs @@ -138,10 +138,6 @@ pub(crate) async fn call_wallet_method_internal( let output_ids = wallet.claimable_outputs(outputs_to_claim).await?; Response::OutputIds(output_ids) } - WalletMethod::ClaimOutputs { output_ids_to_claim } => { - let transaction = wallet.claim_outputs(output_ids_to_claim.to_vec()).await?; - Response::SentTransaction(TransactionWithMetadataDto::from(&transaction)) - } // #[cfg(feature = "participation")] // WalletMethod::DeregisterParticipationEvent { event_id } => { // wallet.deregister_participation_event(&event_id).await?; @@ -390,22 +386,6 @@ pub(crate) async fn call_wallet_method_internal( .await?; Response::Ok } - WalletMethod::Send { - amount, - address, - options, - } => { - let transaction = wallet.send(amount, address, options).await?; - Response::SentTransaction(TransactionWithMetadataDto::from(&transaction)) - } - WalletMethod::SendWithParams { params, options } => { - let transaction = wallet.send_with_params(params, options).await?; - Response::SentTransaction(TransactionWithMetadataDto::from(&transaction)) - } - WalletMethod::SendOutputs { outputs, options } => { - let transaction = wallet.send_outputs(outputs, options).await?; - Response::SentTransaction(TransactionWithMetadataDto::from(&transaction)) - } WalletMethod::SetAlias { alias } => { wallet.set_alias(&alias).await?; Response::Ok diff --git a/bindings/core/src/response.rs b/bindings/core/src/response.rs index 8a3f060e0e..2b7a8f889c 100644 --- a/bindings/core/src/response.rs +++ b/bindings/core/src/response.rs @@ -271,12 +271,12 @@ pub enum Response { /// - [`VerifySecp256k1EcdsaSignature`](crate::method::UtilsMethod::VerifySecp256k1EcdsaSignature) Bool(bool), /// Response for: - /// - [`Backup`](crate::method::WalletMethod::Backup), + /// - [`BackupToStrongholdSnapshot`](crate::method::WalletMethod::BackupToStrongholdSnapshot), /// - [`ClearListeners`](crate::method::WalletMethod::ClearListeners) /// - [`ClearStrongholdPassword`](crate::method::WalletMethod::ClearStrongholdPassword), /// - [`DeregisterParticipationEvent`](crate::method::WalletMethod::DeregisterParticipationEvent), /// - [`EmitTestEvent`](crate::method::WalletMethod::EmitTestEvent), - /// - [`RestoreBackup`](crate::method::WalletMethod::RestoreBackup), + /// - [`RestoreFromStrongholdSnapshot`](crate::method::WalletMethod::RestoreFromStrongholdSnapshot), /// - [`SetAlias`](crate::method::WalletMethod::SetAlias), /// - [`SetClientOptions`](crate::method::WalletMethod::SetClientOptions), /// - [`SetDefaultSyncOptions`](crate::method::WalletMethod::SetDefaultSyncOptions), @@ -326,10 +326,10 @@ pub enum Response { /// - [`PrepareMeltNativeToken`](crate::method::WalletMethod::PrepareMeltNativeToken) /// - [`PrepareMintNativeToken`](crate::method::WalletMethod::PrepareMintNativeToken), /// - [`PrepareMintNfts`](crate::method::WalletMethod::PrepareMintNfts), - /// - [`PrepareSend`](crate::method::WalletMethod::PrepareSend), /// - [`PrepareSendMana`](crate::method::WalletMethod::PrepareSendMana), /// - [`PrepareSendNativeTokens`](crate::method::WalletMethod::PrepareSendNativeTokens), /// - [`PrepareSendNft`](crate::method::WalletMethod::PrepareSendNft), + /// - [`PrepareSend`](crate::method::WalletMethod::PrepareSend), /// - [`PrepareStopParticipating`](crate::method::WalletMethod::PrepareStopParticipating) /// - [`PrepareSendOutputs`](crate::method::WalletMethod::PrepareSendOutputs) /// - [`PrepareVote`](crate::method::WalletMethod::PrepareVote) @@ -358,9 +358,6 @@ pub enum Response { /// - [`Sync`](crate::method::WalletMethod::Sync) Balance(Balance), /// Response for: - /// - [`ClaimOutputs`](crate::method::WalletMethod::ClaimOutputs) - /// - [`Send`](crate::method::WalletMethod::Send) - /// - [`SendOutputs`](crate::method::WalletMethod::SendOutputs) /// - [`SignAndSubmitTransaction`](crate::method::WalletMethod::SignAndSubmitTransaction) /// - [`SubmitAndStoreTransaction`](crate::method::WalletMethod::SubmitAndStoreTransaction) SentTransaction(TransactionWithMetadataDto), diff --git a/bindings/nodejs/lib/types/wallet/bridge/index.ts b/bindings/nodejs/lib/types/wallet/bridge/index.ts index 00e935d384..96f0c82d2f 100644 --- a/bindings/nodejs/lib/types/wallet/bridge/index.ts +++ b/bindings/nodejs/lib/types/wallet/bridge/index.ts @@ -24,8 +24,8 @@ import type { __PrepareMintNativeTokenMethod__, __PrepareMintNftsMethod__, __PrepareOutputMethod__, - __PrepareSendMethod__, __PrepareSendManaMethod__, + __PrepareSendMethod__, __PrepareCreateDelegationMethod__, __PrepareDelayDelegationClaimingMethod__, __PrepareBeginStakingMethod__, @@ -34,8 +34,6 @@ import type { __PrepareSendOutputsMethod__, __RegisterParticipationEventsMethod__, __WaitForTransactionAcceptanceMethod__, - __SendMethod__, - __SendWithParamsMethod__, __PrepareSendNativeTokensMethod__, __PrepareSendNftMethod__, __SendOutputsMethod__, @@ -105,8 +103,8 @@ export type __WalletMethod__ = | __PrepareMintNativeTokenMethod__ | __PrepareMintNftsMethod__ | __PrepareOutputMethod__ - | __PrepareSendMethod__ | __PrepareSendManaMethod__ + | __PrepareSendMethod__ | __PrepareCreateDelegationMethod__ | __PrepareDelayDelegationClaimingMethod__ | __PrepareBeginStakingMethod__ @@ -115,8 +113,6 @@ export type __WalletMethod__ = | __PrepareSendOutputsMethod__ | __RegisterParticipationEventsMethod__ | __WaitForTransactionAcceptanceMethod__ - | __SendMethod__ - | __SendWithParamsMethod__ | __PrepareSendNativeTokensMethod__ | __PrepareSendNftMethod__ | __SendOutputsMethod__ diff --git a/bindings/nodejs/lib/types/wallet/bridge/wallet.ts b/bindings/nodejs/lib/types/wallet/bridge/wallet.ts index baff7e5311..d6621c47b2 100644 --- a/bindings/nodejs/lib/types/wallet/bridge/wallet.ts +++ b/bindings/nodejs/lib/types/wallet/bridge/wallet.ts @@ -343,14 +343,6 @@ export type __PrepareOutputMethod__ = { }; }; -export type __PrepareSendMethod__ = { - name: 'prepareSend'; - data: { - params: SendParams[]; - options?: TransactionOptions; - }; -}; - export type __PrepareSendOutputsMethod__ = { name: 'prepareSendOutputs'; data: { @@ -375,17 +367,8 @@ export type __WaitForTransactionAcceptanceMethod__ = { }; }; -export type __SendMethod__ = { - name: 'send'; - data: { - amount: NumericString; - address: string; - options?: TransactionOptions; - }; -}; - -export type __SendWithParamsMethod__ = { - name: 'sendWithParams'; +export type __PrepareSendMethod__ = { + name: 'prepareSend'; data: { params: SendParams[]; options?: TransactionOptions; diff --git a/bindings/nodejs/lib/wallet/wallet.ts b/bindings/nodejs/lib/wallet/wallet.ts index e1d83e5ae9..8f5f33e3dd 100644 --- a/bindings/nodejs/lib/wallet/wallet.ts +++ b/bindings/nodejs/lib/wallet/wallet.ts @@ -1463,11 +1463,11 @@ export class Wallet { } /** - * Send outputs. + * Send outputs in a transaction. * - * @param outputs Outputs to use in the transaction. - * @param options Additional transaction options. - * @returns The transaction data. + * @param outputs The outputs to send. + * @param transactionOptions Additional transaction options. + * @returns The sent transaction. */ async sendOutputs( outputs: Output[], @@ -1555,18 +1555,9 @@ export class Wallet { if (typeof amount === 'bigint') { amount = amount.toString(10); } - const response = await this.methodHandler.callMethod({ - name: 'send', - data: { - amount, - address, - options: transactionOptions, - }, - }); - const parsed = JSON.parse( - response, - ) as Response; - return plainToInstance(TransactionWithMetadata, parsed.payload); + return ( + await this.prepareSend([{ address, amount }], transactionOptions) + ).send(); } /** @@ -1580,22 +1571,7 @@ export class Wallet { params: SendParams[], transactionOptions?: TransactionOptions, ): Promise { - for (let i = 0; i < params.length; i++) { - if (typeof params[i].amount === 'bigint') { - params[i].amount = params[i].amount.toString(10); - } - } - const response = await this.methodHandler.callMethod({ - name: 'sendWithParams', - data: { - params, - options: transactionOptions, - }, - }); - const parsed = JSON.parse( - response, - ) as Response; - return plainToInstance(TransactionWithMetadata, parsed.payload); + return (await this.prepareSend(params, transactionOptions)).send(); } /** diff --git a/bindings/python/iota_sdk/wallet/wallet.py b/bindings/python/iota_sdk/wallet/wallet.py index eb8c8e6913..f9e666197c 100644 --- a/bindings/python/iota_sdk/wallet/wallet.py +++ b/bindings/python/iota_sdk/wallet/wallet.py @@ -591,18 +591,6 @@ def prepare_output(self, params: OutputParams, }) ) - def prepare_send(self, params: List[SendParams], - options: Optional[TransactionOptions] = None) -> PreparedTransaction: - """Prepare to send base coins. - """ - prepared = PreparedTransactionData.from_dict(self._call_method( - 'prepareSend', { - 'params': params, - 'options': options - } - )) - return PreparedTransaction(self, prepared) - def create_delegation(self, params: CreateDelegationParams, options: Optional[TransactionOptions] = None) -> CreateDelegationTransaction: """Create a delegation. @@ -735,24 +723,25 @@ def send(self, amount: int, address: str, options: Optional[TransactionOptions] = None) -> TransactionWithMetadata: """Send base coins. """ - return TransactionWithMetadata.from_dict(self._call_method( - 'send', { - 'amount': str(amount), - 'address': address, - 'options': options - } - )) + return self.prepare_send([SendParams(address, amount)], options).send() def send_with_params( self, params: List[SendParams], options: Optional[TransactionOptions] = None) -> TransactionWithMetadata: """Send base coins to multiple addresses or with additional parameters. """ - return TransactionWithMetadata.from_dict(self._call_method( - 'sendWithParams', { - 'params': [param.to_dict() for param in params], + return self.prepare_send(params, options).send() + + def prepare_send(self, params: List[SendParams], + options: Optional[TransactionOptions] = None) -> PreparedTransaction: + """Prepare to send with params. + """ + prepared = PreparedTransactionData.from_dict(self._call_method( + 'prepareSend', { + 'params': params, 'options': options } )) + return PreparedTransaction(self, prepared) def send_native_tokens( self, params: List[SendNativeTokenParams], options: Optional[TransactionOptions] = None) -> TransactionWithMetadata: