Skip to content

Commit

Permalink
pull origin main
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-bisonai committed May 29, 2024
2 parents 97db737 + 29ebbe0 commit 9b8f0d3
Show file tree
Hide file tree
Showing 100 changed files with 17,815 additions and 193 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/api.test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ jobs:
check-latest: true
cache-dependency-path: |
./api/go.sum
- name: Run lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54
working-directory: api
skip-pkg-cache: true
skip-build-cache: true
args: --timeout=10m
- name: Run Vet
run: |
cd ./api
go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest
go vet
go vet -vettool=$(which shadow)
- name: Install golang-migrate
run: |
curl -L https://github.com/golang-migrate/migrate/releases/download/v4.17.0/migrate.linux-amd64.tar.gz | tar xvz
Expand Down
14 changes: 14 additions & 0 deletions .github/workflows/delegator.test+build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ jobs:
check-latest: true
cache-dependency-path: |
./delegator/go.sum
- name: Run lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.54
working-directory: delegator
skip-pkg-cache: true
skip-build-cache: true
args: --timeout=10m --skip-dirs=tests
- name: Run vet
run: |
cd ./delegator
go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow@latest
go vet
go vet -vettool=$(which shadow)
- name: Install golang-migrate
run: |
curl -L https://github.com/golang-migrate/migrate/releases/download/v4.17.0/migrate.linux-amd64.tar.gz | tar xvz
Expand Down
117 changes: 117 additions & 0 deletions api/blocks/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package blocks

import (
"bisonai.com/orakl/api/utils"
"github.com/go-playground/validator/v10"
"github.com/gofiber/fiber/v2"
)

type BlockModel struct {
Service string `db:"service" json:"service" validate:"required"`
BlockNumber int64 `db:"block_number" json:"blockNumber" validate:"isZeroOrPositive"`
}

type BlocksModel struct {
Service string `db:"service" json:"service" validate:"required"`
Blocks []int64 `db:"blocks" json:"blocks" validate:"dive,isZeroOrPositive"`
}

var validate *validator.Validate

func init() {
validate = validator.New()
_ = validate.RegisterValidation("isZeroOrPositive", func(fl validator.FieldLevel) bool {
return fl.Field().Int() >= 0
})
}

func validateBlockPayload(payload interface{}) error {
return validate.Struct(payload)
}

func getObservedBlock(c *fiber.Ctx) error {
service := c.Query("service")
if service == "" {
return fiber.NewError(fiber.StatusBadRequest, "service is required")
}
result, err := utils.QueryRow[BlockModel](c, GetObservedBlock, map[string]any{
"service": service,
})
if err != nil {
return err
}
if result.Service == "" {
return c.JSON(nil)
}

return c.JSON(result)
}

func upsertObservedBlock(c *fiber.Ctx) error {
payload := new(BlockModel)
if err := c.BodyParser(payload); err != nil {
return err
}

if err := validateBlockPayload(payload); err != nil {
return err
}

result, err := utils.QueryRow[BlockModel](c, UpsertObservedBlock, map[string]any{
"service": payload.Service,
"block_number": payload.BlockNumber,
})
if err != nil {
return err
}

return c.JSON(result)
}

func getUnprocessedBlocks(c *fiber.Ctx) error {
service := c.Query("service")
if service == "" {
return fiber.NewError(fiber.StatusBadRequest, "service is required")
}
result, err := utils.QueryRows[BlockModel](c, GetUnprocessedBlocks, map[string]any{
"service": service,
})
if err != nil {
return err
}

return c.JSON(result)
}

func insertUnprocessedBlocks(c *fiber.Ctx) error {
payload := new(BlocksModel)
if err := c.BodyParser(payload); err != nil {
return err
}

if err := validateBlockPayload(payload); err != nil {
return err
}

result, err := utils.QueryRows[BlocksModel](c, GenerateInsertBlocksQuery(payload.Blocks, payload.Service), map[string]any{})
if err != nil {
return err
}

return c.JSON(result)
}

func deleteUnprocessedBlock(c *fiber.Ctx) error {
service := c.Params("service")
blockNumber := c.Params("blockNumber")

result, err := utils.QueryRow[BlockModel](c, DeleteUnprocessedBlock, map[string]any{
"service": service,
"block_number": blockNumber,
})
if err != nil {
return err
}

return c.JSON(result)
}
51 changes: 51 additions & 0 deletions api/blocks/queries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package blocks

import (
"fmt"
"strings"
)

const (
// get observedBlock given service
GetObservedBlock = `
SELECT * FROM observed_blocks
WHERE service = @service;
`

// upsert to observed_blocks given service and block_number
UpsertObservedBlock = `
INSERT INTO observed_blocks (service, block_number)
VALUES (@service, @block_number)
ON CONFLICT (service) DO UPDATE SET block_number = GREATEST(observed_blocks.block_number, EXCLUDED.block_number)
RETURNING *;
`

// get all unprocessed blocks given service
GetUnprocessedBlocks = `
SELECT * FROM unprocessed_blocks
WHERE service = @service;
`

// delete unprocessed block given service and block_number
DeleteUnprocessedBlock = `
DELETE FROM unprocessed_blocks
WHERE service = @service AND block_number = @block_number
RETURNING *;
`
)

func GenerateInsertBlocksQuery(blocks []int64, service string) string {
baseQuery := `
INSERT INTO unprocessed_blocks (service, block_number)
VALUES
`
onConflict := `
ON CONFLICT (service, block_number) DO NOTHING;
`
values := make([]string, 0, len(blocks))
for _, block := range blocks {
values = append(values, fmt.Sprintf("('%s', %d)", service, block))
}

return baseQuery + strings.Join(values, ",") + onConflict
}
15 changes: 15 additions & 0 deletions api/blocks/route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package blocks

import (
"github.com/gofiber/fiber/v2"
)

func Routes(router fiber.Router) {
blocks := router.Group("/blocks")

blocks.Get("/observed", getObservedBlock)
blocks.Post("/observed", upsertObservedBlock)
blocks.Post("/unprocessed", insertUnprocessedBlocks)
blocks.Get("/unprocessed", getUnprocessedBlocks)
blocks.Delete("/unprocessed/:service/:blockNumber", deleteUnprocessedBlock)
}
2 changes: 2 additions & 0 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bisonai.com/orakl/api/aggregate"
"bisonai.com/orakl/api/aggregator"
"bisonai.com/orakl/api/apierr"
"bisonai.com/orakl/api/blocks"
"bisonai.com/orakl/api/chain"
"bisonai.com/orakl/api/data"
"bisonai.com/orakl/api/feed"
Expand Down Expand Up @@ -80,4 +81,5 @@ func SetRouter(_router fiber.Router) {
reporter.Routes(_router)
service.Routes(_router)
vrf.Routes(_router)
blocks.Routes(_router)
}
2 changes: 2 additions & 0 deletions api/migrations/000002_blocks.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE IF EXISTS "observed_blocks";
DROP TABLE IF EXISTS "unprocessed_blocks";
10 changes: 10 additions & 0 deletions api/migrations/000002_blocks.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE IF NOT EXISTS "observed_blocks" (
service TEXT NOT NULL UNIQUE,
block_number BIGINT NOT NULL
);

CREATE TABLE IF NOT EXISTS "unprocessed_blocks" (
service TEXT NOT NULL,
block_number BIGINT NOT NULL,
UNIQUE (service, block_number)
);
Binary file not shown.
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_ADA-USDT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0x22740d5af8ee07a8cfc717d076e6f83b08553623"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_ATOM-USDT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0xa827234224826f3e1f5fd235ca99d845113e4ae3"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_AVAX-USDT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0x48802c3ab12a7f8859e1d9bbd6fa7b0b23b7a9b7"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_BNB-USDT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0x36801b1601293b5ea6aeb377b23291dcee368c40"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_BORA-KRW.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0x2fd4255c0a84623fcd78cceb94b81c18b74c1f79"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_BTC-USDT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0x60893bbd0fea7d6dbd94a85a4f1897ad68d93394"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_CHF-USD.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0x1c8754c75ce2affcfcbe10ab6463bf2cc8d01506"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_DAI-USDT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0x7104dc056e45efcfbdff9e24d2aae76fa4dab6a3"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_DOGE-USDT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0xd24e57982b62e13834d504817c15c92977e13fa1"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_DOT-USDT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0xd4ce8ccd0da36e2ad29f94dda28eee8eb6cfaf22"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_ETH-USDT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0xbf61f1f8d45ecb33006a335e7c76f306689dcaab"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_EUR-USD.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0x446dae2fd6dee8f091a97965cc5b24b4f4647fa5"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_FTM-USDT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0xea2a00df67cb3ba0f2b157318ae37c41f1bc4722"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_GBP-USD.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0x6a1f0e6bed3cf7abe8d08eafc04ab5db72a75a11"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_JOY-USDT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0xaa4adf8428e689c603f4cb0c9073f28f72aff572"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_JPY-USD.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0x9eed0595681448866aad64a6de1490c01faa1b6a"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_KLAY-USDT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0x6a08d36e8c10d5d89529c7443cebf37ea2cd01d4"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_KRW-USD.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0xc90431adfa8ee2bfeb97d03800a6e6f765ab117b"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_KSP-KRW.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0xc14904bfe59fb6e2d462b9a972aef253ea8549cd"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_LTC-USDT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0x16047900cf569e63441f0be392ada15ccd9e8b15"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_MATIC-USDT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0x713fc15ad95457f6ae936ce10426914ea6ca31b5"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_MBX-KRW.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0x4d700eab8d99614370108886fe38f5f875b036c9"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_MNR-KRW.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0x669b00079f1e451d62a2f35bf0f2b377d7f5bde5"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_PAXG-USDT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0x357405f2d669b13c43a6887e9a34288d1bf2131e"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_PER-KLAY.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0x4028ecf422b5a6ddb75e68fc98b079bc41e63630"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_SHIB-USDT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0x229da441c546a17ab4b9c15ef18f87d6870b9b2b"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_SOL-USDT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0xa1d63586497f6de047df2ba4ce63718d07e9c7df"
}
3 changes: 3 additions & 0 deletions contracts/v0.2/addresses/cypress/FeedProxy_TRX-USDT.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"address": "0x4090a4ea2f626463b71afefdbd5d602df0fb43cd"
}
Loading

0 comments on commit 9b8f0d3

Please sign in to comment.