Skip to content

Commit

Permalink
SEP-31: make sure we get the amount_in from GET /transactions before …
Browse files Browse the repository at this point in the history
…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 stellar/stellar-protocol#1203.
  • Loading branch information
marcelosalloum authored May 17, 2022
1 parent 9c7e3f3 commit a216df3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
10 changes: 8 additions & 2 deletions packages/demo-wallet-client/src/ducks/sep31Send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -15,6 +15,7 @@ export const pollTransactionUntilReady = async ({
});

let transactionStatus;
let resultJson: Sep31GetTransaction = {};

while (transactionStatus !== TransactionStatus.PENDING_SENDER) {
log.request({ title: `GET \`/transactions/${transactionId}\`` });
Expand All @@ -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;
};
24 changes: 24 additions & 0 deletions packages/demo-wallet-shared/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit a216df3

Please sign in to comment.