Skip to content

Commit

Permalink
Merge pull request #879 from starknet-io/v6/v3-transactions
Browse files Browse the repository at this point in the history
feat: v3 wip with est
  • Loading branch information
tabaktoni authored Dec 7, 2023
2 parents 9934e4f + b95c462 commit bda0d9f
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 54 deletions.
167 changes: 119 additions & 48 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
TypedData,
UniversalDeployerContractPayload,
} from '../types';
import { ETransactionVersion, ETransactionVersion3 } from '../types/api';
import { ETransactionVersion, ETransactionVersion3, ResourceBounds } from '../types/api';
import { CallData } from '../utils/calldata';
import { extractContractHashes, isSierra } from '../utils/contract';
import { starkCurve } from '../utils/ec';
Expand Down Expand Up @@ -161,7 +161,8 @@ export class Account extends Provider implements AccountInterface {
const estimateFeeResponse = await super.getInvokeEstimateFee(
{ ...invocation },
{ ...v3Details(details), version, nonce },
blockIdentifier
blockIdentifier,
details.skipValidate
);

return {
Expand Down Expand Up @@ -201,7 +202,8 @@ export class Account extends Provider implements AccountInterface {
const estimateFeeResponse = await super.getDeclareEstimateFee(
declareContractTransaction,
{ ...v3Details(details), version, nonce },
blockIdentifier
blockIdentifier,
details.skipValidate
);

return {
Expand Down Expand Up @@ -244,7 +246,8 @@ export class Account extends Provider implements AccountInterface {
const estimateFeeResponse = await super.getDeployAccountEstimateFee(
{ ...payload },
{ ...v3Details(details), version, nonce },
blockIdentifier
blockIdentifier,
details.skipValidate
);

return {
Expand Down Expand Up @@ -279,6 +282,7 @@ export class Account extends Provider implements AccountInterface {

const EstimateFeeResponseBulk = await super.getEstimateFeeBulk(accountInvocations, {
blockIdentifier,
skipValidate: details.skipValidate,
});

return [].concat(EstimateFeeResponseBulk as []).map((elem: EstimateFeeResponse) => {
Expand Down Expand Up @@ -316,23 +320,43 @@ export class Account extends Provider implements AccountInterface {
this.getPreferredVersion(ETransactionVersion.V1, ETransactionVersion.V3), // TODO: does this depend on cairo version ?
details.version
);
const maxFee =
details.maxFee ??
(await this.getSuggestedMaxFee(
{ type: TransactionType.INVOKE, payload: calls },
{
...details,
version,
}
));

let suggestedMaxFee: BigNumberish = 0;
let resourceBounds: ResourceBounds = estimateFeeToBounds(ZERO);
if (version === ETransactionVersion.V3) {
resourceBounds =
details.resourceBounds ??
(
await this.getSuggestedFee(
{ type: TransactionType.INVOKE, payload: calls },
{
...details,
version,
}
)
).resourceBounds;
} else {
suggestedMaxFee =
details.maxFee ??
(
await this.getSuggestedFee(
{ type: TransactionType.INVOKE, payload: calls },
{
...details,
version,
}
)
).suggestedMaxFee;
}

const chainId = await this.getChainId();

const signerDetails: InvocationsSignerDetails = {
...v3Details(details),
resourceBounds,
walletAddress: this.address,
nonce,
maxFee,
maxFee: suggestedMaxFee,
version,
chainId,
cairoVersion: await this.getCairoVersion(),
Expand All @@ -346,8 +370,9 @@ export class Account extends Provider implements AccountInterface {
{ contractAddress: this.address, calldata, signature },
{
...v3Details(details),
resourceBounds,
nonce,
maxFee,
maxFee: suggestedMaxFee,
version,
}
);
Expand Down Expand Up @@ -388,21 +413,45 @@ export class Account extends Provider implements AccountInterface {
providedVersion
);

let suggestedMaxFee: BigNumberish = 0;
let resourceBounds: ResourceBounds = estimateFeeToBounds(ZERO);
if (version === ETransactionVersion.V3) {
resourceBounds =
details.resourceBounds ??
(
await this.getSuggestedFee(
{
type: TransactionType.DECLARE,
payload: declareContractPayload,
},
{
...details,
version,
}
)
).resourceBounds;
} else {
suggestedMaxFee =
maxFee ??
(
await this.getSuggestedFee(
{
type: TransactionType.DECLARE,
payload: declareContractPayload,
},
{
...details,
version,
}
)
).suggestedMaxFee;
}

const declareDetails: InvocationsSignerDetails = {
...v3Details(details),
resourceBounds,
maxFee: suggestedMaxFee,
nonce: toBigInt(nonce ?? (await this.getNonce())),
maxFee:
maxFee ??
(await this.getSuggestedMaxFee(
{
type: TransactionType.DECLARE,
payload: declareContractPayload,
},
{
...details,
version,
}
)),
version,
chainId: await this.getChainId(),
walletAddress: this.address,
Expand Down Expand Up @@ -512,20 +561,43 @@ export class Account extends Provider implements AccountInterface {
providedContractAddress ??
calculateContractAddressFromHash(addressSalt, classHash, compiledCalldata, 0);

const maxFee =
details.maxFee ??
(await this.getSuggestedMaxFee(
{
type: TransactionType.DEPLOY_ACCOUNT,
payload: {
classHash,
constructorCalldata: compiledCalldata,
addressSalt,
contractAddress,
},
},
details
));
let suggestedMaxFee: BigNumberish = 0;
let resourceBounds: ResourceBounds = estimateFeeToBounds(ZERO);
if (version === ETransactionVersion.V3) {
resourceBounds =
details.resourceBounds ??
(
await this.getSuggestedFee(
{
type: TransactionType.DEPLOY_ACCOUNT,
payload: {
classHash,
constructorCalldata: compiledCalldata,
addressSalt,
contractAddress,
},
},
details
)
).resourceBounds;
} else {
suggestedMaxFee =
details.maxFee ??
(
await this.getSuggestedFee(
{
type: TransactionType.DEPLOY_ACCOUNT,
payload: {
classHash,
constructorCalldata: compiledCalldata,
addressSalt,
contractAddress,
},
},
details
)
).suggestedMaxFee;
}

const signature = await this.signer.signDeployAccountTransaction({
...v3Details(details),
Expand All @@ -534,7 +606,8 @@ export class Account extends Provider implements AccountInterface {
contractAddress,
addressSalt,
chainId,
maxFee,
resourceBounds,
maxFee: suggestedMaxFee,
version,
nonce,
});
Expand All @@ -544,7 +617,8 @@ export class Account extends Provider implements AccountInterface {
{
...v3Details(details),
nonce,
maxFee,
resourceBounds,
maxFee: suggestedMaxFee,
version,
}
);
Expand Down Expand Up @@ -579,10 +653,7 @@ export class Account extends Provider implements AccountInterface {
return this.verifyMessageHash(hash, signature);
}

public async getSuggestedMaxFee(
{ type, payload }: EstimateFeeAction,
details: EstimateFeeDetails
) {
public async getSuggestedFee({ type, payload }: EstimateFeeAction, details: EstimateFeeDetails) {
let feeEstimate: EstimateFee;

switch (type) {
Expand Down Expand Up @@ -611,7 +682,7 @@ export class Account extends Provider implements AccountInterface {
break;
}

return feeEstimate.suggestedMaxFee;
return feeEstimate;
}

/**
Expand Down
11 changes: 6 additions & 5 deletions src/account/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
DeployAccountContractPayload,
DeployContractResponse,
DeployContractUDCResponse,
EstimateFee,
EstimateFeeAction,
EstimateFeeDetails,
EstimateFeeResponse,
Expand Down Expand Up @@ -166,7 +167,7 @@ export abstract class AccountInterface extends ProviderInterface {
* @param details -
* - [nonce=getNonce]
* - [version=transactionVersion]
* - [maxFee=getSuggestedMaxFee]
* - [maxFee=getSuggestedFee]
* @returns
* - contract_address[]
* - transaction_hash
Expand All @@ -188,7 +189,7 @@ export abstract class AccountInterface extends ProviderInterface {
* @param details -
* - [nonce=getNonce]
* - [version=transactionVersion]
* - [maxFee=getSuggestedMaxFee]
* - [maxFee=getSuggestedFee]
* @returns
* - contract_address
* - transaction_hash
Expand Down Expand Up @@ -221,7 +222,7 @@ export abstract class AccountInterface extends ProviderInterface {
* @param details
* - [nonce=getNonce]
* - [version=transactionVersion]
* - [maxFee=getSuggestedMaxFee]
* - [maxFee=getSuggestedFee]
* @returns
* - declare
* - transaction_hash
Expand Down Expand Up @@ -316,10 +317,10 @@ export abstract class AccountInterface extends ProviderInterface {
* @param {EstimateFeeDetails} details
* @returns suggestedMaxFee
*/
public abstract getSuggestedMaxFee(
public abstract getSuggestedFee(
estimateFeeAction: EstimateFeeAction,
details: EstimateFeeDetails
): Promise<bigint>;
): Promise<EstimateFee>;

/**
* Simulates an array of transaction and returns an array of transaction trace and estimated fee.
Expand Down
2 changes: 1 addition & 1 deletion src/channel/rpc_0_6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ export class RpcChannel {

public async getEstimateFee(
invocations: AccountInvocations,
{ blockIdentifier = this.blockIdentifier, skipValidate = false }: getEstimateFeeBulkOptions
{ blockIdentifier = this.blockIdentifier, skipValidate = true }: getEstimateFeeBulkOptions
) {
const block_id = new Block(blockIdentifier).identifier;
let flags = {};
Expand Down
2 changes: 2 additions & 0 deletions src/types/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export interface EstimateFeeDetails {
nonceDataAvailabilityMode?: EDataAvailabilityMode;
feeDataAvailabilityMode?: EDataAvailabilityMode;
version?: BigNumberish; // TODO: this is BigNumberish for interoperability with InvocationsDetails
resourceBounds?: ResourceBounds; // TODO: required for non estimate and for estimate is 00
skipValidate?: boolean; // TODO: Specific only to estimatFee methods
}

export interface DeployContractResponse {
Expand Down

0 comments on commit bda0d9f

Please sign in to comment.