Skip to content

Commit

Permalink
add balance endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
0age committed Dec 5, 2024
1 parent 692e606 commit e31319e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,13 @@ All compact operations require a valid session ID in the `x-session-id` header.
```

3. **Get Specific Compact**

```http
GET /compact/:chainId/:claimHash
```

Example response:

```json
{
"chainId": "10",
Expand All @@ -156,6 +159,24 @@ All compact operations require a valid session ID in the `x-session-id` header.
}
```

4. **Get Resource Lock Balance**
```http
GET /balance/:chainId/:lockId
```
Returns balance information for a specific resource lock. Example response:
```json
{
"allocatableBalance": "1000000000000000000",
"allocatedBalance": "500000000000000000",
"balanceAvailableToAllocate": "500000000000000000",
"withdrawalStatus": 0
}
```
The `balanceAvailableToAllocate` will be:
- `"0"` if `withdrawalStatus` is non-zero
- `"0"` if `allocatedBalance` >= `allocatableBalance`
- `allocatableBalance - allocatedBalance` otherwise

## Development

### Prerequisites
Expand Down
42 changes: 36 additions & 6 deletions src/__tests__/routes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -384,15 +384,45 @@ describe('API Routes', () => {
});

it('should return 404 for non-existent lock', async () => {
const response = await server.inject({
method: 'GET',
url: '/balance/1/0x0000000000000000000000000000000000000000000000000000000000000000',
headers: {
'x-session-id': sessionId,
// Store original function
const originalRequest = graphqlClient.request;

// Mock GraphQL response with no resource lock
graphqlClient.request = async (): Promise<
AllocatorResponse & AccountDeltasResponse & AccountResponse
> => ({
allocator: {
supportedChains: {
items: [{ allocatorId: '1' }],
},
},
accountDeltas: {
items: [],
},
account: {
resourceLocks: {
items: [], // Empty array indicates no resource lock found
},
claims: {
items: [],
},
},
});

expect(response.statusCode).toBe(404);
try {
const response = await server.inject({
method: 'GET',
url: '/balance/1/0x0000000000000000000000000000000000000000000000000000000000000000',
headers: {
'x-session-id': sessionId,
},
});

expect(response.statusCode).toBe(404);
} finally {
// Restore original function
graphqlClient.request = originalRequest;
}
});

it('should return zero balanceAvailableToAllocate when withdrawal enabled', async () => {
Expand Down
12 changes: 6 additions & 6 deletions src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,12 +258,6 @@ export async function setupRoutes(server: FastifyInstance): Promise<void> {
const { chainId, lockId } = request.params;
const sponsor = request.session.address;

// Extract allocatorId from the lockId
const lockIdBigInt = BigInt(lockId);
const allocatorId =
(lockIdBigInt >> BigInt(160)) &
((BigInt(1) << BigInt(92)) - BigInt(1));

// Get details from GraphQL
const response = await getCompactDetails({
allocator: process.env.ALLOCATOR_ADDRESS!,
Expand All @@ -279,6 +273,12 @@ export async function setupRoutes(server: FastifyInstance): Promise<void> {
return { error: 'Resource lock not found' };
}

// Extract allocatorId from the lockId
const lockIdBigInt = BigInt(lockId);
const allocatorId =
(lockIdBigInt >> BigInt(160)) &
((BigInt(1) << BigInt(92)) - BigInt(1));

// Verify allocatorId matches
const graphqlAllocatorId =
response.allocator.supportedChains.items[0]?.allocatorId;
Expand Down

0 comments on commit e31319e

Please sign in to comment.