Skip to content

Commit 5e78a59

Browse files
authored
fix: withdrawal contract is the lighter caller (#106)
<!--- 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: #<issue> ## 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 8fbd1d5 commit 5e78a59

File tree

4 files changed

+28
-40
lines changed

4 files changed

+28
-40
lines changed

api/handlers/signing.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,6 @@ func (h *SigningHandler) HandleSigning(w http.ResponseWriter, r *http.Request) {
136136
Nonce: b.Nonce.Int,
137137
Deadline: b.Deadline,
138138
LiquidityPool: common.HexToAddress(b.LiquidityPool),
139-
Caller: common.HexToAddress(b.Caller),
140-
BorrowAmount: b.BorrowAmount.Int,
141139
ErrChn: errChn,
142140
Source: 0,
143141
Destination: b.ChainId,

chains/lighter/message/lighter.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
var (
2727
ARBITRUM_CHAIN_ID = big.NewInt(42161)
28+
FEE = big.NewInt(2000000)
2829
)
2930

3031
type Coordinator interface {
@@ -89,14 +90,14 @@ func (h *LighterMessageHandler) HandleMessage(m *message.Message) (*proposal.Pro
8990
return nil, err
9091
}
9192

92-
if err = h.verifyWithdrawal(tx, data.BorrowAmount); err != nil {
93+
if err = h.verifyWithdrawal(tx); err != nil {
9394
data.ErrChn <- err
9495
return nil, err
9596
}
9697

9798
data.ErrChn <- nil
9899

99-
calldata, err := h.calldata(tx, data.BorrowAmount)
100+
calldata, err := h.calldata(tx)
100101
if err != nil {
101102
return nil, err
102103
}
@@ -108,7 +109,7 @@ func (h *LighterMessageHandler) HandleMessage(m *message.Message) (*proposal.Pro
108109
ARBITRUM_CHAIN_ID,
109110
h.lighterAddress,
110111
data.Deadline,
111-
data.Caller,
112+
h.lighterAddress,
112113
data.LiquidityPool,
113114
data.Nonce)
114115
if err != nil {
@@ -135,7 +136,7 @@ func (h *LighterMessageHandler) HandleMessage(m *message.Message) (*proposal.Pro
135136
return nil, nil
136137
}
137138

138-
func (h *LighterMessageHandler) verifyWithdrawal(tx *lighter.LighterTx, borrowAmount *big.Int) error {
139+
func (h *LighterMessageHandler) verifyWithdrawal(tx *lighter.LighterTx) error {
139140
if tx.Type != lighter.TxTypeL2Transfer {
140141
return errors.New("invalid transaction type")
141142
}
@@ -144,14 +145,15 @@ func (h *LighterMessageHandler) verifyWithdrawal(tx *lighter.LighterTx, borrowAm
144145
return errors.New("transfer account index invalid")
145146
}
146147

147-
if borrowAmount.Cmp(new(big.Int).SetUint64(tx.Transfer.USDCAmount)) != -1 {
148-
return errors.New("borrow amount higher than transfer amount")
148+
if tx.Transfer.USDCAmount <= FEE.Uint64() {
149+
return errors.New("fee higher than withdrawal amount")
149150
}
150151

151152
return nil
152153
}
153154

154-
func (h *LighterMessageHandler) calldata(tx *lighter.LighterTx, borrowAmount *big.Int) ([]byte, error) {
155+
func (h *LighterMessageHandler) calldata(tx *lighter.LighterTx) ([]byte, error) {
156+
borrowAmount := new(big.Int).Sub(new(big.Int).SetUint64(tx.Transfer.USDCAmount), FEE)
155157
return consts.LighterABI.Pack(
156158
"fulfillWithdraw",
157159
common.HexToHash(tx.Hash),

chains/lighter/message/lighter_test.go

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,15 @@ func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_ValidMessage() {
8383
ErrChn: errChn,
8484
Nonce: big.NewInt(101),
8585
LiquidityPool: common.HexToAddress("0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657"),
86-
Caller: common.HexToAddress("0xde526bA5d1ad94cC59D7A79d99A59F607d31A657"),
8786
OrderHash: "orderHash",
8887
DepositTxHash: "orderHash",
89-
BorrowAmount: big.NewInt(1900000),
9088
}
9189

9290
s.mockCoordinator.EXPECT().Execute(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil)
9391
s.mockTxFetcher.EXPECT().GetTx(ad.OrderHash).Return(&lighter.LighterTx{
9492
Type: lighter.TxTypeL2Transfer,
9593
Transfer: &lighter.Transfer{
96-
USDCAmount: 2000000,
94+
USDCAmount: 2000001,
9795
ToAccountIndex: 3,
9896
Memo: []byte{238, 123, 250, 212, 202, 237, 62, 98, 106, 248, 169, 199, 213, 3, 76, 213, 137, 238, 73, 144, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
9997
},
@@ -113,7 +111,7 @@ func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_ValidMessage() {
113111
s.Nil(err)
114112
}
115113

116-
func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_InvalidTxType() {
114+
func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_FeeHigherThanAmount() {
117115
s.mockCommunication.EXPECT().Broadcast(
118116
gomock.Any(),
119117
gomock.Any(),
@@ -128,13 +126,12 @@ func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_InvalidTxType() {
128126
ErrChn: errChn,
129127
Nonce: big.NewInt(101),
130128
LiquidityPool: common.HexToAddress("0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657"),
131-
Caller: common.HexToAddress("0xde526bA5d1ad94cC59D7A79d99A59F607d31A657"),
132129
OrderHash: "orderHash",
133130
DepositTxHash: "orderHash",
134-
BorrowAmount: big.NewInt(1900000),
135131
}
132+
136133
s.mockTxFetcher.EXPECT().GetTx(ad.OrderHash).Return(&lighter.LighterTx{
137-
Type: lighter.TxTypeL2Withdraw,
134+
Type: lighter.TxTypeL2Transfer,
138135
Transfer: &lighter.Transfer{
139136
USDCAmount: 2000000,
140137
ToAccountIndex: 3,
@@ -156,7 +153,7 @@ func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_InvalidTxType() {
156153
s.NotNil(err)
157154
}
158155

159-
func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_InvalidAccount() {
156+
func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_InvalidTxType() {
160157
s.mockCommunication.EXPECT().Broadcast(
161158
gomock.Any(),
162159
gomock.Any(),
@@ -171,16 +168,14 @@ func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_InvalidAccount() {
171168
ErrChn: errChn,
172169
Nonce: big.NewInt(101),
173170
LiquidityPool: common.HexToAddress("0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657"),
174-
Caller: common.HexToAddress("0xde526bA5d1ad94cC59D7A79d99A59F607d31A657"),
175171
OrderHash: "orderHash",
176172
DepositTxHash: "orderHash",
177-
BorrowAmount: big.NewInt(1900000),
178173
}
179174
s.mockTxFetcher.EXPECT().GetTx(ad.OrderHash).Return(&lighter.LighterTx{
180-
Type: lighter.TxTypeL2Transfer,
175+
Type: lighter.TxTypeL2Withdraw,
181176
Transfer: &lighter.Transfer{
182-
USDCAmount: 2000000,
183-
ToAccountIndex: 5,
177+
USDCAmount: 2000001,
178+
ToAccountIndex: 3,
184179
Memo: []byte{238, 123, 250, 212, 202, 237, 62, 98, 106, 248, 169, 199, 213, 3, 76, 213, 137, 238, 73, 144, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
185180
},
186181
}, nil)
@@ -199,7 +194,7 @@ func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_InvalidAccount() {
199194
s.NotNil(err)
200195
}
201196

202-
func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_MissingTx() {
197+
func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_InvalidAccount() {
203198
s.mockCommunication.EXPECT().Broadcast(
204199
gomock.Any(),
205200
gomock.Any(),
@@ -214,12 +209,17 @@ func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_MissingTx() {
214209
ErrChn: errChn,
215210
Nonce: big.NewInt(101),
216211
LiquidityPool: common.HexToAddress("0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657"),
217-
Caller: common.HexToAddress("0xde526bA5d1ad94cC59D7A79d99A59F607d31A657"),
218212
OrderHash: "orderHash",
219213
DepositTxHash: "orderHash",
220-
BorrowAmount: big.NewInt(1900000),
221214
}
222-
s.mockTxFetcher.EXPECT().GetTx(ad.OrderHash).Return(nil, fmt.Errorf("not found"))
215+
s.mockTxFetcher.EXPECT().GetTx(ad.OrderHash).Return(&lighter.LighterTx{
216+
Type: lighter.TxTypeL2Transfer,
217+
Transfer: &lighter.Transfer{
218+
USDCAmount: 2000001,
219+
ToAccountIndex: 5,
220+
Memo: []byte{238, 123, 250, 212, 202, 237, 62, 98, 106, 248, 169, 199, 213, 3, 76, 213, 137, 238, 73, 144, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
221+
},
222+
}, nil)
223223

224224
m := &coreMessage.Message{
225225
Data: ad,
@@ -235,7 +235,7 @@ func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_MissingTx() {
235235
s.NotNil(err)
236236
}
237237

238-
func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_BorrowAmountTooHigh() {
238+
func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_MissingTx() {
239239
s.mockCommunication.EXPECT().Broadcast(
240240
gomock.Any(),
241241
gomock.Any(),
@@ -250,20 +250,10 @@ func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_BorrowAmountTooHigh(
250250
ErrChn: errChn,
251251
Nonce: big.NewInt(101),
252252
LiquidityPool: common.HexToAddress("0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657"),
253-
Caller: common.HexToAddress("0xde526bA5d1ad94cC59D7A79d99A59F607d31A657"),
254253
OrderHash: "orderHash",
255254
DepositTxHash: "orderHash",
256-
BorrowAmount: big.NewInt(2000000),
257255
}
258-
259-
s.mockTxFetcher.EXPECT().GetTx(ad.OrderHash).Return(&lighter.LighterTx{
260-
Type: lighter.TxTypeL2Transfer,
261-
Transfer: &lighter.Transfer{
262-
USDCAmount: 2000000,
263-
ToAccountIndex: 3,
264-
Memo: []byte{238, 123, 250, 212, 202, 237, 62, 98, 106, 248, 169, 199, 213, 3, 76, 213, 137, 238, 73, 144, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
265-
},
266-
}, nil)
256+
s.mockTxFetcher.EXPECT().GetTx(ad.OrderHash).Return(nil, fmt.Errorf("not found"))
267257

268258
m := &coreMessage.Message{
269259
Data: ad,

chains/lighter/message/message.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@ type LighterData struct {
1616
OrderHash string
1717
Coordinator peer.ID
1818
LiquidityPool common.Address
19-
Caller common.Address
2019
DepositTxHash string
2120
Calldata string
2221
Nonce *big.Int
23-
BorrowAmount *big.Int
2422
Deadline uint64
2523
Source uint64
2624
Destination uint64

0 commit comments

Comments
 (0)