@stellar/typescript-wallet-sdk 1.1.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +32 -18
- package/lib/bundle.js +10 -56
- package/lib/bundle.js.map +1 -1
- package/lib/bundle_browser.js +10 -56
- package/lib/bundle_browser.js.map +1 -1
- package/package.json +1 -1
- package/src/walletSdk/Watcher/index.ts +9 -8
- package/test/wallet.test.ts +131 -16
package/package.json
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import isEqual from "lodash/isEqual";
|
|
2
|
-
|
|
3
1
|
import { Anchor } from "../Anchor";
|
|
4
2
|
import {
|
|
5
3
|
AnchorTransaction,
|
|
@@ -151,9 +149,9 @@ export class Watcher {
|
|
|
151
149
|
return isInProgress;
|
|
152
150
|
}
|
|
153
151
|
|
|
154
|
-
// if we've had the transaction before, only report updates
|
|
152
|
+
// if we've had the transaction before, only report updates if status changed
|
|
155
153
|
if (registeredTransaction) {
|
|
156
|
-
return
|
|
154
|
+
return registeredTransaction.status !== transaction.status;
|
|
157
155
|
}
|
|
158
156
|
|
|
159
157
|
// if it's NOT a registered transaction, and it's not the first
|
|
@@ -296,12 +294,13 @@ export class Watcher {
|
|
|
296
294
|
const registeredTransaction =
|
|
297
295
|
this._transactionsRegistry[assetCode][transaction.id];
|
|
298
296
|
|
|
299
|
-
// if we've had the transaction before, only report if there is a change
|
|
297
|
+
// if we've had the transaction before, only report if there is a status change
|
|
298
|
+
let isChanged = true;
|
|
300
299
|
if (
|
|
301
300
|
registeredTransaction &&
|
|
302
|
-
|
|
301
|
+
registeredTransaction.status === transaction.status
|
|
303
302
|
) {
|
|
304
|
-
|
|
303
|
+
isChanged = false;
|
|
305
304
|
}
|
|
306
305
|
|
|
307
306
|
this._transactionsRegistry[assetCode][transaction.id] = transaction;
|
|
@@ -320,7 +319,9 @@ export class Watcher {
|
|
|
320
319
|
isRetry: true,
|
|
321
320
|
});
|
|
322
321
|
}, timeout);
|
|
323
|
-
|
|
322
|
+
if (isChanged) {
|
|
323
|
+
onMessage(transaction);
|
|
324
|
+
}
|
|
324
325
|
} else if (
|
|
325
326
|
[
|
|
326
327
|
TransactionStatus.completed,
|
package/test/wallet.test.ts
CHANGED
|
@@ -64,6 +64,13 @@ describe("SEP-24 flow", () => {
|
|
|
64
64
|
let anchor: Anchor;
|
|
65
65
|
let accountKp: SigningKeypair;
|
|
66
66
|
let authToken: string;
|
|
67
|
+
const makeTransaction = (eta: number, txStatus: TransactionStatus) => ({
|
|
68
|
+
kind: "deposit",
|
|
69
|
+
id: "TEST",
|
|
70
|
+
status: txStatus,
|
|
71
|
+
status_eta: eta,
|
|
72
|
+
message: "some message",
|
|
73
|
+
});
|
|
67
74
|
describe("Anchor", () => {
|
|
68
75
|
beforeAll(() => {
|
|
69
76
|
const Wal = Wallet.TestNet();
|
|
@@ -809,6 +816,56 @@ describe("Anchor", () => {
|
|
|
809
816
|
expect(onMessage.callCount).toBe(3);
|
|
810
817
|
expect(onError.callCount).toBe(0);
|
|
811
818
|
|
|
819
|
+
// stops watching
|
|
820
|
+
stop();
|
|
821
|
+
});
|
|
822
|
+
it("should only report transactions with changed status", async () => {
|
|
823
|
+
const txn1 = makeTransaction(0, TransactionStatus.incomplete);
|
|
824
|
+
const txn2 = makeTransaction(0, TransactionStatus.incomplete);
|
|
825
|
+
txn2.message = "message changing";
|
|
826
|
+
const txn3 = makeTransaction(0, TransactionStatus.pending_anchor);
|
|
827
|
+
|
|
828
|
+
const onMessage = sinon.spy((m) => {
|
|
829
|
+
expect(m.message).toBe("some message");
|
|
830
|
+
expect(onMessage.callCount).toBeLessThan(3);
|
|
831
|
+
});
|
|
832
|
+
|
|
833
|
+
const onError = sinon.spy((e) => {
|
|
834
|
+
expect(e).toBeUndefined();
|
|
835
|
+
});
|
|
836
|
+
|
|
837
|
+
// mock default transactions response
|
|
838
|
+
jest
|
|
839
|
+
.spyOn(Sep24.prototype, "getTransactionsForAsset")
|
|
840
|
+
.mockResolvedValueOnce([txn1])
|
|
841
|
+
.mockResolvedValueOnce([txn2])
|
|
842
|
+
.mockResolvedValueOnce([txn3]);
|
|
843
|
+
|
|
844
|
+
// start watching
|
|
845
|
+
const watcher = anchor.sep24().watcher();
|
|
846
|
+
const { stop } = watcher.watchAllTransactions({
|
|
847
|
+
authToken,
|
|
848
|
+
assetCode: "SRT",
|
|
849
|
+
lang: "en-US",
|
|
850
|
+
onMessage,
|
|
851
|
+
onError,
|
|
852
|
+
timeout: 1,
|
|
853
|
+
});
|
|
854
|
+
|
|
855
|
+
// nothing should run at first (async api call in progress)
|
|
856
|
+
expect(onMessage.callCount).toBe(0);
|
|
857
|
+
expect(onError.callCount).toBe(0);
|
|
858
|
+
|
|
859
|
+
clock.next();
|
|
860
|
+
await sleep(1);
|
|
861
|
+
clock.next();
|
|
862
|
+
await sleep(1);
|
|
863
|
+
clock.next();
|
|
864
|
+
await sleep(1);
|
|
865
|
+
|
|
866
|
+
expect(onMessage.callCount).toBe(2);
|
|
867
|
+
expect(onError.callCount).toBe(0);
|
|
868
|
+
|
|
812
869
|
// stops watching
|
|
813
870
|
stop();
|
|
814
871
|
});
|
|
@@ -818,13 +875,6 @@ describe("Anchor", () => {
|
|
|
818
875
|
let clock: sinon.SinonFakeTimers;
|
|
819
876
|
let watcher: Watcher;
|
|
820
877
|
|
|
821
|
-
const makeTransaction = (eta: number, txStatus: TransactionStatus) => ({
|
|
822
|
-
kind: "deposit",
|
|
823
|
-
id: "TEST",
|
|
824
|
-
status: txStatus,
|
|
825
|
-
status_eta: eta,
|
|
826
|
-
});
|
|
827
|
-
|
|
828
878
|
beforeEach(async () => {
|
|
829
879
|
clock = sinon.useFakeTimers(0);
|
|
830
880
|
watcher = anchor.sep24().watcher();
|
|
@@ -1120,7 +1170,7 @@ describe("Anchor", () => {
|
|
|
1120
1170
|
});
|
|
1121
1171
|
|
|
1122
1172
|
test("Several pending transactions, one completed, no more after that", async () => {
|
|
1123
|
-
const onMessage = sinon.spy(() => {
|
|
1173
|
+
const onMessage = sinon.spy((m) => {
|
|
1124
1174
|
expect(onMessage.callCount).toBeLessThanOrEqual(8);
|
|
1125
1175
|
});
|
|
1126
1176
|
|
|
@@ -1142,27 +1192,31 @@ describe("Anchor", () => {
|
|
|
1142
1192
|
.mockResolvedValueOnce(
|
|
1143
1193
|
makeTransaction(2, TransactionStatus.pending_anchor),
|
|
1144
1194
|
)
|
|
1195
|
+
// should not be logged to onMessage
|
|
1145
1196
|
.mockResolvedValueOnce(
|
|
1146
|
-
makeTransaction(3, TransactionStatus.
|
|
1197
|
+
makeTransaction(3, TransactionStatus.pending_anchor),
|
|
1147
1198
|
)
|
|
1148
1199
|
.mockResolvedValueOnce(
|
|
1149
|
-
makeTransaction(4, TransactionStatus.
|
|
1200
|
+
makeTransaction(4, TransactionStatus.pending_external),
|
|
1150
1201
|
)
|
|
1151
1202
|
.mockResolvedValueOnce(
|
|
1152
|
-
makeTransaction(5, TransactionStatus.
|
|
1203
|
+
makeTransaction(5, TransactionStatus.pending_stellar),
|
|
1204
|
+
)
|
|
1205
|
+
.mockResolvedValueOnce(
|
|
1206
|
+
makeTransaction(6, TransactionStatus.pending_trust),
|
|
1153
1207
|
)
|
|
1154
1208
|
.mockResolvedValueOnce(
|
|
1155
|
-
makeTransaction(
|
|
1209
|
+
makeTransaction(7, TransactionStatus.pending_user_transfer_start),
|
|
1156
1210
|
)
|
|
1157
1211
|
.mockResolvedValueOnce(
|
|
1158
|
-
makeTransaction(
|
|
1212
|
+
makeTransaction(8, TransactionStatus.pending_user_transfer_complete),
|
|
1159
1213
|
)
|
|
1160
|
-
.mockResolvedValueOnce(makeTransaction(
|
|
1214
|
+
.mockResolvedValueOnce(makeTransaction(9, TransactionStatus.completed))
|
|
1161
1215
|
.mockResolvedValueOnce(
|
|
1162
|
-
makeTransaction(
|
|
1216
|
+
makeTransaction(10, TransactionStatus.pending_anchor),
|
|
1163
1217
|
)
|
|
1164
1218
|
.mockResolvedValueOnce(
|
|
1165
|
-
makeTransaction(
|
|
1219
|
+
makeTransaction(11, TransactionStatus.pending_external),
|
|
1166
1220
|
);
|
|
1167
1221
|
|
|
1168
1222
|
// start watching
|
|
@@ -1206,6 +1260,9 @@ describe("Anchor", () => {
|
|
|
1206
1260
|
clock.next();
|
|
1207
1261
|
await sleep(1);
|
|
1208
1262
|
|
|
1263
|
+
clock.next();
|
|
1264
|
+
await sleep(1);
|
|
1265
|
+
|
|
1209
1266
|
// 1 incomplete + 7 pending transactions
|
|
1210
1267
|
expect(onMessage.callCount).toBe(8);
|
|
1211
1268
|
expect(onSuccess.callCount).toBe(0);
|
|
@@ -1686,6 +1743,64 @@ describe("Anchor", () => {
|
|
|
1686
1743
|
expect(onSuccess.callCount).toBe(0);
|
|
1687
1744
|
expect(onError.callCount).toBe(1);
|
|
1688
1745
|
});
|
|
1746
|
+
|
|
1747
|
+
it("should only report transactions with changed status", async () => {
|
|
1748
|
+
const txn1 = makeTransaction(0, TransactionStatus.incomplete);
|
|
1749
|
+
const txn2 = makeTransaction(0, TransactionStatus.incomplete);
|
|
1750
|
+
txn2.message = "message changing";
|
|
1751
|
+
const txn3 = makeTransaction(0, TransactionStatus.pending_anchor);
|
|
1752
|
+
|
|
1753
|
+
const onMessage = sinon.spy((m) => {
|
|
1754
|
+
expect(m.message).toBe("some message");
|
|
1755
|
+
expect(onMessage.callCount).toBeLessThan(3);
|
|
1756
|
+
});
|
|
1757
|
+
|
|
1758
|
+
const onSuccess = sinon.spy(() => {
|
|
1759
|
+
expect(onSuccess.callCount).toBe(0);
|
|
1760
|
+
});
|
|
1761
|
+
|
|
1762
|
+
const onError = sinon.spy((e) => {
|
|
1763
|
+
expect(e).toBeUndefined();
|
|
1764
|
+
});
|
|
1765
|
+
|
|
1766
|
+
// mock default transactions response
|
|
1767
|
+
jest
|
|
1768
|
+
.spyOn(Sep24.prototype, "getTransactionBy")
|
|
1769
|
+
.mockResolvedValueOnce(txn1)
|
|
1770
|
+
.mockResolvedValueOnce(txn2)
|
|
1771
|
+
.mockResolvedValueOnce(txn3);
|
|
1772
|
+
|
|
1773
|
+
// start watching
|
|
1774
|
+
const watcher = anchor.sep24().watcher();
|
|
1775
|
+
const { stop } = watcher.watchOneTransaction({
|
|
1776
|
+
authToken,
|
|
1777
|
+
assetCode: "SRT",
|
|
1778
|
+
id: "TEST",
|
|
1779
|
+
lang: "en-US",
|
|
1780
|
+
onMessage,
|
|
1781
|
+
onSuccess,
|
|
1782
|
+
onError,
|
|
1783
|
+
timeout: 1,
|
|
1784
|
+
});
|
|
1785
|
+
|
|
1786
|
+
// nothing should run at first (async api call in progress)
|
|
1787
|
+
expect(onMessage.callCount).toBe(0);
|
|
1788
|
+
expect(onError.callCount).toBe(0);
|
|
1789
|
+
|
|
1790
|
+
clock.next();
|
|
1791
|
+
await sleep(1);
|
|
1792
|
+
clock.next();
|
|
1793
|
+
await sleep(1);
|
|
1794
|
+
clock.next();
|
|
1795
|
+
await sleep(1);
|
|
1796
|
+
|
|
1797
|
+
expect(onMessage.callCount).toBe(2);
|
|
1798
|
+
expect(onError.callCount).toBe(0);
|
|
1799
|
+
expect(onSuccess.callCount).toBe(0);
|
|
1800
|
+
|
|
1801
|
+
// stops watching
|
|
1802
|
+
stop();
|
|
1803
|
+
});
|
|
1689
1804
|
});
|
|
1690
1805
|
});
|
|
1691
1806
|
|