Commit 38119f4b authored by pengjun's avatar pengjun

#627 fix double types of issuance & collateralize

parent 38154040
...@@ -5,8 +5,6 @@ ...@@ -5,8 +5,6 @@
package executor package executor
import ( import (
"math"
"github.com/33cn/chain33/common/db/table" "github.com/33cn/chain33/common/db/table"
"github.com/33cn/chain33/account" "github.com/33cn/chain33/account"
...@@ -30,11 +28,11 @@ const ( ...@@ -30,11 +28,11 @@ const (
const ( const (
Coin = types.Coin // 1e8 Coin = types.Coin // 1e8
DefaultDebtCeiling = 10000 * Coin // 默认借贷限额 DefaultDebtCeiling = 10000 * Coin // 默认借贷限额
DefaultLiquidationRatio = 0.4 // 默认质押比 DefaultLiquidationRatio = 0.4 * 1e4 // 默认质押比
DefaultStabilityFeeRation = 0.08 // 默认稳定费 DefaultStabilityFeeRation = 0.08 * 1e4 // 默认稳定费
DefaultPeriod = 3600 * 24 * 365 // 默认合约限期 DefaultPeriod = 3600 * 24 * 365 // 默认合约限期
DefaultTotalBalance = 0 // 默认放贷总额 DefaultTotalBalance = 0 // 默认放贷总额
PriceWarningRate = 1.3 // 价格提前预警率 PriceWarningRate = 1.3 * 1e4 // 价格提前预警率
ExpireWarningTime = 3600 * 24 * 10 // 提前10天超时预警 ExpireWarningTime = 3600 * 24 * 10 // 提前10天超时预警
) )
...@@ -215,8 +213,8 @@ func (action *Action) GetIndex() int64 { ...@@ -215,8 +213,8 @@ func (action *Action) GetIndex() int64 {
return action.height*types.MaxTxsPerBlock + int64(action.index) return action.height*types.MaxTxsPerBlock + int64(action.index)
} }
func getLatestLiquidationPrice(coll *pty.Collateralize) float64 { func getLatestLiquidationPrice(coll *pty.Collateralize) int64 {
var latest float64 var latest int64
for _, collRecord := range coll.BorrowRecords { for _, collRecord := range coll.BorrowRecords {
if collRecord.LiquidationPrice > latest { if collRecord.LiquidationPrice > latest {
latest = collRecord.LiquidationPrice latest = collRecord.LiquidationPrice
...@@ -250,8 +248,8 @@ func (action *Action) CollateralizeManage(manage *pty.CollateralizeManage) (*typ ...@@ -250,8 +248,8 @@ func (action *Action) CollateralizeManage(manage *pty.CollateralizeManage) (*typ
} }
// 配置借贷参数 // 配置借贷参数
if manage.DebtCeiling < 0 || manage.LiquidationRatio < 0 || manage.LiquidationRatio >= 1 || if manage.DebtCeiling < 0 || manage.LiquidationRatio < 0 || manage.LiquidationRatio >= 10000 ||
manage.StabilityFeeRatio < 0 || manage.StabilityFeeRatio >= 1 { manage.StabilityFeeRatio < 0 || manage.StabilityFeeRatio >= 10000 {
return nil, pty.ErrRiskParam return nil, pty.ErrRiskParam
} }
...@@ -464,28 +462,24 @@ func (action *Action) CollateralizeCreate(create *pty.CollateralizeCreate) (*typ ...@@ -464,28 +462,24 @@ func (action *Action) CollateralizeCreate(create *pty.CollateralizeCreate) (*typ
} }
// 根据最近抵押物价格计算需要冻结的BTY数量 // 根据最近抵押物价格计算需要冻结的BTY数量
func getBtyNumToFrozen(value int64, price float64, ratio float64) (int64, error) { func getBtyNumToFrozen(value int64, price int64, ratio int64) (int64, error) {
if price == 0 { if price == 0 {
clog.Error("Bty price should greate to 0") clog.Error("Bty price should greate to 0")
return 0, pty.ErrPriceInvalid return 0, pty.ErrPriceInvalid
} }
valueReal := float64(value) / 1e8 btyValue := (value * 1e4) / (price * ratio)
btyValue := valueReal / (price * ratio) return btyValue * 1e4, nil
return int64(math.Trunc((btyValue+0.0000001)*1e4)) * 1e4, nil
} }
// 计算清算价格 // 计算清算价格
// value:借出ccny数量, colValue:抵押物数量, price:抵押物价格 // value:借出ccny数量, colValue:抵押物数量, price:抵押物价格
func calcLiquidationPrice(value int64, colValue int64) float64 { func calcLiquidationPrice(value int64, colValue int64) int64 {
liquidationRation := float64(value) / float64(colValue) return (value *pty.CollateralizePreLiquidationRatio) / colValue
liquidationPrice := math.Trunc(liquidationRation*pty.CollateralizePreLiquidationRatio*1e4) / 1e4
return liquidationPrice
} }
// 获取最近抵押物价格 // 获取最近抵押物价格
func getLatestPrice(db dbm.KV) (float64, error) { func getLatestPrice(db dbm.KV) (int64, error) {
data, err := db.Get(PriceKey()) data, err := db.Get(PriceKey())
if err != nil { if err != nil {
clog.Error("getLatestPrice", "get", err) clog.Error("getLatestPrice", "get", err)
...@@ -621,7 +615,7 @@ func (action *Action) CollateralizeBorrow(borrow *pty.CollateralizeBorrow) (*typ ...@@ -621,7 +615,7 @@ func (action *Action) CollateralizeBorrow(borrow *pty.CollateralizeBorrow) (*typ
borrowRecord.StartTime = action.blocktime borrowRecord.StartTime = action.blocktime
borrowRecord.CollateralPrice = lastPrice borrowRecord.CollateralPrice = lastPrice
borrowRecord.DebtValue = borrow.GetValue() borrowRecord.DebtValue = borrow.GetValue()
borrowRecord.LiquidationPrice = math.Trunc(coll.LiquidationRatio*lastPrice*pty.CollateralizePreLiquidationRatio*1e4) / 1e4 borrowRecord.LiquidationPrice = (coll.LiquidationRatio*lastPrice*pty.CollateralizePreLiquidationRatio)/1e8
borrowRecord.Status = pty.CollateralizeUserStatusCreate borrowRecord.Status = pty.CollateralizeUserStatusCreate
borrowRecord.ExpireTime = action.blocktime + coll.Period borrowRecord.ExpireTime = action.blocktime + coll.Period
...@@ -684,8 +678,8 @@ func (action *Action) CollateralizeRepay(repay *pty.CollateralizeRepay) (*types. ...@@ -684,8 +678,8 @@ func (action *Action) CollateralizeRepay(repay *pty.CollateralizeRepay) (*types.
} }
// 借贷金额+利息 // 借贷金额+利息
fee := (float64(borrowRecord.DebtValue) / 1e8) * coll.StabilityFeeRatio fee := (borrowRecord.DebtValue * coll.StabilityFeeRatio)/1e4
realRepay := borrowRecord.DebtValue + int64(math.Trunc((fee+0.0000001)*1e4))*1e4 realRepay := borrowRecord.DebtValue + fee
// 检查 // 检查
if !action.CheckExecTokenAccount(action.fromaddr, realRepay, false) { if !action.CheckExecTokenAccount(action.fromaddr, realRepay, false) {
...@@ -899,12 +893,12 @@ func getGuarantorAddr(db dbm.KV) (string, error) { ...@@ -899,12 +893,12 @@ func getGuarantorAddr(db dbm.KV) (string, error) {
} }
// 系统清算 // 系统清算
func (action *Action) systemLiquidation(coll *pty.Collateralize, price float64) (*types.Receipt, error) { func (action *Action) systemLiquidation(coll *pty.Collateralize, price int64) (*types.Receipt, error) {
var logs []*types.ReceiptLog var logs []*types.ReceiptLog
var kv []*types.KeyValue var kv []*types.KeyValue
for index, borrowRecord := range coll.BorrowRecords { for index, borrowRecord := range coll.BorrowRecords {
if borrowRecord.LiquidationPrice*PriceWarningRate < price { if (borrowRecord.LiquidationPrice*PriceWarningRate)/1e4 < price {
if borrowRecord.Status == pty.CollateralizeUserStatusWarning { if borrowRecord.Status == pty.CollateralizeUserStatusWarning {
borrowRecord.PreStatus = borrowRecord.Status borrowRecord.PreStatus = borrowRecord.Status
borrowRecord.Status = pty.CollateralizeUserStatusCreate borrowRecord.Status = pty.CollateralizeUserStatusCreate
...@@ -1013,8 +1007,8 @@ func (action *Action) expireLiquidation(coll *pty.Collateralize) (*types.Receipt ...@@ -1013,8 +1007,8 @@ func (action *Action) expireLiquidation(coll *pty.Collateralize) (*types.Receipt
} }
// 价格计算策略 // 价格计算策略
func pricePolicy(feed *pty.CollateralizeFeed) float64 { func pricePolicy(feed *pty.CollateralizeFeed) int64 {
var totalPrice float64 var totalPrice int64
var totalVolume int64 var totalVolume int64
for _, volume := range feed.Volume { for _, volume := range feed.Volume {
totalVolume += volume totalVolume += volume
...@@ -1026,7 +1020,7 @@ func pricePolicy(feed *pty.CollateralizeFeed) float64 { ...@@ -1026,7 +1020,7 @@ func pricePolicy(feed *pty.CollateralizeFeed) float64 {
} }
for i, price := range feed.Price { for i, price := range feed.Price {
totalPrice += price * (float64(feed.Volume[i]) / float64(totalVolume)) totalPrice += (price * feed.Volume[i]) / totalVolume
} }
return totalPrice return totalPrice
......
...@@ -7,14 +7,14 @@ message Collateralize { ...@@ -7,14 +7,14 @@ message Collateralize {
string collateralizeId = 1; //放贷ID,一期放贷对应一个ID string collateralizeId = 1; //放贷ID,一期放贷对应一个ID
int64 totalBalance = 2; //当期放贷的总金额(ccny) int64 totalBalance = 2; //当期放贷的总金额(ccny)
int64 debtCeiling = 3; //单用户可借出的限额(ccny) int64 debtCeiling = 3; //单用户可借出的限额(ccny)
double liquidationRatio = 4; //清算比例 int64 liquidationRatio = 4; //清算比例
double stabilityFeeRatio = 5; //稳定费率 int64 stabilityFeeRatio = 5; //稳定费率
string createAddr = 6; //创建人地址 string createAddr = 6; //创建人地址
int64 balance = 7; //放贷剩余金额(ccny) int64 balance = 7; //放贷剩余金额(ccny)
repeated BorrowRecord borrowRecords = 8; //借贷记录 repeated BorrowRecord borrowRecords = 8; //借贷记录
repeated BorrowRecord InvalidRecords = 9; //失效的借贷记录 repeated BorrowRecord InvalidRecords = 9; //失效的借贷记录
int32 status = 10;//当期借贷的状态,是否关闭 int32 status = 10;//当期借贷的状态,是否关闭
double latestLiquidationPrice = 11;//最高清算价格 int64 latestLiquidationPrice = 11;//最高清算价格
int64 period = 12;//借贷最大期限 int64 period = 12;//借贷最大期限
int64 latestExpireTime = 13;//最近超期时间 int64 latestExpireTime = 13;//最近超期时间
int64 collBalance = 14;//抵押bty int64 collBalance = 14;//抵押bty
...@@ -26,9 +26,9 @@ message BorrowRecord { ...@@ -26,9 +26,9 @@ message BorrowRecord {
string accountAddr = 1; //借贷人地址 string accountAddr = 1; //借贷人地址
int64 startTime = 2; //借贷时间 int64 startTime = 2; //借贷时间
int64 collateralValue = 3; //抵押物价值(bty) int64 collateralValue = 3; //抵押物价值(bty)
double collateralPrice = 4; //抵押物价格 int64 collateralPrice = 4; //抵押物价格
int64 debtValue = 5; //债务价值(ccny) int64 debtValue = 5; //债务价值(ccny)
double liquidationPrice = 6; //抵押物清算价格 int64 liquidationPrice = 6; //抵押物清算价格
int32 status = 7; //抵押状态,是否被清算 int32 status = 7; //抵押状态,是否被清算
int64 liquidateTime = 8; //清算时间 int64 liquidateTime = 8; //清算时间
int64 expireTime = 9; //超时清算时间 int64 expireTime = 9; //超时清算时间
...@@ -40,9 +40,9 @@ message BorrowRecord { ...@@ -40,9 +40,9 @@ message BorrowRecord {
// 资产价格记录 // 资产价格记录
message AssetPriceRecord { message AssetPriceRecord {
int64 recordTime = 1; //价格记录时间 int64 recordTime = 1; //价格记录时间
double btyPrice = 2; //bty价格 int64 btyPrice = 2; //bty价格
double btcPrice = 3; //btc价格 int64 btcPrice = 3; //btc价格
double ethPrice = 4; //eth价格 int64 ethPrice = 4; //eth价格
} }
// action // action
...@@ -61,8 +61,8 @@ message CollateralizeAction { ...@@ -61,8 +61,8 @@ message CollateralizeAction {
message CollateralizeManage { message CollateralizeManage {
int64 debtCeiling = 1; //单用户可借出的限额(ccny) int64 debtCeiling = 1; //单用户可借出的限额(ccny)
double liquidationRatio = 2; //清算比例 int64 liquidationRatio = 2; //清算比例
double stabilityFeeRatio = 3; //稳定费 int64 stabilityFeeRatio = 3; //稳定费
int64 period = 4; //合约期限 int64 period = 4; //合约期限
int64 totalBalance = 5; //放贷总量 int64 totalBalance = 5; //放贷总量
int64 currentTime = 6; //设置时间 int64 currentTime = 6; //设置时间
...@@ -98,7 +98,7 @@ message CollateralizeAppend { ...@@ -98,7 +98,7 @@ message CollateralizeAppend {
// 喂价 // 喂价
message CollateralizeFeed { message CollateralizeFeed {
int32 collType = 1; //抵押物价格类型(1,bty,2,btc,3,eth...) int32 collType = 1; //抵押物价格类型(1,bty,2,btc,3,eth...)
repeated double price = 2; //喂价 repeated int64 price = 2; //喂价
repeated int64 volume = 3; //成交量 repeated int64 volume = 3; //成交量
} }
...@@ -131,8 +131,8 @@ message RepCollateralizeCurrentInfo { ...@@ -131,8 +131,8 @@ message RepCollateralizeCurrentInfo {
int32 status = 1;//当期借贷的状态,是否关闭 int32 status = 1;//当期借贷的状态,是否关闭
int64 totalBalance = 2; //当期可借贷的总金额(ccny) int64 totalBalance = 2; //当期可借贷的总金额(ccny)
int64 debtCeiling = 3; //单用户可借出的限额(ccny) int64 debtCeiling = 3; //单用户可借出的限额(ccny)
double liquidationRatio = 4; //清算比例 int64 liquidationRatio = 4; //清算比例
double stabilityFeeRatio = 5; //稳定费 int64 stabilityFeeRatio = 5; //稳定费
string createAddr = 6; //创建人地址 string createAddr = 6; //创建人地址
int64 balance = 7; //剩余可借贷金额(ccny) int64 balance = 7; //剩余可借贷金额(ccny)
int64 period = 8; //合约期限 int64 period = 8; //合约期限
...@@ -192,7 +192,7 @@ message RepCollateralizeRecords { ...@@ -192,7 +192,7 @@ message RepCollateralizeRecords {
// 精确查找借贷记录 // 精确查找借贷记录
message ReqCollateralizeRecord { message ReqCollateralizeRecord {
string collateralizeId = 1; string collateralizeId = 1;
string recordId = 2; string recordId = 2;
} }
// 返回借贷记录 // 返回借贷记录
...@@ -203,8 +203,8 @@ message RepCollateralizeRecord { ...@@ -203,8 +203,8 @@ message RepCollateralizeRecord {
// 返回放贷配置 // 返回放贷配置
message RepCollateralizeConfig { message RepCollateralizeConfig {
int64 debtCeiling = 1; //单用户可借出的限额(ccny) int64 debtCeiling = 1; //单用户可借出的限额(ccny)
double liquidationRatio = 2; //清算比例 int64 liquidationRatio = 2; //清算比例
double stabilityFeeRatio = 3; //稳定费 int64 stabilityFeeRatio = 3; //稳定费
int64 period = 4; //合约期限 int64 period = 4; //合约期限
int64 totalBalance = 5; //放贷总量 int64 totalBalance = 5; //放贷总量
int64 balance = 6; //剩余放贷额度 int64 balance = 6; //剩余放贷额度
...@@ -213,5 +213,5 @@ message RepCollateralizeConfig { ...@@ -213,5 +213,5 @@ message RepCollateralizeConfig {
// 返回最新抵押物价格 // 返回最新抵押物价格
message RepCollateralizePrice { message RepCollateralizePrice {
double price = 1; //当前抵押物最新价格 int64 price = 1; //当前抵押物最新价格
} }
\ No newline at end of file
## 借贷合约表结构 ## 借贷合约表结构
###放贷表coller表结构 ### 放贷表coller表结构
字段名称|类型|说明 字段名称|类型|说明
---|---|--- ---|---|---
collateralizeId|string|放贷ID,主键 collateralizeId|string|放贷ID,主键
...@@ -8,14 +8,14 @@ accountAddr|string|大户地址 ...@@ -8,14 +8,14 @@ accountAddr|string|大户地址
recordId|string|借贷ID recordId|string|借贷ID
status|int32|放贷状态(1:已放贷 2:已收回) status|int32|放贷状态(1:已放贷 2:已收回)
###放贷表coller表索引 ### 放贷表coller表索引
索引名|说明 索引名|说明
---|--- ---|---
status|根据放贷状态查询放贷ID status|根据放贷状态查询放贷ID
addr|根据大户地址查询放贷ID addr|根据大户地址查询放贷ID
addr_status|根据放贷状态和大户地址查询放贷ID addr_status|根据放贷状态和大户地址查询放贷ID
###借贷表borrow表结构 ### 借贷表borrow表结构
字段名称|类型|说明 字段名称|类型|说明
---|---|--- ---|---|---
recordId|string|借贷ID,主键 recordId|string|借贷ID,主键
...@@ -23,7 +23,7 @@ collateralizeId|string|放贷ID ...@@ -23,7 +23,7 @@ collateralizeId|string|放贷ID
accountAddr|string|用户地址 accountAddr|string|用户地址
status|int32|借贷状态(1:已发行 2:价格清算告警 3:价格清算 4:超时清算告警 5:超时清算 6:已清算) status|int32|借贷状态(1:已发行 2:价格清算告警 3:价格清算 4:超时清算告警 5:超时清算 6:已清算)
###放贷表borrow表索引 ### 放贷表borrow表索引
索引名|说明 索引名|说明
---|--- ---|---
status|根据借贷状态查询借贷ID status|根据借贷状态查询借贷ID
......
...@@ -270,9 +270,13 @@ func CreateRawCollateralizeFeedTx(cfg *types.Chain33Config, parm *CollateralizeF ...@@ -270,9 +270,13 @@ func CreateRawCollateralizeFeedTx(cfg *types.Chain33Config, parm *CollateralizeF
} }
v := &CollateralizeFeed{ v := &CollateralizeFeed{
Price: parm.Price,
Volume: parm.Volume, Volume: parm.Volume,
} }
for _, r := range parm.Price {
v.Price = append(v.Price, int64(math.Trunc(r*1e4)))
}
feed := &CollateralizeAction{ feed := &CollateralizeAction{
Ty: CollateralizeActionFeed, Ty: CollateralizeActionFeed,
Value: &CollateralizeAction_Feed{v}, Value: &CollateralizeAction_Feed{v},
...@@ -330,8 +334,8 @@ func CreateRawCollateralizeManageTx(cfg *types.Chain33Config, parm *Collateraliz ...@@ -330,8 +334,8 @@ func CreateRawCollateralizeManageTx(cfg *types.Chain33Config, parm *Collateraliz
v := &CollateralizeManage{ v := &CollateralizeManage{
DebtCeiling: int64(math.Trunc((parm.DebtCeiling+0.0000001)*1e4)) * 1e4, DebtCeiling: int64(math.Trunc((parm.DebtCeiling+0.0000001)*1e4)) * 1e4,
LiquidationRatio: parm.LiquidationRatio, LiquidationRatio: int64(math.Trunc((parm.LiquidationRatio+0.0000001)*1e4)),
StabilityFeeRatio: parm.StabilityFeeRatio, StabilityFeeRatio: int64(math.Trunc((parm.StabilityFeeRatio+0.0000001)*1e4)),
Period: parm.Period, Period: parm.Period,
TotalBalance: int64(math.Trunc((parm.TotalBalance+0.0000001)*1e4)) * 1e4, TotalBalance: int64(math.Trunc((parm.TotalBalance+0.0000001)*1e4)) * 1e4,
} }
......
...@@ -27,7 +27,7 @@ const ( ...@@ -27,7 +27,7 @@ const (
const ( const (
CollateralizeX = "collateralize" CollateralizeX = "collateralize"
CCNYTokenName = "CCNY" CCNYTokenName = "CCNY"
CollateralizePreLiquidationRatio = 1.1 //TODO 预清算比例,抵押物价值跌到借出ccny价值110%的时候开始清算 CollateralizePreLiquidationRatio = 1.1 * 1e4 //TODO 预清算比例,抵押物价值跌到借出ccny价值110%的时候开始清算
) )
//Collateralize status //Collateralize status
......
...@@ -5,8 +5,6 @@ ...@@ -5,8 +5,6 @@
package executor package executor
import ( import (
"math"
"github.com/33cn/chain33/account" "github.com/33cn/chain33/account"
"github.com/33cn/chain33/common" "github.com/33cn/chain33/common"
dbm "github.com/33cn/chain33/common/db" dbm "github.com/33cn/chain33/common/db"
...@@ -27,9 +25,9 @@ const ( ...@@ -27,9 +25,9 @@ const (
const ( const (
Coin = types.Coin // 1e8 Coin = types.Coin // 1e8
DefaultDebtCeiling = 100000 * Coin // 默认借贷限额 DefaultDebtCeiling = 100000 * Coin // 默认借贷限额
DefaultLiquidationRatio = 0.25 // 默认质押比 DefaultLiquidationRatio = 0.25 * 1e4 // 默认质押比
DefaultPeriod = 3600 * 24 * 365 // 默认合约限期 DefaultPeriod = 3600 * 24 * 365 // 默认合约限期
PriceWarningRate = 1.3 // 价格提前预警率 PriceWarningRate = 1.3 * 1e4 // 价格提前预警率
ExpireWarningTime = 3600 * 24 * 10 // 提前10天超时预警 ExpireWarningTime = 3600 * 24 * 10 // 提前10天超时预警
) )
...@@ -266,8 +264,8 @@ func (action *Action) GetIndex() int64 { ...@@ -266,8 +264,8 @@ func (action *Action) GetIndex() int64 {
return action.height*types.MaxTxsPerBlock + int64(action.index) return action.height*types.MaxTxsPerBlock + int64(action.index)
} }
func getLatestLiquidationPrice(issu *pty.Issuance) float64 { func getLatestLiquidationPrice(issu *pty.Issuance) int64 {
var latest float64 var latest int64
for _, collRecord := range issu.DebtRecords { for _, collRecord := range issu.DebtRecords {
if collRecord.LiquidationPrice > latest { if collRecord.LiquidationPrice > latest {
latest = collRecord.LiquidationPrice latest = collRecord.LiquidationPrice
...@@ -431,19 +429,18 @@ func (action *Action) IssuanceCreate(create *pty.IssuanceCreate) (*types.Receipt ...@@ -431,19 +429,18 @@ func (action *Action) IssuanceCreate(create *pty.IssuanceCreate) (*types.Receipt
} }
// 根据最近抵押物价格计算需要冻结的BTY数量 // 根据最近抵押物价格计算需要冻结的BTY数量
func getBtyNumToFrozen(value int64, price float64, ratio float64) (int64, error) { func getBtyNumToFrozen(value int64, price int64, ratio int64) (int64, error) {
if price == 0 { if price == 0 {
clog.Error("Bty price should greate to 0") clog.Error("Bty price should greate to 0")
return 0, pty.ErrPriceInvalid return 0, pty.ErrPriceInvalid
} }
valueReal := float64(value) / 1e8 btyValue := (value * 1e4) / (price * ratio)
btyValue := valueReal / (price * ratio) return btyValue * 1e4, nil
return int64(math.Trunc((btyValue+0.0000001)*1e4)) * 1e4, nil
} }
// 获取最近抵押物价格 // 获取最近抵押物价格
func getLatestPrice(db dbm.KV) (float64, error) { func getLatestPrice(db dbm.KV) (int64, error) {
data, err := db.Get(PriceKey()) data, err := db.Get(PriceKey())
if err != nil { if err != nil {
clog.Error("getLatestPrice", "get", err) clog.Error("getLatestPrice", "get", err)
...@@ -584,7 +581,7 @@ func (action *Action) IssuanceDebt(debt *pty.IssuanceDebt) (*types.Receipt, erro ...@@ -584,7 +581,7 @@ func (action *Action) IssuanceDebt(debt *pty.IssuanceDebt) (*types.Receipt, erro
debtRecord.StartTime = action.blocktime debtRecord.StartTime = action.blocktime
debtRecord.CollateralPrice = lastPrice debtRecord.CollateralPrice = lastPrice
debtRecord.DebtValue = debt.Value debtRecord.DebtValue = debt.Value
debtRecord.LiquidationPrice = math.Trunc(issu.LiquidationRatio*lastPrice*pty.IssuancePreLiquidationRatio*1e4) / 1e4 debtRecord.LiquidationPrice = (issu.LiquidationRatio*lastPrice*pty.IssuancePreLiquidationRatio) / 1e8
debtRecord.Status = pty.IssuanceUserStatusCreate debtRecord.Status = pty.IssuanceUserStatusCreate
debtRecord.ExpireTime = action.blocktime + issu.Period debtRecord.ExpireTime = action.blocktime + issu.Period
...@@ -702,12 +699,12 @@ func (action *Action) IssuanceRepay(repay *pty.IssuanceRepay) (*types.Receipt, e ...@@ -702,12 +699,12 @@ func (action *Action) IssuanceRepay(repay *pty.IssuanceRepay) (*types.Receipt, e
} }
// 系统清算 // 系统清算
func (action *Action) systemLiquidation(issu *pty.Issuance, price float64) (*types.Receipt, error) { func (action *Action) systemLiquidation(issu *pty.Issuance, price int64) (*types.Receipt, error) {
var logs []*types.ReceiptLog var logs []*types.ReceiptLog
var kv []*types.KeyValue var kv []*types.KeyValue
for index, debtRecord := range issu.DebtRecords { for index, debtRecord := range issu.DebtRecords {
if debtRecord.LiquidationPrice*PriceWarningRate < price { if (debtRecord.LiquidationPrice*PriceWarningRate)/1e4 < price {
if debtRecord.Status == pty.IssuanceUserStatusWarning { if debtRecord.Status == pty.IssuanceUserStatusWarning {
debtRecord.PreStatus = debtRecord.Status debtRecord.PreStatus = debtRecord.Status
debtRecord.Status = pty.IssuanceUserStatusCreate debtRecord.Status = pty.IssuanceUserStatusCreate
...@@ -814,8 +811,8 @@ func (action *Action) expireLiquidation(issu *pty.Issuance) (*types.Receipt, err ...@@ -814,8 +811,8 @@ func (action *Action) expireLiquidation(issu *pty.Issuance) (*types.Receipt, err
} }
// 价格计算策略 // 价格计算策略
func pricePolicy(feed *pty.IssuanceFeed) float64 { func pricePolicy(feed *pty.IssuanceFeed) int64 {
var totalPrice float64 var totalPrice int64
var totalVolume int64 var totalVolume int64
for _, volume := range feed.Volume { for _, volume := range feed.Volume {
totalVolume += volume totalVolume += volume
...@@ -826,7 +823,7 @@ func pricePolicy(feed *pty.IssuanceFeed) float64 { ...@@ -826,7 +823,7 @@ func pricePolicy(feed *pty.IssuanceFeed) float64 {
return 0 return 0
} }
for i, price := range feed.Price { for i, price := range feed.Price {
totalPrice += price * (float64(feed.Volume[i]) / float64(totalVolume)) totalPrice += (price * feed.Volume[i]) / totalVolume
} }
return totalPrice return totalPrice
......
...@@ -7,13 +7,13 @@ message Issuance { ...@@ -7,13 +7,13 @@ message Issuance {
string issuanceId = 1; //发行ID,一期发行对应一个ID string issuanceId = 1; //发行ID,一期发行对应一个ID
int64 totalBalance = 2; //当期发行的总金额(ccny) int64 totalBalance = 2; //当期发行的总金额(ccny)
int64 debtCeiling = 3; //单用户可借出的限额(ccny) int64 debtCeiling = 3; //单用户可借出的限额(ccny)
double liquidationRatio = 4; //清算比例 int64 liquidationRatio = 4; //清算比例
int64 collateralValue = 5; //抵押物总数量(bty) int64 collateralValue = 5; //抵押物总数量(bty)
int64 debtValue = 6; //产生的ccny数量 int64 debtValue = 6; //产生的ccny数量
repeated DebtRecord debtRecords = 7; //大户抵押记录 repeated DebtRecord debtRecords = 7; //大户抵押记录
repeated DebtRecord invalidRecords = 8; //大户抵押记录 repeated DebtRecord invalidRecords = 8; //大户抵押记录
int32 status = 9; //当期发行的状态,是否关闭 int32 status = 9; //当期发行的状态,是否关闭
double latestLiquidationPrice = 10;//最高清算价格 int64 latestLiquidationPrice = 10;//最高清算价格
int64 period = 11;//发行最大期限 int64 period = 11;//发行最大期限
int64 latestExpireTime = 12;//最近超期时间 int64 latestExpireTime = 12;//最近超期时间
int64 createTime = 13;//创建时间 int64 createTime = 13;//创建时间
...@@ -26,9 +26,9 @@ message DebtRecord { ...@@ -26,9 +26,9 @@ message DebtRecord {
string accountAddr = 1; //抵押人地址 string accountAddr = 1; //抵押人地址
int64 startTime = 2; //抵押时间 int64 startTime = 2; //抵押时间
int64 collateralValue = 3; //抵押物价值(bty) int64 collateralValue = 3; //抵押物价值(bty)
double collateralPrice = 4; //抵押物价格 int64 collateralPrice = 4; //抵押物价格
int64 debtValue = 5; //债务价值(ccny) int64 debtValue = 5; //债务价值(ccny)
double liquidationPrice = 6; //抵押物清算价格 int64 liquidationPrice = 6; //抵押物清算价格
int32 status = 7; //抵押状态,是否被清算 int32 status = 7; //抵押状态,是否被清算
int64 liquidateTime = 8; //清算时间 int64 liquidateTime = 8; //清算时间
int64 expireTime = 9; //超时清算时间 int64 expireTime = 9; //超时清算时间
...@@ -40,7 +40,7 @@ message DebtRecord { ...@@ -40,7 +40,7 @@ message DebtRecord {
// 资产价格记录 // 资产价格记录
message IssuanceAssetPriceRecord { message IssuanceAssetPriceRecord {
int64 recordTime = 1; //价格记录时间 int64 recordTime = 1; //价格记录时间
double btyPrice = 2; //bty价格 int64 btyPrice = 2; //bty价格
} }
// action // action
...@@ -64,7 +64,7 @@ message IssuanceManage { ...@@ -64,7 +64,7 @@ message IssuanceManage {
message IssuanceCreate { message IssuanceCreate {
int64 totalBalance = 1; //发行总金额 int64 totalBalance = 1; //发行总金额
int64 debtCeiling = 2; //单用户可借出的限额(ccny) int64 debtCeiling = 2; //单用户可借出的限额(ccny)
double liquidationRatio = 3; //清算比例 int64 liquidationRatio = 3; //清算比例
int64 period = 4; //发行最大期限 int64 period = 4; //发行最大期限
} }
...@@ -83,7 +83,7 @@ message IssuanceRepay { ...@@ -83,7 +83,7 @@ message IssuanceRepay {
// 喂价 // 喂价
message IssuanceFeed { message IssuanceFeed {
int32 collType = 1; //抵押物价格类型(1,bty,2,btc,3,eth...) int32 collType = 1; //抵押物价格类型(1,bty,2,btc,3,eth...)
repeated double price = 2; //喂价 repeated int64 price = 2; //喂价
repeated int64 volume = 3; //成交量 repeated int64 volume = 3; //成交量
} }
...@@ -121,7 +121,7 @@ message RepIssuanceCurrentInfo { ...@@ -121,7 +121,7 @@ message RepIssuanceCurrentInfo {
int32 status = 1; //当期发行的状态,是否关闭 int32 status = 1; //当期发行的状态,是否关闭
int64 totalBalance = 2; //当期发行总金额(ccny) int64 totalBalance = 2; //当期发行总金额(ccny)
int64 debtCeiling = 3; //单用户可借出的限额(ccny) int64 debtCeiling = 3; //单用户可借出的限额(ccny)
double liquidationRatio = 4; //清算比例 int64 liquidationRatio = 4; //清算比例
int64 balance = 5; //剩余可借贷金额(ccny) int64 balance = 5; //剩余可借贷金额(ccny)
int64 collateralValue = 6; //抵押物总数量(bty) int64 collateralValue = 6; //抵押物总数量(bty)
int64 debtValue = 7; //产生的ccny数量 int64 debtValue = 7; //产生的ccny数量
...@@ -171,5 +171,5 @@ message RepIssuanceDebtInfo { ...@@ -171,5 +171,5 @@ message RepIssuanceDebtInfo {
// 返回最新抵押物价格 // 返回最新抵押物价格
message RepIssuancePrice { message RepIssuancePrice {
double price = 1; //当前抵押物最新价格 int64 price = 1; //当前抵押物最新价格
} }
\ No newline at end of file
##发行合约表结构 ## 发行合约表结构
###总发行表issuer表结构 ### 总发行表issuer表结构
字段名称|类型|说明 字段名称|类型|说明
---|---|--- ---|---|---
issuanceId|string|总发行ID,主键 issuanceId|string|总发行ID,主键
status|int32|发行状态(1:已发行 2:已下线) status|int32|发行状态(1:已发行 2:已下线)
###总发行表issuer表索引 ### 总发行表issuer表索引
索引名|说明 索引名|说明
---|--- ---|---
status|根据发行状态查询总发行ID status|根据发行状态查询总发行ID
###大户发行表debt表结构 ### 大户发行表debt表结构
字段名称|类型|说明 字段名称|类型|说明
---|---|--- ---|---|---
debtId|string|大户发行ID,主键 debtId|string|大户发行ID,主键
...@@ -19,7 +19,7 @@ issuanceId|string|总发行ID ...@@ -19,7 +19,7 @@ issuanceId|string|总发行ID
accountAddr|string|用户地址 accountAddr|string|用户地址
status|int32|发行状态(1:已发行 2:价格清算告警 3:价格清算 4:超时清算告警 5:超时清算 6:关闭) status|int32|发行状态(1:已发行 2:价格清算告警 3:价格清算 4:超时清算告警 5:超时清算 6:关闭)
###大户发行表debt表索引 ### 大户发行表debt表索引
索引名|说明 索引名|说明
---|--- ---|---
status|根据大户发行状态查询大户发行ID status|根据大户发行状态查询大户发行ID
......
...@@ -146,7 +146,7 @@ func CreateRawIssuanceCreateTx(cfg *types.Chain33Config, parm *IssuanceCreateTx) ...@@ -146,7 +146,7 @@ func CreateRawIssuanceCreateTx(cfg *types.Chain33Config, parm *IssuanceCreateTx)
v := &IssuanceCreate{ v := &IssuanceCreate{
TotalBalance: int64(math.Trunc((parm.TotalBalance+0.0000001)*1e4)) * 1e4, TotalBalance: int64(math.Trunc((parm.TotalBalance+0.0000001)*1e4)) * 1e4,
DebtCeiling: int64(math.Trunc((parm.DebtCeiling+0.0000001)*1e4)) * 1e4, DebtCeiling: int64(math.Trunc((parm.DebtCeiling+0.0000001)*1e4)) * 1e4,
LiquidationRatio: parm.LiquidationRatio, LiquidationRatio: int64(math.Trunc((parm.LiquidationRatio+0.0000001)*1e4)),
Period: parm.Period, Period: parm.Period,
} }
create := &IssuanceAction{ create := &IssuanceAction{
...@@ -233,9 +233,12 @@ func CreateRawIssuanceFeedTx(cfg *types.Chain33Config, parm *IssuanceFeedTx) (*t ...@@ -233,9 +233,12 @@ func CreateRawIssuanceFeedTx(cfg *types.Chain33Config, parm *IssuanceFeedTx) (*t
} }
v := &IssuanceFeed{ v := &IssuanceFeed{
Price: parm.Price,
Volume: parm.Volume, Volume: parm.Volume,
} }
for _, r := range parm.Price {
v.Price = append(v.Price, int64(math.Trunc(r*1e4)))
}
feed := &IssuanceAction{ feed := &IssuanceAction{
Ty: IssuanceActionFeed, Ty: IssuanceActionFeed,
Value: &IssuanceAction_Feed{v}, Value: &IssuanceAction_Feed{v},
......
This diff is collapsed.
...@@ -25,7 +25,7 @@ const ( ...@@ -25,7 +25,7 @@ const (
const ( const (
IssuanceX = "issuance" IssuanceX = "issuance"
CCNYTokenName = "CCNY" CCNYTokenName = "CCNY"
IssuancePreLiquidationRatio = 1.1 //TODO 预清算比例,抵押物价值跌到借出ccny价值110%的时候开始清算 IssuancePreLiquidationRatio = 11000 //TODO 预清算比例,抵押物价值跌到借出ccny价值110%的时候开始清算
) )
//Issuance status //Issuance status
......
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