示例#1
0
// makeListUnspent generates the cmd structure for listunspent commands.
func makeListUnspent(args []interface{}) (btcjson.Cmd, error) {
	var optargs = make([]interface{}, 0, 3)
	if len(args) > 0 {
		optargs = append(optargs, args[0].(int))
	}
	if len(args) > 1 {
		optargs = append(optargs, args[1].(int))
	}
	if len(args) > 2 {
		var addrs []string
		err := json.Unmarshal([]byte(args[2].(string)), &addrs)
		if err != nil {
			return nil, err
		}
		optargs = append(optargs, addrs)
	}
	return btcjson.NewListUnspentCmd("btcctl", optargs...)
}
示例#2
0
func GetBtcCredInRange(min, max int64, conf *util.BitcoindConf) (*[]BtcCred, error) {
	cmd, err := btcjson.NewListUnspentCmd("")
	if err != nil {
		return nil, fmt.Errorf("error while making cmd: %v", err.Error())
	}
	resp, err := util.SendBtcRpc(cmd, conf)
	if err != nil {
		return nil, fmt.Errorf("error while making cmd: %v", err.Error())
	}
	if resp.Error != nil {
		return nil, fmt.Errorf("error during bitcoind JSON-RPC: %v", resp.Error)
	}
	addrs := make(map[string]*addressBalance)
	unspent := resp.Result.([]btcjson.ListUnSpentResult)
	for _, u := range unspent {
		if _, ok := addrs[u.Address]; !ok {
			addrs[u.Address] = &addressBalance{
				Address: u.Address,
				Amount:  0,
			}
		}
		ab := addrs[u.Address]
		ab.Amount += util.B2S(u.Amount)
	}
	addrsList := make([]addressBalance, len(addrs))
	i := 0
	for _, v := range addrs {
		addrsList[i] = *v
		i++
	}
	sort.Sort(byAmount(addrsList))
	var use *[]BtcCred
	for iter := 1; iter <= 5; iter++ {
		use, err = inputsInRange(&addrsList, min, max, iter, len(addrsList)-1)
		if use != nil {
			break
		}
	}
	if err != nil {
		return nil, err
	}
	return use, nil
}