Skip to content
Open
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
163 changes: 163 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
# Bright Union TheGraph schemas

## Installation

Clone the project

```bash
git clone git@github.com:Bright-Union/brightprotocol-graph.git
```

Go to the project directory

```bash
cd brightprotocol-graph
```

Install dependencies

```bash
npm install
```

## Run Locally

**Requirements**: Docker, Node 14+

Start the local dev server

```bash
docker-compose up
```

## Usage

Entity Mapper Schema Subgraph

### Listen to new events

1. Create new type in `schema.graphql`

Example:

```graphql
type CoverBought @entity {
id: ID!
coverId: BigInt!
buyer: Bytes! # address
contractAddress: Bytes! # address
feePercentage: BigInt!
coverPrice: BigInt!
network: String!
distributor: Bytes!
}
```

2. Update subgraph datasource entities with new type

Example:

```yaml
entities:
- CoverBought
```

3. Update subgraph datasource event Handlers with new type

Example:

```yaml

eventHandlers:
- event: CoverBought(indexed uint256,indexed address,indexed
address,uint256,uint256)
handler: handleCoverBought
```

4. Generate entities with cli

Run `graph codegen` to generate entities and event handlers

6. Copy or reuse generated entity from `generated` folder.

Generated entities might be missing some fields, so you might need to add them.

It is recommended to copy the entity from `generated` folder to `src/entities` folder and update the field.

Use `src/entities/CoverBought.entity.ts` as an example.

7. Define mappings for entity field.

Example:
`src/mappings.ts`

```typescript
export function handleCoverBought(event: CoverBoughtEvent): void {
let entity = new CoverBought(event.params.coverId.toHexString());

entity.id = event.params.coverId.toHexString()
entity.timestamp = event.block.timestamp
entity.coverId = event.params.coverId
entity.buyer = event.params.buyer
entity.contractAddress = event.params.contractAddress
entity.feePercentage = event.params.feePercentage
entity.coverPrice = event.params.coverPrice
entity.network = dataSource.network()
entity.distributor = dataSource.address()

entity.save()
}
```

8. Build and test with `npm run deploy-local`

## Build the graph from networks

Current implementation is using Mustache CLI to generate `subgraph.yaml` file from `subgraph.template.yaml` file.

So you can add new contract in `netwroks.json` file.

```json
{
"mainnet": [
{
"name": "nexus_distributor",
"address": "0x3756c3c9374f38e0d9aacb637fed1641504a5b28",
"startBlock": 13212759
}
]
}
```

To build template run `npm run prep-subgraph`, which runs `mustache networks.json subgraph.template.yaml > subgraph.yaml`

Deploy using `graph deploy --studio bu-distributors-mainnet`

### Building for networks other than Ethereum Mainnet

replace *mainnet* keywords in `subgraph.template.yaml` file with new the network name that matches network listed in `networks.json`.
```yaml
{{#mainnet}}
- kind: ethereum
name: {{name}}
network: mainnet
source:
address: "{{address}}"
abi: IDistributor
startBlock: {{startBlock}}
mapping:
kind: ethereum/events
apiVersion: 0.0.5
language: wasm/assemblyscript
entities:
- CoverBought
abis:
- name: IDistributor
file: ./abis/IDistributor.json
eventHandlers:
- event: CoverBought(indexed uint256,indexed address,indexed
address,uint256,uint256)
handler: handleCoverBought
file: ./src/mapping.ts
{{/mainnet}}
```
41 changes: 26 additions & 15 deletions networks.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,45 @@
{
"mainnet": {
"nexus_distributor": {
"address": "0x0000000000000000000000000000000000000000",
"mainnet": [
{
"name": "nexus_distributor",
"address": "0x3756c3c9374f38e0d9aacb637fed1641504a5b28",
"startBlock": 13212759
},
"insurace_distributor": {
{
"name": "nexus_distributor_v2",
"address": "0x425b3a68f1fd5de26b4b9f4be8049e36406b187a",
"startBlock": 13212759
},
{
"name": "insurace_distributor",
"address": "0x98fb0e1191651b6292d3482cebf54e6d35542ca4",
"startBlock": 14416212
},
"ease_distributor": {
{
"name": "ease_distributor",
"address": "0xF11186032dF1E2723600A253A975fcb3dC7bd419",
"startBlock": 15135842
}
},
"bsc": {
"insurace_distributor": {
],
"bsc": [
{
"name": "insurace_distributor",
"address": "0x68f4c3a5ac3ea721c67fc1061e02d39fb55e9fa9",
"startBlock": 12753267
}
},
"matic": {
"insurace_distributor": {
],
"matic": [
{
"name": "insurace_distributor",
"address": "0x371BdDD1321D24C130d79Ec85f125dd34928B054",
"startBlock": 21512216
}
},
"avalanche": {
"insurace_distributor": {
],
"avalanche": [
{
"name": "insurace_distributor",
"address": "0x375d9567b483814d1275869b543101b725779b67",
"startBlock": 11943843
}
}
]
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"scripts": {
"codegen": "graph codegen",
"build": "graph build",
"prep-subgraph": "mustache networks.json subgraph.template.yaml > subgraph.yaml",
"deploy": "graph deploy --node https://api.studio.thegraph.com/deploy/ brightunion_distributors",
"create-local": "graph create --node http://localhost:8020/ brightunion_distributors",
"remove-local": "graph remove --node http://localhost:8020/ brightunion_distributors",
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 brightunion_distributors"
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 brightunion_distributors",
"deploy-studio": "graph build && graph deploy --studio bu-distributors-mainnet"
},
"dependencies": {
"@graphprotocol/graph-cli": "0.32.0",
"@graphprotocol/graph-ts": "0.27.0"
"@graphprotocol/graph-ts": "0.27.0",
"mustache": "^4.2.0"
}
}
67 changes: 67 additions & 0 deletions subgraph.master.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
specVersion: 0.0.4
schema:
file: ./schema.graphql
dataSources:
- kind: ethereum
name: nexus_distributor
network: mainnet
source:
address: "0x0000000000000000000000000000000000000000"
abi: IDistributor
startBlock: 13212759
mapping:
kind: ethereum/events
apiVersion: 0.0.5
language: wasm/assemblyscript
entities:
- CoverBought
abis:
- name: IDistributor
file: ./abis/IDistributor.json
eventHandlers:
- event: CoverBought(indexed uint256,indexed address,indexed
address,uint256,uint256)
handler: handleCoverBought
file: ./src/mapping.ts
- kind: ethereum
name: insurace_distributor
network: mainnet
source:
address: "0x98fb0e1191651b6292d3482cebf54e6d35542ca4"
abi: IDistributor
startBlock: 14416212
mapping:
kind: ethereum/events
apiVersion: 0.0.5
language: wasm/assemblyscript
entities:
- CoverBought
abis:
- name: IDistributor
file: ./abis/IDistributor.json
eventHandlers:
- event: CoverBought(indexed uint256,indexed address,indexed
address,uint256,uint256)
handler: handleCoverBought
file: ./src/mapping.ts
- kind: ethereum
name: ease_distributor
network: mainnet
source:
address: "0xF11186032dF1E2723600A253A975fcb3dC7bd419"
abi: IDistributor
startBlock: 15135842
mapping:
kind: ethereum/events
apiVersion: 0.0.5
language: wasm/assemblyscript
entities:
- CoverBought
abis:
- name: IDistributor
file: ./abis/IDistributor.json
eventHandlers:
- event: CoverBought(indexed uint256,indexed address,indexed
address,uint256,uint256)
handler: handleCoverBought
file: ./src/mapping.ts
27 changes: 27 additions & 0 deletions subgraph.template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
specVersion: 0.0.4
schema:
file: ./schema.graphql
dataSources:
{{#mainnet}}
- kind: ethereum
name: {{name}}
network: mainnet
source:
address: "{{address}}"
abi: IDistributor
startBlock: {{startBlock}}
mapping:
kind: ethereum/events
apiVersion: 0.0.5
language: wasm/assemblyscript
entities:
- CoverBought
abis:
- name: IDistributor
file: ./abis/IDistributor.json
eventHandlers:
- event: CoverBought(indexed uint256,indexed address,indexed
address,uint256,uint256)
handler: handleCoverBought
file: ./src/mapping.ts
{{/mainnet}}
23 changes: 22 additions & 1 deletion subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,28 @@ dataSources:
name: nexus_distributor
network: mainnet
source:
address: "0x0000000000000000000000000000000000000000"
address: "0x3756c3c9374f38e0d9aacb637fed1641504a5b28"
abi: IDistributor
startBlock: 13212759
mapping:
kind: ethereum/events
apiVersion: 0.0.5
language: wasm/assemblyscript
entities:
- CoverBought
abis:
- name: IDistributor
file: ./abis/IDistributor.json
eventHandlers:
- event: CoverBought(indexed uint256,indexed address,indexed
address,uint256,uint256)
handler: handleCoverBought
file: ./src/mapping.ts
- kind: ethereum
name: nexus_distributor_v2
network: mainnet
source:
address: "0x425b3a68f1fd5de26b4b9f4be8049e36406b187a"
abi: IDistributor
startBlock: 13212759
mapping:
Expand Down
Loading