diff --git a/api/censuses.go b/api/censuses.go index ab316127..fef09d2c 100644 --- a/api/censuses.go +++ b/api/censuses.go @@ -178,7 +178,7 @@ func (capi *census3API) createAndPublishCensus(req *CreateCensusRequest, qID str // parse token information tokensInfo := map[string]*strategyoperators.TokenInformation{} for _, token := range strategyTokens { - tokensInfo[token.Symbol.String] = &strategyoperators.TokenInformation{ + tokensInfo[token.Symbol] = &strategyoperators.TokenInformation{ ID: common.BytesToAddress(token.ID).String(), ChainID: token.ChainID, MinBalance: new(big.Int).SetBytes(token.MinBalance).String(), diff --git a/api/strategies.go b/api/strategies.go index b3885b11..3a640755 100644 --- a/api/strategies.go +++ b/api/strategies.go @@ -84,10 +84,10 @@ func (capi *census3API) getStrategies(msg *api.APIdata, ctx *httprouter.HTTPCont return ErrCantGetStrategies.WithErr(err) } for _, strategyToken := range strategyTokens { - if !strategyToken.Symbol.Valid { + if strategyToken.Symbol == "" { return ErrCantGetStrategies.With("invalid token symbol") } - strategyResponse.Tokens[strategyToken.Symbol.String] = &StrategyToken{ + strategyResponse.Tokens[strategyToken.Symbol] = &StrategyToken{ ID: common.BytesToAddress(strategyToken.TokenID).String(), ChainID: strategyToken.ChainID, MinBalance: new(big.Int).SetBytes(strategyToken.MinBalance).String(), @@ -223,7 +223,7 @@ func (capi *census3API) getStrategy(msg *api.APIdata, ctx *httprouter.HTTPContex } // parse and encode tokens information for _, tokenData := range tokensData { - strategy.Tokens[tokenData.Symbol.String] = &StrategyToken{ + strategy.Tokens[tokenData.Symbol] = &StrategyToken{ ID: common.BytesToAddress(tokenData.ID).String(), MinBalance: new(big.Int).SetBytes(tokenData.MinBalance).String(), ChainID: tokenData.ChainID, @@ -273,10 +273,10 @@ func (capi *census3API) getTokenStrategies(msg *api.APIdata, ctx *httprouter.HTT return ErrCantGetStrategies.WithErr(err) } for _, strategyToken := range strategyTokens { - if !strategyToken.Symbol.Valid { + if strategyToken.Symbol == "" { return ErrCantGetStrategies.With("invalid token symbol") } - strategyResponse.Tokens[strategyToken.Symbol.String] = &StrategyToken{ + strategyResponse.Tokens[strategyToken.Symbol] = &StrategyToken{ ID: common.BytesToAddress(strategyToken.TokenID).String(), ChainID: strategyToken.ChainID, MinBalance: new(big.Int).SetBytes(strategyToken.MinBalance).String(), diff --git a/api/tokens.go b/api/tokens.go index 0cb8acb4..490a727a 100644 --- a/api/tokens.go +++ b/api/tokens.go @@ -64,10 +64,10 @@ func (capi *census3API) getTokens(msg *api.APIdata, ctx *httprouter.HTTPContext) tokenResponse := GetTokensItem{ ID: common.BytesToAddress(tokenData.ID).String(), Type: state.TokenType(int(tokenData.TypeID)).String(), - Name: tokenData.Name.String, - StartBlock: tokenData.CreationBlock.Int64, - Tags: tokenData.Tags.String, - Symbol: tokenData.Symbol.String, + Name: tokenData.Name, + StartBlock: tokenData.CreationBlock, + Tags: tokenData.Tags, + Symbol: tokenData.Symbol, ChainID: tokenData.ChainID, } tokens.Tokens = append(tokens.Tokens, tokenResponse) @@ -110,27 +110,6 @@ func (capi *census3API) createToken(msg *api.APIdata, ctx *httprouter.HTTPContex if err != nil { return ErrCantGetToken.WithErr(err) } - var ( - name = new(sql.NullString) - symbol = new(sql.NullString) - creationBlock = new(sql.NullInt64) - totalSupply = new(big.Int) - tags = new(sql.NullString) - ) - if err := name.Scan(info.Name); err != nil { - return ErrCantGetToken.WithErr(err) - } - if err := symbol.Scan(info.Symbol); err != nil { - return ErrCantGetToken.WithErr(err) - } - if info.TotalSupply != nil { - totalSupply = info.TotalSupply - } - if req.Tags != "" { - if err := tags.Scan(req.Tags); err != nil { - return ErrCantGetToken.WithErr(err) - } - } // init db transaction tx, err := capi.db.RW.BeginTx(internalCtx, nil) if err != nil { @@ -144,14 +123,14 @@ func (capi *census3API) createToken(msg *api.APIdata, ctx *httprouter.HTTPContex qtx := capi.db.QueriesRW.WithTx(tx) _, err = qtx.CreateToken(internalCtx, queries.CreateTokenParams{ ID: info.Address.Bytes(), - Name: *name, - Symbol: *symbol, + Name: info.Name, + Symbol: info.Symbol, Decimals: info.Decimals, - TotalSupply: totalSupply.Bytes(), - CreationBlock: *creationBlock, + TotalSupply: info.TotalSupply.Bytes(), + CreationBlock: 0, TypeID: uint64(tokenType), Synced: false, - Tags: *tags, + Tags: req.Tags, ChainID: req.ChainID, }) if err != nil { @@ -163,7 +142,7 @@ func (capi *census3API) createToken(msg *api.APIdata, ctx *httprouter.HTTPContex // create a default strategy to support censuses over the holders of this // single token res, err := qtx.CreateStategy(internalCtx, queries.CreateStategyParams{ - Alias: fmt.Sprintf("default %s strategy", info.Symbol), + Alias: fmt.Sprintf("Default strategy for token %s", info.Symbol), Predicate: lexer.ScapeTokenSymbol(info.Symbol), }) if err != nil { @@ -253,22 +232,20 @@ func (capi *census3API) getToken(msg *api.APIdata, ctx *httprouter.HTTPContext) Type: state.TokenType(int(tokenData.TypeID)).String(), Decimals: tokenData.Decimals, Size: uint64(holders), - Name: tokenData.Name.String, - Symbol: tokenData.Symbol.String, + Name: tokenData.Name, + Symbol: tokenData.Symbol, TotalSupply: new(big.Int).SetBytes(tokenData.TotalSupply).String(), + StartBlock: uint64(tokenData.CreationBlock), Status: &GetTokenStatusResponse{ AtBlock: atBlock, Synced: tokenData.Synced, Progress: tokenProgress, }, - Tags: tokenData.Tags.String, + Tags: tokenData.Tags, // TODO: Only for the MVP, consider to remove it DefaultStrategy: defaultStrategyID, ChainID: tokenData.ChainID, } - if tokenData.CreationBlock.Valid { - tokenResponse.StartBlock = uint64(tokenData.CreationBlock.Int64) - } res, err := json.Marshal(tokenResponse) if err != nil { return ErrEncodeToken.WithErr(err) diff --git a/db/migrations/0002_census3.sql b/db/migrations/0002_census3.sql index bb6da442..335cc227 100644 --- a/db/migrations/0002_census3.sql +++ b/db/migrations/0002_census3.sql @@ -1,26 +1,26 @@ -- +goose Up -- stategies table schema updates -ALTER TABLE strategies ADD COLUMN alias TEXT; +ALTER TABLE strategies ADD COLUMN alias TEXT NOT NULL DEFAULT ''; -- tokens table schema updates CREATE TABLE tokens_copy ( id BLOB NOT NULL, - name TEXT, - symbol TEXT, - decimals INTEGER, - total_supply BLOB, - creation_block BIGINT, - type_id INTEGER NOT NULL, - synced BOOLEAN NOT NULL, - tags TEXT, - chain_id INTEGER NOT NULL, + name TEXT NOT NULL DEFAULT '', + symbol TEXT NOT NULL DEFAULT '', + decimals INTEGER NOT NULL DEFAULT 0, + total_supply BLOB NOT NULL DEFAULT '', + creation_block BIGINT NOT NULL DEFAULT 0, + type_id INTEGER NOT NULL DEFAULT 0, + synced BOOLEAN NOT NULL DEFAULT 0, + tags TEXT NOT NULL DEFAULT '', + chain_id INTEGER NOT NULL DEFAULT 0, PRIMARY KEY (id, chain_id), FOREIGN KEY (type_id) REFERENCES token_types(id) ON DELETE CASCADE ); INSERT INTO tokens_copy SELECT * FROM tokens; DROP TABLE tokens; -DROP INDEX IF EXISTS idx_tokens_type_id; +-- DROP INDEX IF EXISTS idx_tokens_type_id; ALTER TABLE tokens_copy RENAME TO tokens; CREATE INDEX idx_tokens_type_id ON tokens(type_id); @@ -43,9 +43,9 @@ SELECT * FROM ( SELECT token.chain_id FROM tokens AS token WHERE token.id = token_holders.token_id ) AS chain_id FROM token_holders ); -DROP INDEX IF EXISTS idx_token_holders_token_id; -DROP INDEX IF EXISTS idx_token_holders_holder_id; -DROP INDEX IF EXISTS idx_token_holders_block_id; +-- DROP INDEX IF EXISTS idx_token_holders_token_id; +-- DROP INDEX IF EXISTS idx_token_holders_holder_id; +-- DROP INDEX IF EXISTS idx_token_holders_block_id; DROP TABLE token_holders; ALTER TABLE token_holders_copy RENAME TO token_holders; CREATE INDEX idx_token_holders_token_id ON token_holders(token_id); @@ -69,8 +69,8 @@ SELECT * FROM ( ) AS chain_id FROM strategy_tokens ); -DROP INDEX IF EXISTS idx_strategy_tokens_strategy_id; -DROP INDEX IF EXISTS idx_strategy_tokens_token_id; +-- DROP INDEX IF EXISTS idx_strategy_tokens_strategy_id; +-- DROP INDEX IF EXISTS idx_strategy_tokens_token_id; DROP TABLE strategy_tokens; ALTER TABLE strategy_tokens_copy RENAME TO strategy_tokens; CREATE INDEX idx_strategy_tokens_strategy_id ON strategy_tokens(strategy_id); diff --git a/db/sqlc/blocks.sql.go b/db/sqlc/blocks.sql.go index 7d8d1c81..4276dddc 100644 --- a/db/sqlc/blocks.sql.go +++ b/db/sqlc/blocks.sql.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.20.0 +// sqlc v1.22.0 // source: blocks.sql package queries diff --git a/db/sqlc/censuses.sql.go b/db/sqlc/censuses.sql.go index 1e3da650..eb374bf3 100644 --- a/db/sqlc/censuses.sql.go +++ b/db/sqlc/censuses.sql.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.20.0 +// sqlc v1.22.0 // source: censuses.sql package queries diff --git a/db/sqlc/db.go b/db/sqlc/db.go index 99526a5a..7427aff6 100644 --- a/db/sqlc/db.go +++ b/db/sqlc/db.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.20.0 +// sqlc v1.22.0 package queries diff --git a/db/sqlc/holders.sql.go b/db/sqlc/holders.sql.go index 858b9744..b2fa627c 100644 --- a/db/sqlc/holders.sql.go +++ b/db/sqlc/holders.sql.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.20.0 +// sqlc v1.22.0 // source: holders.sql package queries diff --git a/db/sqlc/models.go b/db/sqlc/models.go index e8e62415..1a216661 100644 --- a/db/sqlc/models.go +++ b/db/sqlc/models.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.20.0 +// sqlc v1.22.0 package queries @@ -33,27 +33,27 @@ type Holder struct { type Strategy struct { ID uint64 - Alias string Predicate string + Alias string } type StrategyToken struct { StrategyID uint64 TokenID []byte - ChainID uint64 MinBalance []byte + ChainID uint64 } type Token struct { ID annotations.Address - Name sql.NullString - Symbol sql.NullString + Name string + Symbol string Decimals uint64 TotalSupply annotations.BigInt - CreationBlock sql.NullInt64 + CreationBlock int64 TypeID uint64 Synced bool - Tags sql.NullString + Tags string ChainID uint64 } diff --git a/db/sqlc/strategies.sql.go b/db/sqlc/strategies.sql.go index b67b37c5..bfcab4ba 100644 --- a/db/sqlc/strategies.sql.go +++ b/db/sqlc/strategies.sql.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.20.0 +// sqlc v1.22.0 // source: strategies.sql package queries @@ -53,7 +53,7 @@ func (q *Queries) CreateStrategyToken(ctx context.Context, arg CreateStrategyTok } const listStrategies = `-- name: ListStrategies :many -SELECT id, alias, predicate FROM strategies +SELECT id, predicate, alias FROM strategies ORDER BY id ` @@ -66,7 +66,7 @@ func (q *Queries) ListStrategies(ctx context.Context) ([]Strategy, error) { var items []Strategy for rows.Next() { var i Strategy - if err := rows.Scan(&i.ID, &i.Alias, &i.Predicate); err != nil { + if err := rows.Scan(&i.ID, &i.Predicate, &i.Alias); err != nil { return nil, err } items = append(items, i) @@ -81,7 +81,7 @@ func (q *Queries) ListStrategies(ctx context.Context) ([]Strategy, error) { } const strategiesByTokenID = `-- name: StrategiesByTokenID :many -SELECT s.id, s.alias, s.predicate FROM strategies s +SELECT s.id, s.predicate, s.alias FROM strategies s JOIN strategy_tokens st ON st.strategy_id = s.id WHERE st.token_id = ? ORDER BY s.id @@ -96,7 +96,7 @@ func (q *Queries) StrategiesByTokenID(ctx context.Context, tokenID []byte) ([]St var items []Strategy for rows.Next() { var i Strategy - if err := rows.Scan(&i.ID, &i.Alias, &i.Predicate); err != nil { + if err := rows.Scan(&i.ID, &i.Predicate, &i.Alias); err != nil { return nil, err } items = append(items, i) @@ -111,7 +111,7 @@ func (q *Queries) StrategiesByTokenID(ctx context.Context, tokenID []byte) ([]St } const strategyByID = `-- name: StrategyByID :one -SELECT id, alias, predicate FROM strategies +SELECT id, predicate, alias FROM strategies WHERE id = ? LIMIT 1 ` @@ -119,12 +119,12 @@ LIMIT 1 func (q *Queries) StrategyByID(ctx context.Context, id uint64) (Strategy, error) { row := q.db.QueryRowContext(ctx, strategyByID, id) var i Strategy - err := row.Scan(&i.ID, &i.Alias, &i.Predicate) + err := row.Scan(&i.ID, &i.Predicate, &i.Alias) return i, err } const strategyTokens = `-- name: StrategyTokens :many -SELECT strategy_id, token_id, chain_id, min_balance +SELECT strategy_id, token_id, min_balance, chain_id FROM strategy_tokens ORDER BY strategy_id, token_id ` @@ -141,8 +141,8 @@ func (q *Queries) StrategyTokens(ctx context.Context) ([]StrategyToken, error) { if err := rows.Scan( &i.StrategyID, &i.TokenID, - &i.ChainID, &i.MinBalance, + &i.ChainID, ); err != nil { return nil, err } @@ -158,7 +158,7 @@ func (q *Queries) StrategyTokens(ctx context.Context) ([]StrategyToken, error) { } const strategyTokensByStrategyID = `-- name: StrategyTokensByStrategyID :many -SELECT st.strategy_id, st.token_id, st.chain_id, st.min_balance, t.symbol +SELECT st.strategy_id, st.token_id, st.min_balance, st.chain_id, t.symbol FROM strategy_tokens st JOIN tokens t ON t.ID = st.token_id WHERE strategy_id = ? @@ -168,9 +168,9 @@ ORDER BY strategy_id, token_id type StrategyTokensByStrategyIDRow struct { StrategyID uint64 TokenID []byte - ChainID uint64 MinBalance []byte - Symbol sql.NullString + ChainID uint64 + Symbol string } func (q *Queries) StrategyTokensByStrategyID(ctx context.Context, strategyID uint64) ([]StrategyTokensByStrategyIDRow, error) { @@ -185,8 +185,8 @@ func (q *Queries) StrategyTokensByStrategyID(ctx context.Context, strategyID uin if err := rows.Scan( &i.StrategyID, &i.TokenID, - &i.ChainID, &i.MinBalance, + &i.ChainID, &i.Symbol, ); err != nil { return nil, err diff --git a/db/sqlc/tokenTypes.sql.go b/db/sqlc/tokenTypes.sql.go index 7e4df009..623b106c 100644 --- a/db/sqlc/tokenTypes.sql.go +++ b/db/sqlc/tokenTypes.sql.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.20.0 +// sqlc v1.22.0 // source: tokenTypes.sql package queries diff --git a/db/sqlc/tokens.sql.go b/db/sqlc/tokens.sql.go index be38e73d..6233907c 100644 --- a/db/sqlc/tokens.sql.go +++ b/db/sqlc/tokens.sql.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.20.0 +// sqlc v1.22.0 // source: tokens.sql package queries @@ -32,14 +32,14 @@ VALUES ( type CreateTokenParams struct { ID annotations.Address - Name sql.NullString - Symbol sql.NullString + Name string + Symbol string Decimals uint64 TotalSupply annotations.BigInt - CreationBlock sql.NullInt64 + CreationBlock int64 TypeID uint64 Synced bool - Tags sql.NullString + Tags string ChainID uint64 } @@ -155,7 +155,7 @@ func (q *Queries) TokenByID(ctx context.Context, id annotations.Address) (Token, } const tokensByStrategyID = `-- name: TokensByStrategyID :many -SELECT t.id, t.name, t.symbol, t.decimals, t.total_supply, t.creation_block, t.type_id, t.synced, t.tags, t.chain_id, st.strategy_id, st.token_id, st.chain_id, st.min_balance FROM tokens t +SELECT t.id, t.name, t.symbol, t.decimals, t.total_supply, t.creation_block, t.type_id, t.synced, t.tags, t.chain_id, st.strategy_id, st.token_id, st.min_balance, st.chain_id FROM tokens t JOIN strategy_tokens st ON st.token_id = t.id WHERE st.strategy_id = ? ORDER BY t.name @@ -163,19 +163,19 @@ ORDER BY t.name type TokensByStrategyIDRow struct { ID annotations.Address - Name sql.NullString - Symbol sql.NullString + Name string + Symbol string Decimals uint64 TotalSupply annotations.BigInt - CreationBlock sql.NullInt64 + CreationBlock int64 TypeID uint64 Synced bool - Tags sql.NullString + Tags string ChainID uint64 StrategyID uint64 TokenID []byte - ChainID_2 uint64 MinBalance []byte + ChainID_2 uint64 } func (q *Queries) TokensByStrategyID(ctx context.Context, strategyID uint64) ([]TokensByStrategyIDRow, error) { @@ -200,8 +200,8 @@ func (q *Queries) TokensByStrategyID(ctx context.Context, strategyID uint64) ([] &i.ChainID, &i.StrategyID, &i.TokenID, - &i.ChainID_2, &i.MinBalance, + &i.ChainID_2, ); err != nil { return nil, err } @@ -223,7 +223,7 @@ WHERE id = ? ` type UpdateTokenCreationBlockParams struct { - CreationBlock sql.NullInt64 + CreationBlock int64 ID annotations.Address } diff --git a/service/helper_test.go b/service/helper_test.go index bc01c299..5002d134 100644 --- a/service/helper_test.go +++ b/service/helper_test.go @@ -1,7 +1,6 @@ package service import ( - "database/sql" "math/big" "os" "testing" @@ -70,11 +69,11 @@ func testTokenParams(id, name, symbol string, creationBlock, decimals, typeID ui ) queries.CreateTokenParams { return queries.CreateTokenParams{ ID: common.HexToAddress(id).Bytes(), - Name: sql.NullString{String: name, Valid: name != ""}, - Symbol: sql.NullString{String: symbol, Valid: symbol != ""}, + Name: name, + Symbol: symbol, Decimals: decimals, TotalSupply: new(big.Int).SetInt64(int64(totalSupply)).Bytes(), - CreationBlock: sql.NullInt64{Int64: int64(creationBlock), Valid: creationBlock != 0}, + CreationBlock: int64(creationBlock), TypeID: typeID, Synced: synced, ChainID: chainID, diff --git a/service/holder_scanner_test.go b/service/holder_scanner_test.go index e80dd90f..aa19dc51 100644 --- a/service/holder_scanner_test.go +++ b/service/holder_scanner_test.go @@ -224,5 +224,5 @@ func Test_calcTokenCreationBlock(t *testing.T) { c.Assert(hs.calcTokenCreationBlock(context.Background(), MonkeysAddress), qt.IsNil) token, err := testdb.db.QueriesRW.TokenByID(context.Background(), MonkeysAddress.Bytes()) c.Assert(err, qt.IsNil) - c.Assert(uint64(token.CreationBlock.Int64), qt.Equals, MonkeysCreationBlock) + c.Assert(uint64(token.CreationBlock), qt.Equals, MonkeysCreationBlock) } diff --git a/service/holders_scanner.go b/service/holders_scanner.go index 3094f17e..8c47c8cf 100644 --- a/service/holders_scanner.go +++ b/service/holders_scanner.go @@ -131,7 +131,7 @@ func (s *HoldersScanner) tokenAddresses() (map[common.Address]bool, error) { // parse and return token addresses results := make(map[common.Address]bool) for _, token := range tokens { - results[common.BytesToAddress(token.ID)] = token.CreationBlock.Valid + results[common.BytesToAddress(token.ID)] = token.CreationBlock != 0 } return results, nil } @@ -315,7 +315,7 @@ func (s *HoldersScanner) scanHolders(ctx context.Context, addr common.Address) ( return false, err } ttype := state.TokenType(tokenInfo.TypeID) - tokenLastBlock := uint64(tokenInfo.CreationBlock.Int64) + tokenLastBlock := uint64(tokenInfo.CreationBlock) if blockNumber, err := s.db.QueriesRO.LastBlockByTokenID(ctx, addr.Bytes()); err == nil { tokenLastBlock = blockNumber } @@ -395,15 +395,11 @@ func (s *HoldersScanner) calcTokenCreationBlock(ctx context.Context, addr common if err != nil { return fmt.Errorf("error getting token creation block: %w", err) } - dbCreationBlock := new(sql.NullInt64) - if err := dbCreationBlock.Scan(creationBlock); err != nil { - return fmt.Errorf("error getting token creation block value: %w", err) - } // save the creation block into the database _, err = s.db.QueriesRW.UpdateTokenCreationBlock(ctx, queries.UpdateTokenCreationBlockParams{ ID: addr.Bytes(), - CreationBlock: *dbCreationBlock, + CreationBlock: int64(creationBlock), }) if err != nil { return fmt.Errorf("error updating token creation block on the database: %w", err)