Skip to content

Commit

Permalink
wip: launchpad collection status handling
Browse files Browse the repository at this point in the history
  • Loading branch information
WaDadidou committed Jul 11, 2024
1 parent c98b8d5 commit 08de738
Show file tree
Hide file tree
Showing 6 changed files with 384 additions and 351 deletions.
11 changes: 5 additions & 6 deletions api/launchpad/v1/launchpad.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ service LaunchpadService {
rpc CollectionsByCreator(CollectionsByCreatorRequest) returns (CollectionsByCreatorResponse);
rpc LaunchpadProjects(LaunchpadProjectsRequest) returns (LaunchpadProjectsResponse);
rpc LaunchpadProjectById(LaunchpadProjectByIdRequest) returns (LaunchpadProjectByIdResponse);
rpc LaunchpadProjectsCountByStatus(LaunchpadProjectsCountByStatusRequest) returns (LaunchpadProjectsCountByStatusResponse);
rpc LaunchpadProjectsCount(LaunchpadProjectsCountRequest) returns (LaunchpadProjectsCountResponse);
}

enum Sort {
Expand Down Expand Up @@ -52,7 +52,7 @@ message LaunchpadProjectsRequest {
int32 offset = 3;
Sort sort = 4;
SortDirection sort_direction = 5;
// TODO: user authentication (Member of the admin DAO) using a token
// TODO: user authentication (Member of the admin DAO) using a token
string user_address = 6;
optional Status status = 7;
}
Expand Down Expand Up @@ -106,14 +106,14 @@ message TokenMetadataResponse {
repeated string merkle_proof = 3;
}

message LaunchpadProjectsCountByStatusRequest {
message LaunchpadProjectsCountRequest {
// TODO: user authentication (Member of the admin DAO) using a token
string user_address = 1;
string network_id = 2;
Status status = 3;
optional Status status = 3;
}

message LaunchpadProjectsCountByStatusResponse {
message LaunchpadProjectsCountResponse {
uint32 count = 1;
}

Expand All @@ -125,7 +125,6 @@ message LaunchpadProject {
string creator_id = 3;
string collection_data = 4;
optional string merkle_root = 5;
Status status = 6;
}

message Metadata {
Expand Down
168 changes: 118 additions & 50 deletions go/pkg/launchpad/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ func (s *Launchpad) CollectionsByCreator(ctx context.Context, req *launchpadpb.C
if networkID == "" {
return nil, errors.New("missing network id")
}

_, err := s.conf.NetworkStore.GetNetwork(networkID)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("unknown network id '%s'", networkID))
Expand All @@ -234,6 +233,20 @@ func (s *Launchpad) CollectionsByCreator(ctx context.Context, req *launchpadpb.C
return nil, errors.New("creatorID is mandatory")
}

status := req.GetStatus()
if status < 0 || status > 2 {
return nil, errors.New("invalid status")
}
statusFilterSQL := ""
switch status {
case launchpadpb.Status_STATUS_INCOMPLETE:
statusFilterSQL = "AND lp.collection_data->>'metadatas_merkle_root' ISNULL"
case launchpadpb.Status_STATUS_COMPLETE:
statusFilterSQL = "AND NOT lp.collection_data->>'metadatas_merkle_root' ISNULL"
case launchpadpb.Status_STATUS_DEPLOYED:
statusFilterSQL = "AND NOT lp.collection_data->>'deployed_address' ISNULL"
}

var projects []indexerdb.LaunchpadProject

orderDirection := ""
Expand All @@ -253,11 +266,11 @@ func (s *Launchpad) CollectionsByCreator(ctx context.Context, req *launchpadpb.C
orderSQL = ""
}

err = s.conf.IndexerDB.Raw(fmt.Sprintf(
`
SELECT collection_data FROM launchpad_projects AS lp WHERE lp.creator_id = ? %s
`, orderSQL), creatorID,
).Scan(&projects).Error
err = s.conf.IndexerDB.Raw(fmt.Sprintf("

Check failure on line 269 in go/pkg/launchpad/service.go

View workflow job for this annotation

GitHub Actions / go

newline in string

Check failure on line 269 in go/pkg/launchpad/service.go

View workflow job for this annotation

GitHub Actions / go

syntax error: unexpected newline in argument list; possibly missing comma or )
SELECT collection_data FROM launchpad_projects AS lp WHERE lp.creator_id = ? AND lp.network_id = ? %s %s

Check failure on line 270 in go/pkg/launchpad/service.go

View workflow job for this annotation

GitHub Actions / go

invalid character U+003F '?'
",

Check failure on line 271 in go/pkg/launchpad/service.go

View workflow job for this annotation

GitHub Actions / go

newline in string
statusFilterSQL,
orderSQL), creatorID, networkID).Scan(&projects).Error
if err != nil {
return nil, errors.Wrap(err, "failed to query database")
}
Expand Down Expand Up @@ -287,38 +300,36 @@ func (s *Launchpad) LaunchpadProjects(ctx context.Context, req *launchpadpb.Laun
if networkID == "" {
return nil, errors.New("missing network id")
}
_, err := s.conf.NetworkStore.GetNetwork(networkID)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("unknown network id '%s'", networkID))
}

userAddress := req.GetUserAddress()
if userAddress == "" {
return nil, errors.New("missing user address")
}

_, err := s.conf.NetworkStore.GetNetwork(networkID)
isUserAdmin, err := s.IsUserAdmin(userAddress)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("unknown network id '%s'", networkID))
return nil, errors.Wrap(err, "failed to verify user's authentication")
}
if !isUserAdmin {
return nil, errors.New("Unauthorized")
}

// TODO: user authentication (Member of the admin DAO)
// Control if sender is member of the admin DAO
daoAdminAddress := "tori129kpfu7krgumuc38hfyxwfluq7eu06rhr3awcztr3a9cgjjcx5hswlqj8v"
var isUserAuthorized bool
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 d.contract_address = ?
AND dm.member_address = ?
) AS dao_exists;
`,
daoAdminAddress,
userAddress,
).Scan(&isUserAuthorized).Error
if err != nil {
return nil, errors.Wrap(err, "failed to query database")
status := req.GetStatus()
if status < 0 || status > 2 {
return nil, errors.New("invalid status")
}
if !isUserAuthorized {
return nil, errors.New("Unauthorized")
statusFilterSQL := ""
switch status {
case launchpadpb.Status_STATUS_INCOMPLETE:
statusFilterSQL = "AND lp.collection_data->>'metadatas_merkle_root' ISNULL"
case launchpadpb.Status_STATUS_COMPLETE:
statusFilterSQL = "AND NOT lp.collection_data->>'metadatas_merkle_root' ISNULL"
case launchpadpb.Status_STATUS_DEPLOYED:
statusFilterSQL = "AND NOT lp.collection_data->>'deployed_address' ISNULL"
}

var projects []launchpadpb.LaunchpadProject
Expand All @@ -340,11 +351,11 @@ func (s *Launchpad) LaunchpadProjects(ctx context.Context, req *launchpadpb.Laun
orderSQL = ""
}

err = s.conf.IndexerDB.Raw(fmt.Sprintf(
`
SELECT * FROM launchpad_projects AS lp %s
`, orderSQL),
).Scan(&projects).Error
err = s.conf.IndexerDB.Raw(fmt.Sprintf("

Check failure on line 354 in go/pkg/launchpad/service.go

View workflow job for this annotation

GitHub Actions / go

newline in string

Check failure on line 354 in go/pkg/launchpad/service.go

View workflow job for this annotation

GitHub Actions / go

syntax error: unexpected newline in argument list; possibly missing comma or )
SELECT * FROM launchpad_projects AS lp WHERE lp.network_id = ? %s %s

Check failure on line 355 in go/pkg/launchpad/service.go

View workflow job for this annotation

GitHub Actions / go

invalid character U+003F '?'
",

Check failure on line 356 in go/pkg/launchpad/service.go

View workflow job for this annotation

GitHub Actions / go

newline in string
statusFilterSQL,
orderSQL), networkID).Scan(&projects).Error
if err != nil {
return nil, errors.Wrap(err, "failed to query database")
}
Expand Down Expand Up @@ -385,37 +396,94 @@ func (s *Launchpad) LaunchpadProjectById(ctx context.Context, req *launchpadpb.L
return nil, errors.Wrap(err, fmt.Sprintf("unknown network id '%s'", networkID))
}

isUserAdmin, err := s.IsUserAdmin(userAddress)
if err != nil {
return nil, errors.Wrap(err, "failed to verify user's authentication")
}
if !isUserAdmin {
return nil, errors.New("Unauthorized")
}

var project *launchpadpb.LaunchpadProject

err = s.conf.IndexerDB.Raw("SELECT * FROM launchpad_projects AS lp WHERE lp.project_id = ? AND lp.network_id = ?", projectID, networkID).Scan(&project).Error
if err != nil {
return nil, errors.Wrap(err, "failed to query database")
}

return &launchpadpb.LaunchpadProjectByIdResponse{
Project: project,
}, nil
}

func (s *Launchpad) LaunchpadProjectsCount(ctx context.Context, req *launchpadpb.LaunchpadProjectsCountRequest) (*launchpadpb.LaunchpadProjectsCountResponse, error) {
networkID := req.GetNetworkId()
if networkID == "" {
return nil, errors.New("missing network id")
}

status := req.GetStatus()
if status < 0 || status > 2 {
return nil, errors.New("invalid status")
}
statusFilterSQL := ""
switch status {
case launchpadpb.Status_STATUS_INCOMPLETE:
statusFilterSQL = "AND lp.collection_data->>'metadatas_merkle_root' ISNULL"
case launchpadpb.Status_STATUS_COMPLETE:
statusFilterSQL = "AND NOT lp.collection_data->>'metadatas_merkle_root' ISNULL"
case launchpadpb.Status_STATUS_DEPLOYED:
statusFilterSQL = "AND NOT lp.collection_data->>'deployed_address' ISNULL"
}

userAddress := req.GetUserAddress()
if userAddress == "" {
return nil, errors.New("missing user address")
}

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

isUserAdmin, err := s.IsUserAdmin(userAddress)
if err != nil {
return nil, errors.Wrap(err, "failed to verify user's authentication")
}
if !isUserAdmin {
return nil, errors.New("Unauthorized")
}

var count uint32
err = s.conf.IndexerDB.Raw(fmt.Sprintf("SELECT COUNT(*) FROM launchpad_projects AS lp WHERE lp.network_id = ? %s", statusFilterSQL), networkID).Scan(&count).Error
if err != nil {
return nil, errors.Wrap(err, "failed to query database")
}

return &launchpadpb.LaunchpadProjectsCountByStatusResponse{
Count: count,
}, nil
}

func (s *Launchpad) IsUserAdmin(userAddress string) (bool, error) {
// TODO: user authentication (Member of the admin DAO)
// Control if sender is member of the admin DAO
daoAdminAddress := "tori129kpfu7krgumuc38hfyxwfluq7eu06rhr3awcztr3a9cgjjcx5hswlqj8v"
var isUserAuthorized bool
err = s.conf.IndexerDB.Raw(`
err := s.conf.IndexerDB.Raw("

Check failure on line 473 in go/pkg/launchpad/service.go

View workflow job for this annotation

GitHub Actions / go

newline in string

Check failure on line 473 in go/pkg/launchpad/service.go

View workflow job for this annotation

GitHub Actions / go

syntax error: unexpected newline in argument list; possibly missing comma or )
SELECT EXISTS (
SELECT 1
FROM dao_members dm
JOIN daos d ON dm.dao_contract_address = d.contract_address
WHERE d.contract_address = ?
AND dm.member_address = ?
) AS dao_exists;
`,
",
daoAdminAddress,
userAddress,
).Scan(&isUserAuthorized).Error
if err != nil {
return nil, errors.Wrap(err, "failed to query database")
}
if !isUserAuthorized {
return nil, errors.New("Unauthorized")
}

var project *launchpadpb.LaunchpadProject

err = s.conf.IndexerDB.Raw("SELECT * FROM launchpad_projects AS lp WHERE lp.project_id = ?", projectID).Scan(&project).Error
if err != nil {
return nil, errors.Wrap(err, "failed to query database")
return false, errors.Wrap(err, "failed to query database")
}

return &launchpadpb.LaunchpadProjectByIdResponse{
Project: project,
}, nil
return isUserAuthorized, nil
}
Loading

0 comments on commit 08de738

Please sign in to comment.