create subgraph

This commit is contained in:
Max Alekseenko 2020-04-03 11:51:19 +03:00
parent 1d208f03b3
commit 744553eae0
5 changed files with 2907 additions and 77 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
node_modules
generated
build

View File

@ -1,6 +1,12 @@
type ExampleEntity @entity {
type User @entity {
id: ID!
count: BigInt!
user: Bytes! # address
amount: BigInt! # uint256
amount: BigInt!
}
type Round @entity {
id: ID!
winners: [Bytes!]!
prizes: [BigInt!]!
roundCloser: Bytes!
roundCloserReward: BigInt!
}

View File

@ -1,82 +1,57 @@
import { BigInt } from "@graphprotocol/graph-ts"
import { BigInt, Bytes, Address } from '@graphprotocol/graph-ts';
import {
Contract,
Deposited,
Jackpot,
OwnershipTransferred,
Rewarded,
Withdrawn
} from "../generated/Contract/Contract"
import { ExampleEntity } from "../generated/schema"
} from '../generated/Contract/Contract';
import { User, Round } from '../generated/schema';
export function handleDeposited(event: Deposited): void {
// Entities can be loaded from the store using a string ID; this ID
// needs to be unique across all entities of the same type
let entity = ExampleEntity.load(event.transaction.from.toHex())
// Entities only exist after they have been saved to the store;
// `null` checks allow to create entities on demand
if (entity == null) {
entity = new ExampleEntity(event.transaction.from.toHex())
// Entity fields can be set using simple assignments
entity.count = BigInt.fromI32(0)
let user = User.load(event.params.user.toHex());
if (user == null) {
user = new User(event.params.user.toHex());
user.amount = BigInt.fromI32(0);
}
// BigInt and BigDecimal math are supported
entity.count = entity.count + BigInt.fromI32(1)
// Entity fields can be set based on event parameters
entity.user = event.params.user
entity.amount = event.params.amount
// Entities can be written to the store with `.save()`
entity.save()
// Note: If a handler doesn't require existing field values, it is faster
// _not_ to load the entity from the store. Instead, create it fresh with
// `new Entity(...)`, set the fields that should be updated and save the
// entity back to the store. Fields that were not set or unset remain
// unchanged, allowing for partial updates to be applied.
// It is also possible to access smart contracts from mappings. For
// example, the contract that has emitted the event can be connected to
// with:
//
// let contract = Contract.bind(event.address)
//
// The following functions can then be called on this contract to access
// state variables and other data:
//
// - contract.balanceOf(...)
// - contract.blockTime(...)
// - contract.executorShare(...)
// - contract.fee(...)
// - contract.feeReceiver(...)
// - contract.getLockStart(...)
// - contract.getNewRandom(...)
// - contract.getPrizeSizes(...)
// - contract.getRoundInfo(...)
// - contract.getShares(...)
// - contract.isOwner(...)
// - contract.jackpot(...)
// - contract.jackpotChance(...)
// - contract.jackpotShare(...)
// - contract.maxDeposit(...)
// - contract.minDeposit(...)
// - contract.numberOfParticipants(...)
// - contract.owner(...)
// - contract.posdaoRandomContract(...)
// - contract.roundDuration(...)
// - contract.roundId(...)
// - contract.startedAt(...)
// - contract.totalDepositedBalance(...)
user.amount = user.amount.plus(event.params.amount);
user.save();
}
export function handleJackpot(event: Jackpot): void {}
export function handleWithdrawn(event: Withdrawn): void {
let user = User.load(event.params.user.toHex());
user.amount = user.amount.minus(event.params.amount);
user.save();
}
export function handleOwnershipTransferred(event: OwnershipTransferred): void {}
export function handleJackpot(event: Jackpot): void {
let user = User.load(event.params.winner.toHex());
user.amount = user.amount.plus(event.params.prize);
user.save();
}
export function handleRewarded(event: Rewarded): void {}
export function handleRewarded(event: Rewarded): void {
let winners = new Array<Address>(3);
let prizes = new Array<BigInt>(3);
winners = event.params.winners;
prizes = event.params.prizes;
export function handleWithdrawn(event: Withdrawn): void {}
let winnersInBytes = new Array<Bytes>(0);
for(let i = 0; i < winners.length; i++) {
let user = User.load(winners[i].toHex());
user.amount = user.amount.plus(prizes[i]);
user.save();
winnersInBytes.push(winners[i]);
}
let executor = User.load(event.params.executor.toHex());
executor.amount = executor.amount.plus(event.params.executorReward);
executor.save();
let round = new Round(event.params.roundId.toString());
round.winners = winnersInBytes;
round.prizes = prizes;
round.roundCloser = event.params.executor;
round.roundCloserReward = event.params.executorReward;
round.save();
}

View File

@ -1,4 +1,4 @@
specVersion: 0.0.1
specVersion: 0.0.2
schema:
file: ./schema.graphql
dataSources:
@ -8,14 +8,14 @@ dataSources:
source:
address: "0xf7ECea96dA4951e88E699cfb67d909Ec74Ba917E"
abi: Contract
startBlock: 14345682
mapping:
kind: ethereum/events
apiVersion: 0.0.2
apiVersion: 0.0.3
language: wasm/assemblyscript
entities:
- Deposited
- Jackpot
- OwnershipTransferred
- Rewarded
- Withdrawn
abis:
@ -26,8 +26,6 @@ dataSources:
handler: handleDeposited
- event: Jackpot(indexed uint256,address,uint256)
handler: handleJackpot
- event: OwnershipTransferred(indexed address,indexed address)
handler: handleOwnershipTransferred
- event: Rewarded(indexed uint256,address[3],uint256[3],uint256,address,uint256,uint256,address)
handler: handleRewarded
- event: Withdrawn(indexed address,uint256)

2848
yarn.lock Normal file

File diff suppressed because it is too large Load Diff