Skip to content

Commit

Permalink
refactor: cmd uses TTLer/Noncer
Browse files Browse the repository at this point in the history
  • Loading branch information
randomshinichi committed Nov 11, 2019
1 parent 87f5777 commit 2e3924f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 34 deletions.
61 changes: 32 additions & 29 deletions cmd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"github.com/aeternity/aepp-sdk-go/v6/transactions"
"github.com/aeternity/aepp-sdk-go/v6/utils"

"github.com/aeternity/aepp-sdk-go/v6/aeternity"

"github.com/spf13/cobra"
)

Expand All @@ -33,13 +31,13 @@ var txSpendCmd = &cobra.Command{
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
node := newAeNode()
ttlFunc := aeternity.GenerateGetTTL(node)
nonceFunc := aeternity.GenerateGetNextNonce(node)
ttlFunc := transactions.CreateTTLer(node)
nonceFunc := transactions.CreateNoncer(node)
return txSpendFunc(ttlFunc, nonceFunc, args)
},
}

func txSpendFunc(ttlFunc aeternity.GetTTLFunc, nonceFunc aeternity.GetNextNonceFunc, args []string) (err error) {
func txSpendFunc(ttlFunc transactions.TTLer, nonceFunc transactions.Noncer, args []string) (err error) {
var (
sender string
recipient string
Expand Down Expand Up @@ -69,23 +67,28 @@ func txSpendFunc(ttlFunc aeternity.GetTTLFunc, nonceFunc aeternity.GetNextNonceF

// If nonce was not specified as an argument, connect to the node to
// query it
if nonce == 0 {
nonce, err = nonceFunc(sender)
if err != nil {
return err
if nonce > 0 {
nonceFunc = func(accountID string) (uint64, error) {
return nonce, nil
}
}

// If TTL was not specified as an argument, connect to the node to calculate
// it
if ttl == 0 {
ttl, err = ttlFunc(config.Client.TTL)
if err != nil {
return err
if ttl > 0 {
ttlFunc = func(offset uint64) (uint64, error) {
return ttl, nil
}
}
ttlnoncer := transactions.CreateTTLNoncer(ttlFunc, nonceFunc)
tx, err := transactions.NewSpendTx(sender, recipient, amount, []byte(spendTxPayload), ttlnoncer)
if err != nil {
return err
}

if feeBigInt.Cmp(big.NewInt(0)) != 0 {
tx.SetFee(feeBigInt)
}

tx := transactions.NewSpendTx(sender, recipient, amount, feeBigInt, []byte(spendTxPayload), ttl, nonce)
base64Tx, err := transactions.SerializeTx(tx)
if err != nil {
return err
Expand All @@ -112,8 +115,8 @@ var txContractCreateCmd = &cobra.Command{
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
node := newAeNode()
ttlFunc := aeternity.GenerateGetTTL(node)
nonceFunc := aeternity.GenerateGetNextNonce(node)
ttlFunc := transactions.CreateTTLer(node)
nonceFunc := transactions.CreateNoncer(node)
return txContractCreateFunc(ttlFunc, nonceFunc, args)
},
}
Expand All @@ -123,7 +126,7 @@ type getHeightAccounter interface {
naet.GetAccounter
}

func txContractCreateFunc(ttlFunc aeternity.GetTTLFunc, nonceFunc aeternity.GetNextNonceFunc, args []string) (err error) {
func txContractCreateFunc(ttlFunc transactions.TTLer, nonceFunc transactions.Noncer, args []string) (err error) {
var (
owner string
contract string
Expand All @@ -146,24 +149,24 @@ func txContractCreateFunc(ttlFunc aeternity.GetTTLFunc, nonceFunc aeternity.GetN

// If nonce was not specified as an argument, connect to the node to
// query it
if nonce == 0 {
nonce, err = nonceFunc(owner)
if err != nil {
return err
if nonce > 0 {
nonceFunc = func(accountID string) (uint64, error) {
return nonce, nil
}
}

// If TTL was not specified as an argument, connect to the node to calculate
// it
if ttl == 0 {
ttl, err = ttlFunc(config.Client.TTL)
if err != nil {
return err
if ttl > 0 {
ttlFunc = func(offset uint64) (uint64, error) {
return ttl, nil
}
}
ttlnoncer := transactions.CreateTTLNoncer(ttlFunc, nonceFunc)

tx := transactions.NewContractCreateTx(owner, nonce, contract, config.Client.Contracts.VMVersion, config.Client.Contracts.ABIVersion, config.Client.Contracts.Deposit, config.Client.Contracts.Amount, config.Client.Contracts.GasLimit, config.Client.GasPrice, config.Client.Fee, ttl, calldata)

tx, err := transactions.NewContractCreateTx(owner, contract, config.Client.Contracts.VMVersion, config.Client.Contracts.ABIVersion, config.Client.Contracts.Deposit, config.Client.Contracts.Amount, config.Client.Contracts.GasLimit, config.Client.GasPrice, calldata, ttlnoncer)
if err != nil {
return err
}
txStr, err := transactions.SerializeTx(tx)
if err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions cmd/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"github.com/aeternity/aepp-sdk-go/v6/config"
"github.com/aeternity/aepp-sdk-go/v6/aeternity"
"github.com/aeternity/aepp-sdk-go/v6/transactions"
"github.com/spf13/cobra"
)

Expand All @@ -13,8 +13,8 @@ var bob = "ak_Egp9yVdpxmvAfQ7vsXGvpnyfNq71msbdUpkMNYGTeTe8kPL3v"

func Test_txSpendFunc(t *testing.T) {
type args struct {
ttlFunc aeternity.GetTTLFunc
nonceFunc aeternity.GetNextNonceFunc
ttlFunc transactions.TTLer
nonceFunc transactions.Noncer
args []string
}
tests := []struct {
Expand Down Expand Up @@ -66,8 +66,8 @@ func TestTxDumpRaw(t *testing.T) {

func Test_txContractCreateFunc(t *testing.T) {
type args struct {
ttlFunc aeternity.GetTTLFunc
nonceFunc aeternity.GetNextNonceFunc
ttlFunc transactions.TTLer
nonceFunc transactions.Noncer
args []string
}
tests := []struct {
Expand Down

0 comments on commit 2e3924f

Please sign in to comment.