Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
lAmeR1 committed Jul 16, 2024
2 parents 7d07e71 + 59f8f81 commit f4440bc
Show file tree
Hide file tree
Showing 14 changed files with 1,555 additions and 1,109 deletions.
46 changes: 32 additions & 14 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,44 @@
name: Docker Image CI

name: Docker Image
on:
release:
types: [ published ]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
os: [ ubuntu-latest ]
arch: [ amd64, arm64 ]

runs-on: ${{ matrix.os }}

steps:
- name: Checkout
uses: actions/checkout@v3
- name: Add SHORT_SHA env property with commit short sha
run: echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-7`" >> $GITHUB_ENV
- name: Checkout code
uses: actions/checkout@v2

- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: lamer1
password: ${{ secrets.DOCKER_ACCESS_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: supertypo/kaspa-rest-server:${{github.ref_name}}, supertypo/kaspa-rest-server:latest
file: ./docker/Dockerfile
build-args: |
version=${{github.ref_name}}

- name: Set up QEMU (for ARM64 emulation)
if: matrix.arch == 'arm64'
run: |
sudo apt-get install -y qemu-user-static
- name: Set up Docker Buildx (for multi-platform builds)
uses: docker/setup-buildx-action@v1

- name: Build and push Docker image
run: |
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t supertypo/kaspa-rest-server:${{github.ref_name}} \
-t supertypo/kaspa-rest-server:latest \
--build-arg version=${{github.ref_name}} \
--push .
docker buildx imagetools inspect supertypo/kaspa-rest-server:latest
58 changes: 38 additions & 20 deletions .github/workflows/docker-testimage.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
name: Docker Image TESTBUILD

name: Docker Image
on:
workflow_dispatch
push:
branches-ignore:
- main

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest]
arch: [amd64, arm64]

runs-on: ${{ matrix.os }}

steps:
- name: Checkout
uses: actions/checkout@v3
- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: lamer1
password: ${{ secrets.DOCKER_ACCESS_TOKEN }}
- name: Build and push
uses: docker/build-push-action@v4
with:
context: .
push: true
tags: supertypo/kaspa-rest-server:test
file: ./docker/Dockerfile
build-args: |
version=${{github.ref_name}}
- name: Add SHORT_SHA env property with commit short sha
run: echo "SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-7`" >> $GITHUB_ENV
- name: Checkout code
uses: actions/checkout@v2

- name: Login to Docker Hub
uses: docker/login-action@v2
with:
username: lamer1
password: ${{ secrets.DOCKER_ACCESS_TOKEN }}

- name: Set up QEMU (for ARM64 emulation)
if: matrix.arch == 'arm64'
run: |
sudo apt-get install -y qemu-user-static
- name: Set up Docker Buildx (for multi-platform builds)
uses: docker/setup-buildx-action@v1

- name: Build and push Docker image
run: |
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t supertypo/kaspa-rest-server:${SHORT_SHA} \
--push .
docker buildx imagetools inspect supertypo/kaspa-rest-server:${SHORT_SHA}
24 changes: 24 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM python:3.12.3-slim

ARG REPO_DIR

EXPOSE 8000

ENV KASPAD_HOST_1=n.seeder1.kaspad.net:16110
ARG version
ENV VERSION=$version

RUN apt update
RUN apt install uvicorn gunicorn -y

WORKDIR /app
COPY . .

RUN python -m pip install --upgrade pip
RUN pip install poetry
RUN poetry install --no-root --no-interaction

# make pipenv commands still running
RUN ln /usr/local/bin/poetry /usr/local/bin/pipenv

CMD poetry run gunicorn -b 0.0.0.0:8000 -w 4 -k uvicorn.workers.UvicornWorker main:app --timeout 120
1,021 changes: 0 additions & 1,021 deletions Pipfile.lock

This file was deleted.

24 changes: 24 additions & 0 deletions constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os

NETWORK_TYPE = os.getenv('NETWORK_TYPE', 'mainnet').lower()

match NETWORK_TYPE:
case "mainnet":
address_prefix = "kaspa"
address_example = "kaspa:qqkqkzjvr7zwxxmjxjkmxxdwju9kjs6e9u82uh59z07vgaks6gg62v8707g73"
case "testnet":
address_prefix = "kaspatest"
address_example = "kaspatest:qpqz2vxj23kvh0m73ta2jjn2u4cv4tlufqns2eap8mxyyt0rvrxy6ejkful67"
case "simnet":
address_prefix = "kaspasim"
address_example = "kaspasim:qpqz2vxj23kvh0m73ta2jjn2u4cv4tlufqns2eap8mxyyt0rvrxy6ejkful67"
case "devnet":
address_prefix = "kaspadev"
address_example = "kaspadev:qpqz2vxj23kvh0m73ta2jjn2u4cv4tlufqns2eap8mxyyt0rvrxy6ejkful67"
case _:
raise ValueError(f'Network type {NETWORK_TYPE} not supported.')

ADDRESS_PREFIX = address_prefix
ADDRESS_EXAMPLE = address_example

REGEX_KASPA_ADDRESS = "^" + ADDRESS_PREFIX + ":[a-z0-9]{61,63}$"
35 changes: 0 additions & 35 deletions docker/Dockerfile

This file was deleted.

15 changes: 14 additions & 1 deletion endpoints/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# encoding: utf-8
import os
from fastapi import HTTPException
from functools import wraps

from fastapi import HTTPException
from constants import NETWORK_TYPE


def filter_fields(response_dict, fields):
Expand All @@ -23,3 +24,15 @@ async def wrapper(*args, **kwargs):
return await func(*args, **kwargs)

return wrapper


def mainnet_only(func):
@wraps(func)
async def wrapper(*args, **kwargs):
if NETWORK_TYPE != "mainnet":
raise HTTPException(status_code=503, detail="Endpoint not available. "
"This endpoint is only available in mainnet.")
return await func(*args, **kwargs)

return wrapper

17 changes: 7 additions & 10 deletions endpoints/get_address_transactions.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# encoding: utf-8
import re
import time
import os
from enum import Enum
from typing import List

from fastapi import Path, Query, HTTPException
from fastapi import Path, Query
from pydantic import BaseModel
from sqlalchemy import text, func
from sqlalchemy.future import select
from starlette.responses import Response
from typing import List

from constants import ADDRESS_EXAMPLE, REGEX_KASPA_ADDRESS
from dbsession import async_session
from endpoints import sql_db_only
from endpoints.get_transactions import search_for_transactions, TxSearch, TxModel
Expand Down Expand Up @@ -56,8 +54,7 @@ class PreviousOutpointLookupMode(str, Enum):
@sql_db_only
async def get_transactions_for_address(
kaspaAddress: str = Path(
description="Kaspa address as string e.g. "
"kaspa:pzhh76qc82wzduvsrd9xh4zde9qhp0xc8rl7qu2mvl2e42uvdqt75zrcgpm00",
description=f"Kaspa address as string e.g. {ADDRESS_EXAMPLE}",
regex=REGEX_KASPA_ADDRESS)):
"""
Get all transactions for a given address from database
Expand Down Expand Up @@ -103,7 +100,7 @@ async def get_transactions_for_address(
async def get_full_transactions_for_address(
kaspaAddress: str = Path(
description="Kaspa address as string e.g. "
"kaspa:pzhh76qc82wzduvsrd9xh4zde9qhp0xc8rl7qu2mvl2e42uvdqt75zrcgpm00",
f"{ADDRESS_EXAMPLE}",
regex=REGEX_KASPA_ADDRESS),
limit: int = Query(
description="The number of records to get",
Expand Down Expand Up @@ -211,7 +208,7 @@ async def get_full_transactions_for_address_page(
async def get_transaction_count_for_address(
kaspaAddress: str = Path(
description="Kaspa address as string e.g. "
"kaspa:pzhh76qc82wzduvsrd9xh4zde9qhp0xc8rl7qu2mvl2e42uvdqt75zrcgpm00",
f"{ADDRESS_EXAMPLE}",
regex=REGEX_KASPA_ADDRESS)
):
"""
Expand Down
6 changes: 3 additions & 3 deletions endpoints/get_balance.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
# encoding: utf-8

from fastapi import Path, HTTPException
from pydantic import BaseModel

from constants import ADDRESS_EXAMPLE, REGEX_KASPA_ADDRESS
from server import app, kaspad_client

REGEX_KASPA_ADDRESS = "^kaspa(test)?\:[a-z0-9]{61,63}$"

class BalanceResponse(BaseModel):
address: str = "kaspa:pzhh76qc82wzduvsrd9xh4zde9qhp0xc8rl7qu2mvl2e42uvdqt75zrcgpm00"
address: str = ADDRESS_EXAMPLE
balance: int = 38240000000


@app.get("/addresses/{kaspaAddress}/balance", response_model=BalanceResponse, tags=["Kaspa addresses"])
async def get_balance_from_kaspa_address(
kaspaAddress: str = Path(
description="Kaspa address as string e.g. kaspa:pzhh76qc82wzduvsrd9xh4zde9qhp0xc8rl7qu2mvl2e42uvdqt75zrcgpm00",
description=f"Kaspa address as string e.g. {ADDRESS_EXAMPLE}",
regex=REGEX_KASPA_ADDRESS)):
"""
Get balance for a given kaspa address
Expand Down
3 changes: 3 additions & 0 deletions endpoints/get_price.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from pydantic import BaseModel
from starlette.responses import PlainTextResponse

from endpoints import mainnet_only
from helper import get_kas_price, get_kas_market_data
from server import app

Expand All @@ -13,6 +14,7 @@ class PriceResponse(BaseModel):


@app.get("/info/price", response_model=PriceResponse | str, tags=["Kaspa network info"])
@mainnet_only
async def get_price(stringOnly: bool = False):
"""
Returns the current price for Kaspa in USD.
Expand All @@ -27,6 +29,7 @@ async def get_price(stringOnly: bool = False):
@app.get("/info/market-data",
tags=["Kaspa network info"],
include_in_schema=False)
@mainnet_only
async def get_market_data():
"""
Returns market data for kaspa.
Expand Down
9 changes: 5 additions & 4 deletions endpoints/get_utxos.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# encoding: utf-8
from typing import List

import os
from fastapi import Path, HTTPException
from pydantic import BaseModel
from typing import List

from constants import REGEX_KASPA_ADDRESS, ADDRESS_EXAMPLE
from server import app, kaspad_client

REGEX_KASPA_ADDRESS = "^kaspa(test)?\:[a-z0-9]{61,63}$"
Expand All @@ -25,14 +26,14 @@ class UtxoModel(BaseModel):


class UtxoResponse(BaseModel):
address: str = "kaspa:qrzk988gtanp3nf76xkpexwud5cxfmfygqf42hz38pwea74s6qrj75jee85nj"
address: str = ADDRESS_EXAMPLE
outpoint: OutpointModel
utxoEntry: UtxoModel


@app.get("/addresses/{kaspaAddress}/utxos", response_model=List[UtxoResponse], tags=["Kaspa addresses"])
async def get_utxos_for_address(kaspaAddress: str = Path(
description="Kaspa address as string e.g. kaspa:qqkqkzjvr7zwxxmjxjkmxxdwju9kjs6e9u82uh59z07vgaks6gg62v8707g73",
description=f"Kaspa address as string e.g. {ADDRESS_EXAMPLE}",
regex=REGEX_KASPA_ADDRESS)):
"""
Lists all open utxo for a given kaspa address
Expand Down
Loading

0 comments on commit f4440bc

Please sign in to comment.