Skip to content

Commit da6b5c3

Browse files
authored
fix: lighter updates to address their api breaking changes (#118)
<!--- 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 --------- Signed-off-by: Marin Petrunic <[email protected]>
1 parent 2359568 commit da6b5c3

File tree

4 files changed

+64
-12
lines changed

4 files changed

+64
-12
lines changed

chains/lighter/message/lighter.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ import (
2424
)
2525

2626
var (
27-
ARBITRUM_CHAIN_ID = big.NewInt(42161)
28-
FEE = big.NewInt(2000000)
27+
ARBITRUM_CHAIN_ID = big.NewInt(42161)
28+
FEE = big.NewInt(2000000)
29+
USDC_ACCOUNT_INDEX = 3
2930
)
3031

3132
type Coordinator interface {
@@ -104,7 +105,7 @@ func (h *LighterMessageHandler) HandleMessage(m *message.Message) (*proposal.Pro
104105

105106
unlockHash, err := signature.BorrowUnlockHash(
106107
calldata,
107-
new(big.Int).SetUint64(tx.Transfer.USDCAmount),
108+
new(big.Int).SetUint64(tx.Transfer.Amount),
108109
h.usdcAddress,
109110
ARBITRUM_CHAIN_ID,
110111
h.lighterAddress,
@@ -145,15 +146,19 @@ func (h *LighterMessageHandler) verifyWithdrawal(tx *lighter.LighterTx) error {
145146
return errors.New("transfer account index invalid")
146147
}
147148

148-
if tx.Transfer.USDCAmount <= FEE.Uint64() {
149+
if tx.Transfer.AssetIndex != uint64(USDC_ACCOUNT_INDEX) {
150+
return errors.New("only usdc asset supported on lighter")
151+
}
152+
153+
if tx.Transfer.Amount <= FEE.Uint64() {
149154
return errors.New("fee higher than withdrawal amount")
150155
}
151156

152157
return nil
153158
}
154159

155160
func (h *LighterMessageHandler) calldata(tx *lighter.LighterTx) ([]byte, error) {
156-
borrowAmount := new(big.Int).Sub(new(big.Int).SetUint64(tx.Transfer.USDCAmount), FEE)
161+
borrowAmount := new(big.Int).Sub(new(big.Int).SetUint64(tx.Transfer.Amount), FEE)
157162
return consts.LighterABI.Pack(
158163
"fulfillWithdraw",
159164
common.HexToHash(tx.Hash),

chains/lighter/message/lighter_test.go

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_ValidMessage() {
9191
s.mockTxFetcher.EXPECT().GetTx(ad.OrderHash).Return(&lighter.LighterTx{
9292
Type: lighter.TxTypeL2Transfer,
9393
Transfer: &lighter.Transfer{
94-
USDCAmount: 2000001,
94+
Amount: 2000001,
95+
AssetIndex: 3,
9596
ToAccountIndex: 3,
9697
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},
9798
},
@@ -133,7 +134,8 @@ func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_FeeHigherThanAmount(
133134
s.mockTxFetcher.EXPECT().GetTx(ad.OrderHash).Return(&lighter.LighterTx{
134135
Type: lighter.TxTypeL2Transfer,
135136
Transfer: &lighter.Transfer{
136-
USDCAmount: 2000000,
137+
Amount: 2000000,
138+
AssetIndex: 3,
137139
ToAccountIndex: 3,
138140
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},
139141
},
@@ -174,7 +176,50 @@ func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_InvalidTxType() {
174176
s.mockTxFetcher.EXPECT().GetTx(ad.OrderHash).Return(&lighter.LighterTx{
175177
Type: lighter.TxTypeL2Withdraw,
176178
Transfer: &lighter.Transfer{
177-
USDCAmount: 2000001,
179+
Amount: 2000001,
180+
AssetIndex: 3,
181+
ToAccountIndex: 3,
182+
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},
183+
},
184+
}, nil)
185+
186+
m := &coreMessage.Message{
187+
Data: ad,
188+
Source: 0,
189+
Destination: 10,
190+
}
191+
prop, err := s.handler.HandleMessage(m)
192+
193+
s.Nil(prop)
194+
s.NotNil(err)
195+
196+
err = <-errChn
197+
s.NotNil(err)
198+
}
199+
200+
func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_InvalidAsset() {
201+
s.mockCommunication.EXPECT().Broadcast(
202+
gomock.Any(),
203+
gomock.Any(),
204+
comm.LighterMsg,
205+
"lighter",
206+
).Return(nil)
207+
p, _ := pstoremem.NewPeerstore()
208+
s.mockHost.EXPECT().Peerstore().Return(p)
209+
210+
errChn := make(chan error, 1)
211+
ad := &message.LighterData{
212+
ErrChn: errChn,
213+
Nonce: big.NewInt(101),
214+
LiquidityPool: common.HexToAddress("0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657"),
215+
OrderHash: "orderHash",
216+
DepositTxHash: "orderHash",
217+
}
218+
s.mockTxFetcher.EXPECT().GetTx(ad.OrderHash).Return(&lighter.LighterTx{
219+
Type: lighter.TxTypeL2Withdraw,
220+
Transfer: &lighter.Transfer{
221+
Amount: 2000001,
222+
AssetIndex: 2,
178223
ToAccountIndex: 3,
179224
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},
180225
},
@@ -215,7 +260,8 @@ func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_InvalidAccount() {
215260
s.mockTxFetcher.EXPECT().GetTx(ad.OrderHash).Return(&lighter.LighterTx{
216261
Type: lighter.TxTypeL2Transfer,
217262
Transfer: &lighter.Transfer{
218-
USDCAmount: 2000001,
263+
Amount: 2000001,
264+
AssetIndex: 3,
219265
ToAccountIndex: 5,
220266
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},
221267
},

protocol/lighter/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ const (
2828
)
2929

3030
type Transfer struct {
31-
USDCAmount uint64
31+
Amount uint64
32+
AssetIndex uint64
3233
FromAccountIndex uint64
3334
ToAccountIndex int
3435
Fee uint64

protocol/lighter/mock/mockData.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
package mock
33

44
const LighterMockResponse = `
5-
{"code":200,"hash":"1e16eea4ad2ea1db483e7a0aeea2149fcafd96da3b9e7ecab5e95506f002182d1809858336e2af22","type":12,"info":"{\"FromAccountIndex\":283390,\"ApiKeyIndex\":0,\"ToAccountIndex\":3,\"USDCAmount\":2000000,\"Fee\":3000000,\"Memo\":[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],\"ExpiredAt\":1761149674469,\"Nonce\":2,\"Sig\":\"crr/TWDu2CzHpkOtGaTIow70UcgsdfChqSYurrEoFO/K+f9nZHvpOzNVXtZQBf7JhhVyMK9dDbzgt3nUK7z9N/GdbH/NakIGHzVod+9ULhQ=\",\"L1Sig\":\"0x320bdc9a593130a7e25103a572458188dd4e2efdb18c5d862c7e54da10d780fe751b444a259195baa4b5a7e3dc600dfa2227ec077a09919f3f97a736475eea471c\"}","event_info":"{\"f\":283390,\"t\":3,\"u\":2000000,\"fee\":3000000,\"ae\":\"\"}","status":3,"transaction_index":16,"l1_address":"0xEe7BfAD4caEd3e626AF8a9C7d5034CD589EE4990","account_index":283390,"nonce":2,"expire_at":1761149674469,"block_height":70687079,"queued_at":1761149084285,"sequence_index":16778685698,"parent_hash":"","committed_at":0,"verified_at":0,"executed_at":1761149084292}
5+
{"code":200,"hash":"1e16eea4ad2ea1db483e7a0aeea2149fcafd96da3b9e7ecab5e95506f002182d1809858336e2af22","type":12,"info":"{\"FromAccountIndex\":283390,\"ApiKeyIndex\":0,\"ToAccountIndex\":3,\"Amount\":2000000,\"AssetIndex\":3,\"Fee\":3000000,\"Memo\":[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],\"ExpiredAt\":1761149674469,\"Nonce\":2,\"Sig\":\"crr/TWDu2CzHpkOtGaTIow70UcgsdfChqSYurrEoFO/K+f9nZHvpOzNVXtZQBf7JhhVyMK9dDbzgt3nUK7z9N/GdbH/NakIGHzVod+9ULhQ=\",\"L1Sig\":\"0x320bdc9a593130a7e25103a572458188dd4e2efdb18c5d862c7e54da10d780fe751b444a259195baa4b5a7e3dc600dfa2227ec077a09919f3f97a736475eea471c\"}","event_info":"{\"f\":283390,\"t\":3,\"u\":2000000,\"fee\":3000000,\"ae\":\"\"}","status":3,"transaction_index":16,"l1_address":"0xEe7BfAD4caEd3e626AF8a9C7d5034CD589EE4990","account_index":283390,"nonce":2,"expire_at":1761149674469,"block_height":70687079,"queued_at":1761149084285,"sequence_index":16778685698,"parent_hash":"","committed_at":0,"verified_at":0,"executed_at":1761149084292}
66
`
77

8-
const ExpectedLighterResponse = `{"code":200,"hash":"1e16eea4ad2ea1db483e7a0aeea2149fcafd96da3b9e7ecab5e95506f002182d1809858336e2af22","type":12,"info":"{\"FromAccountIndex\":283390,\"ApiKeyIndex\":0,\"ToAccountIndex\":3,\"USDCAmount\":2000000,\"Fee\":3000000,\"Memo\":[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],\"ExpiredAt\":1761149674469,\"Nonce\":2,\"Sig\":\"crr/TWDu2CzHpkOtGaTIow70UcgsdfChqSYurrEoFO/K+f9nZHvpOzNVXtZQBf7JhhVyMK9dDbzgt3nUK7z9N/GdbH/NakIGHzVod+9ULhQ=\",\"L1Sig\":\"0x320bdc9a593130a7e25103a572458188dd4e2efdb18c5d862c7e54da10d780fe751b444a259195baa4b5a7e3dc600dfa2227ec077a09919f3f97a736475eea471c\"}","event_info":"{\"f\":283390,\"t\":3,\"u\":2000000,\"fee\":3000000,\"ae\":\"\"}","status":3,"transaction_index":16,"l1_address":"0xEe7BfAD4caEd3e626AF8a9C7d5034CD589EE4990","account_index":283390,"nonce":2,"expire_at":1761149674469,"block_height":70687079,"queued_at":1761149084285,"sequence_index":16778685698,"parent_hash":"","committed_at":0,"verified_at":0,"executed_at":1761149084292}`
8+
const ExpectedLighterResponse = `{"code":200,"hash":"1e16eea4ad2ea1db483e7a0aeea2149fcafd96da3b9e7ecab5e95506f002182d1809858336e2af22","type":12,"info":"{\"FromAccountIndex\":283390,\"ApiKeyIndex\":0,\"ToAccountIndex\":3,\"Amount\":2000000,\"AssetIndex\":3,\"Fee\":3000000,\"Memo\":[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],\"ExpiredAt\":1761149674469,\"Nonce\":2,\"Sig\":\"crr/TWDu2CzHpkOtGaTIow70UcgsdfChqSYurrEoFO/K+f9nZHvpOzNVXtZQBf7JhhVyMK9dDbzgt3nUK7z9N/GdbH/NakIGHzVod+9ULhQ=\",\"L1Sig\":\"0x320bdc9a593130a7e25103a572458188dd4e2efdb18c5d862c7e54da10d780fe751b444a259195baa4b5a7e3dc600dfa2227ec077a09919f3f97a736475eea471c\"}","event_info":"{\"f\":283390,\"t\":3,\"u\":2000000,\"fee\":3000000,\"ae\":\"\"}","status":3,"transaction_index":16,"l1_address":"0xEe7BfAD4caEd3e626AF8a9C7d5034CD589EE4990","account_index":283390,"nonce":2,"expire_at":1761149674469,"block_height":70687079,"queued_at":1761149084285,"sequence_index":16778685698,"parent_hash":"","committed_at":0,"verified_at":0,"executed_at":1761149084292}`

0 commit comments

Comments
 (0)