Skip to content

Commit

Permalink
Initialize smart-guessr service (#812)
Browse files Browse the repository at this point in the history
* Initialize smart-guessr service
  • Loading branch information
rimrakhimov authored Mar 28, 2024
1 parent 4ff119c commit 82cb683
Show file tree
Hide file tree
Showing 9 changed files with 442 additions and 0 deletions.
92 changes: 92 additions & 0 deletions .github/workflows/smart-guessr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
on:
push:
branches:
- 'main'
tags:
- 'smart-guessr/v*'
paths:
- smart-guessr/**
- .github/workflows/smart-guessr.yml
pull_request:
paths:
- smart-guessr/**
- .github/workflows/smart-guessr.yml

name: Test and docker (smart-guessr)

env:
REGISTRY: ghcr.io
IMAGE_NAME: blockscout/smart-guessr

defaults:
run:
working-directory: smart-guessr

jobs:
test:
name: Tests
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4

- name: Install bun
uses: oven-sh/setup-bun@v1

- name: Install dependencies
run: bun install

- name: Run tests
run: bun test
if: success() || failure()

push:
name: Docker build and docker push
needs:
- test
if: |
always() &&
(needs.test.result == 'success' || needs.test.result == 'cancelled')
timeout-minutes: 30
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4

- uses: actions-ecosystem/action-regex-match@v2
id: regex
with:
text: ${{ github.ref }}
regex: '^(refs\/tags\/smart-guessr\/(v\d+\.\d+\.\d+))|(refs\/heads\/(main))$'

- name: Extract tag name
id: tags_extractor
run: |
t=${{ steps.regex.outputs.group2 }}
m=${{ steps.regex.outputs.group4 }}
(if ! [[ "$t" == "" ]]; then echo tags=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:$t, ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest; elif ! [[ "$m" == "" ]]; then echo tags=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:$m; else echo tags=; fi) >> $GITHUB_OUTPUT
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Extract metadata for Docker
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build and push
uses: docker/build-push-action@v5
with:
context: "smart-guessr"
file: "smart-guessr/Dockerfile"
push: ${{ steps.tags_extractor.outputs.tags != '' }}
tags: ${{ steps.tags_extractor.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=registry,ref=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:build-cache
cache-to: ${{ github.ref == 'refs/heads/main' && format('type=registry,ref={0}/{1}:build-cache,mode=max', env.REGISTRY, env.IMAGE_NAME) || '' }}
42 changes: 42 additions & 0 deletions smart-guessr/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env.local
.env.development.local
.env.test.local
.env.production.local

# vercel
.vercel

**/*.trace
**/*.zip
**/*.tar.gz
**/*.tgz
**/*.log
package-lock.json
**/*.bun
14 changes: 14 additions & 0 deletions smart-guessr/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM oven/bun

WORKDIR /app

COPY package.json .
COPY bun.lockb .

RUN bun install --production

COPY src src
COPY tsconfig.json .

ENV NODE_ENV production
CMD ["bun", "src/index.ts"]
15 changes: 15 additions & 0 deletions smart-guessr/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Elysia with Bun runtime

## Getting Started
To get started with this template, simply paste this command into your terminal:
```bash
bun create elysia ./elysia-example
```

## Development
To start the development server run:
```bash
bun run dev
```

Open http://localhost:3000/ with your browser to see the result.
Binary file added smart-guessr/bun.lockb
Binary file not shown.
23 changes: 23 additions & 0 deletions smart-guessr/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "smart-guessr",
"version": "1.0.50",
"scripts": {
"dev": "bun --watch src/index.ts",
"build": "bun build src/index.ts",
"start": "NODE_ENV=production bun src/index.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"dependencies": {
"@bogeychan/elysia-logger": "^0.0.20",
"@elysiajs/eden": "^1.0.7",
"@elysiajs/swagger": "^1.0.3",
"@shazow/whatsabi": "^0.11.0",
"elysia": "latest",
"ethers": "^6.11.1",
"import": "^0.0.6"
},
"devDependencies": {
"bun-types": "latest"
},
"module": "src/index.js"
}
75 changes: 75 additions & 0 deletions smart-guessr/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { Elysia, t } from "elysia";
import { swagger } from '@elysiajs/swagger'
import { whatsabi } from "@shazow/whatsabi";
import { ethers } from "ethers";
import { logger } from "@bogeychan/elysia-logger";

export function getPort() {
let port = process.env.SMART_GUESSR__PORT;
if (port == undefined) {
port = "3000";
}
return port;
}

const signatureLookup = new whatsabi.loaders.MultiSignatureLookup([
new whatsabi.loaders.OpenChainSignatureLookup(),
new whatsabi.loaders.FourByteSignatureLookup(),
]);

export function initApp(port: string | number) {
return new Elysia()
.use(swagger({
path: '/api/v1/swagger'
}))
.use(
logger({
level: "info",
})
)
.get("/api/v1/abi", async ( { request, log, query }) => {
log.info(request, "New request");
const result = await processAbi(query.address, query.provider);
log.info(request, `Found ${result.length} abi items`);
return result;
}, {
query: t.Object({
address: t.String(),
provider: t.String(),
}),
beforeHandle({ error, query }) {
query.address = normalizeAddress(query.address);
if (!ethers.isAddress(query.address)) {
return error(400, "Invalid address value")
}
}
})
.listen(port);
}

const port = getPort();
let app = initApp(port);

console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);

async function processAbi(address: string, provider_url: string) {
const provider = ethers.getDefaultProvider(provider_url);

let abi = await whatsabi.autoload(address, {
provider: provider,
signatureLookup: signatureLookup,
abiLoader: false
});

return abi.abi
}

function normalizeAddress(address: string) {
let normalized = address.toLowerCase();
if (!normalized.startsWith('0x')) {
normalized = '0x' + normalized;
}
return normalized;
}
Loading

0 comments on commit 82cb683

Please sign in to comment.