本文整理汇总了Golang中github.com/skycoin/skycoin-exchange/src/server/engine.Exchange.AddOrder方法的典型用法代码示例。如果您正苦于以下问题:Golang Exchange.AddOrder方法的具体用法?Golang Exchange.AddOrder怎么用?Golang Exchange.AddOrder使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/skycoin/skycoin-exchange/src/server/engine.Exchange
的用法示例。
在下文中一共展示了Exchange.AddOrder方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: CreateOrder
// CreateOrder create specifc order.
func CreateOrder(egn engine.Exchange) sknet.HandlerFunc {
return func(c *sknet.Context) error {
rlt := &pp.EmptyRes{}
req := &pp.OrderReq{}
for {
if err := c.BindJSON(req); err != nil {
rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongRequest)
logger.Error(err.Error())
break
}
// validate pubkey
pubkey := req.GetPubkey()
if err := validatePubkey(pubkey); err != nil {
logger.Error(err.Error())
rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongPubkey)
break
}
// get order type
op, err := order.TypeFromStr(req.GetType())
if err != nil {
rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongRequest)
logger.Error(err.Error())
break
}
// find the account
acnt, err := egn.GetAccount(pubkey)
if err != nil {
rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongPubkey)
logger.Error(err.Error())
break
}
cp, bal, err := needBalance(op, req)
if err != nil {
rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongRequest)
logger.Error(err.Error())
break
}
if acnt.GetBalance(cp) < bal {
err := fmt.Errorf("%s balance is not sufficient", cp)
rlt = pp.MakeErrRes(err)
logger.Debug(err.Error())
break
}
var success bool
if op == order.Bid {
defer func() {
if success {
egn.SaveAccount()
} else {
acnt.IncreaseBalance(cp, bal)
}
}()
// decrease the balance, in case of double use the coins.
logger.Info("account:%s decrease %s:%d", acnt.GetID(), cp, bal)
if err := acnt.DecreaseBalance(cp, bal); err != nil {
rlt = pp.MakeErrRes(err)
logger.Error(err.Error())
break
}
}
odr := order.New(pubkey, op, req.GetPrice(), req.GetAmount())
oid, err := egn.AddOrder(req.GetCoinPair(), *odr)
if err != nil {
logger.Error(err.Error())
rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongRequest)
break
}
success = true
logger.Info(fmt.Sprintf("new %s order:%d", op, oid))
res := pp.OrderRes{
Result: pp.MakeResultWithCode(pp.ErrCode_Success),
OrderId: &oid,
}
return c.SendJSON(&res)
}
return c.Error(rlt)
}
}