Skip to content

Commit

Permalink
fix: in launchpad service: add sorts and LaunchpadProjects endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
WaDadidou committed Jul 3, 2024
1 parent 03a8aa4 commit c176bd2
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 74 deletions.
12 changes: 6 additions & 6 deletions api/launchpad/v1/launchpad.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ message CollectionsByCreatorResponse {
}

message LaunchpadProjectsRequest {
string creator_id = 1;
string network_id = 2;
int32 limit = 3;
int32 offset = 4;
Sort sort = 5;
SortDirection sort_direction = 6;
string network_id = 1;
int32 limit = 2;
int32 offset = 3;
Sort sort = 4;
SortDirection sort_direction = 5;
// TODO: user authentication (Member of the admin DAO)
}

message LaunchpadProjectsResponse {
Expand Down
172 changes: 104 additions & 68 deletions go/pkg/launchpad/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,24 +210,24 @@ func (s *Launchpad) TokenMetadata(ctx context.Context, req *launchpadpb.TokenMet

func (s *Launchpad) CollectionsByCreator(ctx context.Context, req *launchpadpb.CollectionsByCreatorRequest) (*launchpadpb.CollectionsByCreatorResponse, error) {
limit := req.GetLimit()
if limit <= 0 {
return errors.New("limit must be a positive number")
}
if limit <= 0 {
return nil, 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")
}
offset := req.GetOffset()
if offset < 0 {
return nil, errors.New("offset must be greater or equal to 0")
}

networkID := req.GetNetworkId()
if networkID == "" {
return errors.New("missing network id")
}
networkID := req.GetNetworkId()
if networkID == "" {
return nil, 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))
}
_, err := s.conf.NetworkStore.GetNetwork(networkID)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("unknown network id '%s'", networkID))
}

creatorID := req.GetCreatorId()
if creatorID == "" {
Expand All @@ -236,86 +236,122 @@ func (s *Launchpad) CollectionsByCreator(ctx context.Context, req *launchpadpb.C

var projects []indexerdb.LaunchpadProject
if err := s.conf.IndexerDB.Find(&projects, "creator_id = ?", creatorID).Error; err != nil {
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 "
}
orderSQL := ""
switch req.GetSort() {
case launchpadpb.Sort_SORT_COLLECTION_NAME:
orderSQL = "lp.collection_name" + orderDirection
case marketplacepb.Sort_SORT_STATUS:
orderSQL = "case when total_volume is null then 1 else 0 end, total_volume " + orderDirection
case marketplacepb.Sort_SORT_CREATED_AT:
orderSQL = "lp.time " + orderDirection


case marketplacepb.Sort_SORT_VOLUME_USD:
orderSQL = "case when total_volume_usd is null then 1 else 0 end, total_volume_usd " + orderDirection
case marketplacepb.Sort_SORT_UNSPECIFIED:
orderSQL = "volume DESC"
}

res := make([]string, len(projects))
return nil, errors.Wrap(err, fmt.Sprintf("unknown creator id '%s'", creatorID))
}

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 "
}
orderSQL := ""
switch req.GetSort() {
case launchpadpb.Sort_SORT_COLLECTION_NAME:
orderSQL = "lp.collection_name" + orderDirection
}

err = s.conf.IndexerDB.Raw(`SELECT collection_data from launchpad_projects ORDER BY %s`,
orderSQL, // ORDER BY here or it won't work
).Scan(&projects).Error
if err != nil {
return nil, errors.Wrap(err, "failed to query database")
}

result := make([]string, len(projects))
for idx, pj := range projects {
res[idx] = pj.CollectionData.String()
result[idx] = string(pj.CollectionData)
}

return &launchpadpb.CollectionsByCreatorResponse{
Collections: res,
Collections: result,
}, nil
}

func (s *Launchpad) LaunchpadProjects(ctx context.Context, req *launchpadpb.CollectionsRequest) (*launchpadpb.CollectionsResponse, error) {


// TODO: Sort, control daoID, return LaunchpadProject[]

func (s *Launchpad) LaunchpadProjects(ctx context.Context, req *launchpadpb.LaunchpadProjectsRequest) (*launchpadpb.LaunchpadProjectsResponse, error) {
limit := req.GetLimit()
if limit <= 0 {
return errors.New("limit must be a positive number")
return nil, 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")
return nil, errors.New("offset must be greater or equal to 0")
}

networkID := req.GetNetworkId()
if networkID == "" {
return errors.New("missing network id")
return nil, errors.New("missing network id")
}

network, err := s.conf.NetworkStore.GetNetwork(networkID)
_, err := s.conf.NetworkStore.GetNetwork(networkID)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("unknown network id '%s'", networkID))
return nil, errors.Wrap(err, fmt.Sprintf("unknown network id '%s'", networkID))
}

daoID := req.GetCreatorId()

if creatorID == "" {
return nil, errors.New("creatorID is mandatory")
// TODO: user authentication (Member of the admin DAO)
// Control if sender is member of the admin DAO
// var isUserAuthorized bool

// userAddress, err := s.authenticate(s.db, req.GetAuthToken())
// if err != nil {
// return nil, errors.Wrap(err, "failed to authenticate")
// }
// err = s.conf.IndexerDB.Raw(`
// SELECT EXISTS (
// SELECT 1
// FROM dao_members dm
// JOIN daos d ON dm.dao_contract_address = d.contract_address
// WHERE dm.member_address = ?
// ) AS dao_exists;
// `,
// userAddress,
// ).Scan(&isUserAuthorized).Error
// if err != nil {
// return errors.Wrap(err, "failed to query database")
// }
// if !isUserAuthorized {
// return errors.New("Unauthorized")
// }

var projects []launchpadpb.LaunchpadProject
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 "
}
orderSQL := ""
switch req.GetSort() {
case launchpadpb.Sort_SORT_COLLECTION_NAME:
orderSQL = "lp.collection_name" + orderDirection
}

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")
err = s.conf.IndexerDB.Raw(`SELECT * from launchpad_projects ORDER BY %s`,
orderSQL, // ORDER BY here or it won't work
).Scan(&projects).Error
if err != nil {
return nil, errors.Wrap(err, "failed to query database")
}

res := make([]string, len(projects))
for idx, pj := range projects {
res[idx] = pj.CollectionData.String()
result := make([]*launchpadpb.LaunchpadProject, len(projects))
for idx, dbProject := range projects {
result[idx] = &launchpadpb.LaunchpadProject{
Id: dbProject.Id,
NetworkId: dbProject.NetworkId,
CollectionName: dbProject.CollectionName,
CreatorId: dbProject.CreatorId,
CollectionData: dbProject.CollectionData,
}
}

return &launchpadpb.CollectionsResponse{
Collections: res,
return &launchpadpb.LaunchpadProjectsResponse{
Projects: result,
}, nil
}

0 comments on commit c176bd2

Please sign in to comment.