Skip to content

Commit

Permalink
uncommited changes and strategy operators moved to subfolder
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez committed Sep 22, 2023
1 parent da58bc9 commit f8783cc
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 10 deletions.
9 changes: 5 additions & 4 deletions api/censuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions api/strategies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions api/operators.go → api/strategyoperators/operators.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package api
package strategyoperators

import (
"context"
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 26 additions & 0 deletions db/queries/tokenTypes.sql
Original file line number Diff line number Diff line change
@@ -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 = ?;
98 changes: 98 additions & 0 deletions db/sqlc/tokenTypes.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f8783cc

Please sign in to comment.