Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify deal approve coin count err #102

Open
wants to merge 1 commit into
base: dex
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
10 changes: 8 additions & 2 deletions x/dex/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -482,8 +482,14 @@ func handleMsgDexDeal(ctx chainTypes.Context, k Keeper, msg *types.MsgDexDeal) (
// Auto SigIn for each account

// Update sigIn status
acc1, ass1, acc2, ass2 := msg.GetDealByDex()
fee1, fee2 := msg.GetDealFeeByDex()
acc1, ass1, acc2, ass2, err := msg.GetDealByDex()
if err != nil {
return nil, err
}
fee1, fee2, err := msg.GetDealFeeByDex()
if err != nil {
return nil, err
}

if err := k.Deal(ctx.Context(), msgData.Dex, acc1, acc2, ass1, ass2, fee1, fee2); err != nil {
return nil, err
Expand Down
14 changes: 7 additions & 7 deletions x/dex/keeper/keeper_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ func (a DexKeeper) Deal(ctx sdk.Context, dex, from, to AccountID,
return errors.Wrapf(dexTypes.ErrDexNotExists, "dex %s not exists to sigin", dex.String())
}

// update sigIn state to FromAccount, should sub amtFrom(include fee), and add gotted(amtTo-toFee)
approveAddForFrom := amtTo.Sub(feeTo)
// update sigIn state to FromAccount, should sub (amtFrom+feeFrom), and add gotted(amtTo)
approveAddForFrom := amtTo
approveNowForFrom, err := a.assetKeeper.GetApproveCoins(ctx, from, dex)
if err != nil {
return errors.Wrapf(err, "get approve data error acc: %s, dex: %s", from, dex)
Expand All @@ -238,16 +238,16 @@ func (a DexKeeper) Deal(ctx sdk.Context, dex, from, to AccountID,
return errors.Wrapf(err, "asset Approve error")
}

if _, err := a.updateSigIn(ctx, true, from, dex, amtFrom); err != nil {
if _, err := a.updateSigIn(ctx, true, from, dex, amtFrom.Add(feeFrom...)); err != nil {
return errors.Wrapf(err, "updateSigIn %s %s by %s error", dex, from, amtFrom)
}

if _, err := a.updateSigIn(ctx, false, from, dex, amtTo.Sub(feeTo)); err != nil {
if _, err := a.updateSigIn(ctx, false, from, dex, amtTo); err != nil {
return errors.Wrapf(err, "updateSigIn %s %s by %s error", dex, from, amtFrom)
}

// update sigIn state to ToAccount, should sub amtTo(include fee), and add gotted(amtFrom-fromFee)
approveAddForTo := amtFrom.Sub(feeFrom)
// update sigIn state to ToAccount, should sub amtTo, and add gotted(amtFrom-feeTo)
approveAddForTo := amtFrom.Sub(feeTo)
approveNowForTo, err := a.assetKeeper.GetApproveCoins(ctx, to, dex)
if err != nil {
return errors.Wrapf(err, "get approve data error acc: %s, dex: %s", to, dex)
Expand All @@ -265,7 +265,7 @@ func (a DexKeeper) Deal(ctx sdk.Context, dex, from, to AccountID,
return errors.Wrapf(err, "updateSigIn %s %s by %s error", dex, to, amtTo)
}

if _, err := a.updateSigIn(ctx, false, to, dex, amtFrom.Sub(feeFrom)); err != nil {
if _, err := a.updateSigIn(ctx, false, to, dex, amtFrom.Sub(feeTo)); err != nil {
return errors.Wrapf(err, "updateSigIn %s %s by %s error", dex, from, amtFrom)
}

Expand Down
29 changes: 24 additions & 5 deletions x/dex/types/msg_asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ type MsgDexDealData struct {
ID []byte `json:"id" yaml:"id"` // L2 block id for prove
Hash []byte `json:"hash" yaml:"hash"` // L2 hash for prove
Proves []byte `json:"proves" yaml:"proves"` // L2 prove datas
FeeFrom Coins `json:"fee_from" yaml:"fee_from"`
FeeTo Coins `json:"fee_to" yaml:"fee_to"`
}

// Type imp for data KuMsgData
Expand All @@ -182,19 +184,36 @@ func NewMsgDexDeal(auth types.AccAddress, dex AccountID, from, to AccountID, fro
msg.WithData(Cdc(), &MsgDexDealData{
Dex: dex,
ExtData: ext,
FeeFrom: types.NewCoins(feeFromFrom),
FeeTo: types.NewCoins(feeFromTo),
}),
),
}
}

func (msg MsgDexDeal) GetDealByDex() (AccountID, Coins, AccountID, Coins) {
func (msg MsgDexDeal) GetDealByDex() (from AccountID, assetFrom Coins, to AccountID, assetTo Coins, err error) {
trs := msg.Transfers
return trs[0].From, trs[0].Amount, trs[2].From, trs[2].Amount
var data MsgDexDealData
data, err = msg.GetData()
if err != nil {
return AccountID{}, nil, AccountID{}, nil, err
}
from = trs[0].From
assetFrom = trs[0].Amount.Sub(data.FeeFrom)
to = trs[1].To
assetTo = trs[2].Amount
return
}

func (msg MsgDexDeal) GetDealFeeByDex() (Coins, Coins) {
trs := msg.Transfers
return trs[0].Amount.Sub(trs[1].Amount), trs[2].Amount.Sub(trs[3].Amount)
func (msg MsgDexDeal) GetDealFeeByDex() (feeFrom Coins, feeTo Coins, err error) {
var data MsgDexDealData
data, err = msg.GetData()
if err != nil {
return nil, nil, err
}
feeFrom = data.FeeFrom
feeTo = data.FeeTo
return
}

func (msg MsgDexDeal) GetData() (MsgDexDealData, error) {
Expand Down