Skip to content

Commit

Permalink
dcrjson: add fundrawtransaction command
Browse files Browse the repository at this point in the history
  • Loading branch information
vctt94 committed Jul 12, 2018
1 parent ddd66de commit 2c9d3bb
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
25 changes: 25 additions & 0 deletions dcrjson/walletsvrcmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,30 @@ func NewEstimatePriorityCmd(numBlocks int64) *EstimatePriorityCmd {
}
}

// FundRawTransactionCmd is a type handling custom marshaling and
// unmarshaling of fundrawtransaction JSON wallet extension commands.
type FundRawTransactionCmd struct {
HexString string
FundAccount string
ChangeAccount *string
LockUnspents *bool `jsonrpcdefault:"false"`
FeeRate *float64
RequiredConfirmations *int32
}

// NewFundRawTransactionCmd returns a new instance which can be used to issue a
// fundrawtransaction JSON-RPC command.
func NewFundRawTransactionCmd(hexString string, fundAccount string, changeAccount *string, lockUnspents *bool, feeRate *float64, requiredConfirmations *int32) *FundRawTransactionCmd {
return &FundRawTransactionCmd{
HexString: hexString,
FundAccount: fundAccount,
ChangeAccount: changeAccount,
LockUnspents: lockUnspents,
FeeRate: feeRate,
RequiredConfirmations: requiredConfirmations,
}
}

// GenerateVoteCmd is a type handling custom marshaling and
// unmarshaling of generatevote JSON wallet extension commands.
type GenerateVoteCmd struct {
Expand Down Expand Up @@ -1270,6 +1294,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, changeAccount string, lockUnspents bool, feeRate float64, requiredConfirmations int32) FutureFundRawTransactionResult {
cmd := dcrjson.NewFundRawTransactionCmd(rawhex, fundAccount, &changeAccount, &lockUnspents, &feeRate, &requiredConfirmations)
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, changeAccount string, lockUnspents bool, feeRate float64, requiredConfirmations int32) (*dcrjson.FundRawTransactionResult, error) {
return c.FundRawTransactionAsync(rawhex, fundAccount, changeAccount, lockUnspents, feeRate, requiredConfirmations).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

0 comments on commit 2c9d3bb

Please sign in to comment.