Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
[Transfer] Fix known properties that don't meet SEP-24 spec (#130)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Morley Zhi authored Dec 12, 2019
1 parent 64e8a15 commit fb34411
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
22 changes: 20 additions & 2 deletions src/transfers/TransferProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -156,7 +174,7 @@ export abstract class TransferProvider {

const { transactions } = await response.json();

return transactions;
return transactions.map(_normalizeTransaction);
}

/**
Expand All @@ -181,7 +199,7 @@ export abstract class TransferProvider {

const transaction: Transaction = await response.json();

return transaction;
return _normalizeTransaction(transaction);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/types/transfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit fb34411

Please sign in to comment.