From 600608f23d966b7dc91f7649f3091a1daf36be3f Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Wed, 9 Oct 2024 20:36:52 +0200 Subject: [PATCH] common: add escrow accounts test Signed-off-by: Gustavo Inacio --- .../__tests__/escrow-accounts.test.ts | 67 +++++++++++++++++++ .../src/allocations/escrow-accounts.ts | 5 +- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 packages/indexer-common/src/allocations/__tests__/escrow-accounts.test.ts diff --git a/packages/indexer-common/src/allocations/__tests__/escrow-accounts.test.ts b/packages/indexer-common/src/allocations/__tests__/escrow-accounts.test.ts new file mode 100644 index 000000000..87286b989 --- /dev/null +++ b/packages/indexer-common/src/allocations/__tests__/escrow-accounts.test.ts @@ -0,0 +1,67 @@ +import { Address, toAddress } from '@graphprotocol/common-ts' +import { EscrowAccountResponse, EscrowAccounts } from '../escrow-accounts' + +const timeout = 30000 + +const SENDER_ADDRESS_1 = toAddress('ffcf8fdee72ac11b5c542428b35eef5769c409f0') +const SENDER_ADDRESS_2 = toAddress('dead47df40c29949a75a6693c77834c00b8ad624') +const SENDER_ADDRESS_3 = toAddress('6aea8894b5ab5a36cdc2d8be9290046801dd5fed') + +describe('EscrowAccounts', () => { + test( + 'fromResponse should create correctly EscrowAccount', + () => { + const response: EscrowAccountResponse = { + escrowAccounts: [ + { + sender: { + id: SENDER_ADDRESS_1, + }, + balance: '1000', + }, + { + sender: { + id: SENDER_ADDRESS_2, + }, + balance: '2000', + }, + ], + } + + const escrowAccounts = EscrowAccounts.fromResponse(response) + + expect(escrowAccounts.getBalanceForSender(SENDER_ADDRESS_1)).toEqual(1000n) + expect(escrowAccounts.getBalanceForSender(SENDER_ADDRESS_2)).toEqual(2000n) + expect(() => escrowAccounts.getBalanceForSender(SENDER_ADDRESS_3)).toThrowError() + }, + timeout, + ) + test('test subtractSenderBalance', () => { + const balances = new Map() + balances.set(SENDER_ADDRESS_1, 1000n) + balances.set(SENDER_ADDRESS_2, 1000n) + balances.set(SENDER_ADDRESS_3, 1000n) + const escrowAccounts = new EscrowAccounts(balances) + + expect(escrowAccounts.getBalanceForSender(SENDER_ADDRESS_1)).toEqual(1000n) + + escrowAccounts.subtractSenderBalance(SENDER_ADDRESS_1, 100n) + expect(escrowAccounts.getBalanceForSender(SENDER_ADDRESS_1)).toEqual(900n) + + escrowAccounts.subtractSenderBalance(SENDER_ADDRESS_1, 100n) + expect(escrowAccounts.getBalanceForSender(SENDER_ADDRESS_1)).toEqual(800n) + + escrowAccounts.subtractSenderBalance(SENDER_ADDRESS_1, 600n) + expect(escrowAccounts.getBalanceForSender(SENDER_ADDRESS_1)).toEqual(200n) + + expect(() => + escrowAccounts.subtractSenderBalance(SENDER_ADDRESS_1, 400n), + ).toThrowError() + + escrowAccounts.subtractSenderBalance(SENDER_ADDRESS_1, 200n) + + expect(escrowAccounts.getBalanceForSender(SENDER_ADDRESS_1)).toEqual(0n) + expect(escrowAccounts.getBalanceForSender(SENDER_ADDRESS_2)).toEqual(1000n) + expect(escrowAccounts.getBalanceForSender(SENDER_ADDRESS_3)).toEqual(1000n) + }) +}) diff --git a/packages/indexer-common/src/allocations/escrow-accounts.ts b/packages/indexer-common/src/allocations/escrow-accounts.ts index 44d6060c2..06784a54b 100644 --- a/packages/indexer-common/src/allocations/escrow-accounts.ts +++ b/packages/indexer-common/src/allocations/escrow-accounts.ts @@ -4,7 +4,7 @@ import gql from 'graphql-tag' type U256 = bigint -type EscrowAccountResponse = { +export type EscrowAccountResponse = { escrowAccounts: { balance: string sender: { @@ -26,6 +26,9 @@ export class EscrowAccounts { subtractSenderBalance(sender: Address, ravValue: U256) { const balance = this.getBalanceForSender(sender) + if (balance < ravValue) { + throw new Error(`Negative balances are not allowed`) + } const newBalance = balance - ravValue this.sendersBalances.set(sender, newBalance) }