diff --git a/packages/light.js/src/index.ts b/packages/light.js/src/index.ts index 1870a7d1..d6f52c3f 100644 --- a/packages/light.js/src/index.ts +++ b/packages/light.js/src/index.ts @@ -17,6 +17,7 @@ export const { accounts$, accountsInfo$, balanceOf$, + transactionCountOf$, blockNumber$, chainName$, defaultAccount$, diff --git a/packages/light.js/src/rpc/eth.ts b/packages/light.js/src/rpc/eth.ts index 9e982f4f..efefb4d4 100644 --- a/packages/light.js/src/rpc/eth.ts +++ b/packages/light.js/src/rpc/eth.ts @@ -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$({ + calls: ['eth_getTransactionCount'], + frequency: [frequency.onEveryBlock$, frequency.onStartup$], + name: 'transactionCountOf$', + pipes: api => [switchMapPromise(() => api.eth.getTransactionCount(address).then(Number))] + })(options)(address); +} + /** * Get the default account managed by the light client. * diff --git a/packages/light.js/src/utils/testHelpers/mockApi.ts b/packages/light.js/src/utils/testHelpers/mockApi.ts index 79583d36..341780c3 100644 --- a/packages/light.js/src/utils/testHelpers/mockApi.ts +++ b/packages/light.js/src/utils/testHelpers/mockApi.ts @@ -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']