Skip to content

Commit

Permalink
Fix: pass models to transfer parser
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored and m-kus committed Dec 2, 2020
1 parent 1966b05 commit 6dd2eb9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
13 changes: 5 additions & 8 deletions internal/events/michelson_extended_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/baking-bad/bcdhub/internal/contractparser/storage"
"github.com/baking-bad/bcdhub/internal/elastic"
"github.com/baking-bad/bcdhub/internal/logger"
"github.com/baking-bad/bcdhub/internal/models"
"github.com/baking-bad/bcdhub/internal/models/tzip"
"github.com/tidwall/gjson"
)
Expand All @@ -22,10 +23,11 @@ type MichelsonExtendedStorage struct {
operationID string
contract string
es elastic.IElastic
bmd []models.BigMapDiff
}

// NewMichelsonExtendedStorage -
func NewMichelsonExtendedStorage(impl tzip.EventImplementation, name, protocol, operationID, contract string, es elastic.IElastic) (*MichelsonExtendedStorage, error) {
func NewMichelsonExtendedStorage(impl tzip.EventImplementation, name, protocol, operationID, contract string, es elastic.IElastic, bmd []models.BigMapDiff) (*MichelsonExtendedStorage, error) {
parser, err := GetParser(name, impl.MichelsonExtendedStorageEvent.ReturnType)
if err != nil {
return nil, err
Expand All @@ -42,6 +44,7 @@ func NewMichelsonExtendedStorage(impl tzip.EventImplementation, name, protocol,
protocol: protocol,
operationID: operationID,
es: es,
bmd: bmd,
contract: contract,
}, nil
}
Expand All @@ -59,13 +62,7 @@ func (mes *MichelsonExtendedStorage) Normalize(value string) gjson.Result {
return gjson.Parse(value)
}

bmd, err := mes.es.GetBigMapDiffsUniqueByOperationID(mes.operationID)
if err != nil {
logger.Error(err)
return gjson.Parse(value)
}

val, err := parser.Enrich(value, "", bmd, true, false)
val, err := parser.Enrich(value, "", mes.bmd, true, false)
if err != nil {
logger.Error(err)
return gjson.Parse(value)
Expand Down
2 changes: 1 addition & 1 deletion internal/parsers/operations/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (p Transaction) Parse(data gjson.Result) ([]elastic.Model, error) {

p.stackTrace.Add(tx)

transfers, err := p.transferParser.Parse(tx)
transfers, err := p.transferParser.Parse(tx, txModels)
if err != nil {
return nil, err
}
Expand Down
14 changes: 10 additions & 4 deletions internal/parsers/transfer/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func NewParser(rpc noderpc.INode, es elastic.IElastic, opts ...ParserOption) (*P
}

// Parse -
func (p *Parser) Parse(operation models.Operation) ([]*models.Transfer, error) {
func (p *Parser) Parse(operation models.Operation, operationModels []elastic.Model) ([]*models.Transfer, error) {
if impl, name, ok := p.events.GetByOperation(operation); ok {
return p.executeEvents(impl, name, operation)
return p.executeEvents(impl, name, operation, operationModels)
} else if operation.Entrypoint == consts.TransferEntrypoint {
parameters := getParameters(operation.Parameters)
for i := range operation.Tags {
Expand All @@ -74,7 +74,7 @@ func (p *Parser) Parse(operation models.Operation) ([]*models.Transfer, error) {
return nil, nil
}

func (p *Parser) executeEvents(impl tzip.EventImplementation, name string, operation models.Operation) ([]*models.Transfer, error) {
func (p *Parser) executeEvents(impl tzip.EventImplementation, name string, operation models.Operation, operationModels []elastic.Model) ([]*models.Transfer, error) {
if operation.Kind != consts.Transaction {
return nil, nil
}
Expand All @@ -99,7 +99,13 @@ func (p *Parser) executeEvents(impl tzip.EventImplementation, name string, opera
case impl.MichelsonExtendedStorageEvent.Is(operation.Entrypoint):
ctx.Parameters = operation.DeffatedStorage
ctx.Entrypoint = consts.DefaultEntrypoint
event, err = events.NewMichelsonExtendedStorage(impl, name, operation.Protocol, operation.GetID(), operation.Destination, p.es)
bmd := make([]models.BigMapDiff, 0)
for i := range operationModels {
if model, ok := operationModels[i].(*models.BigMapDiff); ok && model.OperationID == operation.ID {
bmd = append(bmd, *model)
}
}
event, err = events.NewMichelsonExtendedStorage(impl, name, operation.Protocol, operation.GetID(), operation.Destination, p.es, bmd)
default:
return nil, nil
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/migration/migrations/create_transfers.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (m *CreateTransfersTags) Do(ctx *config.Context) error {
return err
}

transfers, err := parser.Parse(operations[i])
transfers, err := parser.Parse(operations[i], nil)
if err != nil {
return err
}
Expand Down
12 changes: 11 additions & 1 deletion scripts/migration/migrations/extended_storage_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,17 @@ func (m *ExtendedStorageEvents) Do(ctx *config.Context) error {
}

for _, op := range operations {
transfers, err := parser.Parse(op)
bmd, err := ctx.ES.GetBigMapDiffsByOperationID(op.ID)
if err != nil {
if !elastic.IsRecordNotFound(err) {
return err
}
}
opModels := make([]elastic.Model, len(bmd))
for j := range bmd {
opModels[j] = bmd[j]
}
transfers, err := parser.Parse(op, opModels)
if err != nil {
return err
}
Expand Down

0 comments on commit 6dd2eb9

Please sign in to comment.