Skip to content

Commit

Permalink
split pair into two
Browse files Browse the repository at this point in the history
  • Loading branch information
Vritra4 committed Apr 30, 2024
1 parent 0655579 commit ad498d4
Show file tree
Hide file tree
Showing 28 changed files with 1,828 additions and 116 deletions.
4 changes: 3 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use (
.
./submodules/block
./submodules/move-nft
./submodules/pair
./submodules/move-pair
./submodules/wasm-nft
./submodules/wasm-pair
./submodules/tx
)
18 changes: 12 additions & 6 deletions nft/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 24 additions & 25 deletions submodules/pair/types/query.pb.go → pair/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
12 changes: 6 additions & 6 deletions submodules/pair/types/types.pb.go → pair/types/types.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion proto/indexer/pair/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "google/api/annotations.proto";
import "cosmos_proto/cosmos.proto";
import "indexer/pair/v1/types.proto";

option go_package = "github.com/initia-labs/kvindexer/submodules/pair/types";
option go_package = "github.com/initia-labs/kvindexer/pair/types";

// Query provides the service definition for the Token Pairs
service Query {
Expand Down
2 changes: 1 addition & 1 deletion proto/indexer/pair/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package indexer.pair.v1;
import "gogoproto/gogo.proto";
import "cosmos_proto/cosmos.proto";

option go_package = "github.com/initia-labs/kvindexer/submodules/pair/types";
option go_package = "github.com/initia-labs/kvindexer/pair/types";
option (gogoproto.goproto_getters_all) = false;
option (gogoproto.equal_all) = true;

Expand Down
3 changes: 2 additions & 1 deletion submodules/block/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion submodules/block/types/types.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

94 changes: 29 additions & 65 deletions submodules/pair/collect.go → submodules/move-pair/collect.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pair
package move_pair

import (
"context"
Expand All @@ -18,11 +18,12 @@ import (
ibctm "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"

opchildtypes "github.com/initia-labs/OPinit/x/opchild/types"
"github.com/initia-labs/kvindexer/submodules/pair/types"
"github.com/initia-labs/kvindexer/submodules/move_pair/types"
)

const (
ibcTransferPort = "transfer"
ibcTransferPort = "transfer"
ibcNftTransferPort = "nft-transfer"
)

func (sm PairSubmodule) finalizeBlock(ctx context.Context, req abci.RequestFinalizeBlock, res abci.ResponseFinalizeBlock) error {
Expand Down Expand Up @@ -79,25 +80,18 @@ func (sm PairSubmodule) collectIBCNonfungibleTokens(ctx context.Context, txResul
var packetData, classId string

for _, event := range txResult.Events {
switch event.Type {
case "recv_packet":
if event.Type == "recv_packet" {
if sm.pickAttribute(event.Attributes, "packet_src_port") != ibcNftTransferPort {
continue
}
packetData = sm.pickAttribute(event.Attributes, "packet_data")
case "class_trace":
classId = sm.pickAttribute(event.Attributes, "class_id")
packetDstPort := sm.pickAttribute(event.Attributes, "packet_dst_port")
packetDstChannel := sm.pickAttribute(event.Attributes, "packet_dst_channel")
err = sm.pricessIbcNftPairEvent(ctx, packetData, packetDstPort, packetDstChannel)
if err != nil {
sm.Logger(ctx).Warn("failed to handle class_trace event", "error", err, "event", event)
sm.Logger(ctx).Warn("failed to handle recv_packet event", "error", err, "recv_packet.packet_data", packetData, "class_trace.class_id", classId)
}
}
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
Expand All @@ -112,8 +106,12 @@ func (sm PairSubmodule) pickAttribute(attrs []abci.EventAttribute, key string) s
return ""
}

func (sm PairSubmodule) pricessPairEvent(ctx context.Context, packetDataStr, classId string) (err error) {
sm.Logger(ctx).Debug("processPairEvent", "packet_data", packetDataStr, "class_id", classId)
func (sm PairSubmodule) generateCw721FromIcs721PortInfo(port, channel string) string {
return port + "/" + channel
}

func (sm PairSubmodule) pricessIbcNftPairEvent(ctx context.Context, packetDataStr, packetDstPort, packetDstChannel string) (err error) {
sm.Logger(ctx).Debug("processPairEvent", "packet_data", packetDataStr, "port", packetDstPort, "channel", packetDstChannel)

packetData := types.PacketData{}
if err = json.Unmarshal([]byte(packetDataStr), &packetData); err != nil {
Expand All @@ -130,72 +128,38 @@ func (sm PairSubmodule) pricessPairEvent(ctx context.Context, packetDataStr, cla
return cosmoserr.Wrap(err, "failed to unmarshal class data")
}

_, err = sm.GetPair(ctx, false, classId)
collectionName := fmt.Sprintf("%s/%s", sm.generateCw721FromIcs721PortInfo(packetDstPort, packetDstChannel), packetData.ClassId)

_, err = sm.GetPair(ctx, false, collectionName)
if err == nil {
return nil // already exists
}
if !cosmoserr.IsOf(err, collections.ErrNotFound) {
return cosmoserr.Wrap(err, "failed to check class existence")
}

err = sm.SetPair(ctx, false, false, classId, classData.Name)
err = sm.SetPair(ctx, false, false, collectionName, classData.Name)
if err != nil {
return cosmoserr.Wrap(err, "failed to set class")
}

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

func (sm PairSubmodule) collectIBCFungibleTokens(ctx context.Context) error {

sdkCtx := sdk.UnwrapSDKContext(ctx)
chKeeper := sm.channelKeeper

// get all channels
channels := chKeeper.GetAllChannels(sdkCtx)

ibcChannels := []string{}
for _, channel := range channels {
if channel.PortId != ibcTransferPort {
traces := sm.transferKeeper.GetAllDenomTraces(sdkCtx)
for _, trace := range traces {
_, err := sm.fungiblePairsMap.Get(ctx, trace.IBCDenom())
if err != nil && !cosmoserr.IsOf(err, collections.ErrNotFound) {
continue
}

_ /*clientId*/, cs, err := chKeeper.GetChannelClientState(sdkCtx, channel.PortId, channel.ChannelId)
err = sm.fungiblePairsMap.Set(ctx, trace.IBCDenom(), trace.BaseDenom)
if err != nil {
sm.Logger(ctx).Warn("GetChannelClientState", "error", err)
}
counterpartyChainId := getChainIdFromClientState(cs)
if counterpartyChainId == "" {
sm.Logger(ctx).Warn("channel id is nil")
continue
}
if counterpartyChainId != sdk.UnwrapSDKContext(ctx).ChainID() {
continue
}
ibcChannels = append(ibcChannels, channel.ChannelId)
}

traces := sm.transferKeeper.GetAllDenomTraces(sdkCtx)
for _, ibcChannel := range ibcChannels {
for _, trace := range traces {
if trace.Path != fmt.Sprintf("%s/%s", ibcTransferPort, ibcChannel) {
continue
}

prevDenom, err := sm.fungiblePairsMap.Get(ctx, trace.IBCDenom())
if err != nil && !cosmoserr.IsOf(err, collections.ErrNotFound) {
continue
}
// prevDenom should be empty string if not found, or already set
if prevDenom == trace.BaseDenom {
continue
}

err = sm.fungiblePairsMap.Set(ctx, trace.IBCDenom(), trace.BaseDenom)
if err != nil {
return err
}
return err
}
}

Expand Down
2 changes: 1 addition & 1 deletion submodules/pair/go.mod → submodules/move-pair/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/initia-labs/kvindexer/submodules/pair
module github.com/initia-labs/kvindexer/submodules/move_pair

go 1.22

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package pair
package move_pair

import (
"context"

"cosmossdk.io/collections"
"github.com/cosmos/cosmos-sdk/types/query"
"github.com/initia-labs/kvindexer/submodules/pair/types"
"github.com/initia-labs/kvindexer/pair/types"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pair
package move_pair

import (
"context"
Expand Down
Loading

0 comments on commit ad498d4

Please sign in to comment.