Skip to content

Commit

Permalink
Merge pull request #8 from chronicleprotocol/binance_us
Browse files Browse the repository at this point in the history
Added BinanceUS as mock
  • Loading branch information
konstantinzolotarev authored Oct 5, 2022
2 parents 152481d + 1409e3a commit 7039e33
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
68 changes: 68 additions & 0 deletions origin/binance_us.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package origin

import (
"fmt"

"github.com/chronicleprotocol/infestor/smocker"
)

type BinanceUS struct{}

func (b BinanceUS) BuildMocks(e []ExchangeMock) ([]*smocker.Mock, error) {
mocksOne, err := CombineMocks(e, b.build)
if err != nil {
return nil, err
}
tickers, err := CombineMocks(e, b.buildWholeDay)
if err != nil {
return nil, err
}
return append(mocksOne, tickers...), nil
}

func (b BinanceUS) buildWholeDay(e ExchangeMock) (*smocker.Mock, error) {
symbol := e.Symbol.Format("%s%s")
body := `[{"symbol":"%s","lastPrice":"%.8f","bidPrice":"%.8f","askPrice":"%.8f","volume":"%.8f","closeTime":%d}]`

return &smocker.Mock{
Request: smocker.MockRequest{
Method: smocker.ShouldEqual("GET"),
Path: smocker.ShouldEqual("/api/v3/ticker/24hr"),
},
Response: &smocker.MockResponse{
Status: e.StatusCode,
Headers: map[string]smocker.StringSlice{
"Content-Type": []string{
"application/json",
},
},
Body: fmt.Sprintf(body, symbol, e.Price, e.Bid, e.Ask, e.Volume, e.Timestamp.UnixMilli()),
},
}, nil
}

func (b BinanceUS) build(e ExchangeMock) (*smocker.Mock, error) {
symbol := e.Symbol.Format("%s%s")
body := `{"symbol": "%s","price": "%.8f"}`

return &smocker.Mock{
Request: smocker.MockRequest{
Method: smocker.ShouldEqual("GET"),
Path: smocker.ShouldEqual("/api/v3/ticker/price"),
QueryParams: map[string]smocker.StringMatcherSlice{
"symbol": []smocker.StringMatcher{
smocker.ShouldEqual(symbol),
},
},
},
Response: &smocker.MockResponse{
Status: e.StatusCode,
Headers: map[string]smocker.StringSlice{
"Content-Type": []string{
"application/json",
},
},
Body: fmt.Sprintf(body, symbol, e.Price),
},
}, nil
}
1 change: 1 addition & 0 deletions origin/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var exchanges = map[string]Mockable{
"rocketpool": RocketPool{},
"balancer": Balancer{},
"binance": Binance{},
"binance_us": BinanceUS{},
"bitfinex": Bitfinex{},
"bitthumb": Bithumb{},
"bithumb": Bithumb{},
Expand Down

0 comments on commit 7039e33

Please sign in to comment.