Skip to content

Commit

Permalink
Unit tests updated, schema adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
ioay committed Apr 18, 2024
1 parent 175c0ae commit fb4daa7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 138 deletions.
12 changes: 6 additions & 6 deletions subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ enum LogType {
CancelledFromQueue
}

interface ActivityData {
interface ActivityData @key(fields: "id") {
id: ID!
staker: Staker
staker: Staker!
logs: [LogData!]
}

Expand All @@ -23,18 +23,18 @@ type Staker @entity {
activities: [ActivityData!] @derivedFrom(field: "staker")
}

type Stake implements ActivityData @entity {
type Stake implements ActivityData @entity{
id: ID!
staker: Staker
staker: Staker!
logs: [LogData!] @derivedFrom(field: "activity")
initialDepositAmountSatoshi: BigInt!
amountToStakeStBtc: BigInt!
shareStBtc: BigInt!
}

type Unstake implements ActivityData @entity {
type Unstake implements ActivityData @entity{
id: ID!
staker: Staker
staker: Staker!
logs: [LogData!] @derivedFrom(field: "activity")
initialDepositAmountSatoshi: BigInt!
amountToStakeStBtc: BigInt!
Expand Down
6 changes: 3 additions & 3 deletions subgraph/src/acre-bitcoin-depositor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { StakeRequestInitialized as StakeRequestInitializedEvent } from "../generated/AcreBitcoinDepositor/AcreBitcoinDepositor"
import { Stake } from "../generated/schema"
import { getOrCreateStaker, getOrCreateLog } from "./utils"
import { getOrCreateStaker, getOrCreateStake, getOrCreateLog } from "./utils"

// eslint-disable-next-line import/prefer-default-export
export function handleStakeRequestInitialized(
event: StakeRequestInitializedEvent,
): void {
const stakerEntity = getOrCreateStaker(event.params.staker)
const stakeEntity = new Stake(event.transaction.hash.toHexString())
const stakeEntity = getOrCreateStake(event.transaction.hash.toHexString())

const logDataBtc = getOrCreateLog(
`${event.transaction.hash.toHexString()}btc`,
Expand All @@ -32,6 +31,7 @@ export function handleStakeRequestInitialized(
logDataEth.amount = event.params.initialAmount

stakerEntity.save()
// stakeEntity.save()
logDataBtc.save()
logDataEth.save()
}
13 changes: 12 additions & 1 deletion subgraph/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address } from "@graphprotocol/graph-ts"
import { Staker, LogData } from "../generated/schema"
import { Staker, LogData, Stake } from "../generated/schema"

export function getOrCreateStaker(stakerId: Address): Staker {
const stakerHexString = stakerId.toHexString()
Expand All @@ -12,6 +12,17 @@ export function getOrCreateStaker(stakerId: Address): Staker {
return staker
}

export function getOrCreateStake(transactionId: string): Stake {
// const stakeHexString = transactionHash.toHexString()
let stake = Stake.load(transactionId)

if (!stake) {
stake = new Stake(transactionId)
}

return stake
}

export function getOrCreateLog(logId: string): LogData {
let log = LogData.load(logId)

Expand Down
143 changes: 15 additions & 128 deletions subgraph/tests/acre-bitcoin-depositor.test.ts
Original file line number Diff line number Diff line change
@@ -1,158 +1,45 @@
// acre-bitcoin-depositor.test.ts

import {
assert,
describe,
test,
clearStore,
afterEach,
beforeEach,
} from "matchstick-as/assembly/index"
import { BigInt, Address } from "@graphprotocol/graph-ts"
import { createStakeRequestInitializedEvent } from "./acre-bitcoin-depositor-utils"
import { handleStakeRequestInitialized } from "../src/acre-bitcoin-depositor"

describe("createStakeRequestInitialized event", () => {
afterEach(() => {
clearStore()
})

test("StakeRequestInitialized entity does not exist in the store", () => {
assert.notInStore("StakeRequestInitialized", "")
})

test("Staker entity does not exist in the store", () => {
assert.notInStore("Stake", "")
})
const depositKey = BigInt.fromI32(234)
const caller = Address.fromString("0x0000000000000000000000000000000000000001")
const staker = Address.fromString("0x0000000000000000000000000000000000000001")
const initialAmount = BigInt.fromI32(234)

test("LogData entity does not exist in the store", () => {
assert.notInStore("LogData", "")
})
const event = createStakeRequestInitializedEvent(
depositKey,
caller,
staker,
initialAmount,
)

test("should create StakeRequestInitialized entity", () => {
const depositKey = BigInt.fromI32(234)
const caller = Address.fromString(
"0x0000000000000000000000000000000000000001",
)
const staker = Address.fromString(
"0x0000000000000000000000000000000000000001",
)
const initialAmount = BigInt.fromI32(234)

const event = createStakeRequestInitializedEvent(
depositKey,
caller,
staker,
initialAmount,
)
describe("createStakeRequestInitialized event", () => {
beforeEach(() => {
handleStakeRequestInitialized(event)

assert.entityCount("StakeRequestInitialized", 1)
})

test("StakeRequestInitialized entity has proper fields", () => {
const depositKey = BigInt.fromI32(234)
const caller = Address.fromString(
"0x0000000000000000000000000000000000000001",
)
const staker = Address.fromString(
"0x0000000000000000000000000000000000000001",
)
const initialAmount = BigInt.fromI32(10000)

const event = createStakeRequestInitializedEvent(
depositKey,
caller,
staker,
initialAmount,
)

handleStakeRequestInitialized(event)

const txId = event.transaction.hash.toHexString()

assert.fieldEquals(
"StakeRequestInitialized",
txId,
"depositKey",
depositKey.toString(),
)

assert.fieldEquals(
"StakeRequestInitialized",
txId,
"staker",
staker.toHexString(),
)

assert.fieldEquals(
"StakeRequestInitialized",
txId,
"caller",
caller.toHexString(),
)
afterEach(() => {
clearStore()
})

test("should create Staker entity", () => {
const depositKey = BigInt.fromI32(234)
const caller = Address.fromString(
"0x0000000000000000000000000000000000000001",
)
const staker = Address.fromString(
"0x0000000000000000000000000000000000000001",
)
const initialAmount = BigInt.fromI32(234)

const event = createStakeRequestInitializedEvent(
depositKey,
caller,
staker,
initialAmount,
)
handleStakeRequestInitialized(event)

assert.entityCount("Staker", 1)
})

test("should create LogData entities", () => {
const depositKey = BigInt.fromI32(234)
const caller = Address.fromString(
"0x0000000000000000000000000000000000000001",
)
const staker = Address.fromString(
"0x0000000000000000000000000000000000000001",
)
const initialAmount = BigInt.fromI32(234)

const event = createStakeRequestInitializedEvent(
depositKey,
caller,
staker,
initialAmount,
)
handleStakeRequestInitialized(event)

assert.entityCount("LogData", 2)
})

test("LogData entity has proper fields", () => {
const depositKey = BigInt.fromI32(234)
const caller = Address.fromString(
"0x0000000000000000000000000000000000000001",
)
const staker = Address.fromString(
"0x0000000000000000000000000000000000000001",
)
const initialAmount = BigInt.fromI32(10000)

const event = createStakeRequestInitializedEvent(
depositKey,
caller,
staker,
initialAmount,
)

handleStakeRequestInitialized(event)

const txBtcId = `${event.transaction.hash.toHexString()}btc`
const txEthId = `${event.transaction.hash.toHexString()}eth`

Expand Down

0 comments on commit fb4daa7

Please sign in to comment.