Skip to content

Commit

Permalink
feat: make cairoVersion a property of Account
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkelawala committed Apr 22, 2023
1 parent 60c2ca7 commit b6a7d38
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 22 deletions.
38 changes: 21 additions & 17 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Signer, SignerInterface } from '../signer';
import {
Abi,
AllowArray,
CairoVersion,
Call,
DeclareAndDeployContractPayload,
DeclareContractPayload,
Expand Down Expand Up @@ -59,17 +60,22 @@ export class Account extends Provider implements AccountInterface {

public address: string;

public cairoVersion: CairoVersion;

constructor(
providerOrOptions: ProviderOptions | ProviderInterface,
address: string,
pkOrSigner: Uint8Array | string | SignerInterface
pkOrSigner: Uint8Array | string | SignerInterface,
cairoVersion: CairoVersion = '0'
) {
super(providerOrOptions);
this.address = address.toLowerCase();
this.signer =
typeof pkOrSigner === 'string' || pkOrSigner instanceof Uint8Array
? new Signer(pkOrSigner)
: pkOrSigner;

this.cairoVersion = cairoVersion;
}

public async getNonce(blockIdentifier?: BlockIdentifier): Promise<Nonce> {
Expand All @@ -85,7 +91,7 @@ export class Account extends Provider implements AccountInterface {

public async estimateInvokeFee(
calls: AllowArray<Call>,
{ nonce: providedNonce, blockIdentifier, skipValidate, cairoVersion }: EstimateFeeDetails = {}
{ nonce: providedNonce, blockIdentifier, skipValidate }: EstimateFeeDetails = {}
): Promise<EstimateFee> {
const transactions = Array.isArray(calls) ? calls : [calls];
const nonce = toBigInt(providedNonce ?? (await this.getNonce()));
Expand All @@ -98,7 +104,7 @@ export class Account extends Provider implements AccountInterface {
maxFee: ZERO,
version,
chainId,
cairoVersion: cairoVersion ?? '0',
cairoVersion: this.cairoVersion,
};

const invocation = await this.buildInvocation(transactions, signerDetails);
Expand All @@ -119,7 +125,7 @@ export class Account extends Provider implements AccountInterface {

public async estimateDeclareFee(
{ contract, classHash: providedClassHash, casm, compiledClassHash }: DeclareContractPayload,
{ blockIdentifier, nonce: providedNonce, skipValidate, cairoVersion }: EstimateFeeDetails = {}
{ blockIdentifier, nonce: providedNonce, skipValidate }: EstimateFeeDetails = {}
): Promise<EstimateFee> {
const nonce = toBigInt(providedNonce ?? (await this.getNonce()));
const version = !isSierra(contract) ? toBigInt(feeTransactionVersion) : transactionVersion_2;
Expand All @@ -133,7 +139,7 @@ export class Account extends Provider implements AccountInterface {
version,
walletAddress: this.address,
maxFee: ZERO,
cairoVersion: cairoVersion ?? '0',
cairoVersion: this.cairoVersion,
}
);

Expand All @@ -158,7 +164,7 @@ export class Account extends Provider implements AccountInterface {
constructorCalldata = [],
contractAddress: providedContractAddress,
}: DeployAccountContractPayload,
{ blockIdentifier, skipValidate, cairoVersion }: EstimateFeeDetails = {}
{ blockIdentifier, skipValidate }: EstimateFeeDetails = {}
): Promise<EstimateFee> {
const version = toBigInt(feeTransactionVersion);
const nonce = ZERO; // DEPLOY_ACCOUNT transaction will have a nonce zero as it is the first transaction in the account
Expand All @@ -172,7 +178,7 @@ export class Account extends Provider implements AccountInterface {
version,
walletAddress: this.address,
maxFee: ZERO,
cairoVersion: cairoVersion ?? '0',
cairoVersion: this.cairoVersion,
}
);

Expand Down Expand Up @@ -200,7 +206,7 @@ export class Account extends Provider implements AccountInterface {

public async estimateFeeBulk(
transactions: TransactionBulk,
{ nonce: providedNonce, blockIdentifier, cairoVersion }: EstimateFeeDetails = {}
{ nonce: providedNonce, blockIdentifier }: EstimateFeeDetails = {}
): Promise<EstimateFeeBulk> {
const nonce = toBigInt(providedNonce ?? (await this.getNonce()));
const version = toBigInt(feeTransactionVersion);
Expand All @@ -214,7 +220,7 @@ export class Account extends Provider implements AccountInterface {
maxFee: ZERO,
version,
chainId,
cairoVersion: cairoVersion ?? '0',
cairoVersion: this.cairoVersion,
};

const txPayload = transaction.payload;
Expand Down Expand Up @@ -280,7 +286,7 @@ export class Account extends Provider implements AccountInterface {
call: Array<Call>,
signerDetails: InvocationsSignerDetails
): Promise<Invocation> {
const calldata = getExecuteCalldata(call, signerDetails.cairoVersion);
const calldata = getExecuteCalldata(call, this.cairoVersion);
const signature = await this.signer.signTransaction(call, signerDetails);

return {
Expand All @@ -306,20 +312,18 @@ export class Account extends Provider implements AccountInterface {
const version = toBigInt(transactionVersion);
const chainId = await this.getChainId();

const cairoVersion = transactionsDetail.cairoVersion ?? '0';

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

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

const calldata = getExecuteCalldata(transactions, cairoVersion);
const calldata = getExecuteCalldata(transactions, this.cairoVersion);

return this.invokeFunction(
{ contractAddress: this.address, calldata, signature },
Expand Down Expand Up @@ -354,7 +358,7 @@ export class Account extends Provider implements AccountInterface {
const declareContractTransaction = await this.buildDeclarePayload(declareContractPayload, {
...details,
walletAddress: this.address,
cairoVersion: transactionsDetail.cairoVersion ?? '0', // This can be removed as declare doesn't depend on cairo version. Kept here because of the type mismatch
cairoVersion: this.cairoVersion,
});

return this.declareContract(declareContractTransaction, details);
Expand Down Expand Up @@ -629,7 +633,7 @@ export class Account extends Provider implements AccountInterface {

public async simulateTransaction(
calls: AllowArray<Call>,
{ nonce: providedNonce, blockIdentifier, skipValidate, cairoVersion }: EstimateFeeDetails = {}
{ nonce: providedNonce, blockIdentifier, skipValidate }: EstimateFeeDetails = {}
): Promise<TransactionSimulation> {
const transactions = Array.isArray(calls) ? calls : [calls];
const nonce = toBigInt(providedNonce ?? (await this.getNonce()));
Expand All @@ -642,7 +646,7 @@ export class Account extends Provider implements AccountInterface {
maxFee: ZERO,
version,
chainId,
cairoVersion: cairoVersion ?? '0',
cairoVersion: this.cairoVersion,
};

const invocation = await this.buildInvocation(transactions, signerDetails);
Expand Down
3 changes: 3 additions & 0 deletions src/account/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { SignerInterface } from '../signer';
import {
Abi,
AllowArray,
CairoVersion,
Call,
DeclareAndDeployContractPayload,
DeclareContractPayload,
Expand Down Expand Up @@ -33,6 +34,8 @@ export abstract class AccountInterface extends ProviderInterface {

public abstract signer: SignerInterface;

public abstract cairoVersion: CairoVersion;

/**
* Estimate Fee for executing an INVOKE transaction on starknet
*
Expand Down
2 changes: 0 additions & 2 deletions src/types/account.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BlockIdentifier } from '../provider/utils';
import { BigNumberish } from '../utils/num';
import { CairoVersion } from './lib';
import {
DeclareTransactionReceiptResponse,
EstimateFeeResponse,
Expand All @@ -17,7 +16,6 @@ export interface EstimateFeeDetails {
nonce?: BigNumberish;
blockIdentifier?: BlockIdentifier;
skipValidate?: boolean;
cairoVersion?: CairoVersion;
}

export interface DeployContractResponse {
Expand Down
1 change: 0 additions & 1 deletion src/types/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export type InvocationsDetails = {
nonce?: BigNumberish;
maxFee?: BigNumberish;
version?: BigNumberish;
cairoVersion?: CairoVersion;
};

/**
Expand Down
5 changes: 3 additions & 2 deletions src/types/signer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { StarknetChainId } from '../constants';
import { BigNumberish } from '../utils/num';
import { DeployAccountContractPayload, InvocationsDetails } from './lib';
import { CairoVersion, DeployAccountContractPayload, InvocationsDetails } from './lib';

export interface InvocationsSignerDetails extends Required<InvocationsDetails> {
walletAddress: string;
chainId: StarknetChainId;
cairoVersion: CairoVersion;
}

export interface DeclareSignerDetails {
Expand All @@ -18,7 +19,7 @@ export interface DeclareSignerDetails {
}

export type DeployAccountSignerDetails = Required<DeployAccountContractPayload> &
Omit<Required<InvocationsDetails>, 'cairoVersion'> & {
Required<InvocationsDetails> & {
contractAddress: BigNumberish;
chainId: StarknetChainId;
};

0 comments on commit b6a7d38

Please sign in to comment.