Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Block Utility Function #97

Merged
merged 7 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions src/extend-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { HardhatPluginError } from "hardhat/plugins";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { Block, HardhatRuntimeEnvironment } from "hardhat/types";
import * as path from "path";

import {
Expand All @@ -26,7 +26,7 @@ export async function getContractFactoryUtil(hre: HardhatRuntimeEnvironment, con

const metadataPath = await findPath(artifactsPath, metadataSearchTarget);
if (!metadataPath) {
throw new HardhatPluginError(PLUGIN_NAME, `Could not find metadata for ${contractPath}`);
throw new HardhatPluginError(PLUGIN_NAME, `Could not find metadata for contract ${metadataSearchTarget}`);
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
}

const abiSearchTarget = path.join(
Expand Down Expand Up @@ -168,3 +168,32 @@ export async function getTransactionReceiptUtil(
const txReceipt = JSON.parse(executed.stdout.toString()) as TransactionReceipt;
return txReceipt;
}

export async function getBlockUtil(
hre: HardhatRuntimeEnvironment,
blockNumber?: number,
hash?: string
): Promise<Block> {
const blockOptions = {
feederGatewayUrl: hre.config.starknet.networkUrl,
gatewayUrl: hre.config.starknet.networkUrl,
number: blockNumber,
hash: hash
};

// If blockNumber and hash are both provided, it will get the block by hash
// If none of them are provided, it will get the latest block
if (!blockOptions.number && !blockOptions.hash) {
delete blockOptions.number;
delete blockOptions.hash;
}

const executed = await hre.starknetWrapper.getBlock(blockOptions);

if (executed.statusCode) {
const msg = `Could not get block. Error: ${executed.stderr.toString()}`;
throw new HardhatPluginError(PLUGIN_NAME, msg);
}
const block = JSON.parse(executed.stdout.toString()) as Block;
return block;
}
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import {
getTransactionUtil,
getTransactionReceiptUtil,
getWalletUtil,
shortStringToBigIntUtil
shortStringToBigIntUtil,
getBlockUtil
} from "./extend-utils";
import { DevnetUtils } from "./devnet-utils";

Expand Down Expand Up @@ -228,6 +229,11 @@ extendEnvironment((hre) => {
getTransactionReceipt: async (txHash) => {
const txReceipt = await getTransactionReceiptUtil(txHash, hre);
return txReceipt;
},

getBlock: async (blockNumber?: number, hash?: string) => {
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
const block = await getBlockUtil(hre, blockNumber, hash);
return block;
}
};
});
Expand Down
11 changes: 11 additions & 0 deletions src/starknet-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,14 @@ export interface Transaction {
transaction: TransactionData;
transaction_index: number;
}

export interface Block {
block_hash: string;
parent_block_hash: string;
block_number: number;
state_root: string;
status: string;
timestamp: number;
transaction_receipts: Record<string, unknown>;
transactions: Record<string, unknown>;
}
52 changes: 52 additions & 0 deletions src/starknet-wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ interface DeployAccountWrapperOptions {
network: string;
}

interface BlockQueryWrapperOptions {
number?: number;
hash?: string;
gatewayUrl: string;
feederGatewayUrl: string;
}

export abstract class StarknetWrapper {
protected prepareCompileOptions(options: CompileWrapperOptions): string[] {
const ret = [
Expand Down Expand Up @@ -212,6 +219,26 @@ export abstract class StarknetWrapper {
): Promise<ProcessResult>;

public abstract getTransaction(options: TxHashQueryWrapperOptions): Promise<ProcessResult>;

protected prepareBlockQueryOptions(command: string, options: BlockQueryWrapperOptions): string[] {

const commandArr = [
command,
options?.hash ? "--hash" : "",
FabijanC marked this conversation as resolved.
Show resolved Hide resolved
options?.hash ? options.hash : "",
options?.number ? "--number" : "",
options?.number ? options?.number.toString() : "",
"--gateway_url",
options.gatewayUrl,
"--feeder_gateway_url",
options.feederGatewayUrl
];

// delete empty elements/strings
return commandArr.filter(e => e);
}

public abstract getBlock(options: BlockQueryWrapperOptions): Promise<ProcessResult>;
}

function getFullImageName(image: Image): string {
Expand Down Expand Up @@ -415,6 +442,25 @@ export class DockerWrapper extends StarknetWrapper {
);
return executed;
}

public async getBlock(options: BlockQueryWrapperOptions): Promise<ProcessResult> {
const binds: String2String = {};

const dockerOptions = {
binds,
networkMode: "host"
};

const preparedOptions = this.prepareBlockQueryOptions("get_block", options);

const docker = await this.getDocker();
const executed = await docker.runContainer(
this.image,
["starknet", ...preparedOptions],
dockerOptions
);
return executed;
}
}

function checkCommandPath(commandPath: string): void {
Expand Down Expand Up @@ -505,4 +551,10 @@ export class VenvWrapper extends StarknetWrapper {
const executed = await this.execute(this.starknetPath, preparedOptions);
return executed;
}

public async getBlock(options: BlockQueryWrapperOptions): Promise<ProcessResult> {
const preparedOptions = this.prepareBlockQueryOptions("get_block", options);
const executed = await this.execute(this.starknetPath, preparedOptions);
return executed;
}
}
6 changes: 5 additions & 1 deletion src/type-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
import { StarknetWrapper } from "./starknet-wrappers";
import { FlushResponse, LoadL1MessagingContractResponse } from "./devnet-utils";
import { Account, ArgentAccount, OpenZeppelinAccount } from "./account";
import { Transaction, TransactionReceipt } from "./starknet-types";
import { Transaction, TransactionReceipt, Block } from "./starknet-types";
import { HttpNetworkConfig } from "hardhat/types/config";

type StarknetConfig = {
Expand Down Expand Up @@ -71,6 +71,7 @@ type OpenZeppelinAccountType = OpenZeppelinAccount;
type ArgentAccountType = ArgentAccount;
type TransactionReceiptType = TransactionReceipt;
type TransactionType = Transaction;
type BlockType = Block;

declare module "hardhat/types/runtime" {
interface Devnet {
Expand Down Expand Up @@ -179,6 +180,8 @@ declare module "hardhat/types/runtime" {
getTransaction: (txHash: string) => Promise<Transaction>;

getTransactionReceipt: (txHash: string) => Promise<TransactionReceipt>;

getBlock: (blockNumber?: number, hash?: string) => Promise<Block>;
};
}

Expand All @@ -191,4 +194,5 @@ declare module "hardhat/types/runtime" {
type ArgentAccount = ArgentAccountType;
type Transaction = TransactionType;
type TransactionReceipt = TransactionReceiptType;
type Block = BlockType;
}