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

Commit

Permalink
watchTransaction (#108)
Browse files Browse the repository at this point in the history
- Name auth token functions more consistently.
  - getAuthToken -> fetchAuthToken
  - setBearerToken -> setAuthToken
- Explain why fetchAuthToken is on KeyManager and setAuthToken on DepositProvider / WithdrawProvider
- Add a new generic type for watchers 
- Add a function, watchTransaction, to poll the transaction endpoint until the transaction stops pending
- Add tests for the watcher
  • Loading branch information
Morley Zhi authored Sep 26, 2019
1 parent ce96f0b commit 169acaa
Show file tree
Hide file tree
Showing 15 changed files with 654 additions and 106 deletions.
2 changes: 1 addition & 1 deletion plans/authservers.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,5 @@ interface GetAuthTokenParams {
publicKey: PublicKey;
}

const jwt = await KeyManager.getAuthToken(params: GetAuthTokenParams);
const jwt = await KeyManager.fetchAuthToken(params: GetAuthTokenParams);
```
5 changes: 2 additions & 3 deletions playground/src/components/AccountDetails.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, { Component } from "react";
import Json from "react-json-view";

class AccountDetails extends Component {
state = {
Expand Down Expand Up @@ -83,9 +84,7 @@ class AccountDetails extends Component {
<li key={time.toString()}>{time.toString()}</li>
))}
</ul>
{accountDetails && (
<pre>{JSON.stringify(accountDetails, null, 2)}</pre>
)}
{accountDetails && <Json src={accountDetails} />}
</>
)}

Expand Down
2 changes: 1 addition & 1 deletion playground/src/components/Authorization.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Authorization extends Component {
"https://transfer-dot-jewel-api-dev.appspot.com",
);

depositProvider.setBearerToken("testtesttest");
depositProvider.setAuthToken("testtesttest");

this.setState({ depositProvider });

Expand Down
6 changes: 3 additions & 3 deletions playground/src/components/KeyEntry.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ export default class KeyEntry extends Component {
}
};

_getAuthToken = async (authServer) => {
_fetchAuthToken = async (authServer) => {
const { keyManager, password, keyMetadata } = this.state;
this.setState({ authToken: null });
localStorage.setItem("authServer", authServer);

try {
const authToken = await keyManager.getAuthToken({
const authToken = await keyManager.fetchAuthToken({
id: keyMetadata.id,
password,
authServer,
Expand Down Expand Up @@ -118,7 +118,7 @@ export default class KeyEntry extends Component {
<form
onSubmit={(ev) => {
ev.preventDefault();
this._getAuthToken(authServer);
this._fetchAuthToken(authServer);
}}
>
<label>
Expand Down
12 changes: 6 additions & 6 deletions playground/src/components/TransferProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ class TransferProvider extends Component {
}

_setUrl = (url) => {
const depositProvider = new WalletSdk.DepositProvider(url);
const depositProvider = new WalletSdk.DepositProvider(
url,
this.props.dataProvider.getAccountKey(),
);

this.setState({
depositProvider,
Expand All @@ -51,9 +54,7 @@ class TransferProvider extends Component {
transactions,
transactionError,
} = this.state;
const { dataProvider, authToken } = this.props;

const accountKey = dataProvider.getAccountKey();
const { authToken } = this.props;

return (
<div>
Expand Down Expand Up @@ -109,11 +110,10 @@ class TransferProvider extends Component {
theme={ButtonThemes.primary}
onClick={() => {
if (authToken) {
depositProvider.setBearerToken(authToken);
depositProvider.setAuthToken(authToken);
}
depositProvider
.fetchTransactions({
account: accountKey,
asset_code: assetCode,
})
.then((transactions) =>
Expand Down
8 changes: 4 additions & 4 deletions src/KeyManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ describe("KeyManager", function() {
).toEqual(true);
});

describe("getAuthToken", () => {
describe("fetchAuthToken", () => {
beforeEach(() => {
// @ts-ignore
fetch.resetMocks();
Expand All @@ -120,7 +120,7 @@ describe("KeyManager", function() {
});
try {
// @ts-ignore
await testKeyManager.getAuthToken({});
await testKeyManager.fetchAuthToken({});
expect("This test failed").toBe(null);
} catch (e) {
expect(e).toBeTruthy();
Expand Down Expand Up @@ -163,7 +163,7 @@ describe("KeyManager", function() {
});

try {
await testKeyManager.getAuthToken({
await testKeyManager.fetchAuthToken({
id: keyMetadata.id,
password,
authServer,
Expand Down Expand Up @@ -212,7 +212,7 @@ describe("KeyManager", function() {
});

try {
await testKeyManager.getAuthToken({
await testKeyManager.fetchAuthToken({
id: keyMetadata.id,
password,
authServer,
Expand Down
2 changes: 1 addition & 1 deletion src/KeyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export class KeyManager {
* @returns {Promise<string>} authToken JWT
*/
// tslint:enable max-line-length
public async getAuthToken({
public async fetchAuthToken({
id,
password,
authServer,
Expand Down
10 changes: 4 additions & 6 deletions src/data/DataProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
Offer,
Trade,
Transfer,
WatcherParams,
} from "../types";

import { makeDisplayableBalances } from "./makeDisplayableBalances";
Expand All @@ -23,11 +24,6 @@ export interface DataProviderParams {
accountOrKey: Account | string;
}

export interface WatcherParams {
onMessage: (accountDetails: AccountDetails) => void;
onError: (error: any) => void;
}

function isAccount(obj: any): obj is Account {
return obj.publicKey !== undefined;
}
Expand Down Expand Up @@ -188,7 +184,9 @@ export class DataProvider {
* If the account doesn't exist yet, it will re-check it every 2 seconds.
* Returns a function you can execute to stop the watcher.
*/
public watchAccountDetails(params: WatcherParams): () => void {
public watchAccountDetails(
params: WatcherParams<AccountDetails>,
): () => void {
const { onMessage, onError } = params;

this.fetchAccountDetails()
Expand Down
Loading

0 comments on commit 169acaa

Please sign in to comment.