From f8783ccd191ea5c8da2ec916c20fdbbd3b9a7c3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Men=C3=A9ndez?= Date: Fri, 22 Sep 2023 13:23:17 +0200 Subject: [PATCH] uncommited changes and strategy operators moved to subfolder --- api/censuses.go | 9 ++- api/strategies.go | 5 +- api/{ => strategyoperators}/operators.go | 14 +++- db/queries/tokenTypes.sql | 26 +++++++ db/sqlc/tokenTypes.sql.go | 98 ++++++++++++++++++++++++ 5 files changed, 142 insertions(+), 10 deletions(-) rename api/{ => strategyoperators}/operators.go (96%) create mode 100644 db/queries/tokenTypes.sql create mode 100644 db/sqlc/tokenTypes.sql.go diff --git a/api/censuses.go b/api/censuses.go index b8d07afd..7b49c9b3 100644 --- a/api/censuses.go +++ b/api/censuses.go @@ -9,6 +9,7 @@ import ( "strconv" "github.com/ethereum/go-ethereum/common" + "github.com/vocdoni/census3/api/strategyoperators" "github.com/vocdoni/census3/census" queries "github.com/vocdoni/census3/db/sqlc" "github.com/vocdoni/census3/lexer" @@ -139,7 +140,7 @@ func (capi *census3API) createAndPublishCensus(req *CreateCensusRequest, qID str censusWeight := new(big.Int) strategyHolders := map[common.Address]*big.Int{} // parse the predicate - lx := lexer.NewLexer(ValidOperatorsTags) + lx := lexer.NewLexer(strategyoperators.ValidOperatorsTags) validPredicate, err := lx.Parse(strategy.Predicate) if err != nil { return 0, ErrInvalidStrategyPredicate.WithErr(err) @@ -174,16 +175,16 @@ func (capi *census3API) createAndPublishCensus(req *CreateCensusRequest, qID str return 0, ErrCantCreateCensus.WithErr(err) } // parse token information - tokensInfo := map[string]*StrategyToken{} + tokensInfo := map[string]*strategyoperators.TokenInformation{} for _, token := range strategyTokens { - tokensInfo[token.Symbol.String] = &StrategyToken{ + tokensInfo[token.Symbol.String] = &strategyoperators.TokenInformation{ ID: common.BytesToAddress(token.ID).String(), ChainID: token.ChainID, MinBalance: new(big.Int).SetBytes(token.MinBalance).String(), } } // init the operators and the predicate evaluator - operators := InitOperators(capi.db.QueriesRO, tokensInfo) + operators := strategyoperators.InitOperators(capi.db.QueriesRO, tokensInfo) eval := lexer.NewEval[[]string](operators.Map()) // execute the evaluation of the predicate res, err := eval.EvalToken(validPredicate) diff --git a/api/strategies.go b/api/strategies.go index e9d679c5..717e9457 100644 --- a/api/strategies.go +++ b/api/strategies.go @@ -9,6 +9,7 @@ import ( "strconv" "github.com/ethereum/go-ethereum/common" + "github.com/vocdoni/census3/api/strategyoperators" queries "github.com/vocdoni/census3/db/sqlc" "github.com/vocdoni/census3/lexer" "go.vocdoni.io/dvote/httprouter" @@ -104,7 +105,7 @@ func (capi *census3API) createStrategy(msg *api.APIdata, ctx *httprouter.HTTPCon return ErrMalformedStrategy.With("no predicate or alias provided") } // check predicate - lx := lexer.NewLexer(ValidOperatorsTags) + lx := lexer.NewLexer(strategyoperators.ValidOperatorsTags) validatedPredicate, err := lx.Parse(req.Predicate) if err != nil { return ErrInvalidStrategyPredicate.WithErr(err) @@ -293,7 +294,7 @@ func (capi *census3API) validateStrategyPredicate(msg *api.APIdata, ctx *httprou return ErrMalformedStrategy.With("no predicate provided") } - lx := lexer.NewLexer(ValidOperatorsTags) + lx := lexer.NewLexer(strategyoperators.ValidOperatorsTags) resultingToken, err := lx.Parse(req.Predicate) if err != nil { return ErrInvalidStrategyPredicate.WithErr(err) diff --git a/api/operators.go b/api/strategyoperators/operators.go similarity index 96% rename from api/operators.go rename to api/strategyoperators/operators.go index 1b3d0c88..e875131d 100644 --- a/api/operators.go +++ b/api/strategyoperators/operators.go @@ -1,4 +1,4 @@ -package api +package strategyoperators import ( "context" @@ -21,17 +21,23 @@ const ( // ValidOperatorsTags variable contains the supported operator tags var ValidOperatorsTags = []string{ANDTag, ORTag} +type TokenInformation struct { + ID string + ChainID uint64 + MinBalance string +} + // StrategyOperators struct represents a custom set of predicate operators // associated with a SQL database as data source. It brings access to SQL data // inside the lexer evaluator operators. type StrategyOperators struct { db *queries.Queries - tokensInfo map[string]*StrategyToken + tokensInfo map[string]*TokenInformation } // InitOperators function creates a new StrategyOperators struct with the db // instance and info about tokens provided. -func InitOperators(db *queries.Queries, info map[string]*StrategyToken) *StrategyOperators { +func InitOperators(db *queries.Queries, info map[string]*TokenInformation) *StrategyOperators { return &StrategyOperators{ db: db, tokensInfo: info, @@ -250,7 +256,7 @@ func (op *StrategyOperators) OR(iter *lexer.Iteration[[]string]) ([]string, erro dataB = append(dataB, common.BytesToAddress(r).String()) } } - // when both data sources are filled, do the intersection of both lists. + // when both data sources are filled, do the union of both lists. res := append([]string{}, dataA...) for _, addressA := range dataA { for _, addressB := range dataB { diff --git a/db/queries/tokenTypes.sql b/db/queries/tokenTypes.sql new file mode 100644 index 00000000..ef65ec88 --- /dev/null +++ b/db/queries/tokenTypes.sql @@ -0,0 +1,26 @@ +-- name: ListTokenTypes :many +SELECT * FROM token_types +ORDER BY id; + +-- name: TokenTypeByID :one +SELECT * FROM token_types +WHERE id = ? +LIMIT 1; + +-- name: TokenTypeByName :one +SELECT * FROM token_types +WHERE type_name = ? +LIMIT 1; + +-- name: CreateTokenType :execresult +INSERT INTO token_types (type_name) +VALUES (?); + +-- name: UpdateTokenType :execresult +UPDATE token_types +SET type_name = sqlc.arg(type_name) +WHERE id = sqlc.arg(id); + +-- name: DeleteTokenType :execresult +DELETE FROM token_types +WHERE id = ?; \ No newline at end of file diff --git a/db/sqlc/tokenTypes.sql.go b/db/sqlc/tokenTypes.sql.go new file mode 100644 index 00000000..7e4df009 --- /dev/null +++ b/db/sqlc/tokenTypes.sql.go @@ -0,0 +1,98 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.20.0 +// source: tokenTypes.sql + +package queries + +import ( + "context" + "database/sql" +) + +const createTokenType = `-- name: CreateTokenType :execresult +INSERT INTO token_types (type_name) +VALUES (?) +` + +func (q *Queries) CreateTokenType(ctx context.Context, typeName string) (sql.Result, error) { + return q.db.ExecContext(ctx, createTokenType, typeName) +} + +const deleteTokenType = `-- name: DeleteTokenType :execresult +DELETE FROM token_types +WHERE id = ? +` + +func (q *Queries) DeleteTokenType(ctx context.Context, id uint64) (sql.Result, error) { + return q.db.ExecContext(ctx, deleteTokenType, id) +} + +const listTokenTypes = `-- name: ListTokenTypes :many +SELECT id, type_name FROM token_types +ORDER BY id +` + +func (q *Queries) ListTokenTypes(ctx context.Context) ([]TokenType, error) { + rows, err := q.db.QueryContext(ctx, listTokenTypes) + if err != nil { + return nil, err + } + defer rows.Close() + var items []TokenType + for rows.Next() { + var i TokenType + if err := rows.Scan(&i.ID, &i.TypeName); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const tokenTypeByID = `-- name: TokenTypeByID :one +SELECT id, type_name FROM token_types +WHERE id = ? +LIMIT 1 +` + +func (q *Queries) TokenTypeByID(ctx context.Context, id uint64) (TokenType, error) { + row := q.db.QueryRowContext(ctx, tokenTypeByID, id) + var i TokenType + err := row.Scan(&i.ID, &i.TypeName) + return i, err +} + +const tokenTypeByName = `-- name: TokenTypeByName :one +SELECT id, type_name FROM token_types +WHERE type_name = ? +LIMIT 1 +` + +func (q *Queries) TokenTypeByName(ctx context.Context, typeName string) (TokenType, error) { + row := q.db.QueryRowContext(ctx, tokenTypeByName, typeName) + var i TokenType + err := row.Scan(&i.ID, &i.TypeName) + return i, err +} + +const updateTokenType = `-- name: UpdateTokenType :execresult +UPDATE token_types +SET type_name = ? +WHERE id = ? +` + +type UpdateTokenTypeParams struct { + TypeName string + ID uint64 +} + +func (q *Queries) UpdateTokenType(ctx context.Context, arg UpdateTokenTypeParams) (sql.Result, error) { + return q.db.ExecContext(ctx, updateTokenType, arg.TypeName, arg.ID) +}