rpc: implement getclaimable RPC
This commit is contained in:
parent
0e2a1f40ba
commit
51c4868641
4 changed files with 114 additions and 0 deletions
22
pkg/rpc/response/result/claimable.go
Normal file
22
pkg/rpc/response/result/claimable.go
Normal file
|
@ -0,0 +1,22 @@
|
|||
package result
|
||||
|
||||
import "github.com/CityOfZion/neo-go/pkg/util"
|
||||
|
||||
// ClaimableInfo is a result of a getclaimable RPC call.
|
||||
type ClaimableInfo struct {
|
||||
Spents []Claimable `json:"claimable"`
|
||||
Address string `json:"address"`
|
||||
Unclaimed util.Fixed8 `json:"unclaimed"`
|
||||
}
|
||||
|
||||
// Claimable represents spent outputs which can be claimed.
|
||||
type Claimable struct {
|
||||
Tx util.Uint256 `json:"txid"`
|
||||
N int `json:"n"`
|
||||
Value util.Fixed8 `json:"value"`
|
||||
StartHeight uint32 `json:"start_height"`
|
||||
EndHeight uint32 `json:"end_height"`
|
||||
Generated util.Fixed8 `json:"generated"`
|
||||
SysFee util.Fixed8 `json:"sys_fee"`
|
||||
Unclaimed util.Fixed8 `json:"unclaimed"`
|
||||
}
|
|
@ -51,6 +51,14 @@ var (
|
|||
},
|
||||
)
|
||||
|
||||
getclaimableCalled = prometheus.NewCounter(
|
||||
prometheus.CounterOpts{
|
||||
Help: "Number of calls to getclaimable rpc endpoint",
|
||||
Name: "getclaimable_called",
|
||||
Namespace: "neogo",
|
||||
},
|
||||
)
|
||||
|
||||
getconnectioncountCalled = prometheus.NewCounter(
|
||||
prometheus.CounterOpts{
|
||||
Help: "Number of calls to getconnectioncount rpc endpoint",
|
||||
|
|
|
@ -190,6 +190,10 @@ Methods:
|
|||
getblocksysfeeCalled.Inc()
|
||||
results, resultsErr = s.getBlockSysFee(reqParams)
|
||||
|
||||
case "getclaimable":
|
||||
getclaimableCalled.Inc()
|
||||
results, resultsErr = s.getClaimable(reqParams)
|
||||
|
||||
case "getconnectioncount":
|
||||
getconnectioncountCalled.Inc()
|
||||
results = s.coreServer.PeerCount()
|
||||
|
@ -322,6 +326,52 @@ func (s *Server) getApplicationLog(reqParams request.Params) (interface{}, error
|
|||
return result.NewApplicationLog(appExecResult, scriptHash), nil
|
||||
}
|
||||
|
||||
func (s *Server) getClaimable(ps request.Params) (interface{}, error) {
|
||||
p, ok := ps.ValueWithType(0, request.StringT)
|
||||
if !ok {
|
||||
return nil, response.ErrInvalidParams
|
||||
}
|
||||
u, err := p.GetUint160FromAddress()
|
||||
if err != nil {
|
||||
return nil, response.ErrInvalidParams
|
||||
}
|
||||
|
||||
var unclaimed []state.UnclaimedBalance
|
||||
if acc := s.chain.GetAccountState(u); acc != nil {
|
||||
unclaimed = acc.Unclaimed
|
||||
}
|
||||
|
||||
var sum util.Fixed8
|
||||
claimable := make([]result.Claimable, 0, len(unclaimed))
|
||||
for _, ub := range unclaimed {
|
||||
gen, sys, err := s.chain.CalculateClaimable(ub.Value, ub.Start, ub.End)
|
||||
if err != nil {
|
||||
s.log.Info("error while calculating claim bonus", zap.Error(err))
|
||||
continue
|
||||
}
|
||||
|
||||
uc := gen.Add(sys)
|
||||
sum += uc
|
||||
|
||||
claimable = append(claimable, result.Claimable{
|
||||
Tx: ub.Tx,
|
||||
N: int(ub.Index),
|
||||
Value: ub.Value,
|
||||
StartHeight: ub.Start,
|
||||
EndHeight: ub.End,
|
||||
Generated: gen,
|
||||
SysFee: sys,
|
||||
Unclaimed: uc,
|
||||
})
|
||||
}
|
||||
|
||||
return result.ClaimableInfo{
|
||||
Spents: claimable,
|
||||
Address: p.String(),
|
||||
Unclaimed: sum,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Server) getStorage(ps request.Params) (interface{}, error) {
|
||||
param, ok := ps.Value(0)
|
||||
if !ok {
|
||||
|
|
|
@ -363,6 +363,40 @@ var rpcTestCases = map[string][]rpcTestCase{
|
|||
fail: true,
|
||||
},
|
||||
},
|
||||
"getclaimable": {
|
||||
{
|
||||
name: "no params",
|
||||
params: "[]",
|
||||
fail: true,
|
||||
},
|
||||
{
|
||||
name: "invalid address",
|
||||
params: `["invalid"]`,
|
||||
fail: true,
|
||||
},
|
||||
{
|
||||
name: "normal address",
|
||||
params: `["AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU"]`,
|
||||
result: func(*executor) interface{} {
|
||||
// hash of the issueTx
|
||||
h, _ := util.Uint256DecodeStringBE("6da730b566db183bfceb863b780cd92dee2b497e5a023c322c1eaca81cf9ad7a")
|
||||
amount := util.Fixed8FromInt64(52 * 8) // (endHeight - startHeight) * genAmount[0]
|
||||
return &result.ClaimableInfo{
|
||||
Spents: []result.Claimable{
|
||||
{
|
||||
Tx: h,
|
||||
Value: util.Fixed8FromInt64(100000000),
|
||||
EndHeight: 52,
|
||||
Generated: amount,
|
||||
Unclaimed: amount,
|
||||
},
|
||||
},
|
||||
Address: "AZ81H31DMWzbSnFDLFkzh9vHwaDLayV7fU",
|
||||
Unclaimed: amount,
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
"getconnectioncount": {
|
||||
{
|
||||
params: "[]",
|
||||
|
|
Loading…
Reference in a new issue