Skip to content

Commit

Permalink
Fix/erc20 decimals (#4)
Browse files Browse the repository at this point in the history
* Added migration to transform unicode erc20 unicode decimals to numeric

* Fix: erc20 tokens decimals filling with numbers, instead of unicode string

* Improved multicallErc20 token decimals assigning
  • Loading branch information
k-karuna authored Sep 4, 2024
1 parent 48313ee commit d1c17c3
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 13 deletions.
6 changes: 5 additions & 1 deletion cmd/metadata/filler.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,11 @@ func (f Filler) multicallErc20(ctx context.Context, address data.Felt, selectorN
}
task.Metadata[entrypointName] = response[1].ToAsciiString()
task.Metadata[entrypointSymbol] = response[2].ToAsciiString()
task.Metadata[entrypointDecimals] = response[3].ToAsciiString()
decimals, err := response[3].Uint64()
if err != nil {
return err
}
task.Metadata[entrypointDecimals] = decimals
return nil
}

Expand Down
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ require (
github.com/karlseguin/ccache/v2 v2.0.8
github.com/lib/pq v1.10.9
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.31.0
github.com/rs/zerolog v1.33.0
github.com/shopspring/decimal v1.3.1
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
github.com/uptrace/bun v1.1.17
github.com/uptrace/bun v1.2.3
)

require (
Expand Down Expand Up @@ -196,6 +196,7 @@ require (
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/puzpuzpuz/xsync/v3 v3.4.0 // indirect
github.com/quic-go/qpack v0.4.0 // indirect
github.com/quic-go/qtls-go1-20 v0.3.3 // indirect
github.com/quic-go/quic-go v0.38.1 // indirect
Expand All @@ -211,7 +212,7 @@ require (
github.com/testcontainers/testcontainers-go/modules/postgres v0.22.0 // indirect
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
github.com/ucarion/urlpath v0.0.0-20200424170820-7ccc79b76bbb // indirect
github.com/uptrace/bun/dialect/pgdialect v1.1.17 // indirect
github.com/uptrace/bun/dialect/pgdialect v1.2.3 // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/wealdtech/go-merkletree v1.0.1-0.20230205101955-ec7a95ea11ca // indirect
Expand Down Expand Up @@ -245,7 +246,7 @@ require (
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.15.0 // indirect
Expand Down
18 changes: 10 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,8 @@ github.com/prometheus/common v0.44.0/go.mod h1:ofAIvZbQ1e/nugmZGz4/qCb9Ap1VoSTIO
github.com/prometheus/procfs v0.0.0-20180725123919-05ee40e3a273/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/puzpuzpuz/xsync/v3 v3.4.0 h1:DuVBAdXuGFHv8adVXjWWZ63pJq+NRXOWVXlKDBZ+mJ4=
github.com/puzpuzpuz/xsync/v3 v3.4.0/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPKP9jpmh0nnA=
github.com/quic-go/qpack v0.4.0 h1:Cr9BXA1sQS2SmDUWjSofMPNKmvF6IiIfDRmgU0w1ZCo=
github.com/quic-go/qpack v0.4.0/go.mod h1:UZVnYIfi5GRk+zI9UMaCPsmZ2xKJP7XBUvVyT1Knj9A=
github.com/quic-go/qtls-go1-20 v0.3.3 h1:17/glZSLI9P9fDAeyCHBFSWSqJcwx1byhLwP5eUIDCM=
Expand All @@ -774,8 +776,8 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A=
github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand Down Expand Up @@ -874,10 +876,10 @@ github.com/tv42/httpunix v0.0.0-20191220191345-2ba4b9c3382c/go.mod h1:hzIxponao9
github.com/ucarion/urlpath v0.0.0-20200424170820-7ccc79b76bbb h1:Ywfo8sUltxogBpFuMOFRrrSifO788kAFxmvVw31PtQQ=
github.com/ucarion/urlpath v0.0.0-20200424170820-7ccc79b76bbb/go.mod h1:ikPs9bRWicNw3S7XpJ8sK/smGwU9WcSVU3dy9qahYBM=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/uptrace/bun v1.1.17 h1:qxBaEIo0hC/8O3O6GrMDKxqyT+mw5/s0Pn/n6xjyGIk=
github.com/uptrace/bun v1.1.17/go.mod h1:hATAzivtTIRsSJR4B8AXR+uABqnQxr3myKDKEf5iQ9U=
github.com/uptrace/bun/dialect/pgdialect v1.1.17 h1:NsvFVHAx1Az6ytlAD/B6ty3cVE6j9Yp82bjqd9R9hOs=
github.com/uptrace/bun/dialect/pgdialect v1.1.17/go.mod h1:fLBDclNc7nKsZLzNjFL6BqSdgJzbj2HdnyOnLoDvAME=
github.com/uptrace/bun v1.2.3 h1:6KDc6YiNlXde38j9ATKufb8o7MS8zllhAOeIyELKrk0=
github.com/uptrace/bun v1.2.3/go.mod h1:8frYFHrO/Zol3I4FEjoXam0HoNk+t5k7aJRl3FXp0mk=
github.com/uptrace/bun/dialect/pgdialect v1.2.3 h1:YyCxxqeL0lgFWRZzKCOt6mnxUsjqITcxSo0mLqgwMUA=
github.com/uptrace/bun/dialect/pgdialect v1.2.3/go.mod h1:Vx9TscyEq1iN4tnirn6yYGwEflz0KG3rBZTBCLpKAjc=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/urfave/cli v1.22.10/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=
Expand Down Expand Up @@ -1142,8 +1144,8 @@ golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
Expand Down
15 changes: 15 additions & 0 deletions internal/storage/postgres/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package postgres
import (
"context"
"database/sql"
"github.com/dipdup-io/starknet-metadata/internal/storage/postgres/migrations"
"github.com/uptrace/bun/migrate"

models "github.com/dipdup-io/starknet-metadata/internal/storage"
"github.com/dipdup-net/go-lib/config"
Expand Down Expand Up @@ -62,6 +64,10 @@ func initDatabase(ctx context.Context, conn *database.Bun) error {
return errors.Wrap(err, "make comments")
}

if err := applyMigrations(ctx, conn); err != nil {
return err
}

return createIndices(ctx, conn)
}

Expand Down Expand Up @@ -100,3 +106,12 @@ func createTypes(ctx context.Context, conn *database.Bun) error {
return nil
})
}

func applyMigrations(ctx context.Context, conn *database.Bun) error {
migrator := migrate.NewMigrator(conn.DB(), migrations.DbMigrations)
if err := migrator.Init(ctx); err != nil {
return err
}
_, err := migrator.Migrate(ctx)
return err
}
50 changes: 50 additions & 0 deletions internal/storage/postgres/migrations/0001_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package migrations

import (
"context"
"fmt"
"github.com/dipdup-io/starknet-metadata/internal/storage"
"github.com/rs/zerolog/log"
"github.com/uptrace/bun"
)

func init() {
DbMigrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
var erc20Tokens []storage.TokenMetadata
err := db.NewSelect().
Model(&erc20Tokens).
Where("type = ?", storage.TokenTypeERC20).
Scan(ctx)

if err != nil {
fmt.Println("Error:", err)
}

for _, token := range erc20Tokens {
if token.Metadata == nil {
continue
}
unicodeDecimals, ok := token.Metadata["decimals"].(string)
if !ok || unicodeDecimals == "" {
continue
}
runeDecimals := []rune(unicodeDecimals)[0]
token.Metadata["decimals"] = int(runeDecimals)
_, err = db.NewUpdate().
Model(&token).
Column("metadata").
Where("id = ?", token.Id).
Exec(ctx)
if err != nil {
log.Err(err).Uint64("token id", token.Id).Msg("error updating token")
}
}
log.Info().
Int("updated erc20 tokens amount", len(erc20Tokens)).
Msg("migration applied")
return nil

}, func(ctx context.Context, db *bun.DB) error {
return nil
})
}
7 changes: 7 additions & 0 deletions internal/storage/postgres/migrations/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package migrations

import (
"github.com/uptrace/bun/migrate"
)

var DbMigrations = migrate.NewMigrations()

0 comments on commit d1c17c3

Please sign in to comment.