Skip to content

Commit 8dc9b8a

Browse files
committed
feat: default account UniversalDetails, rpc spec update, update interface
1 parent 6e7d277 commit 8dc9b8a

File tree

6 files changed

+219
-178
lines changed

6 files changed

+219
-178
lines changed

src/account/default.ts

Lines changed: 93 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import {
2323
EstimateFee,
2424
EstimateFeeAction,
2525
EstimateFeeBulk,
26-
EstimateFeeDetails,
2726
EstimateFeeResponse,
2827
Invocation,
2928
Invocations,
@@ -38,6 +37,7 @@ import {
3837
TransactionType,
3938
TypedData,
4039
UniversalDeployerContractPayload,
40+
UniversalDetails,
4141
} from '../types';
4242
import { ETransactionVersion, ETransactionVersion3, ResourceBounds } from '../types/api';
4343
import { CallData } from '../utils/calldata';
@@ -127,14 +127,14 @@ export class Account extends Provider implements AccountInterface {
127127

128128
public async estimateFee(
129129
calls: AllowArray<Call>,
130-
estimateFeeDetails: EstimateFeeDetails = {}
130+
estimateFeeDetails: UniversalDetails = {}
131131
): Promise<EstimateFee> {
132132
return this.estimateInvokeFee(calls, estimateFeeDetails);
133133
}
134134

135135
public async estimateInvokeFee(
136136
calls: AllowArray<Call>,
137-
details: EstimateFeeDetails = {}
137+
details: UniversalDetails = {}
138138
): Promise<EstimateFee> {
139139
const { nonce: providedNonce, blockIdentifier, version: providedVersion } = details;
140140

@@ -172,31 +172,28 @@ export class Account extends Provider implements AccountInterface {
172172
}
173173

174174
public async estimateDeclareFee(
175-
{ contract, classHash: providedClassHash, casm, compiledClassHash }: DeclareContractPayload,
176-
details: EstimateFeeDetails = {}
175+
payload: DeclareContractPayload,
176+
details: UniversalDetails = {}
177177
): Promise<EstimateFee> {
178178
const { blockIdentifier, nonce: providedNonce, version: providedVersion } = details;
179179
const nonce = toBigInt(providedNonce ?? (await this.getNonce()));
180180
const version = toTransactionVersion(
181-
!isSierra(contract)
181+
!isSierra(payload.contract)
182182
? ETransactionVersion.F1
183183
: this.getPreferredVersion(ETransactionVersion.F2, ETransactionVersion.F3),
184184
toFeeVersion(providedVersion)
185185
);
186186
const chainId = await this.getChainId();
187187

188-
const declareContractTransaction = await this.buildDeclarePayload(
189-
{ classHash: providedClassHash, contract, casm, compiledClassHash },
190-
{
191-
...v3Details(details),
192-
nonce,
193-
chainId,
194-
version,
195-
walletAddress: this.address,
196-
maxFee: ZERO,
197-
cairoVersion: undefined, // unused parameter
198-
}
199-
);
188+
const declareContractTransaction = await this.buildDeclarePayload(payload, {
189+
...v3Details(details),
190+
nonce,
191+
chainId,
192+
version,
193+
walletAddress: this.address,
194+
maxFee: ZERO,
195+
cairoVersion: undefined, // unused parameter
196+
});
200197

201198
const estimateFeeResponse = await super.getDeclareEstimateFee(
202199
declareContractTransaction,
@@ -217,9 +214,9 @@ export class Account extends Provider implements AccountInterface {
217214
classHash,
218215
addressSalt = 0,
219216
constructorCalldata = [],
220-
contractAddress: providedContractAddress,
217+
contractAddress,
221218
}: DeployAccountContractPayload,
222-
details: EstimateFeeDetails = {}
219+
details: UniversalDetails = {}
223220
): Promise<EstimateFee> {
224221
const { blockIdentifier, version: providedVersion } = details;
225222
const version = toTransactionVersion(
@@ -230,7 +227,7 @@ export class Account extends Provider implements AccountInterface {
230227
const chainId = await this.getChainId();
231228

232229
const payload = await this.buildAccountDeployPayload(
233-
{ classHash, addressSalt, constructorCalldata, contractAddress: providedContractAddress },
230+
{ classHash, addressSalt, constructorCalldata, contractAddress },
234231
{
235232
...v3Details(details),
236233
nonce,
@@ -258,15 +255,15 @@ export class Account extends Provider implements AccountInterface {
258255

259256
public async estimateDeployFee(
260257
payload: UniversalDeployerContractPayload | UniversalDeployerContractPayload[],
261-
transactionsDetail: EstimateFeeDetails = {}
258+
details: UniversalDetails = {}
262259
): Promise<EstimateFee> {
263260
const calls = this.buildUDCContractPayload(payload);
264-
return this.estimateInvokeFee(calls, transactionsDetail);
261+
return this.estimateInvokeFee(calls, details);
265262
}
266263

267264
public async estimateFeeBulk(
268265
invocations: Invocations,
269-
details: EstimateFeeDetails = {}
266+
details: UniversalDetails = {}
270267
): Promise<EstimateFeeBulk> {
271268
const { nonce, blockIdentifier } = details;
272269
const accountInvocations = await this.accountInvocationsFactory(invocations, {
@@ -293,25 +290,35 @@ export class Account extends Provider implements AccountInterface {
293290
});
294291
}
295292

296-
public async buildInvocation(
297-
call: Array<Call>,
298-
details: InvocationsSignerDetails
299-
): Promise<Invocation> {
300-
const calldata = getExecuteCalldata(call, await this.getCairoVersion());
301-
const signature = await this.signer.signTransaction(call, details);
302-
303-
return {
293+
public async simulateTransaction(
294+
invocations: Invocations,
295+
details: SimulateTransactionDetails = {}
296+
): Promise<SimulateTransactionResponse> {
297+
const { nonce, blockIdentifier, skipValidate, skipExecute, version } = details;
298+
const accountInvocations = await this.accountInvocationsFactory(invocations, {
304299
...v3Details(details),
305-
contractAddress: this.address,
306-
calldata,
307-
signature,
308-
};
300+
versions: [
301+
ETransactionVersion.V1, // non-sierra
302+
toTransactionVersion(
303+
this.getPreferredVersion(ETransactionVersion.V2, ETransactionVersion.V3),
304+
version
305+
),
306+
],
307+
nonce,
308+
blockIdentifier,
309+
});
310+
311+
return super.getSimulateTransaction(accountInvocations, {
312+
blockIdentifier,
313+
skipValidate,
314+
skipExecute,
315+
});
309316
}
310317

311318
public async execute(
312319
calls: AllowArray<Call>,
313320
abis: Abi[] | undefined = undefined,
314-
details: EstimateFeeDetails = {}
321+
details: UniversalDetails = {}
315322
): Promise<InvokeFunctionResponse> {
316323
const transactions = Array.isArray(calls) ? calls : [calls];
317324
const nonce = toBigInt(details.nonce ?? (await this.getNonce()));
@@ -366,7 +373,7 @@ export class Account extends Provider implements AccountInterface {
366373
*/
367374
public async declareIfNot(
368375
payload: DeclareContractPayload,
369-
transactionsDetail: EstimateFeeDetails = {}
376+
transactionsDetail: UniversalDetails = {}
370377
): Promise<DeclareContractResponse> {
371378
const declareContractPayload = extractContractHashes(payload);
372379
try {
@@ -382,7 +389,7 @@ export class Account extends Provider implements AccountInterface {
382389

383390
public async declare(
384391
payload: DeclareContractPayload,
385-
details: EstimateFeeDetails = {}
392+
details: UniversalDetails = {}
386393
): Promise<DeclareContractResponse> {
387394
const declareContractPayload = extractContractHashes(payload);
388395
const { nonce, version: providedVersion } = details;
@@ -426,7 +433,7 @@ export class Account extends Provider implements AccountInterface {
426433

427434
public async deploy(
428435
payload: UniversalDeployerContractPayload | UniversalDeployerContractPayload[],
429-
details: EstimateFeeDetails = {}
436+
details: UniversalDetails = {}
430437
): Promise<MultiDeployContractResponse> {
431438
const params = [].concat(payload as []).map((it) => {
432439
const {
@@ -472,7 +479,7 @@ export class Account extends Provider implements AccountInterface {
472479

473480
public async deployContract(
474481
payload: UniversalDeployerContractPayload | UniversalDeployerContractPayload[],
475-
details: EstimateFeeDetails = {}
482+
details: UniversalDetails = {}
476483
): Promise<DeployContractUDCResponse> {
477484
const deployTx = await this.deploy(payload, details);
478485
const txReceipt = await this.waitForTransaction(deployTx.transaction_hash);
@@ -481,7 +488,7 @@ export class Account extends Provider implements AccountInterface {
481488

482489
public async declareAndDeploy(
483490
payload: DeclareAndDeployContractPayload,
484-
details: EstimateFeeDetails = {}
491+
details: UniversalDetails = {}
485492
): Promise<DeclareDeployUDCResponse> {
486493
const { constructorCalldata, salt, unique } = payload;
487494
let declare = await this.declareIfNot(payload, details);
@@ -505,7 +512,7 @@ export class Account extends Provider implements AccountInterface {
505512
addressSalt = 0,
506513
contractAddress: providedContractAddress,
507514
}: DeployAccountContractPayload,
508-
details: EstimateFeeDetails = {}
515+
details: UniversalDetails = {}
509516
): Promise<DeployContractResponse> {
510517
const version = toTransactionVersion(
511518
this.getPreferredVersion(ETransactionVersion.V1, ETransactionVersion.V3),
@@ -558,29 +565,6 @@ export class Account extends Provider implements AccountInterface {
558565
);
559566
}
560567

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-
584568
public async signMessage(typedData: TypedData): Promise<Signature> {
585569
return this.signer.signMessage(typedData, this.address);
586570
}
@@ -610,7 +594,34 @@ export class Account extends Provider implements AccountInterface {
610594
return this.verifyMessageHash(hash, signature);
611595
}
612596

613-
public async getSuggestedFee({ type, payload }: EstimateFeeAction, details: EstimateFeeDetails) {
597+
/*
598+
* Support methods
599+
*/
600+
601+
private async getUniversalSuggestedFee(
602+
version: ETransactionVersion,
603+
{ type, payload }: EstimateFeeAction,
604+
details: UniversalDetails
605+
) {
606+
let maxFee: BigNumberish = 0;
607+
let resourceBounds: ResourceBounds = estimateFeeToBounds(ZERO);
608+
if (version === ETransactionVersion.V3) {
609+
resourceBounds =
610+
details.resourceBounds ??
611+
(await this.getSuggestedFee({ type, payload } as any, details)).resourceBounds;
612+
} else {
613+
maxFee =
614+
details.maxFee ??
615+
(await this.getSuggestedFee({ type, payload } as any, details)).suggestedMaxFee;
616+
}
617+
618+
return {
619+
maxFee,
620+
resourceBounds,
621+
};
622+
}
623+
624+
public async getSuggestedFee({ type, payload }: EstimateFeeAction, details: UniversalDetails) {
614625
let feeEstimate: EstimateFee;
615626

616627
switch (type) {
@@ -642,9 +653,21 @@ export class Account extends Provider implements AccountInterface {
642653
return feeEstimate;
643654
}
644655

645-
/**
646-
* will be renamed to buildDeclareContractTransaction
647-
*/
656+
public async buildInvocation(
657+
call: Array<Call>,
658+
details: InvocationsSignerDetails
659+
): Promise<Invocation> {
660+
const calldata = getExecuteCalldata(call, await this.getCairoVersion());
661+
const signature = await this.signer.signTransaction(call, details);
662+
663+
return {
664+
...v3Details(details),
665+
contractAddress: this.address,
666+
calldata,
667+
signature,
668+
};
669+
}
670+
648671
public async buildDeclarePayload(
649672
payload: DeclareContractPayload,
650673
details: InvocationsSignerDetails
@@ -733,31 +756,6 @@ export class Account extends Provider implements AccountInterface {
733756
return calls;
734757
}
735758

736-
public async simulateTransaction(
737-
invocations: Invocations,
738-
details: SimulateTransactionDetails = {}
739-
): Promise<SimulateTransactionResponse> {
740-
const { nonce, blockIdentifier, skipValidate, skipExecute, version } = details;
741-
const accountInvocations = await this.accountInvocationsFactory(invocations, {
742-
...v3Details(details),
743-
versions: [
744-
ETransactionVersion.V1, // non-sierra
745-
toTransactionVersion(
746-
this.getPreferredVersion(ETransactionVersion.V2, ETransactionVersion.V3),
747-
version
748-
),
749-
],
750-
nonce,
751-
blockIdentifier,
752-
});
753-
754-
return super.getSimulateTransaction(accountInvocations, {
755-
blockIdentifier,
756-
skipValidate,
757-
skipExecute,
758-
});
759-
}
760-
761759
public async accountInvocationsFactory(
762760
invocations: Invocations,
763761
details: AccountInvocationsFactoryDetails

0 commit comments

Comments
 (0)