diff --git a/.idea/modules.xml b/.idea/modules.xml
index 1739e06..f4106b0 100644
--- a/.idea/modules.xml
+++ b/.idea/modules.xml
@@ -2,7 +2,7 @@
-
+
\ No newline at end of file
diff --git a/.idea/SimplistPool.iml b/.idea/open-grin-pool.iml
similarity index 100%
rename from .idea/SimplistPool.iml
rename to .idea/open-grin-pool.iml
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 762485e..ed56273 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -2,7 +2,13 @@
+
+
+
+
+
+
@@ -12,24 +18,49 @@
+
+
+
+
+
+
+
+
+
-
-
\ No newline at end of file
diff --git a/config.go b/config.go
index 3af6be9..60e131f 100644
--- a/config.go
+++ b/config.go
@@ -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"`
diff --git a/config.json b/config.json
index 8ae9fe0..7ed1343 100644
--- a/config.json
+++ b/config.json
@@ -32,6 +32,7 @@
},
"wallet": {
"address": "127.0.0.1",
+ "owner_api_version": "v1",
"owner_api_port": 3420,
"auth_user": "epic",
"auth_pass": "FoakPxzMy56Z4gJPCHKZ"
diff --git a/payer.go b/payer.go
index a96650a..e55b4df 100644
--- a/payer.go
+++ b/payer.go
@@ -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{}
@@ -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
@@ -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())
@@ -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 {