|
8 | 8 | } from '../global/constants';
|
9 | 9 | import { logger } from '../global/logger';
|
10 | 10 | import { LibraryError, Provider } from '../provider';
|
11 |
| -import { ETransactionVersion, ETransactionVersion3 } from '../provider/types/spec.type'; |
| 11 | +import { BlockTag, ETransactionVersion, ETransactionVersion3 } from '../provider/types/spec.type'; |
12 | 12 | import { Signer, type SignerInterface } from '../signer';
|
13 | 13 | import {
|
14 | 14 | // Runtime values
|
@@ -58,6 +58,8 @@ import type {
|
58 | 58 | UniversalDetails,
|
59 | 59 | UserTransaction,
|
60 | 60 | waitForTransactionOptions,
|
| 61 | + fastWaitForTransactionOptions, |
| 62 | + fastExecuteResponse, |
61 | 63 | } from '../types';
|
62 | 64 | import { ETransactionType } from '../types/api';
|
63 | 65 | import { CallData } from '../utils/calldata';
|
@@ -88,6 +90,7 @@ import { assertPaymasterTransactionSafety } from '../utils/paymaster';
|
88 | 90 | import assert from '../utils/assert';
|
89 | 91 | import { defaultDeployer, Deployer } from '../deployer';
|
90 | 92 | import type { TipType } from '../provider/modules/tip';
|
| 93 | +import { RPC09 } from '../channel'; |
91 | 94 |
|
92 | 95 | export class Account extends Provider implements AccountInterface {
|
93 | 96 | public signer: SignerInterface;
|
@@ -332,6 +335,56 @@ export class Account extends Provider implements AccountInterface {
|
332 | 335 | );
|
333 | 336 | }
|
334 | 337 |
|
| 338 | + /** |
| 339 | + * Execute one or multiple calls through the account contract, |
| 340 | + * responding as soon as a new transaction is possible with the same account. |
| 341 | + * Useful for gaming usage. |
| 342 | + * - This method requires the provider to be initialized with `pre_confirmed` blockIdentifier option. |
| 343 | + * - Rpc 0.9 minimum. |
| 344 | + * - In a normal myAccount.execute() call, followed by myProvider.waitForTransaction(), you have an immediate access to the events and to the transaction report. Here, we are processing consecutive transactions faster, but events & transaction reports are not available immediately. |
| 345 | + * - As a consequence of the previous point, do not use contract/account deployment with this method. |
| 346 | + * @param {AllowArray<Call>} transactions - Single call or array of calls to execute |
| 347 | + * @param {UniversalDetails} [transactionsDetail] - Transaction execution options |
| 348 | + * @param {fastWaitForTransactionOptions} [waitDetail={retries: 50, retryInterval: 500}] - options to scan the network for the next possible transaction. `retries` is the number of times to retry, `retryInterval` is the time in ms between retries. |
| 349 | + * @returns {Promise<fastExecuteResponse>} Response containing the transaction result and status for the next transaction. If `isReady` is true, you can execute the next transaction. If false, timeout has been reached before the next transaction was possible. |
| 350 | + * @example |
| 351 | + * ```typescript |
| 352 | + * const myProvider = new RpcProvider({ nodeUrl: url, blockIdentifier: BlockTag.PRE_CONFIRMED }); |
| 353 | + * const myAccount = new Account({ provider: myProvider, address: accountAddress0, signer: privateKey0 }); |
| 354 | + * const resp = await myAccount.fastExecute( |
| 355 | + * call, { tip: recommendedTip}, |
| 356 | + * { retries: 30, retryInterval: 500 }); |
| 357 | + * // if resp.isReady is true, you can launch immediately a new tx. |
| 358 | + * ``` |
| 359 | + */ |
| 360 | + public async fastExecute( |
| 361 | + transactions: AllowArray<Call>, |
| 362 | + transactionsDetail: UniversalDetails = {}, |
| 363 | + waitDetail: fastWaitForTransactionOptions = {} |
| 364 | + ): Promise<fastExecuteResponse> { |
| 365 | + assert( |
| 366 | + this.channel instanceof RPC09.RpcChannel, |
| 367 | + 'Wrong Rpc version in Provider. At least Rpc v0.9 required.' |
| 368 | + ); |
| 369 | + assert( |
| 370 | + this.channel.blockIdentifier === BlockTag.PRE_CONFIRMED, |
| 371 | + 'Provider needs to be initialized with `pre_confirmed` blockIdentifier option.' |
| 372 | + ); |
| 373 | + const initNonce = BigInt( |
| 374 | + transactionsDetail.nonce ?? |
| 375 | + (await this.getNonceForAddress(this.address, BlockTag.PRE_CONFIRMED)) |
| 376 | + ); |
| 377 | + const details = { ...transactionsDetail, nonce: initNonce }; |
| 378 | + const resultTx: InvokeFunctionResponse = await this.execute(transactions, details); |
| 379 | + const resultWait = await this.fastWaitForTransaction( |
| 380 | + resultTx.transaction_hash, |
| 381 | + this.address, |
| 382 | + initNonce, |
| 383 | + waitDetail |
| 384 | + ); |
| 385 | + return { txResult: resultTx, isReady: resultWait } as fastExecuteResponse; |
| 386 | + } |
| 387 | + |
335 | 388 | /**
|
336 | 389 | * First check if contract is already declared, if not declare it
|
337 | 390 | * If contract already declared returned transaction_hash is ''.
|
|
0 commit comments