-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add terms acceptance * resolve comments * resolve comments
- Loading branch information
1 parent
620f6b0
commit 1710e40
Showing
17 changed files
with
228 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package handlers | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/babylonlabs-io/staking-api-service/internal/types" | ||
) | ||
|
||
type TermsAcceptanceLoggingRequest struct { | ||
Address string `json:"address"` | ||
PublicKey string `json:"public_key"` | ||
} | ||
|
||
type TermsAcceptancePublic struct { | ||
Status bool `json:"status"` | ||
} | ||
|
||
func (h *Handler) LogTermsAcceptance(request *http.Request) (*Result, *types.Error) { | ||
address, publicKey, err := parseTermsAcceptanceLoggingRequest(request, h.config.Server.BTCNetParam) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := h.services.AcceptTerms(request.Context(), address, publicKey); err != nil { | ||
return nil, err | ||
} | ||
|
||
return NewResult(TermsAcceptancePublic{Status: true}), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package config | ||
|
||
type TermsAcceptanceConfig struct { | ||
Enabled bool `mapstructure:"enabled"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package model | ||
|
||
import ( | ||
"go.mongodb.org/mongo-driver/bson/primitive" | ||
) | ||
|
||
type TermsAcceptance struct { | ||
Id primitive.ObjectID `bson:"_id,omitempty"` | ||
Address string `bson:"address"` | ||
PublicKey string `bson:"public_key"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/babylonlabs-io/staking-api-service/internal/db/model" | ||
) | ||
|
||
func (db *Database) SaveTermsAcceptance(ctx context.Context, termsAcceptance *model.TermsAcceptance) error { | ||
collection := db.Client.Database(db.DbName).Collection(model.TermsAcceptanceCollection) | ||
|
||
_, err := collection.InsertOne(ctx, termsAcceptance) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package services | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/babylonlabs-io/staking-api-service/internal/db/model" | ||
"github.com/babylonlabs-io/staking-api-service/internal/types" | ||
) | ||
|
||
func (s *Services) AcceptTerms(ctx context.Context, address, publicKey string) *types.Error { | ||
termsAcceptance := &model.TermsAcceptance{ | ||
Address: address, | ||
PublicKey: publicKey, | ||
} | ||
|
||
if err := s.DbClient.SaveTermsAcceptance(ctx, termsAcceptance); err != nil { | ||
return types.NewInternalServiceError(err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package tests | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"math/rand" | ||
"net/http" | ||
"testing" | ||
"time" | ||
|
||
"github.com/babylonlabs-io/staking-api-service/internal/api/handlers" | ||
"github.com/babylonlabs-io/staking-api-service/tests/testutils" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
termsAcceptancePath = "/log_terms_acceptance" | ||
) | ||
|
||
func TestTermsAcceptance(t *testing.T) { | ||
testServer := setupTestServer(t, nil) | ||
defer testServer.Close() | ||
|
||
r := rand.New(rand.NewSource(time.Now().UnixNano())) | ||
address, _ := testutils.RandomBtcAddress(r, testServer.Config.Server.BTCNetParam) | ||
publicKey, _ := testutils.RandomPk() | ||
|
||
// Prepare request body | ||
requestBody := handlers.TermsAcceptanceLoggingRequest{ | ||
Address: address, | ||
PublicKey: publicKey, | ||
} | ||
bodyBytes, _ := json.Marshal(requestBody) | ||
|
||
url := testServer.Server.URL + termsAcceptancePath | ||
resp, err := http.Post(url, "application/json", bytes.NewReader(bodyBytes)) | ||
assert.NoError(t, err, "making POST request to terms acceptance endpoint should not fail") | ||
defer resp.Body.Close() | ||
|
||
assert.Equal(t, http.StatusOK, resp.StatusCode, "expected HTTP 200 OK status") | ||
|
||
var response handlers.PublicResponse[handlers.TermsAcceptancePublic] | ||
err = json.NewDecoder(resp.Body).Decode(&response) | ||
assert.NoError(t, err, "decoding response body should not fail") | ||
assert.Equal(t, true, response.Data.Status) | ||
} | ||
|
||
func TestTermsAcceptanceInvalidAddress(t *testing.T) { | ||
testServer := setupTestServer(t, nil) | ||
defer testServer.Close() | ||
|
||
// Use invalid address | ||
invalidAddress := "invalidaddress" | ||
publicKey, _ := testutils.RandomPk() | ||
|
||
requestBody := handlers.TermsAcceptanceLoggingRequest{} | ||
bodyBytes, _ := json.Marshal(requestBody) | ||
|
||
url := testServer.Server.URL + termsAcceptancePath + "?address=" + invalidAddress + "&public_key=" + publicKey | ||
resp, err := http.Post(url, "application/json", bytes.NewReader(bodyBytes)) | ||
assert.NoError(t, err, "making POST request to terms acceptance endpoint should not fail") | ||
defer resp.Body.Close() | ||
|
||
// Check response | ||
assert.Equal(t, http.StatusBadRequest, resp.StatusCode, "expected HTTP 400 Bad Request status") | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters