From ce226cc20ce6a38fe56728c91432db9edd7cb272 Mon Sep 17 00:00:00 2001 From: Nikhil Saraf <1028334+nikhilsaraf@users.noreply.github.com> Date: Tue, 19 Mar 2019 17:49:15 -0700 Subject: [PATCH] use FeeStats() from new Go SDK (#129), closes #124 need to run `glide install` to get latest SDK and other dependencies do NOT run `glide up` (bug in glide and one of the libraries we use) --- glide.lock | 47 +++++++++++++------- glide.yaml | 18 +++++++- plugins/batchedExchange.go | 5 ++- plugins/sdexExtensions.go | 87 ++++++++++++++++++++++---------------- 4 files changed, 102 insertions(+), 55 deletions(-) diff --git a/glide.lock b/glide.lock index acad24ab8..5c4638560 100644 --- a/glide.lock +++ b/glide.lock @@ -1,14 +1,10 @@ -hash: 846bc46b777e613b79332fe3dd038931e16d76b8b9da1c096bb2082fc232b82e -updated: 2018-12-06T12:00:37.303778558-08:00 +hash: 81ad219d2939a6ac4e4bbd94d3cebf8f1ea07291ae882f7f93ad781c8cf27ce6 +updated: 2019-03-19T16:40:20.118158839-07:00 imports: - name: cloud.google.com/go version: 793297ec250352b0ece46e103381a0fc3dab95a1 subpackages: - compute/metadata -- name: github.com/agl/ed25519 - version: 278e1ec8e8a6e017cd07577924d6766039146ced - subpackages: - - edwards25519 - name: github.com/asaskevich/govalidator version: f9ffefc3facfbe0caee3fea233cbb6e8208f4541 - name: github.com/Beldur/kraken-go-api-client @@ -31,14 +27,20 @@ imports: - query - name: github.com/inconshreveable/mousetrap version: 76626ae9c91c4f2a10f34cad8ce83ea42c93bb75 +- name: github.com/kr/pretty + version: e6ac2fc51e89a3249e82157fa0bb7a18ef9dd5bb - name: github.com/kr/session version: 6f756089c21ec9a4ceb14473bf90d5e3a3e9e303 +- name: github.com/kr/text + version: e373e137fafd8abd480af49182dea0513914adb4 - name: github.com/lechengfan/googleauth version: 7595ba02fbce171759c10d69d96e4cd898d1fa93 - name: github.com/lib/pq version: 50761b0867bd1d9d069276790bcd4a3bccf2324a subpackages: - oid +- name: github.com/magiconair/properties + version: 359442d561ca28acd0fe503aa9f075f505bc9ed0 - name: github.com/manucorporat/sse version: ee05b128a739a0fb76c7ebd3ae4810c1de808d6d - name: github.com/mitchellh/mapstructure @@ -47,10 +49,6 @@ imports: version: 19004f22be08c82a22e679726ca22853c65919ae subpackages: - multithreading -- name: github.com/nullstyle/go-xdr - version: a875e7c9fa2388ce03279f6b5ba1c2ccd1f9e917 - subpackages: - - xdr3 - name: github.com/PagerDuty/go-pagerduty version: 635c5ce271490fba94880e62cde4eea3c1c184b9 - name: github.com/pkg/errors @@ -62,35 +60,49 @@ imports: - name: github.com/segmentio/go-loggly version: eb91657e62b25699dd4e84d5562735440d4beace - name: github.com/sirupsen/logrus - version: 68cec9f21fbf3ea8d8f98c044bc6ce05f17b267a + version: 070c81def33f6362a8267b6a4e56fb7bf23fc6b5 subpackages: - hooks/test +- name: github.com/spf13/cast + version: 4d07383ffe94b5e5a6fa3af9211374a4507a0184 - name: github.com/spf13/cobra version: 7b2c5ac9fc04fc5efafb60700713d4fa609b777b +- name: github.com/spf13/jwalterweatherman + version: 3d60171a64319ef63c78bd45bd60e6eab1e75f8b - name: github.com/spf13/pflag version: e57e3eeb33f795204c1ca35f56c44f83227c6e66 +- name: github.com/spf13/viper + version: db7ff930a189b98d602179d9001d33345f42b8c7 - name: github.com/stellar/go - version: 5bbd27814a3ffca9aeffcbd75a09a6164959776a + version: 61ed9984b1f88fda0d4e5e375021b404d9900959 subpackages: - amount - build - clients/horizon - crc16 + - exp/clients/horizon - hash - keypair - network - price - protocols/horizon - protocols/horizon/base + - protocols/horizon/effects - strkey + - support/app - support/config - support/errors - support/http/mutil - support/log - support/render/hal - support/render/problem + - support/strutils - support/url - xdr +- name: github.com/stellar/go-xdr + version: 0bc96f33a18ef2f963e75431dbed042129eb421b + subpackages: + - xdr3 - name: github.com/stretchr/objx version: cbeaeb16a013161a98496fad62933b1d21786672 - name: github.com/stretchr/testify @@ -99,12 +111,14 @@ imports: - assert - mock - name: golang.org/x/crypto - version: e84da0312774c21d64ee2317962ef669b27ffb41 + version: 2509b142fb2b797aa7587dad548f113b2c0f20ce subpackages: - - internal/subtle + - ed25519 + - ed25519/internal/edwards25519 - nacl/secretbox - poly1305 - salsa20/salsa + - ssh/terminal - name: golang.org/x/net version: 9b4f9f5ad5197c79fd623a3638e70d8b26cef344 subpackages: @@ -118,9 +132,10 @@ imports: - jws - jwt - name: golang.org/x/sys - version: 1f5e250e1174502017917628cc48b52fdc25b531 + version: 6c81ef8f67ca3f42fc9cd71dfbd5f35b0c4b5771 subpackages: - unix + - windows - name: google.golang.org/appengine version: ae0ab99deb4dc413a2b4bd6c8bdd0eb67f1e4d06 subpackages: @@ -133,4 +148,6 @@ imports: - internal/remote_api - internal/urlfetch - urlfetch +- name: gopkg.in/yaml.v2 + version: 7ad95dd0798a40da1ccdff6dff35fd177b5edf40 testImports: [] diff --git a/glide.yaml b/glide.yaml index f8d5ffa7c..7c36f4139 100644 --- a/glide.yaml +++ b/glide.yaml @@ -9,12 +9,28 @@ import: - package: github.com/spf13/pflag version: v1.0.0 - package: github.com/stellar/go - version: 5bbd27814a3ffca9aeffcbd75a09a6164959776a + version: 61ed9984b1f88fda0d4e5e375021b404d9900959 subpackages: - build - clients/horizon - support/config - support/errors +############################################################## +# manually added dependencies to fix glide import resolution +############################################################## +- package: github.com/sirupsen/logrus + version: 070c81def33f6362a8267b6a4e56fb7bf23fc6b5 +- package: github.com/Sirupsen/logrus + repo: git@github.com:sirupsen/logrus + vcs: git + version: 070c81def33f6362a8267b6a4e56fb7bf23fc6b5 +- package: golang.org/x/crypto + version: 2509b142fb2b797aa7587dad548f113b2c0f20ce +- package: golang.org/x/sys + version: 6c81ef8f67ca3f42fc9cd71dfbd5f35b0c4b5771 +############################################################## +# /manually added dependencies to fix glide import resolution +############################################################## - package: github.com/PagerDuty/go-pagerduty version: 635c5ce271490fba94880e62cde4eea3c1c184b9 - package: github.com/lechengfan/googleauth diff --git a/plugins/batchedExchange.go b/plugins/batchedExchange.go index d73e93f34..a3676602f 100644 --- a/plugins/batchedExchange.go +++ b/plugins/batchedExchange.go @@ -329,9 +329,10 @@ func (b BatchedExchange) OpenOrders2Offers(orders []model.OpenOrder, baseAsset h b.offerID2OrderID[ID] = order.ID } - var lmt time.Time + var lmt *time.Time if order.Timestamp != nil { - lmt = time.Unix(int64(*order.Timestamp)/1000, 0) + lastModTime := time.Unix(int64(*order.Timestamp)/1000, 0) + lmt = &lastModTime } offers = append(offers, hProtocol.Offer{ ID: ID, diff --git a/plugins/sdexExtensions.go b/plugins/sdexExtensions.go index a79e3347b..35b6b2634 100644 --- a/plugins/sdexExtensions.go +++ b/plugins/sdexExtensions.go @@ -4,9 +4,9 @@ import ( "fmt" "log" "net/http" - "strconv" - "github.com/stellar/kelp/support/networking" + "github.com/stellar/go/exp/clients/horizon" + hProtocol "github.com/stellar/go/protocols/horizon" ) // OpFeeStroops computes fees per operation @@ -49,34 +49,26 @@ func SdexFeeFnFromStats( return nil, fmt.Errorf("unable to create SdexFeeFnFromStats, maxOpFeeStroops should be >= %d (baseFeeStroops): %d", baseFeeStroops, maxOpFeeStroops) } + client := &horizonclient.Client{ + // TODO horizonclient.Client has a bug in it where it does not use "/" to separate the horizonURL from the fee_stats endpoint + HorizonURL: horizonBaseURL + "/", + HTTP: http.DefaultClient, + } + return func() (uint64, error) { - return getFeeFromStats(horizonBaseURL, capacityTrigger, percentile, maxOpFeeStroops) + return getFeeFromStats(client, capacityTrigger, percentile, maxOpFeeStroops) }, nil } -func getFeeFromStats(horizonBaseURL string, capacityTrigger float64, percentile uint8, maxOpFeeStroops uint64) (uint64, error) { - feeStatsURL := horizonBaseURL + "/fee_stats" - - feeStatsResponseMap := map[string]string{} - e := networking.JSONRequest(http.DefaultClient, "GET", feeStatsURL, "", map[string]string{}, &feeStatsResponseMap, "") - if e != nil { - return 0, fmt.Errorf("error fetching fee stats (URL=%s): %s", feeStatsURL, e) - } - - // parse ledgerCapacityUsage - ledgerCapacityUsage, e := strconv.ParseFloat(feeStatsResponseMap["ledger_capacity_usage"], 64) +func getFeeFromStats(horizonClient horizonclient.ClientInterface, capacityTrigger float64, percentile uint8, maxOpFeeStroops uint64) (uint64, error) { + feeStats, e := horizonClient.FeeStats() if e != nil { - return 0, fmt.Errorf("could not parse ledger_capacity_usage ('%s') as float64: %s", feeStatsResponseMap["ledger_capacity_usage"], e) + return 0, fmt.Errorf("error fetching fee stats: %s", e) } // case where we don't trigger the dynamic fees logic - if ledgerCapacityUsage < capacityTrigger { - var lastFeeInt int - lastFeeInt, e = strconv.Atoi(feeStatsResponseMap["last_ledger_base_fee"]) - if e != nil { - return 0, fmt.Errorf("could not parse last_ledger_base_fee ('%s') as int: %s", feeStatsResponseMap["last_ledger_base_fee"], e) - } - lastFee := uint64(lastFeeInt) + if feeStats.LedgerCapacityUsage < capacityTrigger { + lastFee := uint64(feeStats.LastLedgerBaseFee) if lastFee <= maxOpFeeStroops { log.Printf("lastFee <= maxOpFeeStroops; using last_ledger_base_fee of %d stroops (maxOpFeeStroops = %d)\n", lastFee, maxOpFeeStroops) return lastFee, nil @@ -86,23 +78,44 @@ func getFeeFromStats(horizonBaseURL string, capacityTrigger float64, percentile } // parse percentile value - var pStroopsInt64 uint64 - pKey := fmt.Sprintf("p%d_accepted_fee", percentile) - if pStroops, ok := feeStatsResponseMap[pKey]; ok { - var pStroopsInt int - pStroopsInt, e = strconv.Atoi(pStroops) - if e != nil { - return 0, fmt.Errorf("could not parse percentile value (%s='%s'): %s", pKey, pStroops, e) - } - pStroopsInt64 = uint64(pStroopsInt) - } else { - return 0, fmt.Errorf("could not fetch percentile value (%s) from feeStatsResponseMap: %s", pKey, e) + acceptedFee, e := getAcceptedFee(&feeStats, percentile) + if e != nil { + return 0, fmt.Errorf("could not fetch accepted fee: %s", e) } + acceptedFeeInt64 := uint64(acceptedFee) - if pStroopsInt64 <= maxOpFeeStroops { - log.Printf("pStroopsInt64 <= maxOpFeeStroops; using %s of %d stroops (maxOpFeeStroops = %d)\n", pKey, pStroopsInt64, maxOpFeeStroops) - return pStroopsInt64, nil + if acceptedFeeInt64 <= maxOpFeeStroops { + log.Printf("acceptedFeeInt64 <= maxOpFeeStroops; using acceptedFee of %d stroops at percentile=%d (maxOpFeeStroops=%d)\n", acceptedFeeInt64, percentile, maxOpFeeStroops) + return acceptedFeeInt64, nil } - log.Printf("pStroopsInt64 > maxOpFeeStroops; using maxOpFeeStroops of %d stroops (%s = %d)\n", maxOpFeeStroops, pKey, pStroopsInt64) + log.Printf("acceptedFeeInt64 > maxOpFeeStroops; using maxOpFeeStroops of %d stroops (percentile=%d, acceptedFee=%d stroops)\n", maxOpFeeStroops, percentile, acceptedFeeInt64) return maxOpFeeStroops, nil } + +func getAcceptedFee(fs *hProtocol.FeeStats, percentile uint8) (int, error) { + switch percentile { + case 10: + return fs.P10AcceptedFee, nil + case 20: + return fs.P20AcceptedFee, nil + case 30: + return fs.P30AcceptedFee, nil + case 40: + return fs.P40AcceptedFee, nil + case 50: + return fs.P50AcceptedFee, nil + case 60: + return fs.P60AcceptedFee, nil + case 70: + return fs.P70AcceptedFee, nil + case 80: + return fs.P80AcceptedFee, nil + case 90: + return fs.P90AcceptedFee, nil + case 95: + return fs.P95AcceptedFee, nil + case 99: + return fs.P99AcceptedFee, nil + } + return 0, fmt.Errorf("unable to get accepted fee for percentile: %d", percentile) +}