Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions internal/v2/service/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,110 @@ func Test_ProcessActiveDelegationStats(t *testing.T) {
require.Error(t, statsErr)
})
}

func Test_calculateUserCoStakingAPR(t *testing.T) {
s := &V2Service{}

t.Run("zero satoshis returns zero", func(t *testing.T) {
apr := s.calculateUserCoStakingAPR(
0,
1000000,
1000000000,
1000,
1000000,
50000,
0.5,
)
assert.Zero(t, apr)
})

t.Run("zero global score returns zero", func(t *testing.T) {
apr := s.calculateUserCoStakingAPR(
100000000,
1000000,
0,
1000,
1000000,
50000,
0.5,
)
assert.Zero(t, apr)
})

t.Run("calculates correctly with valid inputs", func(t *testing.T) {
apr := s.calculateUserCoStakingAPR(
100000000,
100000000,
1000000000,
1000,
1000000,
50000,
0.5,
)
assert.Positive(t, apr)
})

t.Run("no BABY staked returns zero co-staking APR", func(t *testing.T) {
apr := s.calculateUserCoStakingAPR(
100000000,
0,
1000000000,
1000,
1000000,
50000,
0.5,
)
assert.Zero(t, apr)
})
}

func Test_calculateBoostCoStakingAPR(t *testing.T) {
s := &V2Service{}

t.Run("zero satoshis returns zero", func(t *testing.T) {
apr := s.calculateBoostCoStakingAPR(
0,
1000000,
1000000000,
1000,
1000000,
50000,
0.5,
)
assert.Zero(t, apr)
})

t.Run("zero global score returns zero", func(t *testing.T) {
apr := s.calculateBoostCoStakingAPR(
100000000,
1000000,
0,
1000,
1000000,
50000,
0.5,
)
assert.Zero(t, apr)
})

t.Run("boost APR >= current APR", func(t *testing.T) {
satoshisStaked := int64(100000000)
ubbnStaked := int64(50000000)
globalTotalScore := int64(1000000000)
scoreRatio := int64(1000)
totalCoStakingRewardSupply := 1000000.0
btcPrice := 50000.0
babyPrice := 0.5

currentAPR := s.calculateUserCoStakingAPR(
satoshisStaked, ubbnStaked, globalTotalScore, scoreRatio,
totalCoStakingRewardSupply, btcPrice, babyPrice,
)
boostAPR := s.calculateBoostCoStakingAPR(
satoshisStaked, ubbnStaked, globalTotalScore, scoreRatio,
totalCoStakingRewardSupply, btcPrice, babyPrice,
)

assert.GreaterOrEqual(t, boostAPR, currentAPR)
})
}
49 changes: 49 additions & 0 deletions tests/api/v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,52 @@ func TestV2_StakerStats(t *testing.T) {

checkCases(t, cases)
}

func TestV2_APR(t *testing.T) {
cases := []testcase{
{
testName: "invalid satoshis_staked - non integer",
endpoint: "/v2/apr?satoshis_staked=abc",
expectedHttpCode: http.StatusBadRequest,
expectedContents: `{"errorCode":"BAD_REQUEST","message":"invalid satoshis_staked: must be a valid integer"}`,
},
{
testName: "invalid ubbn_staked - non integer",
endpoint: "/v2/apr?ubbn_staked=xyz",
expectedHttpCode: http.StatusBadRequest,
expectedContents: `{"errorCode":"BAD_REQUEST","message":"invalid ubbn_staked: must be a valid integer"}`,
},
{
testName: "negative satoshis_staked",
endpoint: "/v2/apr?satoshis_staked=-100",
expectedHttpCode: http.StatusBadRequest,
expectedContents: `{"errorCode":"BAD_REQUEST","message":"invalid satoshis_staked: must be non-negative"}`,
},
{
testName: "negative ubbn_staked",
endpoint: "/v2/apr?ubbn_staked=-100",
expectedHttpCode: http.StatusBadRequest,
expectedContents: `{"errorCode":"BAD_REQUEST","message":"invalid ubbn_staked: must be non-negative"}`,
},
{
testName: "invalid both params",
endpoint: "/v2/apr?satoshis_staked=invalid&ubbn_staked=invalid",
expectedHttpCode: http.StatusBadRequest,
expectedContents: `{"errorCode":"BAD_REQUEST","message":"invalid satoshis_staked: must be a valid integer"}`,
},
{
testName: "default params - service error due to nil bbn client",
endpoint: "/v2/apr",
expectedHttpCode: http.StatusInternalServerError,
expectedContents: `{"errorCode":"INTERNAL_SERVICE_ERROR","message":"Internal service error"}`,
},
{
testName: "valid params - service error due to nil bbn client",
endpoint: "/v2/apr?satoshis_staked=1000000&ubbn_staked=5000000",
expectedHttpCode: http.StatusInternalServerError,
expectedContents: `{"errorCode":"INTERNAL_SERVICE_ERROR","message":"Internal service error"}`,
},
}

checkCases(t, cases)
}
Loading