Skip to content

Commit

Permalink
Merge pull request #20 from hirosystems/develop
Browse files Browse the repository at this point in the history
release to beta
  • Loading branch information
rafaelcr authored Jul 6, 2024
2 parents 99c43ae + 3221578 commit a1658d8
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,4 @@ jobs:
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
# Only push if (there's a new release on main branch, or if building a non-main branch) and (Only run on non-PR events or only PRs that aren't from forks)
push: ${{ (github.ref != 'refs/heads/master' || needs.semantic-release.outputs.new_release_version != '') && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) }}
push: ${{ (github.ref != 'refs/heads/main' || needs.semantic-release.outputs.new_release_version != '') && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository) }}
43 changes: 41 additions & 2 deletions api/src/api/routes/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import { TypeBoxTypeProvider } from '@fastify/type-provider-typebox';
import { Type } from '@sinclair/typebox';
import { FastifyPluginCallback } from 'fastify';
import { Server } from 'http';
import { AddressSchema, LimitSchema, OffsetSchema, BalanceResponseSchema } from '../schemas';
import { parseBalanceResponse } from '../util/helpers';
import {
AddressSchema,
LimitSchema,
OffsetSchema,
BalanceResponseSchema,
ActivityResponseSchema,
} from '../schemas';
import { parseActivityResponse, parseBalanceResponse } from '../util/helpers';
import { Optional, PaginatedResponse } from '@hirosystems/api-toolkit';
import { handleCache } from '../util/cache';

Expand Down Expand Up @@ -47,5 +53,38 @@ export const AddressRoutes: FastifyPluginCallback<
}
);

fastify.get(
'/addresses/:address/activity',
{
schema: {
operationId: 'getAddressActivity',
summary: 'Address activity',
description: 'Retrieves a paginated list of rune activity for an address',
tags: ['Activity'],
params: Type.Object({
address: AddressSchema,
}),
querystring: Type.Object({
offset: Optional(OffsetSchema),
limit: Optional(LimitSchema),
}),
response: {
200: PaginatedResponse(ActivityResponseSchema, 'Paginated activity response'),
},
},
},
async (request, reply) => {
const offset = request.query.offset ?? 0;
const limit = request.query.limit ?? 20;
const results = await fastify.db.getAddressActivity(request.params.address, offset, limit);
await reply.send({
limit,
offset,
total: results.total,
results: results.results.map(r => parseActivityResponse(r)),
});
}
);

done();
};
2 changes: 1 addition & 1 deletion api/src/api/routes/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const BlockRoutes: FastifyPluginCallback<
operationId: 'getBlockActivity',
summary: 'Block activity',
description: 'Retrieves a paginated list of rune activity for a block',
tags: ['Activities'],
tags: ['Activity'],
params: Type.Object({
block: BlockSchema,
}),
Expand Down
4 changes: 2 additions & 2 deletions api/src/api/routes/etchings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export const EtchingRoutes: FastifyPluginCallback<
operationId: 'getRuneActivity',
summary: 'Rune activity',
description: 'Retrieves all activity for a Rune',
tags: ['Activities'],
tags: ['Activity'],
params: Type.Object({
etching: RuneSchema,
}),
Expand Down Expand Up @@ -121,7 +121,7 @@ export const EtchingRoutes: FastifyPluginCallback<
operationId: 'getRuneAddressActivity',
summary: 'Rune activity for address',
description: 'Retrieves all activity for a Rune address',
tags: ['Activities'],
tags: ['Activity'],
params: Type.Object({
etching: RuneSchema,
address: AddressSchema,
Expand Down
2 changes: 1 addition & 1 deletion api/src/api/routes/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const TransactionRoutes: FastifyPluginCallback<
operationId: 'getTransactionActivity',
summary: 'Transaction activity',
description: 'Retrieves a paginated list of rune activity for a transaction',
tags: ['Activities'],
tags: ['Activity'],
params: Type.Object({
tx_id: TransactionIdSchema,
}),
Expand Down
4 changes: 2 additions & 2 deletions api/src/api/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ export const OpenApiSchemaOptions: SwaggerOptions = {
description: 'Rune etchings',
},
{
name: 'Activities',
description: 'Rune activities',
name: 'Activity',
description: 'Rune activity',
},
{
name: 'Balances',
Expand Down
31 changes: 28 additions & 3 deletions api/src/pg/pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,11 @@ export class PgStore extends BasePgStore {
filter: PgSqlQuery,
count: PgSqlQuery,
offset: Offset,
limit: Limit
limit: Limit,
cte?: PgSqlQuery
): Promise<DbPaginatedResult<DbItemWithRune<DbLedgerEntry>>> {
const results = await this.sql<DbCountedQueryResult<DbItemWithRune<DbLedgerEntry>>[]>`
${cte ? cte : this.sql``}
SELECT l.*, r.name, r.spaced_name, r.divisibility, ${count} AS total
FROM ledger AS l
INNER JOIN runes AS r ON r.id = l.rune_id
Expand All @@ -159,9 +161,14 @@ export class PgStore extends BasePgStore {
async getRuneActivity(runeId: Rune, offset: Offset, limit: Limit) {
return this.getActivity(
runeFilter(this.sql, runeId, 'r'),
this.sql`r.total_operations`,
this.sql`COALESCE((SELECT total_operations FROM count), 0)`,
offset,
limit
limit,
this.sql`WITH count AS (
SELECT total_operations FROM supply_changes
WHERE rune_id = (SELECT id FROM runes WHERE ${runeFilter(this.sql, runeId)})
ORDER BY block_height DESC LIMIT 1
)`
);
}

Expand All @@ -174,6 +181,24 @@ export class PgStore extends BasePgStore {
);
}

async getAddressActivity(address: Address, offset: Offset, limit: Limit) {
return this.getActivity(
this.sql`address = ${address}`,
this.sql`COALESCE((SELECT total_operations FROM count), 0)`,
offset,
limit,
this.sql`WITH recent AS (
SELECT DISTINCT ON (rune_id) total_operations
FROM balance_changes
WHERE address = ${address}
ORDER BY rune_id, block_height DESC
),
count AS (
SELECT SUM(total_operations) AS total_operations FROM recent
)`
);
}

async getTransactionActivity(txId: TransactionId, offset: Offset, limit: Limit) {
return this.getActivity(this.sql`l.tx_id = ${txId}`, this.sql`COUNT(*) OVER()`, offset, limit);
}
Expand Down
2 changes: 1 addition & 1 deletion docker/runehook.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM rust:bullseye as build
FROM rust:bullseye AS build

WORKDIR /app

Expand Down
2 changes: 1 addition & 1 deletion docker/runes-api.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM node:20-alpine

WORKDIR /app
COPY ./api /app
COPY .git /app/.git
COPY .git /.git

RUN apk add --no-cache --virtual .build-deps git
RUN npm ci --no-audit && \
Expand Down

0 comments on commit a1658d8

Please sign in to comment.