-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapping.ts
46 lines (37 loc) · 1.47 KB
/
mapping.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { store, BigInt, Address } from '@graphprotocol/graph-ts'
// This is just an example event type generated by `graph-cli`
// from an Ethereum smart contract ABI
import { Payment, JackpotPayment, Commit } from './build/types/Dice2Win/Dice2Win'
// This is an example of an entity type generated from a
// subgraph's GraphQL schema
import { Beneficiary } from './build/types/schema'
const BENEFICIARY = 'Beneficiary'
// Update the winnings of the beneficiary with the given address. Create
// a new beneficiary if none exists yet
// Return that beneficiary, but do not store it yet
function updateWinnings(beneficiary: Address, amount: BigInt) : Beneficiary {
let id = beneficiary.toHex()
let ben = Beneficiary.load(id)
if (ben === null) {
ben = new Beneficiary(id)
ben.winnings = amount
// This initialization is important; otherwise we store NULLS
// in new jackpots
ben.jackpots = BigInt.fromI32(0)
} else {
ben.winnings = ben.winnings.plus(amount)
}
return ben as Beneficiary
}
export function handlePayment(event: Payment): void {
let ben = updateWinnings(event.params.beneficiary, event.params.amount)
store.set(BENEFICIARY, ben.id, ben)
}
export function handleJackpotPayment(event: JackpotPayment): void {
let ben = updateWinnings(event.params.beneficiary, event.params.amount)
ben.jackpots = ben.jackpots.plus(BigInt.fromI32(1))
store.set(BENEFICIARY, ben.id, ben)
}
export function handleCommit(event: Commit): void {
// do nothing yet
}