Skip to content

Commit

Permalink
Bug fix, logging+docs improvement (#6)
Browse files Browse the repository at this point in the history
* Improve testing documentation

* Add config check in testing script

* Fix url network check and refactor logging

* Bump version
  • Loading branch information
FabijanC authored Oct 1, 2021
1 parent d4fc1b4 commit 5f7657b
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 13 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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 = {
...
Expand All @@ -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"
}
...
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
12 changes: 10 additions & 2 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,24 @@ 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
$INIT_SCRIPT
fi

"$TEST_CASE/check.sh" && SUCCESS=$((SUCCESS + 1)) || echo "Test failed!"

git checkout --force
git clean -fd
echo "----------------------------------------------"
Expand Down
14 changes: 10 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -295,9 +295,15 @@ extendEnvironment(hre => {

const testNetworkName = hre.config.mocha.starknetNetwork || DEFAULT_STARKNET_NETWORK;
const testNetwork: HttpNetworkConfig = <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);
}
});
11 changes: 7 additions & 4 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -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");
}

0 comments on commit 5f7657b

Please sign in to comment.