From f1444e733c8a3900000cf66f6cff639831e1aca8 Mon Sep 17 00:00:00 2001 From: WaDadidou Date: Tue, 2 Jul 2024 16:17:16 -0400 Subject: [PATCH] wip: in launchpad service: add sorts and LaunchpadProjects endpoint --- api/launchpad/v1/launchpad.proto | 45 ++++++++++++++++-- go/pkg/launchpad/service.go | 81 +++++++++++++++++++++++++++++++- 2 files changed, 121 insertions(+), 5 deletions(-) diff --git a/api/launchpad/v1/launchpad.proto b/api/launchpad/v1/launchpad.proto index 37c1632074..6c378cb155 100644 --- a/api/launchpad/v1/launchpad.proto +++ b/api/launchpad/v1/launchpad.proto @@ -6,22 +6,50 @@ option go_package = "./launchpadpb"; service LaunchpadService { rpc UploadMetadatas(UploadMetadatasRequest) returns (UploadMetadatasResponse); rpc CalculateCollectionMerkleRoot(CalculateCollectionMerkleRootRequest) returns (CalculateCollectionMerkleRootResponse); - rpc TokenMetadata(TokenMetadataRequest) returns (TokenMetadataResponse); - rpc CollectionsByCreator(CollectionsByCreatorRequest) returns (CollectionsByCreatorResponse); - // rpc UpdateCollectionWhitelists(UpdateCollectionWhitelistsRequest) returns(UpdateCollectionWhitelistsResponse); - // rpc WhitelistedAddressMerkleInfo(WhitelistedAddressMerkleInfoRequest) returns (WhitelistedAddressMerkleInfoResponse); + rpc LaunchpadProjects(LaunchpadProjectsRequest) returns (LaunchpadProjectsResponse); +} + +enum Sort { + SORT_UNSPECIFIED = 0; + SORT_COLLECTION_NAME = 1; + SORT_STATUS = 2; + SORT_CREATED_AT = 3; +} + +enum SortDirection { + SORT_DIRECTION_UNSPECIFIED = 0; + SORT_DIRECTION_ASCENDING = 1; + SORT_DIRECTION_DESCENDING = 2; } message CollectionsByCreatorRequest { string creator_id = 1; + string network_id = 2; + int32 limit = 3; + int32 offset = 4; + Sort sort = 5; + SortDirection sort_direction = 6; } message CollectionsByCreatorResponse { repeated string collections = 1; } +message LaunchpadProjectsRequest { + string creator_id = 1; + string network_id = 2; + int32 limit = 3; + int32 offset = 4; + Sort sort = 5; + SortDirection sort_direction = 6; +} + +message LaunchpadProjectsResponse { + repeated LaunchpadProject projects = 1; +} + message UploadMetadatasRequest { string sender = 1; string network_id = 2; @@ -56,6 +84,15 @@ message TokenMetadataResponse { repeated string merkle_proof = 3; } +message LaunchpadProject { + string id = 1; + string network_id = 2; + string collection_name = 3; + string creator_id = 4; + string collection_data = 5; + optional string merkle_root = 6; +} + // message UpdateCollectionWhitelistsRequest { // string sender = 1; // string network_id = 2; diff --git a/go/pkg/launchpad/service.go b/go/pkg/launchpad/service.go index dd99d2cfb3..8549ff1636 100644 --- a/go/pkg/launchpad/service.go +++ b/go/pkg/launchpad/service.go @@ -209,8 +209,31 @@ func (s *Launchpad) TokenMetadata(ctx context.Context, req *launchpadpb.TokenMet } func (s *Launchpad) CollectionsByCreator(ctx context.Context, req *launchpadpb.CollectionsByCreatorRequest) (*launchpadpb.CollectionsByCreatorResponse, error) { - creatorID := req.GetCreatorId() + + // TODO: Sort, control creatorID, return CollectionData string + + limit := req.GetLimit() + if limit <= 0 { + return errors.New("limit must be a positive number") + } + + offset := req.GetOffset() + if offset < 0 { + return errors.New("offset must be greater or equal to 0") + } + + networkID := req.GetNetworkId() + if networkID == "" { + return errors.New("missing network id") + } + + network, err := s.conf.NetworkStore.GetNetwork(networkID) + if err != nil { + return errors.Wrap(err, fmt.Sprintf("unknown network id '%s'", networkID)) + } + + creatorID := req.GetCreatorId() if creatorID == "" { return nil, errors.New("creatorID is mandatory") } @@ -220,6 +243,16 @@ func (s *Launchpad) CollectionsByCreator(ctx context.Context, req *launchpadpb.C return nil, errors.Wrap(err, "failed to get collection data") } + orderDirection := "" + switch req.GetSortDirection() { + case launchpadpb.SortDirection_SORT_DIRECTION_UNSPECIFIED: + orderDirection = "" + case launchpadpb.SortDirection_SORT_DIRECTION_ASCENDING: + orderDirection = " ASC " + case launchpadpb.SortDirection_SORT_DIRECTION_DESCENDING: + orderDirection = " DESC " + } + res := make([]string, len(projects)) for idx, pj := range projects { res[idx] = pj.CollectionData.String() @@ -229,3 +262,49 @@ func (s *Launchpad) CollectionsByCreator(ctx context.Context, req *launchpadpb.C Collections: res, }, nil } + +func (s *Launchpad) LaunchpadProjects(ctx context.Context, req *launchpadpb.CollectionsRequest) (*launchpadpb.CollectionsResponse, error) { + + + // TODO: Sort, control daoID, return LaunchpadProject[] + + limit := req.GetLimit() + if limit <= 0 { + return errors.New("limit must be a positive number") + } + + offset := req.GetOffset() + if offset < 0 { + return errors.New("offset must be greater or equal to 0") + } + + networkID := req.GetNetworkId() + if networkID == "" { + return errors.New("missing network id") + } + + network, err := s.conf.NetworkStore.GetNetwork(networkID) + if err != nil { + return errors.Wrap(err, fmt.Sprintf("unknown network id '%s'", networkID)) + } + + daoID := req.GetCreatorId() + + if creatorID == "" { + return nil, errors.New("creatorID is mandatory") + } + + var projects []indexerdb.Collections + if err := s.conf.IndexerDB.Find(&projects, "creator_id = ?", creatorID).Error; err != nil { + return nil, errors.Wrap(err, "failed to get collection data") + } + + res := make([]string, len(projects)) + for idx, pj := range projects { + res[idx] = pj.CollectionData.String() + } + + return &launchpadpb.CollectionsResponse{ + Collections: res, + }, nil +}