Skip to content
This repository has been archived by the owner on Nov 23, 2023. It is now read-only.

Commit

Permalink
blockchainSubscribe: param "accounts" optional (#566)
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonlesisz authored Apr 16, 2020
1 parent c1ea517 commit 82f1d6a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 16 deletions.
11 changes: 7 additions & 4 deletions src/js/backend/BlockchainLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export default class Blockchain {
coin: this.coinInfo,
...info,
}));
// TODO: check for 1st block hash (different for btc forks)
});

this.link.on('disconnected', () => {
Expand Down Expand Up @@ -167,7 +168,7 @@ export default class Blockchain {
return this.link.estimateFee(request);
}

async subscribe(accounts: BlockchainSubscribeAccount[]) {
async subscribe(accounts?: BlockchainSubscribeAccount[]) {
// set block listener if it wasn't set before
if (this.link.listenerCount('block') === 0) {
this.link.on('block', (block: BlockchainBlock) => {
Expand All @@ -188,7 +189,10 @@ export default class Blockchain {
});
}

await this.link.subscribe({ type: 'block' });
const blockSubscription = await this.link.subscribe({ type: 'block' });
if (!accounts) {
return blockSubscription;
}

return this.link.subscribe({
type: 'accounts',
Expand Down Expand Up @@ -221,9 +225,8 @@ export default class Blockchain {
this.link.removeAllListeners('notification');

// remove all subscriptions
await this.link.unsubscribe({ type: 'block' });
await this.link.unsubscribe({ type: 'fiatRates' });
return this.link.unsubscribe({ type: 'notification' });
return this.link.unsubscribe({ type: 'block' });
}
// unsubscribe only requested accounts
return this.link.unsubscribe({ type: 'accounts', accounts });
Expand Down
16 changes: 9 additions & 7 deletions src/js/core/methods/blockchain/BlockchainSubscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { getCoinInfo } from '../../../data/CoinInfo';
import type { CoreMessage, CoinInfo, BlockchainSubscribeAccount } from '../../../types';

type Params = {
accounts: BlockchainSubscribeAccount[];
accounts?: BlockchainSubscribeAccount[];
coinInfo: CoinInfo;
}

Expand All @@ -25,15 +25,17 @@ export default class BlockchainSubscribe extends AbstractMethod {

// validate incoming parameters
validateParams(payload, [
{ name: 'accounts', type: 'array', obligatory: true },
{ name: 'accounts', type: 'array', allowEmpty: true },
{ name: 'coin', type: 'string', obligatory: true },
]);

payload.accounts.forEach(account => {
validateParams(account, [
{ name: 'descriptor', type: 'string', obligatory: true },
]);
});
if (payload.accounts) {
payload.accounts.forEach(account => {
validateParams(account, [
{ name: 'descriptor', type: 'string', obligatory: true },
]);
});
}

const coinInfo = getCoinInfo(payload.coin);
if (!coinInfo) {
Expand Down
2 changes: 1 addition & 1 deletion src/js/core/methods/blockchain/BlockchainUnsubscribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default class BlockchainUnsubscribe extends AbstractMethod {

// validate incoming parameters
validateParams(payload, [
{ name: 'accounts', type: 'array' },
{ name: 'accounts', type: 'array', allowEmpty: true },
{ name: 'coin', type: 'string', obligatory: true },
]);

Expand Down
12 changes: 12 additions & 0 deletions src/js/types/__tests__/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,30 @@ export const others = async () => {
coin: 'btc',
});

TrezorConnect.blockchainSubscribe({
coin: 'btc',
});

TrezorConnect.blockchainUnsubscribe({
accounts,
coin: 'btc',
});

TrezorConnect.blockchainUnsubscribe({
coin: 'btc',
});

TrezorConnect.blockchainDisconnect({ coin: 'btc' });

TrezorConnect.blockchainSetCustomBackend({
coin: 'btc',
blockchainLink: undefined,
});

TrezorConnect.blockchainSetCustomBackend({
coin: 'btc',
});

TrezorConnect.blockchainSetCustomBackend({
coin: 'btc',
blockchainLink: {
Expand Down
4 changes: 2 additions & 2 deletions src/js/types/backend/blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export type BlockchainSubscribeFiatRates = {
}

export type BlockchainSubscribe = {
accounts: BlockchainSubscribeAccount[];
accounts?: BlockchainSubscribeAccount[];
coin: string;
}

Expand Down Expand Up @@ -146,7 +146,7 @@ export type BlockchainEstimatedFee = {

export type BlockchainSetCustomBackend = {
coin: string;
blockchainLink: $PropertyType<CoinInfo, 'blockchainLink'>;
blockchainLink?: $PropertyType<CoinInfo, 'blockchainLink'>;
}

export type BlockchainEvent =
Expand Down
12 changes: 12 additions & 0 deletions src/ts/types/__tests__/blockchain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,30 @@ export const others = async () => {
coin: 'btc',
});

TrezorConnect.blockchainSubscribe({
coin: 'btc',
});

TrezorConnect.blockchainUnsubscribe({
accounts,
coin: 'btc',
});

TrezorConnect.blockchainUnsubscribe({
coin: 'btc',
});

TrezorConnect.blockchainDisconnect({ coin: 'btc' });

TrezorConnect.blockchainSetCustomBackend({
coin: 'btc',
blockchainLink: undefined,
});

TrezorConnect.blockchainSetCustomBackend({
coin: 'btc',
});

TrezorConnect.blockchainSetCustomBackend({
coin: 'btc',
blockchainLink: {
Expand Down
4 changes: 2 additions & 2 deletions src/ts/types/backend/blockchain.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export interface BlockchainSubscribeAccount {
}

export interface BlockchainSubscribe {
accounts: BlockchainSubscribeAccount[];
accounts?: BlockchainSubscribeAccount[];
coin: string;
}

Expand Down Expand Up @@ -148,7 +148,7 @@ export interface BlockchainAccountBalanceHistory {

export interface BlockchainSetCustomBackend {
coin: string;
blockchainLink: CoinInfo['blockchainLink'];
blockchainLink?: CoinInfo['blockchainLink'];
}

export type BlockchainEvent =
Expand Down

0 comments on commit 82f1d6a

Please sign in to comment.