From b63f7b29d2b20119b7c199c34e969e4781640da3 Mon Sep 17 00:00:00 2001 From: JoeGruff Date: Fri, 30 Jun 2023 14:25:06 +0900 Subject: [PATCH] loadbot: Add live price oracles. Add a boolean livemidgap that allows loadbot start with a live rate based on mainnet coin USD values. This also alters ts to keep the midgap close to live rates rather than random values. --- client/core/exchangeratefetcher.go | 18 +++---- dex/testing/eth/harness.sh | 21 ++++++-- dex/testing/loadbot/README.md | 3 +- dex/testing/loadbot/loadbot.go | 86 +++++++++++++++++++++++++----- dex/testing/loadbot/whale.go | 39 ++++++++++---- 5 files changed, 133 insertions(+), 34 deletions(-) diff --git a/client/core/exchangeratefetcher.go b/client/core/exchangeratefetcher.go index 704629250d..b943452768 100644 --- a/client/core/exchangeratefetcher.go +++ b/client/core/exchangeratefetcher.go @@ -64,9 +64,9 @@ var ( // fiatRateFetchers is the list of all supported fiat rate fetchers. var fiatRateFetchers = map[string]rateFetcher{ - coinpaprika: fetchCoinpaprikaRates, - dcrdataDotOrg: fetchDcrdataRates, - messari: fetchMessariRates, + coinpaprika: FetchCoinpaprikaRates, + dcrdataDotOrg: FetchDcrdataRates, + messari: FetchMessariRates, } // fiatRateInfo holds the fiat rate and the last update time for an @@ -137,10 +137,10 @@ func newCommonRateSource(fetcher rateFetcher) *commonRateSource { } } -// fetchCoinpaprikaRates retrieves and parses fiat rate data from the +// FetchCoinpaprikaRates retrieves and parses fiat rate data from the // Coinpaprika API. See https://api.coinpaprika.com/#operation/getTickersById // for sample request and response information. -func fetchCoinpaprikaRates(ctx context.Context, log dex.Logger, assets map[uint32]*SupportedAsset) map[uint32]float64 { +func FetchCoinpaprikaRates(ctx context.Context, log dex.Logger, assets map[uint32]*SupportedAsset) map[uint32]float64 { fiatRates := make(map[uint32]float64) fetchRate := func(sa *SupportedAsset) { assetID := sa.ID @@ -190,9 +190,9 @@ func fetchCoinpaprikaRates(ctx context.Context, log dex.Logger, assets map[uint3 return fiatRates } -// fetchDcrdataRates retrieves and parses fiat rate data from dcrdata +// FetchDcrdataRates retrieves and parses fiat rate data from dcrdata // exchange rate API. -func fetchDcrdataRates(ctx context.Context, log dex.Logger, assets map[uint32]*SupportedAsset) map[uint32]float64 { +func FetchDcrdataRates(ctx context.Context, log dex.Logger, assets map[uint32]*SupportedAsset) map[uint32]float64 { assetBTC := assets[btcBipID] assetDCR := assets[dcrBipID] noBTCAsset := assetBTC == nil || assetBTC.Wallet == nil @@ -222,10 +222,10 @@ func fetchDcrdataRates(ctx context.Context, log dex.Logger, assets map[uint32]*S return fiatRates } -// fetchMessariRates retrieves and parses fiat rate data from the Messari API. +// FetchMessariRates retrieves and parses fiat rate data from the Messari API. // See https://messari.io/api/docs#operation/Get%20Asset%20Market%20Data for // sample request and response information. -func fetchMessariRates(ctx context.Context, log dex.Logger, assets map[uint32]*SupportedAsset) map[uint32]float64 { +func FetchMessariRates(ctx context.Context, log dex.Logger, assets map[uint32]*SupportedAsset) map[uint32]float64 { fiatRates := make(map[uint32]float64) fetchRate := func(sa *SupportedAsset) { assetID := sa.ID diff --git a/dex/testing/eth/harness.sh b/dex/testing/eth/harness.sh index 3d1d86fe0d..91d73e0055 100755 --- a/dex/testing/eth/harness.sh +++ b/dex/testing/eth/harness.sh @@ -115,13 +115,28 @@ cat > "${NODES_ROOT}/genesis.json" < 0 || int64(midGap)+int64(tweak) > int64(rateStep) { - target = truncate(int64(midGap)+int64(tweak), int64(rateStep)) + var target uint64 + // If we are trying to stay close to the mid gap, have the whale push + // us there. Otherwise set a random target. + if liveMidGap { + r, err := liveRate() + if err != nil { + m.log.Errorf("error retrieving live rates: %v", err) + target = midGap + } else { + target = truncate(int64(r*float64(rateEncFactor)), int64(rateStep)) + } + } else { + tweak := rand.Float64() * float64(1-(2*rand.Intn(2))) * float64(midGap) * whalePercent + target = rateStep + if tweak > 0 || int64(midGap)+int64(tweak) > int64(rateStep) { + target = truncate(int64(midGap)+int64(tweak), int64(rateStep)) + } } + conversionRatio := float64(conversionFactors[quoteSymbol]) / float64(conversionFactors[baseSymbol]) + m.log.Infof("TS whaling at %v rate.", float64(target)/float64(rateEncFactor)/conversionRatio) sell := true if target > midGap { sell = false @@ -204,7 +225,7 @@ func (*whale) whale(m *Mantle) { rem = 0 } - m.log.Infof("Whaling with %d lots at %v", lots, float64(target)/float64(conversionFactors[quoteSymbol])) + m.log.Infof("Whaling with %d lots at %v", lots, float64(target)/float64(rateEncFactor)/conversionRatio) for _, man := range wMantles { for i := 0; i < int(nMaxOrd); i++ {