-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
70 changed files
with
2,394 additions
and
173 deletions.
There are no files selected for viewing
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
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
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,38 @@ | ||
name: Docker Build and Push | ||
|
||
description: Build and push Docker images | ||
|
||
inputs: | ||
context: | ||
description: 'The build context' | ||
required: false | ||
default: '.' | ||
dockerHubPassword: | ||
description: 'Docker Hub token' | ||
required: true | ||
dockerHubUsername: | ||
description: 'Docker Hub username' | ||
required: true | ||
images: | ||
description: 'The Docker image name' | ||
required: true | ||
|
||
runs: | ||
using: composite | ||
steps: | ||
- uses: docker/setup-buildx-action@v3 | ||
- id: meta | ||
uses: docker/metadata-action@v5 | ||
with: | ||
images: ${{ inputs.images }} | ||
- uses: docker/login-action@v3 | ||
with: | ||
password: ${{ inputs.dockerHubPassword }} | ||
username: ${{ inputs.dockerHubUsername }} | ||
- uses: docker/build-push-action@v6 | ||
with: | ||
context: ${{ inputs.context }} | ||
labels: ${{ steps.meta.outputs.labels }} | ||
platforms: linux/amd64 | ||
push: true | ||
tags: ${{ steps.meta.outputs.tags }} |
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,31 @@ | ||
name: Publish Docker images | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- 'staking-points/**' | ||
- 'token-prices/**' | ||
workflow_dispatch: | ||
|
||
jobs: | ||
build-and-push: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
include: | ||
- context: staking-points | ||
image: hemilabs/staking-points-api | ||
- context: token-prices/api | ||
image: hemilabs/token-prices-api | ||
- context: token-prices/cron | ||
image: hemilabs/token-prices-cron | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: ./.github/actions/docker-build-push | ||
with: | ||
context: ${{ matrix.context }} | ||
dockerHubPassword: ${{ secrets.DOCKERHUB_TOKEN }} | ||
dockerHubUsername: ${{ secrets.DOCKERHUB_USERNAME }} | ||
images: ${{ matrix.image }} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,16 @@ | ||
FROM node:20.18.3-alpine | ||
|
||
ENV NODE_ENV=production | ||
|
||
WORKDIR /app | ||
|
||
COPY package*.json . | ||
RUN npm install --production | ||
|
||
USER node | ||
|
||
COPY . . | ||
|
||
EXPOSE 3002 | ||
|
||
CMD ["npm", "start"] |
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,37 @@ | ||
# Staking Points API | ||
|
||
This service provides the amount of points any user has earned through the Hemi Staking Campaign. | ||
Data is provided by [Absinthe](https://absinthe.network/). | ||
|
||
## Configuration | ||
|
||
These environment variables control how the API works: | ||
|
||
| Variable | Description | Default | | ||
| ---------------- | --------------------------------------------------------------- | ------------------------------------------ | | ||
| ABSINTHE_API_KEY | The JWT used to authenticate to the Absinthe GraphQL API. | | | ||
| ABSINTHE_API_URL | The Absinthe GraphQL API URL. | `https://gql3.absinthe.network/v1/graphql` | | ||
| ORIGINS | Comma-separated list of allowed origins. '\*' is not supported. | `http://localhos:3000` | | ||
| PORT | The HTTP port the server listens for requests. | 3002 | | ||
|
||
## Local development and testing | ||
|
||
To start the service, run: | ||
|
||
```sh | ||
ABSINTHE_API_KEY=MY_API_KEY docker compose up -d | ||
``` | ||
|
||
Alternatively, if the environment variables are stored in a `.env.local` file, run: | ||
|
||
```sh | ||
docker build --tag staking-points-api:local . | ||
docker run --env-file .env.local --init --interactive --publish 3002:3002 --rm --tty staking-points-api:local | ||
``` | ||
|
||
Then the points for a user can be obtained as follows: | ||
|
||
```console | ||
$ curl http://localhost:3002/0x85e0D9e73c12eFE889750f44422a77B544D48d17 | ||
{"points":270644} | ||
``` |
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,5 @@ | ||
{ | ||
"engines": { | ||
"node": ">=20" | ||
} | ||
} |
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,85 @@ | ||
'use strict' | ||
|
||
const http = require('http') | ||
|
||
const absintheApiKey = process.env.ABSINTHE_API_KEY | ||
const absintheApiUrl = | ||
process.env.ABSINTHE_API_URL || 'https://gql3.absinthe.network/v1/graphql' | ||
const portStr = process.env.PORT || '3002' | ||
const port = parseInt(portStr) | ||
const originsStr = process.env.ORIGINS || 'http://localhost:3000' | ||
const origins = originsStr.split(',') | ||
|
||
async function getUserPoints(address) { | ||
const res = await fetch(absintheApiUrl, { | ||
body: JSON.stringify({ | ||
query: `query ($address: String!) { | ||
query_address_points(args: { address: $address, client_season: "hemi" }) { | ||
points | ||
} | ||
}`, | ||
variables: { address }, | ||
}), | ||
headers: { | ||
'Authorization': `Bearer ${absintheApiKey}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
method: 'POST', | ||
}) | ||
if (!res.ok) { | ||
throw new Error(`Points query failed: ${res.statusText}`) | ||
} | ||
|
||
const { data, errors } = await res.json() | ||
if (errors && errors.length) { | ||
const messages = errors.map(err => err.message).join(', ') | ||
throw new Error(`Points query errored: ${messages}`) | ||
} | ||
|
||
return data?.query_address_points[0]?.points || 0 | ||
} | ||
|
||
async function handleRequest(req, res) { | ||
const { 'access-control-request-headers': headers, origin } = req.headers | ||
if (headers) { | ||
res.setHeader('Access-Control-Allow-Headers', headers) | ||
} | ||
res.setHeader('Access-Control-Allow-Methods', 'GET') | ||
const allowOrigin = origins.includes(origin) ? origin : false | ||
res.setHeader('Access-Control-Allow-Origin', allowOrigin) | ||
|
||
if (req.method === 'OPTIONS') { | ||
res.writeHead(204) | ||
res.end() | ||
return | ||
} | ||
|
||
if (req.method !== 'GET') { | ||
res.writeHead(405, { 'Content-Type': 'application/json' }) | ||
res.end(JSON.stringify({ error: 'Method Not Allowed' })) | ||
return | ||
} | ||
|
||
const address = req.url.slice(1) | ||
if (!/^0x[0-9a-f]{40}$/i.test(address)) { | ||
res.writeHead(400, { 'Content-Type': 'application/json' }) | ||
res.end(JSON.stringify({ error: 'Invalid address format' })) | ||
return | ||
} | ||
|
||
let statusCode, data | ||
try { | ||
statusCode = 200 | ||
data = { points: await getUserPoints(address) } | ||
} catch (err) { | ||
// eslint-disable-next-line no-console | ||
console.error(`Failed to handle request: ${err}`) | ||
statusCode = 500 | ||
data = { error: 'Internal Server Error' } | ||
} | ||
|
||
res.writeHead(statusCode, { 'Content-Type': 'application/json' }) | ||
res.end(JSON.stringify(data)) | ||
} | ||
|
||
http.createServer(handleRequest).listen(port) |
Oops, something went wrong.