UNPKG

eulith-web3js

Version:

Official Eulith Typescript client library

200 lines (199 loc) 8.42 kB
import { TransactionConfig, TransactionReceipt } from "web3-eth"; import * as Eulith from "./index"; interface commitAndSendAndWaitArgs { timeoutMS?: number; extraTXParams2Merge?: TransactionConfig; } export declare module AtomicTx { export interface IAtomicTx { /** * Accumulate the argument transactionConfig into a server-side object which, * when this.commit() is called, will produce a single transaction which combines * the added sub-transactions. */ addTransaction(transactionConfig: TransactionConfig): Promise<string>; /** * Convenience method to add multiple transactions at one time */ addTransactions(txs: TransactionConfig[]): Promise<string[]>; readonly provider: Eulith.Provider; readonly atomicTxID: string; } /** * An atomic transaction is a bundle of transactions that all execute at the same time. * * This Transaction object is the root layer of the bundle, it may contain an arbitrary depth of nested transactions, * provided they don't exceed the gas limit of a block on the network. * * Key properties of atomicity: * o Everything fails or everything succeeds --> If at any point in your series of transactions something goes wrong, everything reverts. This is especially useful if you're making a trade that has upstream dependencies. * o Everything is executed as a single unit --> It's not possible for someone else to take action in the middle of your execution. The state of the blockchain is frozen at the start of your transaction and is only mutable by your own actions. As far as the blockchain is concerned, your atomic unit is a single transaction. * o More efficient usage of EVM storage --> Could result in cheaper transactions. * * Constructing the transaction object starts the transaction. * Calling commit() or rollback() renders this object unusable again, so * the caller should create a new one for a second transaction. * * Should the caller need to perform additional operations 'in the context' of the * transaction (such as calls to eulithSwapQuote), use the Eulith.Provider from * this.provider() */ export class Transaction implements IAtomicTx { /** * Begin an atomic transaction */ constructor({ provider, signer, tradingKeyAddress, safeAddress }: { provider?: Eulith.Provider | Eulith.Web3; tradingKeyAddress?: string; safeAddress?: string; signer?: Eulith.Signing.SigningService; }); /** * Accumulate the argument transactionConfig into a server-side object which, * when this.commit() is called, will produce a single transaction which combines * the added sub-transactions. */ addTransaction(transactionConfig: TransactionConfig): Promise<string>; /** * Convenience method to add multiple transactions at one time */ addTransactions(txs: TransactionConfig[]): Promise<string[]>; /** * The Eulith provider responsible for executing this transaction */ get provider(): Eulith.Provider; /** * The id of this atomic transaction */ get atomicTxID(): string; /** * Commits an atomic transaction and returns the serialized unit as a single transaction, * ready to be signed and sent to the network. */ commit(): Promise<TransactionConfig>; commitForEOAProxy(proxyAddress: string, sendingEoa: string): Promise<TransactionConfig>; /** * Commits an atomic transaction and prepares it to be signed by the UI key * as EIP712 Typed Data for execution via the ACE */ commitForAce(): Promise<string>; /** * Commits an atomic transaction, signs the payload, sends it to the blockchain, and waits for a * receipt all in one go. */ commitAndSendAndWait(args?: commitAndSendAndWaitArgs): Promise<TransactionReceipt>; /** * Clears the atomic transaction log with no further action */ rollback(): Promise<void>; getProviderAuthAddress(): string; readonly hasGnosis: boolean; private web3_; private inTransaction_; private readonly transactionId_; private readonly logger_; } /** * An atomic transaction can have a nested unit inside of it. * * This NestedTransaction is an atomic transaction that has its own scope inside an atomic transaction * * The typical use case of a Nested Transaction is a flash loan. Flash loans execute within an atomic transaction, * but must be individually opened and closed, with other actions taken inside each loan. * * For example, you could have a transaction that looks like this: * * Start AtomicTransaction * Start NestedTransaction(FlashLoan) A * Some action with loan A * Close NestedTransaction(FlashLoan) A * Start NestedTransaction(FlashLoan) B * Some action with loan B * Some other action with loan B * Close NestedTransaction(FlashLoan) B * Close AtomicTransaction */ export class NestedTransaction { /** * Start a new NestedTransaction * NestedTransactions can be nested inside an AtomicTransaction or inside another NestedTransaction */ constructor({ parentTx }: { parentTx: AtomicTx.Transaction | NestedTransaction; }); /** * The provider managing this NestedTransaction */ get provider(): Eulith.Provider; /** * Accumulate the argument transactionConfig into a server-side object which, * when this.commit() is called, will produce a single transaction which combines * the added sub-transactions. * * Within the scope of a NestedTransaction, the single transaction resulting from this.commit() * will not be returned immediately; it will be added to the outer transaction scope and eventually * returned when you commit the root level Atomic Transaction */ addTransaction(transactionConfig: TransactionConfig): Promise<string>; /** * Convenience method for adding multiple transactions at once */ addTransactions(txs: TransactionConfig[]): Promise<string[]>; /** * Complete (atomically) this sub-step of the parent atomic transaction. * * This doesn't actually do the step, but marks as completed the sequence of operations * and returns the (1-based) index of this step in the parent atomic transaction. */ commit(): Promise<number>; private web3_; } export interface CommitOptions { trace?: boolean; light_simulation?: string; } export type BundleTransaction = BundleTransactionNormal; export interface BundleTransactionNormal { type: 'normal'; data: BundleTransactionConfig; } export interface BundleTransactionConfig extends TransactionConfig { safeAddress: string; } export function bundleAndCommit(provider: Eulith.Provider, authAddress: string, transactions: BundleTransaction[], commitOptions?: CommitOptions): Promise<TransactionConfig>; interface AceImmediateTx { chain_id: number; nonce: string; max_priority_fee_per_gas: string; max_fee_per_gas: string; gas_limit: string; data: string; } export function getAceImmediateTxTypedData(aceImmediateTx: AceImmediateTx): { types: { EIP712Domain: { name: string; type: string; }[]; AceImmediateTx: { name: string; type: string; }[]; }; primaryType: string; domain: { name: string; version: string; }; message: { chainId: number; nonce: string; maxPriorityFeePerGas: string; maxFeePerGas: string; gasLimit: string; data: string; }; }; export {}; } export {};