-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
100 changed files
with
17,815 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 renamed
BIN
+2.05 MB
audit/20240516_Certik_Audit_Report.pdf → audit/20240528_Certik_Audit_Report.pdf
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0x22740d5af8ee07a8cfc717d076e6f83b08553623" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0xa827234224826f3e1f5fd235ca99d845113e4ae3" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0x48802c3ab12a7f8859e1d9bbd6fa7b0b23b7a9b7" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0x36801b1601293b5ea6aeb377b23291dcee368c40" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0x2fd4255c0a84623fcd78cceb94b81c18b74c1f79" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0x60893bbd0fea7d6dbd94a85a4f1897ad68d93394" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0x1c8754c75ce2affcfcbe10ab6463bf2cc8d01506" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0x7104dc056e45efcfbdff9e24d2aae76fa4dab6a3" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0xd24e57982b62e13834d504817c15c92977e13fa1" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0xd4ce8ccd0da36e2ad29f94dda28eee8eb6cfaf22" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0xbf61f1f8d45ecb33006a335e7c76f306689dcaab" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0x446dae2fd6dee8f091a97965cc5b24b4f4647fa5" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0xea2a00df67cb3ba0f2b157318ae37c41f1bc4722" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0x6a1f0e6bed3cf7abe8d08eafc04ab5db72a75a11" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0xaa4adf8428e689c603f4cb0c9073f28f72aff572" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0x9eed0595681448866aad64a6de1490c01faa1b6a" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0x6a08d36e8c10d5d89529c7443cebf37ea2cd01d4" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0xc90431adfa8ee2bfeb97d03800a6e6f765ab117b" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0xc14904bfe59fb6e2d462b9a972aef253ea8549cd" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0x16047900cf569e63441f0be392ada15ccd9e8b15" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0x713fc15ad95457f6ae936ce10426914ea6ca31b5" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0x4d700eab8d99614370108886fe38f5f875b036c9" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0x669b00079f1e451d62a2f35bf0f2b377d7f5bde5" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0x357405f2d669b13c43a6887e9a34288d1bf2131e" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0x4028ecf422b5a6ddb75e68fc98b079bc41e63630" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0x229da441c546a17ab4b9c15ef18f87d6870b9b2b" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0xa1d63586497f6de047df2ba4ce63718d07e9c7df" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"address": "0x4090a4ea2f626463b71afefdbd5d602df0fb43cd" | ||
} |
Oops, something went wrong.