diff --git a/go.sum b/go.sum index 9348f2a..555d72d 100644 --- a/go.sum +++ b/go.sum @@ -573,8 +573,6 @@ github.com/dymensionxyz/cosmos-sdk v0.46.17-0.20241128210616-e9dfe47b8c73 h1:A0O github.com/dymensionxyz/cosmos-sdk v0.46.17-0.20241128210616-e9dfe47b8c73/go.mod h1:VPUuzF+l+ekSGPV7VVB8m0OMQfwp3QdKWNZjvkU3A1U= github.com/dymensionxyz/cosmosclient v0.4.2-beta.0.20241121093220-e0d7ad456fbd h1:V89QyOFM84o9w0iFdctMU6So8SS/Xt32JWAXGqJduT0= github.com/dymensionxyz/cosmosclient v0.4.2-beta.0.20241121093220-e0d7ad456fbd/go.mod h1:3weqpVj/TqTFpC0LjEB3H+HZSpm7BrQ1QkEg1Ahy6KY= -github.com/dymensionxyz/dymension-rdk v1.6.1-0.20241204193918-4fb6a8d95889 h1:AZ1cskz6smqxkUkCbbbOWfuhMqE0Y1AwbgKSs3zIj+A= -github.com/dymensionxyz/dymension-rdk v1.6.1-0.20241204193918-4fb6a8d95889/go.mod h1:AA0rq+4H+yn/Agnx5L8ermhhny3FYgCv6UW9DzK5vss= github.com/dymensionxyz/dymension-rdk v1.6.1-0.20241206101450-08627417ea7d h1:WtRE6IcI+N8QlseRt8OfUWJLL22l5CEDVfJWoytHK9A= github.com/dymensionxyz/dymension-rdk v1.6.1-0.20241206101450-08627417ea7d/go.mod h1:AA0rq+4H+yn/Agnx5L8ermhhny3FYgCv6UW9DzK5vss= github.com/dymensionxyz/dymint v1.2.0-rc01.0.20241203150638-c0e39f93d729 h1:2HtdhYN0DSqBQTcSeK9+QXBJz8CF7u1YrLu8odkCFSY= diff --git a/proto/rollapp/wasm/authz.proto b/proto/rollapp/wasm/authz.proto new file mode 100644 index 0000000..315e901 --- /dev/null +++ b/proto/rollapp/wasm/authz.proto @@ -0,0 +1,22 @@ +syntax = "proto3"; +package rollapp.wasm; + +import "gogoproto/gogo.proto"; +import "cosmos_proto/cosmos.proto"; +import "cosmos/base/v1beta1/coin.proto"; + +option go_package = "github.com/dymensionxyz/rollapp-wasm/x/wasm"; + +// ContractExecutionAuthorization defines authorization for wasm execute. +message ContractExecutionAuthorization { + option (cosmos_proto.implements_interface) = "cosmos.authz.v1beta1.Authorization"; + + // Contracts is a list of allowed contracts. Optional. + repeated string contracts = 1; + + // SpendLimits defines spending limits for contracts interactions. + repeated cosmos.base.v1beta1.Coin spend_limit = 2 [ + (gogoproto.nullable) = false, + (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins" + ]; +} \ No newline at end of file diff --git a/scripts/settlement/register_sequencer_to_hub.sh b/scripts/settlement/register_sequencer_to_hub.sh index 24046c5..4c23c8e 100644 --- a/scripts/settlement/register_sequencer_to_hub.sh +++ b/scripts/settlement/register_sequencer_to_hub.sh @@ -38,7 +38,7 @@ fi #Register Sequencer # DESCRIPTION="{\"Moniker\":\"${ROLLAPP_CHAIN_ID}-sequencer\",\"Identity\":\"\",\"Website\":\"\",\"SecurityContact\":\"\",\"Details\":\"\"}" SEQ_PUB_KEY="$("$ROLLAPP_EXECUTABLE" dymint show-sequencer)" -BOND_AMOUNT="$("$SETTLEMENT_EXECUTABLE" q sequencer params -o json --node "$HUB_RPC_URL" | jq -r '.params.min_bond.amount')$("$SETTLEMENT_EXECUTABLE" q sequencer params -o json --node "$HUB_RPC_URL" | jq -r '.params.min_bond.denom')" +BOND_AMOUNT="$("$SETTLEMENT_EXECUTABLE" q sequencer params -o json --node "$HUB_RPC_URL" | jq -r '.params.kick_threshold.amount')$("$SETTLEMENT_EXECUTABLE" q sequencer params -o json --node "$HUB_RPC_URL" | jq -r '.params.kick_threshold.denom')" echo "$BOND_AMOUNT" diff --git a/scripts/wasm/deploy_contract.sh b/scripts/wasm/deploy_contract.sh index 6e0987e..a714b3b 100755 --- a/scripts/wasm/deploy_contract.sh +++ b/scripts/wasm/deploy_contract.sh @@ -4,9 +4,8 @@ ROLLAPP_KEY_NAME_GENESIS="rol-user" ROLLAPP_GENESIS_ADDR="$($EXECUTABLE keys show $ROLLAPP_KEY_NAME_GENESIS | grep "address:" | cut -d' ' -f3)" # Store code for cw20_base contract -$EXECUTABLE tx wasm store ./scripts/bytecode/cw20_base.wasm --from rol-user --gas 5000000 --yes - -sleep 5 +$EXECUTABLE tx wasm store ./scripts/bytecode/cw20_base.wasm --from rol-user --gas auto --gas-adjustment=1.2 --gas-prices "1000000000$BASE_DENOM" --yes +sleep 7 CW20_CODE_ID="$($EXECUTABLE q wasm list-code | grep "code_id:" | tail -n 1 | cut -d' ' -f3 | tr -d '"')" @@ -19,13 +18,19 @@ INIT_CW20=$(cat < 0 && !slices.Contains(a.Contracts, m.Contract) { + return authztypes.AcceptResponse{}, errors.New("contract not authorized") + } + + // Check spend limits if specified + if !a.SpendLimit.Empty() { + if m.Funds.IsAnyGT(a.SpendLimit) { + return authztypes.AcceptResponse{}, errors.New("exceeds spend limit") + } + + // Update spend limits + a.SpendLimit = a.SpendLimit.Sub(m.Funds...) + if a.SpendLimit.Empty() { + return authztypes.AcceptResponse{Accept: true, Delete: true}, nil + } + } + + return authztypes.AcceptResponse{Accept: true, Updated: a}, nil +} + +func (a *ContractExecutionAuthorization) ValidateBasic() error { + // Check for duplicate contracts + contractSet := make(map[string]struct{}) + for _, contract := range a.Contracts { + if _, err := sdk.AccAddressFromBech32(contract); err != nil { + return fmt.Errorf("invalid contract address: %s: %w", contract, err) + } + if _, exists := contractSet[contract]; exists { + return fmt.Errorf("duplicate contract address: %s", contract) + } + contractSet[contract] = struct{}{} + } + + if !a.SpendLimit.IsZero() { + err := a.SpendLimit.Validate() + if err != nil { + return fmt.Errorf("validate spend limit: %s", err) + } + } + + return nil +} diff --git a/x/wasm/authz.pb.go b/x/wasm/authz.pb.go new file mode 100644 index 0000000..c0e95e3 --- /dev/null +++ b/x/wasm/authz.pb.go @@ -0,0 +1,397 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: rollapp/wasm/authz.proto + +package wasm + +import ( + fmt "fmt" + _ "github.com/cosmos/cosmos-proto" + github_com_cosmos_cosmos_sdk_types "github.com/cosmos/cosmos-sdk/types" + types "github.com/cosmos/cosmos-sdk/types" + _ "github.com/cosmos/gogoproto/gogoproto" + proto "github.com/gogo/protobuf/proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +// ContractExecutionAuthorization defines authorization for wasm execute. +type ContractExecutionAuthorization struct { + // Contracts is a list of allowed contracts. Optional. + Contracts []string `protobuf:"bytes,1,rep,name=contracts,proto3" json:"contracts,omitempty"` + // SpendLimits defines spending limits for contracts interactions. + SpendLimit github_com_cosmos_cosmos_sdk_types.Coins `protobuf:"bytes,2,rep,name=spend_limit,json=spendLimit,proto3,castrepeated=github.com/cosmos/cosmos-sdk/types.Coins" json:"spend_limit"` +} + +func (m *ContractExecutionAuthorization) Reset() { *m = ContractExecutionAuthorization{} } +func (m *ContractExecutionAuthorization) String() string { return proto.CompactTextString(m) } +func (*ContractExecutionAuthorization) ProtoMessage() {} +func (*ContractExecutionAuthorization) Descriptor() ([]byte, []int) { + return fileDescriptor_543b6f32e6968967, []int{0} +} +func (m *ContractExecutionAuthorization) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *ContractExecutionAuthorization) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_ContractExecutionAuthorization.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *ContractExecutionAuthorization) XXX_Merge(src proto.Message) { + xxx_messageInfo_ContractExecutionAuthorization.Merge(m, src) +} +func (m *ContractExecutionAuthorization) XXX_Size() int { + return m.Size() +} +func (m *ContractExecutionAuthorization) XXX_DiscardUnknown() { + xxx_messageInfo_ContractExecutionAuthorization.DiscardUnknown(m) +} + +var xxx_messageInfo_ContractExecutionAuthorization proto.InternalMessageInfo + +func (m *ContractExecutionAuthorization) GetContracts() []string { + if m != nil { + return m.Contracts + } + return nil +} + +func (m *ContractExecutionAuthorization) GetSpendLimit() github_com_cosmos_cosmos_sdk_types.Coins { + if m != nil { + return m.SpendLimit + } + return nil +} + +func init() { + proto.RegisterType((*ContractExecutionAuthorization)(nil), "rollapp.wasm.ContractExecutionAuthorization") +} + +func init() { proto.RegisterFile("rollapp/wasm/authz.proto", fileDescriptor_543b6f32e6968967) } + +var fileDescriptor_543b6f32e6968967 = []byte{ + // 311 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x54, 0x91, 0xb1, 0x4e, 0xf3, 0x30, + 0x14, 0x85, 0x93, 0xbf, 0xd2, 0x2f, 0x35, 0x65, 0xaa, 0x18, 0xda, 0x0a, 0xb9, 0x55, 0x07, 0x54, + 0x09, 0xd5, 0xa6, 0xb0, 0xb1, 0xd1, 0xaa, 0x1b, 0x53, 0x47, 0x96, 0xca, 0x71, 0xad, 0xd6, 0x22, + 0xf1, 0x8d, 0x62, 0x07, 0x92, 0x3c, 0x05, 0xcf, 0xc1, 0xcc, 0x43, 0x54, 0x4c, 0x1d, 0x18, 0x98, + 0x00, 0x25, 0x2f, 0x82, 0x62, 0x1b, 0x01, 0x93, 0x7d, 0xfd, 0x5d, 0x9f, 0x7b, 0x74, 0x6e, 0xd0, + 0x4b, 0x21, 0x8a, 0x68, 0x92, 0x90, 0x07, 0xaa, 0x62, 0x42, 0x33, 0xbd, 0x2b, 0x71, 0x92, 0x82, + 0x86, 0xee, 0x91, 0x23, 0xb8, 0x21, 0x83, 0xe3, 0x2d, 0x6c, 0xc1, 0x00, 0xd2, 0xdc, 0x6c, 0xcf, + 0xa0, 0xcf, 0x40, 0xc5, 0xa0, 0xd6, 0x16, 0xd8, 0xc2, 0x21, 0x64, 0x2b, 0x12, 0x52, 0xc5, 0xc9, + 0xfd, 0x2c, 0xe4, 0x9a, 0xce, 0x08, 0x03, 0x21, 0x2d, 0x1f, 0xbf, 0xfa, 0x01, 0x5a, 0x80, 0xd4, + 0x29, 0x65, 0x7a, 0x99, 0x73, 0x96, 0x69, 0x01, 0xf2, 0x3a, 0xd3, 0x3b, 0x48, 0x45, 0x49, 0x9b, + 0xa2, 0x7b, 0x12, 0xb4, 0x99, 0xeb, 0x50, 0x3d, 0x7f, 0xd4, 0x9a, 0xb4, 0x57, 0x3f, 0x0f, 0xdd, + 0x28, 0xe8, 0xa8, 0x84, 0xcb, 0xcd, 0x3a, 0x12, 0xb1, 0xd0, 0xbd, 0x7f, 0xa3, 0xd6, 0xa4, 0x73, + 0xd1, 0xc7, 0xce, 0x44, 0x33, 0x16, 0xbb, 0xb1, 0x78, 0x01, 0x42, 0xce, 0xcf, 0xf7, 0xef, 0x43, + 0xef, 0xe9, 0x63, 0x38, 0xd9, 0x0a, 0xbd, 0xcb, 0x42, 0xcc, 0x20, 0x76, 0x8e, 0xdd, 0x31, 0x55, + 0x9b, 0x3b, 0xa2, 0x8b, 0x84, 0x2b, 0xf3, 0x41, 0xad, 0x02, 0xa3, 0x7f, 0xd3, 0xc8, 0x5f, 0x9d, + 0xbe, 0x3c, 0x4f, 0xc7, 0x4e, 0xdb, 0xa6, 0xf4, 0x2d, 0xfe, 0xc7, 0xf3, 0x7c, 0xb9, 0xaf, 0x90, + 0x7f, 0xa8, 0x90, 0xff, 0x59, 0x21, 0xff, 0xb1, 0x46, 0xde, 0xa1, 0x46, 0xde, 0x5b, 0x8d, 0xbc, + 0xdb, 0xb3, 0x5f, 0x73, 0x37, 0x45, 0xcc, 0xa5, 0x12, 0x20, 0xf3, 0xa2, 0x24, 0x2e, 0xe7, 0xa9, + 0xd9, 0x40, 0x6e, 0x16, 0x11, 0xfe, 0x37, 0x21, 0x5d, 0x7e, 0x05, 0x00, 0x00, 0xff, 0xff, 0xc0, + 0x30, 0x3f, 0xec, 0x9f, 0x01, 0x00, 0x00, +} + +func (m *ContractExecutionAuthorization) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *ContractExecutionAuthorization) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *ContractExecutionAuthorization) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.SpendLimit) > 0 { + for iNdEx := len(m.SpendLimit) - 1; iNdEx >= 0; iNdEx-- { + { + size, err := m.SpendLimit[iNdEx].MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err + } + i -= size + i = encodeVarintAuthz(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + } + } + if len(m.Contracts) > 0 { + for iNdEx := len(m.Contracts) - 1; iNdEx >= 0; iNdEx-- { + i -= len(m.Contracts[iNdEx]) + copy(dAtA[i:], m.Contracts[iNdEx]) + i = encodeVarintAuthz(dAtA, i, uint64(len(m.Contracts[iNdEx]))) + i-- + dAtA[i] = 0xa + } + } + return len(dAtA) - i, nil +} + +func encodeVarintAuthz(dAtA []byte, offset int, v uint64) int { + offset -= sovAuthz(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *ContractExecutionAuthorization) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + if len(m.Contracts) > 0 { + for _, s := range m.Contracts { + l = len(s) + n += 1 + l + sovAuthz(uint64(l)) + } + } + if len(m.SpendLimit) > 0 { + for _, e := range m.SpendLimit { + l = e.Size() + n += 1 + l + sovAuthz(uint64(l)) + } + } + return n +} + +func sovAuthz(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozAuthz(x uint64) (n int) { + return sovAuthz(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *ContractExecutionAuthorization) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuthz + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: ContractExecutionAuthorization: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: ContractExecutionAuthorization: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Contracts", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuthz + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthAuthz + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthAuthz + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Contracts = append(m.Contracts, string(dAtA[iNdEx:postIndex])) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field SpendLimit", wireType) + } + var msglen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowAuthz + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + msglen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if msglen < 0 { + return ErrInvalidLengthAuthz + } + postIndex := iNdEx + msglen + if postIndex < 0 { + return ErrInvalidLengthAuthz + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.SpendLimit = append(m.SpendLimit, types.Coin{}) + if err := m.SpendLimit[len(m.SpendLimit)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipAuthz(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthAuthz + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipAuthz(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowAuthz + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowAuthz + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowAuthz + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthAuthz + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupAuthz + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthAuthz + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthAuthz = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowAuthz = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupAuthz = fmt.Errorf("proto: unexpected end of group") +) diff --git a/x/wasm/authz_test.go b/x/wasm/authz_test.go new file mode 100644 index 0000000..08806b9 --- /dev/null +++ b/x/wasm/authz_test.go @@ -0,0 +1,84 @@ +package wasm_test + +import ( + "testing" + + "github.com/CosmWasm/wasmd/x/wasm/types" + sdk "github.com/cosmos/cosmos-sdk/types" + authztypes "github.com/cosmos/cosmos-sdk/x/authz" + "github.com/stretchr/testify/require" + + "github.com/dymensionxyz/rollapp-wasm/x/wasm" +) + +func TestContractExecutionAuthorization_Accept(t *testing.T) { + tests := []struct { + name string + auth *wasm.ContractExecutionAuthorization + msg sdk.Msg + expectedResp authztypes.AcceptResponse + expectedError string + }{ + { + name: "invalid message type", + auth: wasm.NewContractExecutionAuthorization(nil, sdk.Coins{}), + msg: &types.MsgStoreCode{}, + expectedResp: authztypes.AcceptResponse{}, + expectedError: "invalid message type", + }, + { + name: "contract not authorized", + auth: wasm.NewContractExecutionAuthorization([]string{"contract1"}, sdk.Coins{}), + msg: &types.MsgExecuteContract{Contract: "contract2"}, + expectedResp: authztypes.AcceptResponse{}, + expectedError: "contract not authorized", + }, + { + name: "exceeds spend limit", + auth: wasm.NewContractExecutionAuthorization(nil, sdk.NewCoins(sdk.NewInt64Coin("token", 100))), + msg: &types.MsgExecuteContract{Funds: sdk.NewCoins(sdk.NewInt64Coin("token", 200))}, + expectedResp: authztypes.AcceptResponse{}, + expectedError: "exceeds spend limit", + }, + { + name: "spend limit updated", + auth: wasm.NewContractExecutionAuthorization(nil, sdk.NewCoins(sdk.NewInt64Coin("token", 100))), + msg: &types.MsgExecuteContract{Funds: sdk.NewCoins(sdk.NewInt64Coin("token", 50))}, + expectedResp: authztypes.AcceptResponse{Accept: true, Updated: wasm.NewContractExecutionAuthorization(nil, sdk.NewCoins(sdk.NewInt64Coin("token", 50)))}, + expectedError: "", + }, + { + name: "spend limit exhausted", + auth: wasm.NewContractExecutionAuthorization(nil, sdk.NewCoins(sdk.NewInt64Coin("token", 100))), + msg: &types.MsgExecuteContract{Funds: sdk.NewCoins(sdk.NewInt64Coin("token", 100))}, + expectedResp: authztypes.AcceptResponse{Accept: true, Delete: true}, + expectedError: "", + }, + { + name: "contracts field is empty", + auth: wasm.NewContractExecutionAuthorization(nil, sdk.Coins{}), + msg: &types.MsgExecuteContract{Contract: "contract1"}, + expectedResp: authztypes.AcceptResponse{Accept: true, Updated: wasm.NewContractExecutionAuthorization(nil, sdk.Coins{})}, + expectedError: "", + }, + { + name: "spend limit field is empty", + auth: wasm.NewContractExecutionAuthorization([]string{"contract1"}, sdk.Coins{}), + msg: &types.MsgExecuteContract{Contract: "contract1", Funds: sdk.NewCoins(sdk.NewInt64Coin("token", 100))}, + expectedResp: authztypes.AcceptResponse{Accept: true, Updated: wasm.NewContractExecutionAuthorization([]string{"contract1"}, sdk.Coins{})}, + expectedError: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + resp, err := tt.auth.Accept(sdk.Context{}, tt.msg) + if tt.expectedError != "" { + require.EqualError(t, err, tt.expectedError) + } else { + require.NoError(t, err) + } + require.Equal(t, tt.expectedResp, resp) + }) + } +}