Skip to content

Commit 75f88f7

Browse files
committed
Add lighter protocol to the API
1 parent 945ffb5 commit 75f88f7

File tree

4 files changed

+113
-2
lines changed

4 files changed

+113
-2
lines changed

api/handlers/signing.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/ethereum/go-ethereum/common"
1212
"github.com/gorilla/mux"
1313
evmMessage "github.com/sprintertech/sprinter-signing/chains/evm/message"
14+
lighterMessage "github.com/sprintertech/sprinter-signing/chains/lighter/message"
1415
"github.com/sygmaprotocol/sygma-core/relayer/message"
1516
)
1617

@@ -20,8 +21,9 @@ const (
2021
AcrossProtocol ProtocolType = "across"
2122
MayanProtocol ProtocolType = "mayan"
2223
RhinestoneProtocol ProtocolType = "rhinestone"
23-
LifiEscrow ProtocolType = "lifi-escrow"
24+
LifiEscrowProtocol ProtocolType = "lifi-escrow"
2425
LifiProtocol ProtocolType = "lifi"
26+
LighterProtocol ProtocolType = "lighter"
2527
)
2628

2729
type SigningBody struct {
@@ -113,7 +115,7 @@ func (h *SigningHandler) HandleSigning(w http.ResponseWriter, r *http.Request) {
113115
BorrowAmount: b.BorrowAmount.Int,
114116
})
115117
}
116-
case LifiEscrow:
118+
case LifiEscrowProtocol:
117119
{
118120
m = evmMessage.NewLifiEscrowData(0, b.ChainId, &evmMessage.LifiEscrowData{
119121
OrderID: b.DepositId,
@@ -126,6 +128,18 @@ func (h *SigningHandler) HandleSigning(w http.ResponseWriter, r *http.Request) {
126128
BorrowAmount: b.BorrowAmount.Int,
127129
})
128130
}
131+
case LighterProtocol:
132+
{
133+
m = lighterMessage.NewLighterMessage(0, b.ChainId, &lighterMessage.LighterData{
134+
OrderHash: b.DepositId,
135+
Nonce: b.Nonce.Int,
136+
LiquidityPool: common.HexToAddress(b.LiquidityPool),
137+
Caller: common.HexToAddress(b.Caller),
138+
ErrChn: errChn,
139+
Source: 0,
140+
Destination: b.ChainId,
141+
})
142+
}
129143
default:
130144
JSONError(w, fmt.Errorf("invalid protocol %s", b.Protocol), http.StatusBadRequest)
131145
return

api/handlers/signing_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/sprintertech/sprinter-signing/api/handlers"
1717
mock_handlers "github.com/sprintertech/sprinter-signing/api/handlers/mock"
1818
across "github.com/sprintertech/sprinter-signing/chains/evm/message"
19+
lighter "github.com/sprintertech/sprinter-signing/chains/lighter/message"
1920
"github.com/stretchr/testify/suite"
2021
"github.com/sygmaprotocol/sygma-core/relayer/message"
2122
"go.uber.org/mock/gomock"
@@ -394,6 +395,40 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_LifiSuccess() {
394395
s.Equal(http.StatusAccepted, recorder.Code)
395396
}
396397

398+
func (s *SigningHandlerTestSuite) Test_HandleSigning_LighterSuccess() {
399+
msgChn := make(chan []*message.Message)
400+
handler := handlers.NewSigningHandler(msgChn, s.chains)
401+
402+
input := handlers.SigningBody{
403+
DepositId: "depositID",
404+
Protocol: "lighter",
405+
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
406+
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
407+
Calldata: "0xbe5",
408+
Nonce: &handlers.BigInt{big.NewInt(1001)},
409+
BorrowAmount: &handlers.BigInt{big.NewInt(1000)},
410+
}
411+
body, _ := json.Marshal(input)
412+
413+
req := httptest.NewRequest(http.MethodPost, "/v1/chains/1/signatures", bytes.NewReader(body))
414+
req = mux.SetURLVars(req, map[string]string{
415+
"chainId": "1",
416+
})
417+
req.Header.Set("Content-Type", "application/json")
418+
419+
recorder := httptest.NewRecorder()
420+
421+
go func() {
422+
msg := <-msgChn
423+
ad := msg[0].Data.(*lighter.LighterData)
424+
ad.ErrChn <- nil
425+
}()
426+
427+
handler.HandleSigning(recorder, req)
428+
429+
s.Equal(http.StatusAccepted, recorder.Code)
430+
}
431+
397432
type StatusHandlerTestSuite struct {
398433
suite.Suite
399434

app/app.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ import (
3232
"github.com/sprintertech/sprinter-signing/chains/evm/calls/events"
3333
evmListener "github.com/sprintertech/sprinter-signing/chains/evm/listener"
3434
evmMessage "github.com/sprintertech/sprinter-signing/chains/evm/message"
35+
"github.com/sprintertech/sprinter-signing/chains/lighter"
36+
lighterMessage "github.com/sprintertech/sprinter-signing/chains/lighter/message"
3537
"github.com/sprintertech/sprinter-signing/comm"
3638
"github.com/sprintertech/sprinter-signing/comm/elector"
3739
"github.com/sprintertech/sprinter-signing/comm/p2p"
@@ -43,6 +45,7 @@ import (
4345
"github.com/sprintertech/sprinter-signing/price"
4446
"github.com/sprintertech/sprinter-signing/protocol/across"
4547
"github.com/sprintertech/sprinter-signing/protocol/lifi"
48+
lighterAPI "github.com/sprintertech/sprinter-signing/protocol/lighter"
4649
"github.com/sprintertech/sprinter-signing/protocol/mayan"
4750
"github.com/sprintertech/sprinter-signing/topology"
4851
"github.com/sprintertech/sprinter-signing/tss"
@@ -350,6 +353,23 @@ func Run() error {
350353
}
351354
}
352355

356+
lighterConfig, err := lighter.NewLighterConfig(*solverConfig)
357+
panicOnError(err)
358+
lighterAPI := lighterAPI.NewLighterAPI()
359+
lighterMessageHandler := lighterMessage.NewLighterMessageHandler(
360+
lighterConfig.WithdrawalAddress,
361+
lighterConfig.UsdcAddress,
362+
lighterAPI,
363+
coordinator,
364+
host,
365+
communication,
366+
keyshareStore,
367+
sigChn,
368+
)
369+
go lighterMessageHandler.Listen(ctx)
370+
lighterChain := lighter.NewLighterChain(lighterMessageHandler)
371+
domains[lighter.LIGHTER_DOMAIN_ID] = lighterChain
372+
353373
go jobs.StartCommunicationHealthCheckJob(host, configuration.RelayerConfig.MpcConfig.CommHealthCheckInterval, sygmaMetrics)
354374

355375
r := relayer.NewRelayer(domains, sygmaMetrics)

chains/lighter/chain.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package lighter
2+
3+
import (
4+
"context"
5+
6+
"github.com/sygmaprotocol/sygma-core/relayer/message"
7+
"github.com/sygmaprotocol/sygma-core/relayer/proposal"
8+
)
9+
10+
const (
11+
LIGHTER_DOMAIN_ID uint64 = 1513889025
12+
)
13+
14+
type MessageHandler interface {
15+
HandleMessage(m *message.Message) (*proposal.Proposal, error)
16+
}
17+
18+
type LighterChain struct {
19+
messageHandler MessageHandler
20+
domainID uint64
21+
}
22+
23+
func NewLighterChain(messageHandler MessageHandler) *LighterChain {
24+
return &LighterChain{
25+
messageHandler: messageHandler,
26+
domainID: LIGHTER_DOMAIN_ID,
27+
}
28+
}
29+
30+
func (c *LighterChain) PollEvents(_ context.Context) {}
31+
32+
func (c *LighterChain) ReceiveMessage(m *message.Message) (*proposal.Proposal, error) {
33+
return c.messageHandler.HandleMessage(m)
34+
}
35+
36+
func (c *LighterChain) Write(_ []*proposal.Proposal) error {
37+
return nil
38+
}
39+
40+
func (c *LighterChain) DomainID() uint64 {
41+
return c.domainID
42+
}

0 commit comments

Comments
 (0)