Polkadot
There is a big difference between Polkadot and Ethereum’s JSON-RPC method.
You can call the rpc_methods method to view the list of methods supported by Polkadot.
The official document has more detailed information :
We provide support for the following network types :
- Polkadot Realy Mainnet
package main
import (
"fmt"
"github.com/centrifuge/go-substrate-rpc-client/client"
"github.com/centrifuge/go-substrate-rpc-client/rpc/author"
"github.com/centrifuge/go-substrate-rpc-client/rpc/chain"
"github.com/centrifuge/go-substrate-rpc-client/rpc/state"
"github.com/centrifuge/go-substrate-rpc-client/rpc/system"
)
func main() {
const url_auth = "https://username:password@apis.ankr.com/xxxxx/xxxxx/polkadot/full/main" // authentication
const url_token = "https://apis.ankr.com/xxxxx/xxxxx/polkadot/full/main" // token
cl,err := client.Connect("choose url_auth or url_token by your created type")
if err != nil {
panic(err)
}
newRPC, err := NewRPC(cl)
if err != nil {
panic(err)
}
hash, err := newRPC.Chain.GetFinalizedHead()
if err != nil {
panic(err)
}
fmt.Println(hash.Hex())
}
type RPC struct {
Author *author.Author
Chain *chain.Chain
State *state.State
System *system.System
Client *client.Client
}
func NewRPC(cl client.Client) (*RPC, error) {
st := state.NewState(cl)
return &RPC{
Author: author.NewAuthor(cl),
Chain: chain.NewChain(cl),
State: st,
System: system.NewSystem(cl),
client: cl,
}, nil
}
# authentication
$ curl -H "Content-Type: application/json" -u "username:password" -d '{"jsonrpc":"2.0","method":"chain_getBlock","params":[],"id":1}' https://apis.ankr.com/xxxxx/xxxxx/polkadot/full/main
$ curl -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"chain_getBlock","params":[],"id":1}' https://username:password@apis.ankr.com/xxxxx/xxxxx/polkadot/full/main
# token
$ curl -H "Content-Type: application/json" -d '{"jsonrpc":"2.0","method":"chain_getBlock","params":[],"id":1}' https://apis.ankr.com/xxxxx/xxxxx/polkadot/full/main
package main
import (
"fmt"
"github.com/centrifuge/go-substrate-rpc-client/client"
"github.com/centrifuge/go-substrate-rpc-client/rpc/author"
"github.com/centrifuge/go-substrate-rpc-client/rpc/chain"
"github.com/centrifuge/go-substrate-rpc-client/rpc/state"
"github.com/centrifuge/go-substrate-rpc-client/rpc/system"
"time"
)
func main() {
const url_auth = "wss://username:password@apis.ankr.com/wss/xxxxx/xxxxx/polkadot/full/main" // authentication
const url_token = "wss://apis.ankr.com/wss/xxxxx/xxxxx/polkadot/full/main" // token
cl,err := client.Connect("choose url_auth or url_token by your created type")
if err != nil {
panic(err)
}
newRPC, err := NewWebsocket(cl)
if err != nil {
panic(err)
}
sub, err := newRPC.Chain.SubscribeNewHeads()
if err != nil {
panic(err)
}
fmt.Println("---subscribe-----")
go func() {
time.Sleep(10 * time.Second)
fmt.Println("---unsubscribe-----")
sub.Unsubscribe()
}()
go func() {
for c := range sub.Chan {
fmt.Println(c.Number)
}
}()
<-sub.Err()
}
type Websocket struct {
Author *author.Author
Chain *chain.Chain
State *state.State
System *system.System
Client *client.Client
}
func NewWebsocket(cl client.Client) (*Websocket, error) {
st := state.NewState(cl)
return &Websocket{
Author: author.NewAuthor(cl),
Chain: chain.NewChain(cl),
State: st,
System: system.NewSystem(cl),
client: cl,
}, nil
}
# authentication
$ wscat -c wss://username:password@apis.ankr.com/wss/xxxxx/xxxxx/polkadot/full/main
# token
$ wscat -c wss://apis.ankr.com/wss/xxxxx/xxxxx/polkadot/full/main
wait connected...
# subscribe
> {"jsonrpc":"2.0","method":"chain_subscribeNewHeads","params":[],"id":1}
# unsubscribe
> {"jsonrpc":"2.0","method":"chain_unsubscribeNewHeads","params":["0xxxxxxxxxxxxxxx"],"id":1}