Skip to content

Commit

Permalink
fix: default pathfinder does't have pending block
Browse files Browse the repository at this point in the history
  • Loading branch information
tabaktoni committed Dec 2, 2022
1 parent 4296cb3 commit 4619188
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
24 changes: 15 additions & 9 deletions src/provider/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ 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,6 +55,8 @@ 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 @@ -63,20 +67,22 @@ export class Provider implements ProviderInterface {
return this.provider.getChainId();
}

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

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

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

public async getInvokeEstimateFee(
invocationWithTxType: Invocation,
invocationDetails: InvocationsDetailsWithNonce,
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = this.defaultBlockIdentifier
): Promise<EstimateFeeResponse> {
return this.provider.getInvokeEstimateFee(
invocationWithTxType,
Expand All @@ -115,7 +121,7 @@ export class Provider implements ProviderInterface {
public async getStorageAt(
contractAddress: string,
key: BigNumberish,
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = this.defaultBlockIdentifier
): Promise<BigNumberish> {
return this.provider.getStorageAt(contractAddress, key, blockIdentifier);
}
Expand All @@ -130,7 +136,7 @@ export class Provider implements ProviderInterface {

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

public getDeployAccountEstimateFee(
transaction: DeployAccountContractTransaction,
details: InvocationsDetailsWithNonce,
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = this.defaultBlockIdentifier
): Promise<EstimateFeeResponse> {
return this.provider.getDeployAccountEstimateFee(transaction, details, blockIdentifier);
}
Expand Down
51 changes: 32 additions & 19 deletions src/provider/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,20 @@ export type RpcProviderOptions = {
nodeUrl: string;
retries?: number;
headers?: object;
blockIdentifier?: BlockIdentifier;
};

// Default Pathfinder disabled pending block https://github.com/eqlabs/pathfinder/blob/main/README.md
// Note that pending support is disabled by default and must be enabled by setting poll-pending=true in the configuration options.
const defaultOptions = {
headers: { 'Content-Type': 'application/json' },
blockIdentifier: 'latest',
retries: 200,
};

export class RpcProvider implements ProviderInterface {
public nodeUrl: string;

// from interface
public chainId!: StarknetChainId;

public headers: object;
Expand All @@ -50,11 +58,14 @@ export class RpcProvider implements ProviderInterface {

private retries: number;

private blockIdentifier: BlockIdentifier;

constructor(optionsOrProvider: RpcProviderOptions) {
const { nodeUrl, retries, headers } = optionsOrProvider;
const { nodeUrl, retries, headers, blockIdentifier } = optionsOrProvider;
this.nodeUrl = nodeUrl;
this.retries = retries || 200;
this.headers = { 'Content-Type': 'application/json', ...headers };
this.retries = retries || defaultOptions.retries;
this.headers = { ...defaultOptions.headers, ...headers };
this.blockIdentifier = blockIdentifier || defaultOptions.blockIdentifier;

this.getChainId().then((chainId) => {
this.chainId = chainId;
Expand Down Expand Up @@ -97,7 +108,9 @@ export class RpcProvider implements ProviderInterface {
}

// Methods from Interface
public async getBlock(blockIdentifier: BlockIdentifier = 'pending'): Promise<GetBlockResponse> {
public async getBlock(
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<GetBlockResponse> {
return this.getBlockWithTxHashes(blockIdentifier).then(
this.responseParser.parseGetBlockResponse
);
Expand All @@ -108,22 +121,22 @@ export class RpcProvider implements ProviderInterface {
}

public async getBlockWithTxHashes(
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<RPC.GetBlockWithTxHashesResponse> {
const block_id = new Block(blockIdentifier).identifier;
return this.fetchEndpoint('starknet_getBlockWithTxHashes', { block_id });
}

public async getBlockWithTxs(
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<RPC.GetBlockWithTxs> {
const block_id = new Block(blockIdentifier).identifier;
return this.fetchEndpoint('starknet_getBlockWithTxs', { block_id });
}

public async getClassHashAt(
contractAddress: RPC.ContractAddress,
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<RPC.Felt> {
const block_id = new Block(blockIdentifier).identifier;
return this.fetchEndpoint('starknet_getClassHashAt', {
Expand All @@ -134,7 +147,7 @@ export class RpcProvider implements ProviderInterface {

public async getNonceForAddress(
contractAddress: string,
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<RPC.Nonce> {
const block_id = new Block(blockIdentifier).identifier;
return this.fetchEndpoint('starknet_getNonce', {
Expand All @@ -152,7 +165,7 @@ export class RpcProvider implements ProviderInterface {
}

public async getStateUpdate(
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<RPC.StateUpdate> {
const block_id = new Block(blockIdentifier).identifier;
return this.fetchEndpoint('starknet_getStateUpdate', { block_id });
Expand All @@ -161,7 +174,7 @@ export class RpcProvider implements ProviderInterface {
public async getStorageAt(
contractAddress: string,
key: BigNumberish,
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<BigNumberish> {
const parsedKey = toHex(toBN(key));
const block_id = new Block(blockIdentifier).identifier;
Expand Down Expand Up @@ -199,15 +212,15 @@ export class RpcProvider implements ProviderInterface {

public async getClass(
classHash: RPC.Felt,
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<RPC.ContractClass> {
const block_id = new Block(blockIdentifier).identifier;
return this.fetchEndpoint('starknet_getClass', { class_hash: classHash, block_id });
}

public async getClassAt(
contractAddress: string,
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<RPC.ContractClass> {
const block_id = new Block(blockIdentifier).identifier;
return this.fetchEndpoint('starknet_getClassAt', {
Expand All @@ -226,15 +239,15 @@ export class RpcProvider implements ProviderInterface {
public async getEstimateFee(
invocation: Invocation,
invocationDetails: InvocationsDetailsWithNonce,
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<EstimateFeeResponse> {
return this.getInvokeEstimateFee(invocation, invocationDetails, blockIdentifier);
}

public async getInvokeEstimateFee(
invocation: Invocation,
invocationDetails: InvocationsDetailsWithNonce,
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<EstimateFeeResponse> {
const block_id = new Block(blockIdentifier).identifier;
return this.fetchEndpoint('starknet_estimateFee', {
Expand All @@ -256,7 +269,7 @@ export class RpcProvider implements ProviderInterface {
public async getDeclareEstimateFee(
{ senderAddress, contractDefinition, signature }: DeclareContractTransaction,
details: InvocationsDetailsWithNonce,
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<EstimateFeeResponse> {
const block_id = new Block(blockIdentifier).identifier;
return this.fetchEndpoint('starknet_estimateFee', {
Expand All @@ -280,7 +293,7 @@ export class RpcProvider implements ProviderInterface {
public async getDeployAccountEstimateFee(
{ classHash, constructorCalldata, addressSalt, signature }: DeployAccountContractTransaction,
details: InvocationsDetailsWithNonce,
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<EstimateFeeResponse> {
const block_id = new Block(blockIdentifier).identifier;
return this.fetchEndpoint('starknet_estimateFee', {
Expand Down Expand Up @@ -379,7 +392,7 @@ export class RpcProvider implements ProviderInterface {
// Methods from Interface
public async callContract(
call: Call,
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<CallContractResponse> {
const block_id = new Block(blockIdentifier).identifier;
const result = await this.fetchEndpoint('starknet_call', {
Expand Down Expand Up @@ -457,7 +470,7 @@ export class RpcProvider implements ProviderInterface {
* @returns Number of transactions
*/
public async getTransactionCount(
blockIdentifier: BlockIdentifier = 'pending'
blockIdentifier: BlockIdentifier = this.blockIdentifier
): Promise<RPC.GetTransactionCountResponse> {
const block_id = new Block(blockIdentifier).identifier;
return this.fetchEndpoint('starknet_getBlockTransactionCount', { block_id });
Expand Down

0 comments on commit 4619188

Please sign in to comment.