From ca674d898a270f9ece82b7c37d85b6b974cec78c Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Thu, 21 Jul 2022 01:22:49 +0200 Subject: [PATCH 01/14] feat: basis point math package --- math/bpmath/doc.go | 2 ++ math/bpmath/fixed_bp.go | 21 ++++++++++++++++ math/bpmath/fuxed_bp_test.go | 46 ++++++++++++++++++++++++++++++++++++ math/bpmath/types.go | 11 +++++++++ 4 files changed, 80 insertions(+) create mode 100644 math/bpmath/doc.go create mode 100644 math/bpmath/fixed_bp.go create mode 100644 math/bpmath/fuxed_bp_test.go create mode 100644 math/bpmath/types.go diff --git a/math/bpmath/doc.go b/math/bpmath/doc.go new file mode 100644 index 0000000000..cec2cadc5d --- /dev/null +++ b/math/bpmath/doc.go @@ -0,0 +1,2 @@ +// Package bpmath provides types and functions for doing basis point operations. +package bpmath diff --git a/math/bpmath/fixed_bp.go b/math/bpmath/fixed_bp.go new file mode 100644 index 0000000000..6c0e86cd26 --- /dev/null +++ b/math/bpmath/fixed_bp.go @@ -0,0 +1,21 @@ +package bpmath + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// FixedBP assures that all oprations are in 0-10'000 range +type FixedBP uint32 + +// FixedQuo returns a/b in basis points. Returns 10'000 if a >= b or b==0; +// Contract: a>=0 and b >= 0. +func FixedQuo(a, b sdk.Int, rounding Rounding) FixedBP { + if a.GTE(b) { + return ONE + } + bp := a.MulRaw(ONE) + if rounding == UP { + bp = bp.Add(b.SubRaw(1)) + } + return FixedBP(bp.Quo(b).Uint64()) +} diff --git a/math/bpmath/fuxed_bp_test.go b/math/bpmath/fuxed_bp_test.go new file mode 100644 index 0000000000..c67989cb5f --- /dev/null +++ b/math/bpmath/fuxed_bp_test.go @@ -0,0 +1,46 @@ +package bpmath + +import ( + "fmt" + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" +) + +func TestFixedQuo(t *testing.T) { + tcs := []struct { + name string + a uint64 + b uint64 + r Rounding + exp FixedBP + }{ + {"t1", 0, 0, UP, ONE}, + {"t2", 0, 0, DOWN, ONE}, + {"t3", 1, 0, UP, ONE}, + {"t4", 1, 0, DOWN, ONE}, + + {"t5", 20, 10, UP, ONE}, + {"t6", 20, 10, DOWN, ONE}, + {"t7", 20, 20, UP, ONE}, + {"t7-1", 20, 20, DOWN, ONE}, + + {"t8", 1, 2, UP, ONE / 2}, + {"t9", 1, 2, DOWN, ONE / 2}, + {"t10", 1, 3, UP, 3334}, + {"t11", 1, 3, DOWN, 3333}, + {"t12", 2, 3, UP, 6667}, + {"t13", 2, 3, DOWN, 6666}, + {"t14", 10, 99999, UP, 2}, + {"t15", 10, 99999, DOWN, 1}, + {"t16", 10, 99999999, UP, 1}, + {"t17", 10, 99999999, DOWN, 0}, + } + require := require.New(t) + for _, tc := range tcs { + a, b := sdk.NewIntFromUint64(tc.a), sdk.NewIntFromUint64(tc.b) + o := FixedQuo(a, b, tc.r) + require.Equal(int(tc.exp), int(o), fmt.Sprint("test", tc.name)) + } +} diff --git a/math/bpmath/types.go b/math/bpmath/types.go new file mode 100644 index 0000000000..d71439dda3 --- /dev/null +++ b/math/bpmath/types.go @@ -0,0 +1,11 @@ +package bpmath + +type Rounding uint + +const ( + DOWN = iota + UP +) + +const ONE = 10000 +const half = 500 From afbfce8872b5c8dedc32df9ee8922144fe30f762 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Thu, 21 Jul 2022 01:24:46 +0200 Subject: [PATCH 02/14] proto: change kink_utilization from Dec to basis points --- proto/umee/leverage/v1/leverage.proto | 8 +- x/leverage/types/leverage.pb.go | 147 +++++++++++--------------- 2 files changed, 67 insertions(+), 88 deletions(-) diff --git a/proto/umee/leverage/v1/leverage.proto b/proto/umee/leverage/v1/leverage.proto index 5649ece131..0d8b1b4e06 100644 --- a/proto/umee/leverage/v1/leverage.proto +++ b/proto/umee/leverage/v1/leverage.proto @@ -102,11 +102,9 @@ message Token { ]; // The kink_utilization defines the value where the kink rate kicks off for - // borrow rates. - string kink_utilization = 8 [ - (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", - (gogoproto.nullable) = false, - (gogoproto.moretags) = "yaml:\"kink_utilization\"" + // borrow rates. Value is in basis points; allowed values are in [0, 100] range. + uint32 kink_utilization = 8 [ + (gogoproto.moretags) = "yaml:\"kink_utilization\"" ]; // The liquidation_incentive determines the portion of bonus collateral of diff --git a/x/leverage/types/leverage.pb.go b/x/leverage/types/leverage.pb.go index 2358f1caff..8b331bb3ed 100644 --- a/x/leverage/types/leverage.pb.go +++ b/x/leverage/types/leverage.pb.go @@ -101,8 +101,8 @@ type Token struct { // asset (seen when supply utilization is 100%). MaxBorrowRate github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,7,opt,name=max_borrow_rate,json=maxBorrowRate,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"max_borrow_rate" yaml:"max_borrow_rate"` // The kink_utilization defines the value where the kink rate kicks off for - // borrow rates. - KinkUtilization github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,8,opt,name=kink_utilization,json=kinkUtilization,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"kink_utilization" yaml:"kink_utilization"` + // borrow rates. Value is in basis points; allowed values are in [0, 100] range. + KinkUtilization uint32 `protobuf:"varint,8,opt,name=kink_utilization,json=kinkUtilization,proto3" json:"kink_utilization,omitempty" yaml:"kink_utilization"` // The liquidation_incentive determines the portion of bonus collateral of // a token type liquidators receive as a liquidation reward. LiquidationIncentive github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,9,opt,name=liquidation_incentive,json=liquidationIncentive,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"liquidation_incentive" yaml:"liquidation_incentive"` @@ -175,57 +175,57 @@ func init() { func init() { proto.RegisterFile("umee/leverage/v1/leverage.proto", fileDescriptor_8cb1bf9ea641ecc6) } var fileDescriptor_8cb1bf9ea641ecc6 = []byte{ - // 791 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x56, 0x3f, 0x6f, 0x3b, 0x35, - 0x18, 0xce, 0x41, 0x7e, 0xa5, 0x71, 0x9b, 0xa4, 0xbd, 0xa6, 0xed, 0x09, 0xc2, 0x5d, 0x65, 0x09, - 0xd4, 0xa5, 0x39, 0x0a, 0x4c, 0x19, 0xd3, 0x0a, 0x5a, 0x44, 0x0b, 0xb8, 0x45, 0x95, 0x58, 0x4e, - 0xce, 0xc5, 0x24, 0xa7, 0xf8, 0xce, 0xc1, 0x76, 0xfe, 0x75, 0x61, 0x40, 0xec, 0x8c, 0x2c, 0x48, - 0xfd, 0x18, 0x7c, 0x84, 0x8e, 0x1d, 0x11, 0x43, 0x04, 0xed, 0xc2, 0x9c, 0x99, 0x01, 0x9d, 0x7d, - 0x49, 0x2e, 0x6d, 0xa8, 0x14, 0xb5, 0x53, 0xec, 0xe7, 0x7d, 0xf3, 0x3c, 0xaf, 0xfd, 0x3e, 0xf6, - 0x19, 0x38, 0xdd, 0x90, 0x10, 0x97, 0x92, 0x1e, 0xe1, 0xb8, 0x49, 0xdc, 0xde, 0xe1, 0x74, 0x5c, - 0xe9, 0x70, 0x26, 0x99, 0xb9, 0x11, 0x27, 0x54, 0xa6, 0x60, 0xef, 0xf0, 0xdd, 0x52, 0x93, 0x35, - 0x99, 0x0a, 0xba, 0xf1, 0x48, 0xe7, 0xc1, 0xdf, 0xb3, 0x60, 0xe5, 0x6b, 0xcc, 0x71, 0x28, 0xcc, - 0xdf, 0x0c, 0x60, 0xfb, 0x2c, 0xec, 0x50, 0x22, 0x89, 0x47, 0x83, 0x1f, 0xba, 0x41, 0x03, 0xcb, - 0x80, 0x45, 0x9e, 0x6c, 0x71, 0x22, 0x5a, 0x8c, 0x36, 0xac, 0xb7, 0xf6, 0x8c, 0xfd, 0x5c, 0xed, - 0xea, 0x76, 0xe4, 0x64, 0xfe, 0x1c, 0x39, 0x1f, 0x36, 0x03, 0xd9, 0xea, 0xd6, 0x2b, 0x3e, 0x0b, - 0x5d, 0x9f, 0x89, 0x90, 0x89, 0xe4, 0xe7, 0x40, 0x34, 0xda, 0xae, 0x1c, 0x76, 0x88, 0xa8, 0x1c, - 0x13, 0x7f, 0x3c, 0x72, 0x3e, 0x18, 0xe2, 0x90, 0x56, 0xe1, 0xf3, 0xec, 0x10, 0x95, 0x27, 0x09, - 0x5f, 0xce, 0xe2, 0x97, 0x93, 0xb0, 0xf9, 0x23, 0x28, 0x85, 0x41, 0x14, 0x84, 0xdd, 0xd0, 0xf3, - 0x29, 0x13, 0xc4, 0xfb, 0x1e, 0xfb, 0x92, 0x71, 0xeb, 0x6d, 0x55, 0xd4, 0xd9, 0xd2, 0x45, 0xbd, - 0xa7, 0x8b, 0x5a, 0xc4, 0x09, 0x91, 0x99, 0xc0, 0x47, 0x31, 0xfa, 0x99, 0x02, 0xe3, 0x02, 0x18, - 0xc7, 0x3e, 0x25, 0x1e, 0x27, 0x7d, 0xcc, 0x1b, 0x93, 0x02, 0xb2, 0x2f, 0x2b, 0x60, 0x11, 0x27, - 0x44, 0xa6, 0x86, 0x91, 0x42, 0x93, 0x02, 0x7e, 0x36, 0xc0, 0x8e, 0x08, 0x31, 0xa5, 0x73, 0x1b, - 0x28, 0x82, 0x6b, 0x62, 0xbd, 0x51, 0x35, 0x7c, 0xb5, 0x74, 0x0d, 0xef, 0xeb, 0x1a, 0x16, 0xb3, - 0x42, 0x54, 0x52, 0x81, 0x54, 0x3b, 0x2e, 0x82, 0x6b, 0x52, 0xcd, 0xfe, 0x7a, 0xe3, 0x64, 0xe0, - 0xbf, 0x00, 0xbc, 0xb9, 0x64, 0x6d, 0x12, 0x99, 0x9f, 0x02, 0x50, 0xc7, 0x82, 0x78, 0x0d, 0x12, - 0xb1, 0xd0, 0x32, 0x54, 0x29, 0xdb, 0xe3, 0x91, 0xb3, 0xa9, 0xc9, 0x67, 0x31, 0x88, 0x72, 0xf1,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0xee, 0x6d, 0xe3, - 0x97, 0x07, 0x3b, 0x73, 0xf7, 0x60, 0x67, 0xfe, 0x78, 0xb0, 0x33, 0xdf, 0x7d, 0x94, 0xea, 0x79, - 0xfc, 0x14, 0x38, 0x88, 0x88, 0xec, 0x33, 0xde, 0x56, 0x13, 0xb7, 0xf7, 0xb1, 0x3b, 0x98, 0xbd, - 0x1e, 0x94, 0x03, 0xea, 0x2b, 0xea, 0x41, 0xf0, 0xc9, 0x7f, 0x01, 0x00, 0x00, 0xff, 0xff, 0x48, - 0x27, 0xd0, 0x61, 0x5b, 0x08, 0x00, 0x00, + // 796 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x56, 0x4d, 0x6f, 0x2b, 0x35, + 0x14, 0xcd, 0x40, 0x5e, 0x69, 0xfc, 0x5e, 0x92, 0x76, 0x5e, 0xfa, 0x3a, 0xa2, 0x61, 0xa6, 0xb2, + 0x04, 0xea, 0xa6, 0x19, 0x0a, 0xac, 0xb2, 0x4c, 0xab, 0xd2, 0x22, 0x5a, 0xc0, 0x2d, 0xaa, 0xc4, + 0x66, 0xe4, 0x4c, 0x4c, 0x32, 0x8a, 0x67, 0x1c, 0x6c, 0xe7, 0xab, 0x1b, 0x16, 0x88, 0x3d, 0x4b, + 0x36, 0x48, 0xfd, 0x19, 0xfc, 0x84, 0xae, 0x50, 0x97, 0x88, 0x45, 0x04, 0xed, 0x86, 0x75, 0x7e, + 0x01, 0x1a, 0x7b, 0x92, 0x4c, 0xd2, 0x50, 0x29, 0xea, 0x5b, 0xd5, 0x3e, 0xf7, 0xf6, 0x9c, 0x63, + 0xdf, 0x7b, 0xc7, 0x01, 0x4e, 0x37, 0x24, 0xc4, 0xa5, 0xa4, 0x47, 0x38, 0x6e, 0x12, 0xb7, 0x77, + 0x30, 0x5d, 0x57, 0x3a, 0x9c, 0x49, 0x66, 0x6e, 0xc4, 0x09, 0x95, 0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, 0x45, 0x70, 0x4d, 0xac, 0x17, 0xca, 0xc3, 0x57, 0x2b, 0x7b, 0xf8, 0x40, 0x7b, 0x58, + 0xce, 0x0a, 0x51, 0x49, 0x05, 0x52, 0xe5, 0xb8, 0x08, 0xae, 0x49, 0x35, 0xfb, 0xeb, 0x8d, 0x93, + 0x81, 0x7f, 0x00, 0xf0, 0xe2, 0x92, 0xb5, 0x49, 0x64, 0x7e, 0x06, 0x40, 0x1d, 0x0b, 0xe2, 0x35, + 0x48, 0xc4, 0x42, 0xcb, 0x50, 0x56, 0xb6, 0xc6, 0x23, 0x67, 0x53, 0x93, 0xcf, 0x62, 0x10, 0xe5, + 0xe2, 0xcd, 0x51, 0xbc, 0x36, 0x23, 0x50, 0xe0, 0x44, 0x10, 0xde, 0x9b, 0x56, 0x52, 0xb7, 0xd7, + 0xe7, 0x2b, 0x1f, 0x62, 0x4b, 0xeb, 0xcc, 0xb3, 0x41, 0x94, 0x4f, 0x80, 0xe4, 0xf6, 0xfa, 0x60, + 0xd3, 0x67, 0x94, 0x62, 0x49, 0x38, 0xa6, 0x5e, 0x9f, 0x04, 0xcd, 0x96, 0x4c, 0x9a, 0xe7, 0x8b, + 0x95, 0x25, 0xad, 0x49, 0x47, 0x2f, 0x10, 0x42, 0xb4, 0x31, 0xc3, 0xae, 0x14, 0x64, 0xfe, 0x64, + 0x80, 0xad, 0xe5, 0xf3, 0xa4, 0x3b, 0xe7, 0x7c, 0x65, 0xf5, 0xb2, 0x56, 0xff, 0x9f, 0x31, 0x2a, + 0xd1, 0x65, 0xe3, 0x23, 0xc0, 0x86, 0x2a, 0x44, 0x9d, 0x71, 0xce, 0xfa, 0x1e, 0xc7, 0x72, 0xd2, + 0x35, 0xa7, 0x2b, 0xeb, 0x6f, 0xa7, 0x0a, 0x9b, 0xe2, 0x83, 0xa8, 0x10, 0x43, 0x35, 0x85, 0x20, + 0x2c, 0x49, 0x2c, 0xda, 0x0e, 0xa2, 0xf6, 0x9c, 0xe8, 0xda,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, + 0x25, 0x35, 0xcb, 0x20, 0x57, 0xa7, 0xd8, 0x6f, 0xd3, 0x40, 0x48, 0xab, 0x10, 0x33, 0xa0, 0x19, + 0x60, 0x7e, 0x03, 0x4a, 0x71, 0x3f, 0xa4, 0x46, 0x5f, 0xb4, 0x30, 0x27, 0x56, 0x51, 0x1d, 0xd7, + 0x49, 0xbd, 0x2c, 0x4b, 0xb2, 0xe2, 0x97, 0x05, 0x0f, 0x0e, 0xa7, 0xe8, 0x45, 0x0c, 0x56, 0xb3, + 0xff, 0xde, 0x38, 0x46, 0xed, 0xfc, 0xf6, 0x1f, 0x3b, 0x73, 0x7b, 0x6f, 0x1b, 0x77, 0xf7, 0xb6, + 0xf1, 0xf7, 0xbd, 0x6d, 0xfc, 0xf2, 0x60, 0x67, 0xee, 0x1e, 0xec, 0xcc, 0x9f, 0x0f, 0x76, 0xe6, + 0xbb, 0x8f, 0x53, 0x35, 0x8f, 0x1f, 0xf7, 0xfd, 0x88, 0xc8, 0x3e, 0xe3, 0x6d, 0xb5, 0x71, 0x7b, + 0x9f, 0xb8, 0x83, 0xd9, 0xef, 0x01, 0xd5, 0x01, 0xf5, 0x35, 0xf5, 0xc4, 0x7f, 0xfa, 0x5f, 0x00, + 0x00, 0x00, 0xff, 0xff, 0x73, 0xc3, 0xd8, 0xe4, 0x2d, 0x08, 0x00, 0x00, } func (this *Token) Equal(that interface{}) bool { @@ -268,7 +268,7 @@ func (this *Token) Equal(that interface{}) bool { if !this.MaxBorrowRate.Equal(that1.MaxBorrowRate) { return false } - if !this.KinkUtilization.Equal(that1.KinkUtilization) { + if this.KinkUtilization != that1.KinkUtilization { return false } if !this.LiquidationIncentive.Equal(that1.LiquidationIncentive) { @@ -434,16 +434,11 @@ func (m *Token) MarshalToSizedBuffer(dAtA []byte) (int, error) { } i-- dAtA[i] = 0x4a - { - size := m.KinkUtilization.Size() - i -= size - if _, err := m.KinkUtilization.MarshalTo(dAtA[i:]); err != nil { - return 0, err - } - i = encodeVarintLeverage(dAtA, i, uint64(size)) + if m.KinkUtilization != 0 { + i = encodeVarintLeverage(dAtA, i, uint64(m.KinkUtilization)) + i-- + dAtA[i] = 0x40 } - i-- - dAtA[i] = 0x42 { size := m.MaxBorrowRate.Size() i -= size @@ -564,8 +559,9 @@ func (m *Token) Size() (n int) { n += 1 + l + sovLeverage(uint64(l)) l = m.MaxBorrowRate.Size() n += 1 + l + sovLeverage(uint64(l)) - l = m.KinkUtilization.Size() - n += 1 + l + sovLeverage(uint64(l)) + if m.KinkUtilization != 0 { + n += 1 + sovLeverage(uint64(m.KinkUtilization)) + } l = m.LiquidationIncentive.Size() n += 1 + l + sovLeverage(uint64(l)) l = len(m.SymbolDenom) @@ -1048,10 +1044,10 @@ func (m *Token) Unmarshal(dAtA []byte) error { } iNdEx = postIndex case 8: - if wireType != 2 { + if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field KinkUtilization", wireType) } - var stringLen uint64 + m.KinkUtilization = 0 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeverage @@ -1061,26 +1057,11 @@ func (m *Token) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - stringLen |= uint64(b&0x7F) << shift + m.KinkUtilization |= uint32(b&0x7F) << shift if b < 0x80 { break } } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLengthLeverage - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthLeverage - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - if err := m.KinkUtilization.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex case 9: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field LiquidationIncentive", wireType) From f637943aa860b67c5fbdea6d473ecf02d50618f8 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Thu, 21 Jul 2022 01:48:27 +0200 Subject: [PATCH 03/14] add more fixed bp ops --- math/bpmath/fixed_bp.go | 13 +++++++++++++ math/bpmath/fuxed_bp_test.go | 30 +++++++++++++++++++++++++++++- math/bpmath/types.go | 14 +++++++++++++- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/math/bpmath/fixed_bp.go b/math/bpmath/fixed_bp.go index 6c0e86cd26..5488ae4731 100644 --- a/math/bpmath/fixed_bp.go +++ b/math/bpmath/fixed_bp.go @@ -5,6 +5,7 @@ import ( ) // FixedBP assures that all oprations are in 0-10'000 range +// Note FixedBP operations should not be chained - this causes precision loses. type FixedBP uint32 // FixedQuo returns a/b in basis points. Returns 10'000 if a >= b or b==0; @@ -19,3 +20,15 @@ func FixedQuo(a, b sdk.Int, rounding Rounding) FixedBP { } return FixedBP(bp.Quo(b).Uint64()) } + +// FixedMul returns a * b_in_basis_points +// Contract: b \in [0; 10000] +func FixedMul(a sdk.Int, b FixedBP) sdk.Int { + if b == 0 { + return sdk.ZeroInt() + } + if b == ONE { + return a + } + return a.MulRaw(int64(b)).Quo(oneBigInt) +} diff --git a/math/bpmath/fuxed_bp_test.go b/math/bpmath/fuxed_bp_test.go index c67989cb5f..3bf1f5a38f 100644 --- a/math/bpmath/fuxed_bp_test.go +++ b/math/bpmath/fuxed_bp_test.go @@ -9,6 +9,7 @@ import ( ) func TestFixedQuo(t *testing.T) { + t.Parallel() tcs := []struct { name string a uint64 @@ -41,6 +42,33 @@ func TestFixedQuo(t *testing.T) { for _, tc := range tcs { a, b := sdk.NewIntFromUint64(tc.a), sdk.NewIntFromUint64(tc.b) o := FixedQuo(a, b, tc.r) - require.Equal(int(tc.exp), int(o), fmt.Sprint("test", tc.name)) + require.Equal(int(tc.exp), int(o), fmt.Sprint("test ", tc.name)) + } +} + +func TestFixedMul(t *testing.T) { + t.Parallel() + tcs := []struct { + name string + a uint64 + b FixedBP + exp uint64 + }{ + {"t1", 20, 0, 0}, + {"t2", 20, 1, 0}, + {"t3", 20, ONE, 20}, + {"t4", 20000, 0, 0}, + {"t5", 20000, 1, 2}, + {"t6", 20000, 2, 4}, + {"t7", 20000, half, 10000}, + {"t8", 2000, 4, 0}, + {"t9", 2000, 5, 1}, + {"t10", 2000, half, 1000}, + } + require := require.New(t) + for _, tc := range tcs { + a := sdk.NewIntFromUint64(tc.a) + o := FixedMul(a, tc.b) + require.Equal(int64(tc.exp), o.Int64(), fmt.Sprint("test ", tc.name)) } } diff --git a/math/bpmath/types.go b/math/bpmath/types.go index d71439dda3..ab7c35acf0 100644 --- a/math/bpmath/types.go +++ b/math/bpmath/types.go @@ -1,5 +1,9 @@ package bpmath +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + type Rounding uint const ( @@ -8,4 +12,12 @@ const ( ) const ONE = 10000 -const half = 500 +const half = ONE / 2 + +var ( + oneBigInt sdk.Int +) + +func init() { + oneBigInt = sdk.NewIntFromUint64(ONE) +} From a578f0a214ad2935bf5723cd8d0ab2be222a8593 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Thu, 21 Jul 2022 01:51:14 +0200 Subject: [PATCH 04/14] proto --- proto/umee/leverage/v1/leverage.proto | 16 ++- x/leverage/types/leverage.pb.go | 182 +++++++++++++------------- 2 files changed, 102 insertions(+), 96 deletions(-) diff --git a/proto/umee/leverage/v1/leverage.proto b/proto/umee/leverage/v1/leverage.proto index 0d8b1b4e06..987aad0c51 100644 --- a/proto/umee/leverage/v1/leverage.proto +++ b/proto/umee/leverage/v1/leverage.proto @@ -102,17 +102,19 @@ message Token { ]; // The kink_utilization defines the value where the kink rate kicks off for - // borrow rates. Value is in basis points; allowed values are in [0, 100] range. - uint32 kink_utilization = 8 [ - (gogoproto.moretags) = "yaml:\"kink_utilization\"" + // borrow rates. + string kink_utilization = 8 [ + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", + (gogoproto.nullable) = false, + (gogoproto.moretags) = "yaml:\"kink_utilization\"" ]; // The liquidation_incentive determines the portion of bonus collateral of // a token type liquidators receive as a liquidation reward. - string liquidation_incentive = 9 [ - (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", - (gogoproto.nullable) = false, - (gogoproto.moretags) = "yaml:\"liquidation_incentive\"" + // Value is in basis points; allowed values are in [0, 10000] range. + uint32 liquidation_incentive = 9 [ + (gogoproto.moretags) = "yaml:\"liquidation_incentive\"", + (gogoproto.casttype) = "github.com/umee-network/umee/v2/math/bpmath.FixedBP" ]; // The symbol_denom and exponent are solely used to update the oracle's accept diff --git a/x/leverage/types/leverage.pb.go b/x/leverage/types/leverage.pb.go index 8b331bb3ed..d398842988 100644 --- a/x/leverage/types/leverage.pb.go +++ b/x/leverage/types/leverage.pb.go @@ -8,6 +8,7 @@ import ( github_com_cosmos_cosmos_sdk_types "github.com/cosmos/cosmos-sdk/types" _ "github.com/gogo/protobuf/gogoproto" proto "github.com/gogo/protobuf/proto" + github_com_umee_network_umee_v2_math_bpmath "github.com/umee-network/umee/v2/math/bpmath" io "io" math "math" math_bits "math/bits" @@ -101,11 +102,12 @@ type Token struct { // asset (seen when supply utilization is 100%). MaxBorrowRate github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,7,opt,name=max_borrow_rate,json=maxBorrowRate,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"max_borrow_rate" yaml:"max_borrow_rate"` // The kink_utilization defines the value where the kink rate kicks off for - // borrow rates. Value is in basis points; allowed values are in [0, 100] range. - KinkUtilization uint32 `protobuf:"varint,8,opt,name=kink_utilization,json=kinkUtilization,proto3" json:"kink_utilization,omitempty" yaml:"kink_utilization"` + // borrow rates. + KinkUtilization github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,8,opt,name=kink_utilization,json=kinkUtilization,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"kink_utilization" yaml:"kink_utilization"` // The liquidation_incentive determines the portion of bonus collateral of // a token type liquidators receive as a liquidation reward. - LiquidationIncentive github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,9,opt,name=liquidation_incentive,json=liquidationIncentive,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"liquidation_incentive" yaml:"liquidation_incentive"` + // Value is in basis points; allowed values are in [0, 10000] range. + LiquidationIncentive github_com_umee_network_umee_v2_math_bpmath.FixedBP `protobuf:"varint,9,opt,name=liquidation_incentive,json=liquidationIncentive,proto3,casttype=github.com/umee-network/umee/v2/math/bpmath.FixedBP" json:"liquidation_incentive,omitempty" yaml:"liquidation_incentive"` // The symbol_denom and exponent are solely used to update the oracle's accept // list of allowed tokens. SymbolDenom string `protobuf:"bytes,10,opt,name=symbol_denom,json=symbolDenom,proto3" json:"symbol_denom,omitempty" yaml:"symbol_denom"` @@ -175,57 +177,59 @@ func init() { func init() { proto.RegisterFile("umee/leverage/v1/leverage.proto", fileDescriptor_8cb1bf9ea641ecc6) } var fileDescriptor_8cb1bf9ea641ecc6 = []byte{ - // 796 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x56, 0x4d, 0x6f, 0x2b, 0x35, - 0x14, 0xcd, 0x40, 0x5e, 0x69, 0xfc, 0x5e, 0x92, 0x76, 0x5e, 0xfa, 0x3a, 0xa2, 0x61, 0xa6, 0xb2, - 0x04, 0xea, 0xa6, 0x19, 0x0a, 0xac, 0xb2, 0x4c, 0xab, 0xd2, 0x22, 0x5a, 0xc0, 0x2d, 0xaa, 0xc4, - 0x66, 0xe4, 0x4c, 0x4c, 0x32, 0x8a, 0x67, 0x1c, 0x6c, 0xe7, 0xab, 0x1b, 0x16, 0x88, 0x3d, 0x4b, - 0x36, 0x48, 0xfd, 0x19, 0xfc, 0x84, 0xae, 0x50, 0x97, 0x88, 0x45, 0x04, 0xed, 0x86, 0x75, 0x7e, - 0x01, 0x1a, 0x7b, 0x92, 0x4c, 0xd2, 0x50, 0x29, 0xea, 0x5b, 0xd5, 0x3e, 0xf7, 0xf6, 0x9c, 0x63, - 0xdf, 0x7b, 0xc7, 0x01, 0x4e, 0x37, 0x24, 0xc4, 0xa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 0x2c, - 0xe3, 0x84, 0xc8, 0xd4, 0x30, 0x52, 0x68, 0x62, 0xe0, 0x67, 0x03, 0xbc, 0x11, 0x21, 0xa6, 0x74, - 0xee, 0x02, 0x45, 0x70, 0x4d, 0xac, 0x17, 0xca, 0xc3, 0x57, 0x2b, 0x7b, 0xf8, 0x40, 0x7b, 0x58, - 0xce, 0x0a, 0x51, 0x49, 0x05, 0x52, 0xe5, 0xb8, 0x08, 0xae, 0x49, 0x35, 0xfb, 0xeb, 0x8d, 0x93, - 0x81, 0x7f, 0x00, 0xf0, 0xe2, 0x92, 0xb5, 0x49, 0x64, 0x7e, 0x06, 0x40, 0x1d, 0x0b, 0xe2, 0x35, - 0x48, 0xc4, 0x42, 0xcb, 0x50, 0x56, 0xb6, 0xc6, 0x23, 0x67, 0x53, 0x93, 0xcf, 0x62, 0x10, 0xe5, - 0xe2, 0xcd, 0x51, 0xbc, 0x36, 0x23, 0x50, 0xe0, 0x44, 0x10, 0xde, 0x9b, 0x56, 0x52, 0xb7, 0xd7, - 0xe7, 0x2b, 0x1f, 0x62, 0x4b, 0xeb, 0xcc, 0xb3, 0x41, 0x94, 0x4f, 0x80, 0xe4, 0xf6, 0xfa, 0x60, - 0xd3, 0x67, 0x94, 0x62, 0x49, 0x38, 0xa6, 0x5e, 0x9f, 0x04, 0xcd, 0x96, 0x4c, 0x9a, 0xe7, 0x8b, - 0x95, 0x25, 0xad, 0x49, 0x47, 0x2f, 0x10, 0x42, 0xb4, 0x31, 0xc3, 0xae, 0x14, 0x64, 0xfe, 0x64, - 0x80, 0xad, 0xe5, 0xf3, 0xa4, 0x3b, 0xe7, 0x7c, 0x65, 0xf5, 0xb2, 0x56, 0xff, 0x9f, 0x31, 0x2a, - 0xd1, 0x65, 0xe3, 0x23, 0xc0, 0x86, 0x2a, 0x44, 0x9d, 0x71, 0xce, 0xfa, 0x1e, 0xc7, 0x72, 0xd2, - 0x35, 0xa7, 0x2b, 0xeb, 0x6f, 0xa7, 0x0a, 0x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0xa2, 0xc6, 0xce, - 0x44, 0xf3, 0x42, 0x21, 0x0b, 0x4c, 0xba, 0x5c, 0x56, 0xfe, 0x09, 0x26, 0x9d, 0x92, 0x66, 0xd2, - 0x25, 0x35, 0xcb, 0x20, 0x57, 0xa7, 0xd8, 0x6f, 0xd3, 0x40, 0x48, 0xab, 0x10, 0x33, 0xa0, 0x19, - 0x60, 0x7e, 0x03, 0x4a, 0x71, 0x3f, 0xa4, 0x46, 0x5f, 0xb4, 0x30, 0x27, 0x56, 0x51, 0x1d, 0xd7, - 0x49, 0xbd, 0x2c, 0x4b, 0xb2, 0xe2, 0x97, 0x05, 0x0f, 0x0e, 0xa7, 0xe8, 0x45, 0x0c, 0x56, 0xb3, - 0xff, 0xde, 0x38, 0x46, 0xed, 0xfc, 0xf6, 0x1f, 0x3b, 0x73, 0x7b, 0x6f, 0x1b, 0x77, 0xf7, 0xb6, - 0xf1, 0xf7, 0xbd, 0x6d, 0xfc, 0xf2, 0x60, 0x67, 0xee, 0x1e, 0xec, 0xcc, 0x9f, 0x0f, 0x76, 0xe6, - 0xbb, 0x8f, 0x53, 0x35, 0x8f, 0x1f, 0xf7, 0xfd, 0x88, 0xc8, 0x3e, 0xe3, 0x6d, 0xb5, 0x71, 0x7b, - 0x9f, 0xb8, 0x83, 0xd9, 0xef, 0x01, 0xd5, 0x01, 0xf5, 0x35, 0xf5, 0xc4, 0x7f, 0xfa, 0x5f, 0x00, - 0x00, 0x00, 0xff, 0xff, 0x73, 0xc3, 0xd8, 0xe4, 0x2d, 0x08, 0x00, 0x00, + // 823 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x56, 0xcf, 0x73, 0x1b, 0x35, + 0x14, 0xf6, 0x42, 0x1a, 0x12, 0xa5, 0x89, 0x93, 0xad, 0xd3, 0xee, 0x40, 0xf0, 0x66, 0x34, 0x03, + 0x93, 0x4b, 0xbd, 0x94, 0x72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 0xfe, 0xa8, + 0x57, 0xce, 0xce, 0xeb, 0xde, 0xeb, 0xf3, 0xba, 0xf7, 0xfb, 0x79, 0xdd, 0xfb, 0xf1, 0xa2, 0x5e, + 0x79, 0x7d, 0x51, 0xaf, 0xfc, 0x7a, 0x51, 0xaf, 0x7c, 0xfd, 0xd1, 0x7f, 0x15, 0x78, 0x34, 0xfb, + 0x8c, 0xb0, 0x6d, 0xd8, 0x5a, 0xb5, 0x5f, 0x06, 0xf7, 0xff, 0x0e, 0x00, 0x00, 0xff, 0xff, 0xd4, + 0x38, 0x9e, 0x8a, 0x64, 0x08, 0x00, 0x00, } func (this *Token) Equal(that interface{}) bool { @@ -268,10 +272,10 @@ func (this *Token) Equal(that interface{}) bool { if !this.MaxBorrowRate.Equal(that1.MaxBorrowRate) { return false } - if this.KinkUtilization != that1.KinkUtilization { + if !this.KinkUtilization.Equal(that1.KinkUtilization) { return false } - if !this.LiquidationIncentive.Equal(that1.LiquidationIncentive) { + if this.LiquidationIncentive != that1.LiquidationIncentive { return false } if this.SymbolDenom != that1.SymbolDenom { @@ -424,21 +428,21 @@ func (m *Token) MarshalToSizedBuffer(dAtA []byte) (int, error) { i-- dAtA[i] = 0x52 } + if m.LiquidationIncentive != 0 { + i = encodeVarintLeverage(dAtA, i, uint64(m.LiquidationIncentive)) + i-- + dAtA[i] = 0x48 + } { - size := m.LiquidationIncentive.Size() + size := m.KinkUtilization.Size() i -= size - if _, err := m.LiquidationIncentive.MarshalTo(dAtA[i:]); err != nil { + if _, err := m.KinkUtilization.MarshalTo(dAtA[i:]); err != nil { return 0, err } i = encodeVarintLeverage(dAtA, i, uint64(size)) } i-- - dAtA[i] = 0x4a - if m.KinkUtilization != 0 { - i = encodeVarintLeverage(dAtA, i, uint64(m.KinkUtilization)) - i-- - dAtA[i] = 0x40 - } + dAtA[i] = 0x42 { size := m.MaxBorrowRate.Size() i -= size @@ -559,11 +563,11 @@ func (m *Token) Size() (n int) { n += 1 + l + sovLeverage(uint64(l)) l = m.MaxBorrowRate.Size() n += 1 + l + sovLeverage(uint64(l)) - if m.KinkUtilization != 0 { - n += 1 + sovLeverage(uint64(m.KinkUtilization)) - } - l = m.LiquidationIncentive.Size() + l = m.KinkUtilization.Size() n += 1 + l + sovLeverage(uint64(l)) + if m.LiquidationIncentive != 0 { + n += 1 + sovLeverage(uint64(m.LiquidationIncentive)) + } l = len(m.SymbolDenom) if l > 0 { n += 1 + l + sovLeverage(uint64(l)) @@ -1044,27 +1048,8 @@ func (m *Token) Unmarshal(dAtA []byte) error { } iNdEx = postIndex case 8: - if wireType != 0 { - return fmt.Errorf("proto: wrong wireType = %d for field KinkUtilization", wireType) - } - m.KinkUtilization = 0 - for shift := uint(0); ; shift += 7 { - if shift >= 64 { - return ErrIntOverflowLeverage - } - if iNdEx >= l { - return io.ErrUnexpectedEOF - } - b := dAtA[iNdEx] - iNdEx++ - m.KinkUtilization |= uint32(b&0x7F) << shift - if b < 0x80 { - break - } - } - case 9: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field LiquidationIncentive", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field KinkUtilization", wireType) } var stringLen uint64 for shift := uint(0); ; shift += 7 { @@ -1092,10 +1077,29 @@ func (m *Token) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - if err := m.LiquidationIncentive.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.KinkUtilization.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex + case 9: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field LiquidationIncentive", wireType) + } + m.LiquidationIncentive = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowLeverage + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.LiquidationIncentive |= github_com_umee_network_umee_v2_math_bpmath.FixedBP(b&0x7F) << shift + if b < 0x80 { + break + } + } case 10: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field SymbolDenom", wireType) From e9c87756d6421ca348d504cb9a4561653dae5eb0 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Thu, 21 Jul 2022 01:55:56 +0200 Subject: [PATCH 05/14] update implementation --- app/test_helpers.go | 2 +- x/leverage/keeper/keeper.go | 11 ++++++----- x/leverage/keeper/keeper_test.go | 5 +++-- x/leverage/types/token.go | 5 +++-- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/app/test_helpers.go b/app/test_helpers.go index 6084fdf229..bb6e41b6a0 100644 --- a/app/test_helpers.go +++ b/app/test_helpers.go @@ -117,7 +117,7 @@ func IntegrationTestNetworkConfig() network.Config { KinkBorrowRate: sdk.MustNewDecFromStr("0.200000000000000000"), MaxBorrowRate: sdk.MustNewDecFromStr("1.50000000000000000"), KinkUtilization: sdk.MustNewDecFromStr("0.200000000000000000"), - LiquidationIncentive: sdk.MustNewDecFromStr("0.180000000000000000"), + LiquidationIncentive: 1800, EnableMsgSupply: true, EnableMsgBorrow: true, Blacklist: false, diff --git a/x/leverage/keeper/keeper.go b/x/leverage/keeper/keeper.go index e51afd0717..7b2c600bc5 100644 --- a/x/leverage/keeper/keeper.go +++ b/x/leverage/keeper/keeper.go @@ -11,6 +11,7 @@ import ( "github.com/hashicorp/golang-lru/simplelru" "github.com/tendermint/tendermint/libs/log" + "github.com/umee-network/umee/v2/math/bpmath" "github.com/umee-network/umee/v2/x/leverage/types" ) @@ -432,7 +433,7 @@ func (k Keeper) LiquidateBorrow( } // apply liquidation incentive - reward.Amount = reward.Amount.ToDec().Mul(sdk.OneDec().Add(liquidationIncentive)).TruncateInt() + reward.Amount = bpmath.FixedMul(reward.Amount, liquidationIncentive).Add(reward.Amount) maxReward := collateral.AmountOf(reward.Denom) if maxReward.IsZero() { @@ -519,17 +520,17 @@ func (k Keeper) LiquidationParams( rewardDenom string, borrowed sdk.Dec, limit sdk.Dec, -) (sdk.Dec, sdk.Dec, error) { +) (bpmath.FixedBP, sdk.Dec, error) { if borrowed.IsNegative() { - return sdk.ZeroDec(), sdk.ZeroDec(), sdkerrors.Wrap(types.ErrBadValue, borrowed.String()) + return 0, sdk.ZeroDec(), sdkerrors.Wrap(types.ErrBadValue, borrowed.String()) } if limit.IsNegative() { - return sdk.ZeroDec(), sdk.ZeroDec(), sdkerrors.Wrap(types.ErrBadValue, limit.String()) + return 0, sdk.ZeroDec(), sdkerrors.Wrap(types.ErrBadValue, limit.String()) } ts, err := k.GetTokenSettings(ctx, rewardDenom) if err != nil { - return sdk.ZeroDec(), sdk.ZeroDec(), err + return 0, sdk.ZeroDec(), err } // special case: If liquidation threshold is zero, close factor is always 1 diff --git a/x/leverage/keeper/keeper_test.go b/x/leverage/keeper/keeper_test.go index cc7b8d4bca..c287c85515 100644 --- a/x/leverage/keeper/keeper_test.go +++ b/x/leverage/keeper/keeper_test.go @@ -13,6 +13,7 @@ import ( tmproto "github.com/tendermint/tendermint/proto/tendermint/types" umeeapp "github.com/umee-network/umee/v2/app" + "github.com/umee-network/umee/v2/math/bpmath" "github.com/umee-network/umee/v2/x/leverage" "github.com/umee-network/umee/v2/x/leverage/keeper" "github.com/umee-network/umee/v2/x/leverage/types" @@ -43,7 +44,7 @@ func newToken(base, symbol string) types.Token { KinkBorrowRate: sdk.MustNewDecFromStr("0.22"), MaxBorrowRate: sdk.MustNewDecFromStr("1.52"), KinkUtilization: sdk.MustNewDecFromStr("0.8"), - LiquidationIncentive: sdk.MustNewDecFromStr("0.1"), + LiquidationIncentive: 1000, EnableMsgSupply: true, EnableMsgBorrow: true, Blacklist: false, @@ -257,7 +258,7 @@ func (s *IntegrationTestSuite) TestGetToken() { s.Require().Equal(t.KinkBorrowRate, sdk.MustNewDecFromStr("0.22")) s.Require().Equal(t.MaxBorrowRate, sdk.MustNewDecFromStr("1.52")) s.Require().Equal(t.KinkUtilization, sdk.MustNewDecFromStr("0.8")) - s.Require().Equal(t.LiquidationIncentive, sdk.MustNewDecFromStr("0.1")) + s.Require().Equal(t.LiquidationIncentive, bpmath.FixedBP(1000)) s.Require().NoError(t.AssertBorrowEnabled()) s.Require().NoError(t.AssertSupplyEnabled()) diff --git a/x/leverage/types/token.go b/x/leverage/types/token.go index 5f7d52f56a..b5782d3636 100644 --- a/x/leverage/types/token.go +++ b/x/leverage/types/token.go @@ -6,6 +6,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/umee-network/umee/v2/math/bpmath" ) const ( @@ -70,8 +71,8 @@ func (t Token) Validate() error { } // Liquidation incentive is non-negative - if t.LiquidationIncentive.IsNegative() { - return fmt.Errorf("invalid liquidation incentive: %s", t.LiquidationIncentive) + if t.LiquidationIncentive > bpmath.ONE { + return fmt.Errorf("invalid liquidation incentive: %s - must be in [0; 10000]", t.LiquidationIncentive) } // Blacklisted assets cannot have borrow or supply enabled From 1bae06fe915f0b5de1541e55a81e44ac8cbb2e12 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Thu, 21 Jul 2022 02:16:47 +0200 Subject: [PATCH 06/14] bpmath.ToDec --- math/bpmath/fixed_bp.go | 4 ++++ math/bpmath/fuxed_bp_test.go | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/math/bpmath/fixed_bp.go b/math/bpmath/fixed_bp.go index 5488ae4731..908805f39d 100644 --- a/math/bpmath/fixed_bp.go +++ b/math/bpmath/fixed_bp.go @@ -32,3 +32,7 @@ func FixedMul(a sdk.Int, b FixedBP) sdk.Int { } return a.MulRaw(int64(b)).Quo(oneBigInt) } + +func (bp FixedBP) ToDec() sdk.Dec { + return sdk.NewDecWithPrec(int64(bp), 4) +} diff --git a/math/bpmath/fuxed_bp_test.go b/math/bpmath/fuxed_bp_test.go index 3bf1f5a38f..23ca38b430 100644 --- a/math/bpmath/fuxed_bp_test.go +++ b/math/bpmath/fuxed_bp_test.go @@ -72,3 +72,23 @@ func TestFixedMul(t *testing.T) { require.Equal(int64(tc.exp), o.Int64(), fmt.Sprint("test ", tc.name)) } } + +func TestFixedToDec(t *testing.T) { + t.Parallel() + tcs := []struct { + name string + a FixedBP + exp sdk.Dec + }{ + {"t1", 0, sdk.ZeroDec()}, + {"t2", 1, sdk.MustNewDecFromStr("0.0001")}, + {"t3", 20, sdk.MustNewDecFromStr("0.002")}, + {"t4", 9999, sdk.MustNewDecFromStr("0.9999")}, + {"t5", ONE, sdk.NewDec(1)}, + } + require := require.New(t) + for _, tc := range tcs { + o := tc.a.ToDec() + require.Equal(tc.exp.String(), o.String(), fmt.Sprint("test ", tc.name)) + } +} From 7399467f4fd9f587947ddccc6f1dbdabc660ad81 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Thu, 21 Jul 2022 20:30:38 +0200 Subject: [PATCH 07/14] add int32 limited MP operations --- math/bpmath/bp.go | 49 +++++++++++++++ math/bpmath/fixed_bp.go | 28 ++------- .../{fuxed_bp_test.go => fixed_bp_test.go} | 62 +++++++++++-------- x/leverage/keeper/keeper.go | 2 +- 4 files changed, 91 insertions(+), 50 deletions(-) create mode 100644 math/bpmath/bp.go rename math/bpmath/{fuxed_bp_test.go => fixed_bp_test.go} (56%) diff --git a/math/bpmath/bp.go b/math/bpmath/bp.go new file mode 100644 index 0000000000..26a4eeaadd --- /dev/null +++ b/math/bpmath/bp.go @@ -0,0 +1,49 @@ +package bpmath + +import ( + "math" + + sdk "github.com/cosmos/cosmos-sdk/types" +) + +// BP assures represents values in basis points. Maximum value is 2^32-1. +// Note: BP operations should not be chained - this causes precision loses. +type BP uint32 + +// Quo returns a/b in basis points. +// Contract: a>=0 and b > 0. +// Panics if a/b >= MaxUint32/10'000 or if b==0. +func Quo(a, b sdk.Int, rounding Rounding) BP { + return BP(quo(a, b, rounding, math.MaxUint32)) +} + +func quo(a, b sdk.Int, rounding Rounding, max uint64) uint64 { + if b.IsZero() { + panic("divider can't be zero") + } + bp := a.MulRaw(ONE) + if rounding == UP { + bp = bp.Add(b.SubRaw(1)) + } + x := bp.Quo(b).Uint64() + if x > max { + panic("basis points out of band") + } + return x +} + +// Mul returns a * b_in_basis_points +// Contract: b \in [0; MatxUint32] +func Mul(a sdk.Int, b BP) sdk.Int { + if b == 0 { + return sdk.ZeroInt() + } + if b == ONE { + return a + } + return a.MulRaw(int64(b)).Quo(oneBigInt) +} + +func (bp BP) ToDec() sdk.Dec { + return sdk.NewDecWithPrec(int64(bp), 4) +} diff --git a/math/bpmath/fixed_bp.go b/math/bpmath/fixed_bp.go index 908805f39d..649afaee61 100644 --- a/math/bpmath/fixed_bp.go +++ b/math/bpmath/fixed_bp.go @@ -5,32 +5,14 @@ import ( ) // FixedBP assures that all oprations are in 0-10'000 range -// Note FixedBP operations should not be chained - this causes precision loses. +// Note: FixedBP operations should not be chained - this causes precision loses. type FixedBP uint32 -// FixedQuo returns a/b in basis points. Returns 10'000 if a >= b or b==0; -// Contract: a>=0 and b >= 0. +// FixedQuo returns a/b in basis points. Returns 10'000 if a >= b; +// Contract: a>=0 and b > 0. +// Panics if b==0. func FixedQuo(a, b sdk.Int, rounding Rounding) FixedBP { - if a.GTE(b) { - return ONE - } - bp := a.MulRaw(ONE) - if rounding == UP { - bp = bp.Add(b.SubRaw(1)) - } - return FixedBP(bp.Quo(b).Uint64()) -} - -// FixedMul returns a * b_in_basis_points -// Contract: b \in [0; 10000] -func FixedMul(a sdk.Int, b FixedBP) sdk.Int { - if b == 0 { - return sdk.ZeroInt() - } - if b == ONE { - return a - } - return a.MulRaw(int64(b)).Quo(oneBigInt) + return FixedBP(quo(a, b, rounding, ONE)) } func (bp FixedBP) ToDec() sdk.Dec { diff --git a/math/bpmath/fuxed_bp_test.go b/math/bpmath/fixed_bp_test.go similarity index 56% rename from math/bpmath/fuxed_bp_test.go rename to math/bpmath/fixed_bp_test.go index 23ca38b430..dc366a6c57 100644 --- a/math/bpmath/fuxed_bp_test.go +++ b/math/bpmath/fixed_bp_test.go @@ -11,36 +11,43 @@ import ( func TestFixedQuo(t *testing.T) { t.Parallel() tcs := []struct { - name string - a uint64 - b uint64 - r Rounding - exp FixedBP + name string + a uint64 + b uint64 + r Rounding + exp FixedBP + panics bool }{ - {"t1", 0, 0, UP, ONE}, - {"t2", 0, 0, DOWN, ONE}, - {"t3", 1, 0, UP, ONE}, - {"t4", 1, 0, DOWN, ONE}, + {"t1", 0, 0, UP, ONE, true}, + {"t2", 0, 0, DOWN, ONE, true}, + {"t3", 1, 0, UP, ONE, true}, + {"t4", 1, 0, DOWN, ONE, true}, - {"t5", 20, 10, UP, ONE}, - {"t6", 20, 10, DOWN, ONE}, - {"t7", 20, 20, UP, ONE}, - {"t7-1", 20, 20, DOWN, ONE}, + {"t5", 20, 10, UP, 0, true}, + {"t6", 20, 10, DOWN, 0, true}, + {"t7", 20, 20, UP, ONE, false}, + {"t7-1", 20, 20, DOWN, ONE, false}, - {"t8", 1, 2, UP, ONE / 2}, - {"t9", 1, 2, DOWN, ONE / 2}, - {"t10", 1, 3, UP, 3334}, - {"t11", 1, 3, DOWN, 3333}, - {"t12", 2, 3, UP, 6667}, - {"t13", 2, 3, DOWN, 6666}, - {"t14", 10, 99999, UP, 2}, - {"t15", 10, 99999, DOWN, 1}, - {"t16", 10, 99999999, UP, 1}, - {"t17", 10, 99999999, DOWN, 0}, + {"t8", 1, 2, UP, ONE / 2, false}, + {"t9", 1, 2, DOWN, ONE / 2, false}, + {"t10", 1, 3, UP, 3334, false}, + {"t11", 1, 3, DOWN, 3333, false}, + {"t12", 2, 3, UP, 6667, false}, + {"t13", 2, 3, DOWN, 6666, false}, + {"t14", 10, 99999, UP, 2, false}, + {"t15", 10, 99999, DOWN, 1, false}, + {"t16", 10, 99999999, UP, 1, false}, + {"t17", 10, 99999999, DOWN, 0, false}, } require := require.New(t) for _, tc := range tcs { a, b := sdk.NewIntFromUint64(tc.a), sdk.NewIntFromUint64(tc.b) + if tc.panics { + require.Panics(func() { + FixedQuo(a, b, tc.r) + }, tc.name) + continue + } o := FixedQuo(a, b, tc.r) require.Equal(int(tc.exp), int(o), fmt.Sprint("test ", tc.name)) } @@ -51,7 +58,7 @@ func TestFixedMul(t *testing.T) { tcs := []struct { name string a uint64 - b FixedBP + b BP exp uint64 }{ {"t1", 20, 0, 0}, @@ -68,7 +75,7 @@ func TestFixedMul(t *testing.T) { require := require.New(t) for _, tc := range tcs { a := sdk.NewIntFromUint64(tc.a) - o := FixedMul(a, tc.b) + o := Mul(a, tc.b) require.Equal(int64(tc.exp), o.Int64(), fmt.Sprint("test ", tc.name)) } } @@ -89,6 +96,9 @@ func TestFixedToDec(t *testing.T) { require := require.New(t) for _, tc := range tcs { o := tc.a.ToDec() - require.Equal(tc.exp.String(), o.String(), fmt.Sprint("test ", tc.name)) + require.Equal(tc.exp.String(), o.String(), fmt.Sprint("test-fixedbp ", tc.name)) + + bp := BP(tc.a).ToDec() + require.Equal(tc.exp.String(), bp.String(), fmt.Sprint("test-bp ", tc.name)) } } diff --git a/x/leverage/keeper/keeper.go b/x/leverage/keeper/keeper.go index 7b2c600bc5..7bf8a68e11 100644 --- a/x/leverage/keeper/keeper.go +++ b/x/leverage/keeper/keeper.go @@ -433,7 +433,7 @@ func (k Keeper) LiquidateBorrow( } // apply liquidation incentive - reward.Amount = bpmath.FixedMul(reward.Amount, liquidationIncentive).Add(reward.Amount) + reward.Amount = bpmath.Mul(reward.Amount, liquidationIncentive).Add(reward.Amount) maxReward := collateral.AmountOf(reward.Denom) if maxReward.IsZero() { From 32543f57b1065f8957440d7314b3775d053a35d6 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 22 Jul 2022 15:55:17 +0200 Subject: [PATCH 08/14] proto: convert max_borrow_rate to basis points --- proto/umee/leverage/v1/leverage.proto | 8 +- x/leverage/types/leverage.pb.go | 150 ++++++++++++-------------- 2 files changed, 70 insertions(+), 88 deletions(-) diff --git a/proto/umee/leverage/v1/leverage.proto b/proto/umee/leverage/v1/leverage.proto index 987aad0c51..f10efc69aa 100644 --- a/proto/umee/leverage/v1/leverage.proto +++ b/proto/umee/leverage/v1/leverage.proto @@ -95,10 +95,10 @@ message Token { // The max_borrow_rate defines the interest rate for borrowing this // asset (seen when supply utilization is 100%). - string max_borrow_rate = 7 [ - (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec", - (gogoproto.nullable) = false, - (gogoproto.moretags) = "yaml:\"max_borrow_rate\"" + // Value is in basis points; allowed values are in [0, 100'000'000] range. + uint32 max_borrow_rate = 7 [ + (gogoproto.casttype) = "github.com/umee-network/umee/v2/math/bpmath.FixedBP", + (gogoproto.moretags) = "yaml:\"max_borrow_rate\"" ]; // The kink_utilization defines the value where the kink rate kicks off for diff --git a/x/leverage/types/leverage.pb.go b/x/leverage/types/leverage.pb.go index d398842988..f0ef6c7d4d 100644 --- a/x/leverage/types/leverage.pb.go +++ b/x/leverage/types/leverage.pb.go @@ -100,7 +100,8 @@ type Token struct { KinkBorrowRate github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,6,opt,name=kink_borrow_rate,json=kinkBorrowRate,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"kink_borrow_rate" yaml:"kink_borrow_rate"` // The max_borrow_rate defines the interest rate for borrowing this // asset (seen when supply utilization is 100%). - MaxBorrowRate github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,7,opt,name=max_borrow_rate,json=maxBorrowRate,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"max_borrow_rate" yaml:"max_borrow_rate"` + // Value is in basis points; allowed values are in [0, 100'000'000] range. + MaxBorrowRate github_com_umee_network_umee_v2_math_bpmath.FixedBP `protobuf:"varint,7,opt,name=max_borrow_rate,json=maxBorrowRate,proto3,casttype=github.com/umee-network/umee/v2/math/bpmath.FixedBP" json:"max_borrow_rate,omitempty" yaml:"max_borrow_rate"` // The kink_utilization defines the value where the kink rate kicks off for // borrow rates. KinkUtilization github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,8,opt,name=kink_utilization,json=kinkUtilization,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"kink_utilization" yaml:"kink_utilization"` @@ -177,59 +178,59 @@ func init() { func init() { proto.RegisterFile("umee/leverage/v1/leverage.proto", fileDescriptor_8cb1bf9ea641ecc6) } var fileDescriptor_8cb1bf9ea641ecc6 = []byte{ - // 823 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x56, 0xcf, 0x73, 0x1b, 0x35, - 0x14, 0xf6, 0x42, 0x1a, 0x12, 0xa5, 0x89, 0x93, 0xad, 0xd3, 0xee, 0x40, 0xf0, 0x66, 0x34, 0x03, - 0x93, 0x4b, 0xbd, 0x94, 0x72, 0xca, 0xd1, 0xed, 0x94, 0x96, 0xa1, 0xa5, 0x28, 0x65, 0x32, 0xc3, - 0x65, 0x47, 0x5e, 0x3f, 0xec, 0x1d, 0x4b, 0xab, 0x45, 0x92, 0x7f, 0x24, 0x17, 0x0e, 0x4c, 0x6f, - 0x1c, 0x38, 0x72, 0x61, 0xa6, 0x7f, 0x06, 0x7f, 0x42, 0x8e, 0x3d, 0x32, 0x1c, 0x3c, 0x90, 0x5c, - 0x38, 0xe7, 0xc8, 0x89, 0x91, 0xb4, 0xb6, 0xd7, 0xc1, 0x94, 0xf1, 0x84, 0x93, 0xa5, 0xef, 0x3d, - 0x7f, 0xdf, 0x27, 0xbd, 0x27, 0x69, 0x51, 0xd8, 0xe7, 0x00, 0x11, 0x83, 0x01, 0x48, 0xda, 0x81, - 0x68, 0x70, 0x6f, 0x3a, 0x6e, 0xe4, 0x52, 0x68, 0xe1, 0x6f, 0x9b, 0x84, 0xc6, 0x14, 0x1c, 0xdc, - 0x7b, 0xb7, 0xd6, 0x11, 0x1d, 0x61, 0x83, 0x91, 0x19, 0xb9, 0x3c, 0xfc, 0xcb, 0x0a, 0x5a, 0x7d, - 0x4e, 0x25, 0xe5, 0xca, 0xff, 0xd9, 0x43, 0xf5, 0x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0xc2, 0xe4, 0xaa, 0x1c, 0x6c, - 0xbe, 0x81, 0xc9, 0xa5, 0x94, 0x99, 0x5c, 0x27, 0xf8, 0x7b, 0x68, 0xbd, 0xc5, 0x68, 0xd2, 0x63, - 0xa9, 0xd2, 0xc1, 0x96, 0x61, 0x20, 0x33, 0xc0, 0xff, 0x12, 0xd5, 0x4c, 0x1b, 0x95, 0x6e, 0x0c, - 0xd5, 0xa5, 0x12, 0x82, 0xaa, 0x5d, 0x6e, 0x58, 0x7a, 0x90, 0x16, 0x64, 0x99, 0x07, 0x89, 0x8e, - 0x1e, 0x4c, 0xd1, 0x23, 0x03, 0x1e, 0xae, 0xfc, 0xf9, 0x2a, 0xf4, 0x9a, 0xcf, 0xce, 0xfe, 0xa8, - 0x57, 0xce, 0xce, 0xeb, 0xde, 0xeb, 0xf3, 0xba, 0xf7, 0xfb, 0x79, 0xdd, 0xfb, 0xf1, 0xa2, 0x5e, - 0x79, 0x7d, 0x51, 0xaf, 0xfc, 0x7a, 0x51, 0xaf, 0x7c, 0xfd, 0xd1, 0x7f, 0x15, 0x78, 0x34, 0xfb, - 0x8c, 0xb0, 0x6d, 0xd8, 0x5a, 0xb5, 0x5f, 0x06, 0xf7, 0xff, 0x0e, 0x00, 0x00, 0xff, 0xff, 0xd4, - 0x38, 0x9e, 0x8a, 0x64, 0x08, 0x00, 0x00, + // 826 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x56, 0xcf, 0x6f, 0x1b, 0x45, + 0x14, 0xf6, 0x42, 0x1a, 0x92, 0x49, 0x13, 0x27, 0x5b, 0xa7, 0x5d, 0x41, 0xf0, 0x46, 0x23, 0x81, + 0x72, 0xa9, 0x97, 0x52, 0x4e, 0x39, 0xba, 0x55, 0xa1, 0x88, 0x96, 0x76, 0x52, 0x14, 0x89, 0xcb, + 0x6a, 0xbc, 0x7e, 0xd8, 0x2b, 0xcf, 0xec,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f, + 0x5e, 0x56, 0x4b, 0x2f, 0x2e, 0xab, 0xa5, 0x5f, 0x2f, 0xab, 0xa5, 0x2f, 0x3f, 0xf8, 0xaf, 0x01, + 0x0f, 0xe7, 0xdf, 0x13, 0xd6, 0x86, 0x8d, 0x75, 0xfb, 0x89, 0x70, 0xf7, 0xef, 0x00, 0x00, 0x00, + 0xff, 0xff, 0xeb, 0x68, 0xe7, 0x03, 0x6d, 0x08, 0x00, 0x00, } func (this *Token) Equal(that interface{}) bool { @@ -269,7 +270,7 @@ func (this *Token) Equal(that interface{}) bool { if !this.KinkBorrowRate.Equal(that1.KinkBorrowRate) { return false } - if !this.MaxBorrowRate.Equal(that1.MaxBorrowRate) { + if this.MaxBorrowRate != that1.MaxBorrowRate { return false } if !this.KinkUtilization.Equal(that1.KinkUtilization) { @@ -443,16 +444,11 @@ func (m *Token) MarshalToSizedBuffer(dAtA []byte) (int, error) { } i-- dAtA[i] = 0x42 - { - size := m.MaxBorrowRate.Size() - i -= size - if _, err := m.MaxBorrowRate.MarshalTo(dAtA[i:]); err != nil { - return 0, err - } - i = encodeVarintLeverage(dAtA, i, uint64(size)) + if m.MaxBorrowRate != 0 { + i = encodeVarintLeverage(dAtA, i, uint64(m.MaxBorrowRate)) + i-- + dAtA[i] = 0x38 } - i-- - dAtA[i] = 0x3a { size := m.KinkBorrowRate.Size() i -= size @@ -561,8 +557,9 @@ func (m *Token) Size() (n int) { n += 1 + l + sovLeverage(uint64(l)) l = m.KinkBorrowRate.Size() n += 1 + l + sovLeverage(uint64(l)) - l = m.MaxBorrowRate.Size() - n += 1 + l + sovLeverage(uint64(l)) + if m.MaxBorrowRate != 0 { + n += 1 + sovLeverage(uint64(m.MaxBorrowRate)) + } l = m.KinkUtilization.Size() n += 1 + l + sovLeverage(uint64(l)) if m.LiquidationIncentive != 0 { @@ -1014,10 +1011,10 @@ func (m *Token) Unmarshal(dAtA []byte) error { } iNdEx = postIndex case 7: - if wireType != 2 { + if wireType != 0 { return fmt.Errorf("proto: wrong wireType = %d for field MaxBorrowRate", wireType) } - var stringLen uint64 + m.MaxBorrowRate = 0 for shift := uint(0); ; shift += 7 { if shift >= 64 { return ErrIntOverflowLeverage @@ -1027,26 +1024,11 @@ func (m *Token) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - stringLen |= uint64(b&0x7F) << shift + m.MaxBorrowRate |= github_com_umee_network_umee_v2_math_bpmath.FixedBP(b&0x7F) << shift if b < 0x80 { break } } - intStringLen := int(stringLen) - if intStringLen < 0 { - return ErrInvalidLengthLeverage - } - postIndex := iNdEx + intStringLen - if postIndex < 0 { - return ErrInvalidLengthLeverage - } - if postIndex > l { - return io.ErrUnexpectedEOF - } - if err := m.MaxBorrowRate.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { - return err - } - iNdEx = postIndex case 8: if wireType != 2 { return fmt.Errorf("proto: wrong wireType = %d for field KinkUtilization", wireType) From ed614a3096a11f667d70064a3b5f26787b2801e4 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 22 Jul 2022 16:56:10 +0200 Subject: [PATCH 09/14] more tests --- math/bpmath/bp_test.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 math/bpmath/bp_test.go diff --git a/math/bpmath/bp_test.go b/math/bpmath/bp_test.go new file mode 100644 index 0000000000..0983def932 --- /dev/null +++ b/math/bpmath/bp_test.go @@ -0,0 +1,26 @@ +package bpmath + +import ( + "fmt" + "testing" + + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/stretchr/testify/require" +) + +func TestBPToDec(t *testing.T) { + t.Parallel() + tcs := []struct { + name string + a FixedBP + exp sdk.Dec + }{ + {"t1", 99999, sdk.MustNewDecFromStr("9.9999")}, + {"t2", ONE * 10, sdk.MustNewDecFromStr("10.0")}, + } + require := require.New(t) + for _, tc := range tcs { + bp := BP(tc.a).ToDec() + require.Equal(tc.exp.String(), bp.String(), fmt.Sprint("test-bp ", tc.name)) + } +} From 859ec5dd8207ddcf980cdc34e6032738241062f3 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 22 Jul 2022 17:01:36 +0200 Subject: [PATCH 10/14] use generics for Mul --- math/bpmath/bp.go | 2 +- math/bpmath/fixed_bp_test.go | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/math/bpmath/bp.go b/math/bpmath/bp.go index 26a4eeaadd..1878864c7e 100644 --- a/math/bpmath/bp.go +++ b/math/bpmath/bp.go @@ -34,7 +34,7 @@ func quo(a, b sdk.Int, rounding Rounding, max uint64) uint64 { // Mul returns a * b_in_basis_points // Contract: b \in [0; MatxUint32] -func Mul(a sdk.Int, b BP) sdk.Int { +func Mul[T BP | FixedBP](a sdk.Int, b T) sdk.Int { if b == 0 { return sdk.ZeroInt() } diff --git a/math/bpmath/fixed_bp_test.go b/math/bpmath/fixed_bp_test.go index dc366a6c57..1ea901c66d 100644 --- a/math/bpmath/fixed_bp_test.go +++ b/math/bpmath/fixed_bp_test.go @@ -77,6 +77,11 @@ func TestFixedMul(t *testing.T) { a := sdk.NewIntFromUint64(tc.a) o := Mul(a, tc.b) require.Equal(int64(tc.exp), o.Int64(), fmt.Sprint("test ", tc.name)) + + // must work with both FixedBP and BP + o = Mul(a, FixedBP(tc.b)) + require.Equal(int64(tc.exp), o.Int64(), fmt.Sprint("test ", tc.name)) + } } From 19e6996965d406439ac41a0365f59b7daf6765c3 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 22 Jul 2022 17:04:04 +0200 Subject: [PATCH 11/14] update module implementation for MaxBorrowRate --- app/test_helpers.go | 2 +- x/leverage/keeper/interest.go | 10 +++++----- x/leverage/types/token.go | 9 +++++---- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/app/test_helpers.go b/app/test_helpers.go index bb6e41b6a0..800c73b0f7 100644 --- a/app/test_helpers.go +++ b/app/test_helpers.go @@ -115,7 +115,7 @@ func IntegrationTestNetworkConfig() network.Config { LiquidationThreshold: sdk.MustNewDecFromStr("0.050000000000000000"), BaseBorrowRate: sdk.MustNewDecFromStr("0.020000000000000000"), KinkBorrowRate: sdk.MustNewDecFromStr("0.200000000000000000"), - MaxBorrowRate: sdk.MustNewDecFromStr("1.50000000000000000"), + MaxBorrowRate: 15000, // 1.5 KinkUtilization: sdk.MustNewDecFromStr("0.200000000000000000"), LiquidationIncentive: 1800, EnableMsgSupply: true, diff --git a/x/leverage/keeper/interest.go b/x/leverage/keeper/interest.go index ee3eacf483..9687c04ae5 100644 --- a/x/leverage/keeper/interest.go +++ b/x/leverage/keeper/interest.go @@ -27,11 +27,11 @@ func (k Keeper) DeriveBorrowAPY(ctx sdk.Context, denom string) sdk.Dec { if utilization.GTE(token.KinkUtilization) { return Interpolate( - utilization, // x - token.KinkUtilization, // x1 - token.KinkBorrowRate, // y1 - sdk.OneDec(), // x2 - token.MaxBorrowRate, // y2 + utilization, // x + token.KinkUtilization, // x1 + token.KinkBorrowRate, // y1 + sdk.OneDec(), // x2 + token.MaxBorrowRate.ToDec(), // y2 ) } diff --git a/x/leverage/types/token.go b/x/leverage/types/token.go index b5782d3636..66a1d833f7 100644 --- a/x/leverage/types/token.go +++ b/x/leverage/types/token.go @@ -11,7 +11,8 @@ import ( const ( // UTokenPrefix defines the uToken denomination prefix for all uToken types. - UTokenPrefix = "u/" + UTokenPrefix = "u/" + maxBorrowRateLimit = bpmath.ONE * 10_000 ) // UTokenFromTokenDenom returns the uToken denom given a token denom. @@ -66,13 +67,13 @@ func (t Token) Validate() error { if t.KinkBorrowRate.IsNegative() { return fmt.Errorf("invalid kink borrow rate: %s", t.KinkBorrowRate) } - if t.MaxBorrowRate.IsNegative() { - return fmt.Errorf("invalid max borrow rate: %s", t.MaxBorrowRate) + if t.MaxBorrowRate > maxBorrowRateLimit { + return fmt.Errorf("invalid max borrow rate: %d - must be in [0; %d] (value in basis points)", t.LiquidationIncentive, maxBorrowRateLimit) } // Liquidation incentive is non-negative if t.LiquidationIncentive > bpmath.ONE { - return fmt.Errorf("invalid liquidation incentive: %s - must be in [0; 10000]", t.LiquidationIncentive) + return fmt.Errorf("invalid liquidation incentive: %d - must be in [0; 10000]", t.LiquidationIncentive) } // Blacklisted assets cannot have borrow or supply enabled From 6f60f9348c70c46d3a29c0267175e1b8aeebafc8 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 22 Jul 2022 17:16:41 +0200 Subject: [PATCH 12/14] proto: fix cast type --- proto/umee/leverage/v1/leverage.proto | 2 +- x/leverage/types/leverage.pb.go | 106 +++++++++++++------------- 2 files changed, 54 insertions(+), 54 deletions(-) diff --git a/proto/umee/leverage/v1/leverage.proto b/proto/umee/leverage/v1/leverage.proto index f10efc69aa..8b5475f32e 100644 --- a/proto/umee/leverage/v1/leverage.proto +++ b/proto/umee/leverage/v1/leverage.proto @@ -97,7 +97,7 @@ message Token { // asset (seen when supply utilization is 100%). // Value is in basis points; allowed values are in [0, 100'000'000] range. uint32 max_borrow_rate = 7 [ - (gogoproto.casttype) = "github.com/umee-network/umee/v2/math/bpmath.FixedBP", + (gogoproto.casttype) = "github.com/umee-network/umee/v2/math/bpmath.BP", (gogoproto.moretags) = "yaml:\"max_borrow_rate\"" ]; diff --git a/x/leverage/types/leverage.pb.go b/x/leverage/types/leverage.pb.go index f0ef6c7d4d..a134521a98 100644 --- a/x/leverage/types/leverage.pb.go +++ b/x/leverage/types/leverage.pb.go @@ -101,7 +101,7 @@ type Token struct { // The max_borrow_rate defines the interest rate for borrowing this // asset (seen when supply utilization is 100%). // Value is in basis points; allowed values are in [0, 100'000'000] range. - MaxBorrowRate github_com_umee_network_umee_v2_math_bpmath.FixedBP `protobuf:"varint,7,opt,name=max_borrow_rate,json=maxBorrowRate,proto3,casttype=github.com/umee-network/umee/v2/math/bpmath.FixedBP" json:"max_borrow_rate,omitempty" yaml:"max_borrow_rate"` + MaxBorrowRate github_com_umee_network_umee_v2_math_bpmath.BP `protobuf:"varint,7,opt,name=max_borrow_rate,json=maxBorrowRate,proto3,casttype=github.com/umee-network/umee/v2/math/bpmath.BP" json:"max_borrow_rate,omitempty" yaml:"max_borrow_rate"` // The kink_utilization defines the value where the kink rate kicks off for // borrow rates. KinkUtilization github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,8,opt,name=kink_utilization,json=kinkUtilization,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"kink_utilization" yaml:"kink_utilization"` @@ -178,59 +178,59 @@ func init() { func init() { proto.RegisterFile("umee/leverage/v1/leverage.proto", fileDescriptor_8cb1bf9ea641ecc6) } var fileDescriptor_8cb1bf9ea641ecc6 = []byte{ - // 826 bytes of a gzipped FileDescriptorProto + // 830 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xac, 0x56, 0xcf, 0x6f, 0x1b, 0x45, 0x14, 0xf6, 0x42, 0x1a, 0x92, 0x49, 0x13, 0x27, 0x5b, 0xa7, 0x5d, 0x41, 0xf0, 0x46, 0x23, 0x81, - 0x72, 0xa9, 0x97, 0x52, 0x4e, 0x39, 0xba, 0x55, 0xa1, 0x88, 0x96, 0x76, 0x52, 0x14, 0x89, 0xcb, - 0x6a, 0xbc, 0x7e, 0xd8, 0x2b, 0xcf, 0xec, 0x98, 0x99, 0xf1, 0xaf, 0x5c, 0x38, 0x20, 0x6e, 0x08, - 0x71, 0xe4, 0x82, 0xd4, 0x3f, 0x83, 0x3f, 0x21, 0xc7, 0x1e, 0x11, 0x07, 0x0b, 0x92, 0x0b, 0x67, - 0x1f, 0x39, 0xa1, 0x99, 0x59, 0xdb, 0xeb, 0x60, 0x8a, 0x4c, 0x7b, 0xf2, 0xce, 0xf7, 0x9e, 0xbf, - 0xf7, 0xbd, 0x79, 0xdf, 0xcc, 0x2e, 0x0a, 0x7b, 0x1c, 0x20, 0x62, 0xd0, 0x07, 0x49, 0x5b, 0x10, - 0xf5, 0xef, 0xcc, 0x9e, 0x6b, 0x5d, 0x29, 0xb4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, 0x85, 0xa7, + 0x59, 0xca, 0x7b, 0x3c, 0x4e, 0x98, 0x50, 0x10, 0x7f, 0x45, 0x13, 0x2d, 0x64, 0xf0, 0xa6, 0x15, + 0xf5, 0x74, 0x65, 0x51, 0xef, 0x38, 0x51, 0xcb, 0x38, 0x31, 0xf1, 0x73, 0xf8, 0x81, 0x41, 0x1f, + 0x59, 0xd0, 0x08, 0x10, 0x92, 0x26, 0x0c, 0x62, 0x09, 0x03, 0x2a, 0x9b, 0x13, 0x01, 0x6b, 0xaf, + 0x27, 0x60, 0x19, 0x27, 0x26, 0xbe, 0x83, 0x89, 0x45, 0x73, 0x01, 0xdf, 0x79, 0xe8, 0xb6, 0xe2, + 0x94, 0xb1, 0xb9, 0x05, 0x54, 0xe9, 0x05, 0x04, 0x37, 0xac, 0x86, 0xcf, 0x56, 0xd6, 0xf0, 0xae, + 0xd3, 0xb0, 0x9c, 0x15, 0x93, 0x8a, 0x0d, 0x14, 0xb6, 0xe3, 0x34, 0xbd, 0x80, 0xe3, 0xb5, 0x9f, + 0x5e, 0x84, 0x25, 0xfc, 0xfd, 0x16, 0xba, 0xf1, 0x5c, 0x74, 0x20, 0xf3, 0x3f, 0x42, 0xa8, 0x41, + 0x15, 0xc4, 0x4d, 0xc8, 0x04, 0x0f, 0x3c, 0x2b, 0x65, 0x7f, 0x3c, 0x0a, 0xf7, 0x1c, 0xf9, 0x2c, + 0x86, 0xc9, 0xa6, 0x99, 0x3c, 0x34, 0x63, 0x3f, 0x43, 0x3b, 0x12, 0x14, 0xc8, 0xfe, 0x74, 0x27, + 0x9d, 0xbd, 0x3e, 0x5e, 0xb9, 0x89, 0x7d, 0x57, 0x67, 0x9e, 0x0d, 0x93, 0xed, 0x1c, 0xc8, 0x57, + 0x6f, 0x80, 0xf6, 0x12, 0xc1, 0x18, 0xd5, 0x20, 0x29, 0x8b, 0x07, 0x90, 0xb6, 0xda, 0x3a, 0x37, + 0xcf, 0xa7, 0x2b, 0x97, 0x0c, 0x26, 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, 0x57, 0x55, 0xef, 0xc7, 0xeb, 0x6a, 0xe9, 0xe5, 0x75, 0xb5, 0xf4, 0xeb, 0x75, 0xb5, 0xf4, + 0xe5, 0x07, 0xff, 0xb5, 0xc1, 0xc3, 0xd9, 0xaf, 0x84, 0xb5, 0x61, 0x63, 0xdd, 0xfe, 0x1d, 0xdc, + 0xff, 0x3b, 0x00, 0x00, 0xff, 0xff, 0xce, 0xd3, 0xd0, 0x6a, 0x68, 0x08, 0x00, 0x00, } func (this *Token) Equal(that interface{}) bool { @@ -1024,7 +1024,7 @@ func (m *Token) Unmarshal(dAtA []byte) error { } b := dAtA[iNdEx] iNdEx++ - m.MaxBorrowRate |= github_com_umee_network_umee_v2_math_bpmath.FixedBP(b&0x7F) << shift + m.MaxBorrowRate |= github_com_umee_network_umee_v2_math_bpmath.BP(b&0x7F) << shift if b < 0x80 { break } From a0c8e744b88854368e162cef6d713056d94de279 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Fri, 22 Jul 2022 17:19:31 +0200 Subject: [PATCH 13/14] update x/leverage tests --- x/leverage/client/cli/proposal_test.go | 4 ++-- x/leverage/client/tests/tests.go | 4 ++-- x/leverage/gov_handler_test.go | 4 ++-- x/leverage/keeper/keeper_test.go | 4 ++-- x/leverage/simulation/operations_test.go | 12 ++++++------ x/leverage/types/token_test.go | 17 +++++++++-------- 6 files changed, 23 insertions(+), 22 deletions(-) diff --git a/x/leverage/client/cli/proposal_test.go b/x/leverage/client/cli/proposal_test.go index f3211cbb45..4696902ede 100644 --- a/x/leverage/client/cli/proposal_test.go +++ b/x/leverage/client/cli/proposal_test.go @@ -38,9 +38,9 @@ func TestParseUpdateRegistryProposal(t *testing.T) { "liquidation_threshold": "0.05", "base_borrow_rate": "0.02", "kink_borrow_rate": "0.2", - "max_borrow_rate": "1.5", + "max_borrow_rate": 15000, "kink_utilization": "0.2", - "liquidation_incentive": "0.1", + "liquidation_incentive": 1000, "symbol_denom": "UMEE", "exponent": 6, "enable_msg_supply": true, diff --git a/x/leverage/client/tests/tests.go b/x/leverage/client/tests/tests.go index 9dbf8f6470..b4161dbf3b 100644 --- a/x/leverage/client/tests/tests.go +++ b/x/leverage/client/tests/tests.go @@ -268,9 +268,9 @@ func (s *IntegrationTestSuite) TestLeverageScenario() { LiquidationThreshold: sdk.MustNewDecFromStr("0.05"), BaseBorrowRate: sdk.MustNewDecFromStr("0.02"), KinkBorrowRate: sdk.MustNewDecFromStr("0.2"), - MaxBorrowRate: sdk.MustNewDecFromStr("1.5"), + MaxBorrowRate: 15000, KinkUtilization: sdk.MustNewDecFromStr("0.2"), - LiquidationIncentive: sdk.MustNewDecFromStr("0.18"), + LiquidationIncentive: 180, EnableMsgSupply: true, EnableMsgBorrow: true, Blacklist: false, diff --git a/x/leverage/gov_handler_test.go b/x/leverage/gov_handler_test.go index 5084dfc3ad..af63f3d6af 100644 --- a/x/leverage/gov_handler_test.go +++ b/x/leverage/gov_handler_test.go @@ -25,9 +25,9 @@ func newTestToken(base, symbol, reserveFactor string) types.Token { LiquidationThreshold: sdk.MustNewDecFromStr("0.25"), BaseBorrowRate: sdk.MustNewDecFromStr("0.02"), KinkBorrowRate: sdk.MustNewDecFromStr("0.22"), - MaxBorrowRate: sdk.MustNewDecFromStr("1.52"), + MaxBorrowRate: 15200, KinkUtilization: sdk.MustNewDecFromStr("0.8"), - LiquidationIncentive: sdk.MustNewDecFromStr("0.1"), + LiquidationIncentive: 100, EnableMsgSupply: true, EnableMsgBorrow: true, Blacklist: false, diff --git a/x/leverage/keeper/keeper_test.go b/x/leverage/keeper/keeper_test.go index c287c85515..e4fc25b7e9 100644 --- a/x/leverage/keeper/keeper_test.go +++ b/x/leverage/keeper/keeper_test.go @@ -42,7 +42,7 @@ func newToken(base, symbol string) types.Token { LiquidationThreshold: sdk.MustNewDecFromStr("0.25"), BaseBorrowRate: sdk.MustNewDecFromStr("0.02"), KinkBorrowRate: sdk.MustNewDecFromStr("0.22"), - MaxBorrowRate: sdk.MustNewDecFromStr("1.52"), + MaxBorrowRate: 15200, KinkUtilization: sdk.MustNewDecFromStr("0.8"), LiquidationIncentive: 1000, EnableMsgSupply: true, @@ -256,7 +256,7 @@ func (s *IntegrationTestSuite) TestGetToken() { s.Require().Equal(t.LiquidationThreshold, sdk.MustNewDecFromStr("0.25")) s.Require().Equal(t.BaseBorrowRate, sdk.MustNewDecFromStr("0.02")) s.Require().Equal(t.KinkBorrowRate, sdk.MustNewDecFromStr("0.22")) - s.Require().Equal(t.MaxBorrowRate, sdk.MustNewDecFromStr("1.52")) + s.Require().Equal(t.MaxBorrowRate, bpmath.BP(15200)) s.Require().Equal(t.KinkUtilization, sdk.MustNewDecFromStr("0.8")) s.Require().Equal(t.LiquidationIncentive, bpmath.FixedBP(1000)) diff --git a/x/leverage/simulation/operations_test.go b/x/leverage/simulation/operations_test.go index 657295ac70..1f6777caef 100644 --- a/x/leverage/simulation/operations_test.go +++ b/x/leverage/simulation/operations_test.go @@ -38,9 +38,9 @@ func (s *SimTestSuite) SetupTest() { LiquidationThreshold: sdk.MustNewDecFromStr("0.5"), BaseBorrowRate: sdk.MustNewDecFromStr("0.02"), KinkBorrowRate: sdk.MustNewDecFromStr("0.2"), - MaxBorrowRate: sdk.MustNewDecFromStr("1.0"), + MaxBorrowRate: 10000, KinkUtilization: sdk.MustNewDecFromStr("0.8"), - LiquidationIncentive: sdk.MustNewDecFromStr("0.1"), + LiquidationIncentive: 1000, SymbolDenom: umeeapp.DisplayDenom, Exponent: 6, EnableMsgSupply: true, @@ -54,9 +54,9 @@ func (s *SimTestSuite) SetupTest() { LiquidationThreshold: sdk.MustNewDecFromStr("0.8"), BaseBorrowRate: sdk.MustNewDecFromStr("0.05"), KinkBorrowRate: sdk.MustNewDecFromStr("0.3"), - MaxBorrowRate: sdk.MustNewDecFromStr("0.9"), + MaxBorrowRate: 900, KinkUtilization: sdk.MustNewDecFromStr("0.75"), - LiquidationIncentive: sdk.MustNewDecFromStr("0.11"), + LiquidationIncentive: 110, SymbolDenom: "ATOM", Exponent: 6, EnableMsgSupply: true, @@ -70,9 +70,9 @@ func (s *SimTestSuite) SetupTest() { LiquidationThreshold: sdk.MustNewDecFromStr("0.1"), BaseBorrowRate: sdk.MustNewDecFromStr("0.02"), KinkBorrowRate: sdk.MustNewDecFromStr("0.22"), - MaxBorrowRate: sdk.MustNewDecFromStr("1.52"), + MaxBorrowRate: 15200, KinkUtilization: sdk.MustNewDecFromStr("0.87"), - LiquidationIncentive: sdk.MustNewDecFromStr("0.1"), + LiquidationIncentive: 1000, SymbolDenom: "ABC", Exponent: 6, EnableMsgSupply: true, diff --git a/x/leverage/types/token_test.go b/x/leverage/types/token_test.go index 1d6055c320..776e3dbabb 100644 --- a/x/leverage/types/token_test.go +++ b/x/leverage/types/token_test.go @@ -6,6 +6,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/stretchr/testify/require" + "github.com/umee-network/umee/v2/math/bpmath" "github.com/umee-network/umee/v2/x/leverage/types" ) @@ -30,9 +31,9 @@ func TestUpdateRegistryProposal_String(t *testing.T) { LiquidationThreshold: sdk.NewDec(66), BaseBorrowRate: sdk.NewDec(32), KinkBorrowRate: sdk.NewDec(26), - MaxBorrowRate: sdk.NewDec(21), + MaxBorrowRate: 21 * bpmath.ONE, KinkUtilization: sdk.MustNewDecFromStr("0.25"), - LiquidationIncentive: sdk.NewDec(88), + LiquidationIncentive: 880, EnableMsgSupply: true, EnableMsgBorrow: true, Blacklist: false, @@ -49,9 +50,9 @@ registry: liquidation_threshold: "66.000000000000000000" base_borrow_rate: "32.000000000000000000" kink_borrow_rate: "26.000000000000000000" - max_borrow_rate: "21.000000000000000000" + max_borrow_rate: 210000 kink_utilization: "0.250000000000000000" - liquidation_incentive: "88.000000000000000000" + liquidation_incentive: 880 symbol_denom: umee exponent: 6 enable_msg_supply: true @@ -73,9 +74,9 @@ func TestToken_Validate(t *testing.T) { LiquidationThreshold: sdk.MustNewDecFromStr("0.50"), BaseBorrowRate: sdk.MustNewDecFromStr("0.01"), KinkBorrowRate: sdk.MustNewDecFromStr("0.05"), - MaxBorrowRate: sdk.MustNewDecFromStr("1.0"), + MaxBorrowRate: 10000, KinkUtilization: sdk.MustNewDecFromStr("0.75"), - LiquidationIncentive: sdk.MustNewDecFromStr("0.05"), + LiquidationIncentive: 500, EnableMsgSupply: true, EnableMsgBorrow: true, Blacklist: false, @@ -106,13 +107,13 @@ func TestToken_Validate(t *testing.T) { invalidKinkBorrowRate.KinkBorrowRate = sdk.MustNewDecFromStr("-0.05") invalidMaxBorrowRate := validToken() - invalidMaxBorrowRate.MaxBorrowRate = sdk.MustNewDecFromStr("-1.0") + invalidMaxBorrowRate.MaxBorrowRate = bpmath.ONE * 20_000 invalidKinkUtilization := validToken() invalidKinkUtilization.KinkUtilization = sdk.ZeroDec() invalidLiquidationIncentive := validToken() - invalidLiquidationIncentive.LiquidationIncentive = sdk.MustNewDecFromStr("-0.05") + invalidLiquidationIncentive.LiquidationIncentive = bpmath.ONE + 1 invalidBlacklistedBorrow := validToken() invalidBlacklistedBorrow.EnableMsgBorrow = false From 45db29b570ce4f3114d1bc5272d7fef7313099a0 Mon Sep 17 00:00:00 2001 From: Robert Zaremba Date: Wed, 4 Jan 2023 16:48:07 +0100 Subject: [PATCH 14/14] use _ in numbers --- app/test_helpers.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/test_helpers.go b/app/test_helpers.go index 97b3845334..0f6ec8b51b 100644 --- a/app/test_helpers.go +++ b/app/test_helpers.go @@ -115,7 +115,7 @@ func IntegrationTestNetworkConfig() network.Config { LiquidationThreshold: sdk.MustNewDecFromStr("0.05"), BaseBorrowRate: sdk.MustNewDecFromStr("0.02"), KinkBorrowRate: sdk.MustNewDecFromStr("0.2"), - MaxBorrowRate: 15000, + MaxBorrowRate: 1_5000, KinkUtilization: sdk.MustNewDecFromStr("0.2"), LiquidationIncentive: 1800, EnableMsgSupply: true,