Skip to content

Commit cb16470

Browse files
authored
OPC fees (#337)
* OPC fees
1 parent 772b723 commit cb16470

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

schema.graphql

+11
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,17 @@ type GlobalStatistic @entity {
470470
dispenserCount: Int!
471471
}
472472

473+
type OPC @entity {
474+
id: ID!
475+
"fee in percent for swaps involving OPC approved tokens"
476+
swapOceanFee: BigDecimal
477+
"fee in percent for swaps involving non OPC approved tokens"
478+
swapNonOceanFee: BigDecimal
479+
"fee in percent taken by OPC from consumeFees"
480+
consumeFee: BigDecimal
481+
"fee in percent taken by OPC from providerFees"
482+
providerFee: BigDecimal
483+
}
473484

474485
enum NftUpdateType {
475486
METADATA_CREATED,

src/mappings/factoryRouter.ts

+57-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,66 @@
1-
import { NewPool } from '../@types/FactoryRouter/FactoryRouter'
1+
import {
2+
NewPool,
3+
TokenAdded,
4+
OPCFeeChanged,
5+
FactoryRouter
6+
} from '../@types/FactoryRouter/FactoryRouter'
7+
import { BigInt } from '@graphprotocol/graph-ts'
28
import { Pool } from '../@types/schema'
39
import { BPool } from '../@types/templates'
4-
import { addPool } from './utils/globalUtils'
10+
import { addPool, getOPC } from './utils/globalUtils'
11+
import { weiToDecimal } from './utils/generic'
512

613
export function handleNewPool(event: NewPool): void {
714
BPool.create(event.params.poolAddress)
815
const pool = new Pool(event.params.poolAddress.toHexString())
916
pool.save()
1017
addPool()
1118
}
19+
20+
export function handleOPCFeeChanged(event: OPCFeeChanged): void {
21+
const opc = getOPC()
22+
const decimals = BigInt.fromI32(18).toI32()
23+
opc.swapOceanFee = weiToDecimal(
24+
event.params.newSwapOceanFee.toBigDecimal(),
25+
decimals
26+
)
27+
opc.swapNonOceanFee = weiToDecimal(
28+
event.params.newSwapNonOceanFee.toBigDecimal(),
29+
decimals
30+
)
31+
opc.consumeFee = weiToDecimal(
32+
event.params.newConsumeFee.toBigDecimal(),
33+
decimals
34+
)
35+
opc.providerFee = weiToDecimal(
36+
event.params.newProviderFee.toBigDecimal(),
37+
decimals
38+
)
39+
opc.save()
40+
}
41+
42+
export function handleTokenAdded(event: TokenAdded): void {
43+
const contract = FactoryRouter.bind(event.address)
44+
const oceanFees = contract.try_getOPCFees()
45+
if (oceanFees.reverted) return
46+
47+
const opc = getOPC()
48+
const decimals = BigInt.fromI32(18).toI32()
49+
opc.swapOceanFee = weiToDecimal(
50+
oceanFees.value.value0.toBigDecimal(),
51+
decimals
52+
)
53+
opc.swapNonOceanFee = weiToDecimal(
54+
oceanFees.value.value1.toBigDecimal(),
55+
decimals
56+
)
57+
58+
const newConsumeFee = contract.try_getOPCConsumeFee()
59+
if (newConsumeFee.reverted) return
60+
61+
const newProviderFee = contract.try_getOPCProviderFee()
62+
if (newProviderFee.reverted) return
63+
opc.consumeFee = weiToDecimal(newConsumeFee.value.toBigDecimal(), decimals)
64+
opc.providerFee = weiToDecimal(newProviderFee.value.toBigDecimal(), decimals)
65+
opc.save()
66+
}

src/mappings/utils/globalUtils.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import {
33
GlobalStatistic,
44
GlobalTotalFixedSwapPair,
55
GlobalTotalLiquidityPair,
6-
GlobalTotalPoolSwapPair
6+
GlobalTotalPoolSwapPair,
7+
OPC
78
} from '../../@types/schema'
89

910
const GLOBAL_ID = '1'
@@ -16,6 +17,16 @@ export function getGlobalStats(): GlobalStatistic {
1617
}
1718
return globalStats
1819
}
20+
21+
export function getOPC(): OPC {
22+
let globalStats = OPC.load(GLOBAL_ID)
23+
if (!globalStats) {
24+
globalStats = new OPC(GLOBAL_ID)
25+
globalStats.save()
26+
}
27+
return globalStats
28+
}
29+
1930
export function addOrder(): void {
2031
const globalStats = getGlobalStats()
2132
globalStats.orderCount = globalStats.orderCount + 1

subgraph.template.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ dataSources:
119119
eventHandlers:
120120
- event: NewPool(indexed address,bool)
121121
handler: handleNewPool
122+
- event: TokenAdded(indexed address,indexed address)
123+
handler: handleTokenAdded
124+
- event: OPCFeeChanged(indexed address,uint256,uint256,uint256,uint256)
125+
handler: handleOPCFeeChanged
126+
122127

123128
templates:
124129
- name: ERC20Template

0 commit comments

Comments
 (0)