diff --git a/modules/apps/transfer/types/transfer_authorization.go b/modules/apps/transfer/types/transfer_authorization.go index 743b86623a0..20eb1856cc7 100644 --- a/modules/apps/transfer/types/transfer_authorization.go +++ b/modules/apps/transfer/types/transfer_authorization.go @@ -15,16 +15,7 @@ const gasCostPerIteration = uint64(10) var _ authz.Authorization = &TransferAuthorization{} // NewTransferAuthorization creates a new TransferAuthorization object. -func NewTransferAuthorization(sourcePorts, sourceChannels []string, spendLimits []sdk.Coins, allowedAddrs [][]string) *TransferAuthorization { - allocations := []Allocation{} - for index := range sourcePorts { - allocations = append(allocations, Allocation{ - SourcePort: sourcePorts[index], - SourceChannel: sourceChannels[index], - SpendLimit: spendLimits[index], - AllowList: allowedAddrs[index], - }) - } +func NewTransferAuthorization(allocations ...Allocation) *TransferAuthorization { return &TransferAuthorization{ Allocations: allocations, } diff --git a/modules/apps/transfer/types/transfer_authorization_test.go b/modules/apps/transfer/types/transfer_authorization_test.go index 873303d7e7a..35989524722 100644 --- a/modules/apps/transfer/types/transfer_authorization_test.go +++ b/modules/apps/transfer/types/transfer_authorization_test.go @@ -26,7 +26,13 @@ var ( func TestTransferAuthorization(t *testing.T) { app := simapp.Setup(t, false) ctx := app.BaseApp.NewContext(false, tmproto.Header{}) - authorization := NewTransferAuthorization([]string{sourcePort}, []string{sourceChannel}, []sdk.Coins{coins1000}, [][]string{{toAddr.String()}}) + allocation := Allocation{ + SourcePort: sourcePort, + SourceChannel: sourceChannel, + SpendLimit: coins1000, + AllowList: []string{toAddr.String()}, + } + authorization := NewTransferAuthorization(allocation) t.Log("verify authorization returns valid method name") require.Equal(t, authorization.MsgTypeURL(), "/ibc.applications.transfer.v1.MsgTransfer") @@ -41,7 +47,7 @@ func TestTransferAuthorization(t *testing.T) { require.Nil(t, resp.Updated) t.Log("verify updated authorization returns remaining spent limit") - authorization = NewTransferAuthorization([]string{sourcePort}, []string{sourceChannel}, []sdk.Coins{coins1000}, [][]string{{toAddr.String()}}) + authorization = NewTransferAuthorization(allocation) require.Equal(t, authorization.MsgTypeURL(), "/ibc.applications.transfer.v1.MsgTransfer") require.NoError(t, authorization.ValidateBasic()) transfer = NewMsgTransfer(sourcePort, sourceChannel, coin500, fromAddr.String(), toAddr.String(), timeoutHeight, 0, "") @@ -50,7 +56,14 @@ func TestTransferAuthorization(t *testing.T) { require.NoError(t, err) require.False(t, resp.Delete) require.NotNil(t, resp.Updated) - sendAuth := NewTransferAuthorization([]string{sourcePort}, []string{sourceChannel}, []sdk.Coins{coins500}, [][]string{{toAddr.String()}}) + + allocation = Allocation{ + SourcePort: sourcePort, + SourceChannel: sourceChannel, + SpendLimit: coins500, + AllowList: []string{toAddr.String()}, + } + sendAuth := NewTransferAuthorization(allocation) require.Equal(t, sendAuth.String(), resp.Updated.String()) t.Log("expect updated authorization nil after spending remaining amount") @@ -60,26 +73,49 @@ func TestTransferAuthorization(t *testing.T) { require.Nil(t, resp.Updated) t.Log("expect error when spend limit for specific port and channel is not set") - authorization = NewTransferAuthorization([]string{sourcePort}, []string{sourceChannel}, []sdk.Coins{coins1000}, [][]string{{toAddr.String()}}) + allocation = Allocation{ + SourcePort: sourcePort, + SourceChannel: sourceChannel, + SpendLimit: coins1000, + AllowList: []string{toAddr.String()}, + } + authorization = NewTransferAuthorization(allocation) transfer = NewMsgTransfer(sourcePort2, sourceChannel2, coin500, fromAddr.String(), toAddr.String(), timeoutHeight, 0, "") _, err = authorization.Accept(ctx, transfer) require.Error(t, err) t.Log("expect removing only 1 allocation if spend limit is finalized for the port") - authorization = NewTransferAuthorization( - []string{sourcePort, sourcePort2}, - []string{sourceChannel, sourceChannel2}, - []sdk.Coins{coins1000, coins1000}, - [][]string{{toAddr.String()}, {toAddr.String()}}) + + allocations := []Allocation{ + { + SourcePort: sourcePort, + SourceChannel: sourceChannel, + SpendLimit: coins1000, + AllowList: []string{toAddr.String()}, + }, + { + SourcePort: sourcePort2, + SourceChannel: sourceChannel2, + SpendLimit: coins1000, + AllowList: []string{toAddr.String()}, + }, + } + authorization = NewTransferAuthorization(allocations...) transfer = NewMsgTransfer(sourcePort, sourceChannel, coin1000, fromAddr.String(), toAddr.String(), timeoutHeight, 0, "") resp, err = authorization.Accept(ctx, transfer) require.NoError(t, err) require.NotNil(t, resp.Updated) - require.Equal(t, resp.Updated, NewTransferAuthorization([]string{sourcePort2}, []string{sourceChannel2}, []sdk.Coins{coins1000}, [][]string{{toAddr.String()}})) + require.Equal(t, resp.Updated, NewTransferAuthorization(allocations[1])) require.False(t, resp.Delete) t.Log("expect error when transferring to not allowed address") - authorization = NewTransferAuthorization([]string{sourcePort}, []string{sourceChannel}, []sdk.Coins{coins1000}, [][]string{{fromAddr.String()}}) + allocation = Allocation{ + SourcePort: sourcePort, + SourceChannel: sourceChannel, + SpendLimit: coins1000, + AllowList: []string{fromAddr.String()}, + } + authorization = NewTransferAuthorization(allocation) transfer = NewMsgTransfer(sourcePort, sourceChannel, coin500, fromAddr.String(), toAddr.String(), timeoutHeight, 0, "") _, err = authorization.Accept(ctx, transfer) require.Error(t, err)