Skip to content

Commit

Permalink
Handle ESDTNFTCreate, ESDTNFTBurn, ESDTNFTAddQuantity.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Aug 13, 2024
1 parent 956de52 commit a2a97a1
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
3 changes: 3 additions & 0 deletions server/services/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ var (
transactionEventSignalError = core.SignalErrorOperation
transactionEventESDTTransfer = "ESDTTransfer"
transactionEventESDTNFTTransfer = "ESDTNFTTransfer"
transactionEventESDTNFTCreate = "ESDTNFTCreate"
transactionEventESDTNFTBurn = "ESDTNFTBurn"
transactionEventESDTNFTAddQuantity = "ESDTNFTAddQuantity"
transactionEventMultiESDTNFTTransfer = "MultiESDTNFTTransfer"
transactionEventESDTLocalBurn = "ESDTLocalBurn"
transactionEventESDTLocalMint = "ESDTLocalMint"
Expand Down
83 changes: 83 additions & 0 deletions server/services/transactionEventsController.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
66 changes: 66 additions & 0 deletions server/services/transactionsTransformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}

0 comments on commit a2a97a1

Please sign in to comment.