Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hi, how to send BTC to 2 different addresses with different amounts? #31

Closed
elvis-hp opened this issue Feb 10, 2023 · 2 comments
Closed

Comments

@elvis-hp
Copy link

Hi, I have some code to send BTC to one address, can some one help me send to 2 different addresses with different amounts, thank you so much.
` wif, err := btcutil.DecodeWIF(secret)
if err != nil {
return "", err
}

pkHex := hex.EncodeToString(wif.PrivKey.Serialize())
tx := gobcy.TempNewTX(from, destination, *big.NewInt(int64(amount)))

if fee != -1 {
	tx.Fees = *big.NewInt(int64(fee))
}

//Post New TXSkeleton
skel, err := bs.chain.NewTX(tx, false)

if err != nil {
	log.Println("bs.chain.NewTX err: ", err, tx)
	return "", err
}
prikHexs := []string{}
for i := 0; i < len(skel.ToSign); i++ {
	prikHexs = append(prikHexs, pkHex)
}

//Sign it locally
err = skel.Sign(prikHexs)
if err != nil {
	log.Println("skel.Sign error: ", err)
	return "", err
}

// add this one with segwit address:
for i, _ := range skel.Signatures {
	skel.Signatures[i] = skel.Signatures[i] + "01"
}

skel, err = bs.chain.SendTX(skel)
if err != nil {
	log.Println("bs.chain.SendTX err:", err)
	return "", err
}
return skel.Trans.Hash, nil`
@quentinlesceller
Copy link
Contributor

You have to create and initialise a TX object manually.

tx := gobcy.TX{//fields}

@elvis-hp
Copy link
Author

elvis-hp commented Mar 17, 2023

You have to create and initialise a TX object manually.

tx := gobcy.TX{//fields}

Hi, so my address have 10 btc, I need to send all to 2 address. 5 eth for per address.
How to est fee?
I got an error:
"HTTP 400 Bad Request, Message(s): Not enough funds after fees in 1 inputs to pay for 2 outputs, missing -2772."
code:

func (bs *BlockcypherService) EstimateFeeTransactionWithPreferenceFromSegwitAddressMultiAddress(secret string, from string, destinations map[string]int,
	preference string) (*big.Int, error) {

	outAddrs := make(map[string]big.Int)

	for addr, amount := range destinations {
		outAddrs[addr] = *big.NewInt(int64(amount))
	}

	tx := TempNewTXMultiOut(from, outAddrs)

	if len(preference) == 0 {
		tx.Preference = PreferenceMedium
	} else {
		tx.Preference = preference
	}
	skel, err := bs.chain.NewTX(tx, false) // gobcy.TX

	if err != nil {
		log.Println("bs.chain.NewTX err: ", err, tx)
		return nil, err
	}
	log.Println("[SendTransactionWithPreference] fee", skel.Trans.Fees)
	return &skel.Trans.Fees, nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants