From a2a97a1a8df2308e99642f0252df8d5e6875ef4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20B=C4=83ncioiu?= Date: Tue, 13 Aug 2024 10:12:42 +0300 Subject: [PATCH] Handle ESDTNFTCreate, ESDTNFTBurn, ESDTNFTAddQuantity. --- server/services/constants.go | 3 + .../services/transactionEventsController.go | 83 +++++++++++++++++++ server/services/transactionsTransformer.go | 66 +++++++++++++++ 3 files changed, 152 insertions(+) diff --git a/server/services/constants.go b/server/services/constants.go index a12c9650..cf51908a 100644 --- a/server/services/constants.go +++ b/server/services/constants.go @@ -26,6 +26,9 @@ var ( transactionEventSignalError = core.SignalErrorOperation transactionEventESDTTransfer = "ESDTTransfer" transactionEventESDTNFTTransfer = "ESDTNFTTransfer" + transactionEventESDTNFTCreate = "ESDTNFTCreate" + transactionEventESDTNFTBurn = "ESDTNFTBurn" + transactionEventESDTNFTAddQuantity = "ESDTNFTAddQuantity" transactionEventMultiESDTNFTTransfer = "MultiESDTNFTTransfer" transactionEventESDTLocalBurn = "ESDTLocalBurn" transactionEventESDTLocalMint = "ESDTLocalMint" diff --git a/server/services/transactionEventsController.go b/server/services/transactionEventsController.go index 6609e8dd..5831631e 100644 --- a/server/services/transactionEventsController.go +++ b/server/services/transactionEventsController.go @@ -196,6 +196,89 @@ func (controller *transactionEventsController) extractEventsESDTWipe(tx *transac return typedEvents, nil } +func (controller *transactionEventsController) extractEventsESDTNFTCreate(tx *transaction.ApiTransactionResult) ([]*eventESDT, error) { + rawEvents := controller.findManyEventsByIdentifier(tx, transactionEventESDTNFTCreate) + typedEvents := make([]*eventESDT, 0, len(rawEvents)) + + for _, event := range rawEvents { + numTopics := len(event.Topics) + if numTopics != 4 { + return nil, fmt.Errorf("%w: bad number of topics for %s event = %d", errCannotRecognizeEvent, transactionEventESDTNFTCreate, numTopics) + } + + identifier := event.Topics[0] + nonceAsBytes := event.Topics[1] + valueBytes := event.Topics[2] + // We ignore the 4th topic. + + value := big.NewInt(0).SetBytes(valueBytes) + + typedEvents = append(typedEvents, &eventESDT{ + otherAddress: event.Address, + identifier: string(identifier), + nonceAsBytes: nonceAsBytes, + value: value.String(), + }) + } + + return typedEvents, nil +} + +func (controller *transactionEventsController) extractEventsESDTNFTBurn(tx *transaction.ApiTransactionResult) ([]*eventESDT, error) { + rawEvents := controller.findManyEventsByIdentifier(tx, transactionEventESDTNFTBurn) + typedEvents := make([]*eventESDT, 0, len(rawEvents)) + + for _, event := range rawEvents { + numTopics := len(event.Topics) + if numTopics != 3 { + return nil, fmt.Errorf("%w: bad number of topics for %s event = %d", errCannotRecognizeEvent, transactionEventESDTNFTBurn, numTopics) + } + + identifier := event.Topics[0] + nonceAsBytes := event.Topics[1] + valueBytes := event.Topics[2] + + value := big.NewInt(0).SetBytes(valueBytes) + + typedEvents = append(typedEvents, &eventESDT{ + otherAddress: event.Address, + identifier: string(identifier), + nonceAsBytes: nonceAsBytes, + value: value.String(), + }) + } + + return typedEvents, nil +} + +func (controller *transactionEventsController) extractEventsESDTNFTAddQuantity(tx *transaction.ApiTransactionResult) ([]*eventESDT, error) { + rawEvents := controller.findManyEventsByIdentifier(tx, transactionEventESDTNFTAddQuantity) + typedEvents := make([]*eventESDT, 0, len(rawEvents)) + + for _, event := range rawEvents { + numTopics := len(event.Topics) + if numTopics != 3 { + return nil, fmt.Errorf("%w: bad number of topics for %s event = %d", errCannotRecognizeEvent, transactionEventESDTNFTAddQuantity, numTopics) + } + + identifier := event.Topics[0] + nonceAsBytes := event.Topics[1] + valueBytes := event.Topics[2] + + value := big.NewInt(0).SetBytes(valueBytes) + + typedEvents = append(typedEvents, &eventESDT{ + otherAddress: event.Address, + identifier: string(identifier), + nonceAsBytes: nonceAsBytes, + value: value.String(), + }) + } + + return typedEvents, nil +} + + func (controller *transactionEventsController) findManyEventsByIdentifier(tx *transaction.ApiTransactionResult, identifier string) []*transaction.Events { events := make([]*transaction.Events, 0) diff --git a/server/services/transactionsTransformer.go b/server/services/transactionsTransformer.go index 56ac253c..06cde6be 100644 --- a/server/services/transactionsTransformer.go +++ b/server/services/transactionsTransformer.go @@ -343,6 +343,21 @@ func (transformer *transactionsTransformer) addOperationsGivenTransactionEvents( return err } + eventsESDTNFTCreate, err := transformer.eventsController.extractEventsESDTNFTCreate(tx) + if err != nil { + return err + } + + eventsESDTNFTBurn, err := transformer.eventsController.extractEventsESDTNFTBurn(tx) + if err != nil { + return err + } + + eventsESDTNFTAddQuantity, err := transformer.eventsController.extractEventsESDTNFTAddQuantity(tx) + if err != nil { + return err + } + for _, event := range eventsESDTTransfer { if !transformer.provider.HasCustomCurrency(event.identifier) { // We are only emitting balance-changing operations for supported currencies. @@ -416,5 +431,56 @@ func (transformer *transactionsTransformer) addOperationsGivenTransactionEvents( rosettaTx.Operations = append(rosettaTx.Operations, operations...) } + for _, event := range eventsESDTNFTCreate { + if !transformer.provider.HasCustomCurrency(event.identifier) { + // We are only emitting balance-changing operations for supported currencies. + continue + } + + operations := []*types.Operation{ + { + Type: opCustomTransfer, + Account: addressToAccountIdentifier(event.otherAddress), + Amount: transformer.extension.valueToCustomAmount(event.value, event.getExtendedIdentifier()), + }, + } + + rosettaTx.Operations = append(rosettaTx.Operations, operations...) + } + + for _, event := range eventsESDTNFTBurn { + if !transformer.provider.HasCustomCurrency(event.identifier) { + // We are only emitting balance-changing operations for supported currencies. + continue + } + + operations := []*types.Operation{ + { + Type: opCustomTransfer, + Account: addressToAccountIdentifier(event.otherAddress), + Amount: transformer.extension.valueToCustomAmount("-"+event.value, event.getExtendedIdentifier()), + }, + } + + rosettaTx.Operations = append(rosettaTx.Operations, operations...) + } + + for _, event := range eventsESDTNFTAddQuantity { + if !transformer.provider.HasCustomCurrency(event.identifier) { + // We are only emitting balance-changing operations for supported currencies. + continue + } + + operations := []*types.Operation{ + { + Type: opCustomTransfer, + Account: addressToAccountIdentifier(event.otherAddress), + Amount: transformer.extension.valueToCustomAmount(event.value, event.getExtendedIdentifier()), + }, + } + + rosettaTx.Operations = append(rosettaTx.Operations, operations...) + } + return nil }