Skip to content

Commit

Permalink
fix GetAccount & GetBalance helper functinos for cosmos v0.47
Browse files Browse the repository at this point in the history
  • Loading branch information
nddeluca committed Apr 3, 2024
1 parent 6000b08 commit 298cc65
Showing 1 changed file with 48 additions and 28 deletions.
76 changes: 48 additions & 28 deletions testing/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,31 @@ import (
"time"

rclient "github.com/coinbase/rosetta-sdk-go/client"
rpcclient "github.com/cometbft/cometbft/rpc/client"
rpchttpclient "github.com/cometbft/cometbft/rpc/client/http"
ctypes "github.com/cometbft/cometbft/rpc/core/types"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdktypes "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/query"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
kava "github.com/kava-labs/kava/app"
"github.com/kava-labs/rosetta-kava/configuration"
router "github.com/kava-labs/rosetta-kava/server"
rpcclient "github.com/cometbft/cometbft/rpc/client"
rpchttpclient "github.com/cometbft/cometbft/rpc/client/http"
ctypes "github.com/cometbft/cometbft/rpc/core/types"
)

//
// Rosetta Server
//
var config *configuration.Configuration
var server *httptest.Server

//
// Rosetta Client
//
var client *rclient.APIClient

//
// Tendermint RPC
//
var cdc *codec.LegacyAmino
var cdc codec.Codec
var interfaceRegistry codectypes.InterfaceRegistry
var rpc rpcclient.Client

// Test Settings
Expand Down Expand Up @@ -99,7 +97,8 @@ func TestMain(m *testing.M) {
)

encodingConfig := kava.MakeEncodingConfig()
cdc = encodingConfig.Amino
cdc = encodingConfig.Marshaler
interfaceRegistry = encodingConfig.InterfaceRegistry

client = rclient.NewAPIClient(clientConfig)

Expand All @@ -124,21 +123,27 @@ func GetAccount(address string, height int64) (authtypes.AccountI, error) {
return nil, err
}

bz, err := cdc.MarshalJSON(authtypes.QueryAccountRequest{Address: addr.String()})
bz, err := cdc.Marshal(&authtypes.QueryAccountRequest{Address: addr.String()})
if err != nil {
return nil, err
}

path := fmt.Sprintf("custom/%s/%s", authtypes.QuerierRoute, authtypes.QueryAccount)
path := "/cosmos.auth.v1beta1.Query/Account"
opts := rpcclient.ABCIQueryOptions{Height: height, Prove: false}

result, err := ParseABCIResult(rpc.ABCIQueryWithOptions(context.Background(), path, bz, opts))
if err != nil {
return nil, err
}

var resp authtypes.QueryAccountResponse
err = cdc.Unmarshal(result, &resp)
if err != nil {
return nil, err
}

var account authtypes.AccountI
err = cdc.UnmarshalJSON(result, &account)
err = interfaceRegistry.UnpackAny(resp.Account, &account)
if err != nil {
return nil, err
}
Expand All @@ -148,26 +153,41 @@ func GetAccount(address string, height int64) (authtypes.AccountI, error) {

// GetGalance returns the owned coins of an account at a specified height
func GetBalance(address sdktypes.AccAddress, height int64) (sdktypes.Coins, error) {
bz, err := cdc.MarshalJSON(banktypes.NewQueryAllBalancesRequest(address, nil))
if err != nil {
return nil, err
path := "/cosmos.bank.v1beta1.Query/AllBalances"
opts := rpcclient.ABCIQueryOptions{Height: height, Prove: false}
request := banktypes.QueryAllBalancesRequest{
Address: address.String(),
Pagination: &query.PageRequest{Key: nil, Limit: query.DefaultLimit},
}

path := fmt.Sprintf("custom/%s/%s", banktypes.QuerierRoute, banktypes.QueryAllBalances)
opts := rpcclient.ABCIQueryOptions{Height: height, Prove: false}
totalBalances := sdk.NewCoins()

result, err := ParseABCIResult(rpc.ABCIQueryWithOptions(context.Background(), path, bz, opts))
if err != nil {
return nil, err
}
for {
bz, err := cdc.Marshal(&request)
if err != nil {
return nil, err
}

var balance sdktypes.Coins
err = cdc.UnmarshalJSON(result, &balance)
if err != nil {
return nil, err
result, err := ParseABCIResult(rpc.ABCIQueryWithOptions(context.Background(), path, bz, opts))
if err != nil {
return nil, err
}

var resp banktypes.QueryAllBalancesResponse
err = cdc.Unmarshal(result, &resp)
if err != nil {
return nil, err
}

totalBalances = totalBalances.Add(resp.Balances...)

if resp.Pagination.NextKey == nil {
break
}
request.Pagination.Key = resp.Pagination.NextKey
}

return balance, nil
return totalBalances, nil
}

func ParseABCIResult(result *ctypes.ResultABCIQuery, err error) ([]byte, error) {
Expand Down

0 comments on commit 298cc65

Please sign in to comment.