Skip to content

Commit

Permalink
add wallet v2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Command M committed Jan 29, 2020
1 parent 0be6d72 commit 808281a
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
47 changes: 39 additions & 8 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,11 @@ type config struct {
BlockTime int `json:"block_time"`
} `json:"node"`
Wallet struct {
Address string `json:"address"`
OwnerAPIPort int `json:"owner_api_port"`
AuthUser string `json:"auth_user"`
AuthPass string `json:"auth_pass"`
Address string `json:"address"`
OwnerAPIVersion string `json:"owner_api_version"`
OwnerAPIPort int `json:"owner_api_port"`
AuthUser string `json:"auth_user"`
AuthPass string `json:"auth_pass"`
} `json:"wallet"`
Payer struct {
Time string `json:"time"`
Expand Down
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
},
"wallet": {
"address": "127.0.0.1",
"owner_api_version": "v1",
"owner_api_port": 3420,
"auth_user": "epic",
"auth_pass": "FoakPxzMy56Z4gJPCHKZ"
Expand Down
72 changes: 70 additions & 2 deletions payer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ type payer struct {
conf *config
}

func (p *payer) getNewBalance() uint64 {
type jsonRPCResponse struct {
ID string `json:"id"`
JsonRpc string `json:"jsonrpc"`
Method string `json:"method"`
Result interface{} `json:"result"`
Error map[string]interface{} `json:"error"`
}

// deprecated v1 wallet owner api
func (p *payer) getNewBalanceV1() uint64 {
req, _ := http.NewRequest("GET", "http://"+p.conf.Wallet.Address+":"+strconv.Itoa(p.conf.Wallet.OwnerAPIPort)+"/v1/wallet/owner/retrieve_summary_info?refresh", nil)
req.SetBasicAuth(p.conf.Node.AuthUser, p.conf.Node.AuthPass)
client := &http.Client{}
Expand All @@ -35,6 +44,55 @@ func (p *payer) getNewBalance() uint64 {
return uint64(spendable) // unit nanogrin
}

// The V2 Owner API (OwnerRpc) will be removed in grin-wallet 4.0.0. Please migrate to the V3 (OwnerRpcS) API as soon as possible.
func (p *payer) getNewBalanceV2() uint64 {
req, _ := http.NewRequest("POST", "http://"+p.conf.Wallet.Address+":"+strconv.Itoa(p.conf.Wallet.OwnerAPIPort)+"/v2/wallet/owner", strings.NewReader(`{
"jsonrpc": "2.0",
"method": "retrieve_summary_info",
"params": [true, 10],
"id": 1
}`))
req.SetBasicAuth(p.conf.Node.AuthUser, p.conf.Node.AuthPass)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
logger.Error("failed to get balance from wallet for failing to post request, treat this as no income")
return 0
}

dec := json.NewDecoder(res.Body)
var summaryInfo jsonRPCResponse
_ = dec.Decode(&summaryInfo)

result := summaryInfo.Result
if result == nil {
logger.Error(summaryInfo.Error)
logger.Error("failed to get balance from wallet for failing to get result request, treat this as no income")
return 0
}

mapResult, ok := result.(map[string]interface{})
if ok || mapResult["Ok"] != nil {
theOk, ok := mapResult["Ok"].([]interface{})
if ok {
for i := range theOk {
if balanceMap, ok := theOk[i].(map[string]interface{}); ok {
strSpendable := balanceMap["amount_currently_spendable"].(string)
spendable, _ := strconv.Atoi(strSpendable)
return uint64(spendable) // unit nanogrin
}
}
}
}

return 0
}

func (p *payer) getNewBalanceV3() uint64 {
logger.Fatal("V3 support WIP!")
return 0
}

// distribute coins when balance is > 1e9 nano
func (p *payer) distribute(newBalance uint64) {
// get a distribution table
Expand All @@ -54,6 +112,16 @@ func (p *payer) watch() {
logger.Error(err)
}

var getNewBalance func() uint64
switch p.conf.Wallet.OwnerAPIVersion {
case "v1":
getNewBalance = p.getNewBalanceV1
case "v2":
getNewBalance = p.getNewBalanceV2
case "v3":
getNewBalance = p.getNewBalanceV3
}

for {
now := time.Now()
t := time.Date(now.Year(), now.Month(), now.Day(), hour, min, 0, 0, now.Location())
Expand All @@ -65,7 +133,7 @@ func (p *payer) watch() {

select {
case <-timer.C:
newBalance := p.getNewBalance()
newBalance := getNewBalance()
if newBalance > 1e9 {
p.distribute(newBalance - 1e9)
} else {
Expand Down

2 comments on commit 808281a

@kevin070982
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this still compatible with EPIC CASH?

@c0mm4nd
Copy link
Member

Choose a reason for hiding this comment

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

compatible when using "owner_api_version": "v1",

Please sign in to comment.