Skip to content

Commit

Permalink
feat: add subgraph service boilerplate
Browse files Browse the repository at this point in the history
Signed-off-by: Tomás Migone <[email protected]>
  • Loading branch information
tmigone committed Mar 21, 2024
1 parent 1db6c4c commit 8a32061
Show file tree
Hide file tree
Showing 29 changed files with 1,189 additions and 60 deletions.
2 changes: 2 additions & 0 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ runs:
using: composite

steps:
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
- name: Enable corepack for modern yarn
shell: bash
run: corepack enable
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
- name: Set up environment
uses: ./.github/actions/setup
2 changes: 2 additions & 0 deletions .github/workflows/ci-contracts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
- name: Set up environment
uses: ./.github/actions/setup
- name: Run tests
Expand Down
30 changes: 30 additions & 0 deletions .github/workflows/ci-subgraph-service.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: CI - packages/subgraph-service

env:
CI: true

on:
push:
branches: "*"
paths:
- packages/subgraph-service/**
pull_request:
branches: "*"
paths:
- packages/subgraph-service/**
workflow_dispatch:

jobs:
test-ci:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
- name: Set up environment
uses: ./.github/actions/setup
- name: Run tests
run: |
pushd packages/subgraph-service
yarn test
2 changes: 2 additions & 0 deletions .github/workflows/ci-token-dist.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
- name: Set up environment
uses: ./.github/actions/setup
- name: Run tests
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/e2e-contracts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
- name: Set up environment
uses: ./.github/actions/setup
- name: Run e2e tests
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v4
with:
submodules: recursive
- name: Set up environment
uses: ./.github/actions/setup
- name: Publish 🚀
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/verifydeployed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: recursive
- name: Set up environment
uses: ./.github/actions/setup

Expand Down
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ cached/
# Build artifacts
dist/
build/
typechain/
typechain-types/
deployments/hardhat/

# Ignore solc bin output
Expand All @@ -45,8 +47,10 @@ addresses-fork.json
# Keys
.keystore

# Forge artifacts
cache_forge
# Graph client
.graphclient

tx-builder-*.json
!tx-builder-template.json
!tx-builder-template.json
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "packages/subgraph-service/lib/forge-std"]
path = packages/subgraph-service/lib/forge-std
url = https://github.com/foundry-rs/forge-std
8 changes: 7 additions & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,11 @@
. "$(dirname -- "$0")/_/husky.sh"

# contracts
cd packages/contracts
pushd packages/contracts
npx --no-install lint-staged
popd

# subgraph service
pushd packages/subgraph-service
npx --no-install lint-staged
popd
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ This repository is a Yarn workspaces monorepo containing the following packages:
| --- | --- | --- |
| [contracts](./packages/contracts) | [![npm version](https://badge.fury.io/js/@graphprotocol%2Fcontracts.svg)](https://badge.fury.io/js/@graphprotocol%2Fcontracts) | Contracts enabling the open and permissionless decentralized network known as The Graph protocol. |
| [eslint-graph-config](./packages/eslint-graph-config) | [![npm version]()]() | Shared linting and formatting rules for TypeScript projects. |
| [token-distribution](./packages/token-distribution) | - | Contracts managing token locks for network participants |
| [sdk](./packages/sdk) | [![npm version](https://badge.fury.io/js/@graphprotocol%2Fsdk.svg)](https://badge.fury.io/js/@graphprotocol%2Fsdk) | TypeScript based SDK to interact with the protocol contracts |
| [solhint-graph-config](./packages/eslint-graph-config) | [![npm version]()]() | Shared linting and formatting rules for Solidity projects. |
| [subgraph-service](./packages/subgraph-service) | [![npm version]()]() | Contracts for the Subgraph data service in Graph Horizon. |
| [token-distribution](./packages/token-distribution) | - | Contracts managing token locks for network participants |


## Development
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"packages/eslint-graph-config",
"packages/sdk",
"packages/solhint-graph-config",
"packages/subgraph-service",
"packages/token-distribution"
],
"scripts": {
Expand Down
24 changes: 8 additions & 16 deletions packages/contracts/contracts/staking/IHorizonStaking.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
// SPDX-License-Identifier: GPL-2.0-or-later

pragma solidity 0.7.6;
pragma solidity >=0.7.6 <0.9.0;
pragma abicoder v2;

interface Test {
function test() external returns (uint256);
}

interface IHorizonStaking {
struct Provision {
// Service provider that created the provision
Expand Down Expand Up @@ -93,12 +97,7 @@ interface IHorizonStaking {
function stake(uint256 tokens) external;

// create a provision
function provision(
uint256 tokens,
address verifier,
uint256 maxVerifierCut,
uint256 thawingPeriod
) external;
function provision(uint256 tokens, address verifier, uint256 maxVerifierCut, uint256 thawingPeriod) external;

// initiate a thawing to remove tokens from a provision
function thaw(bytes32 provisionId, uint256 tokens) external returns (bytes32 thawRequestId);
Expand All @@ -123,11 +122,7 @@ interface IHorizonStaking {
) external returns (bytes32 thawRequestId);

// slash a service provider
function slash(
bytes32 provisionId,
uint256 tokens,
uint256 verifierAmount
) external;
function slash(bytes32 provisionId, uint256 tokens, uint256 verifierAmount) external;

// set the Service Provider's preferred provisions to be force thawed
function setForceThawProvisions(bytes32[] calldata provisions) external;
Expand All @@ -148,10 +143,7 @@ interface IHorizonStaking {
// `Provision.tokens - Provision.tokensThawing`
function getTokensAvailable(bytes32 provision) external view returns (uint256 tokens);

function getServiceProvider(address serviceProvider)
external
view
returns (ServiceProvider memory);
function getServiceProvider(address serviceProvider) external view returns (ServiceProvider memory);

function getProvision(bytes32 provision) external view returns (Provision memory);
}
2 changes: 1 addition & 1 deletion packages/sdk/src/deployments/lib/address-book.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export abstract class AddressBook<
* @throws AssertionError if the target file is not a valid address book
* @throws Error if the target file does not exist
*/
constructor(_file: string, _chainId: number, strictAssert: boolean = false) {
constructor(_file: string, _chainId: number, strictAssert = false) {
this.strictAssert = strictAssert
this.file = _file
if (!fs.existsSync(this.file)) throw new Error(`Address book path provided does not exist!`)
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/deployments/network/actions/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export const allocateFrom: GraphNetworkAction<{
const metadata = ethers.constants.HashZero

console.log(`\nAllocating ${amount} tokens on ${allocationId}...`)
let extraArgs: ethers.Overrides = {}
const extraArgs: ethers.Overrides = {}
if (process.env.CI) {
extraArgs.gasLimit = BigNumber.from('400000')
}
Expand All @@ -59,7 +59,7 @@ export const allocateFrom: GraphNetworkAction<{
allocationId,
metadata,
proof,
extraArgs
extraArgs,
)
await tx.wait()
}
Expand Down
13 changes: 13 additions & 0 deletions packages/subgraph-service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Sample Hardhat Project

This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, and a script that deploys that contract.

Try running some of the following tasks:

```shell
npx hardhat help
npx hardhat test
REPORT_GAS=true npx hardhat test
npx hardhat node
npx hardhat run scripts/deploy.ts
```
10 changes: 10 additions & 0 deletions packages/subgraph-service/contracts/SubgraphService.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.0 <0.9.0;

import {Test} from "@graphprotocol/contracts/contracts/staking/IHorizonStaking.sol";

contract SimpleTest is Test {
function test() external pure returns (uint256) {
return 42;
}
}
12 changes: 12 additions & 0 deletions packages/subgraph-service/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @ts-check
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */

const eslintGraphConfig = require('eslint-graph-config')
module.exports = [
...eslintGraphConfig.default,
{
ignores: ['typechain-types/*'],
},
]
6 changes: 6 additions & 0 deletions packages/subgraph-service/foundry.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[profile.default]
src = 'contracts'
out = 'build'
libs = ['node_modules', 'lib']
test = 'test'
cache_path = 'cache_forge'
12 changes: 12 additions & 0 deletions packages/subgraph-service/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import '@nomicfoundation/hardhat-foundry'
import '@nomicfoundation/hardhat-toolbox'
import { HardhatUserConfig } from 'hardhat/config'

const config: HardhatUserConfig = {
solidity: '0.8.25',
paths: {
artifacts: './build/contracts',
},
}

export default config
1 change: 1 addition & 0 deletions packages/subgraph-service/lib/forge-std
Submodule forge-std added at ae570f
54 changes: 54 additions & 0 deletions packages/subgraph-service/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "@graphprotocol/subgraph-service",
"version": "0.0.1",
"description": "",
"author": "The Graph Team",
"license": "GPL-2.0-or-later",
"scripts": {
"lint:ts": "eslint '**/*.{js,ts}' --fix",
"lint:sol": "forge fmt",
"lint": "yarn lint:ts && yarn lint:sol",
"clean": "rm -rf build cache typechain-types",
"build": "hardhat compile",
"test": "forge test"
},
"devDependencies": {
"@graphprotocol/contracts": "workspace:^7.0.0",
"@nomicfoundation/hardhat-chai-matchers": "^2.0.0",
"@nomicfoundation/hardhat-ethers": "^3.0.0",
"@nomicfoundation/hardhat-foundry": "^1.1.1",
"@nomicfoundation/hardhat-network-helpers": "^1.0.0",
"@nomicfoundation/hardhat-toolbox": "^4.0.0",
"@nomicfoundation/hardhat-verify": "^2.0.0",
"@typechain/ethers-v6": "^0.5.0",
"@typechain/hardhat": "^9.0.0",
"@types/chai": "^4.2.0",
"@types/mocha": ">=9.1.0",
"@types/node": ">=16.0.0",
"chai": "^4.2.0",
"eslint": "^8.56.0",
"eslint-graph-config": "workspace:^0.0.1",
"ethers": "^6.4.0",
"hardhat": "^2.20.1",
"hardhat-gas-reporter": "^1.0.8",
"lint-staged": "^15.2.2",
"solidity-coverage": "^0.8.0",
"ts-node": ">=8.0.0",
"typechain": "^8.3.0",
"typescript": "^5.3.3"
},
"lint-staged": {
"contracts/**/*.sol": [
"yarn lint:sol"
],
"**/*.ts": [
"yarn lint:ts"
],
"**/*.js": [
"yarn lint:ts"
],
"**/*.json": [
"yarn lint:ts"
]
}
}
5 changes: 5 additions & 0 deletions packages/subgraph-service/remappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@graphprotocol/contracts/=node_modules/@graphprotocol/contracts/
forge-std/=lib/forge-std/src/
ds-test/=lib/forge-std/lib/ds-test/src/
eth-gas-reporter/=node_modules/eth-gas-reporter/
hardhat/=node_modules/hardhat/
28 changes: 28 additions & 0 deletions packages/subgraph-service/scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ethers } from 'hardhat'

async function main() {
const currentTimestampInSeconds = Math.round(Date.now() / 1000)
const unlockTime = currentTimestampInSeconds + 60

const lockedAmount = ethers.parseEther('0.001')
const a = 1
console.log(a)
const lock = await ethers.deployContract('Lock', [unlockTime], {
value: lockedAmount,
})

await lock.waitForDeployment()

console.log(
`Lock with ${ethers.formatEther(
lockedAmount,
)}ETH and unlock timestamp ${unlockTime} deployed to ${typeof lock.target == 'string' ? lock.target : ''}`,
)
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error)
process.exitCode = 1
})
Loading

0 comments on commit 8a32061

Please sign in to comment.