Skip to content

Commit

Permalink
fix pair to collect name instead of description
Browse files Browse the repository at this point in the history
  • Loading branch information
Vritra4 committed Apr 25, 2024
1 parent a0bb57e commit 1aadbcf
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 63 deletions.
137 changes: 78 additions & 59 deletions submodules/pair/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"

channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
exportedibc "github.com/cosmos/ibc-go/v8/modules/core/exported"
ibctm "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"

Expand All @@ -27,17 +28,30 @@ const (
func (sm PairSubmodule) finalizeBlock(ctx context.Context, req abci.RequestFinalizeBlock, res abci.ResponseFinalizeBlock) error {
sm.Logger(ctx).Debug("finalizeBlock", "submodule", types.SubmoduleName, "txs", len(req.Txs), "height", req.Height)

if err := sm.collectOPfungibleTokens(ctx, req); err != nil {
sm.Logger(ctx).Warn("collectOPfungibleTokens", "error", err, "submodule", types.SubmoduleName)
}

if err := sm.collectIBCFungibleTokens(ctx); err != nil {
// don't return error
sm.Logger(ctx).Warn("collectIBCFungibleTokens", "error", err, "submodule", types.SubmoduleName)
}

if err := sm.collectIBCNonfungibleTokens(ctx, res); err != nil {
sm.Logger(ctx).Warn("collectIBCNonfungibleTokens", "error", err, "submodule", types.SubmoduleName)
for txIdx, txBytes := range req.Txs {
tx, err := sm.parseTx(txBytes)
if err != nil {
return err
}
for _, msg := range tx.GetMsgs() {
switch msg := msg.(type) {
case *opchildtypes.MsgFinalizeTokenDeposit:
err = sm.collectOPfungibleTokens(ctx, msg)
if err != nil {
sm.Logger(ctx).Warn("collectOPfungibleTokens", "error", err)
}
case *channeltypes.MsgRecvPacket:
err = sm.collectIBCNonfungibleTokens(ctx, res.TxResults[txIdx])
if err != nil {
sm.Logger(ctx).Warn("collectIBCNonfungibleTokens", "error", err)
}
}
}
}

return nil
Expand All @@ -53,78 +67,83 @@ func (sm PairSubmodule) parseTx(txBytes []byte) (*tx.Tx, error) {
return &tx, nil
}

func (sm PairSubmodule) collectOPfungibleTokens(ctx context.Context, req abci.RequestFinalizeBlock) (err error) {
for _, txBytes := range req.Txs {
tx, err := sm.parseTx(txBytes)
if err != nil {
return err
}
for _, msg := range tx.GetMsgs() {
targetMsg, ok := msg.(*opchildtypes.MsgFinalizeTokenDeposit)
if !ok {
continue
}
err := sm.SetPair(ctx, false, true, targetMsg.Amount.Denom, targetMsg.BaseDenom)
if err != nil {
sm.Logger(ctx).Warn("SetPair", "error", err, "denom", targetMsg.Amount.Denom, "baseDenom", targetMsg.BaseDenom)
}
}
func (sm PairSubmodule) collectOPfungibleTokens(ctx context.Context, msg *opchildtypes.MsgFinalizeTokenDeposit) (err error) {
err = sm.SetPair(ctx, false, true, msg.Amount.Denom, msg.BaseDenom)
if err != nil {
sm.Logger(ctx).Warn("SetPair", "error", err, "denom", msg.Amount.Denom, "baseDenom", msg.BaseDenom)
}
return nil
}

func (sm PairSubmodule) collectIBCNonfungibleTokens(ctx context.Context, res abci.ResponseFinalizeBlock) (err error) {
for _, txResult := range res.TxResults {
for _, event := range txResult.Events {
if event.Type != "write_acknowledgement" {
continue
}
err := sm.handleWriteAcknowledgementEvent(ctx, event.Attributes)
func (sm PairSubmodule) collectIBCNonfungibleTokens(ctx context.Context, txResult *abci.ExecTxResult) (err error) {
var packetData, classId string

for _, event := range txResult.Events {
switch event.Type {
case "recv_packet":
packetData = sm.pickAttribute(event.Attributes, "packet_data")
case "class_trace":
classId = sm.pickAttribute(event.Attributes, "class_id")
if err != nil {
sm.Logger(ctx).Warn("failed to handle write_acknowledgement event", "error", err, "event", event)
sm.Logger(ctx).Warn("failed to handle class_trace event", "error", err, "event", event)
}
}
if packetData != "" && classId != "" {
break
}
}
if packetData == "" || classId == "" {
return nil
}
err = sm.pricessPairEvent(ctx, packetData, classId)
if err != nil {
sm.Logger(ctx).Warn("failed to handle recv_packet event", "error", err, "recv_packet.packet_data", packetData, "class_trace.class_id", classId)
}

return nil
}

func (sm PairSubmodule) handleWriteAcknowledgementEvent(ctx context.Context, attrs []abci.EventAttribute) (err error) {
sm.Logger(ctx).Debug("write-ack", "attrs", attrs)
func (sm PairSubmodule) pickAttribute(attrs []abci.EventAttribute, key string) string {
for _, attr := range attrs {
if attr.Key != "packet_data" {
continue
if attr.Key == key {
return attr.Value
}
}
return ""
}

data := types.WriteAckForNftEvent{}
if err = json.Unmarshal([]byte(attr.Value), &data); err != nil {
// may be not target
return nil
}
func (sm PairSubmodule) pricessPairEvent(ctx context.Context, packetDataStr, classId string) (err error) {
sm.Logger(ctx).Debug("processPairEvent", "packet_data", packetDataStr, "class_id", classId)

cdb, err := base64.StdEncoding.DecodeString(data.ClassData)
if err != nil {
return cosmoserr.Wrap(err, "failed to decode class data")
}
classData := types.NftClassData{}
if err = json.Unmarshal(cdb, &classData); err != nil {
return cosmoserr.Wrap(err, "failed to unmarshal class data")
}
packetData := types.PacketData{}
if err = json.Unmarshal([]byte(packetDataStr), &packetData); err != nil {
// may be not target
return nil
}

_, err = sm.GetPair(ctx, false, data.ClassId)
if err == nil {
return nil // already exists
}
if !cosmoserr.IsOf(err, collections.ErrNotFound) {
return cosmoserr.Wrap(err, "failed to check class existence")
}
cdb, err := base64.StdEncoding.DecodeString(packetData.ClassData)
if err != nil {
return cosmoserr.Wrap(err, "failed to decode class data")
}
classData := types.NftClassData{}
if err = json.Unmarshal(cdb, &classData); err != nil {
return cosmoserr.Wrap(err, "failed to unmarshal class data")
}

err = sm.SetPair(ctx, false, false, data.ClassId, classData.Description.Value)
if err != nil {
return cosmoserr.Wrap(err, "failed to set class")
}
_, err = sm.GetPair(ctx, false, classId)
if err == nil {
return nil // already exists
}
if !cosmoserr.IsOf(err, collections.ErrNotFound) {
return cosmoserr.Wrap(err, "failed to check class existence")
}

sm.Logger(ctx).Info("nft class added", "classId", data.ClassId, "description", classData.Description.Value)
err = sm.SetPair(ctx, false, false, classId, classData.Name)
if err != nil {
return cosmoserr.Wrap(err, "failed to set class")
}

sm.Logger(ctx).Info("nft class added", "classId", classId, "classData", classData)
return nil
}

Expand Down
13 changes: 9 additions & 4 deletions submodules/pair/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ type MoveResource struct {
}

type NftClassData struct {
Description struct {
Value string `json:"value"`
} `json:"initia:description"`
Description string `json:"description"`
Name string `json:"name"`
}

type WriteAckForNftEvent struct {
type PacketData struct {
ClassData string `json:"class_data"`
ClassId string `json:"class_id"`
ClassUri string `json:"class_uri"`
Expand All @@ -57,3 +56,9 @@ type WriteAckForNftEvent struct {
//TokenIds []string `json:"tokenIds"`
//TokenUris []string `json:"tokenUris"`
}

type ClassTrace struct {
TraceHash string `json:"trace_hash"`
ClassId string `json:"class_id"`
//MsgIndex int `json:"msg_index"`
}

0 comments on commit 1aadbcf

Please sign in to comment.