Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: migrate hardhat-graph #1292

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
291 changes: 291 additions & 0 deletions packages/hardhat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,291 @@
# hardhat-graph [POC/WIP]

This is a hardhat plugin that aims to make subgraph building easy for Ethereum developers. The goal
is to allow the users to mimic a big portion of the graph-cli functionality. Below you can see a
list of the currently available tasks, for a demo project that show how to use the pulgin you can
check [this repo](https://github.com/graphprotocol/hardhat-graph-demo).

NOTE: This project is POC/WIP, there could be breaking changes or bugs.

## Tasks

### `init`

- Expects two parameters: `contractName: 'MyContract'` and `address: '0x123..'`
- Has optional param `startBlock` - the optional number of the block that the data source starts
indexing from
- Workflow:
- Generates a subgraph in `./subgraph` using `generateScaffold` from `graph-cli`
- Generates a network.json file in `./subgraph` using `initNetworksConfig` from `graph-cli`
- Initializes a new repo if one does not currently exist. (Currently it does not create an initial
commit)
- Generates or updates an existing .gitignore file.
- Runs `codegen` command
- Example usage:

```typescript
async function deploy(contractName: string) {
....
await contract.deployed();
return { contractName: contractName , address: contract.address}
}

deploy()
.then((result) => hre.run('init', result))
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
```

### `update`

- Expects two parameters: `contractName: 'MyContract'` and `address: '0x123..'`
- Has optional param `startBlock` - the optional number of the block that the data source starts
indexing from
- Workflow:
- Updates the contract ABI in `./subgraph/abis`
- Updates the contract Address in `network.json` if it's deployed to the same network. If the
contract has been deployed to a network that is not present in the config file, adds an entry
for the new network.
- Checks for changes to the contract events. If there are any changes the task will exit and the
user will be informed and prompted to address the changes in the subgraph.yaml file and manually
run `codegen` and `build`.
- Runs `codegen` if there are no changes to the contract events.
- For now you'll have to manually run `graph build --network <network>` from the subgraph folder
if you want to update the dataSources network in the subgraph.
- Example usage:

```typescript
async function deploy(contractName: string) {
....
await contract.deployed();
return { contractName: contractName , address: contract.address}
}

deploy()
.then((result) => hre.run('update', result))
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
```

### `add`

- Expects one mandatory parameter: `address: '0x123..`
- Has optional param `startBlock` - the optional number of the block that the data source starts
indexing from
- Has four optional paramaters:
- `subgraphYaml: path/to/subgraph.yaml` (default is './subgraph.yaml')
- `abi: path/to/Contract.json` Loads abi from file
- `mergeEntities` When this flag is given new entities with already taken names are skipped
- `contractName: MyContract` (default is 'Contract')
- Workflow:

- Checks whether the subgraph exists and creates a command line of the arguments passed
- Runs `graph add` from the graph-cli with the given params which updates the `subgraph.yaml`,
`schema.graphql` and adds a new abi and mapping file
- Runs `codegen`

- Example usage:

```sh
npx hardhat add --address 0x123... --abi path/to/Contract.json --contactName MyContract --merge-entities
```

### `graph`

- Expects two parameters: `contractName: 'MyContract'` and `address: '0x123..` and an optional
positional parameter `subtask` <init|update|add>.
- Workflow:
- Conditionally runs either `init`, `update` or `add` tasks depending if a subgraph already exists
or not. If the optional param `subtask` is passed it will run that subtask instead.
- Example usage:

```typescript
async function deploy(contractName: string) {
....
await contract.deployed();
const deployTx = await contract.deployTransaction.wait();
return { contractName: MyContract , address: contract.address, blockNumber: deployTx.blockNumber}
}

deploy()
.then((result) => hre.run('graph', result))
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
```

or

```sh
npx hardhat graph < init | update | add > --contract-name MyContract --address 0x123... # the subtask parameter is optional
```

## How to

NOTE: npm >7 should auto-install peerDependencies from plugins, but if they are not or you're using
`yarn`, add

```
"@graphprotocol/graph-cli": "^0.30.0",
"@graphprotocol/graph-ts": "^0.27.0",
```

to the hardhat project package.json (Because the `graph add` command was added in version 0.30.0,
this is also the minimum required version)

The plugin can be installed from the repo:

```json
{
...
"devDependencies": {
"hardhat-graph": "https://github.com/graphprotocol/hardhat-graph"
...
}
}
```

or from a specific branch:

```json
{
...
"devDependencies": {
"hardhat-graph": "https://github.com/graphprotocol/hardhat-graph#branch_name"
...
}
}
```

Import the plugin in your `hardhat.config` file:

JS: `require("@graphprotocol/hardhat-graph")`

TS: `import "@graphprotocol/hardhat-graph"`

## Configurable options in hardhat.config file

JS:

```javascript
module.exports = {
...
subgraph: {
name: 'MySubgraph', // Defaults to the name of the root folder of the hardhat project
product: 'hosted-service'|'subgraph-studio', // Defaults to 'subgraph-studio'
indexEvents: true|false, // Defaults to false
allowSimpleName: true|false // Defaults to `false` if product is `hosted-service` and `true` if product is `subgraph-studio`
},
paths: {
subgraph: './path/to/subgraph' // Defaults to './subgraph'
}
}
```

TS:

```typescript
export default {
...
subgraph: {
name: 'MySubgraph', // Defaults to the name of the root folder of the hardhat project
product: 'hosted-service'|'subgraph-studio', // Defaults to 'subgraph-studio'
indexEvents: true|false, // Defaults to false
allowSimpleName: true|false // Defaults to `false` if product is `hosted-service` and `true` if product is `subgraph-studio`
},
paths: {
subgraph: './path/to/subgraph' // Defaults to './subgraph'
}
}
```

## Running local `graph node` against local `hardhat node`

1. Create a `docker-compose.yml` file:

```
version: '3'
services:
graph-node:
image: graphprotocol/graph-node
ports:
- '8000:8000'
- '8001:8001'
- '8020:8020'
- '8030:8030'
- '8040:8040'
depends_on:
- ipfs
- postgres
extra_hosts:
- host.docker.internal:host-gateway
environment:
postgres_host: postgres
postgres_user: graph-node
postgres_pass: let-me-in
postgres_db: graph-node
ipfs: 'ipfs:5001'
ethereum: 'localhost:http://host.docker.internal:8545'
GRAPH_LOG: info
ipfs:
image: ipfs/go-ipfs:v0.10.0
ports:
- '5001:5001'
volumes:
- ./data/ipfs:/data/ipfs
postgres:
image: postgres
ports:
- '5432:5432'
command:
[
"postgres",
"-cshared_preload_libraries=pg_stat_statements"
]
environment:
POSTGRES_USER: graph-node
POSTGRES_PASSWORD: let-me-in
POSTGRES_DB: graph-node
PGDATA: "/data/postgres"
volumes:
- ./data/postgres:/var/lib/postgresql/data
```

2. Add the following to the networks configuration in your `hardhat.config` file:

```
{
...
networks: {
localhost: {
url: "http://0.0.0.0:8545",
},
},
...
}
```

3. Run the hardhat node with `npx hardhat node --hostname 0.0.0.0`
4. Deploy your contract[s] to the localhost network either with a deploy script/task or through the
hardhat console `npx hardhat console --network localhost`
5. Update the network configuration in `subgraph.yaml` file to `localhost` and the addresses to the
deployed contract addresses (You can use `yarn build --network localhost`. If you use
graph-cli >= 0.32.0 you can skip this step and see step 7)
6. Run `docker-compose up` or `docker compose up`
7. Create and deploy the subgraph using the commands in the package.json `yarn create-local` and
`yarn deploy-local` (Since graph-cli 0.32.0 you can use `--network localhost` option with the
deploy command, similarly to `yarn build` in step 5)
8. Interact with your contract
9. Query the subgraph from `http://127.0.0.1:8000/subgraphs/name/<your-subgraph-name>/graphql`

NOTE: If for any reason you stop the hardhat node, it is recommended to stop the graph node, delete
the `ipfs` and `postgres` folders in `data` (or the whole `data` folder) created by the graph node
(you can run `yarn graph-local-clean` that will do that for you), and then repeat steps `3-9`.
30 changes: 30 additions & 0 deletions packages/hardhat/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "@graphprotocol/hardhat-graph",
"version": "0.0.0",
"description": "Hardhat plugin for Ethereum developers to build subgraphs alongside their smart contracts",
"license": "(Apache-2.0 OR MIT)",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist/*",
"src/*",
"README.md"
],
"scripts": {},
"peerDependencies": {
"@graphprotocol/graph-cli": ">= 0.44.0",
"@graphprotocol/graph-ts": ">= 0.29.3",
"hardhat": ">=2.13.0"
},
"dependencies": {
"@whatwg-node/fetch": "^0.8.4",
"ethers": "^5.6.5"
},
"devDependencies": {
"@graphprotocol/graph-cli": "workspace:*",
"@graphprotocol/graph-ts": "workspace:*",
"@types/node": "^12.20.50",
"hardhat": "^2.13.0",
"typescript": "^5.0.0"
}
}
40 changes: 40 additions & 0 deletions packages/hardhat/src/helpers/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ethers } from 'ethers';

export const compareAbiEvents = async (
spinner: any,
toolbox: any,
dataSource: any,
newAbiJson: any,
): Promise<boolean> => {
// Convert to Interface
const newAbi = new ethers.utils.Interface(newAbiJson);
// Get events signatures
const newAbiEvents = Object.keys(newAbi.events);
// Fetch current dataSource events signatures from subgraph.yaml
const currentAbiEvents = dataSource.mapping.eventHandlers.map((handler: { event: string }) => {
return handler.event;
});
// Check for renamed or replaced events
const changedEvents = await eventsDiff(currentAbiEvents, newAbiEvents);
// const removedEvents = await eventsDiff(currentAbiEvents, newAbiEvents)

const changed = newAbiEvents.length != currentAbiEvents.length || changedEvents.length != 0;

if (changed) {
spinner.warn(
`Contract events have been changed!\n
Current events:\n${currentAbiEvents.join('\n')}\n
New events:\n${newAbiEvents.join('\n')}\n
Please address the change in your subgraph.yaml and run \`graph codegen\` and graph \`build --network <network>\` from the subgraph folder!`.replace(
/[ ]{2,}/g,
'',
),
);
}

return changed;
};

const eventsDiff = async (array1: string[], array2: string[]): Promise<string[]> => {
return array1.filter(x => !array2.includes(x));
};
23 changes: 23 additions & 0 deletions packages/hardhat/src/helpers/execution.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import fs from 'fs';
import process from 'process';
import { HardhatRuntimeEnvironment } from 'hardhat/types';

// Changes the process cwd to the passed directory.
// Executes the code in the passed function.
// Changes the process cwd back to the root folder
// Returns the result of the passed function.
export const fromDirectory = async (
hre: HardhatRuntimeEnvironment,
directory: string,
fn: () => Promise<boolean>,
): Promise<boolean> => {
if (fs.existsSync(directory)) {
process.chdir(directory);
}

const result = await fn();

process.chdir(hre.config.paths.root);

return result;
};
Loading