Skip to content

Commit

Permalink
Add FixNonce config in TransactionConfig to fix nonce at 0
Browse files Browse the repository at this point in the history
Signed-off-by: Yilun <[email protected]>
  • Loading branch information
yilunzhang committed Apr 1, 2022
1 parent 3295767 commit dd95a28
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
5 changes: 4 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,17 +249,20 @@ func (c *RPCConfig) RPCGetConcurrency() int32 {
return c.RPCConcurrency
}

// TransactionConfig is the config for making a transaction.
// TransactionConfig is the config for making a transaction. If Nonce is 0 and
// FixNonce is false, then nonce will be fetched from RPC call.
type TransactionConfig struct {
Fee string
Nonce int64 // nonce is changed to signed int for gomobile compatibility
FixNonce bool
Attributes []byte
}

// DefaultTransactionConfig is the default TransactionConfig.
var DefaultTransactionConfig = TransactionConfig{
Fee: "0",
Nonce: 0,
FixNonce: false,
Attributes: nil,
}

Expand Down
14 changes: 7 additions & 7 deletions rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/hex"
"encoding/json"
"errors"
"github.com/nknorg/nkngomobile"
"io/ioutil"
"log"
"net/http"
Expand All @@ -17,6 +16,7 @@ import (
nknConfig "github.com/nknorg/nkn/v2/config"
"github.com/nknorg/nkn/v2/program"
"github.com/nknorg/nkn/v2/transaction"
"github.com/nknorg/nkngomobile"
)

// Signer is the interface that can sign transactions.
Expand Down Expand Up @@ -534,7 +534,7 @@ func TransferContext(ctx context.Context, s signerRPCClient, address, amount str
}

nonce := config.Nonce
if nonce == 0 {
if nonce == 0 && !config.FixNonce {
nonce, err = s.GetNonce(true)
if err != nil {
return "", err
Expand Down Expand Up @@ -580,7 +580,7 @@ func RegisterNameContext(ctx context.Context, s signerRPCClient, name string, co
}

nonce := config.Nonce
if nonce == 0 {
if nonce == 0 && !config.FixNonce {
nonce, err = s.GetNonce(true)
if err != nil {
return "", err
Expand Down Expand Up @@ -623,7 +623,7 @@ func TransferNameContext(ctx context.Context, s signerRPCClient, name string, re
}

nonce := config.Nonce
if nonce == 0 {
if nonce == 0 && !config.FixNonce {
nonce, err = s.GetNonce(true)
if err != nil {
return "", err
Expand Down Expand Up @@ -665,7 +665,7 @@ func DeleteNameContext(ctx context.Context, s signerRPCClient, name string, conf
}

nonce := config.Nonce
if nonce == 0 {
if nonce == 0 && !config.FixNonce {
nonce, err = s.GetNonce(true)
if err != nil {
return "", err
Expand Down Expand Up @@ -713,7 +713,7 @@ func SubscribeContext(ctx context.Context, s signerRPCClient, identifier, topic
}

nonce := config.Nonce
if nonce == 0 {
if nonce == 0 && !config.FixNonce {
nonce, err = s.GetNonce(true)
if err != nil {
return "", err
Expand Down Expand Up @@ -764,7 +764,7 @@ func UnsubscribeContext(ctx context.Context, s signerRPCClient, identifier, topi
}

nonce := config.Nonce
if nonce == 0 {
if nonce == 0 && !config.FixNonce {
nonce, err = s.GetNonce(true)
if err != nil {
return "", err
Expand Down

0 comments on commit dd95a28

Please sign in to comment.