Skip to content

Commit

Permalink
reflect feedback: adjust for naming convention
Browse files Browse the repository at this point in the history
  • Loading branch information
Intizar-T committed May 24, 2024
1 parent 00ce0c6 commit 558d9f1
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 23 deletions.
10 changes: 5 additions & 5 deletions api/listener/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type ListenerInsertModel struct {
Chain string `db:"chain_name" json:"chain" validate:"required"`
}

type ListenerObservedBlockModel struct {
type ObservedBlockModel struct {
BlockKey string `db:"block_key" json:"blockKey" validate:"required"`
BlockNumber int64 `db:"block_number" json:"blockNumber" validate:"blockNumberValidator"`
}
Expand Down Expand Up @@ -158,7 +158,7 @@ func deleteById(c *fiber.Ctx) error {
}

func upsertObservedBlock(c *fiber.Ctx) error {
payload := new(ListenerObservedBlockModel)
payload := new(ObservedBlockModel)
if err := c.BodyParser(payload); err != nil {
return err
}
Expand All @@ -171,16 +171,16 @@ func upsertObservedBlock(c *fiber.Ctx) error {
return err
}

result, err := utils.QueryRow[ListenerObservedBlockModel](c, UpsertObservedBlock, map[string]any{"block_key": payload.BlockKey, "block_number": payload.BlockNumber})
result, err := utils.QueryRow[ObservedBlockModel](c, UpsertObservedBlock, map[string]any{"block_key": payload.BlockKey, "block_number": payload.BlockNumber})
if err != nil {
return err
}
return c.JSON(result)
}

func getObservedBlock(c *fiber.Ctx) error {
block_key := c.Query("blockKey")
result, err := utils.QueryRow[ListenerObservedBlockModel](c, GetObservedBlock, map[string]any{"block_key": block_key})
blockKey := c.Query("blockKey")
result, err := utils.QueryRow[ObservedBlockModel](c, GetObservedBlock, map[string]any{"block_key": blockKey})
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions api/listener/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ const (
`

GetObservedBlock = `
SELECT * FROM observed_block
SELECT * FROM observed_blocks
WHERE block_key = @block_key
LIMIT 1;
`

UpsertObservedBlock = `
INSERT INTO observed_block (block_key, block_number)
INSERT INTO observed_blocks (block_key, block_number)
VALUES (@block_key, @block_number)
ON CONFLICT (block_key)
DO UPDATE SET block_number = @block_number
Expand Down
2 changes: 1 addition & 1 deletion api/migrations/000002_add_observed_block.down.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DROP TABLE IF EXISTS "observed_block";
DROP TABLE IF EXISTS "observed_blocks";
4 changes: 2 additions & 2 deletions api/migrations/000002_add_observed_block.up.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE IF NOT EXISTS "observed_block" (
CREATE TABLE IF NOT EXISTS "observed_blocks" (
block_key TEXT NOT NULL,
block_number BIGINT NOT NULL,
CONSTRAINT "observed_block_key" UNIQUE ("block_key")
CONSTRAINT "observed_blocks_key" UNIQUE ("block_key")
)
3 changes: 2 additions & 1 deletion core/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,6 @@ export enum OraklErrorCode {
AxiosNotSupported,
AxiosInvalidUrl,
FailedToConnectAPI,
UpsertListenerObservedBlockFailed
UpsertObservedBlockFailed,
GetObservedBlockFailed
}
39 changes: 32 additions & 7 deletions core/src/listener/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import axios from 'axios'
import { Logger } from 'pino'
import { OraklError, OraklErrorCode } from '../errors'
import { ORAKL_NETWORK_API_URL } from '../settings'
import { IListenerObservedBlock, IListenerRawConfig } from '../types'
import { IListenerRawConfig } from '../types'
import { buildUrl } from '../utils'
import { IObservedBlock } from './types'

const FILE_NAME = import.meta.url

Expand Down Expand Up @@ -59,29 +60,53 @@ export async function getListener({
}
}

/**
* Get observed block number from the Orakl Network API for a given contract address
*
* @param {string} blockKey
* @param {pino.Logger} logger
* @return {Promise<IObservedBlock>}
* @exception {OraklErrorCode.GetObservedBlockFailed}
*/
export async function getObservedBlock({
blockKey,
logger
}: {
blockKey: string
logger?: Logger
}): Promise<IObservedBlock> {
try {
const endpoint = buildUrl(ORAKL_NETWORK_API_URL, `listener/observed-block?blockKey=${blockKey}`)
return (await axios.get(endpoint))?.data
} catch (e) {
logger?.error({ name: 'getObservedBlock', file: FILE_NAME, ...e }, 'error')
throw new OraklError(OraklErrorCode.GetObservedBlockFailed)
}
}

/**
* Upsert listener observed block number to the Orakl Network API for a given contract address
*
* @param {string} blockKey
* @param {number} blockValue
* @param {pino.Logger} logger
* @return {Promise<IListenerObservedBlock>}
* @exception {UpsertListenerObservedBlockFailed}
* @return {Promise<IObservedBlock>}
* @exception {OraklErrorCode.UpsertObservedBlockFailed}
*/
export async function upsertListenerObservedBlock({
export async function upsertObservedBlock({
blockKey,
blockValue,
logger
}: {
blockKey: string
blockValue: number
logger?: Logger
}): Promise<IListenerObservedBlock> {
}): Promise<IObservedBlock> {
try {
const endpoint = buildUrl(ORAKL_NETWORK_API_URL, 'listener/observed-block')
return (await axios.post(endpoint, { blockKey, blockValue }))?.data
} catch (e) {
logger?.error({ name: 'upsertListenerObservedBlock', file: FILE_NAME, ...e }, 'error')
throw new OraklError(OraklErrorCode.UpsertListenerObservedBlockFailed)
logger?.error({ name: 'upsertObservedBlock', file: FILE_NAME, ...e }, 'error')
throw new OraklError(OraklErrorCode.UpsertObservedBlockFailed)
}
}
5 changes: 5 additions & 0 deletions core/src/listener/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,8 @@ export interface IProcessEventListenerJob {
export interface IContracts {
[key: string]: ethers.Contract
}

export interface IObservedBlock {
blockKey: string
blockValue: number
}
5 changes: 0 additions & 5 deletions core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,11 +317,6 @@ export interface IListenerGroupConfig {
[key: string]: IListenerConfig[]
}

export interface IListenerObservedBlock {
blockKey: string
blockValue: number
}

// Reporter
export interface IReporterConfig {
id: string
Expand Down

0 comments on commit 558d9f1

Please sign in to comment.