From f5499663d45669893a4ac8e64fd4b225ee2ad7fb Mon Sep 17 00:00:00 2001 From: Andrew Chiw Date: Fri, 18 Oct 2019 15:29:22 +0200 Subject: [PATCH] refactor: Oracle*Tx constructors now do the work of Context --- aeternity/helpers.go | 48 -------------------------------------- transactions/tx_oracles.go | 45 ++++++++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 56 deletions(-) diff --git a/aeternity/helpers.go b/aeternity/helpers.go index 079ceb3c..44c21f4c 100644 --- a/aeternity/helpers.go +++ b/aeternity/helpers.go @@ -90,54 +90,6 @@ func NewContextFromNode(node *naet.Node, address string) (ctx *Context) { 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 *transactions.OracleRegisterTx, err error) { - ttl, nonce, err := c.GetTTLNonce(c.Address, config.Client.TTL) - if err != nil { - return - } - - 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 *transactions.OracleExtendTx, err error) { - ttl, nonce, err := c.GetTTLNonce(c.Address, config.Client.TTL) - if err != nil { - return - } - - 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 *transactions.OracleQueryTx, err error) { - ttl, nonce, err := c.GetTTLNonce(c.Address, config.Client.TTL) - if err != nil { - return - } - - 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 *transactions.OracleRespondTx, err error) { - ttl, nonce, err := c.GetTTLNonce(c.Address, config.Client.TTL) - if err != nil { - return - } - - 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 *transactions.ContractCreateTx, err error) { diff --git a/transactions/tx_oracles.go b/transactions/tx_oracles.go index 6ed59142..a2297223 100644 --- a/transactions/tx_oracles.go +++ b/transactions/tx_oracles.go @@ -5,6 +5,7 @@ import ( "math/big" "github.com/aeternity/aepp-sdk-go/v6/binary" + "github.com/aeternity/aepp-sdk-go/v6/config" "github.com/aeternity/aepp-sdk-go/v6/swagguard/node/models" "github.com/aeternity/aepp-sdk-go/v6/utils" rlp "github.com/randomshinichi/rlpae" @@ -159,8 +160,15 @@ func (tx *OracleRegisterTx) GetGasLimit() *big.Int { } // NewOracleRegisterTx is a constructor for a OracleRegisterTx struct -func NewOracleRegisterTx(accountID string, accountNonce uint64, querySpec, responseSpec string, queryFee *big.Int, oracleTTLType, oracleTTLValue uint64, abiVersion uint16, txFee *big.Int, txTTL uint64) *OracleRegisterTx { - return &OracleRegisterTx{accountID, accountNonce, querySpec, responseSpec, queryFee, oracleTTLType, oracleTTLValue, abiVersion, txFee, txTTL} +func NewOracleRegisterTx(accountID string, querySpec, responseSpec string, queryFee *big.Int, oracleTTLType, oracleTTLValue uint64, abiVersion uint16, ttlnoncer TTLNoncer) (tx *OracleRegisterTx, err error) { + ttl, accountNonce, err := ttlnoncer(accountID, config.Client.TTL) + if err != nil { + return + } + + tx = &OracleRegisterTx{accountID, accountNonce, querySpec, responseSpec, queryFee, oracleTTLType, oracleTTLValue, abiVersion, config.Client.Fee, ttl} + CalculateFee(tx) + return } // OracleExtendTx represents a transaction that extends the lifetime of an oracle @@ -275,8 +283,15 @@ func (tx *OracleExtendTx) GetGasLimit() *big.Int { } // NewOracleExtendTx is a constructor for a OracleExtendTx struct -func NewOracleExtendTx(oracleID string, accountNonce, oracleTTLType, oracleTTLValue uint64, Fee *big.Int, TTL uint64) *OracleExtendTx { - return &OracleExtendTx{oracleID, accountNonce, oracleTTLType, oracleTTLValue, Fee, TTL} +func NewOracleExtendTx(oracleID string, accountNonce, oracleTTLType, oracleTTLValue uint64, ttlnoncer TTLNoncer) (tx *OracleExtendTx, err error) { + ttl, accountNonce, err := ttlnoncer(oracleID, config.Client.TTL) + if err != nil { + return + } + + tx = &OracleExtendTx{oracleID, accountNonce, oracleTTLType, oracleTTLValue, config.Client.Fee, ttl} + CalculateFee(tx) + return } // OracleQueryTx represents a transaction that a program sends to query an oracle @@ -427,8 +442,15 @@ func (tx *OracleQueryTx) GetGasLimit() *big.Int { } // NewOracleQueryTx is a constructor for a OracleQueryTx struct -func NewOracleQueryTx(SenderID string, AccountNonce uint64, OracleID, Query string, QueryFee *big.Int, QueryTTLType, QueryTTLValue, ResponseTTLType, ResponseTTLValue uint64, Fee *big.Int, TTL uint64) *OracleQueryTx { - return &OracleQueryTx{SenderID, AccountNonce, OracleID, Query, QueryFee, QueryTTLType, QueryTTLValue, ResponseTTLType, ResponseTTLValue, Fee, TTL} +func NewOracleQueryTx(senderID string, oracleID, query string, queryFee *big.Int, queryTTLType, queryTTLValue, responseTTLType, responseTTLValue uint64, ttlnoncer TTLNoncer) (tx *OracleQueryTx, err error) { + ttl, accountNonce, err := ttlnoncer(senderID, config.Client.TTL) + if err != nil { + return + } + + tx = &OracleQueryTx{senderID, accountNonce, oracleID, query, queryFee, queryTTLType, queryTTLValue, responseTTLType, responseTTLValue, config.Client.Fee, ttl} + CalculateFee(tx) + return } // OracleRespondTx represents a transaction that an oracle sends to respond to an incoming query @@ -560,6 +582,13 @@ func (tx *OracleRespondTx) GetGasLimit() *big.Int { } // NewOracleRespondTx is a constructor for a OracleRespondTx struct -func NewOracleRespondTx(OracleID string, AccountNonce uint64, QueryID string, Response string, TTLType uint64, TTLValue uint64, Fee *big.Int, TTL uint64) *OracleRespondTx { - return &OracleRespondTx{OracleID, AccountNonce, QueryID, Response, TTLType, TTLValue, Fee, TTL} +func NewOracleRespondTx(oracleID string, queryID string, response string, responseTTLType uint64, responseTTLValue uint64, ttlnoncer TTLNoncer) (tx *OracleRespondTx, err error) { + ttl, accountNonce, err := ttlnoncer(oracleID, config.Client.TTL) + if err != nil { + return + } + + tx = &OracleRespondTx{oracleID, accountNonce, queryID, response, responseTTLType, responseTTLValue, config.Client.Fee, ttl} + CalculateFee(tx) + return }