-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: check escrow balance before rav redeem
Signed-off-by: Gustavo Inacio <[email protected]>
- Loading branch information
Showing
2 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
packages/indexer-common/src/allocations/escrow-accounts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Address, toAddress } from '@graphprotocol/common-ts' | ||
import { TAPSubgraph } from '../tap-subgraph' | ||
import gql from 'graphql-tag' | ||
|
||
type U256 = bigint | ||
|
||
type EscrowAccountResponse = { | ||
escrowAccounts: { | ||
balance: string | ||
sender: { | ||
id: string | ||
} | ||
}[] | ||
} | ||
|
||
export class EscrowAccounts { | ||
constructor(private sendersBalances: Map<Address, U256>) {} | ||
|
||
getBalanceForSender(sender: Address): U256 { | ||
const balance = this.sendersBalances.get(sender) | ||
if (balance === undefined) { | ||
throw new Error(`No balance found for sender: ${sender}`) | ||
} | ||
return balance | ||
} | ||
|
||
subtractSenderBalance(sender: Address, ravValue: U256) { | ||
const balance = this.getBalanceForSender(sender) | ||
const newBalance = balance - ravValue | ||
this.sendersBalances.set(sender, newBalance) | ||
} | ||
|
||
static fromResponse(response: EscrowAccountResponse): EscrowAccounts { | ||
const sendersBalances = new Map<Address, U256>() | ||
response.escrowAccounts.forEach((account) => { | ||
sendersBalances.set(toAddress(account.sender.id), BigInt(account.balance)) | ||
}) | ||
|
||
return new EscrowAccounts(sendersBalances) | ||
} | ||
} | ||
|
||
export const getEscrowAccounts = async ( | ||
tapSubgraph: TAPSubgraph, | ||
indexer: Address, | ||
): Promise<EscrowAccounts> => { | ||
const result = await tapSubgraph.query<EscrowAccountResponse>( | ||
gql` | ||
query EscrowAccountQuery($indexer: ID!) { | ||
escrowAccounts(where: { receiver_: { id: $indexer } }) { | ||
balance | ||
sender { | ||
id | ||
} | ||
} | ||
} | ||
`, | ||
{ indexer }, | ||
) | ||
if (!result.data) { | ||
throw `There was an error while querying Tap Subgraph. Errors: ${result.error}` | ||
} | ||
return EscrowAccounts.fromResponse(result.data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters