Skip to content

Commit

Permalink
morph: Add VotesFor 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 25, 2023
1 parent f751d71 commit b91571d
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 @@ -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.
Expand Down

0 comments on commit b91571d

Please sign in to comment.