From b91571dbf43cff7a94fe20f53636e3c594bf40e1 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Mon, 24 Jul 2023 21:13:05 +0300 Subject: [PATCH] morph: Add `VotesFor` method It returns a vote made by a NEO holder. Does not cache NEO contract reader cause its creation needs no network and the whole `morph.Client` is likely to be refactored soon. Signed-off-by: Pavel Karpy --- pkg/morph/client/client.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pkg/morph/client/client.go b/pkg/morph/client/client.go index 00a3485f114..8166f223667 100644 --- a/pkg/morph/client/client.go +++ b/pkg/morph/client/client.go @@ -16,6 +16,8 @@ import ( "github.com/nspcc-dev/neo-go/pkg/rpcclient" "github.com/nspcc-dev/neo-go/pkg/rpcclient/actor" "github.com/nspcc-dev/neo-go/pkg/rpcclient/gas" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/neo" "github.com/nspcc-dev/neo-go/pkg/rpcclient/nep17" "github.com/nspcc-dev/neo-go/pkg/rpcclient/rolemgmt" "github.com/nspcc-dev/neo-go/pkg/smartcontract/trigger" @@ -419,6 +421,30 @@ func (c *Client) IsValidScript(script []byte, signers []transaction.Signer) (res return result.State == vmstate.Halt.String(), nil } +// AccountVote returns a key the provided account has voted with its NEO +// tokens for. Nil key with no error is returned if the account has no NEO. +func (c *Client) AccountVote(addr util.Uint160) (*keys.PublicKey, error) { + c.switchLock.RLock() + defer c.switchLock.RUnlock() + + if c.inactive { + return nil, ErrConnectionLost + } + + neoReader := neo.NewReader(invoker.New(c.client, nil)) + + state, err := neoReader.GetAccountState(addr) + if err != nil { + return nil, fmt.Errorf("can't get vote info: %w", err) + } + + if state == nil { + return nil, nil + } + + return state.VoteTo, nil +} + // NotificationChannel returns channel than receives subscribed // notification from the connected RPC node. // Channel is closed when connection to the RPC node is lost.