Documentation ¶
Overview ¶
Package substrate provides reusable functions for interacting with a substrate node.
Index ¶
- Constants
- Variables
- func DecodeError(meta *types.Metadata, err types.DispatchError) error
- func ExtIsFinal(status *types.ExtrinsicStatus) bool
- func Meta(meta *gsrpc.Metadata) (*gsrpc.MetadataV13, bool)
- func SS58Address(addr gsrpc.AccountID, network NetworkID) (string, error)
- type API
- func (a *API) AccountInfo(addr types.AccountID) (AccountInfo, error)
- func (a *API) BlockHash(n uint64) (types.Hash, error)
- func (a *API) BuildKey(pallet, variable string, args ...[]byte) (types.StorageKey, error)
- func (a *API) LastHeader() (*types.Header, error)
- func (a *API) Metadata() *types.Metadata
- func (a *API) Network() NetworkID
- func (a *API) PastBlock(pastBlocks types.BlockNumber) (types.Hash, error)
- func (a *API) QueryAll(keys []types.StorageKey, startBlock types.Hash) ([]types.StorageChangeSet, error)
- func (a *API) QueryOne(pastBlocks types.BlockNumber, keys ...types.StorageKey) (*types.KeyValueOption, error)
- func (a *API) RuntimeVersion() (*types.RuntimeVersion, error)
- func (a *API) Subscribe(keys ...types.StorageKey) (*state.StorageSubscription, error)
- func (a *API) SubscribeHeaders() (*chain.NewHeadsSubscription, error)
- func (a *API) Transact(ext *types.Extrinsic) (*ExtStatusSub, error)
- type AccountInfo
- type ChainReader
- type Dot
- type EventKey
- type EventSource
- type ExpiredTimeout
- type ExtFactory
- type ExtName
- type ExtSigner
- type ExtStatusPred
- type ExtStatusSub
- type NetworkID
- type Pallet
- func (p *Pallet) BuildExt(call *ExtName, args []interface{}, addr types.AccountID, signer ExtSigner) (*types.Extrinsic, error)
- func (p *Pallet) BuildQuery(variable string, args ...[]byte) (types.StorageKey, error)
- func (p *Pallet) Subscribe(pastBlocks types.BlockNumber) (*EventSource, error)
- func (p *Pallet) Transact(ext *types.Extrinsic) (*ExtStatusSub, error)
- type StorageQueryer
- type TimePoint
- type Timeout
Constants ¶
const ( // PlankPerDot number of planks per Dot. PlankPerDot = 1e12 // PrintPrecision is the precision with which floats are printed // as defined by big.Float.Text. PrintPrecision = 3 )
const ChanBuffSize = 1024
ChanBuffSize is the buffer size of an EventSource. GSRPC buffers up to 20k events.
const DefaultTimeoutPollInterval = time.Second
DefaultTimeoutPollInterval default value for the PollInterval of a Timeout.
Variables ¶
var ( // ErrUnknownError the error could not be decoded. ErrUnknownError = errors.New("unknown dispatch error type") // ErrCallFailed an extrinsic returned an error. ErrCallFailed = errors.New("call failed") )
var ErrWrongNodeVersion = errors.New("wrong node version")
ErrWrongNodeVersion returned if an invalid substrate node version was detected.
var SignaturePrefix = []byte("substrate")
SignaturePrefix is prepended by substrate to all messages before signing.
Functions ¶
func DecodeError ¶
func DecodeError(meta *types.Metadata, err types.DispatchError) error
DecodeError decodes an error into a human-readable form. Returns either ErrUnknownError or ErrCallFailed.
func ExtIsFinal ¶
func ExtIsFinal(status *types.ExtrinsicStatus) bool
ExtIsFinal returns whether an Extrinsic is final.
Types ¶
type API ¶
API wraps a gsrpc.SubstrateAPI in a thread-safe way.
func (*API) AccountInfo ¶
func (a *API) AccountInfo(addr types.AccountID) (AccountInfo, error)
AccountInfo returns the account info for an Address. Can be used to retrieve the free balance, nonce, and other.
func (*API) LastHeader ¶
LastHeader returns the last header.
func (*API) Network ¶
Network returns the ID of the network that the api is connected to. The value is cached on startup.
func (*API) PastBlock ¶
PastBlock queries `pastBlocks` into the past and returns the hash of the block. If `pastBlocks` is larger than the current block number, the genesis block is used.
func (*API) QueryAll ¶
func (a *API) QueryAll(keys []types.StorageKey, startBlock types.Hash) ([]types.StorageChangeSet, error)
QueryAll returns all entries for `keys` from `startBlock` to the last block.
func (*API) QueryOne ¶
func (a *API) QueryOne(pastBlocks types.BlockNumber, keys ...types.StorageKey) (*types.KeyValueOption, error)
QueryOne queries the storage and expects to read at least one value. PastBlocks defines how many blocks into the past the query should look. Returns the latest value that it read or an error if none was found within the last `pastBlocks` blocks.
func (*API) RuntimeVersion ¶
func (a *API) RuntimeVersion() (*types.RuntimeVersion, error)
RuntimeVersion queries and returns the last runtime version.
func (*API) Subscribe ¶
func (a *API) Subscribe(keys ...types.StorageKey) (*state.StorageSubscription, error)
Subscribe subscribes to multiple storage keys.
func (*API) SubscribeHeaders ¶
func (a *API) SubscribeHeaders() (*chain.NewHeadsSubscription, error)
SubscribeHeaders subscribes to new headers.
type AccountInfo ¶
type AccountInfo struct { Nonce types.U32 Consumers types.U32 Providers types.U32 Sufficients types.U32 Free types.U128 Reserved types.U128 MiscFrozen types.U128 FreeFrozen types.U128 }
AccountInfo replaces substrate.AccountInfo since it is outdated. This is advised by the GSRPC team.
type ChainReader ¶
type ChainReader interface { // Metadata returns the latest metadata. Metadata() *gsrpc.Metadata // BlockHash returns the block hash for the given block number. BlockHash(gsrpc.BlockNumber) (gsrpc.Hash, error) // HeaderLatest returns the last header. HeaderLatest() *gsrpc.Header }
ChainReader is used to query the on-chain state.
type Dot ¶
type Dot struct {
// contains filtered or unexported fields
}
Dot wraps a *big.Int and provides conversion and formatting for Dot values.
func NewDotFromPlank ¶
NewDotFromPlank creates a new Dot from the given amount of Planks.
func NewDotsFromPlanks ¶
NewDotsFromPlanks creates new Dots from the given amounts of Planks.
type EventKey ¶
type EventKey struct {
// contains filtered or unexported fields
}
EventKey identifies an event type that an EventSource can listen on.
func SystemAccountKey ¶
SystemAccountKey is the key to query an account.
func SystemEventsKey ¶
func SystemEventsKey() *EventKey
SystemEventsKey is the key of all system events.
type EventSource ¶
EventSource collects all events from the chain. Can then be used to filter out events, eg. for a specific pallet.
func NewEventSource ¶
func NewEventSource(api *API, pastBlocks types.BlockNumber, keys ...*EventKey) (*EventSource, error)
NewEventSource returns a new EventSource. It queries pastBlocks into the past to retrieve old events and starts listening for new events with the passed EventKeys.
func (*EventSource) Err ¶
func (s *EventSource) Err() <-chan error
Err returns the error channel. Will be closed when the EventSource is closed.
func (*EventSource) Events ¶
func (s *EventSource) Events() <-chan types.EventRecordsRaw
Events returns a channel that contains all events that the EventSource found. This channel will never be closed.
type ExpiredTimeout ¶
type ExpiredTimeout struct{}
ExpiredTimeout is always expired. Implements the Perun Timeout interface.
func NewExpiredTimeout ¶
func NewExpiredTimeout() *ExpiredTimeout
NewExpiredTimeout returns a new ExpiredTimeout.
type ExtFactory ¶
type ExtFactory struct {
// contains filtered or unexported fields
}
ExtFactory can be used to build Extrinsics.
func NewExtFactory ¶
func NewExtFactory(sub *API) *ExtFactory
NewExtFactory returns a new ExtFactory.
func (*ExtFactory) BuildExt ¶
func (b *ExtFactory) BuildExt(name *ExtName, args []interface{}) (*types.Extrinsic, error)
BuildExt returns a new Extrinsic with the given args.
func (*ExtFactory) SigOptions ¶
func (b *ExtFactory) SigOptions(addr types.AccountID) (*types.SignatureOptions, error)
SigOptions returns the default signature options for an address.
type ExtName ¶
type ExtName struct {
// contains filtered or unexported fields
}
ExtName identifies an Extrinsic by its name.
func NewExtName ¶
NewExtName creates a new ExtName for the given pallet and function name. Example: NewExtName("PerunModule", "deposit").
type ExtSigner ¶
type ExtSigner interface { // SignExt signs the extrinsic with the specified options and network. SignExt(*gsrpc.Extrinsic, gsrpc.SignatureOptions, NetworkID) error }
ExtSigner signs an Extrinsic by modifying it.
type ExtStatusPred ¶
type ExtStatusPred func(*types.ExtrinsicStatus) bool
ExtStatusPred can be used to filter the status of an Extrinsic.
type ExtStatusSub ¶
ExtStatusSub can be used to subscribe to the status of an Extrinsic.
func NewExtStatusSub ¶
func NewExtStatusSub(sub *author.ExtrinsicStatusSubscription) *ExtStatusSub
NewExtStatusSub returns a new ExtStatusSub and takes ownership of the passed sub.
func (*ExtStatusSub) WaitUntil ¶
func (e *ExtStatusSub) WaitUntil(ctx context.Context, until ExtStatusPred) error
WaitUntil waits until the predicate returns true or the context is cancelled. Can be used for example to wait until an Extrinsic is final with `ExtIsFinal`.
type Pallet ¶
type Pallet struct {
// contains filtered or unexported fields
}
Pallet binds to a pallet that is deployed on a substrate chain.
func (*Pallet) BuildExt ¶
func (p *Pallet) BuildExt(call *ExtName, args []interface{}, addr types.AccountID, signer ExtSigner) (*types.Extrinsic, error)
BuildExt builds and signs an extrinsic.
func (*Pallet) BuildQuery ¶
func (*Pallet) Subscribe ¶
func (p *Pallet) Subscribe(pastBlocks types.BlockNumber) (*EventSource, error)
Subscribe subscribes on all events of the pallet.
type StorageQueryer ¶
type StorageQueryer interface { // QueryOne queries the storage and expects to read at least one value. // PastBlocks defines how many blocks into the past the query should look. // Returns the latest value that it read or an error if none was found // within the last `pastBlocks` blocks. QueryOne(pastBlocks gsrpc.BlockNumber, keys ...gsrpc.StorageKey) (*gsrpc.KeyValueOption, error) // Subscribe subscribes to the changes of a storage key. Subscribe(keys ...gsrpc.StorageKey) (*state.StorageSubscription, error) // StorageKey builds a storage key. BuildKey(prefix, method string, args ...[]byte) (gsrpc.StorageKey, error) }
StorageQueryer can be used to query the on-chain state.
type Timeout ¶
Timeout can be used to wait until a specific timepoint is reached by the blockchain. Implements the Perun Timeout interface.
func NewTimeout ¶
NewTimeout returns a new Timeout which expires at the given time.