Skip to content

Commit

Permalink
[SubGraph] Add mainchain & update sidechain
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Apr 18, 2024
1 parent 6fce6fd commit 3745b7d
Show file tree
Hide file tree
Showing 24 changed files with 554 additions and 22 deletions.
4 changes: 4 additions & 0 deletions packages/subgraph-mainchain/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
NETWORK_NAME=mainchain
GRAPH_KEY=DENET_KEY
SUBGRAPH_NAME=bosagora/dms-osx
SUBGRAPH_VERSION=v0.0.1
4 changes: 4 additions & 0 deletions packages/subgraph-mainchain/.env.mainchain
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
NETWORK_NAME=mainchain
GRAPH_KEY=DENET_KEY
SUBGRAPH_NAME=bosagora/dms-osx
SUBGRAPH_VERSION=v0.0.1
3 changes: 3 additions & 0 deletions packages/subgraph-mainchain/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
subgraph.yaml
generated/
build/
14 changes: 14 additions & 0 deletions packages/subgraph-mainchain/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Copyright (c) 2023 BOSAGORA Foundation

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
21 changes: 21 additions & 0 deletions packages/subgraph-mainchain/manifest/data/mainchain.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"info": "# Do not edit subgraph.yaml,this is a generated file. \n# Instead, edit subgraph.placeholder.yaml and run: yarn manifest",
"network": "mainchain",
"dataSources": {
"LoyaltyToken": {
"name": "LoyaltyToken",
"address": "0xB1A90a5C6e30d64Ab6f64C30eD392F46eDBcb022",
"startBlock": 1
},
"ChainBridge": {
"name": "ChainBridge",
"address": "0x979a62090BDCff36F2e140F6915fbAdA7510cb6a",
"startBlock": 1
},
"LoyaltyBridge": {
"name": "LoyaltyBridge",
"address": "0x4Ca91738C7cD24895467c6d550D96BE8dC4b33AA",
"startBlock": 1
}
}
}
74 changes: 74 additions & 0 deletions packages/subgraph-mainchain/manifest/subgraph.placeholder.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
specVersion: 0.0.5
description: A template for Sub Graphs
repository: https://github.com/bosagora/dms-osx
schema:
file: ./schema.graphql
dataSources:
- kind: ethereum
name: {{dataSources.LoyaltyToken.name}}
network: {{network}}
source:
address: '{{dataSources.LoyaltyToken.address}}'
abi: LoyaltyToken
startBlock: {{dataSources.LoyaltyToken.startBlock}}
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
entities:
- Transfer
abis:
- name: LoyaltyToken
file: $DMS_OSX_MODULE/artifacts/loyalty-tokens/contracts/LoyaltyToken.sol/LoyaltyToken.json
eventHandlers:
- event: Transfer(indexed address,indexed address,uint256)
handler: handleTransfer
file: ./src/loyalty-token.ts

- kind: ethereum
name: {{dataSources.ChainBridge.name}}
network: {{network}}
source:
address: '{{dataSources.ChainBridge.address}}'
abi: ChainBridge
startBlock: {{dataSources.ChainBridge.startBlock}}
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
entities:
- BridgeDeposited
- BridgeWithdrawn
abis:
- name: ChainBridge
file: $DMS_OSX_MODULE/artifacts/dms-bridge-contracts/contracts/bridge/Bridge.sol/Bridge.json
eventHandlers:
- event: BridgeDeposited(bytes32,bytes32,address,uint256,uint256)
handler: handleBridgeDeposited
- event: BridgeWithdrawn(bytes32,bytes32,address,uint256,uint256)
handler: handleBridgeWithdrawn
file: ./src/chain-bridge.ts

- kind: ethereum
name: {{dataSources.LoyaltyBridge.name}}
network: {{network}}
source:
address: '{{dataSources.LoyaltyBridge.address}}'
abi: LoyaltyBridge
startBlock: {{dataSources.LoyaltyBridge.startBlock}}
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
entities:
- BridgeDeposited
- BridgeWithdrawn
abis:
- name: LoyaltyBridge
file: $DMS_OSX_MODULE/artifacts/dms-bridge-contracts/contracts/bridge/Bridge.sol/Bridge.json
eventHandlers:
- event: BridgeDeposited(bytes32,bytes32,address,uint256,uint256)
handler: handleBridgeDeposited
- event: BridgeWithdrawn(bytes32,bytes32,address,uint256,uint256)
handler: handleBridgeWithdrawn
file: ./src/loyalty-bridge.ts
29 changes: 29 additions & 0 deletions packages/subgraph-mainchain/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "dms-osx-subgraph-mainchain",
"version": "1.0.0",
"description": "The DMS Subgraph for mainchain",
"homepage": "https://github.com/bosagora/dms-osx",
"author": "BOSagora Foundation",
"license": "AGPL-3.0-or-later",
"scripts": {
"build:contracts": "cd ../contracts && yarn build",
"manifest": "scripts/build-manifest.sh",
"build": "scripts/build-subgraph.sh",
"deploy": "scripts/deploy-subgraph.sh",
"create:mainchain": "graph create bosagora/dms-osx-mainchain --node http://localhost:8020",
"remove:mainchain": "graph remove bosagora/dms-osx-mainchain --node http://localhost:8020",
"deploy:mainchain": "LOCAL=true scripts/deploy-subgraph.sh",
"formatting:check": "prettier '**/*.{json,ts,js}' -c",
"formatting:write": "prettier '**/*.{json,ts,js}' --write"
},
"devDependencies": {
"@graphprotocol/graph-cli": "^0.55.0",
"@graphprotocol/graph-ts": "^0.31.0",
"matchstick-as": "^0.5.1",
"mustache": "^4.2.0",
"prettier": "^2.5.1",
"prettier-plugin-solidity": "^1.1.1",
"tslint": "^6.1.3",
"tslint-config-prettier": "^1.18.0"
}
}
57 changes: 57 additions & 0 deletions packages/subgraph-mainchain/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
type LoyaltyTransfer @entity(immutable: true) {
id: Bytes!
from: Bytes! # address
to: Bytes! # address
value: BigInt! # uint256
blockNumber: BigInt!
blockTimestamp: BigInt!
transactionHash: Bytes!
}

type ChainBridgeDeposited @entity(immutable: true) {
id: Bytes!
tokenId: Bytes! # bytes32
depositId: Bytes! # bytes32
account: Bytes! # address
amount: BigInt! # uint256
balance: BigInt! # uint256
blockNumber: BigInt!
blockTimestamp: BigInt!
transactionHash: Bytes!
}

type ChainBridgeWithdrawn @entity(immutable: true) {
id: Bytes!
tokenId: Bytes! # bytes32
withdrawId: Bytes! # bytes32
account: Bytes! # address
amount: BigInt! # uint256
balance: BigInt! # uint256
blockNumber: BigInt!
blockTimestamp: BigInt!
transactionHash: Bytes!
}

type LoyaltyBridgeDeposited @entity(immutable: true) {
id: Bytes!
tokenId: Bytes! # bytes32
depositId: Bytes! # bytes32
account: Bytes! # address
amount: BigInt! # uint256
balance: BigInt! # uint256
blockNumber: BigInt!
blockTimestamp: BigInt!
transactionHash: Bytes!
}

type LoyaltyBridgeWithdrawn @entity(immutable: true) {
id: Bytes!
tokenId: Bytes! # bytes32
withdrawId: Bytes! # bytes32
account: Bytes! # address
amount: BigInt! # uint256
balance: BigInt! # uint256
blockNumber: BigInt!
blockTimestamp: BigInt!
transactionHash: Bytes!
}
29 changes: 29 additions & 0 deletions packages/subgraph-mainchain/scripts/build-manifest.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash


if [ -f .env ]
then
export $(cat .env | sed 's/#.*//g' | xargs)
fi

if [ -z "$NETWORK_NAME" ]
then
echo "env is not set, exiting..."
exit -1
else
echo "env Network is set to: $NETWORK_NAME"
fi

FILE=$NETWORK_NAME'.json'
DATA=manifest/data/$FILE

DMS_OSX_MODULE=$(node -e 'console.log(require("path").dirname(require.resolve("dms-osx-artifacts/package.json")))')

echo 'Generating manifest from data file: '$DATA
cat $DATA

mustache \
$DATA \
manifest/subgraph.placeholder.yaml \
| sed -e "s#\$DMS_OSX_MODULE#$DMS_OSX_MODULE#g" \
> subgraph.yaml
14 changes: 14 additions & 0 deletions packages/subgraph-mainchain/scripts/build-subgraph.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh

set -eu

if [ ! -f "./subgraph.yaml" ]; then
echo "The file subgraph.yaml doesn’t exist. Did you run: yarn manifest?"
exit 1
fi

rm -rf generated
rm -rf build

graph codegen
graph build
52 changes: 52 additions & 0 deletions packages/subgraph-mainchain/scripts/deploy-subgraph.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env bash

if [ -f .env ]
then
export $(cat .env | sed 's/#.*//g' | xargs)
fi

if [ -z "$NETWORK_NAME" ] || [ -z "$SUBGRAPH_NAME" ] || [ -z "$GRAPH_KEY" ] || [ -z "$SUBGRAPH_VERSION" ]
then
echo "env variables are not set properly, exiting..."
exit -1
fi

# Exit script as soon as a command fails.
set -o errexit

# Build manifest
echo ''
echo '> Building manifest file subgraph.yaml'
./scripts/build-manifest.sh

# Generate types
echo ''
echo '> Generating types'
graph codegen

# Prepare subgraph name
FULLNAME=$SUBGRAPH_NAME-$NETWORK_NAME
if [ "$STAGING" ]; then
FULLNAME=$FULLNAME-staging
fi

echo ''
echo '> Deploying subgraph: '$FULLNAME

# Deploy subgraph
if [ "$LOCAL" ]
then
graph deploy $FULLNAME \
--ipfs http://localhost:5001 \
--node http://localhost:8020
else
NODE_URL='https://subgraph-deploy-mainnet.dao.bosagora.org'
graph deploy $FULLNAME \
--version-label $SUBGRAPH_VERSION \
--node $NODE_URL \
--deploy-key $GRAPH_KEY > deploy-output.txt

SUBGRAPH_ID=$(grep "Build completed:" deploy-output.txt | grep -oE "Qm[a-zA-Z0-9]{44}")
rm deploy-output.txt
echo "The Graph deployment complete: ${SUBGRAPH_ID}"
fi
36 changes: 36 additions & 0 deletions packages/subgraph-mainchain/src/chain-bridge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
BridgeDeposited as BridgeDepositedEvent,
BridgeWithdrawn as BridgeWithdrawnEvent,
} from "../generated/ChainBridge/ChainBridge";
import { ChainBridgeDeposited, ChainBridgeWithdrawn } from "../generated/schema";
import { AmountUnit } from "dms-osx-subgraph-sidechain/src/utils";

export function handleBridgeDeposited(event: BridgeDepositedEvent): void {
let entity = new ChainBridgeDeposited(event.transaction.hash.concatI32(event.logIndex.toI32()));
entity.tokenId = event.params.tokenId;
entity.depositId = event.params.depositId;
entity.account = event.params.account;
entity.amount = event.params.amount.div(AmountUnit);
entity.balance = event.params.balance.div(AmountUnit);

entity.blockNumber = event.block.number;
entity.blockTimestamp = event.block.timestamp;
entity.transactionHash = event.transaction.hash;

entity.save();
}

export function handleBridgeWithdrawn(event: BridgeWithdrawnEvent): void {
let entity = new ChainBridgeWithdrawn(event.transaction.hash.concatI32(event.logIndex.toI32()));
entity.tokenId = event.params.tokenId;
entity.withdrawId = event.params.withdrawId;
entity.account = event.params.account;
entity.amount = event.params.amount.div(AmountUnit);
entity.balance = event.params.balance.div(AmountUnit);

entity.blockNumber = event.block.number;
entity.blockTimestamp = event.block.timestamp;
entity.transactionHash = event.transaction.hash;

entity.save();
}
36 changes: 36 additions & 0 deletions packages/subgraph-mainchain/src/loyalty-bridge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
BridgeDeposited as BridgeDepositedEvent,
BridgeWithdrawn as BridgeWithdrawnEvent,
} from "../generated/LoyaltyBridge/LoyaltyBridge";
import { LoyaltyBridgeDeposited, LoyaltyBridgeWithdrawn } from "../generated/schema";
import { AmountUnit } from "dms-osx-subgraph-sidechain/src/utils";

export function handleBridgeDeposited(event: BridgeDepositedEvent): void {
let entity = new LoyaltyBridgeDeposited(event.transaction.hash.concatI32(event.logIndex.toI32()));
entity.tokenId = event.params.tokenId;
entity.depositId = event.params.depositId;
entity.account = event.params.account;
entity.amount = event.params.amount.div(AmountUnit);
entity.balance = event.params.balance.div(AmountUnit);

entity.blockNumber = event.block.number;
entity.blockTimestamp = event.block.timestamp;
entity.transactionHash = event.transaction.hash;

entity.save();
}

export function handleBridgeWithdrawn(event: BridgeWithdrawnEvent): void {
let entity = new LoyaltyBridgeWithdrawn(event.transaction.hash.concatI32(event.logIndex.toI32()));
entity.tokenId = event.params.tokenId;
entity.withdrawId = event.params.withdrawId;
entity.account = event.params.account;
entity.amount = event.params.amount.div(AmountUnit);
entity.balance = event.params.balance.div(AmountUnit);

entity.blockNumber = event.block.number;
entity.blockTimestamp = event.block.timestamp;
entity.transactionHash = event.transaction.hash;

entity.save();
}
Loading

0 comments on commit 3745b7d

Please sign in to comment.