Skip to content

Commit

Permalink
Restructure legacy utilities and directly expose starknet.js through …
Browse files Browse the repository at this point in the history
…the runtime environment
  • Loading branch information
penovicp committed Dec 13, 2023
1 parent 861ef94 commit 932a77f
Show file tree
Hide file tree
Showing 30 changed files with 2,110 additions and 2,019 deletions.
1,065 changes: 0 additions & 1,065 deletions src/account.ts

This file was deleted.

21 changes: 11 additions & 10 deletions src/cairo1-compiler.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import fs from "fs";
import os from "os";
import { ProcessResult } from "@nomiclabs/hardhat-docker";
import shell from "shelljs";
import path from "path";
import axios, { AxiosError } from "axios";
import { StarknetPluginError } from "./starknet-plugin-error";
import { TaskArguments } from "hardhat/types";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import shell from "shelljs";
import tar from "tar-fs";
import zlib from "zlib";

import config from "../config.json";
import {
CAIRO_COMPILER_BINARY_URL,
HIDDEN_PLUGIN_COMPILER_SUBDIR,
HIDDEN_PLUGIN_DIR
} from "./constants";
import { StarknetConfig } from "./types/starknet";
import config from "../config.json";
import tar from "tar-fs";
import zlib from "zlib";
import { TaskArguments } from "hardhat/types";
import { StarknetPluginError } from "./starknet-plugin-error";
import { StarknetConfig } from "./types/starknet-environment";

export const exec = (args: string) => {
const result = shell.exec(args, {
Expand Down
31 changes: 18 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as path from "path";
import exitHook from "exit-hook";
import { task, extendEnvironment, extendConfig } from "hardhat/config";
import { StarknetPluginError } from "./starknet-plugin-error";
import { lazyObject } from "hardhat/plugins";
import {
ConfigurableTaskDefinition,
Expand All @@ -9,9 +8,9 @@ import {
HardhatRuntimeEnvironment,
HardhatUserConfig
} from "hardhat/types";
import exitHook from "exit-hook";
import * as path from "node:path";
import * as starknet from "starknet";

import "./type-extensions";
import {
DEFAULT_STARKNET_SOURCES_PATH,
DEFAULT_STARKNET_ARTIFACTS_PATH,
Expand Down Expand Up @@ -39,6 +38,8 @@ import {
getDefaultHttpNetworkConfig,
getNetwork
} from "./utils";

import { StarknetPluginError } from "./starknet-plugin-error";
import { DockerWrapper, VenvWrapper } from "./starknet-wrappers";
import {
amarnaAction,
Expand All @@ -60,12 +61,14 @@ import {
getNonceUtil,
getTransactionTraceUtil,
getBalanceUtil
} from "./extend-utils";
import { DevnetUtils } from "./devnet-utils";
} from "./legacy/extend-utils";
import { DevnetUtils } from "./utils/devnet-utils";
import { ExternalServer } from "./external-server";
import { ArgentAccount, OpenZeppelinAccount } from "./account";
import { OpenZeppelinAccount } from "./legacy/account/open-zeppelin-account";
import { ArgentAccount } from "./legacy/account/argent-account";
import { AmarnaDocker } from "./external-server/docker-amarna";
import { StarknetJsWrapper } from "./starknet-js-wrapper";
import "./type-extensions";

exitHook(() => {
ExternalServer.cleanAll();
Expand Down Expand Up @@ -267,6 +270,13 @@ task("starknet-build", "Builds Scarb projects")

extendEnvironment((hre) => {
hre.starknet = {
...starknet,
devnet: lazyObject(() => new DevnetUtils(hre)),
network: hre.config.starknet.network,
networkConfig: hre.config.starknet.networkConfig as HardhatNetworkConfig
};

hre.starknetLegacy = {
getContractFactory: async (contractPath) => {
const contractFactory = await getContractFactoryUtil(hre, contractPath);
return contractFactory;
Expand All @@ -282,8 +292,6 @@ extendEnvironment((hre) => {
return convertedBigInt;
},

devnet: lazyObject(() => new DevnetUtils(hre)),

getTransaction: async (txHash) => {
const transaction = await getTransactionUtil(txHash, hre);
return transaction;
Expand Down Expand Up @@ -314,9 +322,6 @@ extendEnvironment((hre) => {
return balance;
},

network: hre.config.starknet.network,
networkConfig: hre.config.starknet.networkConfig as HardhatNetworkConfig,

OpenZeppelinAccount: OpenZeppelinAccount,
ArgentAccount: ArgentAccount
};
Expand Down Expand Up @@ -357,5 +362,5 @@ task("amarna", "Runs Amarna, the static-analyzer and linter for Cairo.")
.setAction(amarnaAction);

export * from "./types";
export * from "./starknet-types";
export * from "./types/starknet-types";
export * from "./starknet-plugin-error";
36 changes: 15 additions & 21 deletions src/account-utils.ts → src/legacy/account/account-utils.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
import axios, { AxiosError } from "axios";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import { HardhatRuntimeEnvironment, StarknetContract, StringMap } from "hardhat/types";
import fs from "node:fs";
import path from "node:path";
import { ec, hash, stark } from "starknet";

import {
ABI_SUFFIX,
INTERNAL_ARTIFACTS_DIR,
ABI_SUFFIX,
StarknetChainId,
TransactionHashPrefix,
TRANSACTION_VERSION,
StarknetChainId
} from "./constants";
import { StarknetPluginError } from "./starknet-plugin-error";
import * as starknet from "./starknet-types";
import {
Cairo1ContractClass,
iterativelyCheckStatus,
Numeric,
StarknetContract,
StringMap
} from "./types";
import { numericToHexString } from "./utils";
TRANSACTION_VERSION
} from "../../constants";
import { StarknetPluginError } from "../../starknet-plugin-error";
import { Numeric, starknetTypes } from "../../types";
import { numericToHexString } from "../../utils";
import { Cairo1ContractClass } from "../contract";
import { iterativelyCheckStatus } from "../utils";

export type CallParameters = {
toContract: StarknetContract;
Expand Down Expand Up @@ -70,8 +65,7 @@ export function handleInternalContractArtifacts(
const abiArtifact = contractName + ABI_SUFFIX;

const artifactsSourcePath = path.join(
__dirname,
"..", // necessary since artifact dir is in the root, not in src
__dirname.match(/^.*dist\//)[0], // necessary since artifact dir is in the root
INTERNAL_ARTIFACTS_DIR,
contractDir,
artifactsVersion,
Expand Down Expand Up @@ -155,7 +149,7 @@ export async function sendDeployAccountTx(
version: numericToHexString(TRANSACTION_VERSION),
type: "DEPLOY_ACCOUNT"
})
.catch((error: AxiosError<starknet.StarkError>) => {
.catch((error: AxiosError<starknetTypes.StarkError>) => {
const msg = `Deploying account failed: ${error.response.data.message}`;
throw new StarknetPluginError(msg, error);
});
Expand Down Expand Up @@ -191,7 +185,7 @@ export async function sendDeclareV2Tx(
nonce: numericToHexString(nonce),
max_fee: numericToHexString(maxFee)
})
.catch((error: AxiosError<starknet.StarkError>) => {
.catch((error: AxiosError<starknetTypes.StarkError>) => {
const msg = `Declaring contract failed: ${error.response.data.message}`;
throw new StarknetPluginError(msg, error);
});
Expand All @@ -216,7 +210,7 @@ export async function sendEstimateFeeTx(data: unknown) {

const resp = await axios
.post(`${hre.starknet.networkConfig.url}/feeder_gateway/estimate_fee`, data)
.catch((error: AxiosError<starknet.StarkError>) => {
.catch((error: AxiosError<starknetTypes.StarkError>) => {
const msg = `Estimating fees failed: ${error.response.data.message}`;
throw new StarknetPluginError(msg, error);
});
Expand All @@ -227,5 +221,5 @@ export async function sendEstimateFeeTx(data: unknown) {
unit,
gas_price: BigInt(gas_price),
gas_usage: BigInt(gas_usage)
} as starknet.FeeEstimation;
} as starknetTypes.FeeEstimation;
}
Loading

0 comments on commit 932a77f

Please sign in to comment.