Skip to content

Commit

Permalink
feat(nft): add rpc getnftinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Houshoupei84 authored and RainFallsSilent committed Mar 14, 2023
1 parent 223119c commit c582dc7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
20 changes: 20 additions & 0 deletions dpos/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,26 @@ func (p *Producer) GetTotalDPoSV2VoteRights() float64 {
return result
}

func (p *Producer) GetNFTVotesRight(nftID *common.Uint256) float64 {
var result float64
out:
for _, sVoteDetail := range p.detailedDPoSV2Votes {
var totalN float64
for referKey, votes := range sVoteDetail {
if referKey.IsEqual(*nftID) {
weightF := math.Log10(float64(votes.Info[0].LockTime-votes.BlockHeight) / 7200 * 10)
N := common.Fixed64(float64(votes.Info[0].Votes) * weightF)
totalN += float64(N)
break out
}

}
result += totalN
}

return result
}

func (p *Producer) SetInfo(i payload.ProducerInfo) {
p.info = i
}
Expand Down
1 change: 1 addition & 0 deletions servers/httpjsonrpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func StartRPCServer() {

//nft
mainMux["getcandestroynftids"] = GetCanDestroynftIDs
mainMux["getnftinfo"] = GetNFTInfo

var handler http.Handler
rpcServeMux := http.NewServeMux()
Expand Down
49 changes: 49 additions & 0 deletions servers/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,55 @@ func GetProducerInfo(params Params) map[string]interface{} {
return ResponsePack(Success, producerInfo)
}

func GetNFTInfo(params Params) map[string]interface{} {
idParam, ok := params.String("id")
if !ok {
return ResponsePack(InvalidParams, "need id in an array!")
}

idBytes, err := common.HexStringToBytes(idParam)
if err != nil {
return ResponsePack(InvalidParams, "")
}
nftID, err := common.Uint256FromBytes(idBytes)
if err != nil {
return ResponsePack(InvalidParams, "")
}

type nftInfo struct {
ID string `json:"ID"`
StartHeight uint32 `json:"startheight"`
EndHeight uint32 `json:"endheight"`
Votes string `json:"votes"`
VotesRight string `json:"votesright"`
Rewards string `json:"rewards"`
}

var info nftInfo
info.ID = idParam

producers := Chain.GetState().GetAllProducers()
for _, producer := range producers {
for _, votesInfo := range producer.GetAllDetailedDPoSV2Votes() {
for referKey, detailVoteInfo := range votesInfo {
if referKey.IsEqual(*nftID) {
ct, _ := contract.CreateStakeContractByCode(referKey.Bytes())
nftStakeAddress, _ := ct.ToProgramHash().ToAddress()
info.StartHeight = detailVoteInfo.BlockHeight
info.EndHeight = detailVoteInfo.Info[0].LockTime
info.Votes = detailVoteInfo.Info[0].Votes.String()
info.VotesRight = common.Fixed64(producer.GetNFTVotesRight(nftID)).String()
info.Rewards = Chain.GetState().DPoSV2RewardInfo[nftStakeAddress].String()
return ResponsePack(Success, info)

}
}
}
}
return ResponsePack(InvalidParams, "wrong nft id, not found it!")

}

func GetCanDestroynftIDs(params Params) map[string]interface{} {
idsParam, ok := params.ArrayString("ids")
if !ok {
Expand Down

0 comments on commit c582dc7

Please sign in to comment.