Commit bdf8e51e authored by vipwzw's avatar vipwzw

auto ci

parent 5d9da776
...@@ -7,4 +7,3 @@ strapp=${strcmd%/cmd*} ...@@ -7,4 +7,3 @@ strapp=${strcmd%/cmd*}
OUT_TESTDIR="${1}/dapptest/$strapp" OUT_TESTDIR="${1}/dapptest/$strapp"
mkdir -p "${OUT_TESTDIR}" mkdir -p "${OUT_TESTDIR}"
cp ./test/test-rpc.sh "${OUT_TESTDIR}" cp ./test/test-rpc.sh "${OUT_TESTDIR}"
...@@ -2,12 +2,13 @@ package commands ...@@ -2,12 +2,13 @@ package commands
import ( import (
"fmt" "fmt"
"github.com/spf13/cobra" "strconv"
jsonrpc "github.com/33cn/chain33/rpc/jsonclient" jsonrpc "github.com/33cn/chain33/rpc/jsonclient"
rpctypes "github.com/33cn/chain33/rpc/types" rpctypes "github.com/33cn/chain33/rpc/types"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
pkt "github.com/33cn/plugin/plugin/dapp/collateralize/types" pkt "github.com/33cn/plugin/plugin/dapp/collateralize/types"
"strconv" "github.com/spf13/cobra"
) )
// CollateralizeCmd 斗牛游戏命令行 // CollateralizeCmd 斗牛游戏命令行
...@@ -439,7 +440,7 @@ func CollateralizeQuery(cmd *cobra.Command, args []string) { ...@@ -439,7 +440,7 @@ func CollateralizeQuery(cmd *cobra.Command, args []string) {
var res pkt.RepCollateralizeRecords var res pkt.RepCollateralizeRecords
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res) ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res)
ctx.Run() ctx.Run()
} else if borrowID != ""{ } else if borrowID != "" {
params.FuncName = "CollateralizeRecordByID" params.FuncName = "CollateralizeRecordByID"
req := &pkt.ReqCollateralizeRecord{ req := &pkt.ReqCollateralizeRecord{
...@@ -472,7 +473,7 @@ func CollateralizeQuery(cmd *cobra.Command, args []string) { ...@@ -472,7 +473,7 @@ func CollateralizeQuery(cmd *cobra.Command, args []string) {
} else if statusStr != "" { } else if statusStr != "" {
params.FuncName = "CollateralizeByStatus" params.FuncName = "CollateralizeByStatus"
req := &pkt.ReqCollateralizeByStatus{Status:int32(status)} req := &pkt.ReqCollateralizeByStatus{Status: int32(status)}
params.Payload = types.MustPBToJSON(req) params.Payload = types.MustPBToJSON(req)
var res pkt.RepCollateralizeIDs var res pkt.RepCollateralizeIDs
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res) ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res)
......
...@@ -63,7 +63,7 @@ func (c *Collateralize) GetDriverName() string { ...@@ -63,7 +63,7 @@ func (c *Collateralize) GetDriverName() string {
func (c *Collateralize) addCollateralizeID(collateralizeId string, index int64) (kvs []*types.KeyValue) { func (c *Collateralize) addCollateralizeID(collateralizeId string, index int64) (kvs []*types.KeyValue) {
key := calcCollateralizeKey(collateralizeId, index) key := calcCollateralizeKey(collateralizeId, index)
record := &pty.CollateralizeRecord{ record := &pty.CollateralizeRecord{
CollateralizeId:collateralizeId, CollateralizeId: collateralizeId,
Index: index, Index: index,
} }
kv := &types.KeyValue{Key: key, Value: types.Encode(record)} kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
...@@ -83,7 +83,7 @@ func (c *Collateralize) deleteCollateralizeID(collateralizeId string, index int6 ...@@ -83,7 +83,7 @@ func (c *Collateralize) deleteCollateralizeID(collateralizeId string, index int6
func (c *Collateralize) addCollateralizeStatus(status int32, collateralizeId string, index int64) (kvs []*types.KeyValue) { func (c *Collateralize) addCollateralizeStatus(status int32, collateralizeId string, index int64) (kvs []*types.KeyValue) {
key := calcCollateralizeStatusKey(status, index) key := calcCollateralizeStatusKey(status, index)
record := &pty.CollateralizeRecord{ record := &pty.CollateralizeRecord{
CollateralizeId:collateralizeId, CollateralizeId: collateralizeId,
Index: index, Index: index,
} }
kv := &types.KeyValue{Key: key, Value: types.Encode(record)} kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
...@@ -103,8 +103,8 @@ func (c *Collateralize) deleteCollateralizeStatus(status int32, index int64) (kv ...@@ -103,8 +103,8 @@ func (c *Collateralize) deleteCollateralizeStatus(status int32, index int64) (kv
func (c *Collateralize) addCollateralizeAddr(addr string, collateralizeId string, status int32, index int64) (kvs []*types.KeyValue) { func (c *Collateralize) addCollateralizeAddr(addr string, collateralizeId string, status int32, index int64) (kvs []*types.KeyValue) {
key := calcCollateralizeAddrKey(addr, index) key := calcCollateralizeAddrKey(addr, index)
record := &pty.CollateralizeRecord{ record := &pty.CollateralizeRecord{
CollateralizeId:collateralizeId, CollateralizeId: collateralizeId,
Status:status, Status: status,
Index: index, Index: index,
} }
kv := &types.KeyValue{Key: key, Value: types.Encode(record)} kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
...@@ -125,8 +125,8 @@ func (c *Collateralize) addCollateralizeRecordStatus(recordStatus int32, collate ...@@ -125,8 +125,8 @@ func (c *Collateralize) addCollateralizeRecordStatus(recordStatus int32, collate
key := calcCollateralizeRecordStatusKey(recordStatus, index) key := calcCollateralizeRecordStatusKey(recordStatus, index)
record := &pty.CollateralizeRecord{ record := &pty.CollateralizeRecord{
CollateralizeId:collateralizeId, CollateralizeId: collateralizeId,
RecordId:recordId, RecordId: recordId,
Index: index, Index: index,
} }
...@@ -147,8 +147,8 @@ func (c *Collateralize) addCollateralizeRecordAddr(recordAddr string, collateral ...@@ -147,8 +147,8 @@ func (c *Collateralize) addCollateralizeRecordAddr(recordAddr string, collateral
key := calcCollateralizeRecordAddrKey(recordAddr, index) key := calcCollateralizeRecordAddrKey(recordAddr, index)
record := &pty.CollateralizeRecord{ record := &pty.CollateralizeRecord{
CollateralizeId:collateralizeId, CollateralizeId: collateralizeId,
RecordId:recordId, RecordId: recordId,
Index: index, Index: index,
} }
......
package executor package executor
import ( import (
"github.com/33cn/chain33/client"
"testing" "testing"
"time" "time"
"github.com/33cn/chain33/client"
"github.com/33cn/chain33/account" "github.com/33cn/chain33/account"
apimock "github.com/33cn/chain33/client/mocks" apimock "github.com/33cn/chain33/client/mocks"
"github.com/33cn/chain33/common" "github.com/33cn/chain33/common"
...@@ -94,7 +95,7 @@ func initEnv() *execEnv { ...@@ -94,7 +95,7 @@ func initEnv() *execEnv {
Addr: string(Nodes[1]), Addr: string(Nodes[1]),
} }
accountBToken := types.Account{ accountBToken := types.Account{
Balance: types.Coin/10, Balance: types.Coin / 10,
Frozen: 0, Frozen: 0,
Addr: string(Nodes[1]), Addr: string(Nodes[1]),
} }
...@@ -115,14 +116,14 @@ func initEnv() *execEnv { ...@@ -115,14 +116,14 @@ func initEnv() *execEnv {
accA.SaveExecAccount(execAddr, &accountA) accA.SaveExecAccount(execAddr, &accountA)
manageKeySet("issuance-manage", accountA.Addr, stateDB) manageKeySet("issuance-manage", accountA.Addr, stateDB)
addrKeySet(accountA.Addr, stateDB) addrKeySet(accountA.Addr, stateDB)
tokenAccA,_ := account.NewAccountDB(cfg, tokenE.GetName(), pkt.CCNYTokenName, stateDB) tokenAccA, _ := account.NewAccountDB(cfg, tokenE.GetName(), pkt.CCNYTokenName, stateDB)
tokenAccA.SaveExecAccount(execAddr, &accountAToken) tokenAccA.SaveExecAccount(execAddr, &accountAToken)
accB := account.NewCoinsAccount(cfg) accB := account.NewCoinsAccount(cfg)
accB.SetDB(stateDB) accB.SetDB(stateDB)
accB.SaveExecAccount(execAddr, &accountB) accB.SaveExecAccount(execAddr, &accountB)
manageKeySet("issuance-price-feed", accountB.Addr, stateDB) manageKeySet("issuance-price-feed", accountB.Addr, stateDB)
tokenAccB,_ := account.NewAccountDB(cfg, tokenE.GetName(), pkt.CCNYTokenName, stateDB) tokenAccB, _ := account.NewAccountDB(cfg, tokenE.GetName(), pkt.CCNYTokenName, stateDB)
tokenAccB.SaveExecAccount(execAddr, &accountBToken) tokenAccB.SaveExecAccount(execAddr, &accountBToken)
accC := account.NewCoinsAccount(cfg) accC := account.NewCoinsAccount(cfg)
...@@ -131,10 +132,10 @@ func initEnv() *execEnv { ...@@ -131,10 +132,10 @@ func initEnv() *execEnv {
manageKeySet("issuance-guarantor", accountC.Addr, stateDB) manageKeySet("issuance-guarantor", accountC.Addr, stateDB)
return &execEnv{ return &execEnv{
blockTime:time.Now().Unix(), blockTime: time.Now().Unix(),
blockHeight:cfg.GetDappFork(pkt.CollateralizeX, "Enable"), blockHeight: cfg.GetDappFork(pkt.CollateralizeX, "Enable"),
difficulty:1539918074, difficulty: 1539918074,
kvdb:kvdb, kvdb: kvdb,
api: api, api: api,
db: stateDB, db: stateDB,
execAddr: execAddr, execAddr: execAddr,
...@@ -185,7 +186,6 @@ func TestCollateralize(t *testing.T) { ...@@ -185,7 +186,6 @@ func TestCollateralize(t *testing.T) {
env.kvdb.Set(kv.Key, kv.Value) env.kvdb.Set(kv.Key, kv.Value)
} }
// collateralize create // collateralize create
p1 := &pkt.CollateralizeCreateTx{ p1 := &pkt.CollateralizeCreateTx{
TotalBalance: 1000, TotalBalance: 1000,
...@@ -216,21 +216,20 @@ func TestCollateralize(t *testing.T) { ...@@ -216,21 +216,20 @@ func TestCollateralize(t *testing.T) {
} }
collateralizeID := createTx.Hash() collateralizeID := createTx.Hash()
// query collateralize by id // query collateralize by id
res, err := exec.Query("CollateralizeInfoByID", types.Encode(&pkt.ReqCollateralizeInfo{CollateralizeId: common.ToHex(collateralizeID),})) res, err := exec.Query("CollateralizeInfoByID", types.Encode(&pkt.ReqCollateralizeInfo{CollateralizeId: common.ToHex(collateralizeID)}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// query collateralize by status // query collateralize by status
res, err = exec.Query("CollateralizeByStatus", types.Encode(&pkt.ReqCollateralizeByStatus{Status:1})) res, err = exec.Query("CollateralizeByStatus", types.Encode(&pkt.ReqCollateralizeByStatus{Status: 1}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// query collateralizes by ids // query collateralizes by ids
var collateralizeIDsS []string var collateralizeIDsS []string
collateralizeIDsS = append(collateralizeIDsS, common.ToHex(collateralizeID)) collateralizeIDsS = append(collateralizeIDsS, common.ToHex(collateralizeID))
res, err = exec.Query("CollateralizeInfoByIDs", types.Encode(&pkt.ReqCollateralizeInfos{CollateralizeIds:collateralizeIDsS})) res, err = exec.Query("CollateralizeInfoByIDs", types.Encode(&pkt.ReqCollateralizeInfos{CollateralizeIds: collateralizeIDsS}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// collateralize price // collateralize price
p2 := &pkt.CollateralizeFeedTx{} p2 := &pkt.CollateralizeFeedTx{}
p2.Price = append(p2.Price, 1) p2.Price = append(p2.Price, 1)
...@@ -265,7 +264,6 @@ func TestCollateralize(t *testing.T) { ...@@ -265,7 +264,6 @@ func TestCollateralize(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// collateralize borrow // collateralize borrow
p4 := &pkt.CollateralizeBorrowTx{ p4 := &pkt.CollateralizeBorrowTx{
CollateralizeID: common.ToHex(collateralizeID), CollateralizeID: common.ToHex(collateralizeID),
...@@ -304,20 +302,19 @@ func TestCollateralize(t *testing.T) { ...@@ -304,20 +302,19 @@ func TestCollateralize(t *testing.T) {
assert.NotNil(t, res) assert.NotNil(t, res)
// query collateralize by status // query collateralize by status
res, err = exec.Query("CollateralizeRecordByStatus", res, err = exec.Query("CollateralizeRecordByStatus",
types.Encode(&pkt.ReqCollateralizeRecordByStatus{CollateralizeId:common.ToHex(collateralizeID), Status:1})) types.Encode(&pkt.ReqCollateralizeRecordByStatus{CollateralizeId: common.ToHex(collateralizeID), Status: 1}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// query collateralize by addr // query collateralize by addr
res, err = exec.Query("CollateralizeRecordByAddr", res, err = exec.Query("CollateralizeRecordByAddr",
types.Encode(&pkt.ReqCollateralizeRecordByAddr{CollateralizeId:common.ToHex(collateralizeID),Addr: string(Nodes[1]), Status:1})) types.Encode(&pkt.ReqCollateralizeRecordByAddr{CollateralizeId: common.ToHex(collateralizeID), Addr: string(Nodes[1]), Status: 1}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// collateralize append // collateralize append
p5 := &pkt.CollateralizeAppendTx{ p5 := &pkt.CollateralizeAppendTx{
CollateralizeID: common.ToHex(collateralizeID), CollateralizeID: common.ToHex(collateralizeID),
RecordID:common.ToHex(borrowID), RecordID: common.ToHex(borrowID),
Value: 100, Value: 100,
} }
createTx, err = pkt.CreateRawCollateralizeAppendTx(env.cfg, p5) createTx, err = pkt.CreateRawCollateralizeAppendTx(env.cfg, p5)
...@@ -352,16 +349,15 @@ func TestCollateralize(t *testing.T) { ...@@ -352,16 +349,15 @@ func TestCollateralize(t *testing.T) {
assert.NotNil(t, res) assert.NotNil(t, res)
// query collateralize by status // query collateralize by status
res, err = exec.Query("CollateralizeRecordByStatus", res, err = exec.Query("CollateralizeRecordByStatus",
types.Encode(&pkt.ReqCollateralizeRecordByStatus{CollateralizeId:common.ToHex(collateralizeID), Status:1})) types.Encode(&pkt.ReqCollateralizeRecordByStatus{CollateralizeId: common.ToHex(collateralizeID), Status: 1}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// query collateralize by addr // query collateralize by addr
res, err = exec.Query("CollateralizeRecordByAddr", res, err = exec.Query("CollateralizeRecordByAddr",
types.Encode(&pkt.ReqCollateralizeRecordByAddr{CollateralizeId:common.ToHex(collateralizeID),Addr: string(Nodes[1]), Status:1})) types.Encode(&pkt.ReqCollateralizeRecordByAddr{CollateralizeId: common.ToHex(collateralizeID), Addr: string(Nodes[1]), Status: 1}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// collateralize repay // collateralize repay
p6 := &pkt.CollateralizeRepayTx{ p6 := &pkt.CollateralizeRepayTx{
CollateralizeID: common.ToHex(collateralizeID), CollateralizeID: common.ToHex(collateralizeID),
...@@ -394,16 +390,15 @@ func TestCollateralize(t *testing.T) { ...@@ -394,16 +390,15 @@ func TestCollateralize(t *testing.T) {
} }
// query collateralize by status // query collateralize by status
res, err = exec.Query("CollateralizeRecordByStatus", res, err = exec.Query("CollateralizeRecordByStatus",
types.Encode(&pkt.ReqCollateralizeRecordByStatus{CollateralizeId:common.ToHex(collateralizeID), Status:6})) types.Encode(&pkt.ReqCollateralizeRecordByStatus{CollateralizeId: common.ToHex(collateralizeID), Status: 6}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// query collateralize by addr // query collateralize by addr
res, err = exec.Query("CollateralizeRecordByAddr", res, err = exec.Query("CollateralizeRecordByAddr",
types.Encode(&pkt.ReqCollateralizeRecordByAddr{CollateralizeId:common.ToHex(collateralizeID),Addr: string(Nodes[1]), Status:6})) types.Encode(&pkt.ReqCollateralizeRecordByAddr{CollateralizeId: common.ToHex(collateralizeID), Addr: string(Nodes[1]), Status: 6}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// collateralize liquidate // collateralize liquidate
p7 := &pkt.CollateralizeBorrowTx{ p7 := &pkt.CollateralizeBorrowTx{
CollateralizeID: common.ToHex(collateralizeID), CollateralizeID: common.ToHex(collateralizeID),
...@@ -465,11 +460,10 @@ func TestCollateralize(t *testing.T) { ...@@ -465,11 +460,10 @@ func TestCollateralize(t *testing.T) {
} }
// query collateralize by status // query collateralize by status
res, err = exec.Query("CollateralizeRecordByStatus", res, err = exec.Query("CollateralizeRecordByStatus",
types.Encode(&pkt.ReqCollateralizeRecordByStatus{CollateralizeId:common.ToHex(collateralizeID), Status:3})) types.Encode(&pkt.ReqCollateralizeRecordByStatus{CollateralizeId: common.ToHex(collateralizeID), Status: 3}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// expire liquidate // expire liquidate
p9 := &pkt.CollateralizeBorrowTx{ p9 := &pkt.CollateralizeBorrowTx{
CollateralizeID: common.ToHex(collateralizeID), CollateralizeID: common.ToHex(collateralizeID),
...@@ -531,15 +525,14 @@ func TestCollateralize(t *testing.T) { ...@@ -531,15 +525,14 @@ func TestCollateralize(t *testing.T) {
} }
// query collateralize by status // query collateralize by status
res, err = exec.Query("CollateralizeRecordByStatus", res, err = exec.Query("CollateralizeRecordByStatus",
types.Encode(&pkt.ReqCollateralizeRecordByStatus{CollateralizeId:common.ToHex(collateralizeID), Status:5})) types.Encode(&pkt.ReqCollateralizeRecordByStatus{CollateralizeId: common.ToHex(collateralizeID), Status: 5}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// collateralize retrieve // collateralize retrieve
p11 := &pkt.CollateralizeRetrieveTx{ p11 := &pkt.CollateralizeRetrieveTx{
CollateralizeID: common.ToHex(collateralizeID), CollateralizeID: common.ToHex(collateralizeID),
Balance:100, Balance: 100,
} }
createTx, err = pkt.CreateRawCollateralizeRetrieveTx(env.cfg, p11) createTx, err = pkt.CreateRawCollateralizeRetrieveTx(env.cfg, p11)
if err != nil { if err != nil {
...@@ -567,7 +560,7 @@ func TestCollateralize(t *testing.T) { ...@@ -567,7 +560,7 @@ func TestCollateralize(t *testing.T) {
env.kvdb.Set(kv.Key, kv.Value) env.kvdb.Set(kv.Key, kv.Value)
} }
// query collateralize by status // query collateralize by status
res, err = exec.Query("CollateralizeByStatus", types.Encode(&pkt.ReqCollateralizeByStatus{Status:1})) res, err = exec.Query("CollateralizeByStatus", types.Encode(&pkt.ReqCollateralizeByStatus{Status: 1}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
} }
......
...@@ -6,6 +6,8 @@ package executor ...@@ -6,6 +6,8 @@ package executor
import ( import (
"fmt" "fmt"
"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"
...@@ -14,7 +16,6 @@ import ( ...@@ -14,7 +16,6 @@ import (
pty "github.com/33cn/plugin/plugin/dapp/collateralize/types" pty "github.com/33cn/plugin/plugin/dapp/collateralize/types"
issuanceE "github.com/33cn/plugin/plugin/dapp/issuance/types" issuanceE "github.com/33cn/plugin/plugin/dapp/issuance/types"
tokenE "github.com/33cn/plugin/plugin/dapp/token/executor" tokenE "github.com/33cn/plugin/plugin/dapp/token/executor"
"math"
) )
// List control // List control
...@@ -109,7 +110,7 @@ func NewCollateralizeAction(c *Collateralize, tx *types.Transaction, index int) ...@@ -109,7 +110,7 @@ func NewCollateralizeAction(c *Collateralize, tx *types.Transaction, index int)
} }
return &Action{ return &Action{
coinsAccount: c.GetCoinsAccount(), tokenAccount:tokenDb, db: c.GetStateDB(), localDB:c.GetLocalDB(), coinsAccount: c.GetCoinsAccount(), tokenAccount: tokenDb, db: c.GetStateDB(), localDB: c.GetLocalDB(),
txhash: hash, fromaddr: fromaddr, blocktime: c.GetBlockTime(), height: c.GetHeight(), txhash: hash, fromaddr: fromaddr, blocktime: c.GetBlockTime(), height: c.GetHeight(),
execaddr: dapp.ExecAddress(string(tx.Execer)), difficulty: c.GetDifficulty(), index: index, Collateralize: c} execaddr: dapp.ExecAddress(string(tx.Execer)), difficulty: c.GetDifficulty(), index: index, Collateralize: c}
} }
...@@ -336,7 +337,6 @@ func getCollateralizeConfig(db dbm.KV) (*pty.CollateralizeManage, error) { ...@@ -336,7 +337,6 @@ func getCollateralizeConfig(db dbm.KV) (*pty.CollateralizeManage, error) {
return &collCfg, nil return &collCfg, nil
} }
func isSuperAddr(addr string, db dbm.KV) bool { func isSuperAddr(addr string, db dbm.KV) bool {
data, err := db.Get(AddrKey()) data, err := db.Get(AddrKey())
if err != nil { if err != nil {
...@@ -482,14 +482,14 @@ func (action *Action) CollateralizeCreate(create *pty.CollateralizeCreate) (*typ ...@@ -482,14 +482,14 @@ 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 float64, ratio float64) (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 valueReal := float64(value) / 1e8
btyValue := valueReal/(price * ratio) btyValue := valueReal / (price * ratio)
return int64(math.Trunc((btyValue+0.0000001)*1e4)) * 1e4, nil return int64(math.Trunc((btyValue+0.0000001)*1e4)) * 1e4, nil
} }
...@@ -703,8 +703,8 @@ func (action *Action) CollateralizeRepay(repay *pty.CollateralizeRepay) (*types. ...@@ -703,8 +703,8 @@ func (action *Action) CollateralizeRepay(repay *pty.CollateralizeRepay) (*types.
} }
// 借贷金额+利息 // 借贷金额+利息
fee := (float64(borrowRecord.DebtValue)/1e8) * float64(coll.StabilityFeeRatio) fee := (float64(borrowRecord.DebtValue) / 1e8) * float64(coll.StabilityFeeRatio)
realRepay := borrowRecord.DebtValue + int64(math.Trunc((fee+0.0000001)*1e4)) * 1e4 realRepay := borrowRecord.DebtValue + int64(math.Trunc((fee+0.0000001)*1e4))*1e4
// 检查 // 检查
if !action.CheckExecTokenAccount(action.fromaddr, realRepay, false) { if !action.CheckExecTokenAccount(action.fromaddr, realRepay, false) {
...@@ -838,7 +838,7 @@ func (action *Action) CollateralizeAppend(cAppend *pty.CollateralizeAppend) (*ty ...@@ -838,7 +838,7 @@ func (action *Action) CollateralizeAppend(cAppend *pty.CollateralizeAppend) (*ty
borrowRecord.CollateralValue += cAppend.CollateralValue borrowRecord.CollateralValue += cAppend.CollateralValue
borrowRecord.CollateralPrice = lastPrice borrowRecord.CollateralPrice = lastPrice
borrowRecord.LiquidationPrice = calcLiquidationPrice(borrowRecord.DebtValue, borrowRecord.CollateralValue) borrowRecord.LiquidationPrice = calcLiquidationPrice(borrowRecord.DebtValue, borrowRecord.CollateralValue)
if borrowRecord.LiquidationPrice * PriceWarningRate < lastPrice { if borrowRecord.LiquidationPrice*PriceWarningRate < lastPrice {
// 告警解除 // 告警解除
if borrowRecord.Status == pty.CollateralizeUserStatusWarning { if borrowRecord.Status == pty.CollateralizeUserStatusWarning {
borrowRecord.PreStatus = borrowRecord.Status borrowRecord.PreStatus = borrowRecord.Status
...@@ -927,7 +927,7 @@ func (action *Action) systemLiquidation(coll *pty.Collateralize, price float64) ...@@ -927,7 +927,7 @@ func (action *Action) systemLiquidation(coll *pty.Collateralize, price float64)
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 < 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
...@@ -990,7 +990,7 @@ func (action *Action) expireLiquidation(coll *pty.Collateralize) (*types.Receipt ...@@ -990,7 +990,7 @@ func (action *Action) expireLiquidation(coll *pty.Collateralize) (*types.Receipt
var kv []*types.KeyValue var kv []*types.KeyValue
for index, borrowRecord := range coll.BorrowRecords { for index, borrowRecord := range coll.BorrowRecords {
if borrowRecord.ExpireTime - ExpireWarningTime > action.blocktime { if borrowRecord.ExpireTime-ExpireWarningTime > action.blocktime {
continue continue
} }
...@@ -1057,7 +1057,7 @@ func pricePolicy(feed *pty.CollateralizeFeed) float64 { ...@@ -1057,7 +1057,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 * (float64(feed.Volume[i]) / float64(totalVolume))
} }
return totalPrice return totalPrice
...@@ -1098,7 +1098,7 @@ func (action *Action) CollateralizeFeed(feed *pty.CollateralizeFeed) (*types.Rec ...@@ -1098,7 +1098,7 @@ func (action *Action) CollateralizeFeed(feed *pty.CollateralizeFeed) (*types.Rec
} }
// 超时清算判断 // 超时清算判断
if coll.LatestExpireTime - ExpireWarningTime <= action.blocktime { if coll.LatestExpireTime-ExpireWarningTime <= action.blocktime {
receipt, err := action.expireLiquidation(coll) receipt, err := action.expireLiquidation(coll)
if err != nil { if err != nil {
clog.Error("CollateralizePriceFeed", "Collateralize ID", coll.CollateralizeId, "expire liquidation error", err) clog.Error("CollateralizePriceFeed", "Collateralize ID", coll.CollateralizeId, "expire liquidation error", err)
...@@ -1248,7 +1248,7 @@ func queryCollateralizeByAddr(localdb dbm.Lister, addr string, status int32, ind ...@@ -1248,7 +1248,7 @@ func queryCollateralizeByAddr(localdb dbm.Lister, addr string, status int32, ind
clog.Debug("queryCollateralizesByAddr", "decode", err) clog.Debug("queryCollateralizesByAddr", "decode", err)
return nil, err return nil, err
} }
if status ==0 || coll.Status == status { if status == 0 || coll.Status == status {
ids = append(ids, coll.CollateralizeId) ids = append(ids, coll.CollateralizeId)
} }
} }
......
...@@ -10,7 +10,7 @@ import ( ...@@ -10,7 +10,7 @@ import (
) )
func (c *Collateralize) Query_CollateralizeInfoByID(req *pty.ReqCollateralizeInfo) (types.Message, error) { func (c *Collateralize) Query_CollateralizeInfoByID(req *pty.ReqCollateralizeInfo) (types.Message, error) {
coll,err := queryCollateralizeByID(c.GetStateDB(), req.CollateralizeId) coll, err := queryCollateralizeByID(c.GetStateDB(), req.CollateralizeId)
if err != nil { if err != nil {
clog.Error("Query_CollateralizeInfoByID", "id", req.CollateralizeId, "error", err) clog.Error("Query_CollateralizeInfoByID", "id", req.CollateralizeId, "error", err)
return nil, err return nil, err
...@@ -37,7 +37,7 @@ func (c *Collateralize) Query_CollateralizeInfoByID(req *pty.ReqCollateralizeInf ...@@ -37,7 +37,7 @@ func (c *Collateralize) Query_CollateralizeInfoByID(req *pty.ReqCollateralizeInf
func (c *Collateralize) Query_CollateralizeInfoByIDs(req *pty.ReqCollateralizeInfos) (types.Message, error) { func (c *Collateralize) Query_CollateralizeInfoByIDs(req *pty.ReqCollateralizeInfos) (types.Message, error) {
infos := &pty.RepCollateralizeCurrentInfos{} infos := &pty.RepCollateralizeCurrentInfos{}
for _, id := range req.CollateralizeIds { for _, id := range req.CollateralizeIds {
coll,err := queryCollateralizeByID(c.GetStateDB(), id) coll, err := queryCollateralizeByID(c.GetStateDB(), id)
if err != nil { if err != nil {
clog.Error("Query_CollateralizeInfoByID", "id", id, "error", err) clog.Error("Query_CollateralizeInfoByID", "id", id, "error", err)
return nil, err return nil, err
...@@ -111,7 +111,7 @@ func (c *Collateralize) Query_CollateralizeRecordByAddr(req *pty.ReqCollateraliz ...@@ -111,7 +111,7 @@ func (c *Collateralize) Query_CollateralizeRecordByAddr(req *pty.ReqCollateraliz
if req.Status == 0 { if req.Status == 0 {
ret.Records = records ret.Records = records
} else { } else {
for _,record := range records { for _, record := range records {
if record.Status == req.Status { if record.Status == req.Status {
ret.Records = append(ret.Records, record) ret.Records = append(ret.Records, record)
} }
...@@ -146,7 +146,7 @@ func (c *Collateralize) Query_CollateralizeConfig(req *pty.ReqCollateralizeRecor ...@@ -146,7 +146,7 @@ func (c *Collateralize) Query_CollateralizeConfig(req *pty.ReqCollateralizeRecor
} }
ret := &pty.RepCollateralizeConfig{ ret := &pty.RepCollateralizeConfig{
TotalBalance:config.TotalBalance, TotalBalance: config.TotalBalance,
DebtCeiling: config.DebtCeiling, DebtCeiling: config.DebtCeiling,
LiquidationRatio: config.LiquidationRatio, LiquidationRatio: config.LiquidationRatio,
StabilityFeeRatio: config.StabilityFeeRatio, StabilityFeeRatio: config.StabilityFeeRatio,
...@@ -165,5 +165,5 @@ func (c *Collateralize) Query_CollateralizePrice(req *pty.ReqCollateralizeRecord ...@@ -165,5 +165,5 @@ func (c *Collateralize) Query_CollateralizePrice(req *pty.ReqCollateralizeRecord
return nil, err return nil, err
} }
return &pty.RepCollateralizePrice{Price:price}, nil return &pty.RepCollateralizePrice{Price: price}, nil
} }
...@@ -212,7 +212,7 @@ func CreateRawCollateralizeRepayTx(cfg *types.Chain33Config, parm *Collateralize ...@@ -212,7 +212,7 @@ func CreateRawCollateralizeRepayTx(cfg *types.Chain33Config, parm *Collateralize
v := &CollateralizeRepay{ v := &CollateralizeRepay{
CollateralizeId: parm.CollateralizeID, CollateralizeId: parm.CollateralizeID,
RecordId:parm.RecordID, RecordId: parm.RecordID,
} }
repay := &CollateralizeAction{ repay := &CollateralizeAction{
Ty: CollateralizeActionRepay, Ty: CollateralizeActionRepay,
...@@ -241,7 +241,7 @@ func CreateRawCollateralizeAppendTx(cfg *types.Chain33Config, parm *Collateraliz ...@@ -241,7 +241,7 @@ func CreateRawCollateralizeAppendTx(cfg *types.Chain33Config, parm *Collateraliz
v := &CollateralizeAppend{ v := &CollateralizeAppend{
CollateralizeId: parm.CollateralizeID, CollateralizeId: parm.CollateralizeID,
RecordId:parm.RecordID, RecordId: parm.RecordID,
CollateralValue: int64(math.Trunc((parm.Value+0.0000001)*1e4)) * 1e4, CollateralValue: int64(math.Trunc((parm.Value+0.0000001)*1e4)) * 1e4,
} }
append := &CollateralizeAction{ append := &CollateralizeAction{
......
...@@ -388,18 +388,18 @@ manage() { ...@@ -388,18 +388,18 @@ manage() {
echo "========== # issuance add issuance-price-feed end ==========" echo "========== # issuance add issuance-price-feed end =========="
chain33_BlockWait 1 ${MAIN_HTTP} chain33_BlockWait 1 ${MAIN_HTTP}
# echo "========== # issuance add issuance-guarantor begin ==========" # echo "========== # issuance add issuance-guarantor begin =========="
# tx=$(curl -ksd '{"method":"Chain33.CreateTransaction","params":[{"execer":"manage","actionName":"Modify","payload":{"key": "issuance-guarantor", "value":"'"${IssuanceAddr3}"'", "op":"add"}}]}' ${MAIN_HTTP} | jq -r ".result") # tx=$(curl -ksd '{"method":"Chain33.CreateTransaction","params":[{"execer":"manage","actionName":"Modify","payload":{"key": "issuance-guarantor", "value":"'"${IssuanceAddr3}"'", "op":"add"}}]}' ${MAIN_HTTP} | jq -r ".result")
# #
# data=$(curl -ksd '{"method":"Chain33.DecodeRawTransaction","params":[{"txHex":"'"$tx"'"}]}' ${MAIN_HTTP} | jq -r ".result.txs[0]") # data=$(curl -ksd '{"method":"Chain33.DecodeRawTransaction","params":[{"txHex":"'"$tx"'"}]}' ${MAIN_HTTP} | jq -r ".result.txs[0]")
# ok=$(jq '(.execer != "")' <<<"$data") # ok=$(jq '(.execer != "")' <<<"$data")
# #
# [ "$ok" == true ] # [ "$ok" == true ]
# echo_rst "$FUNCNAME" "$?" # echo_rst "$FUNCNAME" "$?"
# #
# chain33_SignRawTx "$tx" ${SystemManager} ${MAIN_HTTP} # chain33_SignRawTx "$tx" ${SystemManager} ${MAIN_HTTP}
# echo "========== # issuance add issuance-guarantor end ==========" # echo "========== # issuance add issuance-guarantor end =========="
# chain33_BlockWait 1 ${MAIN_HTTP} # chain33_BlockWait 1 ${MAIN_HTTP}
} }
token() { token() {
......
...@@ -2,12 +2,13 @@ package commands ...@@ -2,12 +2,13 @@ package commands
import ( import (
"fmt" "fmt"
"strconv"
jsonrpc "github.com/33cn/chain33/rpc/jsonclient" jsonrpc "github.com/33cn/chain33/rpc/jsonclient"
rpctypes "github.com/33cn/chain33/rpc/types" rpctypes "github.com/33cn/chain33/rpc/types"
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
pkt "github.com/33cn/plugin/plugin/dapp/issuance/types" pkt "github.com/33cn/plugin/plugin/dapp/issuance/types"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"strconv"
) )
// IssuanceCmd 斗牛游戏命令行 // IssuanceCmd 斗牛游戏命令行
...@@ -369,7 +370,7 @@ func IssuanceQuery(cmd *cobra.Command, args []string) { ...@@ -369,7 +370,7 @@ func IssuanceQuery(cmd *cobra.Command, args []string) {
var res pkt.RepIssuanceRecords var res pkt.RepIssuanceRecords
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res) ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res)
ctx.Run() ctx.Run()
} else if debtID != ""{ } else if debtID != "" {
params.FuncName = "IssuanceRecordByID" params.FuncName = "IssuanceRecordByID"
req := &pkt.ReqIssuanceDebtInfo{ req := &pkt.ReqIssuanceDebtInfo{
...@@ -394,7 +395,7 @@ func IssuanceQuery(cmd *cobra.Command, args []string) { ...@@ -394,7 +395,7 @@ func IssuanceQuery(cmd *cobra.Command, args []string) {
} else if statusStr != "" { } else if statusStr != "" {
params.FuncName = "IssuanceByStatus" params.FuncName = "IssuanceByStatus"
req := &pkt.ReqIssuanceByStatus{Status:int32(status)} req := &pkt.ReqIssuanceByStatus{Status: int32(status)}
params.Payload = types.MustPBToJSON(req) params.Payload = types.MustPBToJSON(req)
var res pkt.RepIssuanceIDs var res pkt.RepIssuanceIDs
ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res) ctx := jsonrpc.NewRPCCtx(rpcLaddr, "Chain33.Query", params, &res)
......
...@@ -63,7 +63,7 @@ func (c *Issuance) GetDriverName() string { ...@@ -63,7 +63,7 @@ func (c *Issuance) GetDriverName() string {
func (c *Issuance) addIssuanceID(index int64, issuanceId string) (kvs []*types.KeyValue) { func (c *Issuance) addIssuanceID(index int64, issuanceId string) (kvs []*types.KeyValue) {
key := calcIssuanceKey(issuanceId, index) key := calcIssuanceKey(issuanceId, index)
record := &pty.IssuanceRecord{ record := &pty.IssuanceRecord{
IssuanceId:issuanceId, IssuanceId: issuanceId,
Index: index, Index: index,
} }
kv := &types.KeyValue{Key: key, Value: types.Encode(record)} kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
...@@ -83,7 +83,7 @@ func (c *Issuance) deleteIssuanceID(index int64, issuanceId string) (kvs []*type ...@@ -83,7 +83,7 @@ func (c *Issuance) deleteIssuanceID(index int64, issuanceId string) (kvs []*type
func (c *Issuance) addIssuanceStatus(status int32, index int64, issuanceId string) (kvs []*types.KeyValue) { func (c *Issuance) addIssuanceStatus(status int32, index int64, issuanceId string) (kvs []*types.KeyValue) {
key := calcIssuanceStatusKey(status, index) key := calcIssuanceStatusKey(status, index)
record := &pty.IssuanceRecord{ record := &pty.IssuanceRecord{
IssuanceId:issuanceId, IssuanceId: issuanceId,
Index: index, Index: index,
} }
kv := &types.KeyValue{Key: key, Value: types.Encode(record)} kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
...@@ -103,7 +103,7 @@ func (c *Issuance) deleteIssuanceStatus(status int32, index int64) (kvs []*types ...@@ -103,7 +103,7 @@ func (c *Issuance) deleteIssuanceStatus(status int32, index int64) (kvs []*types
func (c *Issuance) addIssuanceRecordAddr(accountAddr string, index int64, debtId string, issuanceId string) (kvs []*types.KeyValue) { func (c *Issuance) addIssuanceRecordAddr(accountAddr string, index int64, debtId string, issuanceId string) (kvs []*types.KeyValue) {
key := calcIssuanceRecordAddrKey(accountAddr, index) key := calcIssuanceRecordAddrKey(accountAddr, index)
record := &pty.IssuanceRecord{ record := &pty.IssuanceRecord{
IssuanceId:issuanceId, IssuanceId: issuanceId,
DebtId: debtId, DebtId: debtId,
Index: index, Index: index,
} }
...@@ -125,7 +125,7 @@ func (c *Issuance) addIssuanceRecordStatus(recordStatus int32, accountAddr strin ...@@ -125,7 +125,7 @@ func (c *Issuance) addIssuanceRecordStatus(recordStatus int32, accountAddr strin
key := calcIssuanceRecordStatusKey(recordStatus, index) key := calcIssuanceRecordStatusKey(recordStatus, index)
record := &pty.IssuanceRecord{ record := &pty.IssuanceRecord{
IssuanceId:issuanceId, IssuanceId: issuanceId,
DebtId: debtId, DebtId: debtId,
Addr: accountAddr, Addr: accountAddr,
Index: index, Index: index,
...@@ -148,8 +148,8 @@ func (c *Issuance) addIssuancePriceRecord(recordTime int64, price float64) (kvs ...@@ -148,8 +148,8 @@ func (c *Issuance) addIssuancePriceRecord(recordTime int64, price float64) (kvs
key := calcIssuancePriceKey(string(recordTime)) key := calcIssuancePriceKey(string(recordTime))
record := &pty.IssuanceAssetPriceRecord{ record := &pty.IssuanceAssetPriceRecord{
RecordTime:recordTime, RecordTime: recordTime,
BtyPrice:price, BtyPrice: price,
} }
kv := &types.KeyValue{Key: key, Value: types.Encode(record)} kv := &types.KeyValue{Key: key, Value: types.Encode(record)}
kvs = append(kvs, kv) kvs = append(kvs, kv)
...@@ -159,7 +159,7 @@ func (c *Issuance) addIssuancePriceRecord(recordTime int64, price float64) (kvs ...@@ -159,7 +159,7 @@ func (c *Issuance) addIssuancePriceRecord(recordTime int64, price float64) (kvs
func (c *Issuance) deleteIssuancePriceRecord(recordTime int64) (kvs []*types.KeyValue) { func (c *Issuance) deleteIssuancePriceRecord(recordTime int64) (kvs []*types.KeyValue) {
key := calcIssuancePriceKey(string(recordTime)) key := calcIssuancePriceKey(string(recordTime))
kv := &types.KeyValue{Key: key, Value:nil} kv := &types.KeyValue{Key: key, Value: nil}
kvs = append(kvs, kv) kvs = append(kvs, kv)
return kvs return kvs
} }
......
package executor package executor
import ( import (
"github.com/33cn/chain33/client"
"testing" "testing"
"time" "time"
"github.com/33cn/chain33/client"
"github.com/33cn/chain33/account" "github.com/33cn/chain33/account"
apimock "github.com/33cn/chain33/client/mocks" apimock "github.com/33cn/chain33/client/mocks"
"github.com/33cn/chain33/common" "github.com/33cn/chain33/common"
...@@ -96,7 +97,7 @@ func initEnv() *execEnv { ...@@ -96,7 +97,7 @@ func initEnv() *execEnv {
accA.SetDB(stateDB) accA.SetDB(stateDB)
accA.SaveExecAccount(execAddr, &accountA) accA.SaveExecAccount(execAddr, &accountA)
manageKeySet("issuance-manage", accountA.Addr, stateDB) manageKeySet("issuance-manage", accountA.Addr, stateDB)
tokenAccA,_ := account.NewAccountDB(cfg, tokenE.GetName(), pkt.CCNYTokenName, stateDB) tokenAccA, _ := account.NewAccountDB(cfg, tokenE.GetName(), pkt.CCNYTokenName, stateDB)
tokenAccA.SaveExecAccount(execAddr, &accountAToken) tokenAccA.SaveExecAccount(execAddr, &accountAToken)
accB := account.NewCoinsAccount(cfg) accB := account.NewCoinsAccount(cfg)
...@@ -110,10 +111,10 @@ func initEnv() *execEnv { ...@@ -110,10 +111,10 @@ func initEnv() *execEnv {
manageKeySet("issuance-guarantor", accountC.Addr, stateDB) manageKeySet("issuance-guarantor", accountC.Addr, stateDB)
return &execEnv{ return &execEnv{
blockTime:time.Now().Unix(), blockTime: time.Now().Unix(),
blockHeight:cfg.GetDappFork(pkt.IssuanceX, "Enable"), blockHeight: cfg.GetDappFork(pkt.IssuanceX, "Enable"),
difficulty:1539918074, difficulty: 1539918074,
kvdb:kvdb, kvdb: kvdb,
api: api, api: api,
db: stateDB, db: stateDB,
execAddr: execAddr, execAddr: execAddr,
...@@ -163,21 +164,20 @@ func TestIssuance(t *testing.T) { ...@@ -163,21 +164,20 @@ func TestIssuance(t *testing.T) {
} }
issuanceID := createTx.Hash() issuanceID := createTx.Hash()
// query issuance by id // query issuance by id
res, err := exec.Query("IssuanceInfoByID", types.Encode(&pkt.ReqIssuanceInfo{IssuanceId: common.ToHex(issuanceID),})) res, err := exec.Query("IssuanceInfoByID", types.Encode(&pkt.ReqIssuanceInfo{IssuanceId: common.ToHex(issuanceID)}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// query issuance by status // query issuance by status
res, err = exec.Query("IssuanceByStatus", types.Encode(&pkt.ReqIssuanceByStatus{Status:1})) res, err = exec.Query("IssuanceByStatus", types.Encode(&pkt.ReqIssuanceByStatus{Status: 1}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// query issuances by ids // query issuances by ids
var issuanceIDsS []string var issuanceIDsS []string
issuanceIDsS = append(issuanceIDsS, common.ToHex(issuanceID)) issuanceIDsS = append(issuanceIDsS, common.ToHex(issuanceID))
res, err = exec.Query("IssuanceInfoByIDs", types.Encode(&pkt.ReqIssuanceInfos{IssuanceIds:issuanceIDsS})) res, err = exec.Query("IssuanceInfoByIDs", types.Encode(&pkt.ReqIssuanceInfos{IssuanceIds: issuanceIDsS}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// issuance price // issuance price
p2 := &pkt.IssuanceFeedTx{} p2 := &pkt.IssuanceFeedTx{}
p2.Price = append(p2.Price, 1) p2.Price = append(p2.Price, 1)
...@@ -212,7 +212,6 @@ func TestIssuance(t *testing.T) { ...@@ -212,7 +212,6 @@ func TestIssuance(t *testing.T) {
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// issuance manage // issuance manage
p3 := &pkt.IssuanceManageTx{} p3 := &pkt.IssuanceManageTx{}
p3.Addr = append(p3.Addr, string(Nodes[1])) p3.Addr = append(p3.Addr, string(Nodes[1]))
...@@ -242,7 +241,6 @@ func TestIssuance(t *testing.T) { ...@@ -242,7 +241,6 @@ func TestIssuance(t *testing.T) {
env.kvdb.Set(kv.Key, kv.Value) env.kvdb.Set(kv.Key, kv.Value)
} }
// issuance debt // issuance debt
p4 := &pkt.IssuanceDebtTx{ p4 := &pkt.IssuanceDebtTx{
IssuanceID: common.ToHex(issuanceID), IssuanceID: common.ToHex(issuanceID),
...@@ -281,16 +279,15 @@ func TestIssuance(t *testing.T) { ...@@ -281,16 +279,15 @@ func TestIssuance(t *testing.T) {
assert.NotNil(t, res) assert.NotNil(t, res)
// query issuance by status // query issuance by status
res, err = exec.Query("IssuanceRecordsByStatus", res, err = exec.Query("IssuanceRecordsByStatus",
types.Encode(&pkt.ReqIssuanceRecordsByStatus{IssuanceId:common.ToHex(issuanceID), Status:1})) types.Encode(&pkt.ReqIssuanceRecordsByStatus{IssuanceId: common.ToHex(issuanceID), Status: 1}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// query issuance by addr // query issuance by addr
res, err = exec.Query("IssuanceRecordsByAddr", res, err = exec.Query("IssuanceRecordsByAddr",
types.Encode(&pkt.ReqIssuanceRecordsByAddr{IssuanceId:common.ToHex(issuanceID),Addr: string(Nodes[1]), Status:1})) types.Encode(&pkt.ReqIssuanceRecordsByAddr{IssuanceId: common.ToHex(issuanceID), Addr: string(Nodes[1]), Status: 1}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// issuance repay // issuance repay
p5 := &pkt.IssuanceRepayTx{ p5 := &pkt.IssuanceRepayTx{
IssuanceID: common.ToHex(issuanceID), IssuanceID: common.ToHex(issuanceID),
...@@ -323,16 +320,15 @@ func TestIssuance(t *testing.T) { ...@@ -323,16 +320,15 @@ func TestIssuance(t *testing.T) {
} }
// query issuance by status // query issuance by status
res, err = exec.Query("IssuanceRecordsByStatus", res, err = exec.Query("IssuanceRecordsByStatus",
types.Encode(&pkt.ReqIssuanceRecordsByStatus{IssuanceId:common.ToHex(issuanceID), Status:6})) types.Encode(&pkt.ReqIssuanceRecordsByStatus{IssuanceId: common.ToHex(issuanceID), Status: 6}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// query issuance by addr // query issuance by addr
res, err = exec.Query("IssuanceRecordsByAddr", res, err = exec.Query("IssuanceRecordsByAddr",
types.Encode(&pkt.ReqIssuanceRecordsByAddr{IssuanceId:common.ToHex(issuanceID),Addr: string(Nodes[1]), Status:6})) types.Encode(&pkt.ReqIssuanceRecordsByAddr{IssuanceId: common.ToHex(issuanceID), Addr: string(Nodes[1]), Status: 6}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// issuance liquidate // issuance liquidate
p6 := &pkt.IssuanceDebtTx{ p6 := &pkt.IssuanceDebtTx{
IssuanceID: common.ToHex(issuanceID), IssuanceID: common.ToHex(issuanceID),
...@@ -394,7 +390,7 @@ func TestIssuance(t *testing.T) { ...@@ -394,7 +390,7 @@ func TestIssuance(t *testing.T) {
} }
// query issuance by status // query issuance by status
res, err = exec.Query("IssuanceRecordsByStatus", res, err = exec.Query("IssuanceRecordsByStatus",
types.Encode(&pkt.ReqIssuanceRecordsByStatus{IssuanceId:common.ToHex(issuanceID), Status:3})) types.Encode(&pkt.ReqIssuanceRecordsByStatus{IssuanceId: common.ToHex(issuanceID), Status: 3}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
...@@ -459,11 +455,10 @@ func TestIssuance(t *testing.T) { ...@@ -459,11 +455,10 @@ func TestIssuance(t *testing.T) {
} }
// query issuance by status // query issuance by status
res, err = exec.Query("IssuanceRecordsByStatus", res, err = exec.Query("IssuanceRecordsByStatus",
types.Encode(&pkt.ReqIssuanceRecordsByStatus{IssuanceId:common.ToHex(issuanceID), Status:5})) types.Encode(&pkt.ReqIssuanceRecordsByStatus{IssuanceId: common.ToHex(issuanceID), Status: 5}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
// issuance close // issuance close
p10 := &pkt.IssuanceCloseTx{ p10 := &pkt.IssuanceCloseTx{
IssuanceID: common.ToHex(issuanceID), IssuanceID: common.ToHex(issuanceID),
...@@ -494,7 +489,7 @@ func TestIssuance(t *testing.T) { ...@@ -494,7 +489,7 @@ func TestIssuance(t *testing.T) {
env.kvdb.Set(kv.Key, kv.Value) env.kvdb.Set(kv.Key, kv.Value)
} }
// query issuance by status // query issuance by status
res, err = exec.Query("IssuanceByStatus", types.Encode(&pkt.ReqIssuanceByStatus{Status:2})) res, err = exec.Query("IssuanceByStatus", types.Encode(&pkt.ReqIssuanceByStatus{Status: 2}))
assert.Nil(t, err) assert.Nil(t, err)
assert.NotNil(t, res) assert.NotNil(t, res)
} }
......
...@@ -6,6 +6,8 @@ package executor ...@@ -6,6 +6,8 @@ package executor
import ( import (
"fmt" "fmt"
"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"
...@@ -13,7 +15,6 @@ import ( ...@@ -13,7 +15,6 @@ import (
"github.com/33cn/chain33/types" "github.com/33cn/chain33/types"
pty "github.com/33cn/plugin/plugin/dapp/issuance/types" pty "github.com/33cn/plugin/plugin/dapp/issuance/types"
tokenE "github.com/33cn/plugin/plugin/dapp/token/executor" tokenE "github.com/33cn/plugin/plugin/dapp/token/executor"
"math"
) )
// List control // List control
...@@ -180,7 +181,7 @@ func NewIssuanceAction(c *Issuance, tx *types.Transaction, index int) *Action { ...@@ -180,7 +181,7 @@ func NewIssuanceAction(c *Issuance, tx *types.Transaction, index int) *Action {
} }
return &Action{ return &Action{
coinsAccount: c.GetCoinsAccount(), tokenAccount:tokenDb, db: c.GetStateDB(), localDB:c.GetLocalDB(), coinsAccount: c.GetCoinsAccount(), tokenAccount: tokenDb, db: c.GetStateDB(), localDB: c.GetLocalDB(),
txhash: hash, fromaddr: fromaddr, blocktime: c.GetBlockTime(), height: c.GetHeight(), txhash: hash, fromaddr: fromaddr, blocktime: c.GetBlockTime(), height: c.GetHeight(),
execaddr: dapp.ExecAddress(string(tx.Execer)), difficulty: c.GetDifficulty(), index: index, Issuance: c} execaddr: dapp.ExecAddress(string(tx.Execer)), difficulty: c.GetDifficulty(), index: index, Issuance: c}
} }
...@@ -339,7 +340,7 @@ func (action *Action) IssuanceManage(manage *pty.IssuanceManage) (*types.Receipt ...@@ -339,7 +340,7 @@ func (action *Action) IssuanceManage(manage *pty.IssuanceManage) (*types.Receipt
value := types.Encode(&item) value := types.Encode(&item)
action.db.Set(AddrKey(), value) action.db.Set(AddrKey(), value)
kv = append(kv, &types.KeyValue{Key:AddrKey(), Value: value}) kv = append(kv, &types.KeyValue{Key: AddrKey(), Value: value})
} else { } else {
err = types.Decode(data, &item) err = types.Decode(data, &item)
if err != nil { if err != nil {
...@@ -349,7 +350,7 @@ func (action *Action) IssuanceManage(manage *pty.IssuanceManage) (*types.Receipt ...@@ -349,7 +350,7 @@ func (action *Action) IssuanceManage(manage *pty.IssuanceManage) (*types.Receipt
item.GetArr().Value = append(item.GetArr().Value, manage.SuperAddrs...) item.GetArr().Value = append(item.GetArr().Value, manage.SuperAddrs...)
value := types.Encode(&item) value := types.Encode(&item)
action.db.Set(AddrKey(), value) action.db.Set(AddrKey(), value)
kv = append(kv, &types.KeyValue{Key:AddrKey(), Value: value}) kv = append(kv, &types.KeyValue{Key: AddrKey(), Value: value})
} }
receipt = &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: nil} receipt = &types.Receipt{Ty: types.ExecOk, KV: kv, Logs: nil}
...@@ -456,14 +457,14 @@ func (action *Action) IssuanceCreate(create *pty.IssuanceCreate) (*types.Receipt ...@@ -456,14 +457,14 @@ 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 float64, ratio float64) (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 valueReal := float64(value) / 1e8
btyValue := valueReal/(price * ratio) btyValue := valueReal / (price * ratio)
return int64(math.Trunc((btyValue+0.0000001)*1e4)) * 1e4, nil return int64(math.Trunc((btyValue+0.0000001)*1e4)) * 1e4, nil
} }
...@@ -735,7 +736,7 @@ func (action *Action) systemLiquidation(issu *pty.Issuance, price float64) (*typ ...@@ -735,7 +736,7 @@ func (action *Action) systemLiquidation(issu *pty.Issuance, price float64) (*typ
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 < 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
...@@ -797,7 +798,7 @@ func (action *Action) expireLiquidation(issu *pty.Issuance) (*types.Receipt, err ...@@ -797,7 +798,7 @@ func (action *Action) expireLiquidation(issu *pty.Issuance) (*types.Receipt, err
var kv []*types.KeyValue var kv []*types.KeyValue
for index, debtRecord := range issu.DebtRecords { for index, debtRecord := range issu.DebtRecords {
if debtRecord.ExpireTime - ExpireWarningTime > action.blocktime { if debtRecord.ExpireTime-ExpireWarningTime > action.blocktime {
continue continue
} }
...@@ -862,7 +863,7 @@ func pricePolicy(feed *pty.IssuanceFeed) float64 { ...@@ -862,7 +863,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 * (float64(feed.Volume[i]) / float64(totalVolume))
} }
return totalPrice return totalPrice
...@@ -903,7 +904,7 @@ func (action *Action) IssuanceFeed(feed *pty.IssuanceFeed) (*types.Receipt, erro ...@@ -903,7 +904,7 @@ func (action *Action) IssuanceFeed(feed *pty.IssuanceFeed) (*types.Receipt, erro
} }
// 超时清算判断 // 超时清算判断
if issu.LatestExpireTime - ExpireWarningTime <= action.blocktime { if issu.LatestExpireTime-ExpireWarningTime <= action.blocktime {
receipt, err := action.expireLiquidation(issu) receipt, err := action.expireLiquidation(issu)
if err != nil { if err != nil {
clog.Error("IssuancePriceFeed", "Issuance ID", issu.IssuanceId, "expire liquidation error", err) clog.Error("IssuancePriceFeed", "Issuance ID", issu.IssuanceId, "expire liquidation error", err)
......
...@@ -10,7 +10,7 @@ import ( ...@@ -10,7 +10,7 @@ import (
) )
func (c *Issuance) Query_IssuanceInfoByID(req *pty.ReqIssuanceInfo) (types.Message, error) { func (c *Issuance) Query_IssuanceInfoByID(req *pty.ReqIssuanceInfo) (types.Message, error) {
issu,err := queryIssuanceByID(c.GetStateDB(), req.IssuanceId) issu, err := queryIssuanceByID(c.GetStateDB(), req.IssuanceId)
if err != nil { if err != nil {
clog.Error("Query_IssuanceInfoByID", "id", req.IssuanceId, "error", err) clog.Error("Query_IssuanceInfoByID", "id", req.IssuanceId, "error", err)
return nil, err return nil, err
...@@ -33,7 +33,7 @@ func (c *Issuance) Query_IssuanceInfoByID(req *pty.ReqIssuanceInfo) (types.Messa ...@@ -33,7 +33,7 @@ func (c *Issuance) Query_IssuanceInfoByID(req *pty.ReqIssuanceInfo) (types.Messa
func (c *Issuance) Query_IssuanceInfoByIDs(req *pty.ReqIssuanceInfos) (types.Message, error) { func (c *Issuance) Query_IssuanceInfoByIDs(req *pty.ReqIssuanceInfos) (types.Message, error) {
infos := &pty.RepIssuanceCurrentInfos{} infos := &pty.RepIssuanceCurrentInfos{}
for _, id := range req.IssuanceIds { for _, id := range req.IssuanceIds {
issu,err := queryIssuanceByID(c.GetStateDB(), id) issu, err := queryIssuanceByID(c.GetStateDB(), id)
if err != nil { if err != nil {
clog.Error("Query_IssuanceInfoByID", "id", id, "error", err) clog.Error("Query_IssuanceInfoByID", "id", id, "error", err)
return nil, err return nil, err
...@@ -91,7 +91,7 @@ func (c *Issuance) Query_IssuanceRecordsByAddr(req *pty.ReqIssuanceRecordsByAddr ...@@ -91,7 +91,7 @@ func (c *Issuance) Query_IssuanceRecordsByAddr(req *pty.ReqIssuanceRecordsByAddr
if req.Status == 0 { if req.Status == 0 {
ret.Records = records ret.Records = records
} else { } else {
for _,record := range records { for _, record := range records {
if record.Status == req.Status { if record.Status == req.Status {
ret.Records = append(ret.Records, record) ret.Records = append(ret.Records, record)
} }
...@@ -120,5 +120,5 @@ func (c *Issuance) Query_IssuancePrice(req *pty.ReqIssuanceRecordsByStatus) (typ ...@@ -120,5 +120,5 @@ func (c *Issuance) Query_IssuancePrice(req *pty.ReqIssuanceRecordsByStatus) (typ
return nil, err return nil, err
} }
return &pty.RepIssuancePrice{Price:price}, nil return &pty.RepIssuancePrice{Price: price}, nil
} }
...@@ -290,7 +290,7 @@ func CreateRawIssuanceManageTx(cfg *types.Chain33Config, parm *IssuanceManageTx) ...@@ -290,7 +290,7 @@ func CreateRawIssuanceManageTx(cfg *types.Chain33Config, parm *IssuanceManageTx)
return nil, types.ErrInvalidParam return nil, types.ErrInvalidParam
} }
v := &IssuanceManage{SuperAddrs:parm.Addr} v := &IssuanceManage{SuperAddrs: parm.Addr}
manage := &IssuanceAction{ manage := &IssuanceAction{
Ty: IssuanceActionManage, Ty: IssuanceActionManage,
......
This diff is collapsed.
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