Skip to content

Commit

Permalink
refactor: package models split into models, transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
randomshinichi committed Oct 10, 2019
1 parent f41d335 commit 06c5532
Show file tree
Hide file tree
Showing 16 changed files with 53 additions and 52 deletions.
54 changes: 27 additions & 27 deletions aeternity/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/aeternity/aepp-sdk-go/account"
"github.com/aeternity/aepp-sdk-go/binary"
"github.com/aeternity/aepp-sdk-go/config"
"github.com/aeternity/aepp-sdk-go/models"
"github.com/aeternity/aepp-sdk-go/naet"
"github.com/aeternity/aepp-sdk-go/transactions"
rlp "github.com/randomshinichi/rlpae"
)

Expand Down Expand Up @@ -148,20 +148,20 @@ func NewContextFromNode(node *naet.Node, address string) (ctx *Context) {

// SpendTx creates a spend transaction, filling in the account nonce and
// transaction TTL automatically.
func (c *Context) SpendTx(senderID string, recipientID string, amount, fee *big.Int, payload []byte) (tx *models.SpendTx, err error) {
func (c *Context) SpendTx(senderID string, recipientID string, amount, fee *big.Int, payload []byte) (tx *transactions.SpendTx, err error) {
txTTL, accountNonce, err := c.GetTTLNonce(c.Address, config.Client.TTL)
if err != nil {
return
}

// create the transaction
return models.NewSpendTx(senderID, recipientID, amount, fee, payload, txTTL, accountNonce), err
return transactions.NewSpendTx(senderID, recipientID, amount, fee, payload, txTTL, accountNonce), err
}

// NamePreclaimTx creates a name preclaim transaction, filling in the account
// nonce and transaction TTL automatically. It also generates a commitment ID
// and salt, later used to claim the name.
func (c *Context) NamePreclaimTx(name string, fee *big.Int) (tx *models.NamePreclaimTx, nameSalt *big.Int, err error) {
func (c *Context) NamePreclaimTx(name string, fee *big.Int) (tx *transactions.NamePreclaimTx, nameSalt *big.Int, err error) {
txTTL, accountNonce, err := c.GetTTLNonce(c.Address, config.Client.TTL)
if err != nil {
return
Expand All @@ -175,28 +175,28 @@ func (c *Context) NamePreclaimTx(name string, fee *big.Int) (tx *models.NamePrec
}

// build the transaction
tx = models.NewNamePreclaimTx(c.Address, cm, fee, txTTL, accountNonce)
tx = transactions.NewNamePreclaimTx(c.Address, cm, fee, txTTL, accountNonce)

return
}

// NameClaimTx creates a claim transaction, filling in the account nonce and
// transaction TTL automatically.
func (c *Context) NameClaimTx(name string, nameSalt, fee *big.Int) (tx *models.NameClaimTx, err error) {
func (c *Context) NameClaimTx(name string, nameSalt, fee *big.Int) (tx *transactions.NameClaimTx, err error) {
txTTL, accountNonce, err := c.GetTTLNonce(c.Address, config.Client.TTL)
if err != nil {
return
}

// create the transaction
tx = models.NewNameClaimTx(c.Address, name, nameSalt, fee, txTTL, accountNonce)
tx = transactions.NewNameClaimTx(c.Address, name, nameSalt, fee, txTTL, accountNonce)

return tx, err
}

// NameUpdateTx creates a name update transaction, filling in the account nonce
// and transaction TTL automatically.
func (c *Context) NameUpdateTx(name string, targetAddress string) (tx *models.NameUpdateTx, err error) {
func (c *Context) NameUpdateTx(name string, targetAddress string) (tx *transactions.NameUpdateTx, err error) {
txTTL, accountNonce, err := c.GetTTLNonce(c.Address, config.Client.TTL)
if err != nil {
return
Expand All @@ -208,108 +208,108 @@ func (c *Context) NameUpdateTx(name string, targetAddress string) (tx *models.Na
return
}
// create the transaction
tx = models.NewNameUpdateTx(c.Address, encodedNameHash, []string{targetAddress}, absNameTTL, config.Client.Names.ClientTTL, config.Client.Fee, txTTL, accountNonce)
tx = transactions.NewNameUpdateTx(c.Address, encodedNameHash, []string{targetAddress}, absNameTTL, config.Client.Names.ClientTTL, config.Client.Fee, txTTL, accountNonce)

return
}

// NameTransferTx creates a name transfer transaction, filling in the account
// nonce and transaction TTL automatically.
func (c *Context) NameTransferTx(name string, recipientAddress string) (tx *models.NameTransferTx, err error) {
func (c *Context) NameTransferTx(name string, recipientAddress string) (tx *transactions.NameTransferTx, err error) {
txTTL, accountNonce, err := c.GetTTLNonce(c.Address, config.Client.TTL)
if err != nil {
return
}

encodedNameHash := binary.Encode(binary.PrefixName, Namehash(name))

tx = models.NewNameTransferTx(c.Address, encodedNameHash, recipientAddress, config.Client.Fee, txTTL, accountNonce)
tx = transactions.NewNameTransferTx(c.Address, encodedNameHash, recipientAddress, config.Client.Fee, txTTL, accountNonce)
return
}

// NameRevokeTx creates a name revoke transaction, filling in the account nonce
// and transaction TTL automatically.
func (c *Context) NameRevokeTx(name string) (tx *models.NameRevokeTx, err error) {
func (c *Context) NameRevokeTx(name string) (tx *transactions.NameRevokeTx, err error) {
txTTL, accountNonce, err := c.GetTTLNonce(c.Address, config.Client.TTL)
if err != nil {
return
}

encodedNameHash := binary.Encode(binary.PrefixName, Namehash(name))

tx = models.NewNameRevokeTx(c.Address, encodedNameHash, config.Client.Fee, txTTL, accountNonce)
tx = transactions.NewNameRevokeTx(c.Address, encodedNameHash, config.Client.Fee, txTTL, accountNonce)
return
}

// OracleRegisterTx creates an oracle register transaction, filling in the
// account nonce and transaction TTL automatically.
func (c *Context) OracleRegisterTx(querySpec, responseSpec string, queryFee *big.Int, oracleTTLType, oracleTTLValue uint64, VMVersion uint16) (tx *models.OracleRegisterTx, err error) {
func (c *Context) OracleRegisterTx(querySpec, responseSpec string, queryFee *big.Int, oracleTTLType, oracleTTLValue uint64, VMVersion uint16) (tx *transactions.OracleRegisterTx, err error) {
ttl, nonce, err := c.GetTTLNonce(c.Address, config.Client.TTL)
if err != nil {
return
}

tx = models.NewOracleRegisterTx(c.Address, nonce, querySpec, responseSpec, queryFee, oracleTTLType, oracleTTLValue, VMVersion, config.Client.Fee, ttl)
tx = transactions.NewOracleRegisterTx(c.Address, nonce, querySpec, responseSpec, queryFee, oracleTTLType, oracleTTLValue, VMVersion, config.Client.Fee, ttl)
return tx, nil
}

// OracleExtendTx creates an oracle extend transaction, filling in the account
// nonce and transaction TTL automatically.
func (c *Context) OracleExtendTx(oracleID string, ttlType, ttlValue uint64) (tx *models.OracleExtendTx, err error) {
func (c *Context) OracleExtendTx(oracleID string, ttlType, ttlValue uint64) (tx *transactions.OracleExtendTx, err error) {
ttl, nonce, err := c.GetTTLNonce(c.Address, config.Client.TTL)
if err != nil {
return
}

tx = models.NewOracleExtendTx(oracleID, nonce, ttlType, ttlValue, config.Client.Fee, ttl)
tx = transactions.NewOracleExtendTx(oracleID, nonce, ttlType, ttlValue, config.Client.Fee, ttl)
return tx, nil
}

// OracleQueryTx creates an oracle query transaction, filling in the account
// nonce and transaction TTL automatically.
func (c *Context) OracleQueryTx(OracleID, Query string, QueryFee *big.Int, QueryTTLType, QueryTTLValue, ResponseTTLType, ResponseTTLValue uint64) (tx *models.OracleQueryTx, err error) {
func (c *Context) OracleQueryTx(OracleID, Query string, QueryFee *big.Int, QueryTTLType, QueryTTLValue, ResponseTTLType, ResponseTTLValue uint64) (tx *transactions.OracleQueryTx, err error) {
ttl, nonce, err := c.GetTTLNonce(c.Address, config.Client.TTL)
if err != nil {
return
}

tx = models.NewOracleQueryTx(c.Address, nonce, OracleID, Query, QueryFee, QueryTTLType, QueryTTLValue, ResponseTTLType, ResponseTTLValue, config.Client.Fee, ttl)
tx = transactions.NewOracleQueryTx(c.Address, nonce, OracleID, Query, QueryFee, QueryTTLType, QueryTTLValue, ResponseTTLType, ResponseTTLValue, config.Client.Fee, ttl)
return tx, nil
}

// OracleRespondTx creates an oracle response transaction, filling in the
// account nonce and transaction TTL automatically.
func (c *Context) OracleRespondTx(OracleID string, QueryID string, Response string, TTLType uint64, TTLValue uint64) (tx *models.OracleRespondTx, err error) {
func (c *Context) OracleRespondTx(OracleID string, QueryID string, Response string, TTLType uint64, TTLValue uint64) (tx *transactions.OracleRespondTx, err error) {
ttl, nonce, err := c.GetTTLNonce(c.Address, config.Client.TTL)
if err != nil {
return
}

tx = models.NewOracleRespondTx(OracleID, nonce, QueryID, Response, TTLType, TTLValue, config.Client.Fee, ttl)
tx = transactions.NewOracleRespondTx(OracleID, nonce, QueryID, Response, TTLType, TTLValue, config.Client.Fee, ttl)
return tx, nil
}

// ContractCreateTx creates a contract create transaction, filling in the
// account nonce and transaction TTL automatically.
func (c *Context) ContractCreateTx(Code string, CallData string, VMVersion, AbiVersion uint16, Deposit, Amount, GasLimit, Fee *big.Int) (tx *models.ContractCreateTx, err error) {
func (c *Context) ContractCreateTx(Code string, CallData string, VMVersion, AbiVersion uint16, Deposit, Amount, GasLimit, Fee *big.Int) (tx *transactions.ContractCreateTx, err error) {
ttl, nonce, err := c.GetTTLNonce(c.Address, config.Client.TTL)
if err != nil {
return
}

tx = models.NewContractCreateTx(c.Address, nonce, Code, VMVersion, AbiVersion, Deposit, Amount, GasLimit, config.Client.GasPrice, Fee, ttl, CallData)
tx = transactions.NewContractCreateTx(c.Address, nonce, Code, VMVersion, AbiVersion, Deposit, Amount, GasLimit, config.Client.GasPrice, Fee, ttl, CallData)
return tx, nil
}

// ContractCallTx creates a contract call transaction,, filling in the account
// nonce and transaction TTL automatically.
func (c *Context) ContractCallTx(ContractID, CallData string, AbiVersion uint16, Amount, GasLimit, GasPrice, Fee *big.Int) (tx *models.ContractCallTx, err error) {
func (c *Context) ContractCallTx(ContractID, CallData string, AbiVersion uint16, Amount, GasLimit, GasPrice, Fee *big.Int) (tx *transactions.ContractCallTx, err error) {
ttl, nonce, err := c.GetTTLNonce(c.Address, config.Client.TTL)
if err != nil {
return
}

tx = models.NewContractCallTx(c.Address, nonce, ContractID, Amount, GasLimit, GasPrice, AbiVersion, CallData, Fee, ttl)
tx = transactions.NewContractCallTx(c.Address, nonce, ContractID, Amount, GasLimit, GasPrice, AbiVersion, CallData, Fee, ttl)
return tx, nil
}

Expand Down Expand Up @@ -374,12 +374,12 @@ func WaitForTransactionForXBlocks(c getTransactionByHashHeighter, txHash string,

// SignBroadcastTransaction signs a transaction and broadcasts it to a node.
func SignBroadcastTransaction(tx rlp.Encoder, signingAccount *account.Account, n *naet.Node, networkID string) (signedTxStr, hash, signature string, err error) {
signedTx, hash, signature, err := models.SignHashTx(signingAccount, tx, networkID)
signedTx, hash, signature, err := transactions.SignHashTx(signingAccount, tx, networkID)
if err != nil {
return
}

signedTxStr, err = models.SerializeTx(signedTx)
signedTxStr, err = transactions.SerializeTx(signedTx)
if err != nil {
return
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (

"github.com/aeternity/aepp-sdk-go/account"
"github.com/aeternity/aepp-sdk-go/config"
"github.com/aeternity/aepp-sdk-go/models"
"github.com/aeternity/aepp-sdk-go/naet"
"github.com/aeternity/aepp-sdk-go/transactions"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -169,12 +169,12 @@ func signFunc(cmd *cobra.Command, args []string) (err error) {
}

txUnsignedBase64 := args[1]
tx, err := models.DeserializeTxStr(txUnsignedBase64)
tx, err := transactions.DeserializeTxStr(txUnsignedBase64)
if err != nil {
return err
}

txSignedBase64, txHash, signature, err := models.SignHashTx(account, tx, config.Node.NetworkID)
txSignedBase64, txHash, signature, err := transactions.SignHashTx(account, tx, config.Node.NetworkID)
if err != nil {
return err
}
Expand Down
10 changes: 5 additions & 5 deletions cmd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

"github.com/aeternity/aepp-sdk-go/binary"
"github.com/aeternity/aepp-sdk-go/config"
"github.com/aeternity/aepp-sdk-go/models"
"github.com/aeternity/aepp-sdk-go/naet"
"github.com/aeternity/aepp-sdk-go/transactions"
"github.com/aeternity/aepp-sdk-go/v5/utils"

"github.com/aeternity/aepp-sdk-go/v5/aeternity"
Expand Down Expand Up @@ -85,8 +85,8 @@ func txSpendFunc(ttlFunc aeternity.GetTTLFunc, nonceFunc aeternity.GetNextNonceF
}
}

tx := models.NewSpendTx(sender, recipient, amount, feeBigInt, []byte(spendTxPayload), ttl, nonce)
base64Tx, err := models.SerializeTx(tx)
tx := transactions.NewSpendTx(sender, recipient, amount, feeBigInt, []byte(spendTxPayload), ttl, nonce)
base64Tx, err := transactions.SerializeTx(tx)
if err != nil {
return err
}
Expand Down Expand Up @@ -162,9 +162,9 @@ func txContractCreateFunc(ttlFunc aeternity.GetTTLFunc, nonceFunc aeternity.GetN
}
}

tx := models.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 := 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)

txStr, err := models.SerializeTx(tx)
txStr, err := transactions.SerializeTx(tx)
if err != nil {
return err
}
Expand Down
11 changes: 6 additions & 5 deletions integration_test/ga_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/aeternity/aepp-sdk-go/config"
"github.com/aeternity/aepp-sdk-go/models"
"github.com/aeternity/aepp-sdk-go/naet"
"github.com/aeternity/aepp-sdk-go/transactions"
"github.com/aeternity/aepp-sdk-go/v5/aeternity"
"github.com/aeternity/aepp-sdk-go/v5/utils"
rlp "github.com/randomshinichi/rlpae"
Expand Down Expand Up @@ -76,7 +77,7 @@ func TestGeneralizedAccounts(t *testing.T) {
if err != nil {
t.Fatal(err)
}
gaTx := models.NewGAAttachTx(testAccount.Address, 1, authBytecode, auth.TypeInfo[0].FuncHash, config.Client.Contracts.VMVersion, config.Client.Contracts.ABIVersion, config.Client.BaseGas, config.Client.GasPrice, config.Client.Fee, ttl, authInitCalldata)
gaTx := transactions.NewGAAttachTx(testAccount.Address, 1, authBytecode, auth.TypeInfo[0].FuncHash, config.Client.Contracts.VMVersion, config.Client.Contracts.ABIVersion, config.Client.BaseGas, config.Client.GasPrice, config.Client.Fee, ttl, authInitCalldata)
_, txHash, _, err := aeternity.SignBroadcastTransaction(gaTx, testAccount, node, networkID)
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -110,15 +111,15 @@ func TestGeneralizedAccounts(t *testing.T) {
// authData is authorize(3)
authData := "cb_AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACBGtXufEG2HuMYcRcNwsGAeqymslunKf692bHnvwI5K6wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAU3aKBNm"
gas := utils.NewIntFromUint64(10000) // the node will fail the authentication if there isn't enough gas
spendTx := models.NewSpendTx(testAccount.Address, bob.Address, big.NewInt(5000), config.Client.Fee, []byte{}, ttl, 0)
gaMetaTx := models.NewGAMetaTx(testAccount.Address, authData, config.Client.Contracts.ABIVersion, gas, config.Client.GasPrice, config.Client.Fee, ttl, spendTx)
spendTx := transactions.NewSpendTx(testAccount.Address, bob.Address, big.NewInt(5000), config.Client.Fee, []byte{}, ttl, 0)
gaMetaTx := transactions.NewGAMetaTx(testAccount.Address, authData, config.Client.Contracts.ABIVersion, gas, config.Client.GasPrice, config.Client.Fee, ttl, spendTx)

gaMetaTxFinal, hash, _, err := models.SignHashTx(testAccount, gaMetaTx, config.Node.NetworkID)
gaMetaTxFinal, hash, _, err := transactions.SignHashTx(testAccount, gaMetaTx, config.Node.NetworkID)
if err != nil {
t.Fatal(err)
}

gaMetaTxStr, err := models.SerializeTx(gaMetaTxFinal)
gaMetaTxStr, err := transactions.SerializeTx(gaMetaTxFinal)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion models/transactions.go → transactions/transactions.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package transactions

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package transactions

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion models/tx_aens.go → transactions/tx_aens.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package transactions

import (
"io"
Expand Down
2 changes: 1 addition & 1 deletion models/tx_aens_test.go → transactions/tx_aens_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package transactions

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion models/tx_contracts.go → transactions/tx_contracts.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package transactions

import (
"io"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package transactions

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion models/tx_ga.go → transactions/tx_ga.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package transactions

import (
"io"
Expand Down
2 changes: 1 addition & 1 deletion models/tx_ga_test.go → transactions/tx_ga_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package transactions

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion models/tx_oracles.go → transactions/tx_oracles.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package transactions

import (
"io"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package transactions

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion models/tx_spend.go → transactions/tx_spend.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package transactions

import (
"io"
Expand Down
2 changes: 1 addition & 1 deletion models/tx_spend_test.go → transactions/tx_spend_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package models
package transactions

import (
"bytes"
Expand Down

0 comments on commit 06c5532

Please sign in to comment.