func (p *Peer) AmountPaid(minConf int, btcConf *util.BitcoindConf) (*msg.PaymentValue, error) { cmd, err := btcjson.NewListReceivedByAddressCmd("", minConf, false) if err != nil { return nil, fmt.Errorf("error while making cmd: %v", err.Error()) } resp, err := util.SendBtcRpc(cmd, btcConf) ser, ok := resp.Result.([]interface{}) if !ok { return nil, fmt.Errorf("error during bitcoind JSON-RPC: %v", resp) } addrs := p.readPaymentAddrs() addrsMap := make(map[string]bool) for _, addr := range addrs { addrsMap[addr] = true } amt := int64(0) for _, r := range ser { result := r.(map[string]interface{}) if addrsMap[result["address"].(string)] { satoshis := util.B2S(result["amount"].(float64)) fmt.Printf("addr: %v -> %v\n", result["address"], satoshis) amt += satoshis } } fmt.Printf("my addrs: %v\n", addrs) return &msg.PaymentValue{Amount: amt, Currency: msg.BTC}, nil }
// makeListReceivedByAddress generates the cmd structure for listreceivedbyaddress commands. func makeListReceivedByAddress(args []interface{}) (btcjson.Cmd, error) { var optargs = make([]interface{}, 0, 2) if len(args) > 0 { optargs = append(optargs, args[0].(int)) } if len(args) > 1 { optargs = append(optargs, args[1].(bool)) } return btcjson.NewListReceivedByAddressCmd("btcctl", optargs...) }
func getAnyBtcAddr(conf *util.BitcoindConf) (string, error) { msg, err := btcjson.NewListReceivedByAddressCmd(nil, 0, true) if err != nil { return "", err } json, err := msg.MarshalJSON() if err != nil { return "", err } resp, err := btcjson.RpcCommand(conf.User, conf.Password, conf.Server, json) if err != nil { return "", err } for _, r := range resp.Result.([]interface{}) { result := r.(map[string]interface{}) return result["address"].(string), nil } return "", errors.New("no address found") }