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

Add construction endpoints #7854

Merged
merged 18 commits into from
Nov 16, 2020
Merged
31 changes: 31 additions & 0 deletions server/rosetta/cosmos/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,29 @@ func NewSingle(grpcEndpoint, tendermintEndpoint string, optsFunc ...OptionFunc)
}, nil
}

func (c *Client) AccountInfo(ctx context.Context, addr string, height *int64) (auth.AccountI, error) {
// if height is set, send height instruction to account
if height != nil {
strHeight := strconv.FormatInt(*height, 10)
ctx = metadata.AppendToOutgoingContext(ctx, grpctypes.GRPCBlockHeightHeader, strHeight)
}
// retrieve account info
accountInfo, err := c.auth.Account(ctx, &auth.QueryAccountRequest{
Address: addr,
})
if err != nil {
return nil, rosetta.FromGRPCToRosettaError(err)
}
// success
var account auth.AccountI
err = c.ir.UnpackAny(accountInfo.Account, &account)
if err != nil {
return nil, rosetta.WrapError(rosetta.ErrCodec, err.Error())
}

return account, nil
}

func (c *Client) Balances(ctx context.Context, addr string, height *int64) ([]sdk.Coin, error) {
if height != nil {
strHeight := strconv.FormatInt(*height, 10)
Expand Down Expand Up @@ -224,3 +247,11 @@ func (c *Client) Status(ctx context.Context) (*tmtypes.ResultStatus, error) {
}
return status, err
}

func (c *Client) GetTxConfig(ctx context.Context) client.TxConfig {
return c.client.TxConfig
}

func (c *Client) PostTx(ctx context.Context, txBytes []byte) (res *sdk.TxResponse, err error) {
return c.client.BroadcastTx(txBytes)
}
41 changes: 39 additions & 2 deletions server/rosetta/cosmos/conversion/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package conversion

import (
"fmt"
"strconv"
"strings"
"time"

"github.com/coinbase/rosetta-sdk-go/types"
Expand Down Expand Up @@ -65,7 +67,7 @@ func ResultTxSearchToTransaction(txs []*rosetta.SdkTxWithHash) []*types.Transact

// SdkTxResponseToOperations converts a tx response to operations
func SdkTxToOperations(tx sdk.Tx, hasError, withoutStatus bool) []*types.Operation {
return toOperations(tx.GetMsgs(), hasError, withoutStatus)
return ToOperations(tx.GetMsgs(), hasError, withoutStatus)
}

// TendermintTxsToTxIdentifiers converts a tendermint raw transaction into a rosetta tx identifier
Expand All @@ -85,7 +87,7 @@ func TendermintBlockToBlockIdentifier(block *tmcoretypes.ResultBlock) *types.Blo
}
}

func toOperations(msgs []sdk.Msg, hasError bool, withoutStatus bool) []*types.Operation {
func ToOperations(msgs []sdk.Msg, hasError bool, withoutStatus bool) []*types.Operation {
var operations []*types.Operation
for i, msg := range msgs {
switch msg.Type() { // nolint
Expand Down Expand Up @@ -132,6 +134,41 @@ func toOperations(msgs []sdk.Msg, hasError bool, withoutStatus bool) []*types.Op
return operations
}

// GetTransferTxDataFromOperations extracts the from and to addresses from a list of operations.
// We assume that it comes formated in the correct way. And that the balance of the sender is the same
// as the receiver operations.
func GetTransferTxDataFromOperations(ops []*types.Operation) (*banktypes.MsgSend, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

BuildMsgSendFromOperations could be more accurate.

var (
from, to sdk.AccAddress
sendAmt sdk.Coin
err error
)

for _, op := range ops {
if strings.HasPrefix(op.Amount.Value, "-") {
from, err = sdk.AccAddressFromBech32(op.Account.Address)
if err != nil {
return nil, err
}
} else {
to, err = sdk.AccAddressFromBech32(op.Account.Address)
if err != nil {
return nil, err
}

amount, err := strconv.ParseInt(op.Amount.Value, 10, 64)
if err != nil {
return nil, fmt.Errorf("invalid amount")
}

sendAmt = sdk.NewCoin(op.Amount.Currency.Symbol, sdk.NewInt(amount))
}
}

msg := banktypes.NewMsgSend(from, to, sdk.NewCoins(sendAmt))
return msg, nil
}

// TmPeersToRosettaPeers converts tendermint peers to rosetta ones
func TmPeersToRosettaPeers(peers []tmcoretypes.Peer) []*types.Peer {
converted := make([]*types.Peer, len(peers))
Expand Down
26 changes: 26 additions & 0 deletions server/rosetta/docker-compose.integration.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: "3"
services:
cosmos:
build:
context: ../../
dockerfile: server/rosetta/test/docker/node.dockerfile
command: ["simd", "start"]
ports:
- 9090:9090
- 26657:26657

rosetta:
build:
context: ../../
dockerfile: server/rosetta/Dockerfile
command: [
"./simd",
"rosetta",
"--blockchain", "cosmos",
"--network", "test_network",
"--tendermint", "cosmos:26657",
"--grpc", "cosmos:9090",
"--addr", ":8080",
]
ports:
- 8080:8080
8 changes: 7 additions & 1 deletion server/rosetta/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,13 @@ var (
// ErrUnsupportedCurve is returned when the curve specified is not supported
ErrUnsupportedCurve = NewError(15, "unsupported curve, expected secp256k1", false)
// ErrInvalidPubkey is returned when the public key is invalid
ErrInvalidPubkey = NewError(8, "invalid pubkey", false)
ErrInvalidPubkey = NewError(8, "invalid pubkey", false)
ErrInterpreting = NewError(1, "error interpreting data from node", false)
ErrInvalidAddress = NewError(7, "invalid address", false)
ErrInvalidMemo = NewError(11, "invalid memo", false)
ErrInvalidOperation = NewError(4, "invalid operation", false)
ErrInvalidRequest = NewError(6, "invalid request", false)
ErrInvalidTransaction = NewError(5, "invalid transaction", false)
)

// AllowedErrors lists all the rosetta allowed errors
Expand Down
Loading