Skip to content

Commit

Permalink
Upgrade to v0.2.0 (#8)
Browse files Browse the repository at this point in the history
* Fix visibility, typing, docs

* Remove exports from index (fix of previous)

* Use contract factory

* Export Starknet types

* Fix config file replacement in test

* Bump version

* Fix typo in README.md
  • Loading branch information
FabijanC authored Oct 6, 2021
1 parent 5f7657b commit 03034e6
Show file tree
Hide file tree
Showing 16 changed files with 204 additions and 101 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ Read more about the network used in tests in the [Testing network](#testing-netw
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");
const { starknet } = require("hardhat");

describe("Starknet", function () {
this.timeout(300_000); // 5 min
it("Should work", async function () {
const contract = await getStarknetContract("MyContract"); // assumes there is a file MyContract.cairo
await contract.deploy();
it("should work for a fresh deployment", async function () {
const contractFactory = await starknet.getContractFactory("MyContract"); // assumes there is a file MyContract.cairo
const contract = await contractFactory.deploy();
console.log("Deployed at", contract.address);
await contract.invoke("increase_balance", [10]); // invoke method by name and pass arguments in an array
await contract.invoke("increase_balance", [20]);
Expand All @@ -49,6 +49,12 @@ describe("Starknet", function () {
const balance = parseInt(balanceStr);
expect(balance).to.equal(30);
});

it("should work for a previously deployed contract", async function () {
const contractFactory = await starknet.getContractFactory("MyContract"); // assumes there is a file MyContract.cairo
const contract = contractFactory.getContractAt("0x123..."); // you might wanna put an actual address here
await contract.invoke(...);
});
});
```

Expand Down Expand Up @@ -77,8 +83,8 @@ A list of available versions can be found [here](https://hub.docker.com/r/shardl
module.exports = {
...
cairo: {
// Defaults to "latest"
version: "0.4.1"
// Defaults to the latest version
version: "0.4.2"
}
...
};
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.5",
"version": "0.2.0",
"description": "Plugin for using Starknet tools within Hardhat projects",
"main": "dist/index.js",
"files": [
Expand Down
18 changes: 11 additions & 7 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,23 @@ git clone [email protected]:Shard-Labs/starknet-hardhat-example.git
cd starknet-hardhat-example
npm install

CONFIG_FILE_NAME="hardhat.config.ts"

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"
TOTAL=$((TOTAL + 1))
TEST_NAME=$(basename $TEST_CASE)
echo "Test $TOTAL) $TEST_NAME"

CONFIG_FILE_PATH="$TEST_CASE/$CONFIG_FILE_NAME"
if [ ! -f "$CONFIG_FILE_PATH" ]; then
echo "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_NAME"
#replace the dummy config (config_file_name) with the one of this test (config_file_path)
/bin/cp "$CONFIG_FILE_PATH" "$CONFIG_FILE_NAME"

INIT_SCRIPT="$TEST_CASE/init.sh"
if [ -f "$INIT_SCRIPT" ]; then
Expand Down
3 changes: 2 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export const ABI_SUFFIX = "_abi.json";
export const DEFAULT_STARKNET_SOURCES_PATH = "contracts";
export const DEFAULT_STARKNET_ARTIFACTS_PATH = "starknet-artifacts";
export const DOCKER_REPOSITORY = "shardlabs/cairo-cli";
export const DEFAULT_DOCKER_IMAGE_TAG = "latest";
export const DEFAULT_DOCKER_IMAGE_TAG = "0.4.2";
export const DEFAULT_STARKNET_NETWORK = "alpha";
export const ALPHA_URL = "https://alpha2.starknet.io:443"
export const CHECK_STATUS_TIMEOUT = 1000; // ms
80 changes: 44 additions & 36 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as path from "path";
import * as fs from "fs";
import { task, extendEnvironment, extendConfig } from "hardhat/config";
import { HardhatPluginError } from "hardhat/plugins";
import { HardhatPluginError, lazyObject } from "hardhat/plugins";
import { ProcessResult } from "@nomiclabs/hardhat-docker";
import "./type-extensions";
import { DockerWrapper, StarknetContract } from "./types";
import { DockerWrapper, StarknetContractFactory } 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";
Expand Down Expand Up @@ -266,44 +266,52 @@ task("starknet-deploy", "Deploys Starknet contracts which have been compiled.")
});

extendEnvironment(hre => {
hre.getStarknetContract = async contractName => {
let metadataPath: string;
await traverseFiles(
hre.config.paths.starknetArtifacts,
file => path.basename(file) === `${contractName}.json`,
async file => {
metadataPath = file;
return 0;
hre.starknet = lazyObject(() => ({
getContractFactory: async contractName => {
let metadataPath: string;
await traverseFiles(
hre.config.paths.starknetArtifacts,
file => path.basename(file) === `${contractName}.json`,
async file => {
metadataPath = file;
return 0;
}
);
if (!metadataPath) {
throw new HardhatPluginError(PLUGIN_NAME, `Could not find metadata for ${contractName}`);
}
);
if (!metadataPath) {
throw new HardhatPluginError(PLUGIN_NAME, `Could not find metadata for ${contractName}`);
}

let abiPath: string;
await traverseFiles(
hre.config.paths.starknetArtifacts,
file => path.basename(file) === `${contractName}${ABI_SUFFIX}`,
async file => {
abiPath = file;
return 0;
let abiPath: string;
await traverseFiles(
hre.config.paths.starknetArtifacts,
file => path.basename(file) === `${contractName}${ABI_SUFFIX}`,
async file => {
abiPath = file;
return 0;
}
);
if (!abiPath) {
throw new HardhatPluginError(PLUGIN_NAME, `Could not find ABI for ${contractName}`);
}
);
if (!abiPath) {
throw new HardhatPluginError(PLUGIN_NAME, `Could not find ABI for ${contractName}`);
}

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);
}
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.`);
}
if (!testNetwork.url) {
throw new HardhatPluginError(PLUGIN_NAME, `Cannot use network ${testNetworkName}. No "url" specified.`);
}

return new StarknetContract(hre.dockerWrapper, metadataPath, abiPath, testNetwork.url);
}
return new StarknetContractFactory({
dockerWrapper: hre.dockerWrapper,
metadataPath,
abiPath,
gatewayUrl: testNetwork.url,
feederGatewayUrl: testNetwork.url
});
}
}));
});
21 changes: 18 additions & 3 deletions src/type-extensions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { DockerWrapper, StarknetContract } from "./types";
import "hardhat/types/config";
import "hardhat/types/runtime";
import { DockerWrapper, StarknetContract, StarknetContractFactory } from "./types";

type CairoConfig = {
version: string;
Expand Down Expand Up @@ -28,11 +30,24 @@ declare module "hardhat/types/config" {
}
}

type StarknetContractType = StarknetContract;
type StarknetContractFactoryType = StarknetContractFactory;

declare module "hardhat/types/runtime" {
export interface HardhatRuntimeEnvironment {
interface HardhatRuntimeEnvironment {
dockerWrapper: DockerWrapper;
getStarknetContract: (name: string) => Promise<StarknetContract>;
/**
* Fetches a compiled contract by name. E.g. if the contract is defined in MyContract.cairo,
* the provided string should be `MyContract`.
* @param name the case-sensitive contract name
*/
starknet: {
getContractFactory: (name: string) => Promise<StarknetContractFactory>;
}
}

type StarknetContract = StarknetContractType;
type StarknetContractFactory = StarknetContractFactoryType;
}

declare module "mocha" {
Expand Down
Loading

0 comments on commit 03034e6

Please sign in to comment.