Skip to content

Commit b8533ca

Browse files
committed
feat: add source chain to signing request
1 parent 814a2e9 commit b8533ca

7 files changed

Lines changed: 64 additions & 22 deletions

File tree

api/handlers/signing.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ const (
2828

2929
type SigningBody struct {
3030
ChainId uint64
31+
SourceChainId uint64 `json:"sourceChainId"`
3132
DepositId string `json:"depositId"`
3233
Nonce *BigInt `json:"nonce"`
3334
Protocol ProtocolType `json:"protocol"`
@@ -119,13 +120,14 @@ func (h *SigningHandler) HandleSigning(w http.ResponseWriter, r *http.Request) {
119120
}
120121
case LifiEscrowProtocol:
121122
{
122-
m = evmMessage.NewLifiEscrowMessage(0, b.ChainId, &evmMessage.LifiEscrowData{
123+
m = evmMessage.NewLifiEscrowMessage(b.SourceChainId, b.ChainId, &evmMessage.LifiEscrowData{
123124
OrderID: b.DepositId,
125+
DepositTxHash: b.DepositTxHash,
124126
Nonce: b.Nonce.Int,
125127
LiquidityPool: common.HexToAddress(b.LiquidityPool),
126128
Caller: common.HexToAddress(b.Caller),
127129
ErrChn: errChn,
128-
Source: 0,
130+
Source: b.SourceChainId,
129131
Destination: b.ChainId,
130132
BorrowAmount: b.BorrowAmount.Int,
131133
})
@@ -209,6 +211,19 @@ func (h *SigningHandler) validate(b *SigningBody, vars map[string]string) error
209211
return fmt.Errorf("chain '%d' not supported", b.ChainId)
210212
}
211213

214+
if b.Protocol == LifiEscrowProtocol {
215+
if b.SourceChainId == 0 {
216+
return fmt.Errorf("missing field 'sourceChainId' for lifi-escrow protocol")
217+
}
218+
if b.DepositTxHash == "" {
219+
return fmt.Errorf("missing field 'depositTxHash' for lifi-escrow protocol")
220+
}
221+
_, ok = h.chains[b.SourceChainId]
222+
if !ok {
223+
return fmt.Errorf("source chain '%d' not supported", b.SourceChainId)
224+
}
225+
}
226+
212227
return nil
213228
}
214229

api/handlers/signing_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_LifiSuccess() {
373373
Calldata: "0xbe5",
374374
Nonce: &handlers.BigInt{big.NewInt(1001)},
375375
BorrowAmount: &handlers.BigInt{big.NewInt(1000)},
376+
SourceChainId: 1,
377+
DepositTxHash: "0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
376378
}
377379
body, _ := json.Marshal(input)
378380

app/app.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ func Run() error {
175175
lifiOutputSettlers := make(map[uint64]common.Address)
176176
repayerAddresses := make(map[uint64]common.Address)
177177
tokens := make(map[uint64]map[string]config.TokenConfig)
178+
evmClients := make(map[uint64]lifi.ReceiptFetcher)
178179
for _, chainConfig := range configuration.ChainConfigs {
179180
switch chainConfig["type"] {
180181
case "evm":
@@ -185,6 +186,8 @@ func Run() error {
185186
client, err := evmClient.NewEVMClient(c.GeneralChainConfig.Endpoint, kp)
186187
panicOnError(err)
187188

189+
evmClients[*c.GeneralChainConfig.Id] = client
190+
188191
if c.AcrossPool != "" {
189192
poolAddress := common.HexToAddress(c.AcrossPool)
190193
acrossPools[*c.GeneralChainConfig.Id] = poolAddress
@@ -220,6 +223,14 @@ func Run() error {
220223
Tokens: tokens,
221224
}
222225

226+
var lifiApi *lifi.LifiEventFetcher
227+
if len(lifiOutputSettlers) > 0 {
228+
lifiApi = lifi.NewLifiEventFetcher(
229+
evmClients,
230+
common.HexToAddress(solverConfig.ProtocolsMetadata.Lifi.InputSettlerEscrow),
231+
)
232+
}
233+
223234
for _, chainConfig := range configuration.ChainConfigs {
224235
switch chainConfig["type"] {
225236
case "evm":
@@ -304,10 +315,6 @@ func Run() error {
304315

305316
resolver := token.NewTokenResolver(solverConfig, usdPricer)
306317
orderPricer := pricing.NewStandardPricer(resolver)
307-
lifiApi := lifi.NewLifiEventFetcher(
308-
client,
309-
common.HexToAddress(c.LifiInputSettlerEscrow),
310-
)
311318
lifiValidator := validation.NewLifiEscrowOrderValidator(solverConfig, resolver)
312319

313320
lifiMh := evmMessage.NewLifiEscrowMessageHandler(

chains/evm/message/lifiEscrow.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
)
2828

2929
type OrderFetcher interface {
30-
Order(ctx context.Context, hash common.Hash, orderID common.Hash) (*lifi.LifiOrder, error)
30+
Order(ctx context.Context, sourceChainID uint64, hash common.Hash, orderID common.Hash) (*lifi.LifiOrder, error)
3131
}
3232

3333
type OrderValidator interface {
@@ -99,6 +99,7 @@ func (h *LifiEscrowMessageHandler) HandleMessage(m *message.Message) (*proposal.
9999

100100
order, err := h.orderFetcher.Order(
101101
context.Background(),
102+
data.Source,
102103
common.HexToHash(data.DepositTxHash),
103104
common.HexToHash(data.OrderID))
104105
if err != nil {

chains/evm/message/mock/lifiEscrow.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

protocol/lifi/event.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,39 @@ type ReceiptFetcher interface {
2121
}
2222

2323
type LifiEventFetcher struct {
24-
client ReceiptFetcher
24+
clients map[uint64]ReceiptFetcher
2525
inputSettler common.Address
2626
}
2727

2828
func NewLifiEventFetcher(
29-
client ReceiptFetcher,
29+
clients map[uint64]ReceiptFetcher,
3030
inputSettler common.Address,
3131
) *LifiEventFetcher {
3232
return &LifiEventFetcher{
33-
client: client,
33+
clients: clients,
3434
inputSettler: inputSettler,
3535
}
3636
}
3737

38-
func (h *LifiEventFetcher) Order(ctx context.Context, hash common.Hash, orderID common.Hash) (*lifi.LifiOrder, error) {
38+
func (h *LifiEventFetcher) Order(ctx context.Context, sourceChainID uint64, hash common.Hash, orderID common.Hash) (*lifi.LifiOrder, error) {
39+
client, ok := h.clients[sourceChainID]
40+
if !ok {
41+
return nil, fmt.Errorf("no client configured for source chain %d", sourceChainID)
42+
}
43+
3944
ctx, cancel := context.WithTimeout(ctx, TRANSACTION_TIMEOUT)
4045
defer cancel()
4146

42-
log, err := h.fetchOpenEvent(ctx, hash, orderID)
47+
log, err := h.fetchOpenEvent(ctx, client, hash, orderID)
4348
if err != nil {
4449
return nil, err
4550
}
4651

4752
return contracts.ParseOpenEvent(log, h.inputSettler.Hex())
4853
}
4954

50-
func (h *LifiEventFetcher) fetchOpenEvent(ctx context.Context, hash common.Hash, orderID common.Hash) (*types.Log, error) {
51-
receipt, err := h.client.TransactionReceipt(ctx, hash)
55+
func (h *LifiEventFetcher) fetchOpenEvent(ctx context.Context, client ReceiptFetcher, hash common.Hash, orderID common.Hash) (*types.Log, error) {
56+
receipt, err := client.TransactionReceipt(ctx, hash)
5257
if err != nil {
5358
return nil, err
5459
}

protocol/lifi/event_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import (
1313
"go.uber.org/mock/gomock"
1414
)
1515

16+
const testChainID uint64 = 1
17+
1618
type OrderTestSuite struct {
1719
suite.Suite
1820
fetcher *lifi.LifiEventFetcher
@@ -28,13 +30,23 @@ func (s *OrderTestSuite) SetupTest() {
2830
ctrl := gomock.NewController(s.T())
2931
s.mockClient = mock_lifi.NewMockReceiptFetcher(ctrl)
3032
s.inputSettler = common.HexToAddress("0x000025c3226C00B2Cdc200005a1600509f4e00C0")
31-
s.fetcher = lifi.NewLifiEventFetcher(s.mockClient, s.inputSettler)
33+
clients := map[uint64]lifi.ReceiptFetcher{
34+
testChainID: s.mockClient,
35+
}
36+
s.fetcher = lifi.NewLifiEventFetcher(clients, s.inputSettler)
37+
}
38+
39+
func (s *OrderTestSuite) Test_Order_UnsupportedChain() {
40+
_, err := s.fetcher.Order(context.Background(), 999, common.Hash{}, common.Hash{})
41+
42+
s.NotNil(err)
43+
s.Contains(err.Error(), "no client configured for source chain 999")
3244
}
3345

3446
func (s *OrderTestSuite) Test_Order_FetchingTxFails() {
3547
s.mockClient.EXPECT().TransactionReceipt(gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("error"))
3648

37-
_, err := s.fetcher.Order(context.Background(), common.Hash{}, common.Hash{})
49+
_, err := s.fetcher.Order(context.Background(), testChainID, common.Hash{}, common.Hash{})
3850

3951
s.NotNil(err)
4052
}
@@ -44,7 +56,7 @@ func (s *OrderTestSuite) Test_Order_NoEvents() {
4456
Logs: make([]*types.Log, 0),
4557
}, nil)
4658

47-
_, err := s.fetcher.Order(context.Background(), common.Hash{}, common.Hash{})
59+
_, err := s.fetcher.Order(context.Background(), testChainID, common.Hash{}, common.Hash{})
4860

4961
s.NotNil(err)
5062
}
@@ -87,7 +99,7 @@ func (s *OrderTestSuite) Test_Order_InvalidLogs() {
8799
},
88100
}, nil)
89101

90-
_, err := s.fetcher.Order(context.Background(), common.Hash{}, common.Hash{})
102+
_, err := s.fetcher.Order(context.Background(), testChainID, common.Hash{}, common.Hash{})
91103

92104
s.NotNil(err)
93105
}

0 commit comments

Comments
 (0)