func (k Keeper) EnableServiceBinding(
	ctx sdk.Context,
	serviceName string,
	provider sdk.AccAddress,
	deposit sdk.Coins,
	owner sdk.AccAddress,
) error {
	binding, found := k.GetServiceBinding(ctx, serviceName, provider)
	if !found {
		return sdkerrors.Wrap(types.ErrUnknownServiceBinding, "")
	}
	bindingOwner, err := sdk.AccAddressFromBech32(binding.Owner)
	if err != nil {
		return err
	}
	if !owner.Equals(bindingOwner) {
		return sdkerrors.Wrap(types.ErrNotAuthorized, "owner not matching")
	}
	if binding.Available {
		return sdkerrors.Wrap(types.ErrServiceBindingAvailable, "")
	}
	if !deposit.Empty() {
		if err := k.validateDeposit(ctx, deposit); err != nil {
			return err
		}
		binding.Deposit = binding.Deposit.Add(deposit...)
	}
	minDeposit, err := k.GetMinDeposit(ctx, k.GetPricing(ctx, serviceName, provider))
	if err != nil {
		return sdkerrors.Wrapf(types.ErrInvalidMinDeposit, "%s", err)
	}
	if !binding.Deposit.IsAllGTE(minDeposit) {
		return sdkerrors.Wrapf(
			types.ErrInvalidDeposit,
			"insufficient deposit: minimum deposit %s, %s got",
			minDeposit, binding.Deposit,
		)
	}
	if !deposit.Empty() {
		if err := k.bankKeeper.SendCoinsFromAccountToModule(
			ctx, owner, types.DepositAccName, deposit,
		); err != nil {
			return err
		}
	}
	binding.Available = true
	binding.DisabledTime = time.Time{}
	k.SetServiceBinding(ctx, binding)
	return nil
}
EnableServiceBinding enables the specified service binding
package keeper
import (
	"bytes"
	"time"
	gogotypes "github.com/gogo/protobuf/types"
	sdk "github.com/cosmos/cosmos-sdk/types"
	sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
	v040 "github.com/cosmos/cosmos-sdk/x/auth/legacy/v040"
	"github.com/irisnet/irismod/modules/service/types"
)
func (k Keeper) AddServiceBinding(
	ctx sdk.Context,
	serviceName string,
	provider sdk.AccAddress,
	deposit sdk.Coins,
	pricing string,
	qos uint64,
	options string,
	owner sdk.AccAddress,
) error {
	if _, found := k.GetServiceDefinition(ctx, serviceName); !found {
		return sdkerrors.Wrap(types.ErrUnknownServiceDefinition, serviceName)
	}
	if _, found := k.GetServiceBinding(ctx, serviceName, provider); found {
		return sdkerrors.Wrap(types.ErrServiceBindingExists, "")
	}
	currentOwner, found := k.GetOwner(ctx, provider)
	if found && !owner.Equals(currentOwner) {
		return sdkerrors.Wrap(types.ErrNotAuthorized, "owner not matching")
	}
	if err := k.validateDeposit(ctx, deposit); err != nil {
		return err
	}
	maxReqTimeout := k.MaxRequestTimeout(ctx)
	if qos > uint64(maxReqTimeout) {
		return sdkerrors.Wrapf(
			types.ErrInvalidQoS,
			"QoS [%d] must not be greater than maximum request timeout [%d]",
			qos, maxReqTimeout,
		)
	}
	if err := types.ValidateOptions(options); err != nil {
		return err
	}
	parsedPricing, err := k.ParsePricing(ctx, pricing)
	if err != nil {
		return err
	}
	minDeposit, err := k.GetMinDeposit(ctx, parsedPricing)
	if err != nil {
		return sdkerrors.Wrapf(types.ErrInvalidMinDeposit, "%s", err)
	}
	if !deposit.IsAllGTE(minDeposit) {
		return sdkerrors.Wrapf(
			types.ErrInvalidDeposit,
			"insufficient deposit: minimum deposit %s, %s got",
			minDeposit, deposit,
		)
	}
	if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, owner, types.DepositAccName, deposit); err != nil {
		return err
	}
	available := true
	disabledTime := time.Time{}
	svcBinding := types.NewServiceBinding(serviceName, provider, deposit, pricing, qos, options, available, disabledTime, owner)
	k.SetServiceBinding(ctx, svcBinding)
	k.SetOwnerServiceBinding(ctx, svcBinding)
	k.SetPricing(ctx, serviceName, provider, parsedPricing)
	if currentOwner.Empty() {
		k.SetOwner(ctx, provider, owner)
		k.SetOwnerProvider(ctx, owner, provider)
	}
	return nil
}
func (k Keeper) SetServiceBindingForGenesis(
	ctx sdk.Context,
	svcBinding types.ServiceBinding,
) error {
	provider, err := sdk.AccAddressFromBech32(svcBinding.Provider)
	if err != nil {
		return err
	}
	owner, err := sdk.AccAddressFromBech32(svcBinding.Owner)
	if err != nil {
		return err
	}
	pricing, err := types.ParsePricing(svcBinding.Pricing)
	if err != nil {
		return err
	}
	k.SetServiceBinding(ctx, svcBinding)
	k.SetOwnerServiceBinding(ctx, svcBinding)
	k.SetOwner(ctx, provider, owner)
	k.SetOwnerProvider(ctx, owner, provider)
	k.SetPricing(ctx, svcBinding.ServiceName, provider, pricing)
	return nil
}
func (k Keeper) UpdateServiceBinding(
	ctx sdk.Context,
	serviceName string,
	provider sdk.AccAddress,
	deposit sdk.Coins,
	pricing string,
	qos uint64,
	options string,
	owner sdk.AccAddress,
) error {
	binding, found := k.GetServiceBinding(ctx, serviceName, provider)
	if !found {
		return sdkerrors.Wrap(types.ErrUnknownServiceBinding, "")
	}
	bindingOwner, err := sdk.AccAddressFromBech32(binding.Owner)
	if err != nil {
		return err
	}
	if !owner.Equals(bindingOwner) {
		return sdkerrors.Wrap(types.ErrNotAuthorized, "owner not matching")
	}
	updated := false
	if qos != 0 {
		maxReqTimeout := k.MaxRequestTimeout(ctx)
		if qos > uint64(maxReqTimeout) {
			return sdkerrors.Wrapf(
				types.ErrInvalidQoS,
				"QoS [%d] must not be greater than maximum request timeout [%d]",
				qos, maxReqTimeout,
			)
		}
		binding.QoS = qos
		updated = true
	}
	if !deposit.Empty() {
		if err := k.validateDeposit(ctx, deposit); err != nil {
			return err
		}
		binding.Deposit = binding.Deposit.Add(deposit...)
		updated = true
	}
	parsedPricing := k.GetPricing(ctx, serviceName, provider)
	if len(pricing) != 0 {
		parsedPricing, err = k.ParsePricing(ctx, pricing)
		if err != nil {
			return err
		}
		binding.Pricing = pricing
		k.SetPricing(ctx, serviceName, provider, parsedPricing)
		updated = true
	}
	if len(options) != 0 {
		if err := types.ValidateOptions(options); err != nil {
			return err
		}
		binding.Options = options
		updated = true
	}
	if binding.Available && updated {
		minDeposit, err := k.GetMinDeposit(ctx, parsedPricing)
		if err != nil {
			return sdkerrors.Wrapf(types.ErrInvalidMinDeposit, "%s", err)
		}
		if !binding.Deposit.IsAllGTE(minDeposit) {
			return sdkerrors.Wrapf(
				types.ErrInvalidDeposit,
				"insufficient deposit: minimum deposit %s, %s got",
				minDeposit, binding.Deposit,
			)
		}
	}
	if !deposit.Empty() {
		if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, owner, types.DepositAccName, deposit); err != nil {
			return err
		}
	}
	if updated {
		k.SetServiceBinding(ctx, binding)
	}
	return nil
}
func (k Keeper) DisableServiceBinding(
	ctx sdk.Context,
	serviceName string,
	provider,
	owner sdk.AccAddress,
) error {
	binding, found := k.GetServiceBinding(ctx, serviceName, provider)
	if !found {
		return sdkerrors.Wrap(types.ErrUnknownServiceBinding, "")
	}
	bindingOwner, err := sdk.AccAddressFromBech32(binding.Owner)
	if err != nil {
		return err
	}
	if !owner.Equals(bindingOwner) {
		return sdkerrors.Wrap(types.ErrNotAuthorized, "owner not matching")
	}
	if !binding.Available {
		return sdkerrors.Wrap(types.ErrServiceBindingUnavailable, "")
	}
	binding.Available = false
	binding.DisabledTime = ctx.BlockHeader().Time
	k.SetServiceBinding(ctx, binding)
	return nil
}