Documentation ¶
Overview ¶
Package client implements NEO-specific JSON-RPC 2.0 client. This package is currently in alpha and is subject to change.
Client ¶
After creating a client instance with or without a ClientConfig you can interact with the NEO blockchain by its exposed methods.
Some of the methods also allow to pass a verbose bool. This will return a more pretty printed response from the server instead of a raw hex string.
TODO:
Add missing methods to client. Allow client to connect using client cert. More in-depth examples.
Supported methods
getapplicationlog getbestblockhash getblock getblockcount getblockhash getblockheader getblocksysfee getconnectioncount getcontractstate getnep5balances getnep5transfers getpeers getrawmempool getrawtransaction getstorage gettransactionheight getunclaimedgas getvalidators getversion invoke invokefunction invokescript sendrawtransaction submitblock validateaddress
Unsupported methods
claimgas dumpprivkey getbalance getmetricblocktimestamp getnewaddress getwalletheight importprivkey listaddress listplugins sendfrom sendmany sendtoaddress
Example ¶
package main import ( "context" "fmt" "os" "github.com/nspcc-dev/neo-go/pkg/config/netmode" "github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/rpc/client" ) func main() { endpoint := "http://seed5.bridgeprotocol.io:10332" opts := client.Options{Network: netmode.MainNet} c, err := client.New(context.TODO(), endpoint, opts) if err != nil { fmt.Println(err) os.Exit(1) } if err := c.Ping(); err != nil { fmt.Println(err) os.Exit(1) } addr, err := address.StringToUint160("ATySFJAbLW7QHsZGHScLhxq6EyNBxx3eFP") if err != nil { fmt.Println(err) os.Exit(1) } resp, err := c.GetNEP5Balances(addr) if err != nil { fmt.Println(err) os.Exit(1) } fmt.Println(resp.Address) fmt.Println(resp.Balances) }
Output:
Index ¶
- Variables
- type Client
- func (c *Client) AddNetworkFee(tx *transaction.Transaction, extraFee int64, acc *wallet.Account) error
- func (c *Client) CalculateValidUntilBlock() (uint32, error)
- func (c *Client) CreateNEP5TransferTx(acc *wallet.Account, to util.Uint160, token util.Uint160, amount int64, ...) (*transaction.Transaction, error)
- func (c *Client) GetApplicationLog(hash util.Uint256) (*result.ApplicationLog, error)
- func (c *Client) GetBestBlockHash() (util.Uint256, error)
- func (c *Client) GetBlockByHash(hash util.Uint256) (*block.Block, error)
- func (c *Client) GetBlockByHashVerbose(hash util.Uint256) (*result.Block, error)
- func (c *Client) GetBlockByIndex(index uint32) (*block.Block, error)
- func (c *Client) GetBlockByIndexVerbose(index uint32) (*result.Block, error)
- func (c *Client) GetBlockCount() (uint32, error)
- func (c *Client) GetBlockHash(index uint32) (util.Uint256, error)
- func (c *Client) GetBlockHeader(hash util.Uint256) (*block.Header, error)
- func (c *Client) GetBlockHeaderVerbose(hash util.Uint256) (*result.Header, error)
- func (c *Client) GetBlockSysFee(index uint32) (util.Fixed8, error)
- func (c *Client) GetBlockedAccounts() (native.BlockedAccounts, error)
- func (c *Client) GetConnectionCount() (int, error)
- func (c *Client) GetContractState(hash util.Uint160) (*state.Contract, error)
- func (c *Client) GetFeePerByte() (int64, error)
- func (c *Client) GetMaxBlockSize() (int64, error)
- func (c *Client) GetMaxTransactionsPerBlock() (int64, error)
- func (c *Client) GetNEP5Balances(address util.Uint160) (*result.NEP5Balances, error)
- func (c *Client) GetNEP5Transfers(address string) (*result.NEP5Transfers, error)
- func (c *Client) GetPeers() (*result.GetPeers, error)
- func (c *Client) GetRawMemPool() ([]util.Uint256, error)
- func (c *Client) GetRawTransaction(hash util.Uint256) (*transaction.Transaction, error)
- func (c *Client) GetRawTransactionVerbose(hash util.Uint256) (*result.TransactionOutputRaw, error)
- func (c *Client) GetStorageByHash(hash util.Uint160, key []byte) ([]byte, error)
- func (c *Client) GetStorageByID(id int32, key []byte) ([]byte, error)
- func (c *Client) GetTransactionHeight(hash util.Uint256) (uint32, error)
- func (c *Client) GetUnclaimedGas(address string) (util.Fixed8, error)
- func (c *Client) GetValidators() ([]result.Validator, error)
- func (c *Client) GetVersion() (*result.Version, error)
- func (c *Client) InvokeFunction(contract util.Uint160, operation string, params []smartcontract.Parameter, ...) (*result.Invoke, error)
- func (c *Client) InvokeScript(script []byte, cosigners []transaction.Cosigner) (*result.Invoke, error)
- func (c *Client) NEP5BalanceOf(tokenHash util.Uint160) (int64, error)
- func (c *Client) NEP5Decimals(tokenHash util.Uint160) (int64, error)
- func (c *Client) NEP5Name(tokenHash util.Uint160) (string, error)
- func (c *Client) NEP5Symbol(tokenHash util.Uint160) (string, error)
- func (c *Client) NEP5TokenInfo(tokenHash util.Uint160) (*wallet.Token, error)
- func (c *Client) NEP5TotalSupply(tokenHash util.Uint160) (int64, error)
- func (c *Client) Ping() error
- func (c *Client) SendRawTransaction(rawTX *transaction.Transaction) error
- func (c *Client) SetWIF(wif string) error
- func (c *Client) SignAndPushInvocationTx(script []byte, acc *wallet.Account, sysfee int64, netfee util.Fixed8, ...) (util.Uint256, error)
- func (c *Client) SubmitBlock(b block.Block) error
- func (c *Client) TransferNEP5(acc *wallet.Account, to util.Uint160, token util.Uint160, amount int64, ...) (util.Uint256, error)
- func (c *Client) ValidateAddress(address string) error
- func (c *Client) WIF() keys.WIF
- type Notification
- type Options
- type WSClient
- func (c *WSClient) Close()
- func (c *WSClient) SubscribeForExecutionNotifications(contract *util.Uint160) (string, error)
- func (c *WSClient) SubscribeForNewBlocks(primary *int) (string, error)
- func (c *WSClient) SubscribeForNewTransactions(sender *util.Uint160, cosigner *util.Uint160) (string, error)
- func (c *WSClient) SubscribeForTransactionExecutions(state *string) (string, error)
- func (c *WSClient) Unsubscribe(id string) error
- func (c *WSClient) UnsubscribeAll() error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var (
// NeoContractHash is a hash of the NEO native contract.
NeoContractHash, _ = util.Uint160DecodeStringLE("9bde8f209c88dd0e7ca3bf0af0f476cdd8207789")
// GasContractHash is a hash of the GAS native contract.
GasContractHash, _ = util.Uint160DecodeStringLE("8c23f196d8a1bfd103a9dcb1f9ccf0c611377d3b")
)
var PolicyContractHash = util.Uint160{154, 97, 164, 110, 236, 151, 184, 147, 6, 215, 206, 129, 241, 91, 70, 32, 145, 208, 9, 50}
PolicyContractHash represents BE hash of native Policy contract.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client represents the middleman for executing JSON RPC calls to remote NEO RPC nodes.
func (*Client) AddNetworkFee ¶ added in v0.90.0
func (c *Client) AddNetworkFee(tx *transaction.Transaction, extraFee int64, acc *wallet.Account) error
AddNetworkFee adds network fee for each witness script and optional extra network fee to transaction.
func (*Client) CalculateValidUntilBlock ¶ added in v0.90.0
CalculateValidUntilBlock calculates ValidUntilBlock field for tx as current blockchain height + number of validators. Number of validators is the length of blockchain validators list got from GetValidators() method. Validators count is being cached and updated every 100 blocks.
func (*Client) CreateNEP5TransferTx ¶ added in v0.90.0
func (c *Client) CreateNEP5TransferTx(acc *wallet.Account, to util.Uint160, token util.Uint160, amount int64, gas int64) (*transaction.Transaction, error)
CreateNEP5TransferTx creates an invocation transaction for the 'transfer' method of a given contract (token) to move specified amount of NEP5 assets (in FixedN format using contract's number of decimals) to given account and returns it. The returned transaction is not signed.
func (*Client) GetApplicationLog ¶
GetApplicationLog returns the contract log based on the specified txid.
func (*Client) GetBestBlockHash ¶
GetBestBlockHash returns the hash of the tallest block in the main chain.
func (*Client) GetBlockByHash ¶
GetBlockByHash returns a block by its hash.
func (*Client) GetBlockByHashVerbose ¶
GetBlockByHashVerbose returns a block wrapper with additional metadata by its hash.
func (*Client) GetBlockByIndex ¶
GetBlockByIndex returns a block by its height.
func (*Client) GetBlockByIndexVerbose ¶
GetBlockByIndexVerbose returns a block wrapper with additional metadata by its height. NOTE: to get transaction.ID and transaction.Size, use t.Hash() and io.GetVarSize(t) respectively.
func (*Client) GetBlockCount ¶
GetBlockCount returns the number of blocks in the main chain.
func (*Client) GetBlockHash ¶
GetBlockHash returns the hash value of the corresponding block, based on the specified index.
func (*Client) GetBlockHeader ¶
GetBlockHeader returns the corresponding block header information from serialized hex string according to the specified script hash.
func (*Client) GetBlockHeaderVerbose ¶
GetBlockHeaderVerbose returns the corresponding block header information from Json format string according to the specified script hash.
func (*Client) GetBlockSysFee ¶
GetBlockSysFee returns the system fees of the block, based on the specified index.
func (*Client) GetBlockedAccounts ¶ added in v0.90.0
func (c *Client) GetBlockedAccounts() (native.BlockedAccounts, error)
GetBlockedAccounts invokes `getBlockedAccounts` method on a native Policy contract.
func (*Client) GetConnectionCount ¶
GetConnectionCount returns the current number of connections for the node.
func (*Client) GetContractState ¶
GetContractState queries contract information, according to the contract script hash.
func (*Client) GetFeePerByte ¶ added in v0.90.0
GetFeePerByte invokes `getFeePerByte` method on a native Policy contract.
func (*Client) GetMaxBlockSize ¶ added in v0.90.0
GetMaxBlockSize invokes `getMaxBlockSize` method on a native Policy contract.
func (*Client) GetMaxTransactionsPerBlock ¶ added in v0.90.0
GetMaxTransactionsPerBlock invokes `getMaxTransactionsPerBlock` method on a native Policy contract.
func (*Client) GetNEP5Balances ¶
GetNEP5Balances is a wrapper for getnep5balances RPC.
func (*Client) GetNEP5Transfers ¶
func (c *Client) GetNEP5Transfers(address string) (*result.NEP5Transfers, error)
GetNEP5Transfers is a wrapper for getnep5transfers RPC.
func (*Client) GetPeers ¶
GetPeers returns the list of nodes that the node is currently connected/disconnected from.
func (*Client) GetRawMemPool ¶
GetRawMemPool returns the list of unconfirmed transactions in memory.
func (*Client) GetRawTransaction ¶
func (c *Client) GetRawTransaction(hash util.Uint256) (*transaction.Transaction, error)
GetRawTransaction returns a transaction by hash.
func (*Client) GetRawTransactionVerbose ¶
GetRawTransactionVerbose returns a transaction wrapper with additional metadata by transaction's hash. NOTE: to get transaction.ID and transaction.Size, use t.Hash() and io.GetVarSize(t) respectively.
func (*Client) GetStorageByHash ¶ added in v0.90.0
GetStorageByHash returns the stored value, according to the contract script hash and the stored key.
func (*Client) GetStorageByID ¶ added in v0.90.0
GetStorageByID returns the stored value, according to the contract ID and the stored key.
func (*Client) GetTransactionHeight ¶
GetTransactionHeight returns the block index in which the transaction is found.
func (*Client) GetUnclaimedGas ¶ added in v0.90.0
GetUnclaimedGas returns unclaimed GAS amount for the specified address.
func (*Client) GetValidators ¶
GetValidators returns the current NEO consensus nodes information and voting status.
func (*Client) GetVersion ¶
GetVersion returns the version information about the queried node.
func (*Client) InvokeFunction ¶
func (c *Client) InvokeFunction(contract util.Uint160, operation string, params []smartcontract.Parameter, cosigners []transaction.Cosigner) (*result.Invoke, error)
InvokeFunction returns the results after calling the smart contract scripthash with the given operation and parameters. NOTE: this is test invoke and will not affect the blockchain.
func (*Client) InvokeScript ¶
func (c *Client) InvokeScript(script []byte, cosigners []transaction.Cosigner) (*result.Invoke, error)
InvokeScript returns the result of the given script after running it true the VM. NOTE: This is a test invoke and will not affect the blockchain.
func (*Client) NEP5BalanceOf ¶
NEP5BalanceOf invokes `balanceOf` NEP5 method on a specified contract.
func (*Client) NEP5Decimals ¶
NEP5Decimals invokes `decimals` NEP5 method on a specified contract.
func (*Client) NEP5Symbol ¶
NEP5Symbol invokes `symbol` NEP5 method on a specified contract.
func (*Client) NEP5TokenInfo ¶
NEP5TokenInfo returns full NEP5 token info.
func (*Client) NEP5TotalSupply ¶
NEP5TotalSupply invokes `totalSupply` NEP5 method on a specified contract.
func (*Client) Ping ¶
Ping attempts to create a connection to the endpoint. and returns an error if there is one.
func (*Client) SendRawTransaction ¶
func (c *Client) SendRawTransaction(rawTX *transaction.Transaction) error
SendRawTransaction broadcasts a transaction over the NEO network. The given hex string needs to be signed with a keypair. When the result of the response object is true, the TX has successfully been broadcasted to the network.
func (*Client) SetWIF ¶
SetWIF decodes given WIF and adds some wallet data to client. Useful for RPC calls that require an open wallet.
func (*Client) SignAndPushInvocationTx ¶
func (c *Client) SignAndPushInvocationTx(script []byte, acc *wallet.Account, sysfee int64, netfee util.Fixed8, cosigners []transaction.Cosigner) (util.Uint256, error)
SignAndPushInvocationTx signs and pushes given script as an invocation transaction using given wif to sign it and spending the amount of gas specified. It returns a hash of the invocation transaction and an error.
func (*Client) SubmitBlock ¶
SubmitBlock broadcasts a raw block over the NEO network.
func (*Client) TransferNEP5 ¶
func (c *Client) TransferNEP5(acc *wallet.Account, to util.Uint160, token util.Uint160, amount int64, gas int64) (util.Uint256, error)
TransferNEP5 creates an invocation transaction that invokes 'transfer' method on a given token to move specified amount of NEP5 assets (in FixedN format using contract's number of decimals) to given account and sends it to the network returning just a hash of it.
func (*Client) ValidateAddress ¶
ValidateAddress verifies that the address is a correct NEO address.
type Notification ¶ added in v0.75.0
Notification represents server-generated notification for client subscriptions. Value can be one of block.Block, result.ApplicationLog, result.NotificationEvent or transaction.Transaction based on Type.
type Options ¶
type Options struct { // Cert is a client-side certificate, it doesn't work at the moment along // with the other two options below. Cert string Key string CACert string DialTimeout time.Duration RequestTimeout time.Duration Network netmode.Magic }
Options defines options for the RPC client. All values are optional. If any duration is not specified a default of 4 seconds will be used.
type WSClient ¶ added in v0.75.0
type WSClient struct { Client // Notifications is a channel that is used to send events received from // server. Client's code is supposed to be reading from this channel if // it wants to use subscription mechanism, failing to do so will cause // WSClient to block even regular requests. This channel is not buffered. // In case of protocol error or upon connection closure this channel will // be closed, so make sure to handle this. Notifications chan Notification // contains filtered or unexported fields }
WSClient is a websocket-enabled RPC client that can be used with appropriate servers. It's supposed to be faster than Client because it has persistent connection to the server and at the same time is exposes some functionality that is only provided via websockets (like event subscription mechanism).
func NewWS ¶ added in v0.75.0
NewWS returns a new WSClient ready to use (with established websocket connection). You need to use websocket URL for it like `ws://1.2.3.4/ws`.
func (*WSClient) Close ¶ added in v0.75.0
func (c *WSClient) Close()
Close closes connection to the remote side rendering this client instance unusable.
func (*WSClient) SubscribeForExecutionNotifications ¶ added in v0.75.0
SubscribeForExecutionNotifications adds subscription for notifications generated during transaction execution to this instance of client. It can be filtered by contract's hash (that emits notifications), nil value puts no such restrictions.
func (*WSClient) SubscribeForNewBlocks ¶ added in v0.75.0
SubscribeForNewBlocks adds subscription for new block events to this instance of client. It can filtered by primary consensus node index, nil value doesn't add any filters.
func (*WSClient) SubscribeForNewTransactions ¶ added in v0.75.0
func (c *WSClient) SubscribeForNewTransactions(sender *util.Uint160, cosigner *util.Uint160) (string, error)
SubscribeForNewTransactions adds subscription for new transaction events to this instance of client. It can be filtered by sender and/or cosigner, nil value is treated as missing filter.
func (*WSClient) SubscribeForTransactionExecutions ¶ added in v0.75.0
SubscribeForTransactionExecutions adds subscription for application execution results generated during transaction execution to this instance of client. Can be filtered by state (HALT/FAULT) to check for successful or failing transactions, nil value means no filtering.
func (*WSClient) Unsubscribe ¶ added in v0.75.0
Unsubscribe removes subscription for given event stream.
func (*WSClient) UnsubscribeAll ¶ added in v0.75.0
UnsubscribeAll removes all active subscriptions of current client.