Skip to content

Commit

Permalink
morph: Add AccountVote method
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
carpawell committed Jul 27, 2023
1 parent 782f658 commit dd73ebc
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions pkg/morph/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -418,6 +420,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.
Expand Down

0 comments on commit dd73ebc

Please sign in to comment.