当前位置: 首页>>代码示例>>Golang>>正文


Golang Exchange.SaveAccount方法代码示例

本文整理汇总了Golang中github.com/skycoin/skycoin-exchange/src/server/engine.Exchange.SaveAccount方法的典型用法代码示例。如果您正苦于以下问题:Golang Exchange.SaveAccount方法的具体用法?Golang Exchange.SaveAccount怎么用?Golang Exchange.SaveAccount使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/skycoin/skycoin-exchange/src/server/engine.Exchange的用法示例。


在下文中一共展示了Exchange.SaveAccount方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: UpdateCredit

// UpdateCredit update credit.
func UpdateCredit(ee engine.Exchange) sknet.HandlerFunc {
	return func(c *sknet.Context) error {
		var rlt *pp.EmptyRes
		for {
			req := pp.UpdateCreditReq{}
			if err := c.BindJSON(&req); err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongRequest)
				break
			}

			// validate the dst pubkey.
			dstPubkey := req.GetDst()
			if err := validatePubkey(dstPubkey); err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongPubkey)
				break
			}

			// get account.
			a, err := ee.GetAccount(dstPubkey)
			if err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongPubkey)
				break
			}

			// get coin type.
			if err := a.SetBalance(req.GetCoinType(), req.GetAmount()); err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}
			ee.SaveAccount()
			res := pp.UpdateCreditRes{
				Result: pp.MakeResultWithCode(pp.ErrCode_Success),
			}

			return c.SendJSON(&res)
		}
		return c.Error(rlt)
	}
}
开发者ID:skycoin,项目名称:skycoin-exchange,代码行数:44,代码来源:admin.go

示例2: 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)
	}
}
开发者ID:skycoin,项目名称:skycoin-exchange,代码行数:86,代码来源:order.go


注:本文中的github.com/skycoin/skycoin-exchange/src/server/engine.Exchange.SaveAccount方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。