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 all 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
34 changes: 31 additions & 3 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 @@ -11,7 +11,7 @@ import {
import { AccountImplementationType, StarknetContractFactory } from "./types";
import { Account, ArgentAccount, OpenZeppelinAccount } from "./account";
import { checkArtifactExists, findPath, getAccountPath } from "./utils";
import { Transaction, TransactionReceipt } from "./starknet-types";
import { Transaction, TransactionReceipt, BlockIdentifier } from "./starknet-types";

export async function getContractFactoryUtil(hre: HardhatRuntimeEnvironment, contractPath: string) {
const artifactsPath = hre.config.paths.starknetArtifacts;
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 "${contractPath}.cairo"`);
}

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

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

// 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 @@ -41,7 +41,8 @@ import {
getTransactionUtil,
getTransactionReceiptUtil,
getWalletUtil,
shortStringToBigIntUtil
shortStringToBigIntUtil,
getBlockUtil
} from "./extend-utils";
import { DevnetUtils } from "./devnet-utils";
import { IntegratedDevnet } from "./devnet";
Expand Down Expand Up @@ -241,6 +242,11 @@ extendEnvironment((hre) => {
getTransactionReceipt: async (txHash) => {
const txReceipt = await getTransactionReceiptUtil(txHash, hre);
return txReceipt;
},

getBlock: async (identifier) => {
const block = await getBlockUtil(hre, identifier);
return block;
}
};
});
Expand Down
16 changes: 16 additions & 0 deletions src/starknet-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,19 @@ 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>;
}

export interface BlockIdentifier {
blockNumber?: number;
blockHash?: string;
}
57 changes: 57 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,31 @@ export abstract class StarknetWrapper {
): Promise<ProcessResult>;

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

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

const commandArr = [
command,
"--gateway_url",
options.gatewayUrl,
"--feeder_gateway_url",
options.feederGatewayUrl
];

if (options?.hash) {
commandArr.push("--hash");
commandArr.push(options.hash);
}

if (options?.number) {
commandArr.push("--number");
commandArr.push(options.number.toString());
}

return commandArr;
}

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

function getFullImageName(image: Image): string {
Expand Down Expand Up @@ -415,6 +447,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;
}
}

export class VenvWrapper extends StarknetWrapper {
Expand Down Expand Up @@ -492,4 +543,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;
}
}
11 changes: 10 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, BlockIdentifier } from "./starknet-types";
import { NetworkConfig } from "hardhat/types/config";

type StarknetConfig = {
Expand Down Expand Up @@ -89,6 +89,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 @@ -197,6 +198,13 @@ declare module "hardhat/types/runtime" {
getTransaction: (txHash: string) => Promise<Transaction>;

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

/**
* Returns an entire block and the transactions contained within it.
* @param optional block identifier (by block number or hash). To query the last block, simply remove the identifier.
* @returns a block object
*/
getBlock: (identifier?: BlockIdentifier) => Promise<Block>;
};
}

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