From a216df39bbe693fe21cbdb31d53a79c30857a042 Mon Sep 17 00:00:00 2001 From: Marcelo Salloum dos Santos Date: Tue, 17 May 2022 13:40:31 -0300 Subject: [PATCH] SEP-31: make sure we get the amount_in from GET /transactions before sending the stellar payment (#275) ### What Make sure we get the `amount_in` from `GET /transactions` before sending the stellar payment. ### Why To be consistent with https://github.com/stellar/stellar-protocol/pull/1203. --- .../demo-wallet-client/src/ducks/sep31Send.ts | 10 ++++++-- .../sep31Send/pollTransactionUntilReady.ts | 9 ++++--- packages/demo-wallet-shared/types/types.ts | 24 +++++++++++++++++++ 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/packages/demo-wallet-client/src/ducks/sep31Send.ts b/packages/demo-wallet-client/src/ducks/sep31Send.ts index f729631a..90288d75 100644 --- a/packages/demo-wallet-client/src/ducks/sep31Send.ts +++ b/packages/demo-wallet-client/src/ducks/sep31Send.ts @@ -21,6 +21,7 @@ import { pollTransactionUntilComplete, } from "demo-wallet-shared/build/methods/sep31Send"; import { checkTomlForFields } from "demo-wallet-shared/build/methods/checkTomlForFields"; +import { Sep31GetTransaction } from "demo-wallet-shared/build/types/types"; import { Asset, @@ -324,15 +325,20 @@ export const submitSep31SendTransactionAction = createAsyncThunk< }); // Poll transaction until ready - await pollTransactionUntilReady({ + const getSep31Tx: Sep31GetTransaction = await pollTransactionUntilReady({ sendServer, transactionId: postResponse.transactionId, token, }); + const amountIn = getSep31Tx?.transaction?.amount_in; + if (amountIn === undefined) { + throw new Error(`"amount_in" is missing from the GET /transaction response`); + } + // Send payment await sendPayment({ - amount: amount.amount, + amount: amountIn, assetCode, assetIssuer, receiverAddress: postResponse.receiverAddress, diff --git a/packages/demo-wallet-shared/methods/sep31Send/pollTransactionUntilReady.ts b/packages/demo-wallet-shared/methods/sep31Send/pollTransactionUntilReady.ts index 7c0d98e6..879964c9 100644 --- a/packages/demo-wallet-shared/methods/sep31Send/pollTransactionUntilReady.ts +++ b/packages/demo-wallet-shared/methods/sep31Send/pollTransactionUntilReady.ts @@ -1,5 +1,5 @@ import { log } from "../../helpers/log"; -import { TransactionStatus } from "../../types/types"; +import { TransactionStatus, Sep31GetTransaction } from "../../types/types"; export const pollTransactionUntilReady = async ({ sendServer, @@ -15,6 +15,7 @@ export const pollTransactionUntilReady = async ({ }); let transactionStatus; + let resultJson: Sep31GetTransaction = {}; while (transactionStatus !== TransactionStatus.PENDING_SENDER) { log.request({ title: `GET \`/transactions/${transactionId}\`` }); @@ -32,14 +33,16 @@ export const pollTransactionUntilReady = async ({ } // eslint-disable-next-line no-await-in-loop - const resultJson = await result.json(); + resultJson = await result.json(); log.response({ title: `GET \`/transactions/${transactionId}\``, body: resultJson, }); - transactionStatus = resultJson.transaction.status; + transactionStatus = resultJson.transaction?.status; // eslint-disable-next-line no-await-in-loop await new Promise((resolve) => setTimeout(resolve, 2000)); } + + return resultJson; }; diff --git a/packages/demo-wallet-shared/types/types.ts b/packages/demo-wallet-shared/types/types.ts index 409265fd..66b0475c 100644 --- a/packages/demo-wallet-shared/types/types.ts +++ b/packages/demo-wallet-shared/types/types.ts @@ -277,6 +277,30 @@ export interface Sep31SendInitialState { status: ActionStatus | undefined; } +export interface Sep31GetTransaction { + transaction?: { + id: string; + status: string; + status_eta?: number; + amount_in?: string; + amount_in_asset?: string; + amount_out?: string; + amount_out_asset?: string; + amount_fee?: string; + amount_fee_asset?: string; + stellar_account_id: string; + stellar_memo_type: string; + stellar_memo: string; + started_at?: string; + completed_at?: string; + stellar_transaction_id?: string; + external_transaction_id?: string; + refunded?: boolean; + required_info_message?: string; + required_info_updates?: any; + }; +} + export interface Setting { [key: string]: any; }