Skip to content

Commit

Permalink
Initialize subgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
arjanjohan committed Apr 28, 2024
1 parent b98160d commit eab7eff
Show file tree
Hide file tree
Showing 16 changed files with 1,057 additions and 21 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ Due to the limited time, I could not complete everything I set out to do. Additi

### Verified smart contracts

[ScrollFighter](https://sepolia.scrollscan.com/address/0x841b974730da5501C9A6d4CA4479d789375e220E#code)
[ScrollFighterV2](https://sepolia.scrollscan.com/address/0xC9728eD87F9fF73da7e27e18FEB560E52aa4bB3F#code)
[ScrollFighter](https://sepolia.scrollscan.com/address/0x0f567534623A5715a8271235db4Ba58461154307#code)
[ScrollFighterV2 (for testing only)](https://sepolia.scrollscan.com/address/0xC9728eD87F9fF73da7e27e18FEB560E52aa4bB3F#code)
[FighterCoins](https://sepolia.scrollscan.com/address/0x64CDeB6CD5ecfB002bdaFabc98B5C883C5C06B27#code)
[UltraVerifier](https://sepolia.scrollscan.com/address/0x4a2B33A77de8F69b8Cf913aafc6357f4Ce176105#code)

Expand Down
10 changes: 5 additions & 5 deletions packages/graph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ type GameProposed @entity(immutable: true) {
}

type Game @entity {
id: Bytes! # uint256
id: Bytes!
gameId: BigInt! # uint256
wageredAmount: BigInt! # uint256
player1: Bytes! # address
player2: Bytes! # address
players: [Bytes!]! # address
winner: Bytes! # address
fighterIds: [BigInt!]! # uint256
moves: [[BigInt!]!]! # uint256
Expand All @@ -70,6 +70,6 @@ type Game @entity {

type User @entity {
id: Bytes! # address
games_started: [Game!]! @derivedFrom(field: "player1")
games_joined: [Game!]! @derivedFrom(field: "player2")
# games_started: [Game!]! @derivedFrom(field: "player1")
# games_joined: [Game!]! @derivedFrom(field: "player2")
}
43 changes: 34 additions & 9 deletions packages/graph/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import {
GameAccepted as GameAcceptedEvent,
GameEnded as GameEndedEvent,
GameEndedByTimeout as GameEndedByTimeoutEvent,
GameProposed as GameProposedEvent,
Game as GameEntity
GameProposed as GameProposedEvent
} from "../generated/Contract/Contract"
import {
FightersRevealed,
GameAccepted,
GameEnded,
GameEndedByTimeout,
GameProposed
GameProposed,
Game as GameEntity
} from "../generated/schema"

export function handleFightersRevealed(event: FightersRevealedEvent): void {
Expand All @@ -27,6 +27,17 @@ export function handleFightersRevealed(event: FightersRevealedEvent): void {
entity.transactionHash = event.transaction.hash

entity.save()

// TODO: Retrieve game entity and update state
let gameEntity = new GameEntity(
event.transaction.hash.concatI32(event.logIndex.toI32())
)
gameEntity.state = "STARTED"
gameEntity.blockNumber = event.block.number
gameEntity.fighterIds = []
gameEntity.moves = []

gameEntity.save()
}

export function handleGameAccepted(event: GameAcceptedEvent): void {
Expand All @@ -43,6 +54,15 @@ export function handleGameAccepted(event: GameAcceptedEvent): void {
entity.transactionHash = event.transaction.hash

entity.save()

// TODO: Retrieve game entity and update state
let gameEntity = new GameEntity(
event.transaction.hash.concatI32(event.logIndex.toI32())
)
gameEntity.state = "ACCEPTED"
gameEntity.blockNumber = event.block.number

gameEntity.save()
}

export function handleGameEnded(event: GameEndedEvent): void {
Expand Down Expand Up @@ -83,10 +103,6 @@ export function handleGameProposed(event: GameProposedEvent): void {
let entity = new GameProposed(
event.transaction.hash.concatI32(event.logIndex.toI32())
)
let game = new GameEntity(
event.transaction.hash.concatI32(event.logIndex.toI32())
)
// todo: add game entity fields
entity.gameId = event.params.gameId
entity.player1 = event.params.player1
entity.player2 = event.params.player2
Expand All @@ -95,8 +111,17 @@ export function handleGameProposed(event: GameProposedEvent): void {
entity.blockNumber = event.block.number
entity.blockTimestamp = event.block.timestamp
entity.transactionHash = event.transaction.hash

entity.save()
game.save()

let gameEntity = new GameEntity(
event.transaction.hash.concatI32(event.logIndex.toI32())
)
gameEntity.gameId = event.params.gameId
gameEntity.players = [event.params.player1, event.params.player2]
gameEntity.wageredAmount = event.params.wageredAmount
gameEntity.state = "PROPOSED"
gameEntity.blockNumber = event.block.number

gameEntity.save()

}
34 changes: 34 additions & 0 deletions packages/graph2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Graph CLI generated artifacts
build/
generated/

# Dependency directories
node_modules/
jspm_packages/

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# dotenv environment variables file
.env

# Testing
coverage
coverage.json

# Typechain
typechain
typechain-types

# Hardhat files
cache
Loading

0 comments on commit eab7eff

Please sign in to comment.