Skip to content

Commit fdd1230

Browse files
authored
feat: lighter handler (#89)
<!--- Provide a general summary of your changes in the Title above --> ## Description <!--- Describe your changes in detail --> ## Related Issue Or Context <!--- If suggesting a new feature or change, please discuss it in an issue first --> <!--- If fixing a bug, there should be an issue describing it with steps to reproduce --> <!--- Otherwise, describe context and motivation for change herre --> Closes: #88 ## How Has This Been Tested? Testing details. <!--- Please describe in detail how you tested your changes. --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation ## Checklist: <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [ ] I have commented my code, particularly in hard-to-understand areas. - [ ] I have ensured that all acceptance criteria (or expected behavior) from issue are met - [ ] I have updated the documentation locally and in docs. - [ ] I have added tests to cover my changes. - [ ] I have ensured that all the checks are passing and green, I've signed the CLA bot
1 parent db93c51 commit fdd1230

File tree

25 files changed

+1297
-174
lines changed

25 files changed

+1297
-174
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ genmocks:
4242
mockgen -source=./chains/evm/message/confirmations.go -destination=./chains/evm/message/mock/confirmations.go
4343
mockgen -source=./api/handlers/signing.go -destination=./api/handlers/mock/signing.go
4444
mockgen -package mock_message -destination=./chains/evm/message/mock/pricing.go github.com/sprintertech/lifi-solver/pkg/pricing OrderPricer
45+
mockgen -source=./chains/lighter/message/lighter.go -destination=./chains/lighter/message/mock/lighter.go
4546

4647

4748

api/handlers/signing.go

Lines changed: 17 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,19 @@ 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+
BorrowAmount: b.BorrowAmount.Int,
139+
ErrChn: errChn,
140+
Source: 0,
141+
Destination: b.ChainId,
142+
})
143+
}
129144
default:
130145
JSONError(w, fmt.Errorf("invalid protocol %s", b.Protocol), http.StatusBadRequest)
131146
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: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ 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"
37+
"github.com/sprintertech/sprinter-signing/comm"
3538
"github.com/sprintertech/sprinter-signing/comm/elector"
3639
"github.com/sprintertech/sprinter-signing/comm/p2p"
3740
"github.com/sprintertech/sprinter-signing/config"
@@ -42,6 +45,7 @@ import (
4245
"github.com/sprintertech/sprinter-signing/price"
4346
"github.com/sprintertech/sprinter-signing/protocol/across"
4447
"github.com/sprintertech/sprinter-signing/protocol/lifi"
48+
lighterAPI "github.com/sprintertech/sprinter-signing/protocol/lighter"
4549
"github.com/sprintertech/sprinter-signing/protocol/mayan"
4650
"github.com/sprintertech/sprinter-signing/topology"
4751
"github.com/sprintertech/sprinter-signing/tss"
@@ -254,7 +258,7 @@ func Run() error {
254258
sigChn)
255259
go acrossMh.Listen(ctx)
256260

257-
mh.RegisterMessageHandler(evmMessage.AcrossMessage, acrossMh)
261+
mh.RegisterMessageHandler(message.MessageType(comm.AcrossMsg.String()), acrossMh)
258262
supportedChains[*c.GeneralChainConfig.Id] = struct{}{}
259263
confirmationsPerChain[*c.GeneralChainConfig.Id] = c.ConfirmationsByValue
260264
}
@@ -278,7 +282,7 @@ func Run() error {
278282
sigChn)
279283
go mayanMh.Listen(ctx)
280284

281-
mh.RegisterMessageHandler(evmMessage.MayanMessage, mayanMh)
285+
mh.RegisterMessageHandler(message.MessageType(comm.MayanMsg.String()), mayanMh)
282286
supportedChains[*c.GeneralChainConfig.Id] = struct{}{}
283287
confirmationsPerChain[*c.GeneralChainConfig.Id] = c.ConfirmationsByValue
284288
}
@@ -291,7 +295,7 @@ func Run() error {
291295
resolver := token.NewTokenResolver(solverConfig, usdPricer)
292296
orderPricer := pricing.NewStandardPricer(resolver)
293297
lifiApi := lifi.NewLifiAPI()
294-
lifiValidator := validation.NewLifiEscrowOrderValidator(solverConfig, orderPricer)
298+
lifiValidator := validation.NewLifiEscrowOrderValidator(solverConfig, orderPricer, resolver)
295299

296300
lifiMh := evmMessage.NewLifiEscrowMessageHandler(
297301
*c.GeneralChainConfig.Id,
@@ -309,7 +313,7 @@ func Run() error {
309313
sigChn,
310314
)
311315
go lifiMh.Listen(ctx)
312-
mh.RegisterMessageHandler(evmMessage.LifiEscrowMessage, lifiMh)
316+
mh.RegisterMessageHandler(message.MessageType(comm.LifiEscrowMsg.String()), lifiMh)
313317
supportedChains[*c.GeneralChainConfig.Id] = struct{}{}
314318
confirmationsPerChain[*c.GeneralChainConfig.Id] = c.ConfirmationsByValue
315319
}
@@ -323,7 +327,7 @@ func Run() error {
323327
keyshareStore,
324328
)
325329
go lifiUnlockMh.Listen(ctx)
326-
mh.RegisterMessageHandler(evmMessage.LifiUnlockMessage, lifiUnlockMh)
330+
mh.RegisterMessageHandler(message.MessageType(comm.LifiUnlockMsg.String()), lifiUnlockMh)
327331

328332
var startBlock *big.Int
329333
var listener *coreListener.EVMListener
@@ -349,6 +353,24 @@ func Run() error {
349353
}
350354
}
351355

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

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

chains/evm/calls/consts/lighter.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package consts
2+
3+
import (
4+
"strings"
5+
6+
"github.com/ethereum/go-ethereum/accounts/abi"
7+
)
8+
9+
var LighterABI, _ = abi.JSON(strings.NewReader(`[{
10+
"name": "withdraw",
11+
"type": "function",
12+
"stateMutability": "nonpayable",
13+
"inputs": [
14+
{"name": "txHash", "type": "bytes32"},
15+
{"name": "toAddress", "type": "address"},
16+
{"name": "amount", "type": "uint256"}
17+
],
18+
"outputs": []
19+
}]`))

chains/evm/message/across.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/libp2p/go-libp2p/core/peer"
1515
"github.com/rs/zerolog/log"
1616
"github.com/sprintertech/sprinter-signing/chains/evm/calls/events"
17+
"github.com/sprintertech/sprinter-signing/chains/evm/signature"
1718
"github.com/sprintertech/sprinter-signing/comm"
1819
"github.com/sprintertech/sprinter-signing/tss"
1920
"github.com/sprintertech/sprinter-signing/tss/ecdsa/signing"
@@ -141,7 +142,7 @@ func (h *AcrossMessageHandler) HandleMessage(m *message.Message) (*proposal.Prop
141142
return nil, err
142143
}
143144

144-
unlockHash, err := borrowUnlockHash(
145+
unlockHash, err := signature.BorrowUnlockHash(
145146
calldata,
146147
d.OutputAmount,
147148
common.BytesToAddress(d.OutputToken[12:]),

chains/evm/message/lifiEscrow.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/libp2p/go-libp2p/core/peer"
1414
"github.com/rs/zerolog/log"
1515
"github.com/sprintertech/sprinter-signing/chains/evm/calls/consts"
16+
"github.com/sprintertech/sprinter-signing/chains/evm/signature"
1617
"github.com/sprintertech/sprinter-signing/comm"
1718
"github.com/sprintertech/sprinter-signing/config"
1819
"github.com/sprintertech/sprinter-signing/tss"
@@ -150,7 +151,7 @@ func (h *LifiEscrowMessageHandler) HandleMessage(m *message.Message) (*proposal.
150151
big.NewInt(order.Order.FillDeadline.Unix()).Uint64(),
151152
)
152153

153-
unlockHash, err := borrowUnlockHash(
154+
unlockHash, err := signature.BorrowUnlockHash(
154155
calldata,
155156
data.BorrowAmount,
156157
borrowToken,

chains/evm/message/mayan.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/libp2p/go-libp2p/core/peer"
1313
"github.com/rs/zerolog/log"
1414
"github.com/sprintertech/sprinter-signing/chains/evm/calls/contracts"
15+
"github.com/sprintertech/sprinter-signing/chains/evm/signature"
1516
"github.com/sprintertech/sprinter-signing/comm"
1617
"github.com/sprintertech/sprinter-signing/config"
1718
"github.com/sprintertech/sprinter-signing/protocol/mayan"
@@ -160,7 +161,7 @@ func (h *MayanMessageHandler) HandleMessage(m *message.Message) (*proposal.Propo
160161

161162
data.ErrChn <- nil
162163

163-
unlockHash, err := borrowUnlockHash(
164+
unlockHash, err := signature.BorrowUnlockHash(
164165
calldataBytes,
165166
data.BorrowAmount,
166167
destinationBorrowToken.Address,

0 commit comments

Comments
 (0)