diff --git a/dcrjson/walletsvrcmds.go b/dcrjson/walletsvrcmds.go index 3152072488..dbea9f23d2 100644 --- a/dcrjson/walletsvrcmds.go +++ b/dcrjson/walletsvrcmds.go @@ -240,6 +240,32 @@ func NewEstimatePriorityCmd(numBlocks int64) *EstimatePriorityCmd { } } +// FundRawTransactionOptions represents the optional inputs to fund +// a raw transaction. +type FundRawTransactionOptions struct { + ChangeAddress *string `json:"changeaddress"` + FeeRate *float64 `json:"feerate"` + ConfTarget *int32 `json:"conf_target"` +} + +// FundRawTransactionCmd is a type handling custom marshaling and +// unmarshaling of fundrawtransaction JSON wallet extension commands. +type FundRawTransactionCmd struct { + HexString string + FundAccount string + Options *FundRawTransactionOptions +} + +// NewFundRawTransactionCmd returns a new instance which can be used to issue a +// fundrawtransaction JSON-RPC command. +func NewFundRawTransactionCmd(hexString string, fundAccount string, options *FundRawTransactionOptions) *FundRawTransactionCmd { + return &FundRawTransactionCmd{ + HexString: hexString, + FundAccount: fundAccount, + Options: options, + } +} + // GenerateVoteCmd is a type handling custom marshaling and // unmarshaling of generatevote JSON wallet extension commands. type GenerateVoteCmd struct { @@ -1270,6 +1296,7 @@ func init() { MustRegisterCmd("dropvotingaccount", (*DropVotingAccountCmd)(nil), flags) MustRegisterCmd("dumpprivkey", (*DumpPrivKeyCmd)(nil), flags) MustRegisterCmd("estimatepriority", (*EstimatePriorityCmd)(nil), flags) + MustRegisterCmd("fundrawtransaction", (*FundRawTransactionCmd)(nil), flags) MustRegisterCmd("generatevote", (*GenerateVoteCmd)(nil), flags) MustRegisterCmd("getaccount", (*GetAccountCmd)(nil), flags) MustRegisterCmd("getaccountaddress", (*GetAccountAddressCmd)(nil), flags) diff --git a/dcrjson/walletsvrresults.go b/dcrjson/walletsvrresults.go index 654a188084..5996e8b286 100644 --- a/dcrjson/walletsvrresults.go +++ b/dcrjson/walletsvrresults.go @@ -5,6 +5,12 @@ package dcrjson +// FundRawTransactionResult models the data from the fundrawtransaction command. +type FundRawTransactionResult struct { + Hex string `json:"hex"` + Fee float64 `json:"fee"` +} + // GenerateVoteResult models the data from the generatevote command. type GenerateVoteResult struct { Hex string `json:"hex"` diff --git a/rpcclient/wallet.go b/rpcclient/wallet.go index 8f200bc4e3..6edca00711 100644 --- a/rpcclient/wallet.go +++ b/rpcclient/wallet.go @@ -2738,6 +2738,44 @@ func (c *Client) AddTicket(ticket *dcrutil.Tx) error { return c.AddTicketAsync(hex.EncodeToString(ticketB)).Receive() } +// FutureFundRawTransactionResult is a future promise to deliver the result of a +// FundRawTransactionAsync RPC invocation (or an applicable error). +type FutureFundRawTransactionResult chan *response + +// Receive waits for the response promised by the future and returns the unsigned +// transaction with the passed amount and the given address. +func (r FutureFundRawTransactionResult) Receive() (*dcrjson.FundRawTransactionResult, error) { + res, err := receiveFuture(r) + if err != nil { + return nil, err + } + + // Unmarshal result as a string. + var infoRes dcrjson.FundRawTransactionResult + err = json.Unmarshal(res, &infoRes) + if err != nil { + return nil, err + } + + return &infoRes, nil +} + +// FundRawTransactionAsync returns an instance of a type that can be used to get the +// result of the RPC at some future time by invoking the Receive function on the +// returned instance. +// +// See FundRawTransaction for the blocking version and more details. +func (c *Client) FundRawTransactionAsync(rawhex string, fundAccount string, options dcrjson.FundRawTransactionOptions) FutureFundRawTransactionResult { + cmd := dcrjson.NewFundRawTransactionCmd(rawhex, fundAccount, &options) + return c.sendCmd(cmd) +} + +// FundRawTransaction Add inputs to a transaction until it has enough +// in value to meet its out value. +func (c *Client) FundRawTransaction(rawhex string, fundAccount string, options dcrjson.FundRawTransactionOptions) (*dcrjson.FundRawTransactionResult, error) { + return c.FundRawTransactionAsync(rawhex, fundAccount, options).Receive() +} + // FutureGenerateVoteResult is a future promise to deliver the result of a // GenerateVoteAsync RPC invocation (or an applicable error). type FutureGenerateVoteResult chan *response diff --git a/rpcserver.go b/rpcserver.go index 98fe4a7f24..87cffddbdd 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -253,6 +253,7 @@ var rpcAskWallet = map[string]struct{}{ "createencryptedwallet": {}, "createmultisig": {}, "dumpprivkey": {}, + "fundrawtransaction": {}, "getaccount": {}, "getaccountaddress": {}, "getaddressesbyaccount": {},