Skip to content

Commit

Permalink
refactor(commonTransactionRequest): making common transaction request…
Browse files Browse the repository at this point in the history
… public

Signed-off-by: Deepanshu Tripathi <[email protected]>
  • Loading branch information
deepanshutr committed Sep 2, 2024
1 parent baf847a commit d22f8d7
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 166 deletions.
148 changes: 0 additions & 148 deletions helpers/base/common_transaction_request.go

This file was deleted.

155 changes: 137 additions & 18 deletions helpers/common_transaction_request.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,145 @@
package helpers

import (
"fmt"
"github.com/AssetMantle/modules/utilities/rest"
"github.com/cosmos/cosmos-sdk/client"
sdkTypes "github.com/cosmos/cosmos-sdk/types"
"net/http"
"strings"
)

type CommonTransactionRequest interface {
GetFrom() string
SetFrom(string) CommonTransactionRequest
GetChainID() string
SetChainID(string) CommonTransactionRequest
GetFees() sdkTypes.Coins
IsSimulated() bool
GetGasPrices() sdkTypes.DecCoins
GetTimeoutHeight() uint64
GetSequence() uint64
GetMemo() string
GetGas() string
GetGasAdjustment() string
GetAccountNumber() uint64
SetAccountNumber(uint64) CommonTransactionRequest
Validate() error
Sanitize() CommonTransactionRequest
ValidateBasic(responseWriter http.ResponseWriter) bool
type CommonTransactionRequest struct {
From string `json:"from"`
Memo string `json:"memo"`
TimeoutHeight uint64 `json:"timeoutHeight"`
AccountNumber uint64 `json:"accountNumber"`
Sequence uint64 `json:"sequence"`
ChainID string `json:"chainID"`
Gas string `json:"gas"`
Fees string `json:"fees"`
GasPrices string `json:"gasPrices"`
Simulate bool `json:"simulate"`
GasAdjustment string `json:"gasAdjustment"`
}

func (commonTransactionRequest CommonTransactionRequest) SetFrom(from string) CommonTransactionRequest {
commonTransactionRequest.From = from
return commonTransactionRequest
}
func (commonTransactionRequest CommonTransactionRequest) SetAccountNumber(accountNumber uint64) CommonTransactionRequest {
commonTransactionRequest.AccountNumber = accountNumber
return commonTransactionRequest
}
func (commonTransactionRequest CommonTransactionRequest) GetAccountNumber() uint64 {
return commonTransactionRequest.AccountNumber
}
func (commonTransactionRequest CommonTransactionRequest) GetGas() string {
return commonTransactionRequest.Gas
}
func (commonTransactionRequest CommonTransactionRequest) GetGasAdjustment() string {
return commonTransactionRequest.GasAdjustment
}
func (commonTransactionRequest CommonTransactionRequest) GetMemo() string {
return commonTransactionRequest.Memo
}
func (commonTransactionRequest CommonTransactionRequest) GetSequence() uint64 {
return commonTransactionRequest.Sequence
}
func (commonTransactionRequest CommonTransactionRequest) GetTimeoutHeight() uint64 {
return commonTransactionRequest.TimeoutHeight
}
func (commonTransactionRequest CommonTransactionRequest) GetGasPrices() sdkTypes.DecCoins {
decCoins, err := sdkTypes.ParseDecCoins(commonTransactionRequest.GasPrices)
if err != nil {
panic(err)
}

return decCoins
}
func (commonTransactionRequest CommonTransactionRequest) IsSimulated() bool {
return commonTransactionRequest.Simulate
}
func (commonTransactionRequest CommonTransactionRequest) GetFrom() string {
return commonTransactionRequest.From
}
func (commonTransactionRequest CommonTransactionRequest) GetChainID() string {
return commonTransactionRequest.ChainID
}
func (commonTransactionRequest CommonTransactionRequest) SetChainID(chainIDString string) CommonTransactionRequest {
commonTransactionRequest.ChainID = chainIDString
return commonTransactionRequest
}
func (commonTransactionRequest CommonTransactionRequest) GetFees() sdkTypes.Coins {
coins, err := sdkTypes.ParseCoinsNormalized(commonTransactionRequest.Fees)
if err != nil {
panic(err)
}

return coins
}
func (commonTransactionRequest CommonTransactionRequest) Validate() error {
if _, err := sdkTypes.AccAddressFromBech32(commonTransactionRequest.From); err != nil || len(commonTransactionRequest.From) == 0 {
return fmt.Errorf("invalid from address: %s", commonTransactionRequest.From)
}

if _, err := sdkTypes.ParseDecCoins(commonTransactionRequest.GasPrices); err != nil {
return fmt.Errorf("invalid gas prices %s", commonTransactionRequest.GasPrices)
}

if _, err := sdkTypes.ParseCoinsNormalized(commonTransactionRequest.Fees); err != nil {
return fmt.Errorf("invalid fees %s", commonTransactionRequest.Fees)
}

if !commonTransactionRequest.Simulate {
if len(commonTransactionRequest.ChainID) == 0 {
return fmt.Errorf("chain-id required but not specified")
}
if !commonTransactionRequest.GetFees().IsZero() && !commonTransactionRequest.GetGasPrices().IsZero() {
return fmt.Errorf("cannot provide both fees and gas prices")
}
if !commonTransactionRequest.GetFees().IsValid() && !commonTransactionRequest.GetGasPrices().IsValid() {
return fmt.Errorf("invalid fees or gas prices provided")
}
}

return nil
}
func (commonTransactionRequest CommonTransactionRequest) Sanitize() CommonTransactionRequest {
return NewCommonTransactionRequest(commonTransactionRequest.From, commonTransactionRequest.Memo, commonTransactionRequest.ChainID, commonTransactionRequest.Gas, commonTransactionRequest.GasAdjustment, commonTransactionRequest.AccountNumber, commonTransactionRequest.TimeoutHeight, commonTransactionRequest.Sequence, commonTransactionRequest.Fees, commonTransactionRequest.GasPrices, commonTransactionRequest.Simulate)
}
func (commonTransactionRequest CommonTransactionRequest) ValidateBasic(responseWriter http.ResponseWriter) bool {
if err := commonTransactionRequest.Validate(); err != nil {
rest.WriteErrorResponse(responseWriter, http.StatusBadRequest, err.Error())
return false
}

return true
}

func NewCommonTransactionRequest(from, memo, chainID, gas, gasAdjustment string, accountNumber, sequence, timeOutHeight uint64, fees, gasPrices string, simulate bool) CommonTransactionRequest {
return CommonTransactionRequest{
From: strings.TrimSpace(from),
Memo: strings.TrimSpace(memo),
ChainID: strings.TrimSpace(chainID),
Fees: strings.TrimSpace(fees),
GasPrices: strings.TrimSpace(gasPrices),
Gas: strings.TrimSpace(gas),
GasAdjustment: strings.TrimSpace(gasAdjustment),
AccountNumber: accountNumber,
Sequence: sequence,
TimeoutHeight: timeOutHeight,
Simulate: simulate,
}
}
func PrototypeCommonTransactionRequest() CommonTransactionRequest {
return CommonTransactionRequest{}
}

func NewCommonTransactionRequestFromContext(context client.Context) CommonTransactionRequest {
return CommonTransactionRequest{
From: context.FromAddress.String(),
ChainID: context.ChainID,
Simulate: context.Simulate,
}
}

0 comments on commit d22f8d7

Please sign in to comment.