Skip to content

Commit

Permalink
feat: update simulate tx in sequencer
Browse files Browse the repository at this point in the history
  • Loading branch information
irisdv committed Dec 21, 2022
1 parent 8f3ea55 commit d79a9fa
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 88 deletions.
10 changes: 0 additions & 10 deletions __tests__/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,6 @@ describe('deploy and test Wallet', () => {
innerInvokeEstFeeSpy.mockClear();
});

test('simulate transaction', async () => {
const res = await account.simulateTransaction({
contractAddress: erc20Address,
entrypoint: 'transfer',
calldata: [erc20.address, '10', '0'],
});
expect(res).toHaveProperty('fee_estimation');
expect(res).toHaveProperty('trace');
});

test('read balance of wallet', async () => {
const x = await erc20.balanceOf(account.address);

Expand Down
40 changes: 38 additions & 2 deletions __tests__/sequencerProvider.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Contract, Provider, SequencerProvider, stark } from '../src';
import { ZERO } from '../src/constants';
import { feeTransactionVersion } from '../src/utils/hash';
import { toBN } from '../src/utils/number';
import { encodeShortString } from '../src/utils/shortString';
import { fromCallsToExecuteCalldata } from '../src/utils/transaction';
import {
compiledErc20,
compiledL1L2,
Expand All @@ -19,13 +22,16 @@ describeIfSequencer('SequencerProvider', () => {

describe('Gateway specific methods', () => {
let exampleTransactionHash: string;
const wallet = stark.randomAddress();

beforeAll(async () => {
const { deploy } = await account.declareDeploy({
contract: compiledErc20,
classHash: '0x54328a1075b8820eb43caf0caa233923148c983742402dcfc38541dd843d01a',
constructorCalldata: [encodeShortString('Token'), encodeShortString('ERC20'), wallet],
constructorCalldata: [
encodeShortString('Token'),
encodeShortString('ERC20'),
account.address,
],
});

exampleTransactionHash = deploy.transaction_hash;
Expand All @@ -44,6 +50,36 @@ describeIfSequencer('SequencerProvider', () => {
expect(transactionTrace).toHaveProperty('signature');
});

test('simulate transaction', async () => {
const call = {
contractAddress: exampleContractAddress,
entrypoint: 'transfer',
calldata: [exampleContractAddress, '10', '0'],
};
const calldata = fromCallsToExecuteCalldata([call]);
const nonce = toBN(await account.getNonce());
const version = toBN(feeTransactionVersion);
const chainId = await account.getChainId();
const signature = await account.signer.signTransaction([call], {
walletAddress: account.address,
nonce,
version,
maxFee: ZERO,
chainId,
});

const res = await sequencerProvider.simulateTransaction(
{
contractAddress: account.address,
calldata,
signature,
},
{ version, nonce }
);
expect(res).toHaveProperty('trace');
expect(res).toHaveProperty('fee_estimation');
});

test('getCode() -> { bytecode }', async () => {
const code = await sequencerProvider.getCode(exampleContractAddress);
return expect(Array.isArray(code.bytecode)).toBe(true);
Expand Down
33 changes: 0 additions & 33 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
InvokeFunctionResponse,
KeyPair,
MultiDeployContractResponse,
Sequencer,
Signature,
UniversalDeployerContractPayload,
} from '../types';
Expand Down Expand Up @@ -499,36 +498,4 @@ export class Account extends Provider implements AccountInterface {

return feeEstimate.suggestedMaxFee.toString();
}

public async simulateTransaction(
calls: AllowArray<Call>,
{ nonce: providedNonce, blockIdentifier }: EstimateFeeDetails = {}
): Promise<Sequencer.TransactionSimulationResponse> {
const transactions = Array.isArray(calls) ? calls : [calls];
const nonce = toBN(providedNonce ?? (await this.getNonce()));
const version = toBN(feeTransactionVersion);
const chainId = await this.getChainId();

const signerDetails: InvocationsSignerDetails = {
walletAddress: this.address,
nonce,
maxFee: ZERO,
version,
chainId,
};

const signature = await this.signer.signTransaction(transactions, signerDetails);

const calldata = fromCallsToExecuteCalldata(transactions);
const response: any = await super.getSimulateTransaction(
{ contractAddress: this.address, calldata, signature },
{ version, nonce },
blockIdentifier
);

const suggestedMaxFee = estimatedFeeToMaxFee(response.fee_estimation.overall_fee);
response.fee_estimation.suggestedMaxFee = suggestedMaxFee;

return response;
}
}
16 changes: 0 additions & 16 deletions src/account/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
InvocationsDetails,
InvokeFunctionResponse,
MultiDeployContractResponse,
Sequencer,
Signature,
UniversalDeployerContractPayload,
} from '../types';
Expand Down Expand Up @@ -309,19 +308,4 @@ export abstract class AccountInterface extends ProviderInterface {
estimateFeeAction: EstimateFeeAction,
details: EstimateFeeDetails
): Promise<BigNumberish>;

/**
* Estimate Fee for executing an INVOKE transaction on starknet
*
* @param calls the invocation object containing:
* - contractAddress - the address of the contract
* - entrypoint - the entrypoint of the contract
* - calldata - (defaults to []) the calldata
*
* @returns response from estimate_fee
*/
public abstract simulateTransaction(
calls: AllowArray<Call>,
estimateFeeDetails?: EstimateFeeDetails
): Promise<Sequencer.TransactionSimulationResponse>;
}
9 changes: 0 additions & 9 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
Invocation,
InvocationsDetailsWithNonce,
InvokeFunctionResponse,
Sequencer,
Status,
} from '../types';
import { BigNumberish } from '../utils/number';
Expand Down Expand Up @@ -185,12 +184,4 @@ export class Provider implements ProviderInterface {
): Promise<GetTransactionReceiptResponse> {
return this.provider.waitForTransaction(txHash, retryInterval, successStates);
}

public async getSimulateTransaction(
invocation: Invocation,
invocationDetails: InvocationsDetailsWithNonce,
blockIdentifier?: BlockIdentifier
): Promise<Sequencer.TransactionSimulationResponse> {
return this.provider.getSimulateTransaction(invocation, invocationDetails, blockIdentifier);
}
}
8 changes: 0 additions & 8 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import type {
Invocation,
InvocationsDetailsWithNonce,
InvokeFunctionResponse,
Sequencer,
Status,
} from '../types';
import type { BigNumberish } from '../utils/number';
Expand Down Expand Up @@ -279,11 +278,4 @@ export abstract class ProviderInterface {
retryInterval?: number,
successStates?: Array<Status>
): Promise<GetTransactionReceiptResponse>;

// todo documentation
public abstract getSimulateTransaction(
invocation: Invocation,
invocationDetails: InvocationsDetailsWithNonce,
blockIdentifier?: BlockIdentifier
): Promise<Sequencer.TransactionSimulationResponse>;
}
9 changes: 0 additions & 9 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
InvocationsDetailsWithNonce,
InvokeFunctionResponse,
RPC,
Sequencer,
} from '../types';
import fetch from '../utils/fetchPonyfill';
import { getSelectorFromName } from '../utils/hash';
Expand Down Expand Up @@ -482,12 +481,4 @@ export class RpcProvider implements ProviderInterface {
public async getEvents(eventFilter: RPC.EventFilter): Promise<RPC.GetEventsResponse> {
return this.fetchEndpoint('starknet_getEvents', { filter: eventFilter });
}

public async getSimulateTransaction(
_invocation: Invocation,
_invocationDetails: InvocationsDetailsWithNonce,
_blockIdentifier?: BlockIdentifier
): Promise<Sequencer.TransactionSimulationResponse> {
throw new Error('RPC does not implement simulateTransaction function');
}
}
2 changes: 1 addition & 1 deletion src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ export class SequencerProvider implements ProviderInterface {
return this.fetchEndpoint('estimate_message_fee', { blockIdentifier }, validCallL1Handler);
}

public async getSimulateTransaction(
public async simulateTransaction(
invocation: Invocation,
invocationDetails: InvocationsDetailsWithNonce,
blockIdentifier: BlockIdentifier = this.blockIdentifier
Expand Down

0 comments on commit d79a9fa

Please sign in to comment.