Skip to content

Commit

Permalink
Merge pull request #117 from persistenceOne/puneet2019/take
Browse files Browse the repository at this point in the history
changing exchange rate definition.
  • Loading branch information
Deepanshu Tripathi authored Oct 16, 2020
2 parents ddcd06c + 36a181b commit 9acc19a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
3 changes: 2 additions & 1 deletion modules/orders/internal/mapper/order.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package mapper

import (
sdkTypes "github.com/cosmos/cosmos-sdk/types"
"github.com/persistenceOne/persistenceSDK/constants/properties"
"github.com/persistenceOne/persistenceSDK/schema/mappables"
"github.com/persistenceOne/persistenceSDK/schema/traits"
Expand Down Expand Up @@ -54,7 +55,7 @@ func (order order) GetExchangeRate() types.Property {
} else if exchangeRate := order.Mutables.Get().Get(base.NewID(properties.ExchangeRate)); exchangeRate != nil {
return exchangeRate
} else {
data, _ := base.ReadDecData("")
data := base.NewDecData(sdkTypes.OneDec())
return base.NewProperty(base.NewID(properties.ExchangeRate), base.NewFact(data))
}
}
Expand Down
17 changes: 12 additions & 5 deletions modules/orders/internal/transactions/take/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ func (transactionKeeper transactionKeeper) Transact(context sdkTypes.Context, ms
return newTransactionResponse(errors.NotAuthorized)
}
}
if auxiliaryResponse := transactionKeeper.transferAuxiliary.GetKeeper().Help(context, transfer.NewAuxiliaryRequest(message.FromID, order.GetMakerID(), order.GetTakerOwnableID(), message.TakerOwnableSplit)); !auxiliaryResponse.IsSuccessful() {
return newTransactionResponse(auxiliaryResponse.GetError())
}

exchangeRateProperty := metaProperties.GetMetaProperty(base.NewID(properties.ExchangeRate))
if exchangeRateProperty == nil {
Expand All @@ -74,22 +71,32 @@ func (transactionKeeper transactionKeeper) Transact(context sdkTypes.Context, ms
return newTransactionResponse(errors.MetaDataError)
}

sendMakerOwnableSplit := message.TakerOwnableSplit.Quo(sdkTypes.SmallestDec()).Mul(exchangeRate)
sendTakerOwnableSplit := makerOwnableSplit.Mul(exchangeRate)
sendMakerOwnableSplit := message.TakerOwnableSplit.Quo(exchangeRate)
updatedMakerOwnableSplit := makerOwnableSplit.Sub(sendMakerOwnableSplit)
if updatedMakerOwnableSplit.LT(sdkTypes.ZeroDec()) {
if message.TakerOwnableSplit.LT(sendTakerOwnableSplit) {
return newTransactionResponse(errors.InsufficientBalance)
}
sendMakerOwnableSplit = makerOwnableSplit
orders = orders.Remove(order)
} else if updatedMakerOwnableSplit.Equal(sdkTypes.ZeroDec()) {
if message.TakerOwnableSplit.LT(sendTakerOwnableSplit) {
return newTransactionResponse(errors.InsufficientBalance)
}
orders = orders.Remove(order)
} else {
sendTakerOwnableSplit = message.TakerOwnableSplit
mutableProperties, Error := scrub.GetPropertiesFromResponse(transactionKeeper.scrubAuxiliary.GetKeeper().Help(context, scrub.NewAuxiliaryRequest(base.NewMetaProperty(base.NewID(properties.MakerOwnableSplit), base.NewMetaFact(base.NewDecData(updatedMakerOwnableSplit))))))
if Error != nil {
return newTransactionResponse(Error)
}
order = mapper.NewOrder(order.GetID(), order.GetImmutables(), order.GetMutables().Mutate(mutableProperties.GetList()...))
orders = orders.Mutate(order)
}

if auxiliaryResponse := transactionKeeper.transferAuxiliary.GetKeeper().Help(context, transfer.NewAuxiliaryRequest(message.FromID, order.GetMakerID(), order.GetTakerOwnableID(), sendTakerOwnableSplit)); !auxiliaryResponse.IsSuccessful() {
return newTransactionResponse(auxiliaryResponse.GetError())
}
if auxiliaryResponse := transactionKeeper.transferAuxiliary.GetKeeper().Help(context, transfer.NewAuxiliaryRequest(base.NewID(mapper.ModuleName), message.FromID, order.GetMakerOwnableID(), sendMakerOwnableSplit)); !auxiliaryResponse.IsSuccessful() {
return newTransactionResponse(auxiliaryResponse.GetError())
}
Expand Down

0 comments on commit 9acc19a

Please sign in to comment.