Skip to content

Commit 45cab5d

Browse files
authored
add sdk changes for example (#2)
1 parent 8cd784a commit 45cab5d

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

src/sdk/callExtrinsic.go

+47
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,53 @@ import (
1616
"golang.org/x/crypto/blake2b"
1717
)
1818

19+
func CreateExtrinsic(api *SubstrateAPI, ext_call string, keyring signature.KeyringPair, AppID int, arg ...interface{}) (extrinsic.Extrinsic, error) {
20+
meta, err := api.RPC.State.GetMetadataLatest()
21+
if err != nil {
22+
return extrinsic.Extrinsic{}, err
23+
}
24+
call, err := types.NewCall(meta, ext_call, arg...)
25+
if err != nil {
26+
return extrinsic.Extrinsic{}, err
27+
}
28+
ext := extrinsic.NewExtrinsic(call)
29+
genesisHash, err := api.RPC.Chain.GetBlockHash(0)
30+
if err != nil {
31+
return extrinsic.Extrinsic{}, err
32+
}
33+
rv, err := api.RPC.State.GetRuntimeVersionLatest()
34+
if err != nil {
35+
return extrinsic.Extrinsic{}, err
36+
}
37+
key, err := types.CreateStorageKey(meta, "System", "Account", keyring.PublicKey)
38+
if err != nil {
39+
return extrinsic.Extrinsic{}, err
40+
}
41+
42+
var accountInfo types.AccountInfo
43+
ok, err := api.RPC.State.GetStorageLatest(key, &accountInfo)
44+
if err != nil || !ok {
45+
return extrinsic.Extrinsic{}, err
46+
}
47+
nonce := uint32(accountInfo.Nonce)
48+
options := extrinsic.SignatureOptions{
49+
BlockHash: genesisHash,
50+
Era: extrinsic.ExtrinsicEra{IsMortalEra: false},
51+
GenesisHash: genesisHash,
52+
Nonce: types.NewUCompactFromUInt(uint64(nonce)),
53+
SpecVersion: rv.SpecVersion,
54+
Tip: types.NewUCompactFromUInt(100),
55+
AppID: types.NewUCompactFromUInt(uint64(AppID)),
56+
TransactionVersion: rv.TransactionVersion,
57+
}
58+
59+
err = ext.Sign(keyring, options)
60+
if err != nil {
61+
panic(fmt.Sprintf("cannot sign:%v", err))
62+
}
63+
return ext, nil
64+
}
65+
1966
func NewExtrinsic(api *SubstrateAPI, ext_call string, keyring signature.KeyringPair, AppID int, arg ...interface{}) (types.Hash, error) {
2067
meta, err := api.RPC.State.GetMetadataLatest()
2168
if err != nil {

src/sdk/types/types.go

+5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ import (
55
)
66

77
type MultiAddress = types.MultiAddress
8+
89
type Call = types.Call
10+
type Metadata = types.Metadata
911
type Hash = types.Hash
1012
type U32 = types.U32
1113
type U264 = types.U64
1214
type U256 = types.U256
1315
type U128 = types.U128
16+
type Weight = types.Weight
17+
type DispatchClass = types.DispatchClass
18+
type PartialFee = types.UCompact
1419
type ChainProperties struct {
1520
IsEthereum bool
1621
SS58Format types.U32

0 commit comments

Comments
 (0)