Skip to content

Commit 9289a38

Browse files
authored
feat: mayan message handling (#37)
* Extract shared code from the message handler * Adjust across message handler tests * Make confirmation watcher chain specific * Implement watcher tests * Implement mayan basic flow * Add mayan to the app config * Include mayan contract and api in message handler * Add mayan protocol support to signing api * Fix test errors * Replace fetching token config from watcher with token store * Verify fulfill message params * Validate mayan recipient to be liquidity pool * Convert across data to pointer * Remove unnecessary comment * Convert mayan data directly to JSON instead of converting into seprate message * Fix infinite loop when marshalling * Fix configuration errors * Add mayan handler tests * Lint * Fix tests * Avoid floating point miscalculations * Lint * Fix typo * Add missing channel call * Extract duplicate call * Refactor mayan api * Fix pointer to mayan swap
1 parent 35c0976 commit 9289a38

File tree

28 files changed

+3621
-484
lines changed

28 files changed

+3621
-484
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ genmocks:
3636
mockgen -destination=./comm/p2p/mock/conn/conn.go github.com/libp2p/go-libp2p/core/network Conn
3737
mockgen -destination=./comm/p2p/mock/stream/stream.go github.com/libp2p/go-libp2p/core/network Stream,Conn
3838
mockgen -source=./chains/evm/message/across.go -destination=./chains/evm/message/mock/across.go
39+
mockgen -source=./chains/evm/message/mayan.go -destination=./chains/evm/message/mock/mayan.go
40+
mockgen -source=./chains/evm/message/confirmations.go -destination=./chains/evm/message/mock/confirmations.go
3941
mockgen -source=./api/handlers/signing.go -destination=./api/handlers/mock/signing.go
4042

4143

api/handlers/signing.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,27 @@ import (
1010

1111
"github.com/ethereum/go-ethereum/common"
1212
"github.com/gorilla/mux"
13-
across "github.com/sprintertech/sprinter-signing/chains/evm/message"
13+
evmMessage "github.com/sprintertech/sprinter-signing/chains/evm/message"
1414
"github.com/sygmaprotocol/sygma-core/relayer/message"
1515
)
1616

1717
type ProtocolType string
1818

1919
const (
2020
AcrossProtocol ProtocolType = "across"
21+
MayanProtocol ProtocolType = "mayan"
2122
)
2223

2324
type SigningBody struct {
2425
ChainId uint64
25-
DepositId *BigInt `json:"depositId"`
26+
DepositId string `json:"depositId"`
2627
Nonce *BigInt `json:"nonce"`
2728
Protocol ProtocolType `json:"protocol"`
2829
LiquidityPool string `json:"liquidityPool"`
2930
Caller string `json:"caller"`
31+
Calldata string `json:"calldata"`
32+
DepositTxHash string `json:"depositTxHash"`
33+
BorrowAmount *BigInt `json:"borrowAmount"`
3034
}
3135

3236
type SigningHandler struct {
@@ -48,14 +52,14 @@ func (h *SigningHandler) HandleSigning(w http.ResponseWriter, r *http.Request) {
4852
d := json.NewDecoder(r.Body)
4953
err := d.Decode(b)
5054
if err != nil {
51-
JSONError(w, fmt.Sprintf("invalid request body: %s", err), http.StatusBadRequest)
55+
JSONError(w, fmt.Errorf("invalid request body: %s", err), http.StatusBadRequest)
5256
return
5357
}
5458

5559
vars := mux.Vars(r)
5660
err = h.validate(b, vars)
5761
if err != nil {
58-
JSONError(w, fmt.Sprintf("invalid request body: %s", err), http.StatusBadRequest)
62+
JSONError(w, fmt.Errorf("invalid request body: %s", err), http.StatusBadRequest)
5963
return
6064
}
6165
errChn := make(chan error, 1)
@@ -64,23 +68,40 @@ func (h *SigningHandler) HandleSigning(w http.ResponseWriter, r *http.Request) {
6468
switch b.Protocol {
6569
case AcrossProtocol:
6670
{
67-
m = across.NewAcrossMessage(0, b.ChainId, across.AcrossData{
68-
DepositId: b.DepositId.Int,
71+
depositId, _ := new(big.Int).SetString(b.DepositId, 10)
72+
m = evmMessage.NewAcrossMessage(0, b.ChainId, &evmMessage.AcrossData{
73+
DepositId: depositId,
6974
Nonce: b.Nonce.Int,
7075
LiquidityPool: common.HexToAddress(b.LiquidityPool),
7176
Caller: common.HexToAddress(b.Caller),
77+
Source: 0,
78+
Destination: b.ChainId,
7279
ErrChn: errChn,
7380
})
7481
}
82+
case MayanProtocol:
83+
{
84+
m = evmMessage.NewMayanMessage(0, b.ChainId, &evmMessage.MayanData{
85+
Nonce: b.Nonce.Int,
86+
LiquidityPool: common.HexToAddress(b.LiquidityPool),
87+
Caller: common.HexToAddress(b.Caller),
88+
ErrChn: errChn,
89+
Calldata: b.Calldata,
90+
DepositTxHash: b.DepositTxHash,
91+
Source: 0,
92+
Destination: b.ChainId,
93+
BorrowAmount: b.BorrowAmount.Int,
94+
})
95+
}
7596
default:
76-
JSONError(w, fmt.Sprintf("invalid protocol %s", b.Protocol), http.StatusBadRequest)
97+
JSONError(w, fmt.Errorf("invalid protocol %s", b.Protocol), http.StatusBadRequest)
7798
return
7899
}
79100
h.msgChan <- []*message.Message{m}
80101

81102
err = <-errChn
82103
if err != nil {
83-
JSONError(w, fmt.Sprintf("Singing failed: %s", err), http.StatusInternalServerError)
104+
JSONError(w, fmt.Errorf("singing failed: %s", err), http.StatusInternalServerError)
84105
return
85106
}
86107

@@ -94,7 +115,7 @@ func (h *SigningHandler) validate(b *SigningBody, vars map[string]string) error
94115
}
95116
b.ChainId = chainId.Uint64()
96117

97-
if b.DepositId == nil {
118+
if b.DepositId == "" {
98119
return fmt.Errorf("missing field 'depositId'")
99120
}
100121

@@ -144,17 +165,17 @@ func (h *StatusHandler) HandleRequest(w http.ResponseWriter, r *http.Request) {
144165
vars := mux.Vars(r)
145166
chainId, ok := new(big.Int).SetString(vars["chainId"], 0)
146167
if !ok {
147-
JSONError(w, "chain id invalid", http.StatusBadRequest)
168+
JSONError(w, fmt.Errorf("chain id invalid"), http.StatusBadRequest)
148169
return
149170
}
150171
_, ok = h.chains[chainId.Uint64()]
151172
if !ok {
152-
JSONError(w, fmt.Sprintf("chain %d not supported", chainId.Int64()), http.StatusNotFound)
173+
JSONError(w, fmt.Errorf("chain %d not supported", chainId.Int64()), http.StatusNotFound)
153174
return
154175
}
155176
depositId, ok := vars["depositId"]
156177
if !ok {
157-
JSONError(w, "missing 'depositId", http.StatusBadRequest)
178+
JSONError(w, fmt.Errorf("missing 'depositId"), http.StatusBadRequest)
158179
return
159180
}
160181

api/handlers/signing_test.go

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_MissingDepositID() {
5959

6060
go func() {
6161
msg := <-msgChn
62-
ad := msg[0].Data.(across.AcrossData)
62+
ad := msg[0].Data.(*across.AcrossData)
6363
ad.ErrChn <- fmt.Errorf("error handling message")
6464
}()
6565

@@ -74,7 +74,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_MissingCaller() {
7474

7575
input := handlers.SigningBody{
7676
Protocol: "across",
77-
DepositId: &handlers.BigInt{big.NewInt(1000)},
77+
DepositId: "1000",
7878
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
7979
Nonce: &handlers.BigInt{big.NewInt(1001)},
8080
}
@@ -90,7 +90,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_MissingCaller() {
9090

9191
go func() {
9292
msg := <-msgChn
93-
ad := msg[0].Data.(across.AcrossData)
93+
ad := msg[0].Data.(*across.AcrossData)
9494
ad.ErrChn <- fmt.Errorf("error handling message")
9595
}()
9696

@@ -105,7 +105,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_MissingLiquidityPool() {
105105

106106
input := handlers.SigningBody{
107107
Protocol: "across",
108-
DepositId: &handlers.BigInt{big.NewInt(1000)},
108+
DepositId: "1000",
109109
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
110110
Nonce: &handlers.BigInt{big.NewInt(1001)},
111111
}
@@ -121,7 +121,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_MissingLiquidityPool() {
121121

122122
go func() {
123123
msg := <-msgChn
124-
ad := msg[0].Data.(across.AcrossData)
124+
ad := msg[0].Data.(*across.AcrossData)
125125
ad.ErrChn <- fmt.Errorf("error handling message")
126126
}()
127127

@@ -135,7 +135,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_InvalidChainID() {
135135
handler := handlers.NewSigningHandler(msgChn, s.chains)
136136

137137
input := handlers.SigningBody{
138-
DepositId: &handlers.BigInt{big.NewInt(1000)},
138+
DepositId: "1000",
139139
Protocol: "across",
140140
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
141141
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
@@ -153,7 +153,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_InvalidChainID() {
153153

154154
go func() {
155155
msg := <-msgChn
156-
ad := msg[0].Data.(across.AcrossData)
156+
ad := msg[0].Data.(*across.AcrossData)
157157
ad.ErrChn <- fmt.Errorf("error handling message")
158158
}()
159159

@@ -167,7 +167,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_ChainNotSupported() {
167167
handler := handlers.NewSigningHandler(msgChn, s.chains)
168168

169169
input := handlers.SigningBody{
170-
DepositId: &handlers.BigInt{big.NewInt(1000)},
170+
DepositId: "1000",
171171
Protocol: "across",
172172
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
173173
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
@@ -185,7 +185,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_ChainNotSupported() {
185185

186186
go func() {
187187
msg := <-msgChn
188-
ad := msg[0].Data.(across.AcrossData)
188+
ad := msg[0].Data.(*across.AcrossData)
189189
ad.ErrChn <- fmt.Errorf("error handling message")
190190
}()
191191

@@ -199,7 +199,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_InvalidProtocol() {
199199
handler := handlers.NewSigningHandler(msgChn, s.chains)
200200

201201
input := handlers.SigningBody{
202-
DepositId: &handlers.BigInt{big.NewInt(1000)},
202+
DepositId: "1000",
203203
Protocol: "invalid",
204204
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
205205
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
@@ -217,7 +217,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_InvalidProtocol() {
217217

218218
go func() {
219219
msg := <-msgChn
220-
ad := msg[0].Data.(across.AcrossData)
220+
ad := msg[0].Data.(*across.AcrossData)
221221
ad.ErrChn <- fmt.Errorf("error handling message")
222222
}()
223223

@@ -231,7 +231,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_ErrorHandlingMessage() {
231231
handler := handlers.NewSigningHandler(msgChn, s.chains)
232232

233233
input := handlers.SigningBody{
234-
DepositId: &handlers.BigInt{big.NewInt(1000)},
234+
DepositId: "1000",
235235
Protocol: "across",
236236
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
237237
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
@@ -249,7 +249,7 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_ErrorHandlingMessage() {
249249

250250
go func() {
251251
msg := <-msgChn
252-
ad := msg[0].Data.(across.AcrossData)
252+
ad := msg[0].Data.(*across.AcrossData)
253253
ad.ErrChn <- fmt.Errorf("error handling message")
254254
}()
255255

@@ -258,12 +258,12 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_ErrorHandlingMessage() {
258258
s.Equal(http.StatusInternalServerError, recorder.Code)
259259
}
260260

261-
func (s *SigningHandlerTestSuite) Test_HandleSigning_Success() {
261+
func (s *SigningHandlerTestSuite) Test_HandleSigning_AcrossSuccess() {
262262
msgChn := make(chan []*message.Message)
263263
handler := handlers.NewSigningHandler(msgChn, s.chains)
264264

265265
input := handlers.SigningBody{
266-
DepositId: &handlers.BigInt{big.NewInt(1000)},
266+
DepositId: "1000",
267267
Protocol: "across",
268268
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
269269
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
@@ -281,7 +281,41 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_Success() {
281281

282282
go func() {
283283
msg := <-msgChn
284-
ad := msg[0].Data.(across.AcrossData)
284+
ad := msg[0].Data.(*across.AcrossData)
285+
ad.ErrChn <- nil
286+
}()
287+
288+
handler.HandleSigning(recorder, req)
289+
290+
s.Equal(http.StatusAccepted, recorder.Code)
291+
}
292+
293+
func (s *SigningHandlerTestSuite) Test_HandleSigning_MayanSuccess() {
294+
msgChn := make(chan []*message.Message)
295+
handler := handlers.NewSigningHandler(msgChn, s.chains)
296+
297+
input := handlers.SigningBody{
298+
DepositId: "1000",
299+
Protocol: "mayan",
300+
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
301+
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
302+
Calldata: "0xbe5",
303+
Nonce: &handlers.BigInt{big.NewInt(1001)},
304+
BorrowAmount: &handlers.BigInt{big.NewInt(1000)},
305+
}
306+
body, _ := json.Marshal(input)
307+
308+
req := httptest.NewRequest(http.MethodPost, "/v1/chains/1/signatures", bytes.NewReader(body))
309+
req = mux.SetURLVars(req, map[string]string{
310+
"chainId": "1",
311+
})
312+
req.Header.Set("Content-Type", "application/json")
313+
314+
recorder := httptest.NewRecorder()
315+
316+
go func() {
317+
msg := <-msgChn
318+
ad := msg[0].Data.(*across.MayanData)
285319
ad.ErrChn <- nil
286320
}()
287321

api/handlers/util.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,17 @@ func (b *BigInt) MarshalJSON() ([]byte, error) {
3030
return []byte(b.String()), nil
3131
}
3232

33-
func JSONError(w http.ResponseWriter, err interface{}, code int) {
33+
func JSONError(w http.ResponseWriter, err error, code int) {
3434
w.Header().Set("Content-Type", "application/json; charset=utf-8")
3535
w.Header().Set("X-Content-Type-Options", "nosniff")
3636
w.WriteHeader(code)
37-
_ = json.NewEncoder(w).Encode(err)
37+
type errorResponse struct {
38+
Code int `json:"code"`
39+
Reason string `json:"reason"`
40+
}
41+
resp := errorResponse{
42+
Reason: err.Error(),
43+
Code: code,
44+
}
45+
_ = json.NewEncoder(w).Encode(resp)
3846
}

0 commit comments

Comments
 (0)