示例#1
0
func TestAmountPaid(t *testing.T) {
	defer os.RemoveAll(testutil.InitDir(t))
	btcConf, err := util.LoadBitcoindConf("")
	if err != nil {
		t.Fatal(err)
	}
	peer := Peer{ID: msg.OcID("123id")}
	otherPeer := Peer{ID: msg.OcID("456otherid")}

	addr, err := peer.PaymentAddr(1, btcConf)
	if err != nil {
		t.Fatal(err)
	}
	otherAddr, err := otherPeer.PaymentAddr(1, btcConf)
	if err != nil {
		t.Fatal(err)
	}
	fmt.Printf("addr %v other addr %v\n", addr, otherAddr)

	// Send some BTC to ourselves.
	amt := int64(1e6)
	cmd, err := btcjson.NewSendToAddressCmd("", addr, amt)
	if err != nil {
		log.Fatal(err)
	}
	sendBtcResp, err := util.SendBtcRpc(cmd, btcConf)
	_, ok := sendBtcResp.Result.(string)
	if !ok {
		log.Fatal(sendBtcResp)
	}

	// Send some BTC to another address.
	cmd, err = btcjson.NewSendToAddressCmd("", otherAddr, 1e6)
	if err != nil {
		log.Fatal(err)
	}
	sendBtcResp, err = util.SendBtcRpc(cmd, btcConf)
	_, ok = sendBtcResp.Result.(string)
	if !ok {
		log.Fatal(sendBtcResp)
	}

	// Verify balance
	pv, err := peer.AmountPaid(0, btcConf)
	if err != nil {
		log.Fatal(err)
	}
	if pv.Amount != amt {
		t.Fatalf("%v != %v", paid, amt)
	}
}
示例#2
0
func (c *Client) SendBtcPayment(payVal *msg.PaymentValue, payAddr *msg.PaymentAddr) (msg.BtcTxid, error) {
	if payVal.Currency != msg.BTC || payAddr.Currency != msg.BTC {
		panic("unexpected currency: " + payVal.Currency + " " + payAddr.Currency)
	}
	cmd, err := btcjson.NewSendToAddressCmd("", payAddr.Addr, payVal.Amount)
	if err != nil {
		return "", fmt.Errorf("error while making cmd: %v", err.Error())
	}
	resp, err := util.SendBtcRpc(cmd, c.BtcConf)
	if err != nil {
		return "", fmt.Errorf("error while making cmd: %v", err.Error())
	}
	txid, ok := resp.Result.(string)
	if !ok {
		return "", fmt.Errorf("error during bitcoind JSON-RPC: %v", resp)
	}
	return msg.BtcTxid(txid), nil
}
示例#3
0
// makeSendToAddress generates the cmd struture for sendtoaddress commands.
func makeSendToAddress(args []interface{}) (btcjson.Cmd, error) {
	return btcjson.NewSendToAddressCmd("btcctl", args[0].(string), args[1].(int64), args[2:]...)
}