Skip to content

Commit

Permalink
feat: introduce DynamicExtrinsic type for compatibility (#32)
Browse files Browse the repository at this point in the history
Co-authored-by: Freddy Li <[email protected]>
  • Loading branch information
freddyli7 and Freddy Li authored Aug 20, 2024
1 parent e626c7d commit 2d403fe
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 341 deletions.
214 changes: 0 additions & 214 deletions chains/substrate/client/check_metadata_hash_enabled_client.go

This file was deleted.

44 changes: 29 additions & 15 deletions chains/substrate/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"bytes"
"context"
"fmt"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/extrinsic"
"github.com/centrifuge/go-substrate-rpc-client/v4/types/extrinsic/extensions"
"math/big"
"sync"
"time"
Expand Down Expand Up @@ -54,7 +56,8 @@ func (c *SubstrateClient) Transact(method string, args ...interface{}) (types.Ha
return types.Hash{}, nil, fmt.Errorf("failed to construct call: %w", err)
}

ext := types.NewExtrinsic(call)
ext := extrinsic.NewDynamicExtrinsic(&call)

// Get latest runtime version
rv, err := c.Conn.RPC.State.GetRuntimeVersionLatest()
if err != nil {
Expand All @@ -70,21 +73,22 @@ func (c *SubstrateClient) Transact(method string, args ...interface{}) (types.Ha
}

// Sign the extrinsic
o := types.SignatureOptions{
BlockHash: c.Conn.GenesisHash,
Era: types.ExtrinsicEra{IsMortalEra: false},
GenesisHash: c.Conn.GenesisHash,
Nonce: types.NewUCompactFromUInt(uint64(nonce)),
SpecVersion: rv.SpecVersion,
Tip: types.NewUCompactFromUInt(c.tip),
TransactionVersion: rv.TransactionVersion,
}
sub, err := c.submitAndWatchExtrinsic(o, &ext)
sub, err := c.submitAndWatchExtrinsic(
&meta,
ext,
extrinsic.WithEra(types.ExtrinsicEra{IsImmortalEra: true}, c.Conn.GenesisHash),
extrinsic.WithNonce(types.NewUCompactFromUInt(uint64(nonce))),
extrinsic.WithTip(types.NewUCompactFromUInt(c.tip)),
extrinsic.WithSpecVersion(rv.SpecVersion),
extrinsic.WithTransactionVersion(rv.TransactionVersion),
extrinsic.WithGenesisHash(c.Conn.GenesisHash),
extrinsic.WithMetadataMode(extensions.CheckMetadataModeDisabled, extensions.CheckMetadataHash{Hash: types.NewEmptyOption[types.H256]()}),
)
if err != nil {
return types.Hash{}, nil, fmt.Errorf("submission of extrinsic failed: %w", err)
}

hash, err := ExtrinsicHash(ext)
hash, err := DynamicExtrinsicHash(ext)
if err != nil {
return types.Hash{}, nil, err
}
Expand Down Expand Up @@ -144,13 +148,13 @@ func (c *SubstrateClient) nextNonce(meta *types.Metadata) (types.U32, error) {
return latestNonce, nil
}

func (c *SubstrateClient) submitAndWatchExtrinsic(opts types.SignatureOptions, ext *types.Extrinsic) (*author.ExtrinsicStatusSubscription, error) {
err := ext.Sign(*c.key, opts)
func (c *SubstrateClient) submitAndWatchExtrinsic(meta *types.Metadata, ext extrinsic.DynamicExtrinsic, opts ...extrinsic.SigningOption) (*author.ExtrinsicStatusSubscription, error) {
err := ext.Sign(*c.key, meta, opts...)
if err != nil {
return nil, err
}

sub, err := c.Conn.RPC.Author.SubmitAndWatchExtrinsic(*ext)
sub, err := c.Conn.RPC.Author.SubmitAndWatchDynamicExtrinsic(ext)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -211,3 +215,13 @@ func ExtrinsicHash(ext types.Extrinsic) (types.Hash, error) {
}
return types.NewHash(extHash.Bytes()), nil
}

func DynamicExtrinsicHash(ext extrinsic.DynamicExtrinsic) (types.Hash, error) {
extHash := bytes.NewBuffer([]byte{})
encoder := scale.NewEncoder(extHash)
err := ext.Encode(*encoder)
if err != nil {
return types.Hash{}, err
}
return types.NewHash(extHash.Bytes()), nil
}
Loading

0 comments on commit 2d403fe

Please sign in to comment.