Skip to content

Commit

Permalink
make all columns not null to avoid edge cases and simplify queries
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmenendez committed Oct 16, 2023
1 parent 1b3e9aa commit aca53c9
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 109 deletions.
2 changes: 1 addition & 1 deletion api/censuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
10 changes: 5 additions & 5 deletions api/strategies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
Expand Down
51 changes: 14 additions & 37 deletions api/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
32 changes: 16 additions & 16 deletions db/migrations/0002_census3.sql
Original file line number Diff line number Diff line change
@@ -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);

Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion db/sqlc/blocks.sql.go

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

2 changes: 1 addition & 1 deletion db/sqlc/censuses.sql.go

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

2 changes: 1 addition & 1 deletion db/sqlc/db.go

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

2 changes: 1 addition & 1 deletion db/sqlc/holders.sql.go

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

14 changes: 7 additions & 7 deletions db/sqlc/models.go

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

26 changes: 13 additions & 13 deletions db/sqlc/strategies.sql.go

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

2 changes: 1 addition & 1 deletion db/sqlc/tokenTypes.sql.go

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

Loading

0 comments on commit aca53c9

Please sign in to comment.