From 5f7657b311b5531c15154a5e5a460a5f577c74e7 Mon Sep 17 00:00:00 2001 From: FabijanC Date: Fri, 1 Oct 2021 12:10:33 +0200 Subject: [PATCH] Bug fix, logging+docs improvement (#6) * Improve testing documentation * Add config check in testing script * Fix url network check and refactor logging * Bump version --- README.md | 9 +++++++-- package.json | 2 +- scripts/test.sh | 12 ++++++++++-- src/index.ts | 14 ++++++++++---- src/types.ts | 11 +++++++---- src/utils.ts | 11 +++++++++++ 6 files changed, 46 insertions(+), 13 deletions(-) create mode 100644 src/utils.ts diff --git a/README.md b/README.md index 4c6664fb..b0ab8c1b 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,9 @@ To test Starknet contracts with Mocha, use the regular Hardhat `test` task: npx hardhat test ``` -Inside the tests, use the following syntax: +Read more about the network used in tests in the [Testing network](#testing-network) section. + +Inside the tests, use the following *modus operandi* (comparable to the [official Python tutorial](https://www.cairo-lang.org/docs/hello_starknet/unit_tests.html)): ```javascript const { expect } = require("chai"); const { getStarknetContract } = require("hardhat"); @@ -83,6 +85,9 @@ module.exports = { ``` ### Testing network +A [Ganache](https://github.com/trufflesuite/ganache)-like localhost devnet for Starknet is under development. + +Until then, your best bet will probably be relying on the alpha testnet (which is the default if you don't specify anything). ```javascript module.exports = { ... @@ -93,7 +98,7 @@ module.exports = { }, mocha: { // Used for deployment in tests - // Defaults to "alpha" + // Defaults to "alpha", which is preconfigured even if you don't see it under `networks:` starknetNetwork: "myNetwork" } ... diff --git a/package.json b/package.json index 5627012a..4734b328 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@shardlabs/starknet-hardhat-plugin", - "version": "0.1.4", + "version": "0.1.5", "description": "Plugin for using Starknet tools within Hardhat projects", "main": "dist/index.js", "files": [ diff --git a/scripts/test.sh b/scripts/test.sh index 9669a63f..01b0cc5b 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -9,9 +9,16 @@ npm install TOTAL=0 SUCCESS=0 for TEST_CASE in ../test/*; do + CONFIG_FILE="$TEST_CASE/hardhat.config.js" + if [ ! -f "$CONFIG_FILE" ]; then + echo "Skipping; no config file provided" + continue + fi + /bin/cp "$CONFIG_FILE" hardhat.config.js + + TEST_NAME=$(basename $TEST_CASE) TOTAL=$((TOTAL + 1)) - echo "Test $TOTAL) $TEST_CASE" - /bin/cp "$TEST_CASE/hardhat.config.js" hardhat.config.js + echo "Test $TOTAL) $TEST_NAME" INIT_SCRIPT="$TEST_CASE/init.sh" if [ -f "$INIT_SCRIPT" ]; then @@ -19,6 +26,7 @@ for TEST_CASE in ../test/*; do fi "$TEST_CASE/check.sh" && SUCCESS=$((SUCCESS + 1)) || echo "Test failed!" + git checkout --force git clean -fd echo "----------------------------------------------" diff --git a/src/index.ts b/src/index.ts index 9619c666..505bffb5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,6 +7,7 @@ import "./type-extensions"; import { DockerWrapper, StarknetContract } from "./types"; import { PLUGIN_NAME, ABI_SUFFIX, DEFAULT_STARKNET_SOURCES_PATH, DEFAULT_STARKNET_ARTIFACTS_PATH, DEFAULT_DOCKER_IMAGE_TAG, DOCKER_REPOSITORY, DEFAULT_STARKNET_NETWORK, ALPHA_URL } from "./constants"; import { HardhatConfig, HardhatUserConfig, HttpNetworkConfig } from "hardhat/types"; +import { adaptLog } from "./utils"; async function traverseFiles( traversable: string, @@ -51,10 +52,9 @@ function processExecuted(executed: ProcessResult): number { if (executed.stderr.length) { // synchronize param names reported by actual CLI with param names used by this plugin - const err = executed.stderr.toString() - .replace("--network", "--starknet-network") - .replace("--gateway_url", "--gateway-url") - console.error(err); + const err = executed.stderr.toString(); + const replacedErr = adaptLog(err); + console.error(replacedErr); } const finalMsg = executed.statusCode ? "Failed" : "Succeeded"; @@ -295,9 +295,15 @@ extendEnvironment(hre => { const testNetworkName = hre.config.mocha.starknetNetwork || DEFAULT_STARKNET_NETWORK; const testNetwork: HttpNetworkConfig = hre.config.networks[testNetworkName]; + if (!testNetwork) { + const msg = `Network ${testNetworkName} is specified under "mocha.starknetNetwork", but not defined in "networks".`; + throw new HardhatPluginError(PLUGIN_NAME, msg); + } + if (!testNetwork.url) { throw new HardhatPluginError(PLUGIN_NAME, `Cannot use network ${testNetworkName}. No "url" specified.`); } + return new StarknetContract(hre.dockerWrapper, metadataPath, abiPath, testNetwork.url); } }); diff --git a/src/types.ts b/src/types.ts index f9b02a8e..9a2d1fe9 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,6 +1,7 @@ import { HardhatDocker, Image } from "@nomiclabs/hardhat-docker"; import { HardhatPluginError } from "hardhat/plugins"; import { PLUGIN_NAME } from "./constants"; +import { adaptLog } from "./utils"; export class DockerWrapper { private docker: HardhatDocker; @@ -53,9 +54,10 @@ export class StarknetContract { ); if (executed.statusCode) { - throw new HardhatPluginError(PLUGIN_NAME, "Could not deploy"); + const msg = "Could not deploy contract. Check the network url in config and if it's responsive."; + throw new HardhatPluginError(PLUGIN_NAME, msg); } - + const matched = executed.stdout.toString().match(/^Contract address: (.*)$/m); this.address = matched[1]; if (!this.address) { @@ -94,8 +96,9 @@ export class StarknetContract { ); if (executed.statusCode) { - // TODO edit err msg (replace strings) - throw new HardhatPluginError(PLUGIN_NAME, `Could not ${kind} ${functionName}:\n` + executed.stderr.toString()); + const msg = `Could not ${kind} ${functionName}:\n` + executed.stderr.toString(); + const replacedMsg = adaptLog(msg); + throw new HardhatPluginError(PLUGIN_NAME, replacedMsg); } return executed; diff --git a/src/utils.ts b/src/utils.ts new file mode 100644 index 00000000..8403efd4 --- /dev/null +++ b/src/utils.ts @@ -0,0 +1,11 @@ +/** + * Replaces Starknet specific terminology with the terminology used in this plugin. + * + * @param msg the log message to be adapted + * @returns the log message with adaptation replacements + */ +export function adaptLog(msg: string): string { + return msg + .replace("--network", "--starknet-network") + .replace("--gateway_url", "--gateway-url"); +} \ No newline at end of file