-
Notifications
You must be signed in to change notification settings - Fork 179
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can sign and send transactions to moonbeam? #399
Comments
@YuXiaoCoder Please check the following issue for more details about signing and submitting transactions: |
package main
import (
"fmt"
"math/big"
gsrpc "github.com/centrifuge/go-substrate-rpc-client/v4"
"github.com/centrifuge/go-substrate-rpc-client/v4/signature"
"github.com/centrifuge/go-substrate-rpc-client/v4/types"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/codec"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/extrinsic"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/extrinsic/extensions"
"go.uber.org/zap"
)
func main() {
mnemonic := "XXXXXX"
// 从助记词派生出密钥对
keypair, err := signature.KeyringPairFromSecret(mnemonic+"/m/44'/60'/0'/0/0", uint16(1284))
if err != nil {
zap.S().Warnf("failed to derive key pair: %v", err)
return
}
fmt.Println(keypair.Address)
// Instantiate the API
api, err := gsrpc.NewSubstrateAPI("wss://moonriver-rpc.dwellir.com")
if err != nil {
panic(err)
}
meta, err := api.RPC.State.GetMetadataLatest()
if err != nil {
panic(err)
}
// Create a call, transferring 1e10 units to Bob
bob, err := types.NewMultiAddressFromHexAccountID("0x80893554E58bBCCa39faA3d88573e88f94145d86")
if err != nil {
panic(err)
}
// 1 unit of transfer
bal, ok := new(big.Int).SetString("1000000000000", 10)
if !ok {
panic(fmt.Errorf("failed to convert balance"))
}
c, err := types.NewCall(meta, "Balances.transfer_keep_alive", bob, types.NewUCompact(bal))
if err != nil {
panic(err)
}
ext := extrinsic.NewDynamicExtrinsic(&c)
genesisHash, err := api.RPC.Chain.GetBlockHash(0)
if err != nil {
panic(err)
}
rv, err := api.RPC.State.GetRuntimeVersionLatest()
if err != nil {
panic(err)
}
key, err := types.CreateStorageKey(meta, "System", "Account", keypair.PublicKey)
if err != nil {
panic(err)
}
var accountInfo types.AccountInfo
ok, err = api.RPC.State.GetStorageLatest(key, &accountInfo)
if err != nil || !ok {
panic(err)
}
err = ext.Sign(
keypair,
meta,
extrinsic.WithEra(types.ExtrinsicEra{IsImmortalEra: true}, genesisHash),
extrinsic.WithNonce(types.NewUCompactFromUInt(uint64(accountInfo.Nonce))),
extrinsic.WithTip(types.NewUCompactFromUInt(0)),
extrinsic.WithSpecVersion(rv.SpecVersion),
extrinsic.WithTransactionVersion(rv.TransactionVersion),
extrinsic.WithGenesisHash(genesisHash),
extrinsic.WithMetadataMode(extensions.CheckMetadataModeDisabled, extensions.CheckMetadataHash{Hash: types.NewEmptyOption[types.H256]()}),
extrinsic.WithAssetID(types.NewEmptyOption[types.AssetID]()),
)
if err != nil {
panic(err)
}
encodedExt, err := codec.EncodeToHex(ext)
if err != nil {
panic(err)
}
fmt.Printf("Ext - %s\n", encodedExt)
sub, err := api.RPC.Author.SubmitAndWatchDynamicExtrinsic(ext)
if err != nil {
panic(err)
}
defer sub.Unsubscribe()
for {
select {
case st := <-sub.Chan():
extStatus, _ := st.MarshalJSON()
fmt.Printf("Status for transaction - %s\n", string(extStatus))
case err := <-sub.Err():
panic(err)
}
}
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do I sign and send a transaction to Moonbeam, I understand Polkadot's method!
The text was updated successfully, but these errors were encountered: