Commit 350814d5 authored by pengjun's avatar pengjun Committed by vipwzw

#1024 collateralize和issuance合约支持coins可配精度

parent c34694de
......@@ -393,10 +393,12 @@ Enable=0
[fork.sub.issuance]
Enable=0
ForkIssuanceTableUpdate=0
ForkIssuancePrecision=0
[fork.sub.collateralize]
Enable=0
ForkCollateralizeTableUpdate=0
ForkCollateralizePrecision=0
#对已有的平行链如果不是从0开始同步数据,需要设置这个kvmvccmavl的对应平行链高度的fork,如果从0开始同步,statehash会跟以前mavl的不同
[fork.sub.store-kvmvccmavl]
......
......@@ -394,10 +394,12 @@ Enable=0
[fork.sub.issuance]
Enable=0
ForkIssuanceTableUpdate=0
ForkIssuancePrecision=0
[fork.sub.collateralize]
Enable=0
ForkCollateralizeTableUpdate=0
ForkCollateralizePrecision=0
#对已有的平行链如果不是从0开始同步数据,需要设置这个kvmvccmavl的对应平行链高度的fork,如果从0开始同步,statehash会跟以前mavl的不同
[fork.sub.store-kvmvccmavl]
......
......@@ -33,6 +33,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/robertkrimen/otto v0.0.0-20180617131154-15f95af6e78d
github.com/rs/cors v1.7.0
github.com/shopspring/decimal v1.2.0
github.com/spf13/cobra v1.1.1
github.com/stretchr/testify v1.7.0
github.com/tjfoc/gmsm v1.3.2
......
......@@ -6,6 +6,8 @@ package executor
import (
"github.com/33cn/chain33/common/db/table"
"github.com/shopspring/decimal"
"math"
"github.com/33cn/chain33/account"
"github.com/33cn/chain33/common"
......@@ -569,12 +571,25 @@ func (action *Action) CollateralizeBorrow(borrow *pty.CollateralizeBorrow) (*typ
return nil, err
}
// 精度转换 #1024
// token精度转成精度8
valueReal := borrow.GetValue()
cfg := action.Collateralize.GetAPI().GetConfig()
if(cfg.IsDappFork(action.Collateralize.GetHeight(), pty.CollateralizeX, pty.ForkCollateralizePrecision)) {
precisionNum := int(math.Log10(float64(cfg.GetTokenPrecision())))
valueReal = decimal.NewFromInt(valueReal).Shift(int32(-precisionNum)).Shift(8).IntPart()
}
// 根据价格和需要借贷的金额,计算需要质押的抵押物数量
btyFrozen, err := getBtyNumToFrozen(borrow.GetValue(), lastPrice, coll.LiquidationRatio)
btyFrozen, err := getBtyNumToFrozen(valueReal, lastPrice, coll.LiquidationRatio)
if err != nil {
clog.Error("CollateralizeBorrow.getBtyNumToFrozen", "CollID", coll.CollateralizeId, "addr", action.fromaddr, "execaddr", action.execaddr, "error", err)
return nil, err
}
// bty精度8转成coins精度
if(cfg.IsDappFork(action.Collateralize.GetHeight(), pty.CollateralizeX, pty.ForkCollateralizePrecision)) {
precisionNum := int(math.Log10(float64(cfg.GetCoinPrecision())))
btyFrozen = decimal.NewFromInt(btyFrozen).Shift(-8).Shift(int32(precisionNum)).IntPart()
}
// 检查抵押物账户余额
if !action.CheckExecAccountBalance(action.fromaddr, btyFrozen, 0) {
......@@ -680,8 +695,22 @@ func (action *Action) CollateralizeRepay(repay *pty.CollateralizeRepay) (*types.
return nil, pty.ErrRecordNotExist
}
// 精度转换 #1024
// token精度转成精度8
cfg := action.Collateralize.GetAPI().GetConfig()
valueReal := borrowRecord.DebtValue
if(cfg.IsDappFork(action.Collateralize.GetHeight(), pty.CollateralizeX, pty.ForkCollateralizePrecision)) {
precisionNum := int(math.Log10(float64(cfg.GetTokenPrecision())))
valueReal = decimal.NewFromInt(valueReal).Shift(int32(-precisionNum)).Shift(8).IntPart()
}
// 借贷金额+利息
fee := ((borrowRecord.DebtValue * coll.StabilityFeeRatio) / 1e8) * 1e4
fee := ((valueReal * coll.StabilityFeeRatio) / 1e8) * 1e4
// 精度8转成token精度
if(cfg.IsDappFork(action.Collateralize.GetHeight(), pty.CollateralizeX, pty.ForkCollateralizePrecision)) {
precisionNum := int(math.Log10(float64(cfg.GetTokenPrecision())))
fee = decimal.NewFromInt(fee).Shift(-8).Shift(int32(precisionNum)).IntPart()
}
realRepay := borrowRecord.DebtValue + fee
// 检查
......@@ -813,7 +842,17 @@ func (action *Action) CollateralizeAppend(cAppend *pty.CollateralizeAppend) (*ty
// 构造借出记录
borrowRecord.CollateralValue += cAppend.CollateralValue
borrowRecord.CollateralPrice = lastPrice
borrowRecord.LiquidationPrice = calcLiquidationPrice(borrowRecord.DebtValue, borrowRecord.CollateralValue)
// 精度转换 #1024
cfg := action.Collateralize.GetAPI().GetConfig()
debtValueReal := borrowRecord.DebtValue
collateralValueReal := borrowRecord.CollateralValue
if(cfg.IsDappFork(action.Collateralize.GetHeight(), pty.CollateralizeX, pty.ForkCollateralizePrecision)) {
precisionNum := int(math.Log10(float64(cfg.GetTokenPrecision())))
debtValueReal = decimal.NewFromInt(debtValueReal).Shift(int32(-precisionNum)).Shift(8).IntPart()
collateralValueReal = decimal.NewFromInt(collateralValueReal).Shift(int32(-precisionNum)).Shift(8).IntPart()
}
borrowRecord.LiquidationPrice = calcLiquidationPrice(debtValueReal, collateralValueReal)
if borrowRecord.LiquidationPrice*PriceWarningRate < lastPrice {
// 告警解除
if borrowRecord.Status == pty.CollateralizeUserStatusWarning {
......
......@@ -30,6 +30,7 @@ func init() {
func InitFork(cfg *types.Chain33Config) {
cfg.RegisterDappFork(CollateralizeX, "Enable", 0)
cfg.RegisterDappFork(CollateralizeX, ForkCollateralizeTableUpdate, 0)
cfg.RegisterDappFork(CollateralizeX, ForkCollateralizePrecision, 0)
}
//InitExecutor ...
......@@ -157,7 +158,7 @@ func CreateRawCollateralizeCreateTx(cfg *types.Chain33Config, parm *Collateraliz
llog.Error("CreateRawCollateralizeCreateTx", "parm", parm)
return nil, types.ErrInvalidParam
}
totalBalanceInt64, err := types.FormatFloatDisplay2Value(parm.TotalBalance, cfg.GetCoinPrecision())
totalBalanceInt64, err := types.FormatFloatDisplay2Value(parm.TotalBalance, cfg.GetTokenPrecision())
if err != nil {
return nil, errors.Wrapf(types.ErrInvalidParam, "FormatFloatDisplay2Value.TotalBalance")
}
......@@ -188,7 +189,7 @@ func CreateRawCollateralizeBorrowTx(cfg *types.Chain33Config, parm *Collateraliz
llog.Error("CreateRawCollateralizeBorrowTx", "parm", parm)
return nil, types.ErrInvalidParam
}
valueInt64, err := types.FormatFloatDisplay2Value(parm.Value, cfg.GetCoinPrecision())
valueInt64, err := types.FormatFloatDisplay2Value(parm.Value, cfg.GetTokenPrecision())
if err != nil {
return nil, errors.Wrapf(types.ErrInvalidParam, "FormatFloatDisplay2Value.Value")
}
......@@ -315,7 +316,7 @@ func CreateRawCollateralizeRetrieveTx(cfg *types.Chain33Config, parm *Collateral
llog.Error("CreateRawCollateralizeCloseTx", "parm", parm)
return nil, types.ErrInvalidParam
}
balanceInt64, err := types.FormatFloatDisplay2Value(parm.Balance, cfg.GetCoinPrecision())
balanceInt64, err := types.FormatFloatDisplay2Value(parm.Balance, cfg.GetTokenPrecision())
if err != nil {
return nil, errors.Wrapf(types.ErrInvalidParam, "FormatFloatDisplay2Value.Balance")
}
......@@ -348,11 +349,11 @@ func CreateRawCollateralizeManageTx(cfg *types.Chain33Config, parm *Collateraliz
llog.Error("CreateRawCollateralizeManageTx", "parm", parm)
return nil, types.ErrInvalidParam
}
totalBalanceInt64, err := types.FormatFloatDisplay2Value(parm.TotalBalance, cfg.GetCoinPrecision())
totalBalanceInt64, err := types.FormatFloatDisplay2Value(parm.TotalBalance, cfg.GetTokenPrecision())
if err != nil {
return nil, errors.Wrapf(types.ErrInvalidParam, "FormatFloatDisplay2Value.totalBalance")
}
debtCeilingInt64, err := types.FormatFloatDisplay2Value(parm.DebtCeiling, cfg.GetCoinPrecision())
debtCeilingInt64, err := types.FormatFloatDisplay2Value(parm.DebtCeiling, cfg.GetTokenPrecision())
if err != nil {
return nil, errors.Wrapf(types.ErrInvalidParam, "FormatFloatDisplay2Value.DebtCeiling")
}
......
......@@ -56,4 +56,5 @@ const (
//fork ...
var (
ForkCollateralizeTableUpdate = "ForkCollateralizeTableUpdate"
ForkCollateralizePrecision = "ForkCollateralizePrecision"
)
......@@ -13,6 +13,8 @@ import (
"github.com/33cn/chain33/types"
pty "github.com/33cn/plugin/plugin/dapp/issuance/types"
tokenE "github.com/33cn/plugin/plugin/dapp/token/executor"
"github.com/shopspring/decimal"
"math"
)
// List control
......@@ -536,12 +538,25 @@ func (action *Action) IssuanceDebt(debt *pty.IssuanceDebt) (*types.Receipt, erro
return nil, err
}
// 精度转换 #1024
// 先将token由token精度转成精度8
valueReal := debt.GetValue()
cfg := action.Issuance.GetAPI().GetConfig()
if(cfg.IsDappFork(action.Issuance.GetHeight(), pty.IssuanceX, pty.ForkIssuancePrecision)) {
precisionNum := int(math.Log10(float64(cfg.GetTokenPrecision())))
valueReal = decimal.NewFromInt(valueReal).Shift(int32(-precisionNum)).Shift(8).IntPart()
}
// 根据价格和需要借贷的金额,计算需要质押的抵押物数量
btyFrozen, err := getBtyNumToFrozen(debt.Value, lastPrice, issu.LiquidationRatio)
btyFrozen, err := getBtyNumToFrozen(valueReal, lastPrice, issu.LiquidationRatio)
if err != nil {
clog.Error("IssuanceDebt.getBtyNumToFrozen", "CollID", issu.IssuanceId, "addr", action.fromaddr, "execaddr", action.execaddr, "error", err)
return nil, err
}
// 再将bty由精度8转成coins精度
if(cfg.IsDappFork(action.Issuance.GetHeight(), pty.IssuanceX, pty.ForkIssuancePrecision)) {
precisionNum := int(math.Log10(float64(cfg.GetCoinPrecision())))
btyFrozen = decimal.NewFromInt(btyFrozen).Shift(-8).Shift(int32(precisionNum)).IntPart()
}
// 检查抵押物账户余额
if !action.CheckExecAccountBalance(action.fromaddr, btyFrozen, 0) {
......
......@@ -30,6 +30,7 @@ func init() {
func InitFork(cfg *types.Chain33Config) {
cfg.RegisterDappFork(IssuanceX, "Enable", 0)
cfg.RegisterDappFork(IssuanceX, ForkIssuanceTableUpdate, 0)
cfg.RegisterDappFork(IssuanceX, ForkIssuancePrecision, 0)
}
//InitExecutor ...
......@@ -148,11 +149,11 @@ func CreateRawIssuanceCreateTx(cfg *types.Chain33Config, parm *IssuanceCreateTx)
return nil, types.ErrInvalidParam
}
totalBalanceInt64, err := types.FormatFloatDisplay2Value(parm.TotalBalance, cfg.GetCoinPrecision())
totalBalanceInt64, err := types.FormatFloatDisplay2Value(parm.TotalBalance, cfg.GetTokenPrecision())
if err != nil {
return nil, errors.Wrapf(types.ErrInvalidParam, "FormatFloatDisplay2Value.totalBalance")
}
debtCeilingInt64, err := types.FormatFloatDisplay2Value(parm.DebtCeiling, cfg.GetCoinPrecision())
debtCeilingInt64, err := types.FormatFloatDisplay2Value(parm.DebtCeiling, cfg.GetTokenPrecision())
if err != nil {
return nil, errors.Wrapf(types.ErrInvalidParam, "FormatFloatDisplay2Value.DebtCeiling")
}
......@@ -187,7 +188,7 @@ func CreateRawIssuanceDebtTx(cfg *types.Chain33Config, parm *IssuanceDebtTx) (*t
llog.Error("CreateRawIssuanceBorrowTx", "parm", parm)
return nil, types.ErrInvalidParam
}
valueInt64, err := types.FormatFloatDisplay2Value(parm.Value, cfg.GetCoinPrecision())
valueInt64, err := types.FormatFloatDisplay2Value(parm.Value, cfg.GetTokenPrecision())
if err != nil {
return nil, errors.Wrapf(types.ErrInvalidParam, "FormatFloatDisplay2Value.Value")
}
......
......@@ -55,4 +55,5 @@ const (
//fork ...
var (
ForkIssuanceTableUpdate = "ForkIssuanceTableUpdate"
ForkIssuancePrecision = "ForkIssuancePrecision"
)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment