-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ifps token metadata decimals parsing. Added: migration
- Loading branch information
1 parent
10a88ec
commit 6a08c67
Showing
6 changed files
with
147 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
scripts/migration/migrations/token_metadata_set_decimals.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package migrations | ||
|
||
import ( | ||
"github.com/baking-bad/bcdhub/internal/config" | ||
"github.com/baking-bad/bcdhub/internal/logger" | ||
"github.com/baking-bad/bcdhub/internal/models" | ||
"github.com/baking-bad/bcdhub/internal/models/tokenmetadata" | ||
"github.com/baking-bad/bcdhub/internal/parsers/tzip/tokens" | ||
) | ||
|
||
// TokenMetadataSetDecimals - migration that set decimals in token metadata for all metadata with empty decimals | ||
type TokenMetadataSetDecimals struct{} | ||
|
||
// Key - | ||
func (m *TokenMetadataSetDecimals) Key() string { | ||
return "token_metadata_set_decimals" | ||
} | ||
|
||
// Description - | ||
func (m *TokenMetadataSetDecimals) Description() string { | ||
return "set decimals in token metadata for all metadata with empty decimals" | ||
} | ||
|
||
// Do - migrate function | ||
func (m *TokenMetadataSetDecimals) Do(ctx *config.Context) error { | ||
var updates []models.Model | ||
|
||
for _, network := range ctx.Config.Scripts.Networks { | ||
logger.Info("Work with %swork", network) | ||
rpc, err := ctx.GetRPC(network) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
parser := tokens.NewParser(ctx.BigMapDiffs, ctx.Blocks, ctx.Protocols, ctx.Schema, ctx.Storage, rpc, ctx.SharePath, network, ctx.Config.IPFSGateways...) | ||
|
||
logger.Info("Receiving token metadata....") | ||
tokenMetadata, err := ctx.TokenMetadata.Get(tokenmetadata.GetContext{ | ||
TokenID: -1, | ||
Network: network, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
logger.Info("Received %d metadata....", len(tokenMetadata)) | ||
|
||
if len(tokenMetadata) == 0 { | ||
continue | ||
} | ||
|
||
for i, tm := range tokenMetadata { | ||
if tm.Decimals != nil { | ||
continue | ||
} | ||
|
||
parsedTm, err := parser.Parse(tm.Contract, 0) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for j, ptm := range parsedTm { | ||
if ptm.Decimals != nil { | ||
logger.Info("Found: contract=%s decimals=%v", ptm.Contract, *ptm.Decimals) | ||
tokenMetadata[i].Decimals = parsedTm[j].Decimals | ||
updates = append(updates, &tokenMetadata[i]) | ||
} | ||
} | ||
} | ||
} | ||
|
||
logger.Info("Total updates: %d", len(updates)) | ||
|
||
return ctx.Storage.BulkUpdate(updates) | ||
} |