add xastro/astro ratio in astroport staking

This commit is contained in:
Laurent Le Houerou 2022-04-18 14:07:24 +04:00
parent b0d2c071c5
commit 4aa9e12c92

View File

@ -1,6 +1,8 @@
package astroport
import (
"context"
"github.com/galacticship/terra"
"github.com/galacticship/terra/cosmos"
"github.com/pkg/errors"
@ -22,6 +24,45 @@ func NewStaking(querier *terra.Querier) (*Staking, error) {
}, nil
}
func (s *Staking) TotalShares(ctx context.Context) (decimal.Decimal, error) {
var q struct {
TotalShares struct{} `json:"total_shares"`
}
var r decimal.Decimal
err := s.QueryStore(ctx, q, &r)
if err != nil {
return decimal.Zero, errors.Wrap(err, "querying contract")
}
return terra.XASTRO.ValueFromTerra(r), nil
}
func (s *Staking) TotalDeposit(ctx context.Context) (decimal.Decimal, error) {
var q struct {
TotalDeposit struct{} `json:"total_deposit"`
}
var r decimal.Decimal
err := s.QueryStore(ctx, q, &r)
if err != nil {
return decimal.Zero, errors.Wrap(err, "querying contract")
}
return terra.ASTRO.ValueFromTerra(r), nil
}
func (s *Staking) XASTROPerASTRO(ctx context.Context) (decimal.Decimal, error) {
totalShares, err := s.TotalShares(ctx)
if err != nil {
return decimal.Zero, errors.Wrap(err, "getting total shares")
}
totalDeposit, err := s.TotalDeposit(ctx)
if err != nil {
return decimal.Zero, errors.Wrap(err, "getting total deposit")
}
if totalDeposit.Equals(decimal.Zero) {
return decimal.NewFromInt(1), nil
}
return totalShares.Div(totalDeposit), nil
}
func (s *Staking) NewEnterMessage(sender cosmos.AccAddress, amount decimal.Decimal) (cosmos.Msg, error) {
var q struct {
Enter struct{} `json:"enter"`