Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ func Run() error {
lighterConfig.WithdrawalAddress,
lighterConfig.UsdcAddress,
lighterConfig.RepaymentAddress,
lighterConfig.ConfirmationsByValue,
lighterAPI,
coordinator,
host,
Expand Down
15 changes: 12 additions & 3 deletions chains/lighter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type LighterConfig struct {
WithdrawalAddress common.Address
UsdcAddress common.Address
RepaymentAddress string
// usd bucket -> confirmations
ConfirmationsByValue map[uint64]uint64
}

func NewLighterConfig(solverConfig solverConfig.SolverConfig) (*LighterConfig, error) {
Expand All @@ -37,9 +39,16 @@ func NewLighterConfig(solverConfig solverConfig.SolverConfig) (*LighterConfig, e
return nil, fmt.Errorf("withdrawal address not configured")
}

confirmations := make(map[uint64]uint64)
for _, confirmation := range solverConfig.Chains[LIGHTER_CAIP].Confirmations {
// nolint:gosec
confirmations[uint64(confirmation.MaxAmountUSD)] = uint64(confirmation.Confirmations)
}

return &LighterConfig{
WithdrawalAddress: common.HexToAddress(withdrawalAddress),
RepaymentAddress: solverConfig.ProtocolsMetadata.Lighter.RepaymentAddress,
UsdcAddress: common.HexToAddress(usdcConfig.Address),
WithdrawalAddress: common.HexToAddress(withdrawalAddress),
RepaymentAddress: solverConfig.ProtocolsMetadata.Lighter.RepaymentAddress,
UsdcAddress: common.HexToAddress(usdcConfig.Address),
ConfirmationsByValue: confirmations,
}, nil
}
23 changes: 20 additions & 3 deletions chains/lighter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,26 @@ func (s *NewLighterConfigTestSuite) Test_ValidConfig() {
Decimals: 6,
}

expectedBlockConfirmations := make(map[uint64]uint64)
expectedBlockConfirmations[1000] = 5
expectedBlockConfirmations[2000] = 10

solverChains := make(map[string]solverConfig.Chain)
solverChains["eip155:42161"] = solverConfig.Chain{
Tokens: tokens,
}
solverChains["lighter:1"] = solverConfig.Chain{
Confirmations: []solverConfig.Confirmations{
{
Confirmations: 5,
MaxAmountUSD: 1000,
},
{
Confirmations: 10,
MaxAmountUSD: 2000,
},
},
}

config, err := lighter.NewLighterConfig(solverConfig.SolverConfig{
Chains: solverChains,
Expand All @@ -90,8 +106,9 @@ func (s *NewLighterConfigTestSuite) Test_ValidConfig() {

s.Nil(err)
s.Equal(config, &lighter.LighterConfig{
WithdrawalAddress: common.HexToAddress("withdrawal"),
UsdcAddress: common.HexToAddress("usdc"),
RepaymentAddress: "3",
WithdrawalAddress: common.HexToAddress("withdrawal"),
UsdcAddress: common.HexToAddress("usdc"),
RepaymentAddress: "3",
ConfirmationsByValue: expectedBlockConfirmations,
})
}
27 changes: 25 additions & 2 deletions chains/lighter/message/lighter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (
"encoding/json"
"errors"
"fmt"
"maps"
"math"
"math/big"
"slices"
"strconv"

"github.com/ethereum/go-ethereum/common"
Expand All @@ -24,8 +27,9 @@ import (
)

var (
ARBITRUM_CHAIN_ID = big.NewInt(42161)
USDC_ACCOUNT_INDEX uint64 = 3
ARBITRUM_CHAIN_ID = big.NewInt(42161)
USDC_ACCOUNT_INDEX uint64 = 3
USDC_DECIMALS float64 = 6
)

type Coordinator interface {
Expand All @@ -47,12 +51,14 @@ type LighterMessageHandler struct {
usdcAddress common.Address
repaymentAccount string
txFetcher TxFetcher
confirmations map[uint64]uint64
}

func NewLighterMessageHandler(
lighterAddress common.Address,
usdcAddress common.Address,
repaymentAccount string,
confirmations map[uint64]uint64,
txFetcher TxFetcher,
coordinator Coordinator,
host host.Host,
Expand All @@ -70,6 +76,7 @@ func NewLighterMessageHandler(
comm: comm,
fetcher: fetcher,
sigChn: sigChn,
confirmations: confirmations,
}
}

Expand Down Expand Up @@ -149,9 +156,25 @@ func (h *LighterMessageHandler) verifyWithdrawal(tx *lighter.LighterTx) error {
return errors.New("only usdc asset supported on lighter")
}

if err := h.verifyOrderSize(tx.Transfer.Amount / uint64(math.Pow(10, USDC_DECIMALS))); err != nil {
return err
}

return nil
}

func (h *LighterMessageHandler) verifyOrderSize(orderValue uint64) error {
buckets := slices.Collect(maps.Keys(h.confirmations))
slices.Sort(buckets)
for _, bucket := range buckets {
if orderValue < bucket {
return nil
}
}

return fmt.Errorf("order value %d exceeds confirmation buckets", orderValue)
}

func (h *LighterMessageHandler) calldata(tx *lighter.LighterTx) ([]byte, error) {
return consts.LighterABI.Pack(
"fulfillWithdraw",
Expand Down
47 changes: 46 additions & 1 deletion chains/lighter/message/lighter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,14 @@ func (s *LighterMessageHandlerTestSuite) SetupTest() {
s.mockTxFetcher = mock_message.NewMockTxFetcher(ctrl)

s.sigChn = make(chan interface{}, 1)
confirmations := make(map[uint64]uint64)
confirmations[200] = 0

s.handler = message.NewLighterMessageHandler(
common.Address{},
common.Address{},
"3",
confirmations,
s.mockTxFetcher,
s.mockCoordinator,
s.mockHost,
Expand Down Expand Up @@ -173,7 +176,7 @@ func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_InvalidAsset() {
DepositTxHash: "orderHash",
}
s.mockTxFetcher.EXPECT().GetTx(ad.OrderHash).Return(&lighter.LighterTx{
Type: lighter.TxTypeL2Withdraw,
Type: lighter.TxTypeL2Transfer,
Transfer: &lighter.Transfer{
Amount: 2000001,
AssetIndex: 2,
Expand Down Expand Up @@ -238,6 +241,48 @@ func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_InvalidAccount() {
s.NotNil(err)
}

func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_InvalidOrderValue() {
s.mockCommunication.EXPECT().Broadcast(
gomock.Any(),
gomock.Any(),
comm.LighterMsg,
"lighter",
).Return(nil)
p, _ := pstoremem.NewPeerstore()
s.mockHost.EXPECT().Peerstore().Return(p)

errChn := make(chan error, 1)
ad := &message.LighterData{
ErrChn: errChn,
Nonce: big.NewInt(101),
LiquidityPool: common.HexToAddress("0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657"),
OrderHash: "orderHash",
DepositTxHash: "orderHash",
}
s.mockTxFetcher.EXPECT().GetTx(ad.OrderHash).Return(&lighter.LighterTx{
Type: lighter.TxTypeL2Transfer,
Transfer: &lighter.Transfer{
Amount: 200000001,
AssetIndex: 3,
ToAccountIndex: 3,
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},
},
}, nil)

m := &coreMessage.Message{
Data: ad,
Source: 0,
Destination: 10,
}
prop, err := s.handler.HandleMessage(m)

s.Nil(prop)
s.NotNil(err)

err = <-errChn
s.NotNil(err)
}

func (s *LighterMessageHandlerTestSuite) Test_HandleMessage_MissingTx() {
s.mockCommunication.EXPECT().Broadcast(
gomock.Any(),
Expand Down
Loading