Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Check some test fail & fix #141

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/apptesting/test_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package apptesting
import (
"time"

"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
Expand Down Expand Up @@ -103,7 +104,7 @@ func (s *KeeperTestHelper) EndBlock() {
s.App.EndBlocker(s.Ctx, reqEndBlock)
}

func (s *KeeperTestHelper) AllocateRewardsToValidator(valAddr sdk.ValAddress, rewardAmt sdk.Int) {
func (s *KeeperTestHelper) AllocateRewardsToValidator(valAddr sdk.ValAddress, rewardAmt math.Int) {
validator, found := s.App.StakingKeeper.GetValidator(s.Ctx, valAddr)
s.Require().True(found)

Expand Down
4 changes: 2 additions & 2 deletions utils/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func decodeImpl(data []byte, v interface{}) ([]byte, error) {
return rem, err
case reflect.Uint64:
val, rem, err := DecodeUnsigned64(data)
ev.SetUint(uint64(val))
ev.SetUint(val)
return rem, err
case reflect.Int8:
val, rem, err := DecodeSigned8(data)
Expand All @@ -44,7 +44,7 @@ func decodeImpl(data []byte, v interface{}) ([]byte, error) {
return rem, err
case reflect.Int64:
val, rem, err := DecodeSigned64(data)
ev.SetInt(int64(val))
ev.SetInt(val)
return rem, err
case reflect.String:
val, rem, err := DecodeString(data)
Expand Down
24 changes: 12 additions & 12 deletions utils/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ func encodeImpl(v interface{}) ([]byte, error) {
case reflect.Uint32:
return EncodeUnsigned32(uint32(rv.Uint())), nil
case reflect.Uint64:
return EncodeUnsigned64(uint64(rv.Uint())), nil
return EncodeUnsigned64(rv.Uint()), nil
case reflect.Int8:
return EncodeSigned8(int8(rv.Int())), nil
case reflect.Int16:
return EncodeSigned16(int16(rv.Int())), nil
case reflect.Int32:
return EncodeSigned32(int32(rv.Int())), nil
case reflect.Int64:
return EncodeSigned64(int64(rv.Int())), nil
return EncodeSigned64(rv.Int()), nil
case reflect.String:
return EncodeString(rv.String()), nil
case reflect.Slice:
Expand Down Expand Up @@ -79,58 +79,58 @@ func MustEncode(v ...interface{}) []byte {
return res
}

// EncodeUnsigned8 takes an `uint8` variable and encodes it into a byte array
// EncodeUnsigned8 takes an `uint8` variable and encodes it into a byte array.
func EncodeUnsigned8(v uint8) []byte {
return []byte{v}
}

// EncodeUnsigned16 takes an `uint16` variable and encodes it into a byte array
// EncodeUnsigned16 takes an `uint16` variable and encodes it into a byte array.
func EncodeUnsigned16(v uint16) []byte {
bytes := make([]byte, 2)
binary.BigEndian.PutUint16(bytes, v)
return bytes
}

// EncodeUnsigned32 takes an `uint32` variable and encodes it into a byte array
// EncodeUnsigned32 takes an `uint32` variable and encodes it into a byte array.
func EncodeUnsigned32(v uint32) []byte {
bytes := make([]byte, 4)
binary.BigEndian.PutUint32(bytes, v)
return bytes
}

// EncodeUnsigned64 takes an `uint64` variable and encodes it into a byte array
// EncodeUnsigned64 takes an `uint64` variable and encodes it into a byte array.
func EncodeUnsigned64(v uint64) []byte {
bytes := make([]byte, 8)
binary.BigEndian.PutUint64(bytes, v)
return bytes
}

// EncodeSigned8 takes an `int8` variable and encodes it into a byte array
// EncodeSigned8 takes an `int8` variable and encodes it into a byte array.
func EncodeSigned8(v int8) []byte {
return EncodeUnsigned8(uint8(v))
}

// EncodeSigned16 takes an `int16` variable and encodes it into a byte array
// EncodeSigned16 takes an `int16` variable and encodes it into a byte array.
func EncodeSigned16(v int16) []byte {
return EncodeUnsigned16(uint16(v))
}

// EncodeSigned32 takes an `int32` variable and encodes it into a byte array
// EncodeSigned32 takes an `int32` variable and encodes it into a byte array.
func EncodeSigned32(v int32) []byte {
return EncodeUnsigned32(uint32(v))
}

// EncodeSigned64 takes an `int64` variable and encodes it into a byte array
// EncodeSigned64 takes an `int64` variable and encodes it into a byte array.
func EncodeSigned64(v int64) []byte {
return EncodeUnsigned64(uint64(v))
}

// EncodeBytes takes a `[]byte` variable and encodes it into a byte array
// EncodeBytes takes a `[]byte` variable and encodes it into a byte array.
func EncodeBytes(v []byte) []byte {
return append(EncodeUnsigned32(uint32(len(v))), v...)
}

// EncodeString takes a `string` variable and encodes it into a byte array
// EncodeString takes a `string` variable and encodes it into a byte array.
func EncodeString(v string) []byte {
return append(EncodeUnsigned32(uint32(len(v))), []byte(v)...)
}
20 changes: 10 additions & 10 deletions x/exp/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type IBCModule struct {
keeper keeper.ExpKeeper
}

// NewIBCModule creates a new IBCModule given the keeper
// NewIBCModule creates a new IBCModule given the keeper.
func NewIBCModule(cdc codec.Codec, k keeper.ExpKeeper) IBCModule {
return IBCModule{
cdc: cdc,
Expand Down Expand Up @@ -57,7 +57,7 @@ func ValidateChannelParams(

// -------------------------------------------------------------------------------------------------------------------

// OnChanOpenInit implements the IBCModule interface
// OnChanOpenInit implements the IBCModule interface.
func (am IBCModule) OnChanOpenInit(
ctx sdk.Context,
order channeltypes.Order,
Expand All @@ -80,7 +80,7 @@ func (am IBCModule) OnChanOpenInit(
return version, nil
}

// OnChanOpenTry implements the IBCModule interface
// OnChanOpenTry implements the IBCModule interface.
func (am IBCModule) OnChanOpenTry(
ctx sdk.Context,
order channeltypes.Order,
Expand Down Expand Up @@ -110,7 +110,7 @@ func (am IBCModule) OnChanOpenTry(
return counterpartyVersion, nil
}

// OnChanOpenAck implements the IBCModule interface
// OnChanOpenAck implements the IBCModule interface.
func (am IBCModule) OnChanOpenAck(
ctx sdk.Context,
portID,
Expand All @@ -121,7 +121,7 @@ func (am IBCModule) OnChanOpenAck(
return nil
}

// OnChanOpenConfirm implements the IBCModule interface
// OnChanOpenConfirm implements the IBCModule interface.
func (am IBCModule) OnChanOpenConfirm(
ctx sdk.Context,
portID,
Expand All @@ -130,7 +130,7 @@ func (am IBCModule) OnChanOpenConfirm(
return nil
}

// OnChanCloseInit implements the IBCModule interface
// OnChanCloseInit implements the IBCModule interface.
func (am IBCModule) OnChanCloseInit(
ctx sdk.Context,
portID,
Expand All @@ -140,7 +140,7 @@ func (am IBCModule) OnChanCloseInit(
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "user cannot close channel")
}

// OnChanCloseConfirm implements the IBCModule interface
// OnChanCloseConfirm implements the IBCModule interface.
func (am IBCModule) OnChanCloseConfirm(
ctx sdk.Context,
portID,
Expand All @@ -149,7 +149,7 @@ func (am IBCModule) OnChanCloseConfirm(
return nil
}

// OnRecvPacket implements the IBCModule interface
// OnRecvPacket implements the IBCModule interface.
func (am IBCModule) OnRecvPacket(
ctx sdk.Context,
packet channeltypes.Packet,
Expand All @@ -159,7 +159,7 @@ func (am IBCModule) OnRecvPacket(
return nil
}

// OnAcknowledgementPacket implements the IBCModule interface
// OnAcknowledgementPacket implements the IBCModule interface.
func (am IBCModule) OnAcknowledgementPacket(
ctx sdk.Context,
packet channeltypes.Packet,
Expand Down Expand Up @@ -211,7 +211,7 @@ func (am IBCModule) OnAcknowledgementPacket(

// -------------------------------------------------------------------------------------------------------------------

// OnTimeoutPacket implements the IBCModule interface
// OnTimeoutPacket implements the IBCModule interface.
func (am IBCModule) OnTimeoutPacket(
ctx sdk.Context,
packet channeltypes.Packet,
Expand Down
4 changes: 2 additions & 2 deletions x/exp/keeper/execute_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ func (k ExpKeeper) ValidateBurnRequestByTime(ctx sdk.Context, burnRequest types.
}

func (k ExpKeeper) ValidateMintRequestByTime(ctx sdk.Context, mintRequest types.MintRequest) bool {
mintPeriod := k.GetBurnExpPeriod(ctx)
return mintRequest.RequestTime.Add(mintPeriod).After(ctx.BlockTime())
mintPeriod := k.GetClosePoolPeriod(ctx)
return mintRequest.RequestTime.Add(mintPeriod).Before(ctx.BlockTime())
}
3 changes: 2 additions & 1 deletion x/exp/keeper/exp_request.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/notional-labs/craft/x/exp/types"
Expand Down Expand Up @@ -56,7 +57,7 @@ func (k ExpKeeper) GetDaoTokenPrice(ctx sdk.Context) sdk.Dec {
}

// calculate exp value by ibc asset .
func (k ExpKeeper) calculateDaoTokenValue(ctx sdk.Context, amount sdk.Int) sdk.Dec {
func (k ExpKeeper) calculateDaoTokenValue(ctx sdk.Context, amount math.Int) sdk.Dec {
daoTokenPrice := k.GetDaoTokenPrice(ctx)

return daoTokenPrice.MulInt(amount)
Expand Down
16 changes: 8 additions & 8 deletions x/exp/keeper/ibc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// DONTCOVER
// No need to cover this simple methods

// ChanCloseInit defines a wrapper function for the channel Keeper's function
// ChanCloseInit defines a wrapper function for the channel Keeper's function.
func (k ExpKeeper) ChanCloseInit(ctx sdk.Context, portID, channelID string) error {
capName := host.ChannelCapabilityPath(portID, channelID)
chanCap, ok := k.scopedKeeper.GetCapability(ctx, capName)
Expand All @@ -23,42 +23,42 @@ func (k ExpKeeper) ChanCloseInit(ctx sdk.Context, portID, channelID string) erro
return k.channelKeeper.ChanCloseInit(ctx, portID, channelID, chanCap)
}

// IsBound checks if the module is already bound to the desired port
// IsBound checks if the module is already bound to the desired port.
func (k ExpKeeper) IsBound(ctx sdk.Context, portID string) bool {
_, ok := k.scopedKeeper.GetCapability(ctx, host.PortPath(portID))
return ok
}

// BindPort defines a wrapper function for the port Keeper's function in
// order to expose it to module's InitGenesis function
// order to expose it to module's InitGenesis function.
func (k ExpKeeper) BindPort(ctx sdk.Context, portID string) error {
capability := k.portKeeper.BindPort(ctx, portID)
return k.ClaimCapability(ctx, capability, host.PortPath(portID))
}

// GetPort returns the portID for the module. Used in ExportGenesis
// GetPort returns the portID for the module. Used in ExportGenesis.
func (k ExpKeeper) GetPort(ctx sdk.Context) string {
store := ctx.KVStore(k.storeKey)
return string(store.Get(types.IBCPortKey))
}

// SetPort sets the portID for the module. Used in InitGenesis
// SetPort sets the portID for the module. Used in InitGenesis.
func (k ExpKeeper) SetPort(ctx sdk.Context, portID string) {
store := ctx.KVStore(k.storeKey)
store.Set(types.IBCPortKey, []byte(portID))
}

// AuthenticateCapability wraps the scopedKeeper's AuthenticateCapability function
// AuthenticateCapability wraps the scopedKeeper's AuthenticateCapability function.
func (k ExpKeeper) AuthenticateCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) bool {
return k.scopedKeeper.AuthenticateCapability(ctx, cap, name)
}

// ClaimCapability wraps the scopedKeeper's ClaimCapability method
// ClaimCapability wraps the scopedKeeper's ClaimCapability method.
func (k ExpKeeper) ClaimCapability(ctx sdk.Context, cap *capabilitytypes.Capability, name string) error {
return k.scopedKeeper.ClaimCapability(ctx, cap, name)
}

// ExecuteMintExpByIbcToken only run in OnPacketRecv
// ExecuteMintExpByIbcToken only run in OnPacketRecv.
func (k ExpKeeper) ExecuteMintExpByIbcToken(ctx sdk.Context, fromAddress sdk.AccAddress, coin sdk.Coin) error {
mintRequest, _ := k.GetMintRequest(ctx, fromAddress)
expWillGet := k.calculateDaoTokenValue(ctx, coin.Amount)
Expand Down
45 changes: 45 additions & 0 deletions x/exp/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ func (k ExpKeeper) verifyAccountToWhiteList(ctx sdk.Context, memberAddress sdk.A
return nil
}

func (k ExpKeeper) verifyAccountToMintRequestList(ctx sdk.Context, memberAddress sdk.AccAddress) error {
// check if dstAddress already in mint request list .
list := k.GetMintRequestsByStatus(ctx, int(types.StatusOnGoingRequest))

for _, accountRecord := range list {
if memberAddress.String() == accountRecord.Account {
return types.ErrAddressdNotFound
}
}
return nil
}

func (k ExpKeeper) stakingCheck(ctx sdk.Context, memberAccount sdk.AccAddress, ar types.AccountRecord) error {
balance := k.bankKeeper.GetBalance(ctx, memberAccount, k.GetDenom(ctx))
if !ar.MaxToken.Amount.Equal(balance.Amount) {
Expand Down Expand Up @@ -210,6 +222,39 @@ func (k ExpKeeper) requestBurnCoinFromAddress(ctx sdk.Context, memberAccount sdk
return nil
}

//nolint:unused
func (k ExpKeeper) executeMintExpByIbcToken(ctx sdk.Context, fromAddress sdk.AccAddress, coin sdk.Coin) error {
mintRequest, _ := k.GetMintRequest(ctx, fromAddress)
expWillGet := k.calculateDaoTokenValue(ctx, coin.Amount)
fmt.Println("before", mintRequest, expWillGet)

if expWillGet.GTE(mintRequest.DaoTokenLeft) {
coinSpend := sdk.NewCoin(k.GetIbcDenom(ctx), mintRequest.DaoTokenLeft.TruncateInt())

err := k.FundPoolForExp(ctx, sdk.NewCoins(coinSpend), fromAddress)
if err != nil {
return err
}

mintRequest.DaoTokenMinted = mintRequest.DaoTokenLeft.Add(mintRequest.DaoTokenMinted)
mintRequest.DaoTokenLeft = sdk.NewDec(0)

k.SetMintRequest(ctx, mintRequest)
} else {
err := k.FundPoolForExp(ctx, sdk.NewCoins(coin), fromAddress)
if err != nil {
return sdkerrors.Wrap(err, "fund error")
}
k.removeMintRequest(ctx, mintRequest)
decCoin := sdk.NewDecFromInt(coin.Amount)

mintRequest.DaoTokenMinted = mintRequest.DaoTokenMinted.Add(decCoin)
mintRequest.DaoTokenLeft = mintRequest.DaoTokenLeft.Sub(decCoin)
k.SetMintRequest(ctx, mintRequest)
}
return nil
}

// SendIbcOracle send a package to query exp price over ibc.
func (k ExpKeeper) SendIbcOracle(ctx sdk.Context, fromAddress sdk.AccAddress, coin sdk.Coin) error {
return nil
Expand Down
4 changes: 2 additions & 2 deletions x/exp/keeper/msgserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (k msgServer) JoinDaoByIbcAsset(goCtx context.Context, msg *types.MsgJoinDa
return nil, types.ErrGov
}

err = k.verifyAccountToWhiteList(ctx, joinAddress)
err = k.verifyAccountToMintRequestList(ctx, joinAddress)

if err != nil {
return &types.MsgJoinDaoByIbcAssetResponse{}, err
Expand Down Expand Up @@ -189,7 +189,7 @@ func (k msgServer) SpendIbcAssetToExp(goCtx context.Context, msg *types.MsgSpend
return &types.MsgSpendIbcAssetToExpResponse{}, nil
}

// NEED REMOVE WHEN ORACLE DONE
// NEED REMOVE WHEN ORACLE DONE.
func (k msgServer) AdjustDaoPrice(goCtx context.Context, msg *types.MsgAdjustDaoTokenPrice) (*types.MsgAdjustDaoTokenPriceResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

Expand Down
Loading