UNPKG

eulith-web3js-core

Version:

Eulith core web3js SDK (code to access Eulith services via web3js)

101 lines (100 loc) 5.27 kB
import Web3 from "web3"; import { TransactionConfig, TransactionReceipt } from "web3-eth"; import * as Eulith from "../src/index"; /** * @typedef Eulith.Web3 * * This can be used to access Eulith-specific APIs, but often all that is needed * is Eulith.Provider, and that can be used directly with a web3 js object. * * @todo For now - extend Web3 the way the python code does. Perhaps do a class for just eulith-specific methods * or perhaps find a more object oriented path (like we did for AtomicTx) * * Also note - this API deviates from the web3js and etherjs patterns in one important respect: * their sendTransaction API returns a promise which resolves * <https://web3js.readthedocs.io/en/v1.2.11/web3-eth.html#sendtransaction> 'when the transaction receipt is available.' * (oversimplifying, PromiEvent) * * This causes a number of problems when using the Eulith atomic transactions, because we NEVER generate those transaction receipts. * and by default, web3js keeps a waiter around polling (at least on http and in some ways as bad with websocket/etc). It causes * false positive errors to be reported from various channels (e.g. automation tests). And wastes resources. * * So we follow the web3.py pattern, and return a Promise<HASH TAG> instead (eulith_send_and_sign_transaction). * * Then, the caller may poll wait with the that hash * await ew3.eth.getTransactionReceipt(txHash) * to get the receipt. * * Another 'deviation' to report/note, is that the python API has the (exposed in the API) concept of 'signing middleware'. * EVEN IF this makes sense in the python implementation (I doubt), I propose changing the public API instead to take a * 'signer' object. And have that 'signer' object ONLY be used in the context of a new proposed method - eulith_send_and_sign_transaction * - and have that do the signing, INSTEAD of the hooking of eth.send_transaction. * * The current behavior of the python code is quite idiosyncratic, and confusing, with respect to WHEN the send_transaction calls * are intercepted and signed (in context of an atomic transaction changes things, as does from/to address/presence of signature etc). * * Perhaps better to have an explicit method eulith_send_and_sign_transaction so you know you are signing (seems like the API flows * require the caller to know / understand this anyhow). * */ export declare class EulithWeb3 extends Web3 { /** * @constructor * @param provider - this is a web3js provider, plus supports eulith apis * @param disable_transaction_polling defaults true * @param signer - modifies the behavior of a number of eulith apis (not web3.eth apis) - to include the signers address - but mostly for eulith_send_and_sign_transaction * * @todo discuss with Moh/Kristian if it might be cleaner to make ISigner a parameter to those APIs? So this subtlty is clear */ constructor({ provider, signer, disable_transaction_polling }: { provider: Eulith.Provider; signer?: Eulith.Signing.ICryptographicSigner | Eulith.Signing.SigningService; disable_transaction_polling?: boolean; }); /** * The signer is an optional property of a web3 object, so this can return null */ get signer(): Eulith.Signing.SigningService | undefined; /** * Fetch the logger associate dwith this web3 object. */ get logger(): Eulith.Logging.ILogger; /** * @todo CONSIDER DEPRECATING * * delegate to this.eth.sendTransaction, but just return the transaction hash; * * to get the txReceipt, CALL * const txReceipt: TransactionReceipt = await ew3.eth.getTransactionReceipt(txHash); * @param transactionConfig * @returns * * This method is PERHAPS not useful, since there is probably no point in ever sending unsigned transactions * (besides our use in transactions, which could be done differently, and certainly doesnt require this to be public) */ eulith_send_unsigned_transaction(transactionConfig: TransactionConfig, extraURLQueryParams?: { [key: string]: string; }): Promise<string>; eulith_swap_quote(params: Eulith.Swaps.Request): Promise<[number, TransactionConfig[]]>; /** * Send a series of transactions (eulith_send_and_sign_transaction), and wait for each to be confirmed, * returning the transaction receipts of each. * * At any point, one of them could fail, with some having gone on to completion. This process aborts at that time * and offers no clues about how many were processed (could include the completed ones in a special exception) * * @param txs * @returns */ eulith_send_multi_transaction(txs: TransactionConfig[]): Promise<TransactionReceipt[]>; /** * Returns the provider object associated with this transaction. * * @todo NOTE due to quirks of how we implement signing, and cloning etc, this might not be the same * provider, but instead one derieved from - the original one provided in the constructor. */ get provider(): Eulith.Provider; private signer_?; private logger_; private providerForAPIsRequiringAuthAddressField_; }