Skip to content

Commit

Permalink
feat: simulate_transaction support on sequencer
Browse files Browse the repository at this point in the history
  • Loading branch information
ivpavici committed Nov 15, 2022
1 parent ad2e55a commit 304c3cc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
24 changes: 22 additions & 2 deletions src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import {
CallL1Handler,
GetContractAddressesResponse,
GetTransactionStatusResponse,
GetTransactionTraceResponse,
Sequencer,
TransactionTraceResponse,
} from '../types/api';
import { DeclareContractTransaction, DeployAccountContractTransaction } from '../types/lib';
import fetch from '../utils/fetchPonyfill';
Expand Down Expand Up @@ -139,6 +139,7 @@ export class SequencerProvider implements ProviderInterface {
'call_contract',
'estimate_fee',
'estimate_message_fee',
'simulate_transaction',
];

return postMethodEndpoints.includes(endpoint) ? 'POST' : 'GET';
Expand Down Expand Up @@ -500,7 +501,7 @@ export class SequencerProvider implements ProviderInterface {
* @param txHash
* @returns the transaction trace
*/
public async getTransactionTrace(txHash: BigNumberish): Promise<GetTransactionTraceResponse> {
public async getTransactionTrace(txHash: BigNumberish): Promise<TransactionTraceResponse> {
const txHashHex = toHex(toBN(txHash));
return this.fetchEndpoint('get_transaction_trace', { transactionHash: txHashHex });
}
Expand All @@ -518,4 +519,23 @@ export class SequencerProvider implements ProviderInterface {

return this.fetchEndpoint('estimate_message_fee', { blockIdentifier }, validCallL1Handler);
}

public async simulateTransaction(
invocation: Invocation,
invocationDetails: InvocationsDetailsWithNonce,
blockIdentifier: BlockIdentifier = 'pending'
): Promise<Sequencer.TransactionSimulationResponse> {
return this.fetchEndpoint(
'simulate_transaction',
{ blockIdentifier },
{
type: 'INVOKE_FUNCTION',
contract_address: invocation.contractAddress,
calldata: invocation.calldata ?? [],
signature: bigNumberishArrayToDecimalStringArray(invocation.signature || []),
version: toHex(toBN(invocationDetails?.version || 1)),
nonce: toHex(toBN(invocationDetails.nonce)),
}
);
}
}
19 changes: 17 additions & 2 deletions src/types/api/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export type ExecutionResources = {
n_memory_holes: number;
};

export type GetTransactionTraceResponse = {
export type TransactionTraceResponse = {
validate_invocation?: FunctionInvocation;
function_invocation?: FunctionInvocation;
fee_transfer_invocation?: FunctionInvocation;
Expand Down Expand Up @@ -237,12 +237,20 @@ export namespace Sequencer {
| DeployEstimateFee
| DeployAccountEstimateFee;

export type TransactionSimulationResponse = {
trace: TransactionTraceResponse;
fee_estimation: Sequencer.EstimateFeeResponse;
};

export type SimulateTransaction = Omit<InvokeFunctionTransaction, 'max_fee' | 'entry_point_type'>;

// Support 0.9.1 changes in a backward-compatible way
export type EstimateFeeResponse =
| {
overall_fee: number;
gas_price: number;
gas_usage: number;
uint: string;
}
| {
amount: BN;
Expand Down Expand Up @@ -279,7 +287,7 @@ export namespace Sequencer {
transactionHash: string;
};
REQUEST: never;
RESPONSE: GetTransactionTraceResponse;
RESPONSE: TransactionTraceResponse;
};
get_transaction_receipt: {
QUERY: {
Expand Down Expand Up @@ -369,5 +377,12 @@ export namespace Sequencer {
REQUEST: any;
RESPONSE: EstimateFeeResponse;
};
simulate_transaction: {
QUERY: {
blockIdentifier: BlockIdentifier;
};
REQUEST: SimulateTransaction;
RESPONSE: TransactionSimulationResponse;
};
};
}
19 changes: 17 additions & 2 deletions www/docs/API/provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,11 @@ Gets the status of a transaction.

<hr/>

provider.**getTransactionTrace**(txHash) => _Promise < GetTransactionTraceResponse >_
provider.**getTransactionTrace**(txHash) => _Promise < TransactionTraceResponse >_

Gets the transaction trace from a tx hash.

###### _GetTransactionTraceResponse_
###### _TransactionTraceResponse_

```typescript
{
Expand Down Expand Up @@ -391,6 +391,21 @@ Gets the transaction trace from a tx hash.
}
```

<hr/>

provider.**simulateTransaction**(invocationWithTxType, invocationDetails, blockIdentifier) => _Promise < TransactionSimulationResponse >_

Simulate transaction execution.

###### _TransactionSimulationResponse_

```typescript
{
trace: TransactionTraceResponse;
fee_estimation: EstimateFeeResponse;
}
```

## RpcProvider

### Creating an instance
Expand Down

0 comments on commit 304c3cc

Please sign in to comment.