substrate

package module
v0.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 13, 2024 License: Apache-2.0 Imports: 11 Imported by: 3

README

substrate-api-rpc

Contents

Install

go get github.com/itering/substrate-api-rpc

Usage

Codec
Extrinsic Decode
metadataRaw := "" // rpc state_getMetadata
specVersion := 0  // rpc chain_getRuntimeVersion
encodeExtrinsic := []string{"0x280402000b10449a7e7301", "0x1c0407005e8b4100"}
decodeExtrinsics, err := substrate.DecodeExtrinsic(encodeExtrinsic, metadata.Process(metadataRaw), specVersion)
Event Decode
metadataRaw := "" // rpc state_getMetadata
specVersion := 0  // rpc chain_getRuntimeVersion
event = "0x080000000000000080e36a09000000000200000001000000000000ca9a3b00000000020000"
substrate.DecodeEvent(event, metadataInstant, specVersion)
Log Decode
logs := ["0x054241424501014a7024ec6c4be378c35c254860d8f4ddc6f9d53ea8ce42ca00bc77c280511f1cb4c93fbd825e3c7dcabb36221372a9b5359c496e095d31afc359bdb9fac45487"]
substrate.DecodeLogDigest(logs)
Storage Decode
raw := "0x2efb"
storage.Decode(raw, "i16", nil)
KeyRing
Generate KeyPair
keyRing := keyring.New(keyring.Sr25519Type, AliceSeed) // sr25519
keyRing := keyring.New(keyring.Ed25519Type, AliceSeed) // ed25519
Sign Message
keyRing.Sign("hello world") // sign utf-8 message
keyRing.Sign("0xffff")      // sign hex message
RPC
Substrate RPC

Example

state_getMetadata

blockHash := ""
rpc.GetMetadataByHash(conn, blockHash)

state_getStorage

validatorsRaw, err := rpc.ReadStorage(conn, "Session", "Validators", blockHash)
validatorList := validatorsRaw.ToStringSlice()
Send Extrinsic
// set websocket endoint 
websocket.SetEndpoint("wss://shibuya-rpc.dwellir.com")

client = &rpc.Client{}

// init latest metadata
raw, err := GetMetadataByHash(nil)
if err != nil {
    panic(err)
}

// set metadata
client.SetMetadata(metadata.RegNewMetadataType(92, raw))

// set sr25519 seed
client.SetKeyRing(keyring.New(keyring.Sr25519Type, AliceSeed))

// sign transaction
signedTransaction, err := client.SignTransaction("Balances", "transfer", map[string]interface{}{"Id": BobAccountId}, 12345)

// send transaction async, will return block hash
blockHash, err := client.SendAuthorSubmitAndWatchExtrinsic(signedTransaction)

// send transaction synchronize, will return transaction hash
transactionHash, err := client.SendAuthorSubmitExtrinsic(signedTransaction)

More information can be viewed https://polkadot.js.org/api/substrate/rpc.html

Contributions

We welcome contributions of any kind. Issues labeled can be good (first) contributions.

LICENSE

Apache-2.0

Documentation

Index

Constants

View Source
const (
	CidAura = 0x61727561
	CidBabe = 0x45424142
)

Variables

This section is empty.

Functions

func DecodeEvent

func DecodeEvent(rawList string, metadata *metadata.Instant, spec int) (r interface{}, err error)

DecodeEvent Event decode

func DecodeExtrinsic

func DecodeExtrinsic(rawList []string, metadata *metadata.Instant, spec int) (r []map[string]interface{}, err error)

Extrinsic decode

func DecodeLogDigest

func DecodeLogDigest(rawList []string) (r []storage.DecoderLog, err error)

LogDigest decode

func ExtractAuthor

func ExtractAuthor(data []byte, sessionValidators []string) string

func RegCustomTypes added in v0.0.5

func RegCustomTypes(sourceCode []byte)

Types

type Mortal

type Mortal struct {
	Period uint64
	Phase  uint64
}

func DecodeMortal

func DecodeMortal(era string) *Mortal

func (*Mortal) Birth

func (m *Mortal) Birth(current uint64) uint64

func (*Mortal) Death

func (m *Mortal) Death(current uint64) uint64

type PreRuntime

type PreRuntime struct {
	Data   string `json:"data"`
	Engine int64  `json:"engine"`
}

Directories

Path Synopsis
pkg
recws
Package recws provides websocket client based on gorilla/websocket that will automatically reconnect if the connection is dropped.
Package recws provides websocket client based on gorilla/websocket that will automatically reconnect if the connection is dropped.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL