Skip to content

Commit eb69a2a

Browse files
authored
Merge pull request #5 from chronicleprotocol/balancerv2_wsteth_fix
make balancerV2 mock configurable
2 parents d8a8a17 + 6c52df9 commit eb69a2a

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed

e2e/exchanges_test.go

+81
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ type ExchangesE2ESuite struct {
4141
url string
4242
}
4343

44+
type jsonrpcMessage struct {
45+
Version string `json:"jsonrpc"`
46+
ID json.RawMessage `json:"id,omitempty"`
47+
Method string `json:"method,omitempty"`
48+
Params json.RawMessage `json:"params,omitempty"`
49+
Result string `json:"result,omitempty"`
50+
}
51+
4452
func (s *ExchangesE2ESuite) SetupSuite() {
4553
smockerHost, exist := os.LookupEnv("SMOCKER_HOST")
4654
s.Require().True(exist, "SMOCKER_HOST env variable have to be set")
@@ -102,6 +110,79 @@ func (s *ExchangesE2ESuite) TestBalancer() {
102110
defer func() { _ = resp.Body.Close() }()
103111
}
104112

113+
func (s *ExchangesE2ESuite) TestBalancerV2_getLatest() {
114+
ex := origin.NewExchange("balancerV2").WithSymbol("STETH/ETH").WithPrice(1)
115+
// No `pool` field should fail
116+
err := infestor.NewMocksBuilder().Reset().Add(ex).Deploy(s.api)
117+
s.Require().Error(err)
118+
119+
// With set rate and price
120+
price := "0x0000000000000000000000000000000000000000000000000dd22d6848e229b8"
121+
ex = ex.WithCustom("rate", price).WithCustom("price", price)
122+
err = infestor.NewMocksBuilder().Debug().Reset().Add(ex).Deploy(s.api)
123+
s.Require().NoError(err)
124+
125+
url := fmt.Sprintf("%s/", s.url)
126+
reqJSON := `{"method":"eth_call","params":[{"from":null,"to":"0x0000000000000000000000000000000000000000","data":"0xb10be7390000000000000000000000000000000000000000000000000000000000000000"}, "latest"],"id":1,"jsonrpc":"2.0"}`
127+
jsonStr := []byte(fmt.Sprint(reqJSON))
128+
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonStr))
129+
s.Require().NoError(err)
130+
s.Require().Equal(http.StatusOK, resp.StatusCode)
131+
132+
var response jsonrpcMessage
133+
err = parseBody(resp, &response)
134+
s.Require().NoError(err)
135+
s.Require().NotNil(response)
136+
s.Require().Equal(price, response.Result)
137+
138+
// Test status code
139+
ex = ex.WithStatusCode(http.StatusNotFound)
140+
err = infestor.NewMocksBuilder().Reset().Add(ex).Deploy(s.api)
141+
s.Require().NoError(err)
142+
143+
resp, err = http.Post(url, "application/json", bytes.NewBuffer(jsonStr))
144+
s.Require().NoError(err)
145+
s.Require().Equal(http.StatusNotFound, resp.StatusCode)
146+
defer func() { _ = resp.Body.Close() }()
147+
}
148+
149+
func (s *ExchangesE2ESuite) TestBalancerV2_getPriceRateCache() {
150+
ex := origin.NewExchange("balancerV2").WithSymbol("STETH/ETH").WithPrice(1)
151+
// No `pool` field should fail
152+
err := infestor.NewMocksBuilder().Reset().Add(ex).Deploy(s.api)
153+
s.Require().Error(err)
154+
155+
// With set rate and price
156+
rate := "0x0000000000000000000000000000000000000000000000000dd22d6848e229b8"
157+
ex = ex.WithCustom("rate", rate).WithCustom("price", rate)
158+
err = infestor.NewMocksBuilder().Debug().Reset().Add(ex).Deploy(s.api)
159+
s.Require().NoError(err)
160+
161+
url := fmt.Sprintf("%s/", s.url)
162+
reqJSON := `{"method":"eth_call","params":[{"from":null,"to":"0x0000000000000000000000000000000000000000","data":"0xb867ee5a"}, "latest"],"id":1,"jsonrpc":"2.0"}`
163+
jsonStr := []byte(fmt.Sprint(reqJSON))
164+
resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonStr))
165+
s.Require().NoError(err)
166+
s.Require().Equal(http.StatusOK, resp.StatusCode)
167+
168+
var response jsonrpcMessage
169+
err = parseBody(resp, &response)
170+
s.Require().NoError(err)
171+
s.Require().NotNil(response)
172+
fmt.Println(response.Result)
173+
s.Require().Equal(rate, response.Result)
174+
175+
// Test status code
176+
ex = ex.WithStatusCode(http.StatusNotFound)
177+
err = infestor.NewMocksBuilder().Reset().Add(ex).Deploy(s.api)
178+
s.Require().NoError(err)
179+
180+
resp, err = http.Post(url, "application/json", bytes.NewBuffer(jsonStr))
181+
s.Require().NoError(err)
182+
s.Require().Equal(http.StatusNotFound, resp.StatusCode)
183+
defer func() { _ = resp.Body.Close() }()
184+
}
185+
105186
func (s *ExchangesE2ESuite) TestBinance() {
106187
ex := origin.NewExchange("binance").WithSymbol("ETH/BTC").WithPrice(1)
107188

origin/balancerv2.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ func (b BalancerV2) BuildMocks(e []ExchangeMock) ([]*smocker.Mock, error) {
3030
}
3131

3232
func (b BalancerV2) buildGetLatest(e ExchangeMock) (*smocker.Mock, error) {
33+
price, ok := e.Custom["price"]
34+
if !ok {
35+
return nil, fmt.Errorf("`price` custom field is requierd for balancerV2")
36+
}
37+
3338
// getLatest(uint256)
3439
m := smocker.ShouldContainSubstring("0xb10be739")
3540

@@ -48,12 +53,17 @@ func (b BalancerV2) buildGetLatest(e ExchangeMock) (*smocker.Mock, error) {
4853
"application/json",
4954
},
5055
},
51-
Body: fmt.Sprintf(rpcJSONResult, "0x0000000000000000000000000000000000000000000000000dd22d6848e229b8"),
56+
Body: fmt.Sprintf(rpcJSONResult, price),
5257
},
5358
}, nil
5459
}
5560

5661
func (b BalancerV2) buildGetPriceRateCache(e ExchangeMock) (*smocker.Mock, error) {
62+
rate, ok := e.Custom["rate"]
63+
if !ok {
64+
return nil, fmt.Errorf("`rate` custom field is requierd for balancerV2")
65+
}
66+
5767
// getPriceRateCache(uint256,uint256,uint256)
5868
m := smocker.ShouldContainSubstring("0xb867ee5a")
5969

@@ -72,7 +82,7 @@ func (b BalancerV2) buildGetPriceRateCache(e ExchangeMock) (*smocker.Mock, error
7282
"application/json",
7383
},
7484
},
75-
Body: fmt.Sprintf(rpcJSONResult, "0x0000000000000000000000000000000000000000000000000dd22d6848e229b8"),
85+
Body: fmt.Sprintf(rpcJSONResult, rate),
7686
},
7787
}, nil
7888
}

0 commit comments

Comments
 (0)