Skip to content

Commit

Permalink
[GO-53] Allow to invalidate token metadata via API (#36)
Browse files Browse the repository at this point in the history
* [GO-53] Allow to invalidate token metadata via API

* Lint
  • Loading branch information
m-kus authored Nov 17, 2022
1 parent c61e03d commit 1519527
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 1 deletion.
1 change: 1 addition & 0 deletions build/metadata/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ COPY ./cmd/metadata/mappings ./mappings
COPY ./cmd/metadata/graphql ./graphql
COPY ./build/*.yml ./
COPY ./cmd/metadata/views/*.sql ./views/
COPY ./cmd/metadata/sql/*.sql ./sql/
COPY ./cmd/metadata/custom_hasura_config ./custom_hasura_config

ENTRYPOINT ["/go/bin/dipdup-metadata"]
21 changes: 21 additions & 0 deletions cmd/metadata/custom_hasura_config/0_expired_field.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type": "bulk",
"source": "default",
"resource_version": 195,
"args": [
{
"type": "pg_add_computed_field",
"args": {
"table": { "schema": "public", "name": "token_metadata" },
"name": "expired",
"definition": {
"function": { "name": "token_metadata_expired", "schema": "public" },
"table_argument": null,
"session_argument": null
},
"comment": null,
"source": "default"
}
}
]
}
58 changes: 58 additions & 0 deletions cmd/metadata/custom_hasura_config/1_partner_role.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"type": "bulk",
"source": "default",
"resource_version": 195,
"args": [
{
"type": "pg_create_select_permission",
"args": {
"table": { "name": "token_metadata", "schema": "public" },
"role": "partner",
"permission": {
"columns": [
"id",
"created_at",
"updated_at",
"update_id",
"token_id",
"network",
"contract",
"link",
"metadata",
"retry_count",
"status",
"image_processed",
"error"
],
"computed_fields": ["expired"],
"backend_only": false,
"filter": {},
"limit": 100,
"allow_aggregations": false
},
"source": "default"
}
},
{
"type": "pg_create_update_permission",
"args": {
"table": { "name": "token_metadata", "schema": "public" },
"role": "partner",
"permission": {
"columns": ["retry_count", "status"],
"filter": {
"_and": [
{ "contract": { "_eq": "X-Hasura-User-Id" } },
{ "status": { "_neq": 1 } },
{ "expired": { "_eq": "true" } }
]
},
"backend_only": false,
"set": {}
},
"source": "default"
}
}
]
}

30 changes: 30 additions & 0 deletions cmd/metadata/custom_hasura_config/2_invalidate_endpoint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"type": "bulk",
"source": "default",
"resource_version": 195,
"args": [
{
"type": "add_query_to_collection",
"args": {
"collection_name": "allowed-queries",
"query_name": "Invalidate token metadata",
"query": "mutation Invalidate($contract: String) {\n update_token_metadata(\n where: {\n contract: {_eq: $contract}, \n status: {_neq: \"1\"}, \n expired: {_eq: true}\n }, \n _set: {\n status: \"1\", \n retry_count: \"0\"\n }) {\n \taffected_rows\n }\n}"
}
},
{
"type": "create_rest_endpoint",
"args": {
"name": "Invalidate token metadata",
"url": "invalidate_token_metadata",
"definition": {
"query": {
"query_name": "Invalidate token metadata",
"collection_name": "allowed-queries"
}
},
"methods": ["PUT", "PATCH"]
}
}
]
}

36 changes: 36 additions & 0 deletions cmd/metadata/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func main() {
prometheusService.Start()
}

if err := execScripts(ctx, cfg.Database); err != nil {
log.Err(err).Msg("execScripts")
return
}

views, err := createViews(ctx, cfg.Database)
if err != nil {
log.Err(err).Msg("createViews")
Expand Down Expand Up @@ -210,3 +215,34 @@ func createViews(ctx context.Context, database golibConfig.Database) ([]string,

return views, nil
}

func execScripts(ctx context.Context, database golibConfig.Database) error {
files, err := os.ReadDir("sql")
if err != nil {
return err
}

db, err := models.NewDatabase(ctx, database)
if err != nil {
return err
}
defer db.Close()

for i := range files {
if files[i].IsDir() {
continue
}

path := fmt.Sprintf("sql/%s", files[i].Name())
raw, err := os.ReadFile(path)
if err != nil {
return err
}

if err := db.Exec(string(raw)); err != nil {
return err
}
}

return nil
}
11 changes: 11 additions & 0 deletions cmd/metadata/sql/token_metadata_expired.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE OR REPLACE FUNCTION public.token_metadata_expired(IN p_item token_metadata)
RETURNS boolean
LANGUAGE 'plpgsql' STABLE
PARALLEL SAFE
COST 100

AS $BODY$
begin
return to_timestamp(p_item.updated_at + 7200) < now();
end;
$BODY$;
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ services:
environment:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-changeme}
- ADMIN_SECRET=${ADMIN_SECRET:-changeme}
- IPFS_NODE_URI=${IPFS_NODE_URI:-https://ipfs.io}
volumes:
- ipfs:/etc/metadata/ipfs

Expand All @@ -33,7 +34,7 @@ services:
retries: 5

hasura:
image: hasura/graphql-engine:v2.0.1
image: hasura/graphql-engine:v2.0.8
ports:
- 127.0.0.1:8080:8080
restart: always
Expand Down

0 comments on commit 1519527

Please sign in to comment.