Skip to content

Commit

Permalink
feat: auto-detect account cairoVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Sep 22, 2023
1 parent 71e634e commit 85bbe39
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 18 deletions.
34 changes: 24 additions & 10 deletions src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class Account extends Provider implements AccountInterface {
providerOrOptions: ProviderOptions | ProviderInterface,
address: string,
pkOrSigner: Uint8Array | string | SignerInterface,
cairoVersion: CairoVersion = '0'
cairoVersion?: CairoVersion
) {
super(providerOrOptions);
this.address = address.toLowerCase();
Expand All @@ -80,7 +80,9 @@ export class Account extends Provider implements AccountInterface {
? new Signer(pkOrSigner)
: pkOrSigner;

this.cairoVersion = cairoVersion.toString() as CairoVersion;
if (cairoVersion) {
this.cairoVersion = cairoVersion.toString() as CairoVersion;
}
}

public async getNonce(blockIdentifier?: BlockIdentifier): Promise<Nonce> {
Expand All @@ -96,6 +98,18 @@ export class Account extends Provider implements AccountInterface {
}
}

/**
* Async Get cairo version (auto set it, if not set by user)
* @param version CairoVersion
*/
public async getCairoVersion() {
if (!this.cairoVersion) {
const { cairo } = await super.getContractVersion(this.address);
this.cairoVersion = cairo;
}
return this.cairoVersion;
}

public async estimateFee(
calls: AllowArray<Call>,
estimateFeeDetails?: EstimateFeeDetails | undefined
Expand All @@ -118,7 +132,7 @@ export class Account extends Provider implements AccountInterface {
maxFee: ZERO,
version,
chainId,
cairoVersion: this.cairoVersion,
cairoVersion: await this.getCairoVersion(),
};

const invocation = await this.buildInvocation(transactions, signerDetails);
Expand Down Expand Up @@ -153,7 +167,7 @@ export class Account extends Provider implements AccountInterface {
version,
walletAddress: this.address,
maxFee: ZERO,
cairoVersion: this.cairoVersion,
cairoVersion: await this.getCairoVersion(),
}
);

Expand Down Expand Up @@ -192,7 +206,7 @@ export class Account extends Provider implements AccountInterface {
version,
walletAddress: this.address,
maxFee: ZERO,
cairoVersion: this.cairoVersion,
cairoVersion: await this.getCairoVersion(),
}
);

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

return {
Expand Down Expand Up @@ -278,12 +292,12 @@ export class Account extends Provider implements AccountInterface {
maxFee,
version,
chainId,
cairoVersion: this.cairoVersion,
cairoVersion: await this.getCairoVersion(),
};

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

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

return this.invokeFunction(
{ contractAddress: this.address, calldata, signature },
Expand Down Expand Up @@ -340,7 +354,7 @@ export class Account extends Provider implements AccountInterface {
const declareContractTransaction = await this.buildDeclarePayload(declareContractPayload, {
...details,
walletAddress: this.address,
cairoVersion: this.cairoVersion,
cairoVersion: await this.getCairoVersion(),
});

return this.declareContract(declareContractTransaction, details);
Expand Down Expand Up @@ -654,7 +668,7 @@ export class Account extends Provider implements AccountInterface {
maxFee: ZERO,
version,
chainId,
cairoVersion: this.cairoVersion,
cairoVersion: await this.getCairoVersion(),
};
const txPayload: any = 'payload' in transaction ? transaction.payload : transaction;
const common = {
Expand Down
5 changes: 3 additions & 2 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Call,
CallContractResponse,
ContractClassResponse,
ContractVersion,
DeclareContractResponse,
DeclareContractTransaction,
DeployAccountContractTransaction,
Expand Down Expand Up @@ -256,14 +257,14 @@ export class RpcProvider implements ProviderInterface {
public async getContractVersion(
contractAddress: string,
{ blockIdentifier = this.blockIdentifier, compiler = true }: getContractVersionOptions
) {
): Promise<ContractVersion> {
const contractClass = await this.getClassAt(contractAddress, blockIdentifier);
if (isSierra(contractClass)) {
if (compiler) {
const abiTest = getAbiContractVersion(contractClass.abi);
return { cairo: '1', compiler: abiTest.compiler };
}
return { cairo: '1', compiler: 'unknown' };
return { cairo: '1', compiler: undefined };
}
return { cairo: '0', compiler: '0' };
}
Expand Down
5 changes: 3 additions & 2 deletions src/provider/sequencer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
CallContractResponse,
CallL1Handler,
ContractClassResponse,
ContractVersion,
DeclareContractResponse,
DeclareContractTransaction,
DeployAccountContractTransaction,
Expand Down Expand Up @@ -350,14 +351,14 @@ export class SequencerProvider implements ProviderInterface {
blockIdentifier: this.blockIdentifier,
compiler: true,
}
) {
): Promise<ContractVersion> {
const contractClass = await this.getClassAt(contractAddress, blockIdentifier);
if (isSierra(contractClass)) {
if (compiler) {
const abiTest = getAbiContractVersion(contractClass.abi);
return { cairo: '1', compiler: abiTest.compiler };
}
return { cairo: '1', compiler: 'unknown' };
return { cairo: '1', compiler: undefined };
}
return { cairo: '0', compiler: '0' };
}
Expand Down
7 changes: 4 additions & 3 deletions src/types/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ export type Invocation = CallDetails & { signature?: Signature };

export type Call = CallDetails & { entrypoint: string };

export type CairoVersion = '0' | '1';
export type CairoVersion = '0' | '1' | undefined;
export type CompilerVersion = '0' | '1' | '2' | undefined;

export type InvocationsDetails = {
nonce?: BigNumberish;
Expand Down Expand Up @@ -252,8 +253,8 @@ export interface CallStruct {
* compiler: version of the cairo compiler used to compile the contract
*/
export type ContractVersion = {
cairo: string | 'unknown';
compiler: string | 'unknown';
cairo: CairoVersion;
compiler: CompilerVersion;
};

export * from './contract';
2 changes: 1 addition & 1 deletion src/utils/calldata/cairo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function getAbiContractVersion(abi: Abi): ContractVersion {
(it) => it.type === 'function' && (it.inputs.length || it.outputs.length)
);
if (!testFunction) {
return { cairo: 'unknown', compiler: 'unknown' };
return { cairo: undefined, compiler: undefined };
}
const io = testFunction.inputs.length ? testFunction.inputs : testFunction.outputs;
if (isCairo1Type(io[0].type)) {
Expand Down

0 comments on commit 85bbe39

Please sign in to comment.