diff --git a/protocols/astroport/staking.go b/protocols/astroport/staking.go index e12b3e7..3876f16 100644 --- a/protocols/astroport/staking.go +++ b/protocols/astroport/staking.go @@ -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"`