Example #1
0
func TestGetOutput(t *testing.T) {
	type args struct {
		hash string
	}
	tests := []struct {
		name    string
		args    args
		want    *pp.Output
		wantErr error
	}{
		// TODO: Add test cases.
		{
			"normal",
			args{
				"a57c038591f862b8fada57e496ef948183b153348d7932921f865a8541a477c5",
			},
			&pp.Output{
				Time:          pp.PtrUint64(1477037552),
				SrcBlockSeq:   pp.PtrUint64(443),
				SrcTx:         pp.PtrString("b8ca61c0788bd711c89563f9bc60add172ee01b543ea5dcb1955c51bbfcbbaa2"),
				OwnerAddress:  pp.PtrString("cBnu9sUvv12dovBmjQKTtfE4rbjMmf3fzW"),
				Coins:         pp.PtrUint64(1000000),
				Hours:         pp.PtrUint64(7),
				SpentBlockSeq: pp.PtrUint64(450),
				SpentTx:       pp.PtrString("b1481d614ffcc27408fe2131198d9d2821c78601a0aa23d8e9965b2a5196edc0"),
			},
			nil,
		},
		{
			"invalid uxhash len",
			args{
				"123",
			},
			nil,
			errors.New("invalid output hash, encoding/hex: odd length hex string"),
		},
		{
			"invalid uxhash ",
			args{
				"a57c038591f862b8fada57e496ef948183b153348d7932921f865a8541a477c7",
			},
			nil,
			errors.New("not found\n"),
		},
	}
	for _, tt := range tests {
		got, err := GetOutput("127.0.0.1:6420", tt.args.hash)
		if !reflect.DeepEqual(err, tt.wantErr) {
			t.Errorf("%q. GetOutput() error = %v, wantErr %v", tt.name, err, tt.wantErr)
			continue
		}
		if !reflect.DeepEqual(got, tt.want) {
			t.Errorf("%q. GetOutput() = %v, want %v", tt.name, got, tt.want)
		}
	}
}
Example #2
0
// GetBalance get balance of specific addresses.
func (btc Bitcoin) GetBalance(addrs []string) (pp.Balance, error) {
	v, err := getBalanceExplr(addrs)
	if err != nil {
		return pp.Balance{}, err
	}
	return pp.Balance{Amount: pp.PtrUint64(v)}, nil
}
Example #3
0
// 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)
	}
}
Example #4
0
func makeOrderReq(r *http.Request) (*pp.OrderReq, error) {
	// get coin_pair
	cp := r.FormValue("coin_pair")
	if cp == "" {
		return nil, errors.New("coin_pair is empty")
	}

	// get order type
	tp := r.FormValue("type")
	if tp == "" {
		return nil, errors.New("type is empty")
	}

	// get price
	pc := r.FormValue("price")
	if pc == "" {
		return nil, errors.New("price is empty")
	}
	price, err := strconv.ParseUint(pc, 10, 64)
	if err != nil {
		return nil, err
	}

	// get amount
	amt := r.FormValue("amt")
	if amt == "" {
		return nil, errors.New("amt is empty")
	}
	amount, err := strconv.ParseUint(amt, 10, 64)
	if err != nil {
		return nil, err
	}

	return &pp.OrderReq{
		CoinPair: pp.PtrString(cp),
		Type:     pp.PtrString(tp),
		Price:    pp.PtrUint64(price),
		Amount:   pp.PtrUint64(amount),
	}, nil
}
Example #5
0
// GetUtxos gets bitcoin utxos of specific addresses.
func (btc *Bitcoin) GetUtxos(addrs []string) (interface{}, error) {
	utxos, err := GetUnspentOutputs(addrs)
	if err != nil {
		return nil, err
	}
	btcUxs := make([]*pp.BtcUtxo, len(utxos))
	for i, u := range utxos {
		btcUxs[i] = &pp.BtcUtxo{
			Address: pp.PtrString(u.GetAddress()),
			Txid:    pp.PtrString(u.GetTxid()),
			Vout:    pp.PtrUint32(u.GetVout()),
			Amount:  pp.PtrUint64(u.GetAmount()),
		}
	}

	var res = pp.GetUtxoRes{
		Result:   pp.MakeResultWithCode(pp.ErrCode_Success),
		BtcUtxos: btcUxs,
	}

	return res, nil
}
Example #6
0
// AdminUpdateBalance update balance of specific account
// mode: PUT
// url: /api/v1/admin/account/balance?dst=[:dst]&coin_type=[:coin_type]&amt=[:amt]
// params:
//      dst: the dst account pubkey, whose balance will be updated.
//      coin_type: skycoin or bitcoin, the coin you want to credit.
//      amt: balance that will be updated.
func AdminUpdateBalance(se Servicer) httprouter.Handle {
	return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		var rlt *pp.EmptyRes
		for {
			// get dst_pubkey
			dstPk := r.FormValue("dst")
			if dstPk == "" {
				err := errors.New("dst pubkey is empty")
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}

			// validate the dst_pubkey
			if _, err := cipher.PubKeyFromHex(dstPk); err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(errors.New("invalid dst pubkey"))
				break
			}

			// get coin type.
			cp := r.FormValue("coin_type")
			if cp == "" {
				err := errors.New("coin_type is empty")
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}

			// get amt
			amt := r.FormValue("amt")
			if amt == "" {
				err := errors.New("amt is empty")
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}
			amount, err := strconv.ParseUint(amt, 10, 64)
			if err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}

			a, err := account.GetActive()
			if err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrRes(err)
				break
			}
			req := pp.UpdateCreditReq{
				Pubkey:   pp.PtrString(a.Pubkey),
				CoinType: pp.PtrString(cp),
				Amount:   pp.PtrUint64(amount),
				Dst:      pp.PtrString(dstPk),
			}

			res := pp.UpdateCreditRes{}
			if err := sknet.EncryGet(se.GetServAddr(), "/admin/update/credit", req, &res); err != nil {
				logger.Error(err.Error())
				rlt = pp.MakeErrResWithCode(pp.ErrCode_ServerError)
				break
			}

			sendJSON(w, res)
			return
		}
		sendJSON(w, rlt)
	}
}