Skip to content

Commit

Permalink
Merge pull request #6 from AcalaNetwork/added-homa-subql
Browse files Browse the repository at this point in the history
added stats subql
  • Loading branch information
shunjizhan authored May 14, 2024
2 parents 77d913e + bd5412b commit fbd1af3
Show file tree
Hide file tree
Showing 25 changed files with 515 additions and 169 deletions.
60 changes: 60 additions & 0 deletions subql-old/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# These are some examples of commonly ignored file patterns.
# You should customize this list as applicable to your project.
# Learn more about .gitignore:
# https://www.atlassian.com/git/tutorials/saving-changes/gitignore

# Node artifact files
node_modules/
dist/

# lock files
yarn.lock
package-lock.json

# Compiled Java class files
*.class

# Compiled Python bytecode
*.py[cod]

# Log files
*.log

# Package files
*.jar

# Generated files
target/
dist/
src/types
project.yaml

# JetBrains IDE
.idea/

# Unit test reports
TEST*.xml

# Generated by MacOS
.DS_Store

# Generated by Windows
Thumbs.db

# Applications
*.app
*.exe
*.war

# Large media files
*.mp4
*.tiff
*.avi
*.flv
*.mov
*.wmv

.data
.yarn

.DS_Store
1 change: 1 addition & 0 deletions subql-old/.project-cid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
QmR1bNNxccr9A8523Wq4WWgbDr7euY7jCBPq4gnAWuFv6N
10 changes: 10 additions & 0 deletions subql-old/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Euphrates Subql
Subql that indexes [Euphrates](https://farm.acala.network/) transactions.

## Deploy
publish to IPFS
```
export SUBQL_ACCESS_TOKEN=<your token>
subql publish
```
File renamed without changes.
File renamed without changes.
64 changes: 64 additions & 0 deletions subql-old/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
version: "3"

services:
postgres:
image: postgres:12-alpine
ports:
- 5432:5432
environment:
POSTGRES_PASSWORD: postgres
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 2s
timeout: 5s
retries: 100

subquery-node:
image: onfinality/subql-node-ethereum:latest
depends_on:
"postgres":
condition: service_healthy
restart: always
environment:
DB_USER: postgres
DB_PASS: postgres
DB_DATABASE: postgres
DB_HOST: postgres
DB_PORT: 5432
volumes:
- ./:/app
command:
- -f=ipfs://QmR1bNNxccr9A8523Wq4WWgbDr7euY7jCBPq4gnAWuFv6N
# - -f=./app
- --db-schema=euphrates
- --workers=4
- --batch-size=30
- --unfinalized-blocks=false
- --disable-historical
- --network-endpoint=https://eth-rpc-acala.aca-api.network
healthcheck:
test: ["CMD", "curl", "-f", "http://subquery-node:3000/ready"]
interval: 3s
timeout: 5s
retries: 10

graphql-engine:
image: onfinality/subql-query:latest
ports:
- 3000:3000
depends_on:
"postgres":
condition: service_healthy
"subquery-node":
condition: service_healthy
restart: always
environment:
DB_USER: postgres
DB_PASS: postgres
DB_DATABASE: postgres
DB_HOST: postgres
DB_PORT: 5432
command:
- --name=app
- --playground
- --indexer=http://subquery-node:3000
37 changes: 37 additions & 0 deletions subql-old/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "euphrates-analytics-subql",
"version": "0.0.2",
"description": "Subql for Acala Euphrates",
"main": "dist/index.js",
"scripts": {
"generate-types": "typechain --target=ethers-v5 --out-dir=./src/typechain abis/*.json",
"build": "subql build",
"codegen": "subql codegen && yarn generate-types",
"start:docker": "docker-compose pull && docker-compose up --remove-orphans",
"dev": "yarn codegen && yarn build && docker-compose pull && docker-compose up --remove-orphans",
"prepack": "rm -rf dist && npm run build",
"test": "jest"
},
"files": [
"dist",
"schema.graphql",
"project.yaml"
],
"author": "Acala Team",
"license": "MIT",
"dependencies": {
"@subql/common": "latest",
"@subql/types-ethereum": "latest",
"@subql/validator": "latest",
"abab": "^2.0.6",
"assert": "^2.0.0"
},
"devDependencies": {
"@subql/cli": "latest",
"@subql/node-ethereum": "latest",
"@subql/testing": "latest",
"@subql/types": "latest",
"ethers": "^5.7.2",
"typescript": "latest"
}
}
78 changes: 78 additions & 0 deletions subql-old/project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import {
EthereumProject,
EthereumDatasourceKind,
EthereumHandlerKind,
} from "@subql/types-ethereum";

// Can expand the Datasource processor types via the generic param
const project: EthereumProject = {
specVersion: "1.0.0",
version: "0.0.1",
name: "euphrates analytics",
description:
"",
runner: {
node: {
name: "@subql/node-ethereum",
version: ">=3.0.0",
},
query: {
name: "@subql/query",
version: "*",
},
},
schema: {
file: "./schema.graphql",
},
network: {
/**
* chainId is the EVM Chain ID, for Ethereum this is 1
* https://chainlist.org/chain/1
*/
chainId: "787",
/**
* This endpoint must be a public non-pruned archive node
* Public nodes may be rate limited, which can affect indexing speed
* When developing your project we suggest getting a private API key
* You can get them from OnFinality for free https://app.onfinality.io
* https://documentation.onfinality.io/support/the-enhanced-api-service
*/
endpoint: [ "https://eth-rpc-acala.aca-api.network" ],
// dictionary: "https://gx.api.subquery.network/sq/subquery/eth-dictionary",
},
dataSources: [
{
kind: EthereumDatasourceKind.Runtime,
startBlock: 4440289,
options: {
// Must be a key of assets
abi: "staking",
address: "0x7Fe92EC600F15cD25253b421bc151c51b0276b7D",
},
assets: new Map([["staking", { file: "./abis/Staking.json" }]]),
mapping: {
file: "./dist/index.js",
handlers: [
{
kind: EthereumHandlerKind.Event,
handler: "handleStake",
filter: {
topics: ["Stake(address,uint256,uint256)"],
},
},
{
kind: EthereumHandlerKind.Event,
handler: "handleUnstake",
filter: {
topics: ["Unstake(address,uint256,uint256)"],
},
},
],
},
},
],
repository: "https://github.com/subquery/ethereum-subql-starter",
};

// Must set default to the project instance
export default project;
11 changes: 11 additions & 0 deletions subql-old/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type StakeTx @entity {
id: ID!
type: Int! # 1: stake, 0: unstake
from: String!
txHash: String!
blockNumber: Int!
timestamp: Date!
poolId: Int!
amount: BigInt! # dot equivalent amount
originalAmount: BigInt # original amount
}
6 changes: 6 additions & 0 deletions subql-old/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// https://github.com/subquery/subql/issues/1277#issuecomment-1404181415
import { atob } from 'abab';
global.atob = atob as any;

//Exports all handler functions
export * from "./mappings/mappingHandlers";
69 changes: 69 additions & 0 deletions subql-old/src/mappings/mappingHandlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import assert from "assert";

import {
StakeLog, UnstakeLog,
} from "../types/abi-interfaces/Staking";
import { StakeTx } from "../types";
import { Homa__factory } from "../typechain";

const HOMA_ADDR = '0x0000000000000000000000000000000000000805';
const homa = Homa__factory.connect(HOMA_ADDR, api);

export const ldotToDotAmount = async (amount: bigint): Promise<bigint> => {
const exchangeRate = await homa.getExchangeRate();
return amount * exchangeRate.toBigInt() / BigInt(1e18);
}

export async function handleStake(log: StakeLog): Promise<void> {
logger.info("new staking at block " + log.blockNumber.toString());
assert(log.args, "Require args on the logs");

const poolId = log.args.poolId.toNumber();
let amount = log.args.amount.toBigInt();
const originalAmount = amount;

if (poolId === 5) { // amount is in LDOT
amount = await ldotToDotAmount(amount)
}

const tx = StakeTx.create({
type: 1, // stake
id: log.transactionHash,
txHash: log.transactionHash,
from: log.transaction.from,
blockNumber: log.blockNumber,
timestamp: new Date(Number(log.transaction.blockTimestamp * 1000n) ),
poolId,
amount,
originalAmount,
});

await tx.save();
}

export async function handleUnstake(log: UnstakeLog): Promise<void> {
logger.info("new unstaking at block " + log.blockNumber.toString());
assert(log.args, "Require args on the logs");

const poolId = log.args.poolId.toNumber();
let amount = log.args.amount.toBigInt();
const originalAmount = amount;

if (poolId === 5) { // rawAmount is in LDOT
amount = await ldotToDotAmount(amount)
}

const tx = StakeTx.create({
type: 0, // unstake
id: log.transactionHash,
txHash: log.transactionHash,
from: log.transaction.from,
blockNumber: log.blockNumber,
timestamp: new Date(Number(log.transaction.blockTimestamp * 1000n) ),
poolId,
amount,
originalAmount,
});

await tx.save();
}
20 changes: 20 additions & 0 deletions subql-old/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"esModuleInterop": true,
"declaration": true,
"importHelpers": true,
"resolveJsonModule": true,
"module": "commonjs",
"outDir": "dist",
"rootDir": "src",
"target": "ESNext",
"strict": true
},
"include": [
"src/**/*",
"node_modules/@subql/types-core/dist/global.d.ts",
"node_modules/@subql/types-ethereum/dist/global.d.ts"
]
}
4 changes: 1 addition & 3 deletions subql/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,4 @@ Thumbs.db
*.wmv

.data
.yarn

.DS_Store
.eslintcache
2 changes: 1 addition & 1 deletion subql/.project-cid
Original file line number Diff line number Diff line change
@@ -1 +1 @@
QmR1bNNxccr9A8523Wq4WWgbDr7euY7jCBPq4gnAWuFv6N
QmcYcYtBE9qFghbJaqSMGMXYjR7EVUcr7StiMJhxEYnF4X
11 changes: 2 additions & 9 deletions subql/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
# Euphrates Subql
Subql that indexes [Euphrates](https://farm.acala.network/) transactions.
# Acala Stats Subql

## Deploy
publish to IPFS
```
export SUBQL_ACCESS_TOKEN=<your token>
subql publish
```
Subquery for Acala Stats
Loading

0 comments on commit fbd1af3

Please sign in to comment.