Skip to content

Commit f5c7eea

Browse files
authored
Feature/templates (#349)
* templates
1 parent 992323b commit f5c7eea

9 files changed

+234
-88
lines changed

schema.graphql

+11-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,8 @@ type FixedRateExchangeSwap @entity {
365365

366366
type Dispenser @entity {
367367
"token address"
368-
id: ID!
368+
id: ID!
369+
contract: String!
369370
active: Boolean!
370371
"if using the enterprise template the owner will always be the erc721 factory, for normal template it will a user"
371372
owner: String
@@ -486,6 +487,7 @@ type OPC @entity {
486487
consumeFee: BigDecimal
487488
"fee in percent taken by OPC from providerFees"
488489
providerFee: BigDecimal
490+
approvedTokens: [String!]
489491
}
490492

491493
enum NftUpdateType {
@@ -516,3 +518,11 @@ type NftUpdate @entity {
516518
timestamp: Int!
517519
tx: String!
518520
}
521+
522+
523+
type Template @entity{
524+
id: ID!
525+
fixedRateTemplates: [String!]
526+
dispenserTemplates: [String!]
527+
ssTemplates: [String!]
528+
}

scripts/generatenetworkssubgraphs.js

-8
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,6 @@ async function replaceContractAddresses() {
3030
/__ERC721FACTORYADDRESS__/g,
3131
"'" + addresses[network].ERC721Factory + "'"
3232
)
33-
subgraph = subgraph.replace(
34-
/__FIXEDRATEEXCHANGEADDRESS__/g,
35-
"'" + addresses[network].FixedPrice + "'"
36-
)
37-
subgraph = subgraph.replace(
38-
/__DISPENSERADDRESS__/g,
39-
"'" + addresses[network].Dispenser + "'"
40-
)
4133
subgraph = subgraph.replace(
4234
/__FACTORYROUTERADDRESS__/g,
4335
"'" + addresses[network].Router + "'"

src/mappings/dispenser.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
DispenserDeactivated,
55
OwnerWithdrawed,
66
TokensDispensed
7-
} from '../@types/Dispenser/Dispenser'
7+
} from '../@types/templates/Dispenser/Dispenser'
88
import { DispenserCreated } from '../@types/ERC721Factory/ERC721Factory'
99
import { Dispenser, DispenserTransaction } from '../@types/schema'
1010
import { decimal } from './utils/constants'
@@ -26,6 +26,7 @@ export function handleNewDispenser(event: DispenserCreated): void {
2626
const dispenser = new Dispenser(dispenserID)
2727
const token = getToken(event.params.datatokenAddress, false)
2828
dispenser.token = token.id
29+
dispenser.contract = event.address.toHexString()
2930

3031
dispenser.owner = event.params.owner.toHexString()
3132
dispenser.maxBalance = weiToDecimal(

src/mappings/factoryRouter.ts

+124-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
import {
22
NewPool,
33
TokenAdded,
4+
TokenRemoved,
45
OPCFeeChanged,
5-
FactoryRouter
6+
FactoryRouter,
7+
SSContractAdded,
8+
SSContractRemoved,
9+
FixedRateContractAdded,
10+
FixedRateContractRemoved,
11+
DispenserContractAdded,
12+
DispenserContractRemoved
613
} from '../@types/FactoryRouter/FactoryRouter'
714
import { BigInt } from '@graphprotocol/graph-ts'
815
import { Pool } from '../@types/schema'
9-
import { BPool } from '../@types/templates'
10-
import { addPool, getOPC } from './utils/globalUtils'
16+
import { BPool, FixedRateExchange, Dispenser } from '../@types/templates'
17+
import { addPool, getOPC, getTemplates } from './utils/globalUtils'
1118
import { weiToDecimal } from './utils/generic'
1219

1320
export function handleNewPool(event: NewPool): void {
@@ -62,5 +69,119 @@ export function handleTokenAdded(event: TokenAdded): void {
6269
if (newProviderFee.reverted) return
6370
opc.consumeFee = weiToDecimal(newConsumeFee.value.toBigDecimal(), decimals)
6471
opc.providerFee = weiToDecimal(newProviderFee.value.toBigDecimal(), decimals)
72+
73+
// add token to approvedTokens
74+
let existingTokens: string[]
75+
if (!opc.approvedTokens) existingTokens = []
76+
else existingTokens = opc.approvedTokens as string[]
77+
if (!existingTokens.includes(event.params.token.toHexString()))
78+
existingTokens.push(event.params.token.toHexString())
79+
opc.approvedTokens = existingTokens
80+
6581
opc.save()
6682
}
83+
84+
export function handleTokenRemoved(event: TokenRemoved): void {
85+
const opc = getOPC()
86+
const newList: string[] = []
87+
let existingTokens: string[]
88+
if (!opc.approvedTokens) existingTokens = []
89+
else existingTokens = opc.approvedTokens as string[]
90+
if (!existingTokens || existingTokens.length < 1) return
91+
while (existingTokens.length > 0) {
92+
const role = existingTokens.shift().toString()
93+
if (!role) break
94+
if (role !== event.params.token.toHexString()) newList.push(role)
95+
}
96+
opc.approvedTokens = newList
97+
opc.save()
98+
}
99+
100+
export function handleSSContractAdded(event: SSContractAdded): void {
101+
// add token to approvedTokens
102+
const templates = getTemplates()
103+
let existingContracts: string[]
104+
if (!templates.ssTemplates) existingContracts = []
105+
else existingContracts = templates.ssTemplates as string[]
106+
if (!existingContracts.includes(event.params.contractAddress.toHexString()))
107+
existingContracts.push(event.params.contractAddress.toHexString())
108+
templates.ssTemplates = existingContracts
109+
templates.save()
110+
}
111+
export function handleSSContractRemoved(event: SSContractRemoved): void {
112+
const templates = getTemplates()
113+
const newList: string[] = []
114+
let existingContracts: string[]
115+
if (!templates.ssTemplates) existingContracts = []
116+
else existingContracts = templates.ssTemplates as string[]
117+
if (!existingContracts || existingContracts.length < 1) return
118+
while (existingContracts.length > 0) {
119+
const role = existingContracts.shift().toString()
120+
if (!role) break
121+
if (role !== event.params.contractAddress.toHexString()) newList.push(role)
122+
}
123+
templates.ssTemplates = newList
124+
templates.save()
125+
}
126+
export function handleFixedRateContractAdded(
127+
event: FixedRateContractAdded
128+
): void {
129+
FixedRateExchange.create(event.params.contractAddress)
130+
// add token to approvedTokens
131+
const templates = getTemplates()
132+
let existingContracts: string[]
133+
if (!templates.fixedRateTemplates) existingContracts = []
134+
else existingContracts = templates.fixedRateTemplates as string[]
135+
if (!existingContracts.includes(event.params.contractAddress.toHexString()))
136+
existingContracts.push(event.params.contractAddress.toHexString())
137+
templates.fixedRateTemplates = existingContracts
138+
templates.save()
139+
}
140+
export function handleFixedRateContractRemoved(
141+
event: FixedRateContractRemoved
142+
): void {
143+
const templates = getTemplates()
144+
const newList: string[] = []
145+
let existingContracts: string[]
146+
if (!templates.fixedRateTemplates) existingContracts = []
147+
else existingContracts = templates.fixedRateTemplates as string[]
148+
if (!existingContracts || existingContracts.length < 1) return
149+
while (existingContracts.length > 0) {
150+
const role = existingContracts.shift().toString()
151+
if (!role) break
152+
if (role !== event.params.contractAddress.toHexString()) newList.push(role)
153+
}
154+
templates.fixedRateTemplates = newList
155+
templates.save()
156+
}
157+
export function handleDispenserContractAdded(
158+
event: DispenserContractAdded
159+
): void {
160+
Dispenser.create(event.params.contractAddress)
161+
162+
const templates = getTemplates()
163+
let existingContracts: string[]
164+
if (!templates.dispenserTemplates) existingContracts = []
165+
else existingContracts = templates.dispenserTemplates as string[]
166+
if (!existingContracts.includes(event.params.contractAddress.toHexString()))
167+
existingContracts.push(event.params.contractAddress.toHexString())
168+
templates.dispenserTemplates = existingContracts
169+
templates.save()
170+
}
171+
export function handleDispenserContractRemoved(
172+
event: DispenserContractRemoved
173+
): void {
174+
const templates = getTemplates()
175+
const newList: string[] = []
176+
let existingContracts: string[]
177+
if (!templates.dispenserTemplates) existingContracts = []
178+
else existingContracts = templates.dispenserTemplates as string[]
179+
if (!existingContracts || existingContracts.length < 1) return
180+
while (existingContracts.length > 0) {
181+
const role = existingContracts.shift().toString()
182+
if (!role) break
183+
if (role !== event.params.contractAddress.toHexString()) newList.push(role)
184+
}
185+
templates.dispenserTemplates = newList
186+
templates.save()
187+
}

src/mappings/fixedRateExchange.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
ExchangeRateChanged,
99
Swapped,
1010
PublishMarketFeeChanged
11-
} from '../@types/FixedRateExchange/FixedRateExchange'
11+
} from '../@types/templates/FixedRateExchange/FixedRateExchange'
1212
import {
1313
FixedRateExchange,
1414
FixedRateExchangeSwap,

src/mappings/utils/dispenserUtils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Dispenser } from '../../@types/schema'
22
import { getToken } from './tokenUtils'
33
import { Address } from '@graphprotocol/graph-ts'
44
import { weiToDecimal } from './generic'
5-
import { Dispenser as DispenserContract } from '../../@types/Dispenser/Dispenser'
5+
import { Dispenser as DispenserContract } from '../../@types/templates/Dispenser/Dispenser'
66

77
export function getDispenserGraphID(
88
contractAddress: Address,

src/mappings/utils/fixedRateUtils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { FixedRateExchange } from '../../@types/schema'
22

3-
import { FixedRateExchange as FixedRateExchangeContract } from '../../@types/FixedRateExchange/FixedRateExchange'
3+
import { FixedRateExchange as FixedRateExchangeContract } from '../../@types/templates/FixedRateExchange/FixedRateExchange'
44
import { Address, Bytes } from '@graphprotocol/graph-ts'
55
import { getToken } from './tokenUtils'
66
import { weiToDecimal } from './generic'

src/mappings/utils/globalUtils.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
GlobalTotalFixedSwapPair,
55
GlobalTotalLiquidityPair,
66
GlobalTotalPoolSwapPair,
7-
OPC
7+
OPC,
8+
Template
89
} from '../../@types/schema'
910

1011
const GLOBAL_ID = '1'
@@ -27,6 +28,15 @@ export function getOPC(): OPC {
2728
return globalStats
2829
}
2930

31+
export function getTemplates(): Template {
32+
let templates = Template.load(GLOBAL_ID)
33+
if (!templates) {
34+
templates = new Template(GLOBAL_ID)
35+
templates.save()
36+
}
37+
return templates
38+
}
39+
3040
export function addOrder(): void {
3141
const globalStats = getGlobalStats()
3242
globalStats.orderCount = globalStats.orderCount + 1

0 commit comments

Comments
 (0)