Skip to content

Commit

Permalink
fix: sequencer defaults, remove defaultProvider default blockIdentifi…
Browse files Browse the repository at this point in the history
…er, spelling, class
  • Loading branch information
tabaktoni committed Dec 8, 2022
1 parent 6166c8b commit 555a9a3
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 66 deletions.
1 change: 0 additions & 1 deletion src/account/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,6 @@ export class Account extends Provider implements AccountInterface {
return { declare: { ...declare, class_hash: classHash }, deploy };
}


public async deployAccount(
{
classHash,
Expand Down
18 changes: 8 additions & 10 deletions src/contract/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import assert from 'minimalistic-assert';

import { AccountInterface } from '../account';
import { ProviderInterface, defaultProvider } from '../provider';
import { BlockIdentifier } from '../provider/utils';
import {
Abi,
AbiEntry,
Expand All @@ -21,7 +20,7 @@ import {
StructAbi,
} from '../types';
import { BigNumberish, toBN, toFelt } from '../utils/number';
import { ContractInterface } from './interface';
import { CallOptions, ContractInterface } from './interface';

function parseFelt(candidate: string): BN {
try {
Expand Down Expand Up @@ -232,12 +231,11 @@ export class Contract implements ContractInterface {
public async call(
method: string,
args: Array<any> = [],
{
blockIdentifier = 'pending',
}: {
blockIdentifier?: BlockIdentifier;
} = {}
options: CallOptions = {}
): Promise<Result> {
// default value also for null
const blockIdentifier = options?.blockIdentifier;

// ensure contract is connected
assert(this.address !== null, 'contract is not connected to an address');

Expand Down Expand Up @@ -361,16 +359,16 @@ export class Contract implements ContractInterface {
*/
protected validateMethodAndArgs(type: 'INVOKE' | 'CALL', method: string, args: Array<any> = []) {
// ensure provided method exists
const invokeableFunctionNames = this.abi
const invocableFunctionNames = this.abi
.filter((abi) => {
if (abi.type !== 'function') return false;
const isView = abi.stateMutability === 'view';
return type === 'INVOKE' ? !isView : isView;
})
.map((abi) => abi.name);
assert(
invokeableFunctionNames.includes(method),
`${type === 'INVOKE' ? 'invokeable' : 'viewable'} method not found in abi`
invocableFunctionNames.includes(method),
`${type === 'INVOKE' ? 'invocable' : 'viewable'} method not found in abi`
);

// ensure args match abi type
Expand Down
12 changes: 5 additions & 7 deletions src/contract/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import {
Result,
} from '../types';

export type CallOptions = {
blockIdentifier?: BlockIdentifier;
};

export abstract class ContractInterface {
public abstract abi: Abi;

Expand Down Expand Up @@ -60,13 +64,7 @@ export abstract class ContractInterface {
* @param options optional blockIdentifier
* @returns Result of the call as an array with key value pars
*/
public abstract call(
method: string,
args?: Array<any>,
options?: {
blockIdentifier?: BlockIdentifier;
}
): Promise<Result>;
public abstract call(method: string, args?: Array<any>, options?: CallOptions): Promise<Result>;

/**
* Invokes a method on a contract
Expand Down
24 changes: 9 additions & 15 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ export interface ProviderOptions {
export class Provider implements ProviderInterface {
private provider!: ProviderInterface;

private defaultBlockIdentifier: BlockIdentifier;

constructor(providerOrOptions?: ProviderOptions | ProviderInterface) {
if (providerOrOptions instanceof Provider) {
// providerOrOptions is Provider
Expand All @@ -53,8 +51,6 @@ export class Provider implements ProviderInterface {
// providerOrOptions is none, create SequencerProvider as default
this.provider = new SequencerProvider();
}

this.defaultBlockIdentifier = providerOrOptions instanceof RpcProvider ? 'latest' : 'pending';
}

public get chainId(): StarknetChainId {
Expand All @@ -65,22 +61,20 @@ export class Provider implements ProviderInterface {
return this.provider.getChainId();
}

public async getBlock(
blockIdentifier: BlockIdentifier = this.defaultBlockIdentifier
): Promise<GetBlockResponse> {
public async getBlock(blockIdentifier: BlockIdentifier): Promise<GetBlockResponse> {
return this.provider.getBlock(blockIdentifier);
}

public async getClassAt(
contractAddress: string,
blockIdentifier: BlockIdentifier = this.defaultBlockIdentifier
blockIdentifier: BlockIdentifier
): Promise<ContractClass> {
return this.provider.getClassAt(contractAddress, blockIdentifier);
}

public async getClassHashAt(
contractAddress: string,
blockIdentifier: BlockIdentifier = this.defaultBlockIdentifier
blockIdentifier: BlockIdentifier
): Promise<string> {
return this.provider.getClassHashAt(contractAddress, blockIdentifier);
}
Expand All @@ -92,15 +86,15 @@ export class Provider implements ProviderInterface {
public async getEstimateFee(
invocationWithTxType: Invocation,
invocationDetails: InvocationsDetailsWithNonce,
blockIdentifier: BlockIdentifier = this.defaultBlockIdentifier
blockIdentifier: BlockIdentifier
): Promise<EstimateFeeResponse> {
return this.provider.getEstimateFee(invocationWithTxType, invocationDetails, blockIdentifier);
}

public async getInvokeEstimateFee(
invocationWithTxType: Invocation,
invocationDetails: InvocationsDetailsWithNonce,
blockIdentifier: BlockIdentifier = this.defaultBlockIdentifier
blockIdentifier?: BlockIdentifier
): Promise<EstimateFeeResponse> {
return this.provider.getInvokeEstimateFee(
invocationWithTxType,
Expand All @@ -119,7 +113,7 @@ export class Provider implements ProviderInterface {
public async getStorageAt(
contractAddress: string,
key: BigNumberish,
blockIdentifier: BlockIdentifier = this.defaultBlockIdentifier
blockIdentifier: BlockIdentifier
): Promise<BigNumberish> {
return this.provider.getStorageAt(contractAddress, key, blockIdentifier);
}
Expand All @@ -134,7 +128,7 @@ export class Provider implements ProviderInterface {

public async callContract(
request: Call,
blockIdentifier: BlockIdentifier = this.defaultBlockIdentifier
blockIdentifier?: BlockIdentifier
): Promise<CallContractResponse> {
return this.provider.callContract(request, blockIdentifier);
}
Expand Down Expand Up @@ -163,15 +157,15 @@ export class Provider implements ProviderInterface {
public async getDeclareEstimateFee(
transaction: DeclareContractTransaction,
details: InvocationsDetailsWithNonce,
blockIdentifier: BlockIdentifier = this.defaultBlockIdentifier
blockIdentifier?: BlockIdentifier
): Promise<EstimateFeeResponse> {
return this.provider.getDeclareEstimateFee(transaction, details, blockIdentifier);
}

public getDeployAccountEstimateFee(
transaction: DeployAccountContractTransaction,
details: InvocationsDetailsWithNonce,
blockIdentifier: BlockIdentifier = this.defaultBlockIdentifier
blockIdentifier?: BlockIdentifier
): Promise<EstimateFeeResponse> {
return this.provider.getDeployAccountEstimateFee(transaction, details, blockIdentifier);
}
Expand Down
8 changes: 4 additions & 4 deletions src/provider/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export abstract class ProviderInterface {
* Gets the transaction information from a tx id.
*
* @param txHash
* @returns the transacton object { transaction_id, status, transaction, block_number?, block_number?, transaction_index?, transaction_failure_reason? }
* @returns the transaction object { transaction_id, status, transaction, block_number?, block_number?, transaction_index?, transaction_failure_reason? }
*/
public abstract getTransaction(transactionHash: BigNumberish): Promise<GetTransactionResponse>;

Expand Down Expand Up @@ -223,7 +223,7 @@ export abstract class ProviderInterface {
public abstract getInvokeEstimateFee(
invocation: Invocation,
details: InvocationsDetailsWithNonce,
blockIdentifier: BlockIdentifier
blockIdentifier?: BlockIdentifier
): Promise<EstimateFeeResponse>;

/**
Expand All @@ -243,7 +243,7 @@ export abstract class ProviderInterface {
public abstract getDeclareEstimateFee(
transaction: DeclareContractTransaction,
details: InvocationsDetailsWithNonce,
blockIdentifier: BlockIdentifier
blockIdentifier?: BlockIdentifier
): Promise<EstimateFeeResponse>;

/**
Expand All @@ -264,7 +264,7 @@ export abstract class ProviderInterface {
public abstract getDeployAccountEstimateFee(
transaction: DeployAccountContractTransaction,
details: InvocationsDetailsWithNonce,
blockIdentifier: BlockIdentifier
blockIdentifier?: BlockIdentifier
): Promise<EstimateFeeResponse>;

/**
Expand Down
2 changes: 1 addition & 1 deletion src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ export class RpcProvider implements ProviderInterface {
}

if (retries === 0) {
throw new Error('waitForTransaction timedout with retries');
throw new Error(`waitForTransaction timed-out with retries ${this.retries}`);
}
}

Expand Down

0 comments on commit 555a9a3

Please sign in to comment.