Skip to content
Merged
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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ genmocks:
mockgen -destination=./comm/p2p/mock/stream/stream.go github.com/libp2p/go-libp2p/core/network Stream,Conn
mockgen -source=./chains/evm/message/across.go -destination=./chains/evm/message/mock/across.go
mockgen -source=./chains/evm/message/lifiEscrow.go -destination=./chains/evm/message/mock/lifiEscrow.go
mockgen -source=./chains/evm/message/unlock.go -destination=./chains/evm/message/mock/unlock.go
mockgen -source=./chains/evm/message/confirmations.go -destination=./chains/evm/message/mock/confirmations.go
mockgen -source=./api/handlers/signing.go -destination=./api/handlers/mock/signing.go
mockgen -package mock_message -destination=./chains/evm/message/mock/pricing.go github.com/sprintertech/lifi-solver/pkg/pricing OrderPricer
Expand Down
33 changes: 20 additions & 13 deletions api/handlers/unlock.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ import (
const SIGNATURE_TIMEOUT = time.Second * 15

type UnlockResponse struct {
Signature string `json:"signature"`
ID string `json:"id"`
Signature string `json:"signature"`
RepaymentAddress string `json:"repaymentAddress"`
ID string `json:"id"`
}

type UnlockBody struct {
ChainId uint64
Protocol ProtocolType `json:"protocol"`
OrderID string `json:"orderId"`
Settler string `json:"settler"`
ChainId uint64
Protocol ProtocolType `json:"protocol"`
OrderID string `json:"orderId"`
Settler string `json:"settler"`
BorrowToken string `json:"borrowToken"`
}
Comment thread
mpetrun5 marked this conversation as resolved.

type UnlockHandler struct {
Expand Down Expand Up @@ -60,16 +62,19 @@ func (h *UnlockHandler) HandleUnlock(w http.ResponseWriter, r *http.Request) {
}

sigChn := make(chan interface{}, 1)
repaymentAddressChn := make(chan common.Hash, 1)
var m *message.Message
switch b.Protocol {
case LifiEscrowProtocol:
{
m = evmMessage.NewLifiUnlockMessage(0, b.ChainId, &evmMessage.LifiUnlockData{
Source: 0,
Destination: b.ChainId,
SigChn: sigChn,
OrderID: b.OrderID,
Settler: common.HexToAddress(b.Settler),
Source: 0,
Destination: b.ChainId,
SigChn: sigChn,
OrderID: b.OrderID,
Settler: common.HexToAddress(b.Settler),
RepaymentAddressChn: repaymentAddressChn,
BorrowToken: b.BorrowToken,
Comment thread
mpetrun5 marked this conversation as resolved.
})
}
default:
Expand All @@ -85,15 +90,17 @@ func (h *UnlockHandler) HandleUnlock(w http.ResponseWriter, r *http.Request) {
return
case sig := <-sigChn:
{
repaymentAddress := <-repaymentAddressChn
sig, ok := sig.(signing.EcdsaSignature)
if !ok {
JSONError(w, fmt.Errorf("invalid signature"), http.StatusInternalServerError)
return
}

data, _ := json.Marshal(UnlockResponse{
Signature: hex.EncodeToString(sig.Signature),
ID: sig.ID,
Signature: hex.EncodeToString(sig.Signature),
ID: sig.ID,
RepaymentAddress: repaymentAddress.Hex(),
})
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
Expand Down
9 changes: 8 additions & 1 deletion api/handlers/unlock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http/httptest"
"testing"

"github.com/ethereum/go-ethereum/common"
"github.com/gorilla/mux"
"github.com/sprintertech/sprinter-signing/api/handlers"
across "github.com/sprintertech/sprinter-signing/chains/evm/message"
Expand Down Expand Up @@ -117,6 +118,7 @@ func (s *UnlockHandlerTestSuite) Test_HandleUnlock_ValidRequest() {
Signature: sigBytes,
ID: "id",
}
ad.RepaymentAddressChn <- common.HexToHash("0x1")
}()

handler.HandleUnlock(recorder, req)
Expand All @@ -125,5 +127,10 @@ func (s *UnlockHandlerTestSuite) Test_HandleUnlock_ValidRequest() {
data, err := io.ReadAll(recorder.Body)
s.Nil(err)

s.Equal(string(data), "{\"signature\":\"abcd\",\"id\":\"id\"}")
s.Equal(
string(data),
"{\"signature\":\"abcd\","+
"\"repaymentAddress\":\"0x0000000000000000000000000000000000000000000000000000000000000001\","+
"\"id\":\"id\"}",
)
}
31 changes: 19 additions & 12 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ func Run() error {
err = usdPricer.Start(ctx)
panicOnError(err)
multiPricer := aggregator.New(usdPricer)
resolver := token.NewTokenResolver(solverConfig, multiPricer)

var hubPoolContract across.TokenMatcher
acrossPools := make(map[uint64]common.Address)
Expand Down Expand Up @@ -284,7 +285,6 @@ func Run() error {
}

if c.LifiOutputSettler != "" {

if *c.GeneralChainConfig.Id == ETHEREUM {
w3Client, err := w3.Dial(c.GeneralChainConfig.Endpoint)
panicOnError(err)
Expand All @@ -300,7 +300,7 @@ func Run() error {
)
multiPricer.Add(vaultPricer, "srRoyUSDC")
}
resolver := token.NewTokenResolver(solverConfig, multiPricer)
resolver = token.NewTokenResolver(solverConfig, multiPricer)

lifiConfig, err := lifiConfig.GetSolverConfig(solverConfig, protocols.LifiEscrow, lifiConfig.PulsarSolver)
panicOnError(err)
Expand Down Expand Up @@ -351,16 +351,23 @@ func Run() error {
)
}

lifiUnlockMh := evmMessage.NewLifiUnlockHandler(
*c.GeneralChainConfig.Id,
repayerAddresses,
coordinator,
host,
communication,
keyshareStore,
)
go lifiUnlockMh.Listen(ctx)
mh.RegisterMessageHandler(message.MessageType(comm.LifiUnlockMsg.String()), lifiUnlockMh)
repayer, ok := repayerAddresses[*c.GeneralChainConfig.Id]
if ok {
lifiAPI := lifi.NewLifiAPI()
lifiUnlockMh := evmMessage.NewLifiUnlockHandler(
*c.GeneralChainConfig.Id,
repayer,
c.Processors,
lifiAPI,
resolver,
coordinator,
host,
communication,
keyshareStore,
)
go lifiUnlockMh.Listen(ctx)
mh.RegisterMessageHandler(message.MessageType(comm.LifiUnlockMsg.String()), lifiUnlockMh)
}

var startBlock *big.Int
var listener *coreListener.EVMListener
Expand Down
8 changes: 8 additions & 0 deletions chains/evm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package evm
import (
"fmt"
"math/big"
"strings"
"time"

"github.com/creasty/defaults"
Expand All @@ -28,6 +29,7 @@ type EVMConfig struct {
LifiOutputSettler string
LifiInputSettlerEscrow string
Repayer string
Processors map[string]common.Address
// Liquidator contract per token address
Liquidators map[common.Address]common.Address

Expand Down Expand Up @@ -97,6 +99,11 @@ func NewEVMConfig(chainConfig map[string]interface{}, solverConfig solverConfig.
}
}

processors := make(map[string]common.Address)
for symbol, processor := range solverConfig.ProtocolsMetadata.Lifi.RepaymentProcessors[id] {
processors[strings.ToLower(symbol)] = common.HexToAddress(processor.Address)
}

confirmations := make(map[uint64]uint64)
for _, confirmation := range sc.Confirmations {
// nolint:gosec
Expand All @@ -115,6 +122,7 @@ func NewEVMConfig(chainConfig map[string]interface{}, solverConfig solverConfig.
GeneralChainConfig: c.GeneralChainConfig,
Admin: c.Admin,
Repayer: solverConfig.ProtocolsMetadata.Sprinter.Repayer[id],
Processors: processors,
AcrossPool: solverConfig.ProtocolsMetadata.Across.SpokePools[id],
AcrossHubPool: solverConfig.ProtocolsMetadata.Across.HubPools[id],
LifiOutputSettler: solverConfig.ProtocolsMetadata.Lifi.OutputSettler,
Expand Down
11 changes: 11 additions & 0 deletions chains/evm/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func (s *NewEVMConfigTestSuite) Test_ValidConfig() {
Liquidators: map[common.Address]common.Address{
common.HexToAddress("0x12"): common.HexToAddress(liquidator),
},
Processors: make(map[string]common.Address),
})
}

Expand Down Expand Up @@ -231,10 +232,19 @@ func (s *NewEVMConfigTestSuite) Test_ValidConfigWithCustomTxParams() {
},
Lifi: &solverConfig.LifiMetadata{
OutputSettler: "settler",
RepaymentProcessors: map[string]map[string]solverConfig.RepaymentProcessor{
"eip155:1": map[string]solverConfig.RepaymentProcessor{
"uSdC": solverConfig.RepaymentProcessor{
Address: "0xdBBE3D8c2d2b22A2611c5A94A9a12C2fCD49Eb28",
},
},
},
},
},
})

processors := make(map[string]common.Address)
processors["usdc"] = common.HexToAddress("0xdBBE3D8c2d2b22A2611c5A94A9a12C2fCD49Eb28")
id := new(uint64)
*id = 1
s.Nil(err)
Expand All @@ -255,6 +265,7 @@ func (s *NewEVMConfigTestSuite) Test_ValidConfigWithCustomTxParams() {
Repayer: "repayer",
ConfirmationsByValue: expectedBlockConfirmations,
Tokens: expectedTokens,
Processors: processors,
Liquidators: make(map[common.Address]common.Address),
})
}
22 changes: 11 additions & 11 deletions chains/evm/message/lifiEscrow.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,32 +220,32 @@ func (h *LifiEscrowMessageHandler) borrowToken(
return common.Address{}, destChainID, err
}

if data.BorrowToken != tokenIn.Symbol && data.BorrowToken != tokenOut.Symbol {
borrowToken, err := h.tokenResolver.Token(
order.GenericOutputs[0].ChainID,
common.HexToHash(data.BorrowToken))
Comment thread
mpetrun5 marked this conversation as resolved.
if err != nil {
return common.Address{}, destChainID, err
}

if borrowToken.Symbol != tokenIn.Symbol && borrowToken.Symbol != tokenOut.Symbol {
return common.Address{}, destChainID, fmt.Errorf(
"borrow token %s must be either input %s or output token symbol %s",
data.BorrowToken,
tokenIn.Symbol,
tokenOut.Symbol)
}

if data.BorrowToken == tokenIn.Symbol {
dstToken, err := h.tokenResolver.TokenFromSymbol(
order.GenericOutputs[0].ChainID,
tokenIn.Symbol)
if err != nil {
return common.Address{}, destChainID, err
}

if borrowToken.Symbol == tokenIn.Symbol {
scaledInputAmount := chains.ScaleTokenAmount(
order.GenericInputs[0].Amount,
tokenIn.Decimals,
dstToken.Decimals)
borrowToken.Decimals)
if scaledInputAmount.Cmp(data.BorrowAmount) == -1 {
return common.Address{}, destChainID, fmt.Errorf(
"order input is less than requested borrow amount")
}

return common.BytesToAddress(dstToken.Address[:]), destChainID, nil
return common.BytesToAddress(borrowToken.Address[:]), destChainID, nil
} else {
amountOutValue := tokenOut.AmountToUSD(data.BorrowAmount)
if amountInValue < amountOutValue {
Expand Down
8 changes: 5 additions & 3 deletions chains/evm/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,12 @@ func NewSprinterCreditMessage(
}

type LifiUnlockData struct {
SigChn chan interface{} `json:"-"`
SigChn chan interface{} `json:"-"`
RepaymentAddressChn chan common.Hash `json:"-"`

OrderID string
Settler common.Address
OrderID string
Settler common.Address
BorrowToken string

Coordinator peer.ID
Source uint64
Expand Down
97 changes: 97 additions & 0 deletions chains/evm/message/mock/unlock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading