Skip to content

Commit cbdbc64

Browse files
authored
add fixedrate balances (#339)
* add fixedrate balances * fix dispensers
1 parent cb16470 commit cbdbc64

6 files changed

+170
-61
lines changed

schema.graphql

+6-2
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,17 @@ type User @entity {
293293

294294
type FixedRateExchange @entity {
295295
"fixed rate exchange id"
296-
id: ID!
296+
id: ID!
297+
contract: String!
298+
exchangeId: String!
297299
owner: User!
298300
datatoken: Token!
299301
baseToken: Token!
300302
"amount of datatokens available to be sold, this is relevant if the exchange is not able to mint"
301-
datatokenBalance: BigDecimal!
303+
datatokenSupply: BigDecimal!
302304
"amount of basetokens available to be collected by the owner"
305+
baseTokenSupply: BigDecimal!
306+
datatokenBalance: BigDecimal!
303307
baseTokenBalance: BigDecimal!
304308
price: BigDecimal!
305309
active: Boolean!

scripts/generatenetworkssubgraphs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ async function replaceContractAddresses() {
3636
)
3737
subgraph = subgraph.replace(
3838
/__DISPENSERADDRESS__/g,
39-
"'" + addresses[network].FixedPrice + "'"
39+
"'" + addresses[network].Dispenser + "'"
4040
)
4141
subgraph = subgraph.replace(
4242
/__FACTORYROUTERADDRESS__/g,

src/mappings/dispenser.ts

+39-10
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,22 @@ import {
88
import { DispenserCreated } from '../@types/ERC721Factory/ERC721Factory'
99
import { Dispenser, DispenserTransaction } from '../@types/schema'
1010
import { decimal } from './utils/constants'
11-
import { getDispenser } from './utils/dispenserUtils'
11+
import {
12+
getDispenser,
13+
getDispenserGraphID,
14+
updateDispenserBalance
15+
} from './utils/dispenserUtils'
1216
import { weiToDecimal } from './utils/generic'
1317
import { addDispenser } from './utils/globalUtils'
1418
import { getToken } from './utils/tokenUtils'
1519
import { getUser } from './utils/userUtils'
1620

1721
export function handleNewDispenser(event: DispenserCreated): void {
18-
const dispenser = new Dispenser(event.params.datatokenAddress.toHex())
22+
const dispenserID = getDispenserGraphID(
23+
event.address,
24+
event.params.datatokenAddress
25+
)
26+
const dispenser = new Dispenser(dispenserID)
1927
const token = getToken(event.params.datatokenAddress, false)
2028
dispenser.token = token.id
2129

@@ -40,36 +48,51 @@ export function handleNewDispenser(event: DispenserCreated): void {
4048
}
4149

4250
export function handleActivate(event: DispenserActivated): void {
43-
const dispenser = getDispenser(event.params.datatokenAddress.toHex())
51+
const dispenserID = getDispenserGraphID(
52+
event.address,
53+
event.params.datatokenAddress
54+
)
55+
const dispenser = getDispenser(dispenserID)
4456
dispenser.active = true
4557
dispenser.save()
4658
}
4759

4860
export function handleDeactivate(event: DispenserDeactivated): void {
49-
const dispenser = getDispenser(event.params.datatokenAddress.toHex())
61+
const dispenserID = getDispenserGraphID(
62+
event.address,
63+
event.params.datatokenAddress
64+
)
65+
const dispenser = getDispenser(dispenserID)
5066
dispenser.active = true
5167
dispenser.save()
5268
}
5369

5470
export function handleAllowedSwapperChanged(
5571
event: DispenserAllowedSwapperChanged
5672
): void {
57-
const dispenser = getDispenser(event.params.datatoken.toHex())
73+
const dispenserID = getDispenserGraphID(event.address, event.params.datatoken)
74+
const dispenser = getDispenser(dispenserID)
5875
dispenser.allowedSwapper = event.params.newAllowedSwapper.toHex()
5976
dispenser.save()
6077
}
6178

6279
export function handleTokensDispensed(event: TokensDispensed): void {
80+
const dispenserID = getDispenserGraphID(
81+
event.address,
82+
event.params.datatokenAddress
83+
)
6384
const id = event.transaction.hash
6485
.toHexString()
6586
.concat('-')
66-
.concat(event.params.datatokenAddress.toHexString())
87+
.concat(dispenserID)
6788

6889
const dispenserTransaction = new DispenserTransaction(id)
69-
const dispenser = getDispenser(event.params.datatokenAddress.toHex())
70-
dispenser.balance = dispenser.balance.minus(
71-
event.params.amount.toBigDecimal()
90+
const dispenser = getDispenser(dispenserID)
91+
dispenser.balance = updateDispenserBalance(
92+
event.address,
93+
event.params.datatokenAddress
7294
)
95+
7396
dispenser.save()
7497

7598
dispenserTransaction.dispenser = dispenser.id
@@ -79,11 +102,17 @@ export function handleTokensDispensed(event: TokensDispensed): void {
79102
dispenserTransaction.createdTimestamp = event.block.timestamp.toI32()
80103
dispenserTransaction.tx = event.transaction.hash.toHex()
81104
dispenserTransaction.block = event.block.number.toI32()
105+
const token = getToken(event.params.datatokenAddress, true)
106+
dispenserTransaction.amount = weiToDecimal(
107+
event.params.amount.toBigDecimal(),
108+
token.decimals
109+
)
82110
dispenserTransaction.save()
83111
}
84112

85113
export function handleOwnerWinthdraw(event: OwnerWithdrawed): void {
86-
const dispenser = getDispenser(event.params.datatoken.toHex())
114+
const dispenserID = getDispenserGraphID(event.address, event.params.datatoken)
115+
const dispenser = getDispenser(dispenserID)
87116
dispenser.balance = decimal.ZERO
88117
dispenser.save()
89118
}

src/mappings/fixedRateExchange.ts

+49-38
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,27 @@ import {
1414
FixedRateExchangeSwap,
1515
FixedRateExchangeUpdate
1616
} from '../@types/schema'
17-
import { getFixedRateExchange, getUpdateOrSwapId } from './utils/fixedRateUtils'
17+
import {
18+
getFixedRateExchange,
19+
getUpdateOrSwapId,
20+
getFixedRateGraphID,
21+
updateFixedRateExchangeSupply
22+
} from './utils/fixedRateUtils'
1823
import { weiToDecimal } from './utils/generic'
1924
import { addFixedRateExchange } from './utils/globalUtils'
2025
import { getToken } from './utils/tokenUtils'
2126
import { getUser } from './utils/userUtils'
2227

2328
export function handleExchangeCreated(event: ExchangeCreated): void {
24-
const fixedRateExchange = new FixedRateExchange(
25-
event.params.exchangeId.toHexString()
29+
const fixedRateId = getFixedRateGraphID(
30+
event.params.exchangeId.toHexString(),
31+
event.address
2632
)
33+
const fixedRateExchange = new FixedRateExchange(fixedRateId)
2734
const user = getUser(event.params.exchangeOwner.toHexString())
2835
fixedRateExchange.owner = user.id
36+
fixedRateExchange.contract = event.address.toHexString()
37+
fixedRateExchange.exchangeId = event.params.exchangeId.toHexString()
2938
fixedRateExchange.datatoken = getToken(event.params.datatoken, true).id
3039
fixedRateExchange.baseToken = getToken(event.params.baseToken, false).id
3140

@@ -40,17 +49,18 @@ export function handleExchangeCreated(event: ExchangeCreated): void {
4049
fixedRateExchange.save()
4150

4251
addFixedRateExchange()
52+
updateFixedRateExchangeSupply(event.params.exchangeId, event.address)
4353
}
4454

4555
export function handleRateChange(event: ExchangeRateChanged): void {
46-
const fixedRateExchange = getFixedRateExchange(
47-
event.params.exchangeId.toHex()
56+
const fixedRateId = getFixedRateGraphID(
57+
event.params.exchangeId.toHexString(),
58+
event.address
4859
)
60+
61+
const fixedRateExchange = getFixedRateExchange(fixedRateId)
4962
const newExchangeUpdate = new FixedRateExchangeUpdate(
50-
getUpdateOrSwapId(
51-
event.transaction.hash.toHex(),
52-
event.params.exchangeId.toHex()
53-
)
63+
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
5464
)
5565
newExchangeUpdate.oldPrice = fixedRateExchange.price
5666
newExchangeUpdate.createdTimestamp = event.block.timestamp.toI32()
@@ -68,24 +78,25 @@ export function handleRateChange(event: ExchangeRateChanged): void {
6878
}
6979

7080
export function handleMintStateChanged(event: ExchangeMintStateChanged): void {
71-
const fixedRateExchange = getFixedRateExchange(
72-
event.params.exchangeId.toHex()
81+
const fixedRateId = getFixedRateGraphID(
82+
event.params.exchangeId.toHexString(),
83+
event.address
7384
)
85+
const fixedRateExchange = getFixedRateExchange(fixedRateId)
7486
fixedRateExchange.withMint = event.params.withMint
7587
fixedRateExchange.save()
7688
}
7789

7890
// TODO: implement fre updates/history for changes
7991

8092
export function handleActivated(event: ExchangeActivated): void {
81-
const fixedRateExchange = getFixedRateExchange(
82-
event.params.exchangeId.toHex()
93+
const fixedRateId = getFixedRateGraphID(
94+
event.params.exchangeId.toHexString(),
95+
event.address
8396
)
97+
const fixedRateExchange = getFixedRateExchange(fixedRateId)
8498
const newExchangeUpdate = new FixedRateExchangeUpdate(
85-
getUpdateOrSwapId(
86-
event.transaction.hash.toHex(),
87-
event.params.exchangeId.toHex()
88-
)
99+
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
89100
)
90101
newExchangeUpdate.oldActive = fixedRateExchange.active
91102
newExchangeUpdate.newActive = true
@@ -100,14 +111,13 @@ export function handleActivated(event: ExchangeActivated): void {
100111
}
101112

102113
export function handleDeactivated(event: ExchangeDeactivated): void {
103-
const fixedRateExchange = getFixedRateExchange(
104-
event.params.exchangeId.toHex()
114+
const fixedRateId = getFixedRateGraphID(
115+
event.params.exchangeId.toHexString(),
116+
event.address
105117
)
118+
const fixedRateExchange = getFixedRateExchange(fixedRateId)
106119
const newExchangeUpdate = new FixedRateExchangeUpdate(
107-
getUpdateOrSwapId(
108-
event.transaction.hash.toHex(),
109-
event.params.exchangeId.toHex()
110-
)
120+
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
111121
)
112122
newExchangeUpdate.oldActive = fixedRateExchange.active
113123
newExchangeUpdate.newActive = false
@@ -124,14 +134,13 @@ export function handleDeactivated(event: ExchangeDeactivated): void {
124134
export function handleAllowedSwapperChanged(
125135
event: ExchangeAllowedSwapperChanged
126136
): void {
127-
const fixedRateExchange = getFixedRateExchange(
128-
event.params.exchangeId.toHex()
137+
const fixedRateId = getFixedRateGraphID(
138+
event.params.exchangeId.toHexString(),
139+
event.address
129140
)
141+
const fixedRateExchange = getFixedRateExchange(fixedRateId)
130142
const newExchangeUpdate = new FixedRateExchangeUpdate(
131-
getUpdateOrSwapId(
132-
event.transaction.hash.toHex(),
133-
event.params.exchangeId.toHex()
134-
)
143+
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
135144
)
136145

137146
newExchangeUpdate.createdTimestamp = event.block.timestamp.toI32()
@@ -147,21 +156,20 @@ export function handleAllowedSwapperChanged(
147156

148157
// TODO: implement market fee, opf fee
149158
export function handleSwap(event: Swapped): void {
150-
const fixedRateExchange = getFixedRateExchange(
151-
event.params.exchangeId.toHex()
159+
const fixedRateId = getFixedRateGraphID(
160+
event.params.exchangeId.toHexString(),
161+
event.address
152162
)
163+
const fixedRateExchange = getFixedRateExchange(fixedRateId)
153164

154165
const swap = new FixedRateExchangeSwap(
155-
getUpdateOrSwapId(
156-
event.transaction.hash.toHex(),
157-
event.params.exchangeId.toHex()
158-
)
166+
getUpdateOrSwapId(event.transaction.hash.toHex(), fixedRateId)
159167
)
160168
swap.createdTimestamp = event.block.timestamp.toI32()
161169
swap.tx = event.transaction.hash.toHex()
162170
swap.block = event.block.number.toI32()
163171

164-
swap.exchangeId = event.params.exchangeId.toHex()
172+
swap.exchangeId = fixedRateId
165173
swap.by = getUser(event.params.by.toHex()).id
166174

167175
// we need to fetch the decimals of the base token
@@ -179,14 +187,17 @@ export function handleSwap(event: Swapped): void {
179187
)
180188

181189
swap.save()
190+
updateFixedRateExchangeSupply(event.params.exchangeId, event.address)
182191
}
183192

184193
export function handlePublishMarketFeeChanged(
185194
event: PublishMarketFeeChanged
186195
): void {
187-
const fixedRateExchange = getFixedRateExchange(
188-
event.params.exchangeId.toHex()
196+
const fixedRateId = getFixedRateGraphID(
197+
event.params.exchangeId.toHexString(),
198+
event.address
189199
)
200+
const fixedRateExchange = getFixedRateExchange(fixedRateId)
190201
if (fixedRateExchange) {
191202
fixedRateExchange.publishMarketFeeAddress =
192203
event.params.newMarketCollector.toHexString()

src/mappings/utils/dispenserUtils.ts

+29-7
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,40 @@
11
import { Dispenser } from '../../@types/schema'
22
import { getToken } from './tokenUtils'
3-
import { Address } from '@graphprotocol/graph-ts'
3+
import { Address, BigDecimal } from '@graphprotocol/graph-ts'
4+
import { weiToDecimal } from './generic'
5+
import { Dispenser as DispenserContract } from '../../@types/Dispenser/Dispenser'
46

5-
export function createDispenser(address: string): Dispenser {
6-
const dispenser = new Dispenser(address)
7-
dispenser.token = getToken(Address.fromString(address), true).id
7+
export function getDispenserGraphID(
8+
contractAddress: Address,
9+
datatokenAddress: Address
10+
): string {
11+
return contractAddress.toHexString() + '-' + datatokenAddress.toHexString()
12+
}
13+
14+
export function createDispenser(dispenserID: string): Dispenser {
15+
const dispenser = new Dispenser(dispenserID)
816
dispenser.save()
917
return dispenser
1018
}
1119

12-
export function getDispenser(address: string): Dispenser {
13-
let dispenser = Dispenser.load(address)
20+
export function getDispenser(dispenserID: string): Dispenser {
21+
let dispenser = Dispenser.load(dispenserID)
1422
if (dispenser === null) {
15-
dispenser = createDispenser(address)
23+
dispenser = createDispenser(dispenserID)
1624
}
1725
return dispenser
1826
}
27+
28+
export function updateDispenserBalance(
29+
contractAddress: Address,
30+
datatokenAddress: Address
31+
): BigDecimal {
32+
const contract = DispenserContract.bind(contractAddress)
33+
const dispenserDetails = contract.try_status(datatokenAddress)
34+
if (dispenserDetails == null) return BigDecimal.fromString('0')
35+
const token = getToken(datatokenAddress, true)
36+
return weiToDecimal(
37+
dispenserDetails.value.value5.toBigDecimal(),
38+
token.decimals
39+
)
40+
}

0 commit comments

Comments
 (0)