Implement settxfee command

This commit is contained in:
Josh Rickmar 2013-10-07 15:14:39 -04:00
parent b137542c92
commit c1cf2c59bb
2 changed files with 63 additions and 12 deletions

View file

@ -169,6 +169,8 @@ func ProcessFrontendMsg(reply chan []byte, msg []byte) {
SendFrom(reply, &jsonMsg) SendFrom(reply, &jsonMsg)
case "sendmany": case "sendmany":
SendMany(reply, &jsonMsg) SendMany(reply, &jsonMsg)
case "settxfee":
SetTxFee(reply, &jsonMsg)
case "walletlock": case "walletlock":
WalletLock(reply, &jsonMsg) WalletLock(reply, &jsonMsg)
case "walletpassphrase": case "walletpassphrase":
@ -408,12 +410,13 @@ func SendFrom(reply chan []byte, msg *btcjson.Message) {
return return
} }
// fee needs to be a global, set from another json method. TxFee.Lock()
var fee uint64 fee := TxFee.i
TxFee.Unlock()
pairs := map[string]uint64{ pairs := map[string]uint64{
toaddr58: uint64(amt), toaddr58: uint64(amt),
} }
rawtx, err := w.txToPairs(pairs, fee, int(minconf)) rawtx, err := w.txToPairs(pairs, uint64(fee), int(minconf))
if err != nil { if err != nil {
e := InternalError e := InternalError
e.Message = err.Error() e.Message = err.Error()
@ -527,9 +530,10 @@ func SendMany(reply chan []byte, msg *btcjson.Message) {
return return
} }
// fee needs to be a global, set from another json method. TxFee.Lock()
var fee uint64 fee := TxFee.i
rawtx, err := w.txToPairs(pairs, fee, int(minconf)) TxFee.Unlock()
rawtx, err := w.txToPairs(pairs, uint64(fee), int(minconf))
if err != nil { if err != nil {
e := InternalError e := InternalError
e.Message = err.Error() e.Message = err.Error()
@ -562,6 +566,45 @@ func SendMany(reply chan []byte, msg *btcjson.Message) {
_ = comment _ = comment
} }
// SetTxFee sets the global transaction fee added to transactions.
func SetTxFee(reply chan []byte, msg *btcjson.Message) {
e := InvalidParams
params, ok := msg.Params.([]interface{})
if !ok {
ReplyError(reply, msg.Id, &e)
return
}
if len(params) != 1 {
e.Message = "Incorrect number of parameters"
ReplyError(reply, msg.Id, &e)
return
}
jsonFee, ok := params[0].(float64)
if !ok {
e.Message = "Amount is not a number"
ReplyError(reply, msg.Id, &e)
return
}
if jsonFee < 0 {
e.Message = "Amount cannot be negative"
ReplyError(reply, msg.Id, &e)
return
}
fee, err := btcjson.JSONToAmount(jsonFee)
if err != nil {
e.Message = fmt.Sprintf("Cannot convert JSON number to int64: %v", err)
ReplyError(reply, msg.Id, &e)
return
}
// TODO(jrick): need to notify all frontends of new tx fee.
TxFee.Lock()
TxFee.i = fee
TxFee.Unlock()
ReplySuccess(reply, msg.Id, true)
}
// CreateEncryptedWallet creates a new encrypted wallet. The form of the command is: // CreateEncryptedWallet creates a new encrypted wallet. The form of the command is:
// //
// createencryptedwallet [account] [description] [passphrase] // createencryptedwallet [account] [description] [passphrase]
@ -573,33 +616,33 @@ func SendMany(reply chan []byte, msg *btcjson.Message) {
// Wallets will be created on MainNet, or TestNet3 if btcwallet is run with // Wallets will be created on MainNet, or TestNet3 if btcwallet is run with
// the --testnet option. // the --testnet option.
func CreateEncryptedWallet(reply chan []byte, msg *btcjson.Message) { func CreateEncryptedWallet(reply chan []byte, msg *btcjson.Message) {
e := InvalidParams
params, ok := msg.Params.([]interface{}) params, ok := msg.Params.([]interface{})
e := &InvalidParams
if !ok { if !ok {
ReplyError(reply, msg.Id, e) ReplyError(reply, msg.Id, &e)
return return
} }
if len(params) != 3 { if len(params) != 3 {
e.Message = "Incorrect number of parameters" e.Message = "Incorrect number of parameters"
ReplyError(reply, msg.Id, e) ReplyError(reply, msg.Id, &e)
return return
} }
wname, ok := params[0].(string) wname, ok := params[0].(string)
if !ok { if !ok {
e.Message = "Account is not a string" e.Message = "Account is not a string"
ReplyError(reply, msg.Id, e) ReplyError(reply, msg.Id, &e)
return return
} }
desc, ok := params[1].(string) desc, ok := params[1].(string)
if !ok { if !ok {
e.Message = "Description is not a string" e.Message = "Description is not a string"
ReplyError(reply, msg.Id, e) ReplyError(reply, msg.Id, &e)
return return
} }
pass, ok := params[2].(string) pass, ok := params[2].(string)
if !ok { if !ok {
e.Message = "Passphrase is not a string" e.Message = "Passphrase is not a string"
ReplyError(reply, msg.Id, e) ReplyError(reply, msg.Id, &e)
return return
} }

View file

@ -25,6 +25,7 @@ import (
"github.com/conformal/btcwallet/tx" "github.com/conformal/btcwallet/tx"
"github.com/conformal/btcwire" "github.com/conformal/btcwire"
"sort" "sort"
"sync"
"time" "time"
) )
@ -36,6 +37,13 @@ var ErrInsufficientFunds = errors.New("insufficient funds")
// requested bitcoin network is invalid (neither mainnet nor testnet). // requested bitcoin network is invalid (neither mainnet nor testnet).
var ErrUnknownBitcoinNet = errors.New("unknown bitcoin network") var ErrUnknownBitcoinNet = errors.New("unknown bitcoin network")
// TxFee represents the global transaction fee added to newly-created
// transactions and sent as a reward to the block miner.
var TxFee struct {
sync.Mutex
i int64
}
// ByAmount defines the methods needed to satisify sort.Interface to // ByAmount defines the methods needed to satisify sort.Interface to
// sort a slice of Utxos by their amount. // sort a slice of Utxos by their amount.
type ByAmount []*tx.Utxo type ByAmount []*tx.Utxo