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

Commit

Permalink
Fetch transactions with stellar / external ids (#133)
Browse files Browse the repository at this point in the history
- Add support for other ways of finding a transaction.
- Make it possible to watch more than one transaction on a given transfer server at a time.
  • Loading branch information
Morley Zhi authored Dec 12, 2019
1 parent fb34411 commit 74547fa
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 16 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.2",
"version": "0.0.8-rc.3",
"description": "Libraries to help you write Stellar-enabled wallets in Javascript",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
75 changes: 61 additions & 14 deletions src/transfers/TransferProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ export abstract class TransferProvider {
public authToken?: string;

// This type monstrosity courtesy of https://stackoverflow.com/a/56239226
protected _oneTransactionWatcher?: ReturnType<typeof setTimeout>;
protected _oneTransactionWatcher: {
[id: string]: ReturnType<typeof setTimeout>;
};
protected _allTransactionsWatcher?: ReturnType<typeof setTimeout>;

protected _watchOneTransactionRegistry: WatchOneTransactionRegistry;
Expand Down Expand Up @@ -105,6 +107,7 @@ export abstract class TransferProvider {
this._watchAllTransactionsRegistry = {};
this._transactionsRegistry = {};
this._transactionsIgnoredRegistry = {};
this._oneTransactionWatcher = {};
}

protected async fetchInfo(): Promise<Info> {
Expand Down Expand Up @@ -184,14 +187,42 @@ export abstract class TransferProvider {
params: TransactionParams,
isWatching: boolean,
): Promise<Transaction> {
const { asset_code, id } = params;
const {
asset_code,
id,
stellar_transaction_id,
external_transaction_id,
} = params;

// one of either id or stellar_transaction_id must be provided
if (
id === undefined &&
stellar_transaction_id === undefined &&
external_transaction_id === undefined
) {
throw new Error(
"fetchTransaction: One of `id`, `external_transaction_id`, " +
"or `stellar_transaction_id` must be provided! ",
);
}

const isAuthRequired = this.getAuthStatus(
isWatching ? "watchOneTransaction" : "fetchTransaction",
asset_code,
);

let qs: { [name: string]: string } = {};

if (id) {
qs = { id };
} else if (stellar_transaction_id) {
qs = { stellar_transaction_id };
} else if (external_transaction_id) {
qs = { external_transaction_id };
}

const response = await fetch(
`${this.transferServer}/transaction?${queryString.stringify({ id })}`,
`${this.transferServer}/transaction?${queryString.stringify(qs)}`,
{
headers: isAuthRequired ? this.getHeaders() : undefined,
},
Expand Down Expand Up @@ -371,20 +402,32 @@ export abstract class TransferProvider {
const {
asset_code,
id,
external_transaction_id,
stellar_transaction_id,
onMessage,
onSuccess,
onError,
timeout = 5000,
isRetry = false,
...otherParams
} = params;

const txId = id || external_transaction_id || stellar_transaction_id;

if (txId === undefined) {
throw new Error(
"fetchTransaction: One of `id`, `external_transaction_id`, " +
"or `stellar_transaction_id` must be provided! ",
);
}

// if it's a first blush, drop it in the registry
if (!isRetry) {
this._watchOneTransactionRegistry = {
...this._watchOneTransactionRegistry,
[asset_code]: {
...(this._watchOneTransactionRegistry[asset_code] || {}),
[id]: true,
[txId]: true,
},
};
}
Expand All @@ -395,7 +438,7 @@ export abstract class TransferProvider {
if (
!(
this._watchOneTransactionRegistry[asset_code] &&
this._watchOneTransactionRegistry[asset_code][id]
this._watchOneTransactionRegistry[asset_code][txId]
)
) {
return;
Expand All @@ -416,14 +459,16 @@ export abstract class TransferProvider {
this._transactionsRegistry[asset_code][transaction.id] = transaction;

if (transaction.status.indexOf("pending") === 0) {
if (this._oneTransactionWatcher) {
clearTimeout(this._oneTransactionWatcher);
if (this._oneTransactionWatcher[txId]) {
clearTimeout(this._oneTransactionWatcher[txId]);
}

this._oneTransactionWatcher = setTimeout(() => {
this._oneTransactionWatcher[txId] = setTimeout(() => {
this.watchOneTransaction({
asset_code,
id,
external_transaction_id,
stellar_transaction_id,
onMessage,
onSuccess,
onError,
Expand All @@ -448,19 +493,21 @@ export abstract class TransferProvider {
if (
!(
this._watchOneTransactionRegistry[asset_code] &&
this._watchOneTransactionRegistry[asset_code][id]
this._watchOneTransactionRegistry[asset_code][txId]
)
) {
return;
}

if (this._oneTransactionWatcher) {
clearTimeout(this._oneTransactionWatcher);
if (this._oneTransactionWatcher[txId]) {
clearTimeout(this._oneTransactionWatcher[txId]);
}

this.watchOneTransaction({
asset_code,
id,
external_transaction_id,
stellar_transaction_id,
onMessage,
onSuccess,
onError,
Expand All @@ -469,9 +516,9 @@ export abstract class TransferProvider {
});
},
stop: () => {
if (this._oneTransactionWatcher) {
this._watchOneTransactionRegistry[asset_code][id] = false;
clearTimeout(this._oneTransactionWatcher);
if (this._oneTransactionWatcher[txId]) {
this._watchOneTransactionRegistry[asset_code][txId] = false;
clearTimeout(this._oneTransactionWatcher[txId]);
}
},
};
Expand Down
4 changes: 3 additions & 1 deletion src/types/transfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,9 @@ export interface TransactionsParams {

export interface TransactionParams {
asset_code: string;
id: string;
id?: string;
stellar_transaction_id?: string;
external_transaction_id?: string;
}

export interface WatchAllTransactionsParams extends WatcherParams<Transaction> {
Expand Down

0 comments on commit 74547fa

Please sign in to comment.