basic provider fund locking implementation

master
michael 2022-09-07 23:06:55 +00:00
parent c9f73980ae
commit c85bbaca1f
5 changed files with 132 additions and 30 deletions

View File

@ -37,6 +37,7 @@ message MsgNewBidResponse {
message MsgLockFunds { message MsgLockFunds {
string creator = 1; string creator = 1;
string amount = 2;
} }
message MsgLockFundsResponse { message MsgLockFundsResponse {

View File

@ -4,6 +4,7 @@ import (
"strconv" "strconv"
"colinear/x/colinearcore/types" "colinear/x/colinearcore/types"
"github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags" "github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx" "github.com/cosmos/cosmos-sdk/client/tx"
@ -14,9 +15,9 @@ var _ = strconv.Itoa(0)
func CmdLockFunds() *cobra.Command { func CmdLockFunds() *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "lock-funds", Use: "lock-funds [amount in uCLR]",
Short: "Broadcast message lockFunds", Short: "Broadcast message lockFunds",
Args: cobra.ExactArgs(0), Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) { RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientTxContext(cmd) clientCtx, err := client.GetClientTxContext(cmd)
@ -24,8 +25,11 @@ func CmdLockFunds() *cobra.Command {
return err return err
} }
argAmount := args[0]
msg := types.NewMsgLockFunds( msg := types.NewMsgLockFunds(
clientCtx.GetFromAddress().String(), clientCtx.GetFromAddress().String(),
argAmount,
) )
if err := msg.ValidateBasic(); err != nil { if err := msg.ValidateBasic(); err != nil {
return err return err

View File

@ -2,7 +2,10 @@ package keeper
import ( import (
"context" "context"
"fmt"
"math/big"
"colinear/x/colinearcore/auctionconfig"
"colinear/x/colinearcore/types" "colinear/x/colinearcore/types"
sdk "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types"
@ -11,8 +14,50 @@ import (
func (k msgServer) LockFunds(goCtx context.Context, msg *types.MsgLockFunds) (*types.MsgLockFundsResponse, error) { func (k msgServer) LockFunds(goCtx context.Context, msg *types.MsgLockFunds) (*types.MsgLockFundsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx) ctx := sdk.UnwrapSDKContext(goCtx)
// TODO senderAddr, err := sdk.AccAddressFromBech32(msg.Creator)
_ = ctx 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 {
return nil, fmt.Errorf("final locked amount must be above minimum required (actual: %s)", totalAfterPay.String())
}
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 {
return nil, fmt.Errorf("failed to lock %s CLR in bank module: %s", paying.String(), err)
}
} else {
return nil, fmt.Errorf("not enough balance to lock %s CLR (your balance: %s)", paying.String(), spendable.AmountOf("clr").String())
}
lockedUsers.Users[msg.Creator] = totalAfterPay.String()
return &types.MsgLockFundsResponse{}, nil return &types.MsgLockFundsResponse{}, nil
} }

View File

@ -9,9 +9,10 @@ const TypeMsgLockFunds = "lock_funds"
var _ sdk.Msg = &MsgLockFunds{} var _ sdk.Msg = &MsgLockFunds{}
func NewMsgLockFunds(creator string) *MsgLockFunds { func NewMsgLockFunds(creator string, amount string) *MsgLockFunds {
return &MsgLockFunds{ return &MsgLockFunds{
Creator: creator, Creator: creator,
Amount: amount,
} }
} }

View File

@ -253,6 +253,7 @@ var xxx_messageInfo_MsgNewBidResponse proto.InternalMessageInfo
type MsgLockFunds struct { type MsgLockFunds struct {
Creator string `protobuf:"bytes,1,opt,name=creator,proto3" json:"creator,omitempty"` Creator string `protobuf:"bytes,1,opt,name=creator,proto3" json:"creator,omitempty"`
Amount string `protobuf:"bytes,2,opt,name=amount,proto3" json:"amount,omitempty"`
} }
func (m *MsgLockFunds) Reset() { *m = MsgLockFunds{} } func (m *MsgLockFunds) Reset() { *m = MsgLockFunds{} }
@ -295,6 +296,13 @@ func (m *MsgLockFunds) GetCreator() string {
return "" return ""
} }
func (m *MsgLockFunds) GetAmount() string {
if m != nil {
return m.Amount
}
return ""
}
type MsgLockFundsResponse struct { type MsgLockFundsResponse struct {
} }
@ -343,31 +351,31 @@ func init() {
func init() { proto.RegisterFile("colinearcore/tx.proto", fileDescriptor_0c3854a2a9bba3b4) } func init() { proto.RegisterFile("colinearcore/tx.proto", fileDescriptor_0c3854a2a9bba3b4) }
var fileDescriptor_0c3854a2a9bba3b4 = []byte{ var fileDescriptor_0c3854a2a9bba3b4 = []byte{
// 376 bytes of a gzipped FileDescriptorProto // 383 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x53, 0xcb, 0x4a, 0xc3, 0x40, 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x53, 0xcd, 0x4a, 0xf3, 0x40,
0x14, 0x6d, 0xfa, 0x88, 0xe6, 0x5a, 0x17, 0x8e, 0x6d, 0x09, 0x41, 0x43, 0x88, 0x2e, 0x02, 0x4a, 0x14, 0x6d, 0xfa, 0x93, 0xef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3,
0xb3, 0xfe, 0x53, 0xe4, 0xa9, 0x0f, 0xa0, 0x2c, 0xda, 0x3f, 0x58, 0x6e, 0xcb, 0x45, 0xda, 0xd1, 0xe0, 0xac, 0x95, 0xba, 0x3e, 0xa3, 0x46, 0xf7, 0xb7, 0x8a, 0xdc, 0xf5, 0x16, 0xb4, 0x65, 0x2c,
0x1a, 0xa2, 0x2c, 0xbe, 0x77, 0xfe, 0x39, 0xd3, 0xa5, 0xe9, 0x4c, 0x97, 0xbe, 0x67, 0xba, 0xf4, 0x3b, 0x3f, 0xcb, 0xf2, 0x22, 0x63, 0x6f, 0x8d, 0xa2, 0xcc, 0xbe, 0x7f, 0xfc, 0x36, 0x37, 0x95,
0x36, 0xd7, 0x2b, 0xd3, 0xb9, 0x5e, 0xf9, 0x9a, 0xeb, 0x95, 0xfb, 0xfd, 0xcc, 0xdc, 0x9d, 0x74, 0xd9, 0xdc, 0x54, 0x3e, 0xe6, 0xa6, 0xf2, 0xbc, 0x30, 0x2b, 0xb3, 0x85, 0x59, 0x79, 0x5f, 0x98,
0xcb, 0x0b, 0xf1, 0x1a, 0xa0, 0x78, 0x92, 0xe3, 0xa5, 0x38, 0xfd, 0x09, 0x00, 0x00, 0xff, 0xff, 0x95, 0x9b, 0xed, 0x4c, 0xdc, 0x9b, 0xf6, 0xca, 0x8b, 0xf2, 0x14, 0xa0, 0xbc, 0x57, 0xe3, 0x65,
0x3b, 0x99, 0x2d, 0x8c, 0x2d, 0x03, 0x00, 0x00, 0x39, 0xfc, 0x0c, 0x00, 0x00, 0xff, 0xff, 0x79, 0x5d, 0x54, 0xc3, 0x45, 0x03, 0x00, 0x00,
} }
// Reference imports to suppress errors if they are not otherwise used. // Reference imports to suppress errors if they are not otherwise used.
@ -702,6 +710,13 @@ func (m *MsgLockFunds) MarshalToSizedBuffer(dAtA []byte) (int, error) {
_ = i _ = i
var l int var l int
_ = l _ = l
if len(m.Amount) > 0 {
i -= len(m.Amount)
copy(dAtA[i:], m.Amount)
i = encodeVarintTx(dAtA, i, uint64(len(m.Amount)))
i--
dAtA[i] = 0x12
}
if len(m.Creator) > 0 { if len(m.Creator) > 0 {
i -= len(m.Creator) i -= len(m.Creator)
copy(dAtA[i:], m.Creator) copy(dAtA[i:], m.Creator)
@ -831,6 +846,10 @@ func (m *MsgLockFunds) Size() (n int) {
if l > 0 { if l > 0 {
n += 1 + l + sovTx(uint64(l)) n += 1 + l + sovTx(uint64(l))
} }
l = len(m.Amount)
if l > 0 {
n += 1 + l + sovTx(uint64(l))
}
return n return n
} }
@ -1417,6 +1436,38 @@ func (m *MsgLockFunds) Unmarshal(dAtA []byte) error {
} }
m.Creator = string(dAtA[iNdEx:postIndex]) m.Creator = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex iNdEx = postIndex
case 2:
if wireType != 2 {
return fmt.Errorf("proto: wrong wireType = %d for field Amount", wireType)
}
var stringLen uint64
for shift := uint(0); ; shift += 7 {
if shift >= 64 {
return ErrIntOverflowTx
}
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 ErrInvalidLengthTx
}
postIndex := iNdEx + intStringLen
if postIndex < 0 {
return ErrInvalidLengthTx
}
if postIndex > l {
return io.ErrUnexpectedEOF
}
m.Amount = string(dAtA[iNdEx:postIndex])
iNdEx = postIndex
default: default:
iNdEx = preIndex iNdEx = preIndex
skippy, err := skipTx(dAtA[iNdEx:]) skippy, err := skipTx(dAtA[iNdEx:])