本文整理汇总了Golang中github.com/skycoin/skycoin-exchange/src/server/engine.Exchange类的典型用法代码示例。如果您正苦于以下问题:Golang Exchange类的具体用法?Golang Exchange怎么用?Golang Exchange使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Exchange类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: New
// New create sknet engine and register handlers.
func New(ee engine.Exchange, quit chan bool) *sknet.Engine {
engine := sknet.New(ee.GetSecKey(), quit)
engine.Use(sknet.Logger())
engine.Register("/create/account", api.CreateAccount(ee))
engine.Register("/create/deposit_address", api.GetNewAddress(ee))
engine.Register("/get/account/balance", api.GetAccountBalance(ee))
engine.Register("/get/address/balance", api.GetAddrBalance(ee))
engine.Register("/withdrawl", api.Withdraw(ee))
engine.Register("/create/order", api.CreateOrder(ee))
engine.Register("/get/coins", api.GetCoins(ee))
engine.Register("/get/orders", api.GetOrders(ee))
// utxos handler
engine.Register("/get/utxos", api.GetUtxos(ee))
// output history handler
engine.Register("/get/output", api.GetOutput(ee))
// transaction handler
engine.Register("/inject/tx", api.InjectTx(ee))
engine.Register("/get/tx", api.GetTx(ee))
engine.Register("/get/rawtx", api.GetRawTx(ee))
engine.Register("/admin/update/credit", api.UpdateCredit(ee))
return engine
}
示例2: GetUtxos
// GetUtxos get unspent output of specific address.
func GetUtxos(egn engine.Exchange) sknet.HandlerFunc {
return func(c *sknet.Context) error {
var req pp.GetUtxoReq
var rlt *pp.EmptyRes
for {
if err := c.BindJSON(&req); err != nil {
rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongRequest)
logger.Error(err.Error())
break
}
coin, err := egn.GetCoin(req.GetCoinType())
if err != nil {
rlt = pp.MakeErrRes(err)
logger.Error(err.Error())
break
}
res, err := coin.GetUtxos(req.GetAddresses())
if err != nil {
rlt = pp.MakeErrRes(err)
logger.Error(err.Error())
break
}
return c.SendJSON(&res)
}
return c.Error(rlt)
}
}
示例3: GetAddrBalance
// GetAddrBalance get balance of specific address.
func GetAddrBalance(ee engine.Exchange) sknet.HandlerFunc {
return func(c *sknet.Context) error {
var rlt *pp.EmptyRes
for {
req := pp.GetAddrBalanceReq{}
if err := c.BindJSON(&req); err != nil {
logger.Error(err.Error())
rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongRequest)
break
}
coin, err := ee.GetCoin(req.GetCoinType())
if err != nil {
rlt = pp.MakeErrRes(err)
logger.Error(err.Error())
break
}
addrs := strings.Split(req.GetAddrs(), ",")
b, err := coin.GetBalance(addrs)
if err != nil {
logger.Error(err.Error())
rlt = pp.MakeErrRes(err)
break
}
res := pp.GetAddrBalanceRes{
Result: pp.MakeResultWithCode(pp.ErrCode_Success),
Balance: &b,
}
return c.SendJSON(&res)
}
return c.Error(rlt)
}
}
示例4: GetAccountBalance
// GetBalance return balance of specific account.
func GetAccountBalance(ee engine.Exchange) sknet.HandlerFunc {
return func(c *sknet.Context) error {
rlt := &pp.EmptyRes{}
for {
req := pp.GetAccountBalanceReq{}
if err := c.BindJSON(&req); err != nil {
logger.Error(err.Error())
rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongRequest)
break
}
// validate pubkey
pubkey := req.GetPubkey()
if err := validatePubkey(pubkey); err != nil {
logger.Error(err.Error())
rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongPubkey)
break
}
a, err := ee.GetAccount(pubkey)
if err != nil {
rlt = pp.MakeErrResWithCode(pp.ErrCode_NotExits)
break
}
bal := a.GetBalance(req.GetCoinType())
bres := pp.GetAccountBalanceRes{
Result: pp.MakeResultWithCode(pp.ErrCode_Success),
Balance: &pp.Balance{Amount: pp.PtrUint64(bal)},
}
return c.SendJSON(&bres)
}
return c.Error(rlt)
}
}
示例5: GetRawTx
// GetRawTx return rawtx of specifc tx.
func GetRawTx(egn engine.Exchange) sknet.HandlerFunc {
return func(c *sknet.Context) error {
var rlt *pp.EmptyRes
for {
req := pp.GetRawTxReq{}
if err := c.BindJSON(&req); err != nil {
rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongRequest)
break
}
coin, err := egn.GetCoin(req.GetCoinType())
if err != nil {
logger.Error(err.Error())
rlt = pp.MakeErrRes(err)
break
}
rawtx, err := coin.GetRawTx(req.GetTxid())
if err != nil {
logger.Error(err.Error())
rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongRequest)
break
}
res := pp.GetRawTxRes{
Result: pp.MakeResultWithCode(pp.ErrCode_Success),
CoinType: req.CoinType,
Rawtx: pp.PtrString(rawtx),
}
return c.SendJSON(&res)
}
return c.Error(rlt)
}
}
示例6: GetCoins
// GetCoins get supported coins.
func GetCoins(egn engine.Exchange) sknet.HandlerFunc {
return func(c *sknet.Context) error {
coins := pp.CoinsRes{
Result: pp.MakeResultWithCode(pp.ErrCode_Success),
Coins: egn.GetSupportCoins(),
}
return c.SendJSON(&coins)
}
}
示例7: createBtcWithdrawTx
// createBtcWithdrawTx create withdraw transaction.
// amount is the number of coins that want to withdraw.
// toAddr is the address that the coins will be sent to.
func createBtcWithdrawTx(egn engine.Exchange, amount uint64, toAddr string) (*BtcTxResult, error) {
uxs, err := egn.ChooseUtxos(bitcoin.Type, amount+egn.GetBtcFee(), ChooseUtxoTm)
if err != nil {
return nil, err
}
utxos := uxs.([]bitcoin.Utxo)
for _, u := range utxos {
logger.Debug("using utxos: txid:%s vout:%d addr:%s", u.GetTxid(), u.GetVout(), u.GetAddress())
}
var success bool
defer func() {
if !success {
// put utxos back to pool if withdraw failed.
go func() { egn.PutUtxos(bitcoin.Type, utxos) }()
}
}()
var totalAmounts uint64
for _, u := range utxos {
totalAmounts += u.GetAmount()
}
fee := egn.GetBtcFee()
outAddrs := []bitcoin.TxOut{}
chgAmt := totalAmounts - fee - amount
chgAddr := ""
if chgAmt > 0 {
// generate a change address
chgAddr = egn.GetNewAddress(bitcoin.Type)
outAddrs = append(outAddrs,
bitcoin.TxOut{Addr: toAddr, Value: amount},
bitcoin.TxOut{Addr: chgAddr, Value: chgAmt})
} else {
outAddrs = append(outAddrs, bitcoin.TxOut{Addr: toAddr, Value: amount})
}
// change utxo to UtxoWithkey
utxoKeys, err := makeBtcUtxoWithkeys(utxos, egn)
if err != nil {
return nil, err
}
logger.Debug("creating transaction...")
tx, err := bitcoin.NewTransaction(utxoKeys, outAddrs)
if err != nil {
logger.Error(err.Error())
return nil, err
}
success = true
rlt := BtcTxResult{
Tx: tx,
UsingUtxos: utxos[:],
ChangeAddr: chgAddr,
}
return &rlt, nil
}
示例8: makeBtcUtxoWithkeys
func makeBtcUtxoWithkeys(utxos []bitcoin.Utxo, egn engine.Exchange) ([]bitcoin.UtxoWithkey, error) {
utxoks := make([]bitcoin.UtxoWithkey, len(utxos))
for i, u := range utxos {
key, err := egn.GetAddrPrivKey(bitcoin.Type, u.GetAddress())
if err != nil {
return []bitcoin.UtxoWithkey{}, err
}
utxoks[i] = bitcoin.NewUtxoWithKey(u, key)
}
return utxoks, nil
}
示例9: GetOrders
// GetOrders get order list.
func GetOrders(egn engine.Exchange) sknet.HandlerFunc {
return func(c *sknet.Context) error {
rlt := &pp.EmptyRes{}
for {
req := pp.GetOrderReq{}
if err := c.BindJSON(&req); err != nil {
rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongRequest)
break
}
op, err := order.TypeFromStr(req.GetType())
if err != nil {
rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongRequest)
logger.Error(err.Error())
break
}
ords, err := egn.GetOrders(req.GetCoinPair(), op, req.GetStart(), req.GetEnd())
if err != nil {
rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongRequest)
logger.Error(err.Error())
break
}
res := pp.GetOrderRes{
CoinPair: req.CoinPair,
Type: req.Type,
Orders: make([]*pp.Order, len(ords)),
}
for i := range ords {
res.Orders[i] = &pp.Order{
Id: &ords[i].ID,
Type: req.Type,
Price: &ords[i].Price,
Amount: &ords[i].Amount,
RestAmt: &ords[i].RestAmt,
CreatedAt: &ords[i].CreatedAt,
}
}
res.Result = pp.MakeResultWithCode(pp.ErrCode_Success)
return c.SendJSON(&res)
}
return c.Error(rlt)
}
}
示例10: 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)
}
}
示例11: GetTx
// GetTx get transaction by id.
func GetTx(egn engine.Exchange) sknet.HandlerFunc {
return func(c *sknet.Context) error {
var rlt *pp.EmptyRes
for {
req := pp.GetTxReq{}
if err := c.BindJSON(&req); err != nil {
logger.Error(err.Error())
rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongRequest)
break
}
coin, err := egn.GetCoin(req.GetCoinType())
if err != nil {
logger.Error(err.Error())
rlt = pp.MakeErrRes(err)
break
}
// brief validate transaction id
if !coin.ValidateTxid(req.GetTxid()) {
rlt = pp.MakeErrRes(errors.New("invalid transaction id"))
break
}
tx, err := coin.GetTx(req.GetTxid())
if err != nil {
logger.Error(err.Error())
rlt = pp.MakeErrRes(err)
break
}
res := pp.GetTxRes{
Result: pp.MakeResultWithCode(pp.ErrCode_Success),
CoinType: req.CoinType,
Tx: tx,
}
return c.SendJSON(&res)
}
return c.Error(rlt)
}
}
示例12: IsAdmin
// IsAdmin middleware for checking if the account is admin.
func IsAdmin(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
}
if !ee.IsAdmin(req.GetPubkey()) {
logger.Error("not admin")
rlt = pp.MakeErrResWithCode(pp.ErrCode_UnAuthorized)
break
}
return c.Next()
}
return c.Error(rlt)
}
}
示例13: InjectTx
// InjectTx inject transaction.
func InjectTx(egn engine.Exchange) sknet.HandlerFunc {
return func(c *sknet.Context) error {
var rlt *pp.EmptyRes
for {
req := pp.InjectTxnReq{}
if err := c.BindJSON(&req); err != nil {
logger.Error(err.Error())
rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongRequest)
break
}
// get coin gateway
coin, err := egn.GetCoin(req.GetCoinType())
if err != nil {
logger.Error(err.Error())
rlt = pp.MakeErrRes(err)
break
}
// inject tx.
txid, err := coin.InjectTx(req.GetTx())
if err != nil {
logger.Error(err.Error())
rlt = pp.MakeErrRes(err)
break
}
res := pp.InjectTxnRes{
Result: pp.MakeResultWithCode(pp.ErrCode_Success),
Txid: pp.PtrString(txid),
}
return c.SendJSON(&res)
}
return c.Error(rlt)
}
}
示例14: getWithdrawReqParams
func getWithdrawReqParams(c *sknet.Context, ee engine.Exchange) (*ReqParams, error) {
rp := NewReqParams()
req := pp.WithdrawalReq{}
if err := c.BindJSON(&req); err != nil {
return nil, err
}
// validate pubkey
pubkey := req.GetPubkey()
if err := validatePubkey(pubkey); err != nil {
return nil, err
}
a, err := ee.GetAccount(pubkey)
if err != nil {
return nil, err
}
rp.Values["account"] = a
rp.Values["cointype"] = req.GetCoinType()
rp.Values["amt"] = req.GetCoins()
rp.Values["outAddr"] = req.GetOutputAddress()
return rp, nil
}
示例15: GetNewAddress
// GetNewAddress account create new address for depositing.
func GetNewAddress(ee engine.Exchange) sknet.HandlerFunc {
return func(c *sknet.Context) error {
rlt := &pp.EmptyRes{}
for {
req := pp.GetDepositAddrReq{}
if err := c.BindJSON(&req); err != nil {
logger.Error(err.Error())
rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongRequest)
break
}
// validate pubkey
pubkey := req.GetPubkey()
if err := validatePubkey(pubkey); err != nil {
logger.Error(err.Error())
rlt = pp.MakeErrResWithCode(pp.ErrCode_WrongPubkey)
break
}
at, err := ee.GetAccount(pubkey)
if err != nil {
rlt = pp.MakeErrResWithCode(pp.ErrCode_NotExits)
break
}
ct := req.GetCoinType()
// get the new address for depositing
addr := ee.GetNewAddress(ct)
// add the new address to engin for watching it's utxos.
at.AddDepositAddress(ct, addr)
ee.WatchAddress(ct, addr)
ds := pp.GetDepositAddrRes{
Result: pp.MakeResultWithCode(pp.ErrCode_Success),
CoinType: req.CoinType,
Address: &addr,
}
return c.SendJSON(&ds)
}
return c.Error(rlt)
}
}