Skip to content

Commit

Permalink
refactor(update): toTransactionVersion, getUniversalSuggestedFee
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Dec 11, 2023
1 parent 3b2914a commit 6e7d277
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 129 deletions.
181 changes: 69 additions & 112 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ import {
formatSignature,
randomAddress,
reduceV2,
toETransactionVersions,
toFeeVersion,
toTransactionVersion,
v3Details,
Expand Down Expand Up @@ -321,42 +320,23 @@ export class Account extends Provider implements AccountInterface {
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 estimate = await this.getUniversalSuggestedFee(
version,
{ type: TransactionType.INVOKE, payload: calls },
{
...details,
version,
}
);

const chainId = await this.getChainId();

const signerDetails: InvocationsSignerDetails = {
...v3Details(details),
resourceBounds,
resourceBounds: estimate.resourceBounds,
walletAddress: this.address,
nonce,
maxFee: suggestedMaxFee,
maxFee: estimate.maxFee,
version,
chainId,
cairoVersion: await this.getCairoVersion(),
Expand All @@ -370,9 +350,9 @@ export class Account extends Provider implements AccountInterface {
{ contractAddress: this.address, calldata, signature },
{
...v3Details(details),
resourceBounds,
resourceBounds: estimate.resourceBounds,
nonce,
maxFee: suggestedMaxFee,
maxFee: estimate.maxFee,
version,
}
);
Expand Down Expand Up @@ -405,52 +385,30 @@ export class Account extends Provider implements AccountInterface {
details: EstimateFeeDetails = {}
): Promise<DeclareContractResponse> {
const declareContractPayload = extractContractHashes(payload);
const { maxFee, nonce, version: providedVersion } = details;
const { nonce, version: providedVersion } = details;
const version = toTransactionVersion(
!isSierra(payload.contract)
? ETransactionVersion.V1
: this.getPreferredVersion(ETransactionVersion.V2, ETransactionVersion.V3),
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 estimate = await this.getUniversalSuggestedFee(
version,
{
type: TransactionType.DECLARE,
payload: declareContractPayload,
},
{
...details,
version,
}
);

const declareDetails: InvocationsSignerDetails = {
...v3Details(details),
resourceBounds,
maxFee: suggestedMaxFee,
resourceBounds: estimate.resourceBounds,
maxFee: estimate.maxFee,
nonce: toBigInt(nonce ?? (await this.getNonce())),
version,
chainId: await this.getChainId(),
Expand Down Expand Up @@ -561,43 +519,19 @@ export class Account extends Provider implements AccountInterface {
providedContractAddress ??
calculateContractAddressFromHash(addressSalt, classHash, compiledCalldata, 0);

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 estimate = await this.getUniversalSuggestedFee(
version,
{
type: TransactionType.DEPLOY_ACCOUNT,
payload: {
classHash,
constructorCalldata: compiledCalldata,
addressSalt,
contractAddress,
},
},
details
);

const signature = await this.signer.signDeployAccountTransaction({
...v3Details(details),
Expand All @@ -606,8 +540,8 @@ export class Account extends Provider implements AccountInterface {
contractAddress,
addressSalt,
chainId,
resourceBounds,
maxFee: suggestedMaxFee,
resourceBounds: estimate.resourceBounds,
maxFee: estimate.maxFee,
version,
nonce,
});
Expand All @@ -617,13 +551,36 @@ export class Account extends Provider implements AccountInterface {
{
...v3Details(details),
nonce,
resourceBounds,
maxFee: suggestedMaxFee,
resourceBounds: estimate.resourceBounds,
maxFee: estimate.maxFee,
version,
}
);
}

private async getUniversalSuggestedFee(
version: ETransactionVersion,
{ type, payload }: EstimateFeeAction,
details: EstimateFeeDetails
) {
let maxFee: BigNumberish = 0;
let resourceBounds: ResourceBounds = estimateFeeToBounds(ZERO);
if (version === ETransactionVersion.V3) {
resourceBounds =
details.resourceBounds ??
(await this.getSuggestedFee({ type, payload } as any, details)).resourceBounds;
} else {
maxFee =
details.maxFee ??
(await this.getSuggestedFee({ type, payload } as any, details)).suggestedMaxFee;
}

return {
maxFee,
resourceBounds,
};
}

public async signMessage(typedData: TypedData): Promise<Signature> {
return this.signer.signMessage(typedData, this.address);
}
Expand Down Expand Up @@ -697,7 +654,7 @@ export class Account extends Provider implements AccountInterface {

if (
typeof compiledClassHash === 'undefined' &&
Object.values(ETransactionVersion3).includes(details.version as any)
(details.version === ETransactionVersion3.F3 || details.version === ETransactionVersion3.V3)
) {
throw Error('V3 Transaction work with Cairo1 Contracts and require compiledClassHash');
}
Expand Down Expand Up @@ -784,7 +741,7 @@ export class Account extends Provider implements AccountInterface {
const accountInvocations = await this.accountInvocationsFactory(invocations, {
...v3Details(details),
versions: [
toTransactionVersion(ETransactionVersion.V1), // non-sierra
ETransactionVersion.V1, // non-sierra
toTransactionVersion(
this.getPreferredVersion(ETransactionVersion.V2, ETransactionVersion.V3),
version
Expand All @@ -808,7 +765,7 @@ export class Account extends Provider implements AccountInterface {
const { nonce, blockIdentifier } = details;
const safeNonce = await this.getNonceSafe(nonce);
const chainId = await this.getChainId();
const versions = details.versions.map((it) => toETransactionVersions(it));
const versions = details.versions.map((it) => toTransactionVersion(it));

// BULK ACTION FROM NEW ACCOUNT START WITH DEPLOY_ACCOUNT
const tx0Payload: any = 'payload' in invocations[0] ? invocations[0].payload : invocations[0];
Expand Down
31 changes: 14 additions & 17 deletions src/utils/stark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,24 @@ export function intDAM(dam: EDataAvailabilityMode) {
}

/**
* Convert to ETransactionVersion or throw an error
* @param defaultVersion ETransactionVersion
* Convert to ETransactionVersion or throw an error.
* Return providedVersion is specified else return defaultVersion
* @param defaultVersion BigNumberish
* @param providedVersion BigNumberish | undefined
* @returns ETransactionVersion
*/
export function toTransactionVersion(
defaultVersion: ETransactionVersion,
providedVersion?: BigNumberish
) {
if (providedVersion && !Object.values(ETransactionVersion).includes(providedVersion as any)) {
throw Error(`toTransactionVersion: ${providedVersion} is not supported`);
export function toTransactionVersion(defaultVersion: BigNumberish, providedVersion?: BigNumberish) {
const providedVersion0xs = providedVersion ? toHex(providedVersion) : undefined;
const defaultVersion0xs = toHex(defaultVersion);

if (providedVersion && !Object.values(ETransactionVersion).includes(providedVersion0xs as any)) {
throw Error(`providedVersion ${providedVersion} is not ETransactionVersion`);
}
if (!Object.values(ETransactionVersion).includes(defaultVersion0xs as any)) {
throw Error(`defaultVersion ${defaultVersion} is not ETransactionVersion`);
}
return (providedVersion ? toHex(providedVersion) : defaultVersion) as ETransactionVersion;

return (providedVersion ? providedVersion0xs : defaultVersion0xs) as ETransactionVersion;
}

/**
Expand Down Expand Up @@ -187,11 +192,3 @@ export function reduceV2(providedVersion: ETransactionVersion) {
if (providedVersion === ETransactionVersion.V2) return ETransactionVersion.V1;
return providedVersion;
}

export function toETransactionVersions(version: string) {
if (!Object.values(ETransactionVersion).includes(version as any)) {
throw Error(`Provided ${version} is not ETransactionVersion`);
}

return version as ETransactionVersion;
}

0 comments on commit 6e7d277

Please sign in to comment.