2022-09-07 13:25:57 -07:00
|
|
|
package keeper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-09-07 16:06:55 -07:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2022-09-07 13:25:57 -07:00
|
|
|
|
2022-09-07 16:06:55 -07:00
|
|
|
"colinear/x/colinearcore/auctionconfig"
|
2022-09-07 13:25:57 -07:00
|
|
|
"colinear/x/colinearcore/types"
|
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (k msgServer) LockFunds(goCtx context.Context, msg *types.MsgLockFunds) (*types.MsgLockFundsResponse, error) {
|
|
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
|
|
|
2022-09-07 16:06:55 -07:00
|
|
|
senderAddr, err := sdk.AccAddressFromBech32(msg.Creator)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("must be a bech32-formatted address: %s", msg.Creator)
|
|
|
|
}
|
|
|
|
|
|
|
|
lockedUsers, found := k.Keeper.GetLockedUsers(ctx)
|
|
|
|
if !found {
|
|
|
|
// initialize if not yet initialized
|
|
|
|
lockedUsers = types.LockedUsers{
|
|
|
|
Users: map[string]string{},
|
|
|
|
}
|
|
|
|
// return nil, errors.New("unable to get locked users")
|
|
|
|
}
|
|
|
|
|
|
|
|
required := big.NewInt(auctionconfig.ProviderMinLockedUClr)
|
|
|
|
|
|
|
|
paying := new(big.Int)
|
|
|
|
paying.SetString(msg.Amount, 10)
|
|
|
|
|
|
|
|
prevAmountS, ok := lockedUsers.Users[msg.Creator]
|
|
|
|
if !ok { // not found
|
|
|
|
prevAmountS = "0"
|
|
|
|
}
|
|
|
|
prevAmount := new(big.Int)
|
|
|
|
prevAmount.SetString(prevAmountS, 10)
|
|
|
|
|
|
|
|
// make sure that total amount after lock meets minimum required locked amount
|
|
|
|
totalAfterPay := new(big.Int)
|
|
|
|
totalAfterPay.Add(prevAmount, paying)
|
|
|
|
if totalAfterPay.Cmp(required) == -1 {
|
2022-09-08 14:43:42 -07:00
|
|
|
return nil, fmt.Errorf("final locked amount must be at least minimum required (actual: %s)", totalAfterPay.String())
|
2022-09-07 16:06:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
spendable := k.bank.SpendableCoins(ctx, senderAddr)
|
|
|
|
if spendable.AmountOf("uclr").BigInt().Cmp(paying) != -1 {
|
|
|
|
sendCoins := sdk.NewCoins(sdk.NewCoin("uclr", sdk.NewIntFromBigInt(paying)))
|
|
|
|
if err := k.bank.SendCoinsFromAccountToModule(ctx, senderAddr, types.ModuleName, sendCoins); err != nil {
|
2022-09-08 14:43:42 -07:00
|
|
|
return nil, fmt.Errorf("failed to lock %s uCLR in bank module: %s", paying.String(), err)
|
2022-09-07 16:06:55 -07:00
|
|
|
}
|
|
|
|
} else {
|
2022-09-08 14:43:42 -07:00
|
|
|
return nil, fmt.Errorf("not enough balance to lock %s uCLR (your balance: %s)", paying.String(), spendable.AmountOf("uclr").String())
|
2022-09-07 16:06:55 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
lockedUsers.Users[msg.Creator] = totalAfterPay.String()
|
2022-09-07 13:25:57 -07:00
|
|
|
|
2022-09-07 16:49:51 -07:00
|
|
|
k.Keeper.SetLockedUsers(ctx, lockedUsers)
|
|
|
|
|
2022-09-07 13:25:57 -07:00
|
|
|
return &types.MsgLockFundsResponse{}, nil
|
|
|
|
}
|