|
| 1 | +package plugins |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +const wantLowerBoundXLM = 0.04 |
| 11 | +const wantUpperBoundXLM = 0.08 |
| 12 | + |
| 13 | +func TestMakePriceFeed(t *testing.T) { |
| 14 | + testCases := []struct { |
| 15 | + typ string |
| 16 | + url string |
| 17 | + wantLowerOrEqualBound float64 |
| 18 | + wantHigherOrEqualBound float64 |
| 19 | + }{ |
| 20 | + { |
| 21 | + typ: "exchange", |
| 22 | + url: "kraken/XXLM/ZUSD/mid", |
| 23 | + wantLowerOrEqualBound: wantLowerBoundXLM, |
| 24 | + wantHigherOrEqualBound: wantUpperBoundXLM, |
| 25 | + }, { |
| 26 | + typ: "exchange", |
| 27 | + url: "ccxt-kraken/XLM/USD/last", |
| 28 | + wantLowerOrEqualBound: wantLowerBoundXLM, |
| 29 | + wantHigherOrEqualBound: wantUpperBoundXLM, |
| 30 | + }, { |
| 31 | + typ: "exchange", |
| 32 | + url: "ccxt-binance/XLM/USDT/bid", |
| 33 | + wantLowerOrEqualBound: wantLowerBoundXLM, |
| 34 | + wantHigherOrEqualBound: wantUpperBoundXLM, |
| 35 | + }, { |
| 36 | + typ: "exchange", |
| 37 | + url: "ccxt-coinbasepro/XLM/USD/ask", |
| 38 | + wantLowerOrEqualBound: wantLowerBoundXLM, |
| 39 | + wantHigherOrEqualBound: wantUpperBoundXLM, |
| 40 | + }, { |
| 41 | + typ: "fixed", |
| 42 | + url: "1.23456", |
| 43 | + wantLowerOrEqualBound: 1.23456, |
| 44 | + wantHigherOrEqualBound: 1.23456, |
| 45 | + }, { |
| 46 | + typ: "sdex", |
| 47 | + url: "XLM:/USD:GDUKMGUGDZQK6YHYA5Z6AY2G4XDSZPSZ3SW5UN3ARVMO6QSRDWP5YLEX", |
| 48 | + wantLowerOrEqualBound: wantLowerBoundXLM, |
| 49 | + wantHigherOrEqualBound: wantUpperBoundXLM, |
| 50 | + }, { |
| 51 | + typ: "sdex", |
| 52 | + url: "USD:GDUKMGUGDZQK6YHYA5Z6AY2G4XDSZPSZ3SW5UN3ARVMO6QSRDWP5YLEX/XLM:", |
| 53 | + wantLowerOrEqualBound: 1 / wantUpperBoundXLM, |
| 54 | + wantHigherOrEqualBound: 1 / wantLowerBoundXLM, |
| 55 | + }, { |
| 56 | + typ: "function", |
| 57 | + url: "max(fixed/1.0,fixed/1.4)", |
| 58 | + wantLowerOrEqualBound: 1.4, |
| 59 | + wantHigherOrEqualBound: 1.4, |
| 60 | + }, { |
| 61 | + typ: "function", |
| 62 | + url: "max(fixed/0.02,exchange/ccxt-kraken/XLM/USD/last)", |
| 63 | + wantLowerOrEqualBound: wantLowerBoundXLM, |
| 64 | + wantHigherOrEqualBound: wantUpperBoundXLM, |
| 65 | + }, { |
| 66 | + typ: "function", |
| 67 | + url: "invert(fixed/0.02)", |
| 68 | + wantLowerOrEqualBound: 50.0, |
| 69 | + wantHigherOrEqualBound: 50.0, |
| 70 | + }, |
| 71 | + // not testing fiat here because it requires an access key |
| 72 | + // not testing crypto here because it's returning an error when passed an actual URL but works in practice |
| 73 | + } |
| 74 | + |
| 75 | + // cannot run this in parallel because ccxt fails (by not recognizing exchanges) when hit with too many requests at once |
| 76 | + for _, k := range testCases { |
| 77 | + t.Run(k.typ+"/"+k.url, func(t *testing.T) { |
| 78 | + pf, e := MakePriceFeed(k.typ, k.url) |
| 79 | + if !assert.NoError(t, e) { |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + price, e := pf.GetPrice() |
| 84 | + if !assert.NoError(t, e) { |
| 85 | + return |
| 86 | + } |
| 87 | + |
| 88 | + assert.True(t, price >= k.wantLowerOrEqualBound, fmt.Sprintf("price was %.10f, should have been >= %.10f", price, k.wantLowerOrEqualBound)) |
| 89 | + assert.True(t, price <= k.wantHigherOrEqualBound, fmt.Sprintf("price was %.10f, should have been <= %.10f", price, k.wantHigherOrEqualBound)) |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
0 commit comments