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

dcrjson: add fundrawtransaction command #1316

Merged
merged 3 commits into from
Jul 29, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions dcrjson/walletsvrcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,33 @@ func NewEstimatePriorityCmd(numBlocks int64) *EstimatePriorityCmd {
}
}

// FundRawTransactionOptions represents the optional inputs to fund
// a raw transaction.
type FundRawTransactionOptions struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type must use json:"..." struct tags to provide the key name

ChangeAccount *string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change account is not part of the equivalent bitcoind RPC (instead, any address can be provided as an option). Why is this different?

LockUnspents *bool `jsonrpcdefault:"false"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not add this for now. Locking outputs doesn't actually work in dcrwallet currently.

FeeRate *float64
RequiredConfirmations *int32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bitcoind names this 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 {
Expand Down Expand Up @@ -1270,6 +1297,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)
Expand Down
6 changes: 6 additions & 0 deletions dcrjson/walletsvrresults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
38 changes: 38 additions & 0 deletions rpcclient/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ var rpcAskWallet = map[string]struct{}{
"createencryptedwallet": {},
"createmultisig": {},
"dumpprivkey": {},
"fundrawtransaction": {},
"getaccount": {},
"getaccountaddress": {},
"getaddressesbyaccount": {},
Expand Down