Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor construction API #51

Merged
merged 14 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion server/services/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,16 @@ func fromObjectsMap(obj objectsMap, value any) error {
}

func isZeroAmount(amount string) bool {
return amount == "" || amount == "0" || amount == "-0"
if amount == "" {
return true
}

value, ok := big.NewInt(0).SetString(amount, 10)
if ok {
return value.Sign() == 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

return false
}

func getMagnitudeOfAmount(amount string) string {
Expand Down
1 change: 1 addition & 0 deletions server/services/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func Test_IsZeroAmount(t *testing.T) {
require.True(t, isZeroAmount(""))
require.True(t, isZeroAmount("0"))
require.True(t, isZeroAmount("-0"))
require.True(t, isZeroAmount("00"))
require.False(t, isZeroAmount("1"))
require.False(t, isZeroAmount("-1"))
}
Expand Down
24 changes: 13 additions & 11 deletions server/services/constructionMetadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services
import (
"encoding/json"
"errors"
"fmt"

"github.com/ElrondNetwork/elrond-proxy-go/data"
)
Expand Down Expand Up @@ -50,16 +51,17 @@ func (metadata *constructionMetadata) toTransaction() (*data.Transaction, error)
return nil, err
}

tx := &data.Transaction{}
tx.Sender = metadata.Sender
tx.Receiver = metadata.Receiver
tx.Nonce = metadata.Nonce
tx.Value = metadata.Amount
tx.GasLimit = metadata.GasLimit
tx.GasPrice = metadata.GasPrice
tx.Data = metadata.Data
tx.ChainID = metadata.ChainID
tx.Version = uint32(metadata.Version)
tx := &data.Transaction{
Sender: metadata.Sender,
Receiver: metadata.Receiver,
Nonce: metadata.Nonce,
Value: metadata.Amount,
GasLimit: metadata.GasLimit,
GasPrice: metadata.GasPrice,
Data: metadata.Data,
ChainID: metadata.ChainID,
Version: uint32(metadata.Version),
}

return tx, nil
}
Expand All @@ -78,7 +80,7 @@ func (metadata *constructionMetadata) validate() error {
return errors.New("missing metadata: 'gasPrice'")
}
if metadata.Version != 1 {
return errors.New("bad metadata: unexpected 'version'")
return fmt.Errorf("bad metadata: unexpected 'version' %v", metadata.Version)
}
if len(metadata.ChainID) == 0 {
return errors.New("missing metadata: 'chainID'")
Expand Down
4 changes: 2 additions & 2 deletions server/services/constructionMetadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ func TestConstructionMetadata_Validate(t *testing.T) {
Receiver: "bob",
GasLimit: 50000,
GasPrice: 1000000000,
}).validate(), "bad metadata: unexpected 'version'")
}).validate(), "bad metadata: unexpected 'version' 0")

require.ErrorContains(t, (&constructionMetadata{
Sender: "alice",
Receiver: "bob",
GasLimit: 50000,
GasPrice: 1000000000,
Version: 42,
}).validate(), "bad metadata: unexpected 'version'")
}).validate(), "bad metadata: unexpected 'version' 42")

require.ErrorContains(t, (&constructionMetadata{
Sender: "alice",
Expand Down
23 changes: 12 additions & 11 deletions server/services/constructionService.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,18 @@ func (service *constructionService) ConstructionMetadata(
return nil, errTyped
}

metadata := &constructionMetadata{}
metadata.Nonce = account.Account.Nonce
metadata.Sender = requestOptions.Sender
metadata.Receiver = requestOptions.Receiver
metadata.Amount = requestOptions.Amount
metadata.CurrencySymbol = requestOptions.CurrencySymbol
metadata.GasLimit = gasLimit
metadata.GasPrice = gasPrice
metadata.Data = computedData
metadata.ChainID = service.provider.GetNetworkConfig().NetworkID
metadata.Version = transactionVersion
metadata := &constructionMetadata{
Nonce: account.Account.Nonce,
Sender: requestOptions.Sender,
Receiver: requestOptions.Receiver,
Amount: requestOptions.Amount,
CurrencySymbol: requestOptions.CurrencySymbol,
GasLimit: gasLimit,
GasPrice: gasPrice,
Data: computedData,
ChainID: service.provider.GetNetworkConfig().NetworkID,
Version: transactionVersion,
}

metadataAsObjectsMap, err := toObjectsMap(metadata)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions version/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ const (
RosettaVersion = "v1.4.12"

// RosettaMiddlewareVersion is the version of this package (application)
RosettaMiddlewareVersion = "v0.2.8"
RosettaMiddlewareVersion = "v0.3.0"

// NodeVersion is the canonical version of the node runtime
// TODO: We should fetch this from node/status.
NodeVersion = "v1.3.43-rosetta1"
NodeVersion = "v1.3.44-rosetta1"
)