Skip to content

Commit

Permalink
add prefund command
Browse files Browse the repository at this point in the history
  • Loading branch information
hayeah committed Dec 11, 2017
1 parent 6c2efa9 commit 7519b23
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 5 deletions.
10 changes: 10 additions & 0 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ func (c *solarCLI) ContractsRepository() *contract.ContractsRepository {
return c.repo
}

func (c *solarCLI) QtumRPC() *qtum.RPC {
rpc, err := qtum.NewRPC(*qtumRPC)
if err != nil {
fmt.Println("Invalid QTUM RPC URL:", *qtumRPC)
os.Exit(1)
}

return rpc
}

func (c *solarCLI) Deployer() (deployer deployer.Deployer) {
log := log.New(os.Stderr, "", log.Lshortfile)

Expand Down
3 changes: 3 additions & 0 deletions contract/contractsRepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ type DeployedContract struct {
TransactionID Bytes `json:"txid"`
CreatedAt time.Time `json:"createdAt"`
Confirmed bool `json:"confirmed"`

// qtum
Sender string `json:"sender,omitempty"`
}

type ContractsRepository struct {
Expand Down
7 changes: 4 additions & 3 deletions deployer/qtum/qtumDeployer.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
)

type Deployer struct {
rpc *rpc
rpc *RPC
*contract.ContractsRepository
}

func NewDeployer(rpcURL *url.URL, repo *contract.ContractsRepository) (*Deployer, error) {
return &Deployer{
rpc: &rpc{
rpc: &RPC{
BaseURL: rpcURL,
},
ContractsRepository: repo,
Expand Down Expand Up @@ -49,7 +49,7 @@ func (d *Deployer) ConfirmContract(c *contract.DeployedContract) (err error) {
}

func (d *Deployer) CreateContract(c *contract.CompiledContract, jsonParams []byte, name string, overwrite bool, aslib bool) (err error) {
if overwrite {
if !overwrite {
if aslib && d.LibExists(name) {
return errors.Errorf("library name already used: %s", name)
} else if !aslib && d.Exists(name) {
Expand Down Expand Up @@ -82,6 +82,7 @@ func (d *Deployer) CreateContract(c *contract.CompiledContract, jsonParams []byt
TransactionID: tx.TxID,
Address: tx.Address,
CreatedAt: time.Now(),
Sender: tx.Sender,
}

if aslib {
Expand Down
15 changes: 13 additions & 2 deletions deployer/qtum/qtumRPC.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,22 @@ type TransactionReceipt struct {
Address contract.Bytes `json:"address"`
}

type rpc struct {
func NewRPC(baseurl string) (*RPC, error) {
u, err := url.Parse(baseurl)
if err != nil {
return nil, err
}

return &RPC{
BaseURL: u,
}, nil
}

type RPC struct {
BaseURL *url.URL
}

func (rpc *rpc) Call(result interface{}, method string, params ...interface{}) (err error) {
func (rpc *RPC) Call(result interface{}, method string, params ...interface{}) (err error) {
url := rpc.BaseURL

jsonReq := jsonRPCRequest{
Expand Down
65 changes: 65 additions & 0 deletions prefund.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package solar

import (
"encoding/json"
"fmt"
"strings"
)

func init() {
cli := app.Command("prefund", "(qtum) fund an owner address with uxtos")

owner := cli.Arg("owner", "contract name or address to fund").Required().String()

amount := cli.Arg("amount", "fund an uxto with this amount").Required().Float64()
multiples := cli.Arg("multiples", "fund this number of identical uxtos").Default("1").Int()

appTasks["prefund"] = func() (err error) {
rpc := solar.QtumRPC()

repo := solar.ContractsRepository()

var ownerAddr string

contract, found := repo.Get(*owner)
if found {
ownerAddr = contract.Sender
} else {
ownerAddr = *owner
}

// The JSON object is allowed to have duplicate keys for this call
// { <addr>: <amount>, ... }

var uxtos []string
for i := 0; i < *multiples; i++ {
uxto := fmt.Sprintf(`"%s": %f`, ownerAddr, *amount)
uxtos = append(uxtos, uxto)
}

amounts := "{\n" + strings.Join(uxtos, ",\n") + "\n}"

// fmt.Println("jsonuxtos", amounts)

var result interface{}

/*
sendmanywithdupes "fromaccount" {"address":amount,...} ( minconf "comment" ["address",...] )
1. "fromaccount" (string, required) DEPRECATED. The account to send the funds from. Should be "" for the default account
2. "amounts" (string, required) A json object with addresses and amounts
{
"address":amount (numeric or string) The qtum address is the key, the numeric amount (can be string) in QTUM is the value
,...
}
*/
err = rpc.Call(&result, "sendmanywithdupes", "", json.RawMessage(amounts))
if err != nil {
return
}

fmt.Println("sendmanywithdupes txid:", result)

return
}
}

0 comments on commit 7519b23

Please sign in to comment.