From fb3441128f3bef1c494c1626b3ea781232931f3d Mon Sep 17 00:00:00 2001 From: Morley Zhi Date: Thu, 12 Dec 2019 14:50:42 -0500 Subject: [PATCH] [Transfer] Fix known properties that don't meet SEP-24 spec (#130) Some anchors don't 100% meet the SEP-24 spec for transaction object requirements, so normalize transaction responses to fix some known examples. - Settle submits _id instead of id, so rewrite that if found. - Saldo submits amount instead of amount_in and amount_out, so rewrite that if found. --- package.json | 2 +- src/transfers/TransferProvider.ts | 22 ++++++++++++++++++++-- src/types/transfers.ts | 4 ++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index de06a61e..549e40bb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@stellar/wallet-sdk", - "version": "0.0.8-rc.1", + "version": "0.0.8-rc.2", "description": "Libraries to help you write Stellar-enabled wallets in Javascript", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/transfers/TransferProvider.ts b/src/transfers/TransferProvider.ts index 401fa207..408abd97 100644 --- a/src/transfers/TransferProvider.ts +++ b/src/transfers/TransferProvider.ts @@ -43,6 +43,24 @@ interface TransactionsRegistry { [asset_code: string]: TransactionsRegistryAsset; } +function _normalizeTransaction(transaction: Transaction) { + // some anchors (Settle) return _id instead of id, so rewrite that + if (transaction._id && transaction.id === undefined) { + transaction.id = transaction._id; + } + + // others (Saldo) provide amount but not amount_in / amount_out + if ( + transaction.amount && + transaction.amount_in === undefined && + transaction.amount_out === undefined + ) { + transaction.amount_in = transaction.amount; + transaction.amount_out = transaction.amount; + } + return transaction; +} + /** * TransferProvider is the base class for WithdrawProvider and DepositProvider. */ @@ -156,7 +174,7 @@ export abstract class TransferProvider { const { transactions } = await response.json(); - return transactions; + return transactions.map(_normalizeTransaction); } /** @@ -181,7 +199,7 @@ export abstract class TransferProvider { const transaction: Transaction = await response.json(); - return transaction; + return _normalizeTransaction(transaction); } /** diff --git a/src/types/transfers.ts b/src/types/transfers.ts index 1e4bb886..4073c56c 100644 --- a/src/types/transfers.ts +++ b/src/types/transfers.ts @@ -261,6 +261,10 @@ interface BaseTransaction { external_transaction_id?: string; message?: string; refunded?: boolean; + + // these are off-spec props from certain anchors + _id?: string; + amount?: string; } export interface DepositTransaction extends BaseTransaction {