Skip to content

Commit

Permalink
Merge pull request #1249 from edenia/feat/evm-backend
Browse files Browse the repository at this point in the history
  • Loading branch information
xavier506 authored Aug 10, 2023
2 parents 3a38b45 + 8e88b51 commit 48edcbf
Show file tree
Hide file tree
Showing 117 changed files with 5,052 additions and 18 deletions.
11 changes: 11 additions & 0 deletions .env.telos
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ HAPI_RE_CAPTCHA_PROJECT_ID=
HAPI_PUBLIC_RE_CAPTCHA_KEY=
HAPI_CREATE_ACCOUNT_ACTION_NAME=

# hapi-evm
HAPI_EVM_SERVER_PORT=9090
HAPI_EVM_SERVER_ADDRESS=hapi-evm
HAPI_EVM_ENDPOINT=https://testnet.telos.net/evm
HAPI_EVM_API_ENDPOINTS=["https://telos.greymass.com","https://telos.eosphere.io","telos.caleos.io","mainnet.telosusa.io"]
HAPI_EVM_NETWORK_CHAIN_ID=4667b205c6838ef70ff7988f6e8257e8be0e1284a2f59699054a018f743b1d11
HAPI_EVM_EOS_EVM_ACCOUNT=eosio.evm
HAPI_EVM_HASURA_URL=http://hasura:8080/v1/graphql/evm
HAPI_EVM_HASURA_ADMIN_SECRET=myadminsecretkey
HAPI_BLOCK_INTERVAL_SEC=0.3

#webapp
PORT=3000
REACT_APP_VERSION=dev
Expand Down
21 changes: 21 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,27 @@ services:
HAPI_EOS_MAX_CPU_BLOCK: '${HAPI_EOS_MAX_CPU_BLOCK}'
HAPI_EOS_MAX_NET_BLOCK: '${HAPI_EOS_MAX_NET_BLOCK}'
HAPI_EOS_MISSED_BLOCKS_ENABLED: '${HAPI_EOS_MISSED_BLOCKS_ENABLED}'
hapi-evm:
container_name: '${STAGE}-${APP_NAME}-hapi-evm'
build: ./hapi-evm
ports:
- '9091:9090'
volumes:
- type: bind
source: ./hapi-evm
target: /app
working_dir: /app
command: bash -c "yarn && yarn start:dev"
environment:
HAPI_EVM_SERVER_PORT: '${HAPI_EVM_SERVER_PORT}'
HAPI_EVM_SERVER_ADDRESS: '${HAPI_EVM_SERVER_ADDRESS}'
HAPI_EVM_ENDPOINT: '${HAPI_EVM_ENDPOINT}'
HAPI_EVM_API_ENDPOINTS: '${HAPI_EVM_API_ENDPOINTS}'
HAPI_EVM_NETWORK_CHAIN_ID: '${HAPI_EVM_NETWORK_CHAIN_ID}'
HAPI_EVM_EOS_EVM_ACCOUNT: '${HAPI_EVM_EOS_EVM_ACCOUNT}'
HAPI_EVM_HASURA_URL: '${HAPI_EVM_HASURA_URL}'
HAPI_EVM_HASURA_ADMIN_SECRET: '${HAPI_EVM_HASURA_ADMIN_SECRET}'
HAPI_BLOCK_INTERVAL_SEC: '${HAPI_BLOCK_INTERVAL_SEC}'
hasura:
container_name: '${STAGE}-${APP_NAME}-hasura'
image: hasura/graphql-engine:v2.16.0.cli-migrations-v3
Expand Down
5 changes: 5 additions & 0 deletions hapi-evm/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node_modules/
.git/
.gitignore
.eslintrc
Dockerfile
2 changes: 2 additions & 0 deletions hapi-evm/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
dist
16 changes: 16 additions & 0 deletions hapi-evm/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
// "root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended"
],
"parserOptions": {
"ecmaVersion": 2020
}
}
11 changes: 11 additions & 0 deletions hapi-evm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
node_modules

yarn-debug.log*
yarn-error.log*

resources/**/*
# yarn
dist
4 changes: 4 additions & 0 deletions hapi-evm/.husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

cd hapi && yarn lint-staged
8 changes: 8 additions & 0 deletions hapi-evm/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"trailingComma": "none",
"endOfLine": "auto",
"tabWidth": 2,
"singleQuote": true,
"semi": false,
"arrowParens": "avoid"
}
16 changes: 16 additions & 0 deletions hapi-evm/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# ---------- Base ----------
FROM node:16.20.0 as base
WORKDIR /app

# ---------- Builder ----------
FROM base AS builder
COPY package.json yarn.lock ./
RUN yarn --ignore-optional
COPY ./src ./src

# ---------- Release ----------
FROM base AS release
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/src ./src
USER node
CMD ["node", "./src/index.js"]
21 changes: 21 additions & 0 deletions hapi-evm/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 EOS Costa Rica

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
42 changes: 42 additions & 0 deletions hapi-evm/makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
include ../utils/meta.mk ../utils/help.mk

LATEST_TAG ?= latest

install: ##@local Install all dependencies
install:
@npm install

clean-install: ##@local Reinstalls all dependencies
clean-install:
@rm -Rf node_modules
@npm install

run: ##@local Run the project locally (without docker)
run: node_modules
@$(SHELL_EXPORT) npm run dev

build-docker: ##@devops Build the docker image
build-docker: ./Dockerfile
@docker pull $(DOCKER_REGISTRY)/$(IMAGE_NAME_HAPI):$(VERSION) || true
@docker build \
--target release \
-t $(DOCKER_REGISTRY)/$(IMAGE_NAME_HAPI):$(VERSION) \
-t $(DOCKER_REGISTRY)/$(IMAGE_NAME_HAPI):$(LATEST_TAG) \
.

pull-image: ##@devops Pull the latest image from registry for caching
pull-image:
@docker pull $(DOCKER_REGISTRY)/$(IMAGE_NAME_HAPI):$(LATEST_TAG) || true

build-docker-cached: ##@devops Build the docker image using cached layers
build-docker-cached: ./Dockerfile
@docker build \
--target prod-stage \
--cache-from $(DOCKER_REGISTRY)/$(IMAGE_NAME_HAPI):$(LATEST_TAG) \
-t $(DOCKER_REGISTRY)/$(IMAGE_NAME_HAPI):$(VERSION) \
-t $(DOCKER_REGISTRY)/$(IMAGE_NAME_HAPI):$(LATEST_TAG) \
.

push-image: ##@devops Push the freshly built image and tag with release or latest tag
push-image:
@docker push $(DOCKER_REGISTRY)/$(IMAGE_NAME_HAPI):$(VERSION)
6 changes: 6 additions & 0 deletions hapi-evm/nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"watch": ["src"],
"ext": ".ts,.js",
"ignore": [],
"exec": "ts-node ./src/index.ts"
}
49 changes: 49 additions & 0 deletions hapi-evm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"name": "evm-backend",
"version": "1.0.0",
"description": "EVM Dashbaord Backend Service",
"main": "dist/index.js",
"author": "edenia.com",
"license": "MIT",
"scripts": {
"build": "tsc",
"start": "tsc && node dist/index.js",
"start:dev": "nodemon",
"prepare": "cd .. && husky install hapi/.husky"
},
"lint-staged": {
"*.ts": [
"eslint . --ext .ts",
"prettier --write"
]
},
"dependencies": {
"@hapi/boom": "^10.0.1",
"@hapi/hapi": "^21.3.2",
"@types/web3": "^1.2.2",
"axios": "^1.4.0",
"eosjs": "^22.1.0",
"eosjs-api": "^7.0.4",
"graphql": "16",
"graphql-request": "^6.0.0",
"joi": "^17.9.2",
"moment": "^2.29.4",
"node-fetch": "^3.3.1",
"web3": "^4.0.3",
"websocket": "^1.0.34"
},
"devDependencies": {
"@types/node": "^20.1.5",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"eslint": "^8.40.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^5.0.0",
"husky": "^8.0.3",
"lint-staged": "^13.2.2",
"nodemon": "^3.0.1",
"prettier": "^3.0.0",
"ts-node": "^10.9.1",
"typescript": "^5.0.4"
}
}
Empty file added hapi-evm/src/cache/index.ts
Empty file.
Empty file added hapi-evm/src/cache/v1/index.ts
Empty file.
8 changes: 8 additions & 0 deletions hapi-evm/src/config/hasura.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const url =
process.env.HAPI_HASURA_URL || 'http://hasura:8080/v1/graphql'
export const adminSecret =
process.env.HAPI_HASURA_ADMIN_SECRET || 'myadminsecretkey'

if (!url || !adminSecret) {
throw new Error('Missing required hasura env variables')
}
4 changes: 4 additions & 0 deletions hapi-evm/src/config/hyperion.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const api =
process.env.HAPI_HYPERION_API || 'https://test.telos.eosusa.io'
export const startAt =
process.env.HAPI_HYPERION_START_AT || '2021-06-02T00:00:00.000+00:00'
4 changes: 4 additions & 0 deletions hapi-evm/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export * as serverConfig from './server.config'
export * as hasuraConfig from './hasura.config'
export * as networkConfig from './network.config'
export * as hyperionConfig from './hyperion.config'
9 changes: 9 additions & 0 deletions hapi-evm/src/config/network.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const evmEndpoint =
process.env.HAPI_EVM_ENDPOINT || 'http://localhost/evm'
export const chainId = process.env.HAPI_NETWORK_CHAIN_ID || 'chainid1'
export const evmAccount = process.env.HAPI_EVM_EOS_EVM_ACCOUNT || 'eosio.evm'
export const eosEndpoints =
process.env.HAPI_EVM_EOS_EVM_ACCOUNT?.split(',') || []
export const blockIntervalSec = parseFloat(
process.env.HAPI_BLOCK_INTERVAL_SEC || '0.5'
)
2 changes: 2 additions & 0 deletions hapi-evm/src/config/server.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const port = process.env.HAPI_EVM_SERVER_PORT
export const host = process.env.HAPI_EVM_SERVER_ADDRESS
36 changes: 36 additions & 0 deletions hapi-evm/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import Hapi from '@hapi/hapi'
import { Server } from '@hapi/hapi'

import routes from './routes'
import { serverConfig } from './config'
import workerService from './services/worker'

const init = async () => {
const server: Server = Hapi.server({
port: serverConfig.port,
host: serverConfig.host,
routes: {
cors: { origin: ['*'] }
},
debug: { request: ['handler'] }
})

routes(server)

await server.start()

workerService.init()

console.log(`🚀 Server ready at ${server.info.uri}`)
server.table().forEach(route => console.log(`${route.method}\t${route.path}`))
}

process.on('uncaughtException', (err, origin) => {
console.log('Uncaught Exception:', err, 'Exception origin:', origin)
})

process.on('unhandledRejection', (reason, promise) => {
console.log('Unhandled Rejection:', promise, 'reason:', reason)
})

init()
2 changes: 2 additions & 0 deletions hapi-evm/src/models/block/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as interfaces from './interfaces'
export * as queries from './queries'
9 changes: 9 additions & 0 deletions hapi-evm/src/models/block/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { TransactionHash, TransactionInfo } from 'web3-types'

export interface CappedBlock {
hash: string
gas_used: number
transactions: TransactionHash[] | TransactionInfo[]
number: number
timestamp: Date
}
Loading

0 comments on commit 48edcbf

Please sign in to comment.