Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unassigned deposits #13

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 181 additions & 0 deletions abis/Escrow.abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@
"name": "EscrowStillThawing",
"type": "error"
},
{
"inputs": [],
"name": "InputsLengthMismatch",
"type": "error"
},
{
"inputs": [
{
Expand All @@ -77,6 +82,22 @@
"name": "InsufficientThawAmount",
"type": "error"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "available",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "required",
"type": "uint256"
}
],
"name": "InsufficientUnassignedEscrow",
"type": "error"
},
{
"inputs": [],
"name": "InvalidRAVSigner",
Expand Down Expand Up @@ -233,6 +254,31 @@
"name": "Deposit",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "receiver",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "DepositAssigned",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down Expand Up @@ -351,6 +397,31 @@
"name": "ThawSigner",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "depositor",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "UnassignedDeposit",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down Expand Up @@ -396,6 +467,42 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "receiver",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "assignDeposit",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address[]",
"name": "receivers",
"type": "address[]"
},
{
"internalType": "uint256[]",
"name": "amounts",
"type": "uint256[]"
}
],
"name": "assignDepositMany",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -474,6 +581,42 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address[]",
"name": "receivers",
"type": "address[]"
},
{
"internalType": "uint256[]",
"name": "amounts",
"type": "uint256[]"
}
],
"name": "depositMany",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "sender",
"type": "address"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "depositUnassigned",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -586,6 +729,25 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "sender",
"type": "address"
}
],
"name": "getUnassignedEscrowAmount",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand Down Expand Up @@ -716,6 +878,25 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "sender",
"type": "address"
}
],
"name": "unassignedAccounts",
"outputs": [
{
"internalType": "uint256",
"name": "balance",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand Down
2 changes: 2 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type Sender @entity {
escrowAccounts: [EscrowAccount!] @derivedFrom(field: "sender")
transactions: [Transaction!] @derivedFrom(field: "sender")
authorizedSigners: [AuthorizedSigner!] @derivedFrom(field: "sender")
unassignedBalance: BigInt!
}

type Receiver @entity {
Expand All @@ -37,6 +38,7 @@ type Transaction @entity{
escrowAccount: EscrowAccount!
}


type AuthorizedSigner @entity{
id: ID!
isAuthorized: Boolean!
Expand Down
33 changes: 31 additions & 2 deletions src/mappings/escrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
EscrowAccount,
AuthorizedSigner
} from '../types/schema'
import { Deposit, Withdraw, Redeem, Thaw, AuthorizeSigner, RevokeAuthorizedSigner, CancelThaw, CancelThawSigner} from '../types/Escrow/Escrow'
import { Deposit, Withdraw, Redeem, Thaw, AuthorizeSigner, RevokeAuthorizedSigner, CancelThaw, CancelThawSigner, UnassignedDeposit, DepositAssigned} from '../types/Escrow/Escrow'
let ZERO_BI = BigInt.fromI32(0)
let ZERO_AD = '0x0000000000000000000000000000000000000000'

Expand All @@ -31,6 +31,34 @@ export function handleCancelThaw(event: CancelThaw): void {
escrow.save()
}

export function handleUnassignedDeposit(event: UnassignedDeposit): void{
let sender = createOrLoadSender(event.params.sender.toHexString())
sender.unassignedBalance = sender.unassignedBalance.plus(event.params.amount)
sender.save()
}

export function handleDepositAssigned(event: DepositAssigned): 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())

transaction.type = "deposit"
transaction.sender = sender.id
transaction.receiver = receiver.id
transaction.amount = event.params.amount
transaction.escrowAccount = escrow.id
transaction.transactionGroupID = event.transaction.hash.toHexString()

sender.unassignedBalance = sender.unassignedBalance.minus(event.params.amount)

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

transaction.save()
escrow.save()
sender.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())
Expand Down Expand Up @@ -129,6 +157,7 @@ export function createOrLoadSender(id: string): Sender{
let sender = Sender.load(id)
if(sender == null){
sender = new Sender(id)
sender.unassignedBalance = ZERO_BI
sender.save()
}
return sender as Sender
Expand Down Expand Up @@ -168,4 +197,4 @@ export function createOrLoadEscrowAccount(sender: string, receiver: string): Esc
escrowAccount.save()
}
return escrowAccount as EscrowAccount
}
}
4 changes: 4 additions & 0 deletions subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,7 @@ dataSources:
handler: handleThawSigner
- event: CancelThawSigner(indexed address,indexed address,uint256)
handler: handleCancelThawSigner
- event: UnassignedDeposit(indexed address,indexed address,uint256)
handler: handleUnassignedDeposit
- event: DepositAssigned(indexed address,indexed address,uint256)
handler: handleDepositAssigned
21 changes: 21 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,27 @@ def check_subgraph_transaction(
raise Exception(f"Subgraph expected info at amount incorrect")


@backoff.on_exception(backoff.expo, Exception, max_tries=MAX_TRIES)
def check_subgraph_sender(id, unassignedBalance, endpoint=subgraph_endpoint):
graphql_query = """
query($id: String!){
senders(where: {id: $id}) {
unassignedBalance
}
}
"""
vars = {"id": id.lower()}
request_data = {"query": graphql_query, "variables": vars}
resp = obtain_subgraph_info_backoff(endpoint, request_data, "senders")
print(f" ==== Subgraph response ==== \n {resp.text}")

data = json.loads(resp.text)["data"]["senders"][0]
print(data)
balance = str(data["unassignedBalance"])
if balance != str(unassignedBalance):
raise Exception(f"Subgraph expected info at unassignedBalance incorrect")


@backoff.on_exception(backoff.expo, Exception, max_tries=MAX_TRIES)
def check_subgraph_escrow_account(
sender, receiver, total_amount_thawing, balance, endpoint=subgraph_endpoint
Expand Down
Loading
Loading