Skip to content

Commit

Permalink
Fixed all build,lint and test errors
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarengathomas committed Jan 2, 2025
1 parent 5804d00 commit b42fbec
Show file tree
Hide file tree
Showing 39 changed files with 281 additions and 257 deletions.
1 change: 0 additions & 1 deletion .env.testnet
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ ABSINTHE_API_KEY=
ABSINTHE_EVENT_NAME=
TESTNET=
HOURS_INTERVAL=
BLOCK_EXPLORER_URL=
METRICS_FOLDER_PATH=
5 changes: 4 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY package*.json .

RUN npm ci --production

COPY . .

RUN npm install --production
RUN npm run build

CMD [ "npm", "run", "collect-metrics"]
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ The environment variables are defined in the `.env` file. The following variable
- `ABSINTHE_EVENT_NAME`: The Absinthe Event Name to send in the points requests
- `TESTNET`: Boolean value to set if the scripts should run on testnet or no
- `HOURS_INTERVAL`: Interval in hours to search for blocks/transactions that have smart contracts creation
- `BLOCK_EXPLORER_URL`: The Hemi Network's URL of the block explorer.
- `METRICS_FOLDER_PATH`: Path to the folder to store the Website metrics(`metrics.json`) file.

Example of the .env file
Expand All @@ -89,7 +88,6 @@ ABSINTHE_API_KEY=
ABSINTHE_EVENT_NAME=
TESTNET=
HOURS_INTERVAL=
BLOCK_EXPLORER_URL=
METRICS_FOLDER_PATH=
```

Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
"types": "dist/index.d.ts",
"private": true,
"scripts": {
"lint": "eslint",
"lint:fix": "eslint --fix",
"test": "vitest run",
"test:cov": "vitest run --coverage",
"test:watch": "vitest watch",
"build": "tsc -p ./tsconfig.build.json",
"collect-metrics": "node dist/presentation/cli/CollectMetrics.js",
"give-points": "node ./src/presentation/cli/GivePoints.ts"
"give-points": "node ./src/presentation/cli/GivePoints.ts",
"lint": "eslint src",
"lint:fix": "eslint src --fix",
"test": "vitest run",
"test:cov": "vitest run --coverage",
"test:watch": "vitest watch"
},
"author": "Thomas Alvarenga ([email protected])",
"author": "Thomas Alvarenga <[email protected]>",
"license": "MIT",
"dependencies": {
"hemi-viem": "1.7.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { File } from '../../domain/entities/File'
import { ChainMetricsRepository } from '../../domain/repositories/ChainMetricsRepository'
import {
ChainMetricsRepository
} from '../../domain/repositories/ChainMetricsRepository'
import { FileRepository } from '../../domain/repositories/FileRepository'
import { FileContent } from '../../domain/valueObjects/FileContent'
import { Filename } from '../../domain/valueObjects/Filename'
Expand All @@ -17,17 +19,17 @@ export class CollectChainMetricsUsecase {
}

async execute(): Promise<void> {
console.info(`Hemi Connector | Collecting chain metrics...`)
console.info('Hemi Connector | Collecting chain metrics...')

const metrics = await this.chainMetricsRepository.collect()

console.info(`Hemi Connector | Creating metrics.json file`)
console.info('Hemi Connector | Creating metrics.json file')

const file = this.createFile(metrics.toString())

await this.fileRepository.save(file)

console.info(`Hemi Connector | Metrics file created with success!`)
console.info('Hemi Connector | Metrics file created with success!')
}

private createFile(content: string): File {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Score } from '../../domain/entities/Score'
import { Transaction } from '../../domain/entities/Transaction'
import { TransactionReceipt } from '../../domain/entities/TransactionReceipt'
import { ChainRepository } from '../../domain/repositories/ChainRepository'
import { ScoreRepository } from '../../domain/repositories/ScoreRepository'
import { Address } from '../../domain/valueObjects/Address'
Expand All @@ -24,23 +25,37 @@ export class GivePointsToContractCreationUsecase {
async execute({
hours
}: GivePointsToContractCreationDto): Promise<void> {
console.info(`Hemi Connector | Searching for smart contracts created on the last ${hours} hours`)
console.info(`\
Hemi Connector | \
Searching for smart contracts created on the last ${hours} hours`
)

const blockDiff = BigInt((hours * 60 * 60) / 12) // X hours worth of Hemi blocks
const blockDiff =
BigInt((hours * 60 * 60) / 12) // X hours worth of Hemi blocks
const blockNumber = await this.chainRepository.getBlockNumber()
const toBlock = blockNumber.value
const fromBlock = toBlock - blockDiff

for (let currentBlock = fromBlock; currentBlock <= toBlock; currentBlock += BigInt(1)) {
console.info(`--------------------------------------------------------`)
console.info(`Hemi Connector | Current block number: ${currentBlock}`)
for (
let currentBlock = fromBlock;
currentBlock <= toBlock;
currentBlock += BigInt(1)
) {
console.info(`\
--------------------------------------------------------
Hemi Connector | \
Current block number: ${currentBlock}`
)

const contractCreationTxs =
await this.getContractCreationTransactions(currentBlock)

console.info(`Hemi Connector | ${contractCreationTxs.length} contract creation transactions found`)
console.info(`\
Hemi Connector | \
${contractCreationTxs.length} contract creation transactions found`
)

await this.checkTransactionsToEarnPoints(contractCreationTxs)
await this.checkTransactionsToEarnPoints(contractCreationTxs)
}
}

Expand All @@ -62,49 +77,66 @@ export class GivePointsToContractCreationUsecase {
transactions: Transaction[]
): Promise<void> {
for (const transaction of transactions) {
const receipt =
const receipt =
await this.chainRepository.getTransactionReceipt(transaction.hash)

if (receipt && receipt.contractAddress) {
const isHelloWorldContract =
await this.validateHelloWorldContract(receipt.contractAddress)

if (isHelloWorldContract) {
console.info(`Hemi Connector | ${transaction.from.value} created a Hello World smart contract`)
await this.givePointsToTransaction(transaction)
}
const isHelloWorldContract = await this.isHelloWorldContract(receipt)

if (isHelloWorldContract) {
console.info(`\
Hemi Connector | \
${transaction.from.value} created a Hello World smart contract`
)
await this.givePointsToTransaction(transaction)
}
}
}

private async validateHelloWorldContract(contractAddress: Address): Promise<boolean> {
private async isHelloWorldContract(
receipt: TransactionReceipt | null
): Promise<boolean> {
if (!receipt?.contractAddress) {
return false
}

return await this.validateHelloWorldContract(receipt.contractAddress)
}

private async validateHelloWorldContract(
contractAddress: Address
): Promise<boolean> {
try {
await this.chainRepository.callContractMethod(
contractAddress,
'getGreeting',
HelloWorldAbi
)
return true
}
catch (error) {
} catch (error) {
return false
}
}

private async givePointsToTransaction(transaction: Transaction): Promise<void> {
private async givePointsToTransaction(
transaction: Transaction
): Promise<void> {
const amount = 500

try {
const score = Score.create({
address: transaction.from,
amount
}, transaction.hash)

await this.scoreRepository.givePoints(score)
console.info(`Hemi Connector | ${transaction.from.value} received ${amount} points on Absinthe`)
}
catch (error) {
console.error(`Hemi Connector | Error giving points to ${transaction.from.value}: ${error}`)
console.info(`\
Hemi Connector | \
${transaction.from.value} received ${amount} points on Absinthe`
)
} catch (error) {
console.error(`\
Hemi Connector | \
Error giving points to ${transaction.from.value}`, error
)
}
}
}
80 changes: 40 additions & 40 deletions src/application/abi/HelloWorldAbi.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
export const HelloWorldAbi = [
{
"inputs": [],
"name": "getGreeting",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "greeting",
"outputs": [
{
"internalType": "string",
"name": "",
"type": "string"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "string",
"name": "_greeting",
"type": "string"
}
],
"name": "setGreeting",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
];
{
inputs: [],
name: 'getGreeting',
outputs: [
{
internalType: 'string',
name: '',
type: 'string'
}
],
stateMutability: 'view',
type: 'function'
},
{
inputs: [],
name: 'greeting',
outputs: [
{
internalType: 'string',
name: '',
type: 'string'
}
],
stateMutability: 'view',
type: 'function'
},
{
inputs: [
{
internalType: 'string',
name: '_greeting',
type: 'string'
}
],
name: 'setGreeting',
outputs: [],
stateMutability: 'nonpayable',
type: 'function'
}
]
24 changes: 10 additions & 14 deletions src/domain/base/DomainError.spec.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
import { describe, it, expect } from "vitest"
import { DomainError } from "./DomainError"
import { describe, it, expect } from 'vitest'
import { DomainError } from './DomainError'

describe("src/domain/DomainError", () => {
it("should be defined", () => {
expect(DomainError).toBeDefined()
describe('src/domain/DomainError', () => {
it('should be instance of Error', () => {
expect(new DomainError('TEST_ERROR')).toBeInstanceOf(Error)
})

it("should be instance of Error", () => {
expect(new DomainError("TEST_ERROR")).toBeInstanceOf(Error)
})

describe("constructor", () => {
it("should accept a code parameter", () => {
const errorCode = "TEST_ERROR"
describe('constructor', () => {
it('should accept a code parameter', () => {
const errorCode = 'TEST_ERROR'
const domainError = new DomainError(errorCode)

expect(domainError.toObject().code).toBe(errorCode)
})

it("should accept a exposable parameter", () => {
it('should accept a exposable parameter', () => {
const exposable = true
const domainError = new DomainError("TEST_ERROR", exposable)
const domainError = new DomainError('TEST_ERROR', exposable)

expect(domainError.toObject().exposable).toBe(exposable)
})
Expand Down
Loading

0 comments on commit b42fbec

Please sign in to comment.