Skip to content

Commit

Permalink
feat!: Changing ID types to Byte
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosvdr committed May 14, 2024
1 parent f1e35a3 commit 60d1d61
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 44 deletions.
16 changes: 8 additions & 8 deletions schema.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type EscrowAccount @entity{
# Thaw transaction hash is used as ID
id: ID!
id: Bytes!
sender: Sender!
receiver: Receiver!
balance: BigInt!
Expand All @@ -11,7 +11,7 @@ type EscrowAccount @entity{

type Sender @entity {
# sender address
id: ID!
id: Bytes!
# Sender Escrow
escrowAccounts: [EscrowAccount!] @derivedFrom(field: "sender")
transactions: [Transaction!] @derivedFrom(field: "sender")
Expand All @@ -20,25 +20,25 @@ type Sender @entity {

type Receiver @entity {
# receiver address
id: ID!
id: Bytes!
escrowAccounts: [EscrowAccount!] @derivedFrom(field: "receiver")
transactions: [Transaction!] @derivedFrom(field: "receiver")
}

type Transaction @entity{
id: ID!
transactionGroupID: String!
type Transaction @entity(immutable: true){
id: Bytes!
transactionGroupID: Bytes!
type: String!
sender: Sender!
receiver: Receiver!
allocationID: String
allocationID: Bytes
expectedAmount: BigInt
amount: BigInt!
escrowAccount: EscrowAccount!
}

type Signer @entity{
id: ID!
id: Bytes!
isAuthorized: Boolean!
sender: Sender!
thawEndTimestamp: BigInt!
Expand Down
76 changes: 41 additions & 35 deletions src/mappings/escrow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable prefer-const */
import { BigInt } from '@graphprotocol/graph-ts'
import { BigInt, Bytes } from '@graphprotocol/graph-ts'
import {
Transaction,
Sender,
Expand All @@ -9,12 +9,12 @@ import {
} from '../types/schema'
import { Deposit, Withdraw, Redeem, Thaw, AuthorizeSigner, RevokeAuthorizedSigner, CancelThaw, CancelThawSigner} from '../types/Escrow/Escrow'
let ZERO_BI = BigInt.fromI32(0)
let ZERO_AD = '0x0000000000000000000000000000000000000000'
let ZERO_AD = Bytes.fromHexString('0x0000000000000000000000000000000000000000')

export function handleThaw(event: Thaw): void {
let sender = createOrLoadSender(event.params.sender.toHexString())
let receiver = createOrLoadReceiver(event.params.receiver.toHexString())
let escrow = createOrLoadEscrowAccount(event.params.sender.toHexString(), event.params.receiver.toHexString())
let sender = createOrLoadSender(event.params.sender)
let receiver = createOrLoadReceiver(event.params.receiver)
let escrow = createOrLoadEscrowAccount(event.params.sender, event.params.receiver)

escrow.totalAmountThawing = event.params.totalAmountThawing
escrow.thawEndTimestamp = event.params.thawEndTimestamp
Expand All @@ -25,17 +25,19 @@ export function handleThaw(event: Thaw): void {
}

export function handleCancelThaw(event: CancelThaw): void {
let escrow = createOrLoadEscrowAccount(event.params.sender.toHexString(), event.params.receiver.toHexString())
let escrow = createOrLoadEscrowAccount(event.params.sender, event.params.receiver)
escrow.totalAmountThawing = ZERO_BI
escrow.thawEndTimestamp = ZERO_BI
escrow.save()
}

export function handleDeposit(event: Deposit): void {
let transaction = new Transaction(event.transaction.hash.toHexString() + '-' + event.logIndex.toString())
let sender = createOrLoadSender(event.params.sender.toHexString())
let receiver = createOrLoadReceiver(event.params.receiver.toHexString())
let escrow = createOrLoadEscrowAccount(event.params.sender.toHexString(), event.params.receiver.toHexString())
let index = event.logIndex.toI32()
let transactionId = event.transaction.hash.concatI32(index)
let transaction = new Transaction(transactionId)
let sender = createOrLoadSender(event.params.sender)
let receiver = createOrLoadReceiver(event.params.receiver)
let escrow = createOrLoadEscrowAccount(event.params.sender, event.params.receiver)

escrow.balance = escrow.balance.plus(event.params.amount)

Expand All @@ -44,17 +46,19 @@ export function handleDeposit(event: Deposit): void {
transaction.receiver = receiver.id
transaction.amount = event.params.amount
transaction.escrowAccount = escrow.id
transaction.transactionGroupID = event.transaction.hash.toHexString()
transaction.transactionGroupID = event.transaction.hash

transaction.save()
escrow.save()
}

export function handleWidthrawals(event: Withdraw): void {
let transaction = new Transaction(event.transaction.hash.toHexString() + '-' + event.logIndex.toString())
let sender = createOrLoadSender(event.params.sender.toHexString())
let receiver = createOrLoadReceiver(event.params.receiver.toHexString())
let escrow = createOrLoadEscrowAccount(event.params.sender.toHexString(), event.params.receiver.toHexString())
let index = event.logIndex.toI32()
let transactionId = event.transaction.hash.concatI32(index)
let transaction = new Transaction(transactionId)
let sender = createOrLoadSender(event.params.sender)
let receiver = createOrLoadReceiver(event.params.receiver)
let escrow = createOrLoadEscrowAccount(event.params.sender, event.params.receiver)

escrow.balance = escrow.balance.minus(event.params.amount)
escrow.totalAmountThawing = ZERO_BI
Expand All @@ -65,67 +69,69 @@ export function handleWidthrawals(event: Withdraw): void {
transaction.receiver = receiver.id
transaction.amount = event.params.amount
transaction.escrowAccount = escrow.id
transaction.transactionGroupID = event.transaction.hash.toHexString()
transaction.transactionGroupID = event.transaction.hash

transaction.save()
escrow.save()

}

export function handleRedeems(event: Redeem): void {
let transaction = new Transaction(event.transaction.hash.toHexString() + '-' + event.logIndex.toString())
let sender = createOrLoadSender(event.params.sender.toHexString())
let receiver = createOrLoadReceiver(event.params.receiver.toHexString())
let escrow = createOrLoadEscrowAccount(event.params.sender.toHexString(), event.params.receiver.toHexString())
let index = event.logIndex.toI32()
let transactionId = event.transaction.hash.concatI32(index)
let transaction = new Transaction(transactionId)
let sender = createOrLoadSender(event.params.sender)
let receiver = createOrLoadReceiver(event.params.receiver)
let escrow = createOrLoadEscrowAccount(event.params.sender, event.params.receiver)
escrow.balance = escrow.balance.minus(event.params.actualAmount)
transaction.type = "redeem"
transaction.sender = sender.id
transaction.receiver = receiver.id

transaction.amount = event.params.actualAmount
transaction.expectedAmount = event.params.expectedAmount
transaction.allocationID = event.params.allocationID.toHexString()
transaction.allocationID = event.params.allocationID
transaction.escrowAccount = escrow.id
transaction.transactionGroupID = event.transaction.hash.toHexString()
transaction.transactionGroupID = event.transaction.hash

transaction.save()
escrow.save()

}

export function handleSignerAuthorization(event: AuthorizeSigner): void {
let signer = createOrLoadSigner(event.params.signer.toHexString())
let signer = createOrLoadSigner(event.params.signer)
signer.isAuthorized = true
signer.sender = event.params.sender.toHexString()
signer.sender = event.params.sender
signer.thawEndTimestamp = ZERO_BI
signer.save()
}

export function handleRevokeSignerAuthorization(event: RevokeAuthorizedSigner): void {
let signer = createOrLoadSigner(event.params.authorizedSigner.toHexString())
let signer = createOrLoadSigner(event.params.authorizedSigner)
signer.isAuthorized = false
signer.sender = event.params.sender.toHexString()
signer.sender = event.params.sender
signer.thawEndTimestamp = ZERO_BI
signer.save()
}

export function handleThawSigner(event: CancelThawSigner): void {
let signer = createOrLoadSigner(event.params.authorizedSigner.toHexString())
signer.sender = event.params.sender.toHexString()
let signer = createOrLoadSigner(event.params.authorizedSigner)
signer.sender = event.params.sender
signer.isAuthorized = true
signer.thawEndTimestamp = event.params.thawEndTimestamp
signer.save()
}

export function handleCancelThawSigner(event: CancelThawSigner): void {
let signer = createOrLoadSigner(event.params.authorizedSigner.toHexString())
signer.sender = event.params.sender.toHexString()
let signer = createOrLoadSigner(event.params.authorizedSigner)
signer.sender = event.params.sender
signer.isAuthorized = true
signer.thawEndTimestamp = ZERO_BI
signer.save()
}

export function createOrLoadSender(id: string): Sender{
export function createOrLoadSender(id: Bytes): Sender{
let sender = Sender.load(id)
if(sender == null){
sender = new Sender(id)
Expand All @@ -134,7 +140,7 @@ export function createOrLoadSender(id: string): Sender{
return sender as Sender
}

export function createOrLoadReceiver(id: string): Receiver{
export function createOrLoadReceiver(id: Bytes): Receiver{
let receiver = Receiver.load(id)
if(receiver == null){
receiver = new Receiver(id)
Expand All @@ -143,7 +149,7 @@ export function createOrLoadReceiver(id: string): Receiver{
return receiver as Receiver
}

export function createOrLoadSigner(id: string): Signer{
export function createOrLoadSigner(id: Bytes): Signer{
let signer = Signer.load(id)
if(signer == null){
signer = new Signer(id)
Expand All @@ -155,8 +161,8 @@ export function createOrLoadSigner(id: string): Signer{
return signer as Signer
}

export function createOrLoadEscrowAccount(sender: string, receiver: string): EscrowAccount{
let sender_receiver = sender + '-' + receiver
export function createOrLoadEscrowAccount(sender: Bytes, receiver: Bytes): EscrowAccount{
let sender_receiver = sender.concat(receiver)
let escrowAccount = EscrowAccount.load(sender_receiver)
if(escrowAccount == null){
escrowAccount = new EscrowAccount(sender_receiver)
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ def check_subgraph_escrow_account(
}
}
"""
vars = {"id": sender.lower() + "-" + receiver.lower()}
vars = {"id": sender.lower() + receiver.lower().replace('0x','')}
request_data = {"query": graphql_query, "variables": vars}
resp = obtain_subgraph_info_backoff(endpoint, request_data, "escrowAccounts")
print(f" ==== Subgraph response ==== \n {resp.text}")
Expand Down

0 comments on commit 60d1d61

Please sign in to comment.