Skip to content

Commit

Permalink
Added handleDepositFinalized event, renamed acre-bitcoin depositor to…
Browse files Browse the repository at this point in the history
… bitcoin-depositor, added deploy information to readme.md
  • Loading branch information
ioay committed Apr 21, 2024
1 parent e5be694 commit 7c33999
Show file tree
Hide file tree
Showing 14 changed files with 524 additions and 242 deletions.
2 changes: 1 addition & 1 deletion subgraph/.prettierrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
...require("../.prettierrc.js"),
...require("../.prettierrc.js"),
}
30 changes: 29 additions & 1 deletion subgraph/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ EVM-compatible JSON-RPC API. You can use Thesis private RPC from Alchemy or
create a private one
[here](https://www.alchemy.com/overviews/private-rpc-endpoint).

1. Install Docker on your local machine:
1. Install Docker on your local machine:

- Mac: https://docs.docker.com/desktop/install/mac-install/
- Windows: https://docs.docker.com/desktop/install/windows-install/
- Linux: https://docs.docker.com/desktop/install/linux-install/
Expand Down Expand Up @@ -74,3 +75,30 @@ Note: use it only if your subgraph is not created in the local Graph node.
```
http://localhost:8000/subgraphs/name/acre-subgraph
```

### Deploy the subgraph to Subgraph Studio

1. Once your subgraph has been created in Subgraph Studio you can initialize the subgraph code using this command:

```
graph init --studio <SUBGRAPH_SLUG>
```

The <SUBGRAPH_SLUG> value can be found on your subgraph details page in Subgraph Studio
(https://thegraph.com/docs/en/deploying/deploying-a-subgraph-to-studio/#create-your-subgraph-in-subgraph-studio)

2. Before being able to deploy your subgraph to Subgraph Studio, you need to login into your account within the CLI.

```
graph auth --studio <DEPLOY KEY>
```

The <SUBGRAPH_SLUG> can be found on your "My Subgraphs" page or your subgraph details page.

3. Deploying a Subgraph to Subgraph Studio

```
graph deploy --studio <SUBGRAPH_SLUG>
```

After running this command, the CLI will ask for a version label, you can name it however you want, you can use labels such as 0.1 and 0.2 or use letters as well such as uniswap-v2-0.1.
33 changes: 0 additions & 33 deletions subgraph/abis/AcreBitcoinDepositor.json

This file was deleted.

82 changes: 82 additions & 0 deletions subgraph/abis/BitcoinDepositor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
[
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "depositKey",
"type": "uint256"
},
{
"indexed": true,
"internalType": "address",
"name": "caller",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "depositOwner",
"type": "address"
},
{
"indexed": true,
"internalType": "uint16",
"name": "referral",
"type": "uint16"
},
{
"indexed": false,
"internalType": "uint256",
"name": "initialAmount",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "bridgedAmount",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "depositorFee",
"type": "uint256"
}
],
"name": "DepositFinalized",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "depositKey",
"type": "uint256"
},
{
"indexed": true,
"internalType": "address",
"name": "caller",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "depositOwner",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "initialAmount",
"type": "uint256"
}
],
"name": "DepositInitialized",
"type": "event"
}
]
2 changes: 1 addition & 1 deletion subgraph/networks.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sepolia": {
"AcreBitcoinDepositor": {
"BitcoinDepositor": {
"address": "0x37E34EbC743FFAf96b56b3f62854bb7E733a4B50",
"startBlock": 5394071
}
Expand Down
12 changes: 5 additions & 7 deletions subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ enum ChainType {
}

enum LogType {
Unknown
Initialized
Finalized
Queued
FinalizedFromQueue
CancelledFromQueue
}

interface ActivityData @key(fields: "id") {
Expand All @@ -23,21 +21,21 @@ type DepositOwner @entity {
activities: [ActivityData!] @derivedFrom(field: "depositOwner")
}

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

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

Expand Down
42 changes: 0 additions & 42 deletions subgraph/src/acre-bitcoin-depositor.ts

This file was deleted.

85 changes: 85 additions & 0 deletions subgraph/src/bitcoin-depositor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import {
DepositInitialized as DepositInitializedEvent,
DepositFinalized as DepositFinalizedEvent,
} from "../generated/BitcoinDepositor/BitcoinDepositor"
import {
getOrCreateDepositOwner,
getOrCreateDeposit,
getOrCreateLog,
} from "./utils"

// eslint-disable-next-line import/prefer-default-export
export function handleDepositInitialized(event: DepositInitializedEvent): void {
const depositOwnerEntity = getOrCreateDepositOwner(event.params.depositOwner)
const depositEntity = getOrCreateDeposit(
event.params.depositKey.toHexString(),
)

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

const logDataBtc = getOrCreateLog(
`${event.transaction.hash.toHexString()}btc`,
)
const logDataEth = getOrCreateLog(
`${event.transaction.hash.toHexString()}eth`,
)

logDataBtc.activity = depositEntity.id

// This timestamp may be different than the actual time
// when the BTC transaction took place:
// It indicates when Ethereum received event about this BTC deposit,
// not when the BTC transaction happened.
logDataBtc.timestamp = event.block.timestamp
logDataBtc.chain = "Bitcoin"
logDataBtc.amount = event.params.initialAmount

logDataEth.activity = depositEntity.id
logDataEth.timestamp = event.block.timestamp
logDataEth.chain = "Ethereum"
logDataEth.amount = event.params.initialAmount

depositOwnerEntity.save()
depositEntity.save()
logDataBtc.save()
logDataEth.save()
}

export function handleDepositFinalized(event: DepositFinalizedEvent): void {
const depositOwnerEntity = getOrCreateDepositOwner(event.params.depositOwner)
const depositEntity = getOrCreateDeposit(
event.params.depositKey.toHexString(),
)

depositEntity.depositOwner = depositOwnerEntity.id
depositEntity.initialDepositAmountSatoshi = event.params.initialAmount
depositEntity.amountToDepositStBtc = event.params.bridgedAmount

const logDataBtc = getOrCreateLog(
`${event.transaction.hash.toHexString()}btc`,
)
const logDataEth = getOrCreateLog(
`${event.transaction.hash.toHexString()}eth`,
)

logDataBtc.activity = depositEntity.id

// This timestamp may be different than the actual time
// when the BTC transaction took place:
// It indicates when Ethereum received event about this BTC deposit,
// not when the BTC transaction happened.
logDataBtc.timestamp = event.block.timestamp
logDataBtc.chain = "Bitcoin"
logDataBtc.amount = event.params.initialAmount

logDataEth.activity = depositEntity.id
logDataEth.timestamp = event.block.timestamp
logDataEth.chain = "Ethereum"
logDataEth.amount = event.params.initialAmount

depositOwnerEntity.save()
depositEntity.save()
logDataBtc.save()
logDataEth.save()
}
13 changes: 6 additions & 7 deletions 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 { DepositOwner, LogData, Stake } from "../generated/schema"
import { DepositOwner, LogData, Deposit } from "../generated/schema"

export function getOrCreateDepositOwner(depositOwnerId: Address): DepositOwner {
const depositOwnerHexString = depositOwnerId.toHexString()
Expand All @@ -12,15 +12,14 @@ export function getOrCreateDepositOwner(depositOwnerId: Address): DepositOwner {
return depositOwner
}

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

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

return stake
return deposit
}

export function getOrCreateLog(logId: string): LogData {
Expand Down
Loading

0 comments on commit 7c33999

Please sign in to comment.