Skip to content

Commit

Permalink
Upgrade to v0.3.1 (#20)
Browse files Browse the repository at this point in the history
* Add keywords to package.json

* Log commit hash in test

* Update alpha (v4) and docker image used (v0.6.0)

* Reword README.md

* Bump version to 0.3.1

* Fix log adaptation

* Support hre.starknet.network (without lazyObject)
  • Loading branch information
FabijanC authored Nov 18, 2021
1 parent b4e6880 commit 2944305
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 34 deletions.
43 changes: 21 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ This plugin was tested with:
- npm/npx v7.21.1
- Docker v20.10.8:
- Make sure you have a running Docker daemon.
- Linux OS / macOS:
- For developers using Windows, the recommended way is to use WSL 2.
- Linux / macOS:
- On Windows, we recommend using WSL 2.

## Install
```
npm install @shardlabs/starknet-hardhat-plugin
```
Add the following line to the top of your `hardhat.config.ts` (or `hardhat.config.js`):
```typescript
import "@shardlabs/starknet-hardhat-plugin";
```

## Use
This plugin adds the following tasks which target the source/artifact/test directories of your Hardhat project:
Expand Down Expand Up @@ -40,7 +44,7 @@ module.exports = {
```
you can use it by calling `npx hardhat starknet-deploy --starknet-network myNetwork`.

The `alpha` testnet is available by default, you don't need to specify it.
The Alpha testnet is available by default, you don't need to specify it.

## Test
To test Starknet contracts with Mocha, use the regular Hardhat `test` task which expects test files in your designated test directory:
Expand All @@ -49,12 +53,11 @@ npx hardhat test
```

Read more about the network used in tests in the [Testing network](#testing-network) section.
These examples are inspired by the [official Python tutorial](https://www.cairo-lang.org/docs/hello_starknet/unit_tests.html).

Test your contracts in the following manner (comparable to the [official Python tutorial](https://www.cairo-lang.org/docs/hello_starknet/unit_tests.html)).

All function names, argument names and return value names should be referred to by names specified in contract source files.

`BigInt` is used because `felt` may be too big for javascript. Use `BigInt` like `BigInt("10")` or `10n`.
### Important note
- `BigInt` is used because `felt` may be too big for javascript. Use it like `BigInt("10")` or, since ES2020, like `10n`.
- All function names, argument names and return value names should be referred to by the names specified in contract source files.

```typescript
import { expect } from "chai";
Expand All @@ -80,7 +83,13 @@ describe("My Test", function () {
await contract.invoke("increase_balance", { amount: BigInt("20") });

const { res } = await contract.call("get_balance"); // call method by name and receive the result by name
expect(res).to.deep.equal(BigInt(40)); // since ECMAScript 2020, you can also use 40n instead of BigInt(40)
expect(res).to.deep.equal(BigInt(40)); // you can also use 40n instead of BigInt(40)
});

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

/**
Expand Down Expand Up @@ -108,12 +117,6 @@ describe("My Test", function () {
expect(res).to.deep.equal(BigInt(30));
});

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

/**
* Assumes there is a file MyAuthContract.cairo whose compilation artifacts have been generated.
* The contract is assumed to have:
Expand All @@ -129,11 +132,7 @@ describe("My Test", function () {
const contract = await authContractFactory.deploy({ lucky_user: publicKey, initial_balance: 10 });

// signature is calculated for each transaction according to `publicKey` used and `amount` passed
const signature = [
BigInt("123..."),
BigInt("456...")
];

const signature = [BigInt("123..."), BigInt("456...")];
await contract.invoke("increase_balance", { user: publicKey, amount: 20 }, signature);

// notice how `res` is mapped to `balance`
Expand Down Expand Up @@ -171,14 +170,14 @@ module.exports = {
...
cairo: {
// The default in this version of the plugin
version: "0.5.2"
version: "0.6.0"
}
...
};
```

### Testing network
If you don't specify a `mocha.starknetNetwork`, the program defaults to using the alpha testnet for Mocha tests.
If you don't specify a `mocha.starknetNetwork`, the program defaults to using the Alpha testnet for Mocha tests.

A faster approach, but still in beta-phase, is to use [starknet-devnet](https://github.com/Shard-Labs/starknet-devnet), a Ganache-like local testnet.

Expand Down
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
{
"name": "@shardlabs/starknet-hardhat-plugin",
"version": "0.3.0",
"version": "0.3.1",
"description": "Plugin for using Starknet tools within Hardhat projects",
"main": "dist/index.js",
"files": [
"dist"
],
"keywords": [
"starknet",
"hardhat",
"plugin",
"starkware",
"cairo",
"compile",
"deploy",
"test"
],
"repository": {
"type": "git",
"url": "https://github.com/Shard-Labs/starknet-hardhat-plugin.git"
Expand Down
1 change: 1 addition & 0 deletions scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ set -e
rm -rf starknet-hardhat-example
git clone -b plugin --single-branch [email protected]:Shard-Labs/starknet-hardhat-example.git
cd starknet-hardhat-example
git log -n 1
npm install

CONFIG_FILE_NAME="hardhat.config.ts"
Expand Down
4 changes: 2 additions & 2 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ 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 = "0.5.2";
export const DEFAULT_DOCKER_IMAGE_TAG = "0.6.0";
export const DEFAULT_STARKNET_NETWORK = "alpha";
export const ALPHA_URL = "https://alpha3.starknet.io:443"
export const ALPHA_URL = "https://alpha4.starknet.io:443"
export const CHECK_STATUS_TIMEOUT = 1000; // ms
export const LEN_SUFFIX = "_len";
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ async function traverseFiles(
*/
function processExecuted(executed: ProcessResult): number {
if (executed.stdout.length) {
console.log(executed.stdout.toString());
console.log(adaptLog(executed.stdout.toString()));
}

if (executed.stderr.length) {
Expand Down Expand Up @@ -257,6 +257,7 @@ task("starknet-deploy", "Deploys Starknet contracts which have been compiled.")

const providedStarknetNetwork: string = args.starknetNetwork || process.env.STARKNET_NETWORK;
if (providedStarknetNetwork) {
hre.starknet.network = providedStarknetNetwork;
const httpNetwork = <HttpNetworkConfig> hre.config.networks[providedStarknetNetwork];
args.gatewayUrl = args.gatewayUrl || httpNetwork.url;
}
Expand Down Expand Up @@ -313,7 +314,7 @@ async function findPath(traversable: string, name: string) {
}

extendEnvironment(hre => {
hre.starknet = lazyObject(() => ({
hre.starknet = {
getContractFactory: async contractName => {
const metadataPath = await findPath(hre.config.paths.starknetArtifacts, `${contractName}.json`);
if (!metadataPath) {
Expand Down Expand Up @@ -344,5 +345,5 @@ extendEnvironment(hre => {
feederGatewayUrl: testNetwork.url
});
}
}));
};
});
17 changes: 12 additions & 5 deletions src/type-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,20 @@ type StarknetContractFactoryType = StarknetContractFactory;
declare module "hardhat/types/runtime" {
interface HardhatRuntimeEnvironment {
dockerWrapper: DockerWrapper;
/**
* 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: {
/**
* 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
* @returns a factory for generating instances of the desired contract
*/
getContractFactory: (name: string) => Promise<StarknetContractFactory>;

/**
* The selected starknet-network, present when specified with --starknet-network.
*/
network?: string;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
export function adaptLog(msg: string): string {
return msg
.replace("--network", "--starknet-network")
.replace("--gateway_url", "--gateway-url");
.replace("gateway_url", "gateway-url");
}

const DOCKER_HOST = "host.docker.internal";
Expand Down

0 comments on commit 2944305

Please sign in to comment.