2020-03-06 17:38:17 +00:00
|
|
|
package result
|
|
|
|
|
|
|
|
import (
|
2020-11-13 13:54:38 +00:00
|
|
|
"errors"
|
|
|
|
"math"
|
|
|
|
|
2020-03-06 17:38:17 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/state"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Unclaimed wrapper is used to represent getunclaimed return result.
|
|
|
|
type Unclaimed struct {
|
|
|
|
Available util.Fixed8 `json:"available"`
|
|
|
|
Unavailable util.Fixed8 `json:"unavailable"`
|
|
|
|
Unclaimed util.Fixed8 `json:"unclaimed"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewUnclaimed creates a new Unclaimed wrapper using given Blockchainer.
|
|
|
|
func NewUnclaimed(a *state.Account, chain core.Blockchainer) (*Unclaimed, error) {
|
|
|
|
var (
|
|
|
|
available util.Fixed8
|
|
|
|
unavailable util.Fixed8
|
|
|
|
)
|
|
|
|
|
2020-03-13 10:54:13 +00:00
|
|
|
err := a.Unclaimed.ForEach(func(ucb *state.UnclaimedBalance) error {
|
2020-03-06 17:38:17 +00:00
|
|
|
gen, sys, err := chain.CalculateClaimable(ucb.Value, ucb.Start, ucb.End)
|
|
|
|
if err != nil {
|
2020-03-13 10:54:13 +00:00
|
|
|
return err
|
2020-03-06 17:38:17 +00:00
|
|
|
}
|
|
|
|
available += gen + sys
|
2020-03-13 10:54:13 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-03-06 17:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
blockHeight := chain.BlockHeight()
|
|
|
|
for _, usb := range a.Balances[core.GoverningTokenID()] {
|
|
|
|
_, txHeight, err := chain.GetTransaction(usb.Tx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-11-13 13:54:38 +00:00
|
|
|
if txHeight == math.MaxUint32 {
|
|
|
|
return nil, errors.New("wrong transaction stored in account data")
|
|
|
|
}
|
2020-03-06 17:38:17 +00:00
|
|
|
gen, sys, err := chain.CalculateClaimable(usb.Value, txHeight, blockHeight)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
unavailable += gen + sys
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Unclaimed{
|
|
|
|
Available: available,
|
|
|
|
Unavailable: unavailable,
|
|
|
|
Unclaimed: available + unavailable,
|
|
|
|
}, nil
|
|
|
|
}
|