Skip to content

Commit

Permalink
Update reflects the latest contract to Sepolia: Staker -> DepositPwner
Browse files Browse the repository at this point in the history
  • Loading branch information
ioay committed Apr 18, 2024
1 parent fb4daa7 commit ddb6813
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 55 deletions.
4 changes: 2 additions & 2 deletions subgraph/abis/AcreBitcoinDepositor.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
{
"indexed": true,
"internalType": "address",
"name": "staker",
"name": "depositOwner",
"type": "address"
},
{
Expand All @@ -27,7 +27,7 @@
"type": "uint256"
}
],
"name": "StakeRequestInitialized",
"name": "DepositInitialized",
"type": "event"
}
]
20 changes: 10 additions & 10 deletions subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,36 @@ enum LogType {

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

type Staker @entity {
type DepositOwner @entity {
# Id is the ethereum address of the account.
id: ID!
activities: [ActivityData!] @derivedFrom(field: "staker")
activities: [ActivityData!] @derivedFrom(field: "depositOwner")
}

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

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

type LogData @entity {
# Id is the transaction hash
# Id is the transaction hash.
id: ID!
timestamp: BigInt!
activity: ActivityData
Expand Down
21 changes: 13 additions & 8 deletions subgraph/src/acre-bitcoin-depositor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { StakeRequestInitialized as StakeRequestInitializedEvent } from "../generated/AcreBitcoinDepositor/AcreBitcoinDepositor"
import { getOrCreateStaker, getOrCreateStake, getOrCreateLog } from "./utils"
import { DepositInitialized as DepositInitializedEvent } from "../generated/AcreBitcoinDepositor/AcreBitcoinDepositor"
import {
getOrCreateDepositOwner,
getOrCreateStake,
getOrCreateLog,
} from "./utils"

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

stakeEntity.depositOwner = depositOwnerEntity.id
stakeEntity.initialDepositAmountSatoshi = event.params.initialAmount

const logDataBtc = getOrCreateLog(
`${event.transaction.hash.toHexString()}btc`,
)
Expand All @@ -30,8 +35,8 @@ export function handleStakeRequestInitialized(
logDataEth.chain = "Ethereum"
logDataEth.amount = event.params.initialAmount

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

export function getOrCreateStaker(stakerId: Address): Staker {
const stakerHexString = stakerId.toHexString()
let staker = Staker.load(stakerHexString)
export function getOrCreateDepositOwner(depositOwnerId: Address): DepositOwner {
const depositOwnerHexString = depositOwnerId.toHexString()
let depositOwner = DepositOwner.load(depositOwnerHexString)

if (!staker) {
staker = new Staker(stakerHexString)
if (!depositOwner) {
depositOwner = new DepositOwner(depositOwnerHexString)
}

return staker
return depositOwner
}

export function getOrCreateStake(transactionId: string): Stake {
Expand Down
13 changes: 8 additions & 5 deletions subgraph/subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ dataSources:
name: AcreBitcoinDepositor
network: sepolia
source:
address: "0x37E34EbC743FFAf96b56b3f62854bb7E733a4B50"
address: "0x2F86FE8C5683372Db667E6f6d88dcB6d55a81286"
abi: AcreBitcoinDepositor
startBlock: 5394071
startBlock: 5675744
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
entities:
- StakeRequestInitialized
- DepositOwner
- Stake
- Unstake
- LogData
abis:
- name: AcreBitcoinDepositor
file: ./abis/AcreBitcoinDepositor.json
eventHandlers:
- event: StakeRequestInitialized(indexed uint256,indexed address,indexed address,uint256)
handler: handleStakeRequestInitialized
- event: DepositInitialized(indexed uint256,indexed address,indexed address,uint256)
handler: handleDepositInitialized
receipt: true
file: ./src/acre-bitcoin-depositor.ts
29 changes: 14 additions & 15 deletions subgraph/tests/acre-bitcoin-depositor-utils.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { ethereum, BigInt, Address } from "@graphprotocol/graph-ts"
import { newMockEvent } from "matchstick-as/assembly/defaults"
import { StakeRequestInitialized } from "../generated/AcreBitcoinDepositor/AcreBitcoinDepositor"
import { DepositInitialized } from "../generated/AcreBitcoinDepositor/AcreBitcoinDepositor"

// eslint-disable-next-line import/prefer-default-export
export function createStakeRequestInitializedEvent(
export function createDepositInitializedEvent(
depositKey: BigInt,
caller: Address,
staker: Address,
depositOwner: Address,
initialAmount: BigInt,
): StakeRequestInitialized {
const stakeRequestInitializedEvent =
changetype<StakeRequestInitialized>(newMockEvent())
): DepositInitialized {
const depositInitializedEvent = changetype<DepositInitialized>(newMockEvent())

stakeRequestInitializedEvent.parameters = []
depositInitializedEvent.parameters = []

const depositKeyParam = new ethereum.EventParam(
"depositKey",
Expand All @@ -23,20 +22,20 @@ export function createStakeRequestInitializedEvent(
ethereum.Value.fromAddress(caller),
)

const stakerParam = new ethereum.EventParam(
"staker",
ethereum.Value.fromAddress(staker),
const depositOwnerParam = new ethereum.EventParam(
"depositOwner",
ethereum.Value.fromAddress(depositOwner),
)

const initialAmountParam = new ethereum.EventParam(
"initialAmount",
ethereum.Value.fromUnsignedBigInt(initialAmount),
)

stakeRequestInitializedEvent.parameters.push(depositKeyParam)
stakeRequestInitializedEvent.parameters.push(callerParam)
stakeRequestInitializedEvent.parameters.push(stakerParam)
stakeRequestInitializedEvent.parameters.push(initialAmountParam)
depositInitializedEvent.parameters.push(depositKeyParam)
depositInitializedEvent.parameters.push(callerParam)
depositInitializedEvent.parameters.push(depositOwnerParam)
depositInitializedEvent.parameters.push(initialAmountParam)

return stakeRequestInitializedEvent
return depositInitializedEvent
}
52 changes: 44 additions & 8 deletions subgraph/tests/acre-bitcoin-depositor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,78 @@ import {
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"
import { createDepositInitializedEvent } from "./acre-bitcoin-depositor-utils"
import { handleDepositInitialized } from "../src/acre-bitcoin-depositor"

const depositKey = BigInt.fromI32(234)
const caller = Address.fromString("0x0000000000000000000000000000000000000001")
const staker = Address.fromString("0x0000000000000000000000000000000000000001")
const depositOwner = Address.fromString(
"0x0000000000000000000000000000000000000001",
)
const initialAmount = BigInt.fromI32(234)

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

describe("createStakeRequestInitialized event", () => {
beforeEach(() => {
handleStakeRequestInitialized(event)
handleDepositInitialized(event)
})

afterEach(() => {
clearStore()
})

test("should create Staker entity", () => {
assert.entityCount("Staker", 1)
test("should create DepositOwner entity", () => {
assert.entityCount("DepositOwner", 1)
})

test("should create Stake entity", () => {
assert.entityCount("Stake", 1)
})

test("should create LogData entities", () => {
assert.entityCount("LogData", 2)
})

test("Stake entity has proper fields", () => {
assert.fieldEquals(
"Stake",
event.transaction.hash.toHexString(),
"depositOwner",
depositOwner.toHexString(),
)

assert.fieldEquals(
"Stake",
event.transaction.hash.toHexString(),
"initialDepositAmountSatoshi",
event.params.initialAmount.toString(),
)
})

test("LogData entity has proper fields", () => {
const txBtcId = `${event.transaction.hash.toHexString()}btc`
const txEthId = `${event.transaction.hash.toHexString()}eth`

assert.fieldEquals(
"LogData",
txBtcId,
"activity",
event.transaction.hash.toHexString(),
)

assert.fieldEquals(
"LogData",
txEthId,
"activity",
"event.transaction.hash.toHexString()",
)

assert.fieldEquals(
"LogData",
txBtcId,
Expand Down

0 comments on commit ddb6813

Please sign in to comment.