This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
eth.ts
133 lines (123 loc) · 4.08 KB
/
eth.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
// SPDX-License-Identifier: MIT
import BigNumber from 'bignumber.js';
import { map, switchMap } from 'rxjs/operators';
import { Address, Block, RpcObservableOptions } from '../types';
import createRpc$ from './utils/createRpc';
import frequency from '../frequency';
import { switchMapPromise } from '../utils/operators';
/**
* Observable containing the EIP155 chain ID used for transaction signing.
* Calls `eth_chainId`
*
* @param options - Options to pass to {@link RpcObservableOptions}.
* @return - An Observable containing the chain ID.
*/
export function chainId$ (options?: RpcObservableOptions) {
return createRpc$<any, BigNumber | symbol>({
calls: ['eth_chainId'],
frequency: [frequency.onStartup$],
name: 'chainId$',
pipes: api => [switchMapPromise(() => api.eth.chainId())]
})(options)();
}
/**
* Observable which contains the array of all addresses managed by the light
* client.
*
* Calls eth_accounts.
*
* @param options - Options to pass to {@link RpcObservableOptions}.
* @return - An Observable containing the list of public addresses.
*/
export function accounts$ (options?: RpcObservableOptions) {
return createRpc$<Address[], Address[]>({
frequency: [frequency.onAccountsChanged$],
name: 'accounts$'
})(options)();
}
/**
* Get the balance of a given account. Calls `eth_getBalance`.
*
* @param address - The account address to query the balance.
* @param options - Options to pass to {@link RpcObservableOptions}.
* @return - An Observable containing the balance.
*/
export function balanceOf$ (address: Address, options?: RpcObservableOptions) {
return createRpc$<any, BigNumber | symbol>({
calls: ['eth_getBalance'],
frequency: [frequency.onEveryBlock$, frequency.onStartup$],
name: 'balanceOf$',
pipes: api => [switchMapPromise(() => api.eth.getBalance(address))]
})(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, BigNumber | symbol>({
calls: ['eth_getTransactionCount'],
frequency: [frequency.onEveryBlock$, frequency.onStartup$],
name: 'transactionCountOf$',
pipes: api => [
switchMapPromise(() => api.eth.getTransactionCount(address))
]
})(options)(address);
}
/**
* Get the default account managed by the light client.
*
* @param options - Options to pass to {@link RpcObservableOptions}.
* @return - An Observable containing the public address
* of the default account.
*/
export function defaultAccount$ (options?: RpcObservableOptions) {
return createRpc$<Address[], Address>({
dependsOn: accounts$,
name: 'defaultAccount$',
pipes: () => [map(accounts => accounts[0])]
})(options)();
}
/**
* Get the current block number.
*
* @return {Observable<Number>} - An Observable containing the block height.
*/
export function blockNumber$ (options?: RpcObservableOptions) {
return createRpc$<Block, BigNumber>({
frequency: [frequency.onEveryBlock$],
name: 'blockNumber$',
pipes: () => [map(block => block.number)]
})(options)();
}
/**
* Shorthand for fetching the current account's balance.
*/
export function myBalance$ (options?: RpcObservableOptions) {
return createRpc$<Address, BigNumber | symbol>({
calls: ['eth_getBalance'],
dependsOn: defaultAccount$,
name: 'myBalance$',
pipes: () => [switchMap(defaultAccount => balanceOf$(defaultAccount))]
})(options)();
}
/**
* Get the syncStatus state.
*
* @return - An Observable containing the syncing state object, or false.
*/
export function syncStatus$ (options?: RpcObservableOptions) {
return createRpc$<object | boolean, object | boolean>({
frequency: [frequency.onSyncingChanged$],
name: 'syncStatus$'
})(options)();
}