Skip to content

Commit

Permalink
keep lock id in BigInt internally
Browse files Browse the repository at this point in the history
  • Loading branch information
0age committed Dec 9, 2024
1 parent 6513b44 commit 9cad3c6
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
10 changes: 5 additions & 5 deletions src/__tests__/balance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ describe('Balance Functions', () => {
db,
'0x4560000000000000000000000000000000000456',
'10',
'0x0000000000000000000000000000000000000000000000000000000000123000',
BigInt('0x123000'),
[]
);

Expand All @@ -179,7 +179,7 @@ describe('Balance Functions', () => {
db,
'0x4560000000000000000000000000000000000456',
'10',
'0x0000000000000000000000000000000000000000000000000000000000123000',
BigInt('0x123000'),
['0x1000000000000000000000000000000000000000000000000000000000000001'] // Processed claim for the active compact
);

Expand All @@ -192,7 +192,7 @@ describe('Balance Functions', () => {
db,
'0x4560000000000000000000000000000000000456',
'10',
'0x0000000000000000000000000000000000000000000000000000000000123000',
BigInt('0x123000'),
[
'0x1000000000000000000000000000000000000000000000000000000000000001',
'0x2000000000000000000000000000000000000000000000000000000000000002',
Expand All @@ -207,7 +207,7 @@ describe('Balance Functions', () => {
db,
'0x7890000000000000000000000000000000000789', // Non-existent sponsor
'10',
'0x0000000000000000000000000000000000000000000000000000000000123000',
BigInt('0x123000'),
[]
);

Expand All @@ -219,7 +219,7 @@ describe('Balance Functions', () => {
db,
'0x4560000000000000000000000000000000000456',
'10',
'0x0000000000000000000000000000000000000000000000000000000000456000', // Non-existent lock
BigInt('0x456000'), // Non-existent lock
[]
);

Expand Down
10 changes: 2 additions & 8 deletions src/balance.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { PGlite } from '@electric-sql/pglite';
import { getFinalizationThreshold } from './chain-config.js';
import { hexToBytes } from 'viem/utils';
import { toBigInt } from './utils/encoding.js';

/**
* Calculate the total allocated balance for a given sponsor, chain, and resource lock
Expand All @@ -14,7 +13,7 @@ export async function getAllocatedBalance(
db: PGlite,
sponsor: string,
chainId: string,
lockId: string,
lockId: bigint,
processedClaimHashes: string[]
): Promise<bigint> {
try {
Expand All @@ -28,13 +27,8 @@ export async function getAllocatedBalance(
: (`0x${sponsor}` as `0x${string}`)
);

// Convert lockId to BigInt first (handles both decimal and hex formats)
const lockIdBigInt = toBigInt(lockId, 'lockId');
if (lockIdBigInt === null) {
throw new Error('Invalid lockId');
}
// Convert BigInt to proper hex string with 0x prefix and padding
const lockIdHex = '0x' + lockIdBigInt.toString(16).padStart(64, '0');
const lockIdHex = '0x' + lockId.toString(16).padStart(64, '0');
const lockIdBytes = hexToBytes(lockIdHex as `0x${string}`);

const processedClaimBytea = processedClaimHashes.map((hash) =>
Expand Down
17 changes: 14 additions & 3 deletions src/routes/balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getAddress } from 'viem/utils';
import { getAllocatedBalance } from '../balance';
import { getCompactDetails, getAllResourceLocks } from '../graphql';
import { createAuthMiddleware } from './session';
import { toBigInt } from '../utils/encoding';

interface Balance {
chainId: string;
Expand Down Expand Up @@ -88,12 +89,18 @@ export async function setupBalanceRoutes(
const allocatableBalance =
BigInt(resourceLock.balance) + pendingBalance;

// Convert lockId to BigInt
const lockIdBigInt = toBigInt(lock.resourceLock.lockId, 'lockId');
if (lockIdBigInt === null) {
throw new Error('Invalid lockId format');
}

// Get allocated balance
const allocatedBalance = await getAllocatedBalance(
server.db,
sponsor,
lock.chainId,
lock.resourceLock.lockId,
lockIdBigInt,
lockDetails.account.claims.items.map((claim) => claim.claimHash)
);

Expand Down Expand Up @@ -175,7 +182,11 @@ export async function setupBalanceRoutes(
}

// Extract allocatorId from the lockId
const lockIdBigInt = BigInt(lockId);
const lockIdBigInt = toBigInt(lockId, 'lockId');
if (lockIdBigInt === null) {
throw new Error('Invalid lockId format');
}

const allocatorId =
(lockIdBigInt >> BigInt(160)) &
((BigInt(1) << BigInt(92)) - BigInt(1));
Expand Down Expand Up @@ -209,7 +220,7 @@ export async function setupBalanceRoutes(
server.db,
sponsor,
chainId,
lockId,
lockIdBigInt,
response.account.claims.items.map((claim) => claim.claimHash)
);

Expand Down
2 changes: 1 addition & 1 deletion src/validation/allocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export async function validateAllocation(
db,
getAddress(compact.sponsor).toLowerCase(),
chainId,
bigintToHex(compact.id),
compact.id,
response.account.claims.items.map((item) => item.claimHash)
);

Expand Down

0 comments on commit 9cad3c6

Please sign in to comment.