Skip to content
This repository was archived by the owner on May 24, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/light.js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const {
accounts$,
accountsInfo$,
balanceOf$,
transactionCountOf$,
blockNumber$,
chainName$,
defaultAccount$,
Expand Down
16 changes: 16 additions & 0 deletions packages/light.js/src/rpc/eth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ export function balanceOf$ (address: Address, options?: RpcObservableOptions) {
})(options)(address);
}

/**
* Get the transaction count of a given account. Calls `eth_getTransactionCount`
*
* @param address - Address of the account whose transaction count we want to query.
* @param options - Options to pass to {@link RpcObservableOptions}.
* @return - An Observable containing the transaction count.
*/
export function transactionCountOf$ (address: Address, options?: RpcObservableOptions) {
return createRpc$<any, number | Symbol>({
calls: ['eth_getTransactionCount'],
frequency: [frequency.onEveryBlock$, frequency.onStartup$],
name: 'transactionCountOf$',
pipes: api => [switchMapPromise(() => api.eth.getTransactionCount(address).then(Number))]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is then(Number)?

Copy link
Contributor Author

@axelchalon axelchalon Dec 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use the Number constructor to convert the value to a number (e.g. "5" to 5)
otherwise the value we receive is an object/string; I thought that it would make sense if transactionCountOf$ returns a number type

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice. So in this case the TS def should be createRpc$<any, number | Symbol>?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes, you're right!

})(options)(address);
}

/**
* Get the default account managed by the light client.
*
Expand Down
2 changes: 1 addition & 1 deletion packages/light.js/src/utils/testHelpers/mockApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class MockProvider extends EventEmitter {

// List of JSONRPCs we want to mock
const listOfMockRps: { [index: string]: string[] } = {
eth: ['accounts', 'blockNumber', 'getBalance', 'newHeads', 'syncing'],
eth: ['accounts', 'blockNumber', 'getBalance', 'getTransactionCount', 'newHeads', 'syncing'],
fake: ['method'],
net: ['peerCount'],
parity: ['accountsInfo', 'chain', 'postTransaction']
Expand Down