Skip to content

Commit

Permalink
(feat) Add possibility to transfer entire balance. (backport #6877) (#…
Browse files Browse the repository at this point in the history
…6930)

* (feat) Add possibility to transfer entire balance. (#6877)

* Add possibility to transfer entire balance.

* Added entry to the Changelog.

* Added e2e test

* Added forwarding

* Update modules/apps/transfer/keeper/relay.go

Co-authored-by: DimitrisJim <d.f.hilliard@gmail.com>

* Move UnboundedSpendLimit to token.go

* add documentation

* add test to compatibility matrices

* PR Feedback.

---------

Co-authored-by: DimitrisJim <d.f.hilliard@gmail.com>
Co-authored-by: Carlos Rodriguez <carlos@interchain.io>
(cherry picked from commit 92e1f38)

# Conflicts:
#	.github/compatibility-test-matrices/main/transfer-v2-multidenom-chain-a.json
#	.github/compatibility-test-matrices/release-v9.0.x/transfer-v2-multidenom-chain-a.json
#	.github/compatibility-test-matrices/unreleased/transfer-v2-multidenom.json
#	CHANGELOG.md
#	docs/docs/02-apps/01-transfer/04-messages.md
#	docs/docs/02-apps/01-transfer/10-ICS20-v1/04-messages.md
#	e2e/tests/transfer/base_test.go
#	modules/apps/transfer/keeper/relay.go
#	modules/apps/transfer/keeper/relay_test.go
#	modules/apps/transfer/types/token.go
#	modules/apps/transfer/types/transfer_authorization.go

* fix conflicts

* remove unused code

---------

Co-authored-by: Nikolas De Giorgis <nikolas.degiorgis@interchain.io>
Co-authored-by: Carlos Rodriguez <carlos@interchain.io>
  • Loading branch information
3 people authored Jul 25, 2024
1 parent d8789ab commit babe971
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"chain-a": [
"main"
],
"chain-b": [
"main"
],
"entrypoint": [
"TestTransferTestSuite"
],
"test": [
"TestMsgTransfer_Succeeds_Nonincentivized_MultiDenom",
"TestMsgTransfer_EntireBalance",
"TestMsgTransfer_Fails_InvalidAddress_MultiDenom"
],
"relayer-type": [
"hermes"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"chain-a": [
"release-v9.0.x"
],
"chain-b": [
"release-v9.0.x"
],
"entrypoint": [
"TestTransferTestSuite"
],
"test": [
"TestMsgTransfer_Succeeds_Nonincentivized_MultiDenom",
"TestMsgTransfer_EntireBalance",
"TestMsgTransfer_Fails_InvalidAddress_MultiDenom"
],
"relayer-type": [
"hermes"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"chain-a": [
"release-v9.0.x"
],
"chain-b": [
"release-v9.0.x"
],
"entrypoint": [
"TestTransferTestSuite"
],
"test": [
"TestMsgTransfer_Succeeds_Nonincentivized_MultiDenom",
"TestMsgTransfer_EntireBalance",
"TestMsgTransfer_Fails_InvalidAddress_MultiDenom"
],
"relayer-type": [
"hermes"
]
}
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Features

* (apps/transfer) [\#6877](https://github.com/cosmos/ibc-go/pull/6877) Added the possibility to transfer the entire user balance of a particular denomination by using [`UnboundedSpendLimit`](https://github.com/cosmos/ibc-go/blob/715f00eef8727da41db25fdd4763b709bdbba07e/modules/apps/transfer/types/transfer_authorization.go#L253-L255) as the token amount.

### Bug Fixes

## [v8.3.2](https://github.com/cosmos/ibc-go/releases/tag/v8.3.2) - 2024-06-20
Expand Down
5 changes: 5 additions & 0 deletions modules/apps/transfer/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func (k Keeper) sendTransfer(
telemetry.NewLabel(coretypes.LabelDestinationChannel, destinationChannel),
}

// Using types.UnboundedSpendLimit allows us to send the entire balance of a given denom.
if token.Amount.Equal(types.UnboundedSpendLimit()) {
token.Amount = k.bankKeeper.GetBalance(ctx, sender, token.Denom).Amount
}

// NOTE: SendTransfer simply sends the denomination as it exists on its own
// chain inside the packet data. The receiving chain will perform denom
// prefixing as necessary.
Expand Down
9 changes: 9 additions & 0 deletions modules/apps/transfer/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ func (suite *KeeperTestSuite) TestSendTransfer() {
memo = "memo"
}, true,
},
{
"successful transfer of entire balance",
func() {
coin.Amount = types.UnboundedSpendLimit()
var ok bool
expEscrowAmount, ok = sdkmath.NewIntFromString(ibctesting.DefaultGenesisAccBalance)
suite.Require().True(ok)
}, true,
},
{
"source channel not found",
func() {
Expand Down
13 changes: 13 additions & 0 deletions modules/apps/transfer/types/coin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package types

import (
"fmt"
"math/big"
"strings"

sdkmath "cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
)

// maxUint256 is the maximum value for a 256 bit unsigned integer.
var maxUint256 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1))

// SenderChainIsSource returns false if the denomination originally came
// from the receiving chain and true otherwise.
func SenderChainIsSource(sourcePort, sourceChannel, denom string) bool {
Expand Down Expand Up @@ -47,3 +51,12 @@ func GetTransferCoin(portID, channelID, baseDenom string, amount sdkmath.Int) sd
denomTrace := ParseDenomTrace(GetPrefixedDenom(portID, channelID, baseDenom))
return sdk.NewCoin(denomTrace.IBCDenom(), amount)
}

// UnboundedSpendLimit returns the sentinel value that can be used
// as the amount for a denomination's spend limit for which spend limit updating
// should be disabled. Please note that using this sentinel value means that a grantee
// will be granted the privilege to do ICS20 token transfers for the total amount
// of the denomination available at the granter's account.
func UnboundedSpendLimit() sdkmath.Int {
return sdkmath.NewIntFromBigInt(maxUint256)
}
14 changes: 0 additions & 14 deletions modules/apps/transfer/types/transfer_authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ package types

import (
"context"
"math/big"
"slices"
"strings"

"github.com/cosmos/gogoproto/proto"

errorsmod "cosmossdk.io/errors"
sdkmath "cosmossdk.io/math"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/authz"
Expand All @@ -21,9 +19,6 @@ import (

var _ authz.Authorization = (*TransferAuthorization)(nil)

// maxUint256 is the maximum value for a 256 bit unsigned integer.
var maxUint256 = new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1))

// NewTransferAuthorization creates a new TransferAuthorization object.
func NewTransferAuthorization(allocations ...Allocation) *TransferAuthorization {
return &TransferAuthorization{
Expand Down Expand Up @@ -183,12 +178,3 @@ func validateMemo(ctx sdk.Context, memo string, allowedMemos []string) error {

return nil
}

// UnboundedSpendLimit returns the sentinel value that can be used
// as the amount for a denomination's spend limit for which spend limit updating
// should be disabled. Please note that using this sentinel value means that a grantee
// will be granted the privilege to do ICS20 token transfers for the total amount
// of the denomination available at the granter's account.
func UnboundedSpendLimit() sdkmath.Int {
return sdkmath.NewIntFromBigInt(maxUint256)
}
6 changes: 5 additions & 1 deletion testing/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ type SenderAccount struct {
SenderAccount sdk.AccountI
}

const (
DefaultGenesisAccBalance = "10000000000000000000"
)

// TestChain is a testing struct that wraps a simapp with the last TM Header, the current ABCI
// header and the validators of the TestChain. It also contains a field called ChainID. This
// is the clientID that *other* chains use to refer to this TestChain. The SenderAccount
Expand Down Expand Up @@ -108,7 +112,7 @@ func NewTestChainWithValSet(tb testing.TB, coord *Coordinator, chainID string, v
for i := 0; i < MaxAccounts; i++ {
senderPrivKey := secp256k1.GenPrivKey()
acc := authtypes.NewBaseAccount(senderPrivKey.PubKey().Address().Bytes(), senderPrivKey.PubKey(), uint64(i), 0)
amount, ok := sdkmath.NewIntFromString("10000000000000000000")
amount, ok := sdkmath.NewIntFromString(DefaultGenesisAccBalance)
require.True(tb, ok)

// add sender account
Expand Down

0 comments on commit babe971

Please sign in to comment.