Skip to content

Commit 6e7d277

Browse files
committed
refactor(update): toTransactionVersion, getUniversalSuggestedFee
1 parent 3b2914a commit 6e7d277

File tree

2 files changed

+83
-129
lines changed

2 files changed

+83
-129
lines changed

src/account/default.ts

Lines changed: 69 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ import {
5353
formatSignature,
5454
randomAddress,
5555
reduceV2,
56-
toETransactionVersions,
5756
toFeeVersion,
5857
toTransactionVersion,
5958
v3Details,
@@ -321,42 +320,23 @@ export class Account extends Provider implements AccountInterface {
321320
details.version
322321
);
323322

324-
let suggestedMaxFee: BigNumberish = 0;
325-
let resourceBounds: ResourceBounds = estimateFeeToBounds(ZERO);
326-
if (version === ETransactionVersion.V3) {
327-
resourceBounds =
328-
details.resourceBounds ??
329-
(
330-
await this.getSuggestedFee(
331-
{ type: TransactionType.INVOKE, payload: calls },
332-
{
333-
...details,
334-
version,
335-
}
336-
)
337-
).resourceBounds;
338-
} else {
339-
suggestedMaxFee =
340-
details.maxFee ??
341-
(
342-
await this.getSuggestedFee(
343-
{ type: TransactionType.INVOKE, payload: calls },
344-
{
345-
...details,
346-
version,
347-
}
348-
)
349-
).suggestedMaxFee;
350-
}
323+
const estimate = await this.getUniversalSuggestedFee(
324+
version,
325+
{ type: TransactionType.INVOKE, payload: calls },
326+
{
327+
...details,
328+
version,
329+
}
330+
);
351331

352332
const chainId = await this.getChainId();
353333

354334
const signerDetails: InvocationsSignerDetails = {
355335
...v3Details(details),
356-
resourceBounds,
336+
resourceBounds: estimate.resourceBounds,
357337
walletAddress: this.address,
358338
nonce,
359-
maxFee: suggestedMaxFee,
339+
maxFee: estimate.maxFee,
360340
version,
361341
chainId,
362342
cairoVersion: await this.getCairoVersion(),
@@ -370,9 +350,9 @@ export class Account extends Provider implements AccountInterface {
370350
{ contractAddress: this.address, calldata, signature },
371351
{
372352
...v3Details(details),
373-
resourceBounds,
353+
resourceBounds: estimate.resourceBounds,
374354
nonce,
375-
maxFee: suggestedMaxFee,
355+
maxFee: estimate.maxFee,
376356
version,
377357
}
378358
);
@@ -405,52 +385,30 @@ export class Account extends Provider implements AccountInterface {
405385
details: EstimateFeeDetails = {}
406386
): Promise<DeclareContractResponse> {
407387
const declareContractPayload = extractContractHashes(payload);
408-
const { maxFee, nonce, version: providedVersion } = details;
388+
const { nonce, version: providedVersion } = details;
409389
const version = toTransactionVersion(
410390
!isSierra(payload.contract)
411391
? ETransactionVersion.V1
412392
: this.getPreferredVersion(ETransactionVersion.V2, ETransactionVersion.V3),
413393
providedVersion
414394
);
415395

416-
let suggestedMaxFee: BigNumberish = 0;
417-
let resourceBounds: ResourceBounds = estimateFeeToBounds(ZERO);
418-
if (version === ETransactionVersion.V3) {
419-
resourceBounds =
420-
details.resourceBounds ??
421-
(
422-
await this.getSuggestedFee(
423-
{
424-
type: TransactionType.DECLARE,
425-
payload: declareContractPayload,
426-
},
427-
{
428-
...details,
429-
version,
430-
}
431-
)
432-
).resourceBounds;
433-
} else {
434-
suggestedMaxFee =
435-
maxFee ??
436-
(
437-
await this.getSuggestedFee(
438-
{
439-
type: TransactionType.DECLARE,
440-
payload: declareContractPayload,
441-
},
442-
{
443-
...details,
444-
version,
445-
}
446-
)
447-
).suggestedMaxFee;
448-
}
396+
const estimate = await this.getUniversalSuggestedFee(
397+
version,
398+
{
399+
type: TransactionType.DECLARE,
400+
payload: declareContractPayload,
401+
},
402+
{
403+
...details,
404+
version,
405+
}
406+
);
449407

450408
const declareDetails: InvocationsSignerDetails = {
451409
...v3Details(details),
452-
resourceBounds,
453-
maxFee: suggestedMaxFee,
410+
resourceBounds: estimate.resourceBounds,
411+
maxFee: estimate.maxFee,
454412
nonce: toBigInt(nonce ?? (await this.getNonce())),
455413
version,
456414
chainId: await this.getChainId(),
@@ -561,43 +519,19 @@ export class Account extends Provider implements AccountInterface {
561519
providedContractAddress ??
562520
calculateContractAddressFromHash(addressSalt, classHash, compiledCalldata, 0);
563521

564-
let suggestedMaxFee: BigNumberish = 0;
565-
let resourceBounds: ResourceBounds = estimateFeeToBounds(ZERO);
566-
if (version === ETransactionVersion.V3) {
567-
resourceBounds =
568-
details.resourceBounds ??
569-
(
570-
await this.getSuggestedFee(
571-
{
572-
type: TransactionType.DEPLOY_ACCOUNT,
573-
payload: {
574-
classHash,
575-
constructorCalldata: compiledCalldata,
576-
addressSalt,
577-
contractAddress,
578-
},
579-
},
580-
details
581-
)
582-
).resourceBounds;
583-
} else {
584-
suggestedMaxFee =
585-
details.maxFee ??
586-
(
587-
await this.getSuggestedFee(
588-
{
589-
type: TransactionType.DEPLOY_ACCOUNT,
590-
payload: {
591-
classHash,
592-
constructorCalldata: compiledCalldata,
593-
addressSalt,
594-
contractAddress,
595-
},
596-
},
597-
details
598-
)
599-
).suggestedMaxFee;
600-
}
522+
const estimate = await this.getUniversalSuggestedFee(
523+
version,
524+
{
525+
type: TransactionType.DEPLOY_ACCOUNT,
526+
payload: {
527+
classHash,
528+
constructorCalldata: compiledCalldata,
529+
addressSalt,
530+
contractAddress,
531+
},
532+
},
533+
details
534+
);
601535

602536
const signature = await this.signer.signDeployAccountTransaction({
603537
...v3Details(details),
@@ -606,8 +540,8 @@ export class Account extends Provider implements AccountInterface {
606540
contractAddress,
607541
addressSalt,
608542
chainId,
609-
resourceBounds,
610-
maxFee: suggestedMaxFee,
543+
resourceBounds: estimate.resourceBounds,
544+
maxFee: estimate.maxFee,
611545
version,
612546
nonce,
613547
});
@@ -617,13 +551,36 @@ export class Account extends Provider implements AccountInterface {
617551
{
618552
...v3Details(details),
619553
nonce,
620-
resourceBounds,
621-
maxFee: suggestedMaxFee,
554+
resourceBounds: estimate.resourceBounds,
555+
maxFee: estimate.maxFee,
622556
version,
623557
}
624558
);
625559
}
626560

561+
private async getUniversalSuggestedFee(
562+
version: ETransactionVersion,
563+
{ type, payload }: EstimateFeeAction,
564+
details: EstimateFeeDetails
565+
) {
566+
let maxFee: BigNumberish = 0;
567+
let resourceBounds: ResourceBounds = estimateFeeToBounds(ZERO);
568+
if (version === ETransactionVersion.V3) {
569+
resourceBounds =
570+
details.resourceBounds ??
571+
(await this.getSuggestedFee({ type, payload } as any, details)).resourceBounds;
572+
} else {
573+
maxFee =
574+
details.maxFee ??
575+
(await this.getSuggestedFee({ type, payload } as any, details)).suggestedMaxFee;
576+
}
577+
578+
return {
579+
maxFee,
580+
resourceBounds,
581+
};
582+
}
583+
627584
public async signMessage(typedData: TypedData): Promise<Signature> {
628585
return this.signer.signMessage(typedData, this.address);
629586
}
@@ -697,7 +654,7 @@ export class Account extends Provider implements AccountInterface {
697654

698655
if (
699656
typeof compiledClassHash === 'undefined' &&
700-
Object.values(ETransactionVersion3).includes(details.version as any)
657+
(details.version === ETransactionVersion3.F3 || details.version === ETransactionVersion3.V3)
701658
) {
702659
throw Error('V3 Transaction work with Cairo1 Contracts and require compiledClassHash');
703660
}
@@ -784,7 +741,7 @@ export class Account extends Provider implements AccountInterface {
784741
const accountInvocations = await this.accountInvocationsFactory(invocations, {
785742
...v3Details(details),
786743
versions: [
787-
toTransactionVersion(ETransactionVersion.V1), // non-sierra
744+
ETransactionVersion.V1, // non-sierra
788745
toTransactionVersion(
789746
this.getPreferredVersion(ETransactionVersion.V2, ETransactionVersion.V3),
790747
version
@@ -808,7 +765,7 @@ export class Account extends Provider implements AccountInterface {
808765
const { nonce, blockIdentifier } = details;
809766
const safeNonce = await this.getNonceSafe(nonce);
810767
const chainId = await this.getChainId();
811-
const versions = details.versions.map((it) => toETransactionVersions(it));
768+
const versions = details.versions.map((it) => toTransactionVersion(it));
812769

813770
// BULK ACTION FROM NEW ACCOUNT START WITH DEPLOY_ACCOUNT
814771
const tx0Payload: any = 'payload' in invocations[0] ? invocations[0].payload : invocations[0];

src/utils/stark.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,24 @@ export function intDAM(dam: EDataAvailabilityMode) {
129129
}
130130

131131
/**
132-
* Convert to ETransactionVersion or throw an error
133-
* @param defaultVersion ETransactionVersion
132+
* Convert to ETransactionVersion or throw an error.
133+
* Return providedVersion is specified else return defaultVersion
134+
* @param defaultVersion BigNumberish
134135
* @param providedVersion BigNumberish | undefined
135136
* @returns ETransactionVersion
136137
*/
137-
export function toTransactionVersion(
138-
defaultVersion: ETransactionVersion,
139-
providedVersion?: BigNumberish
140-
) {
141-
if (providedVersion && !Object.values(ETransactionVersion).includes(providedVersion as any)) {
142-
throw Error(`toTransactionVersion: ${providedVersion} is not supported`);
138+
export function toTransactionVersion(defaultVersion: BigNumberish, providedVersion?: BigNumberish) {
139+
const providedVersion0xs = providedVersion ? toHex(providedVersion) : undefined;
140+
const defaultVersion0xs = toHex(defaultVersion);
141+
142+
if (providedVersion && !Object.values(ETransactionVersion).includes(providedVersion0xs as any)) {
143+
throw Error(`providedVersion ${providedVersion} is not ETransactionVersion`);
144+
}
145+
if (!Object.values(ETransactionVersion).includes(defaultVersion0xs as any)) {
146+
throw Error(`defaultVersion ${defaultVersion} is not ETransactionVersion`);
143147
}
144-
return (providedVersion ? toHex(providedVersion) : defaultVersion) as ETransactionVersion;
148+
149+
return (providedVersion ? providedVersion0xs : defaultVersion0xs) as ETransactionVersion;
145150
}
146151

147152
/**
@@ -187,11 +192,3 @@ export function reduceV2(providedVersion: ETransactionVersion) {
187192
if (providedVersion === ETransactionVersion.V2) return ETransactionVersion.V1;
188193
return providedVersion;
189194
}
190-
191-
export function toETransactionVersions(version: string) {
192-
if (!Object.values(ETransactionVersion).includes(version as any)) {
193-
throw Error(`Provided ${version} is not ETransactionVersion`);
194-
}
195-
196-
return version as ETransactionVersion;
197-
}

0 commit comments

Comments
 (0)