Skip to content

Commit b1a58b7

Browse files
authored
Feature/df rewards (#531)
* bump contracts * oceanjs * add DFRewards
1 parent c5d86d1 commit b1a58b7

File tree

9 files changed

+418
-19
lines changed

9 files changed

+418
-19
lines changed

.github/workflows/tests.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ jobs:
5252
run: |
5353
bash -x start_ocean.sh --with-thegraph --skip-subgraph-deploy --no-dashboard 2>&1 > start_ocean.log &
5454
env:
55-
CONTRACTS_VERSION: v1.1.3
55+
CONTRACTS_VERSION: v1.1.4
5656

5757
- run: npm ci
5858

package-lock.json

+16-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"test-fixed": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/FixedRateExchange.test.ts'",
2929
"test-users": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/users.test.ts'",
3030
"test-ve": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/VeOcean.test.ts'",
31+
"test-df": "TS_NODE_PROJECT='test/integration/tsconfig.json' mocha --config=test/integration/.mocharc.json --node-env=test --exit 'test/integration/DFRewards.test.ts'",
3132
"lint": "eslint --ignore-path .gitignore --ext .js --ext .ts --ext .tsx .",
3233
"lint:fix": "eslint --ignore-path .gitignore --ext .js,.ts,.tsx . --fix",
3334
"format": "prettier --ignore-path .gitignore './**/*.{css,yml,js,ts,tsx,json,yaml}' --write",
@@ -65,8 +66,8 @@
6566
"typescript": "^4.8.3"
6667
},
6768
"dependencies": {
68-
"@oceanprotocol/contracts": "^1.1.3",
69-
"@oceanprotocol/lib": "^2.0.0",
69+
"@oceanprotocol/contracts": "^1.1.4",
70+
"@oceanprotocol/lib": "^2.0.2",
7071
"cross-fetch": "^3.1.4"
7172
},
7273
"repository": {

schema.graphql

+36
Original file line numberDiff line numberDiff line change
@@ -466,4 +466,40 @@ type VeDeposit @entity {
466466
timestamp: BigInt!
467467
block: Int!
468468
tx: String!
469+
}
470+
471+
472+
enum DFHistoryType {
473+
Allocated,
474+
Claimed
475+
}
476+
477+
type DFAvailableClaim @entity {
478+
"id = {userId}-{tokenId}"
479+
id: ID!
480+
receiver: DFReward!
481+
amount: BigDecimal!
482+
token: Token!
483+
}
484+
485+
486+
type DFHistory @entity {
487+
"id = {user-id}-{txId}-{eventId}"
488+
id: ID!
489+
receiver: DFReward!
490+
amount: BigDecimal!
491+
token: Token!
492+
type: DFHistoryType!
493+
timestamp: BigInt!
494+
block: Int!
495+
tx: String!
496+
}
497+
498+
499+
type DFReward @entity {
500+
"id = {user address}"
501+
id: ID!
502+
receiver: User!
503+
availableClaims: [DFAvailableClaim!] @derivedFrom(field: "receiver")
504+
history: [DFHistory!] @derivedFrom(field: "receiver")
469505
}

scripts/generatenetworkssubgraphs.js

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ async function replaceContractAddresses() {
2222
let subgraph = fs.readFileSync('./subgraph.template.yaml', 'utf8')
2323
const subgraphVe = fs.readFileSync('./subgraph_ve.template.yaml', 'utf8')
2424
if (addresses[network].veOCEAN) {
25+
console.log('\t Adding veOCEAN')
2526
// fix identation , due to vs auto format (subgraph_ve.template is moved to left)
2627
const lines = subgraphVe.split('\n')
2728
for (let line = 0; line < lines.length; line++) {

src/mappings/dfRewards.ts

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { BigInt } from '@graphprotocol/graph-ts'
2+
import { Allocated, Claimed } from '../@types/DFRewards/DFRewards'
3+
import { DFHistory } from '../@types/schema'
4+
import { weiToDecimal } from './utils/generic'
5+
import { getToken } from './utils/tokenUtils'
6+
import { getDFReward, getDFAvailableClaim } from './utils/dfUtils'
7+
8+
export function handleAllocated(event: Allocated): void {
9+
// loop all allocations
10+
const token = getToken(event.params.tokenAddress, false)
11+
for (let i = 0; i < event.params.tos.length; i++) {
12+
const reward = getDFReward(event.params.tos[i])
13+
const history = new DFHistory(
14+
event.params.tos[i].toHexString() +
15+
'-' +
16+
event.transaction.hash.toHex() +
17+
'-' +
18+
event.logIndex.toString()
19+
)
20+
history.amount = weiToDecimal(
21+
event.params.values[i].toBigDecimal(),
22+
BigInt.fromI32(token.decimals).toI32()
23+
)
24+
history.receiver = reward.id
25+
history.token = token.id
26+
history.type = 'Allocated'
27+
history.timestamp = event.block.timestamp
28+
history.tx = event.transaction.hash.toHex()
29+
history.block = event.block.number.toI32()
30+
history.save()
31+
32+
// update available claims
33+
const claim = getDFAvailableClaim(
34+
event.params.tos[i],
35+
event.params.tokenAddress
36+
)
37+
claim.amount = claim.amount.plus(history.amount)
38+
claim.save()
39+
}
40+
}
41+
42+
export function handleClaimed(event: Claimed): void {
43+
// loop all allocations
44+
const token = getToken(event.params.tokenAddress, false)
45+
const reward = getDFReward(event.params.to)
46+
const history = new DFHistory(
47+
event.transaction.hash.toHex() + '-' + event.logIndex.toString()
48+
)
49+
history.amount = weiToDecimal(
50+
event.params.value.toBigDecimal(),
51+
BigInt.fromI32(token.decimals).toI32()
52+
)
53+
history.receiver = reward.id
54+
history.token = token.id
55+
history.type = 'Claimed'
56+
history.timestamp = event.block.timestamp
57+
history.tx = event.transaction.hash.toHex()
58+
history.block = event.block.number.toI32()
59+
history.save()
60+
61+
// update available claims
62+
const claim = getDFAvailableClaim(event.params.to, event.params.tokenAddress)
63+
claim.amount = claim.amount.minus(history.amount)
64+
claim.save()
65+
}

src/mappings/utils/dfUtils.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Address, BigDecimal } from '@graphprotocol/graph-ts'
2+
import { DFAvailableClaim, DFReward } from '../../@types/schema'
3+
import { getUser } from './userUtils'
4+
5+
export function createDFReward(address: Address): DFReward {
6+
const dfRewards = new DFReward(address.toHexString())
7+
const user = getUser(address.toHexString())
8+
dfRewards.receiver = user.id
9+
dfRewards.save()
10+
return dfRewards
11+
}
12+
13+
export function getDFReward(address: Address): DFReward {
14+
let dfRewards = DFReward.load(address.toHexString())
15+
if (dfRewards === null) {
16+
dfRewards = createDFReward(address)
17+
}
18+
return dfRewards
19+
}
20+
21+
export function getDFAvailableClaim(
22+
user: Address,
23+
token: Address
24+
): DFAvailableClaim {
25+
const id = user.toHexString() + '-' + token.toHexString()
26+
let dfClaim = DFAvailableClaim.load(id)
27+
if (dfClaim == null) {
28+
dfClaim = new DFAvailableClaim(id)
29+
dfClaim.receiver = user.toHexString()
30+
dfClaim.amount = BigDecimal.zero()
31+
dfClaim.token = token.toHexString()
32+
}
33+
return dfClaim
34+
}

subgraph_ve.template.yaml

+23
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,26 @@
6666
eventHandlers:
6767
- event: DelegateBoost(indexed address,indexed address,indexed uint256,uint256,uint256,uint256)
6868
handler: handleDelegation
69+
70+
- name: DFRewards
71+
kind: ethereum/contract
72+
network: __NETWORK__
73+
source:
74+
abi: DFRewards
75+
address: __DFREWARDSADDRESS__
76+
startBlock: __STARTBLOCK__
77+
mapping:
78+
kind: ethereum/events
79+
apiVersion: 0.0.6
80+
language: wasm/assemblyscript
81+
file: ./src/mappings/dfRewards.ts
82+
entities:
83+
- DFRewards
84+
abis:
85+
- name: DFRewards
86+
file: ./node_modules/@oceanprotocol/contracts/artifacts/contracts/df/DFRewards.sol/DFRewards.json
87+
eventHandlers:
88+
- event: Allocated(address[],uint256[],address)
89+
handler: handleAllocated
90+
- event: Claimed(address,uint256,address)
91+
handler: handleClaimed

0 commit comments

Comments
 (0)