UNPKG

9.39 kBTypeScriptView Raw
1import { Account, AddressString, Block, ClaimTransaction, Hash256String, InvocationTransaction, NetworkType, RawCallReceipt, SmartContractDefinition, Transaction, TransactionOptions, TransactionReceipt, TransactionResult, Transfer, UpdateAccountNameOptions, UserAccount, UserAccountID, UserAccountProvider, UserAccountProviders } from '@neo-one/client-common';
2import { Monitor } from '@neo-one/monitor';
3import BigNumber from 'bignumber.js';
4import { BehaviorSubject, Observable } from 'rxjs';
5import { AsyncParallelHook } from 'tapable';
6import { SmartContract, SmartContractAny } from './types';
7/**
8 * Object which contains the points that can be hooked into on the `Client`.
9 */
10export interface ClientHooks {
11 /**
12 * Called before the `Transaction` is relayed.
13 */
14 readonly beforeRelay: AsyncParallelHook<TransactionOptions>;
15 /**
16 * Called when there is an `Error` thrown during relaying a `Transaction`.
17 */
18 readonly relayError: AsyncParallelHook<Error>;
19 /**
20 * Called after successfully relaying a `Transaction`.
21 */
22 readonly afterRelay: AsyncParallelHook<Transaction>;
23 /**
24 * Called when the `confirmed` method of a `TransactionResult` is invoked.
25 */
26 readonly beforeConfirmed: AsyncParallelHook<Transaction>;
27 /**
28 * Called when there is an `Error` thrown during the `confirmed` method of a `TransactionResult`.
29 */
30 readonly confirmedError: AsyncParallelHook<Transaction, Error>;
31 /**
32 * Called after the `confirmed` method of a `TransactionResult` resolves.
33 */
34 readonly afterConfirmed: AsyncParallelHook<Transaction, TransactionReceipt>;
35 /**
36 * Called after a constant method is invoked.
37 */
38 readonly afterCall: AsyncParallelHook<RawCallReceipt>;
39 /**
40 * Called when an `Error` is thrown from a constant method invocation.
41 */
42 readonly callError: AsyncParallelHook<Error>;
43}
44/**
45 * Properties represent features that a given `UserAccountID` supports.
46 */
47export interface UserAccountFeatures {
48 /**
49 * `true` if the `UserAccountID` can be deleted.
50 */
51 readonly delete: boolean;
52 /**
53 * `true` if the `name` of the `UserAccount` associated with the `UserAccountID` can be updated.
54 */
55 readonly updateName: boolean;
56}
57/**
58 * `Client#block$` `Observable` item.
59 */
60export interface BlockEntry {
61 /**
62 * Emitted block.
63 */
64 readonly block: Block;
65 /**
66 * Network of the block.
67 */
68 readonly network: NetworkType;
69}
70/**
71 * `Client#accountState$` `Observable` item.
72 */
73export interface AccountStateEntry {
74 /**
75 * Currently selected `UserAccount`
76 */
77 readonly currentUserAccount: UserAccount;
78 /**
79 * Blockchain account info for `currentUserAccount`
80 */
81 readonly account: Account;
82}
83/**
84 * Main entrypoint to the `@neo-one/client` APIs. The `Client` class abstracts away user accounts and even how those accounts are provided to your dapp, for example, they might come from an extension like NEX, dapp browser like nOS or through some other integration.
85 *
86 * See the [Client APIs](https://neo-one.io/docs/client-apis) chapter of the main guide for more information.
87 */
88export declare class Client<TUserAccountProvider extends UserAccountProvider = any, TUserAccountProviders extends UserAccountProviders<TUserAccountProvider> = any> {
89 /**
90 * Hook into the lifecycle of various requests. Can be used to automatically add logging, or parameter transformations across the application, for example.
91 */
92 readonly hooks: ClientHooks;
93 /**
94 * Emits a value whenever a new user account is selected.
95 *
96 * Immediately emits the latest value when subscribed to.
97 */
98 readonly currentUserAccount$: Observable<UserAccount | undefined>;
99 /**
100 * Emits a value whenever a new list of user accounts is available.
101 *
102 * Immediately emits the latest value when subscribed to.
103 */
104 readonly userAccounts$: Observable<readonly UserAccount[]>;
105 /**
106 * Emits a value whenever a new network is selected.
107 *
108 * Immediately emits the latest value when subscribed to.
109 */
110 readonly currentNetwork$: Observable<NetworkType>;
111 /**
112 * Emits a value whenever a new list of networks user account is available.
113 *
114 * Immediately emits the latest value when subscribed to.
115 */
116 readonly networks$: Observable<readonly NetworkType[]>;
117 protected readonly providers$: BehaviorSubject<TUserAccountProviders>;
118 protected readonly selectedProvider$: BehaviorSubject<TUserAccountProvider>;
119 private readonly currentNetworkInternal$;
120 private readonly reset$;
121 /**
122 * Emits a value whenever a block is persisted to the blockchain.
123 *
124 * Immediately emits the latest block/network when subscribed to.
125 */
126 readonly block$: Observable<BlockEntry>;
127 /**
128 * Emits a value whenever a new user account is selected and whenever a block is persisted to the blockchain.
129 *
130 * Immediately emits the latest value when subscribed to.
131 */
132 readonly accountState$: Observable<AccountStateEntry | undefined>;
133 constructor(providersIn: TUserAccountProviders);
134 /**
135 * The configured `UserAccountProvider`s for this `Client` instance.
136 */
137 readonly providers: TUserAccountProviders;
138 /**
139 * Get the details of the `UserAccount` for a given `UserAccountID`.
140 *
141 * @param idIn `UserAccountID` to find the `UserAccount` for
142 * @returns `UserAccount` or throws an `UnknownAccountError` if one could not be found.
143 */
144 getUserAccount(idIn: UserAccountID): UserAccount;
145 /**
146 * Sets a `UserAccountID` as the currently selected `UserAccountID`.
147 *
148 * @param idIn `UserAccountID` to select, or `undefined` to deselect the current `UserAccountID`.
149 */
150 selectUserAccount(idIn?: UserAccountID): Promise<void>;
151 /**
152 * Sets a `NetworkType` as the currently selected `NetworkType`.
153 *
154 * @param networkIn `NetworkType` to select.
155 */
156 selectNetwork(networkIn: NetworkType): Promise<void>;
157 /**
158 * @returns `Promise` which resolves to the `UserAccountFeatures` supported by the given `UserAccountID`.
159 */
160 getSupportedFeatures(idIn: UserAccountID): Promise<UserAccountFeatures>;
161 /**
162 * Deletes the `UserAccountID` from its underlying provider. Throws an `DeleteUserAccountUnsupportedError` if the operation is unsupported.
163 *
164 * Users should check `getSupportedFeatures` before calling this method.
165 */
166 deleteUserAccount(idIn: UserAccountID): Promise<void>;
167 /**
168 * Updates the name of the `UserAccountID` in the underlying provider. Throws an `UpdateUserAccountUnsupportedError` if the operation is unsupported.
169 *
170 * Users should check `getSupportedFeatures` before calling this method.
171 */
172 updateUserAccountName(options: UpdateAccountNameOptions): Promise<void>;
173 /**
174 * @returns the currently selected `UserAccount` or `undefined` if there are no `UserAccount`s.
175 */
176 getCurrentUserAccount(): UserAccount | undefined;
177 /**
178 * @returns the currently selected `NetworkType`
179 */
180 getCurrentNetwork(): NetworkType;
181 /**
182 * @returns a list of all available `UserAccount`s
183 */
184 getUserAccounts(): readonly UserAccount[];
185 /**
186 * @returns a list of all available `NetworkType`s
187 */
188 getNetworks(): readonly NetworkType[];
189 /**
190 * Constructs a `SmartContract` instance for the provided `definition` backed by this `Client` instance.
191 */
192 smartContract<T extends SmartContract<any, any> = SmartContractAny>(definition: SmartContractDefinition): T;
193 /**
194 * Transfer native assets in the specified amount(s) to the specified Address(es).
195 *
196 * Accepts either a single transfer or an array of transfer objects.
197 *
198 * Note that we use an `InvocationTransaction` for transfers in order to reduce the overall bundle size since they can be used equivalently to `ContractTransaction`s.
199 *
200 * @returns `Promise<TransactionResult<TransactionReceipt, InvocationTransaction>>`.
201 */
202 transfer(amount: BigNumber, asset: Hash256String, to: AddressString, options?: TransactionOptions): Promise<TransactionResult<TransactionReceipt, InvocationTransaction>>;
203 transfer(transfers: readonly Transfer[], options?: TransactionOptions): Promise<TransactionResult>;
204 /**
205 * Claim all available unclaimed `GAS` for the currently selected account (or the specified `from` `UserAccountID`).
206 */
207 claim(optionsIn?: TransactionOptions): Promise<TransactionResult<TransactionReceipt, ClaimTransaction>>;
208 /**
209 * @returns `Promise` which resolves to an `Account` object for the provided `UserAccountID`.
210 */
211 getAccount(id: UserAccountID, monitor?: Monitor): Promise<Account>;
212 protected getProvider(options?: TransactionOptions): TUserAccountProvider;
213 protected getNetworkProvider(network: NetworkType): TUserAccountProvider;
214 protected applyBeforeRelayHook(options: TransactionOptions): Promise<void>;
215 protected addTransactionHooks<TTransactionResult extends TransactionResult>(res: Promise<TTransactionResult>): Promise<TTransactionResult>;
216 protected getTransfersOptions(argsIn: readonly any[]): {
217 readonly transfers: readonly Transfer[];
218 readonly options: TransactionOptions;
219 };
220}