Skip to content

Commit

Permalink
refactor: use cosmos sdk Int type for balances/token amounts (#679)
Browse files Browse the repository at this point in the history
* refactor: use csdk Int type for balances/token amounts

* chore: use ZeroInt func

(cherry picked from commit 8e02aef)

# Conflicts:
#	chain/cosmos/cosmos_chain.go
#	chain/polkadot/parachain_node.go
#	chain/polkadot/polkadot_chain.go
#	chain/polkadot/query.go
#	chain/polkadot/relay_chain_node.go
#	chain/polkadot/tx.go
#	conformance/flush.go
#	conformance/test.go
#	examples/cosmos/light_client_test.go
#	examples/hyperspace/hyperspace_test.go
#	examples/ibc/interchain_accounts_test.go
#	examples/ibc/learn_ibc_test.go
#	examples/ibc/packet_forward_test.go
#	examples/ibc/wasm/wasm_ibc_test.go
#	examples/ibc/wasm/wasm_icq_test.go
#	examples/polkadot/polkadot_chain_test.go
#	examples/polkadot/push_wasm_client_code_test.go
#	go.mod
#	ibc/types.go
#	interchain_test.go
#	internal/blockdb/messages_view_test.go
#	test_user.go
  • Loading branch information
jtieri authored and mergify[bot] committed Aug 2, 2023
1 parent ab3a123 commit 5e83413
Show file tree
Hide file tree
Showing 28 changed files with 2,859 additions and 124 deletions.
6 changes: 3 additions & 3 deletions chain/cosmos/chain_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ func (tn *ChainNode) SendIBCTransfer(
) (string, error) {
command := []string{
"ibc-transfer", "transfer", "transfer", channelID,
amount.Address, fmt.Sprintf("%d%s", amount.Amount, amount.Denom),
amount.Address, fmt.Sprintf("%s%s", amount.Amount.String(), amount.Denom),
}
if options.Timeout != nil {
if options.Timeout.NanoSeconds > 0 {
Expand All @@ -712,7 +712,7 @@ func (tn *ChainNode) SendIBCTransfer(
func (tn *ChainNode) SendFunds(ctx context.Context, keyName string, amount ibc.WalletAmount) error {
_, err := tn.ExecTx(ctx,
keyName, "bank", "send", keyName,
amount.Address, fmt.Sprintf("%d%s", amount.Amount, amount.Denom),
amount.Address, fmt.Sprintf("%s%s", amount.Amount.String(), amount.Denom),
)
return err
}
Expand Down Expand Up @@ -1275,7 +1275,7 @@ func (tn *ChainNode) SendICABankTransfer(ctx context.Context, connectionID, from
"amount": []map[string]any{
{
"denom": amount.Denom,
"amount": amount.Amount,
"amount": amount.Amount.String(),
},
},
})
Expand Down
15 changes: 10 additions & 5 deletions chain/cosmos/cosmos_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import (
"sync"
"time"

<<<<<<< HEAD
=======
"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
>>>>>>> 8e02aef (refactor: use cosmos sdk Int type for balances/token amounts (#679))
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
"github.com/cosmos/cosmos-sdk/crypto/hd"
Expand Down Expand Up @@ -473,23 +478,23 @@ func (c *CosmosChain) ExportState(ctx context.Context, height int64) (string, er

// GetBalance fetches the current balance for a specific account address and denom.
// Implements Chain interface
func (c *CosmosChain) GetBalance(ctx context.Context, address string, denom string) (int64, error) {
func (c *CosmosChain) GetBalance(ctx context.Context, address string, denom string) (math.Int, error) {
params := &bankTypes.QueryBalanceRequest{Address: address, Denom: denom}
grpcAddress := c.getFullNode().hostGRPCPort
conn, err := grpc.Dial(grpcAddress, grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return 0, err
return math.Int{}, err
}
defer conn.Close()

queryClient := bankTypes.NewQueryClient(conn)
res, err := queryClient.Balance(ctx, params)

if err != nil {
return 0, err
return math.Int{}, err
}

return res.Balance.Amount.Int64(), nil
return res.Balance.Amount, nil
}

// AllBalances fetches an account address's balance for all denoms it holds
Expand Down Expand Up @@ -781,7 +786,7 @@ func (c *CosmosChain) Start(testName string, ctx context.Context, additionalGene
}

for _, wallet := range additionalGenesisWallets {
if err := validator0.AddGenesisAccount(ctx, wallet.Address, []types.Coin{{Denom: wallet.Denom, Amount: types.NewInt(wallet.Amount)}}); err != nil {
if err := validator0.AddGenesisAccount(ctx, wallet.Address, []types.Coin{{Denom: wallet.Denom, Amount: wallet.Amount}}); err != nil {
return err
}
}
Expand Down
4 changes: 2 additions & 2 deletions chain/cosmos/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ func PollForBalance(ctx context.Context, chain *CosmosChain, deltaBlocks uint64,
if err != nil {
return nil, err
}
if bal != balance.Amount {
return nil, fmt.Errorf("balance (%d) does not match expected: (%d)", bal, balance.Amount)
if !balance.Amount.Equal(bal) {
return nil, fmt.Errorf("balance (%s) does not match expected: (%s)", bal.String(), balance.Amount.String())
}
return nil, nil
}
Expand Down
2 changes: 1 addition & 1 deletion chain/penumbra/penumbra_app_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func (p *PenumbraAppNode) GenerateGenesisFile(
}
allocationsCsv := []byte(`"amount","denom","address"\n`)
for _, allocation := range allocations {
allocationsCsv = append(allocationsCsv, []byte(fmt.Sprintf(`"%d","%s","%s"\n`, allocation.Amount, allocation.Denom, allocation.Address))...)
allocationsCsv = append(allocationsCsv, []byte(fmt.Sprintf(`"%s","%s","%s"\n`, allocation.Amount.String(), allocation.Denom, allocation.Address))...)
}
if err := fw.WriteFile(ctx, p.VolumeName, "allocations.csv", allocationsCsv); err != nil {
return fmt.Errorf("error writing allocations to file: %w", err)
Expand Down
13 changes: 7 additions & 6 deletions chain/penumbra/penumbra_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"

"cosmossdk.io/math"
"github.com/BurntSushi/toml"
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
Expand Down Expand Up @@ -64,9 +65,9 @@ type PenumbraValidatorFundingStream struct {
}

type PenumbraGenesisAppStateAllocation struct {
Amount int64 `json:"amount"`
Denom string `json:"denom"`
Address string `json:"address"`
Amount math.Int `json:"amount"`
Denom string `json:"denom"`
Address string `json:"address"`
}

func NewPenumbraChain(log *zap.Logger, testName string, chainConfig ibc.ChainConfig, numValidators int, numFullNodes int) *PenumbraChain {
Expand Down Expand Up @@ -227,7 +228,7 @@ func (c *PenumbraChain) Height(ctx context.Context) (uint64, error) {
}

// Implements Chain interface
func (c *PenumbraChain) GetBalance(ctx context.Context, address string, denom string) (int64, error) {
func (c *PenumbraChain) GetBalance(ctx context.Context, address string, denom string) (math.Int, error) {
panic("implement me")
}

Expand Down Expand Up @@ -425,13 +426,13 @@ func (c *PenumbraChain) Start(testName string, ctx context.Context, additionalGe

// self delegation
allocations[2*i] = PenumbraGenesisAppStateAllocation{
Amount: 100_000_000_000,
Amount: math.NewInt(100_000_000_000),
Denom: fmt.Sprintf("udelegation_%s", validatorTemplateDefinition.IdentityKey),
Address: validatorTemplateDefinition.FundingStreams[0].Address,
}
// liquid
allocations[2*i+1] = PenumbraGenesisAppStateAllocation{
Amount: 1_000_000_000_000,
Amount: math.NewInt(1_000_000_000_000),
Denom: chainCfg.Denom,
Address: validatorTemplateDefinition.FundingStreams[0].Address,
}
Expand Down
Loading

0 comments on commit 5e83413

Please sign in to comment.