Skip to content

Commit dac1856

Browse files
lacoop6tumihaisc
andauthored
Add swap volume, consume volume to subgraph (#64)
* Add swap volume, consume volume to subgraph * add Global object, run lint Co-authored-by: mihaisc <[email protected]>
1 parent 9b3c466 commit dac1856

10 files changed

+13349
-13355
lines changed

abis/FixedRateExchange.json

+13,142-13,244
Large diffs are not rendered by default.

abis/Metadata.json

+11-37
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,14 @@
110110
"ast": {
111111
"absolutePath": "/ocean/ocean-contracts/contracts/metadata/Metadata.sol",
112112
"exportedSymbols": {
113-
"Metadata": [
114-
5158
115-
]
113+
"Metadata": [5158]
116114
},
117115
"id": 5159,
118116
"nodeType": "SourceUnit",
119117
"nodes": [
120118
{
121119
"id": 5072,
122-
"literals": [
123-
"solidity",
124-
"0.5",
125-
".7"
126-
],
120+
"literals": ["solidity", "0.5", ".7"],
127121
"nodeType": "PragmaDirective",
128122
"src": "0:22:11"
129123
},
@@ -145,9 +139,7 @@
145139
"documentation": "@title Metadata\n \n@dev Metadata stands for Decentralized Document. It allows publishers\n to publish their dataset metadata in decentralized way.\n It follows the Ocean DID Document standard: \n https://github.com/oceanprotocol/OEPs/blob/master/7/v0.2/README.md",
146140
"fullyImplemented": true,
147141
"id": 5158,
148-
"linearizedBaseContracts": [
149-
5158
150-
],
142+
"linearizedBaseContracts": [5158],
151143
"name": "Metadata",
152144
"nodeType": "ContractDefinition",
153145
"nodes": [
@@ -408,9 +400,7 @@
408400
"src": "886:195:11",
409401
"statements": [
410402
{
411-
"assignments": [
412-
5098
413-
],
403+
"assignments": [5098],
414404
"declarations": [
415405
{
416406
"constant": false,
@@ -625,10 +615,7 @@
625615
"id": 5103,
626616
"name": "require",
627617
"nodeType": "Identifier",
628-
"overloadedDeclarations": [
629-
6536,
630-
6537
631-
],
618+
"overloadedDeclarations": [6536, 6537],
632619
"referencedDeclaration": 6537,
633620
"src": "954:7:11",
634621
"typeDescriptions": {
@@ -1248,20 +1235,14 @@
12481235
"legacyAST": {
12491236
"absolutePath": "/ocean/ocean-contracts/contracts/metadata/Metadata.sol",
12501237
"exportedSymbols": {
1251-
"Metadata": [
1252-
5158
1253-
]
1238+
"Metadata": [5158]
12541239
},
12551240
"id": 5159,
12561241
"nodeType": "SourceUnit",
12571242
"nodes": [
12581243
{
12591244
"id": 5072,
1260-
"literals": [
1261-
"solidity",
1262-
"0.5",
1263-
".7"
1264-
],
1245+
"literals": ["solidity", "0.5", ".7"],
12651246
"nodeType": "PragmaDirective",
12661247
"src": "0:22:11"
12671248
},
@@ -1283,9 +1264,7 @@
12831264
"documentation": "@title Metadata\n \n@dev Metadata stands for Decentralized Document. It allows publishers\n to publish their dataset metadata in decentralized way.\n It follows the Ocean DID Document standard: \n https://github.com/oceanprotocol/OEPs/blob/master/7/v0.2/README.md",
12841265
"fullyImplemented": true,
12851266
"id": 5158,
1286-
"linearizedBaseContracts": [
1287-
5158
1288-
],
1267+
"linearizedBaseContracts": [5158],
12891268
"name": "Metadata",
12901269
"nodeType": "ContractDefinition",
12911270
"nodes": [
@@ -1546,9 +1525,7 @@
15461525
"src": "886:195:11",
15471526
"statements": [
15481527
{
1549-
"assignments": [
1550-
5098
1551-
],
1528+
"assignments": [5098],
15521529
"declarations": [
15531530
{
15541531
"constant": false,
@@ -1763,10 +1740,7 @@
17631740
"id": 5103,
17641741
"name": "require",
17651742
"nodeType": "Identifier",
1766-
"overloadedDeclarations": [
1767-
6536,
1768-
6537
1769-
],
1743+
"overloadedDeclarations": [6536, 6537],
17701744
"referencedDeclaration": 6537,
17711745
"src": "954:7:11",
17721746
"typeDescriptions": {
@@ -2415,4 +2389,4 @@
24152389
"userdoc": {
24162390
"methods": {}
24172391
}
2418-
}
2392+
}

docker/.env.example

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
INFURA_PROJECT_ID="xxx"
1+
INFURA_PROJECT_ID="5dfd675029394431a1a86137a281b9bf"

docker/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ You can replace this with anything else in `docker-compose.yaml`.
4444
> To do this, run:
4545
>
4646
> ```
47-
> CONTAINER_ID=$(docker container ls | grep graph-node | cut -d' ' -f1)
48-
> docker exec $CONTAINER_ID /bin/bash -c 'apt install -y iproute2 && ip route' | awk '/^default via /{print $3}'
47+
CONTAINER_ID=$(docker container ls | grep graph-node | cut -d' ' -f1)
48+
docker exec $CONTAINER_ID /bin/bash -c 'apt install -y iproute2 && ip route' | awk '/^default via /{print $3}'
4949
> ```
5050
>
5151
> This will print the host's IP address. Then, put it into `docker-compose.yml`:

schema.graphql

+82-63
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,67 @@
11
type PoolFactory @entity {
2-
id: ID!
2+
id: ID!
3+
4+
totalValueLocked: BigDecimal # total value from all pools expressed in OCEAN
5+
6+
totalOceanLiquidity: BigDecimal! # Total of OCEAN liquidity from all pools
7+
totalSwapVolume: BigDecimal! # All the swap volume in Ocean
8+
totalSwapFee: BigDecimal! # All the swap fee in Ocean
9+
totalOrderVolume: BigDecimal
310

4-
totalValueLocked: BigDecimal # total value from all pools expressed in OCEAN
5-
totalOceanLiquidity: BigDecimal! # Total of OCEAN liquidity from all pools
6-
totalSwapVolume: BigDecimal! # All the swap volume in Ocean
7-
totalSwapFee: BigDecimal! # All the swap fee in Ocean
8-
poolCount: Int! # Number of pools
9-
finalizedPoolCount: Int! # Number of finalized pools
10-
pools: [Pool!] @derivedFrom(field: "factoryID")
11+
poolCount: Int! # Number of pools
12+
finalizedPoolCount: Int! # Number of finalized pools
13+
orderCount: BigInt # Number of total consumes
14+
pools: [Pool!] @derivedFrom(field: "factoryID")
1115
}
1216

13-
type Pool @entity {
14-
id: ID! # Pool address
15-
factoryID: PoolFactory!
16-
controller: Bytes! # Controller address
17-
publicSwap: Boolean! # isPublicSwap
18-
finalized: Boolean! # isFinalized
19-
symbol: String # Pool token symbol
20-
name: String # Pool token name
21-
cap: BigInt # Maximum supply if any
22-
active: Boolean! # isActive
23-
swapFee: BigDecimal! # Swap Fees
24-
totalWeight: BigDecimal!
25-
totalShares: BigDecimal! # Total pool token shares
26-
totalSwapVolume: BigDecimal! # Total swap volume in OCEAN
27-
totalSwapFee: BigDecimal! # Total swap fee in OCEAN
28-
valueLocked: BigDecimal! # value locked in pool expressed in OCEAN (captures both Ocean and Datatoken)
29-
datatokenReserve: BigDecimal! # Total pool reserve of Datatoken
30-
oceanReserve: BigDecimal! # Total pool reserve of OCEAN
31-
spotPrice: BigDecimal!
32-
consumePrice: BigDecimal!
17+
type Global @entity {
18+
id: ID!
19+
totalValueLocked: BigDecimal # total value from all pools expressed in OCEAN
20+
totalOceanLiquidity: BigDecimal! # Total of OCEAN liquidity from all pools
21+
totalSwapVolume: BigDecimal! # All the swap volume in Ocean
22+
totalOrderVolume: BigDecimal
23+
orderCount: BigInt
24+
poolCount: Int!
25+
}
3326

34-
tokenCount: BigInt! # Number of tokens in the pool
35-
holderCount: BigInt! # Number of addresses holding a positive balance of pool shares
36-
joinCount: BigInt! # liquidity has been added
37-
exitCount: BigInt! # liquidity has been removed
38-
swapCount: BigInt!
39-
transactionCount: BigInt! # Number of transactions in this pool involving liquidity changes
40-
datatokenAddress: String!
41-
createTime: Int! # Block time pool was created
42-
tx: Bytes # Pool creation transaction id
43-
tokens: [PoolToken!] @derivedFrom(field: "poolId")
44-
shares: [PoolShare!] @derivedFrom(field: "poolId")
45-
transactions: [PoolTransaction!] @derivedFrom(field: "poolAddress")
46-
transactionsTokenValues: [PoolTransactionTokenValues!]
47-
@derivedFrom(field: "poolAddress")
27+
type Pool @entity {
28+
id: ID! # Pool address
29+
factoryID: PoolFactory!
30+
controller: Bytes! # Controller address
31+
publicSwap: Boolean! # isPublicSwap
32+
finalized: Boolean! # isFinalized
33+
symbol: String # Pool token symbol
34+
name: String # Pool token name
35+
cap: BigInt # Maximum supply if any
36+
active: Boolean! # isActive
37+
swapFee: BigDecimal! # Swap Fees
38+
39+
totalWeight: BigDecimal!
40+
totalShares: BigDecimal! # Total pool token shares
41+
totalSwapVolume: BigDecimal! # Total swap volume in OCEAN
42+
totalSwapFee: BigDecimal! # Total swap fee in OCEAN
43+
44+
valueLocked: BigDecimal! # value locked in pool expressed in OCEAN (captures both Ocean and Datatoken)
45+
datatokenReserve: BigDecimal! # Total pool reserve of Datatoken
46+
oceanReserve: BigDecimal! # Total pool reserve of OCEAN
47+
spotPrice: BigDecimal!
48+
consumePrice: BigDecimal!
49+
50+
tokenCount: BigInt! # Number of tokens in the pool
51+
holderCount: BigInt! # Number of addresses holding a positive balance of pool shares
52+
joinCount: BigInt! # liquidity has been added
53+
exitCount: BigInt! # liquidity has been removed
54+
swapCount: BigInt!
55+
transactionCount: BigInt! # Number of transactions in this pool involving liquidity changes
56+
57+
datatokenAddress: String!
58+
createTime: Int! # Block time pool was created
59+
tx: Bytes # Pool creation transaction id
60+
61+
tokens: [PoolToken!] @derivedFrom(field: "poolId")
62+
shares: [PoolShare!] @derivedFrom(field: "poolId")
63+
transactions: [PoolTransaction!] @derivedFrom(field: "poolAddress")
64+
transactionsTokenValues: [PoolTransactionTokenValues!] @derivedFrom(field: "poolAddress")
4865
}
4966

5067

@@ -118,27 +135,29 @@ type DatatokenFactory @entity {
118135
}
119136

120137
type Datatoken @entity {
121-
id: ID! # token address
122-
factoryID: DatatokenFactory!
123-
124-
symbol: String
125-
name: String
126-
decimals: Int!
127-
address: String!
128-
cap: BigDecimal!
129-
supply: BigDecimal!
130-
minter: User!
131-
publisher: String!
132-
133-
holderCount: BigInt! # Number of addresses holding a balance of datatoken
134-
orderCount: BigInt! # Number of orders executed for this dataset
135-
metadataUpdateCount: BigInt!
136-
137-
createTime: Int! # Block time datatoken was created
138-
tx: Bytes # Datatoken creation transaction id
139-
balances: [TokenBalance!] @derivedFrom(field: "datatokenId")
140-
orders: [TokenOrder!] @derivedFrom(field: "datatokenId")
141-
updates: [MetadataUpdate!] @derivedFrom(field: "datatokenId") # list of MetadataUpdate objects
138+
id: ID! # token address
139+
factoryID: DatatokenFactory!
140+
141+
symbol: String
142+
name: String
143+
decimals: Int!
144+
address: String!
145+
cap: BigDecimal!
146+
supply: BigDecimal!
147+
minter: User!
148+
publisher: String!
149+
150+
holderCount: BigInt! # Number of addresses holding a balance of datatoken
151+
orderCount: BigInt! # Number of orders executed for this dataset
152+
metadataUpdateCount: BigInt!
153+
154+
orderVolume: BigDecimal
155+
createTime: Int! # Block time datatoken was created
156+
tx: Bytes # Datatoken creation transaction id
157+
158+
balances: [TokenBalance!] @derivedFrom(field: "datatokenId")
159+
orders: [TokenOrder!] @derivedFrom(field: "datatokenId")
160+
updates: [MetadataUpdate!] @derivedFrom(field: "datatokenId") # list of MetadataUpdate objects
142161
}
143162

144163
type MetadataUpdate @entity {

src/helpers.ts

+53-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import {
1818
Datatoken,
1919
TokenBalance,
2020
TokenTransaction,
21-
PoolTransactionTokenValues
21+
PoolTransactionTokenValues,
22+
Global
2223
} from './@types/schema'
2324

2425
import { Pool } from './@types/templates/Pool/Pool'
@@ -37,7 +38,7 @@ export const ENABLE_DEBUG = true
3738

3839
const network = dataSource.network()
3940

40-
function getOceanAddress(): string {
41+
export function getOceanAddress(): string {
4142
// switch is not working for some reason
4243
if (network == 'ropsten') return '0x5e8dcb2afa23844bcc311b00ad1a0c30025aade9'
4344
if (network == 'rinkeby') return '0x8967bcf84170c91b0d24d4302c2376283b0b3a07'
@@ -53,6 +54,21 @@ function getOceanAddress(): string {
5354

5455
export const OCEAN: string = getOceanAddress()
5556

57+
export function getGlobalStats(): Global | null {
58+
let gStats: Global | null = Global.load('1')
59+
if (gStats == null) {
60+
gStats = new Global('1')
61+
gStats.totalOceanLiquidity = ZERO_BD
62+
gStats.totalSwapVolume = ZERO_BD
63+
gStats.totalValueLocked = ZERO_BD
64+
gStats.totalOrderVolume = ZERO_BD
65+
gStats.orderCount = BigInt.fromI32(0)
66+
gStats.poolCount = 0
67+
}
68+
69+
return gStats
70+
}
71+
5672
export function _debuglog(
5773
message: string,
5874
event: ethereum.Event,
@@ -192,6 +208,30 @@ export function updatePoolTokenBalance(
192208
poolToken.balance = balance
193209
}
194210

211+
export function updatePoolSwapVolume(
212+
pool: Pool,
213+
swapAmount: BigDecimal
214+
// source: string
215+
): void {
216+
debuglog(
217+
'########## updating poolToken balance (source, oldBalance, newBalance, poolId) ',
218+
null,
219+
[source, pool.totalSwapVolume.toString(), swapAmount.toString(), pool.id]
220+
)
221+
if (swapAmount < ZERO_BD || pool.totalSwapVolume < ZERO_BD) {
222+
log.warning(
223+
'EEEEEEEEEEEEEEEEE poolToken.balance < Zero: pool={}, poolToken={}, oldBalance={}, newBalance={}',
224+
[
225+
pool.id,
226+
poolToken.tokenAddress.toString(),
227+
poolToken.balance.toString(),
228+
swapAmount.toString()
229+
]
230+
)
231+
}
232+
233+
poolToken.swapBalanceOcean = poolToken.swapBalanceOcean.plus(swapAmount)
234+
}
195235
export function createUserEntity(address: string): void {
196236
if (User.load(address) == null) {
197237
const user = new User(address)
@@ -289,9 +329,15 @@ export function updatePoolTransactionToken(
289329
log.warning('ptxTokenValues {} saved', [ptxTokenValues.id])
290330
if (ptxTokenValues.tokenAddress == OCEAN) {
291331
const factory = PoolFactory.load('1')
332+
292333
factory.totalOceanLiquidity = factory.totalOceanLiquidity
293334
.plus(ptxTokenValues.tokenReserve)
294335
.minus(pool.oceanReserve)
336+
337+
const gStats: Global | null = getGlobalStats()
338+
gStats.totalOceanLiquidity = factory.totalOceanLiquidity
339+
340+
gStats.save()
295341
if (factory.totalOceanLiquidity < ZERO_BD || pool.oceanReserve < ZERO_BD) {
296342
log.warning(
297343
'EEEEEEEEEEEEEEEEE totalOceanLiquidity or oceanReserve < Zero: pool={}, totOcnLiq={}, ocnRes={}',
@@ -467,6 +513,11 @@ export function createPoolTransaction(
467513
.minus(oldValueLocked)
468514
.plus(pool.valueLocked)
469515

516+
const gStats: Global | null = getGlobalStats()
517+
518+
gStats.totalValueLocked = factory.totalValueLocked
519+
gStats.save()
520+
470521
pool.transactionCount = pool.transactionCount.plus(BigInt.fromI32(1))
471522

472523
pool.save()

0 commit comments

Comments
 (0)