Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve delegation return type #106

Merged
merged 2 commits into from
Nov 14, 2024
Merged
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
56 changes: 36 additions & 20 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,37 @@ const docTemplate = `{
}
}
},
"v2service.DelegationStaking": {
"type": "object",
"properties": {
"end_height": {
"type": "integer"
},
"staking_amount": {
"type": "string"
},
"staking_time": {
"type": "string"
},
"staking_tx_hash_hex": {
"type": "string"
},
"start_height": {
"type": "integer"
}
}
},
"v2service.DelegationUnbonding": {
"type": "object",
"properties": {
"unbonding_time": {
"type": "string"
},
"unbonding_tx": {
"type": "string"
}
}
},
"v2service.FinalityProviderPublic": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -1161,8 +1192,11 @@ const docTemplate = `{
"v2service.StakerDelegationPublic": {
"type": "object",
"properties": {
"end_height": {
"type": "integer"
"delegation_staking": {
"$ref": "#/definitions/v2service.DelegationStaking"
},
"delegation_unbonding": {
"$ref": "#/definitions/v2service.DelegationUnbonding"
},
"finality_provider_btc_pks_hex": {
"type": "array",
Expand All @@ -1176,26 +1210,8 @@ const docTemplate = `{
"staker_btc_pk_hex": {
"type": "string"
},
"staking_amount": {
"type": "string"
},
"staking_time": {
"type": "string"
},
"staking_tx_hash_hex": {
"type": "string"
},
"start_height": {
"type": "integer"
},
"state": {
"$ref": "#/definitions/indexertypes.DelegationState"
},
"unbonding_time": {
"type": "string"
},
"unbonding_tx": {
"type": "string"
}
}
},
Expand Down
56 changes: 36 additions & 20 deletions docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,37 @@
}
}
},
"v2service.DelegationStaking": {
"type": "object",
"properties": {
"end_height": {
"type": "integer"
},
"staking_amount": {
"type": "string"
},
"staking_time": {
"type": "string"
},
"staking_tx_hash_hex": {
"type": "string"
},
"start_height": {
"type": "integer"
}
}
},
"v2service.DelegationUnbonding": {
"type": "object",
"properties": {
"unbonding_time": {
"type": "string"
},
"unbonding_tx": {
"type": "string"
}
}
},
"v2service.FinalityProviderPublic": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -1153,8 +1184,11 @@
"v2service.StakerDelegationPublic": {
"type": "object",
"properties": {
"end_height": {
"type": "integer"
"delegation_staking": {
"$ref": "#/definitions/v2service.DelegationStaking"
},
"delegation_unbonding": {
"$ref": "#/definitions/v2service.DelegationUnbonding"
},
"finality_provider_btc_pks_hex": {
"type": "array",
Expand All @@ -1168,26 +1202,8 @@
"staker_btc_pk_hex": {
"type": "string"
},
"staking_amount": {
"type": "string"
},
"staking_time": {
"type": "string"
},
"staking_tx_hash_hex": {
"type": "string"
},
"start_height": {
"type": "integer"
},
"state": {
"$ref": "#/definitions/indexertypes.DelegationState"
},
"unbonding_time": {
"type": "string"
},
"unbonding_tx": {
"type": "string"
}
}
},
Expand Down
38 changes: 24 additions & 14 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,26 @@ definitions:
version:
type: integer
type: object
v2service.DelegationStaking:
properties:
end_height:
type: integer
staking_amount:
type: string
staking_time:
type: string
staking_tx_hash_hex:
type: string
start_height:
type: integer
type: object
v2service.DelegationUnbonding:
properties:
unbonding_time:
type: string
unbonding_tx:
type: string
type: object
v2service.FinalityProviderPublic:
properties:
active_delegations:
Expand Down Expand Up @@ -405,8 +425,10 @@ definitions:
type: object
v2service.StakerDelegationPublic:
properties:
end_height:
type: integer
delegation_staking:
$ref: '#/definitions/v2service.DelegationStaking'
delegation_unbonding:
$ref: '#/definitions/v2service.DelegationUnbonding'
finality_provider_btc_pks_hex:
items:
type: string
Expand All @@ -415,20 +437,8 @@ definitions:
type: string
staker_btc_pk_hex:
type: string
staking_amount:
type: string
staking_time:
type: string
staking_tx_hash_hex:
type: string
start_height:
type: integer
state:
$ref: '#/definitions/indexertypes.DelegationState'
unbonding_time:
type: string
unbonding_tx:
type: string
type: object
v2service.StakerStatsPublic:
properties:
Expand Down
55 changes: 36 additions & 19 deletions internal/v2/service/staker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,26 @@ import (
"github.com/babylonlabs-io/staking-api-service/internal/shared/utils/datagen"
)

type DelegationStaking struct {
StakingTxHashHex string `json:"staking_tx_hash_hex"`
StakingTime string `json:"staking_time"`
StakingAmount string `json:"staking_amount"`
StartHeight uint32 `json:"start_height,omitempty"`
EndHeight uint32 `json:"end_height,omitempty"`
}

type DelegationUnbonding struct {
UnbondingTime string `json:"unbonding_time"`
UnbondingTx string `json:"unbonding_tx"`
}

type StakerDelegationPublic struct {
StakingTxHashHex string `json:"staking_tx_hash_hex"`
ParamsVersion string `json:"params_version"`
FinalityProviderBtcPksHex []string `json:"finality_provider_btc_pks_hex"`
StakerBtcPkHex string `json:"staker_btc_pk_hex"`
StakingTime string `json:"staking_time"`
StakingAmount string `json:"staking_amount"`
UnbondingTime string `json:"unbonding_time"`
UnbondingTx string `json:"unbonding_tx"`
State indexertypes.DelegationState `json:"state"`
StartHeight uint32 `json:"start_height"`
EndHeight uint32 `json:"end_height"`
FinalityProviderBtcPksHex []string `json:"finality_provider_btc_pks_hex"`
DelegationStaking DelegationStaking `json:"delegation_staking"`
DelegationUnbonding DelegationUnbonding `json:"delegation_unbonding"`
State indexertypes.DelegationState `json:"state"`
}

type StakerStatsPublic struct {
Expand All @@ -39,22 +47,31 @@ func (s *V2Service) GetStakerDelegations(ctx context.Context, stakingTxHashHex s
return nil, "", types.NewErrorWithMsg(http.StatusInternalServerError, types.InternalServiceError, "failed to get staker delegations")
}

// Initialize result structure
delegationsPublic := make([]*StakerDelegationPublic, 0, len(resultMap.Data))

// Group delegations by state
for _, delegation := range resultMap.Data {
delegationsPublic = append(delegationsPublic, &StakerDelegationPublic{
StakingTxHashHex: delegation.StakingTxHashHex,
delegationPublic := &StakerDelegationPublic{
ParamsVersion: delegation.ParamsVersion,
FinalityProviderBtcPksHex: delegation.FinalityProviderBtcPksHex,
StakerBtcPkHex: delegation.StakerBtcPkHex,
StakingTime: delegation.StakingTime,
StakingAmount: delegation.StakingAmount,
UnbondingTime: delegation.UnbondingTime,
UnbondingTx: delegation.UnbondingTx,
State: delegation.State,
StartHeight: delegation.StartHeight,
EndHeight: delegation.EndHeight,
})
DelegationStaking: DelegationStaking{
StakingTxHashHex: delegation.StakingTxHashHex,
StakingTime: delegation.StakingTime,
StakingAmount: delegation.StakingAmount,
StartHeight: delegation.StartHeight,
EndHeight: delegation.EndHeight,
},
DelegationUnbonding: DelegationUnbonding{
UnbondingTime: delegation.UnbondingTime,
UnbondingTx: delegation.UnbondingTx,
},
State: delegation.State,
}
delegationsPublic = append(delegationsPublic, delegationPublic)
}

return delegationsPublic, resultMap.PaginationToken, nil
}

Expand Down
Loading