Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: PoC Of Router #10

Merged
merged 7 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions best_swap_route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package router

type BestSwapRoute struct {
}
123 changes: 82 additions & 41 deletions core/alpha_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,97 @@ import "router/core/currency"

type AlphaRouter struct {
//chainId ChainId
//portionProvider IPortionProvider
portionProvider IPortionProvider
}

func NewAlphaRouter(params AlphaRouterParams) *AlphaRouter {
return &AlphaRouter{}
}

func (a AlphaRouter) route(
baseCurrency currency.Currency, // currencyIn
quoteCurrency currency.Currency, // currencyOut으로 바꿔도 될 것 같다.
amount float64,
// amount fractions.CurrencyAmount,
// tradeType TradeType,
// swapConfig SwapOptions,
// Todo: goroutine
func (a *AlphaRouter) route(
baseCurrency currency.Currency,
quoteCurrency currency.Currency,
amount float64, // todo: float64 -> fraction
tradeType TradeType,
swapConfig SwapOptions,
routerConfig AlphaRouterConfig,
) SwapRoute {
//originalAmount := amount

// currencyIn, currencyOut은 Currency 타입이고
// Currency 타입은 NativeCurrency(GNOT)이거나 Token 타입이다.
// 아래에서 Token 타입이길 원하는 듯하다.
//tokenIn := currencyIn.Wrapped()
//tokenOut := currencyOut.Wrapped()

//core 패키지를 TradeType 패키지로 변경하면 가독성이 더 좋아질 듯 하다.
//if tradeType == EXACT_OUTPUT {
// // TODO: GetPortionAmount에서 반환 값인 CurrencyAmount을 반환하지 못할 경우가 있을 수도 있다.(높은 확률로)
// portionAmount := a.portionProvider.GetPortionAmount(
// amount,
// tradeType,
// swapConfig,
// )
//
//result := portionAmount.GreaterThan(0)
//if result {
// amount = amount.add(portionAmount)
//}
//originalAmount := amount // for save

//currencyIn, currencyOut := a.determineCurrencyInOutFromTradeType(tradeType, baseCurrency, quoteCurrency)

// token은 currency의 wrapped된 버전이다.
//tokenIn := currencyIn.GetToken()
//tokenOut := currencyOut.GetToken()

// 왠만하면 함수로 뺄 것
// 내용 이해 필요
if tradeType == EXACT_OUTPUT {
portionAmount, portionErr := a.portionProvider.GetPortionAmount(amount, tradeType, swapConfig)

if portionErr == nil && portionAmount > 0 {
// In case of exact out swap, before we route, we need to make sure that the
// token out amount accounts for flat portion, and token in amount after the best swap route contains the token in equivalent of portion.
// In other words, in case a pool's LP fee bps is lower than the portion bps (0.01%/0.05% for v3), a pool can go insolvency.
// This is because instead of the swapper being responsible for the portion,
// the pool instead gets responsible for the portion.
// The addition below avoids that situation.
amount += portionAmount
}
}

// routing config merge다루는 부분 패스
//routerConfig = setRouterConfig(routingConfig, chainId)

// tokenIn 또는 tokenOut과 동일한 값...
//quoteToken := quoteCurrency.GetToken()

// main logic?
//routes := a.getSwapRouteFromChain(tokenIn, tokenOut, amount, tradeType, routingConfig)

//if routes == nil {
// // todo: error 처리 해 줄 것
//}

//trade := a.buildTrade(currencyIn, currencyOut, tradeType, routes)

swapRoute := a.buildSwapRoute()
return swapRoute
}

func (a *AlphaRouter) determineCurrencyInOutFromTradeType(
tradeType TradeType,
baseCurrency currency.Currency,
quoteCurrency currency.Currency,
) (currency.Currency, currency.Currency) {
if tradeType == EXACT_INPUT {
return baseCurrency, quoteCurrency
}
return quoteCurrency, baseCurrency
}

// todo: goroutine
func (a *AlphaRouter) getSwapRouteFromChain(tokenIn, tokenOut currency.Token, amount float64, tradeType TradeType, routingConfig AlphaRouterConfig) *BestSwapRoute {
//percents, amount := a.getAmountDistribution(amount, routingConfig)

return &BestSwapRoute{}
}

func (a *AlphaRouter) getAmountDistribution(amount float64, routingConfig AlphaRouterConfig) (float64, float64) {

return 0, 0
}

func (a *AlphaRouter) buildTrade(currencyIn currency.Currency, currencyOut currency.Currency, tradeType TradeType, routes Routes) Trade {

return Trade{}
}

func (a *AlphaRouter) buildSwapRoute() SwapRoute {
return SwapRoute{}
}

//
//func (a AlphaRouter) determineCurrencyInOutFromTradeType(
// tradeType TradeType,
// amount fractions.CurrencyAmount,
// quoteCurrency currency.Currency,
//) (currency.Currency, currency.Currency) {
// if tradeType == EXACT_INPUT {
// return amount.Currency, quoteCurrency
// } else {
// return quoteCurrency, amount.Currency
// }
//}
func (a *AlphaRouter) setRouterConfig(routerConfig AlphaRouterConfig, chainId int) AlphaRouterConfig {
return AlphaRouterConfig{}
}
9 changes: 9 additions & 0 deletions core/alpha_router_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package core

type AlphaRouterConfig struct {
v3ProtocolPoolSelection ProtocolPoolSelection
maxSwapsPerPath int
maxSplits int
minSplits int
distributionPercent int
}
4 changes: 4 additions & 0 deletions core/best_swap_route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package core

type BestSwapRoute struct {
}
1 change: 0 additions & 1 deletion core/currency/base_currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type BaseCurrency struct {
address string
}

// 이게 필요할까??
func NewBaseCurrency(chainId int64, decimals int64, symbol string, name string) *BaseCurrency {
// 아래 코드는 원문
//invariant(Number.isSafeInteger(chainId), 'CHAIN_ID');
Expand Down
1 change: 1 addition & 0 deletions core/currency/currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package currency

// Currency는 Token | NativeCurrency
type Currency interface {
GetToken() Token
}
4 changes: 4 additions & 0 deletions core/currency/native_currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ package currency
type NativeCurrency struct {
BaseCurrency
}

func (n *NativeCurrency) getToken() Token {
return Token{} // 임시
}
4 changes: 4 additions & 0 deletions core/currency/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ type Token struct {
BaseCurrency
address string
}

func (t *Token) getToken() Token {
return *t
}
8 changes: 8 additions & 0 deletions core/portion_provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core

type IPortionProvider interface {
GetPortionAmount(tokenOutAmount float64, tradeType TradeType, swapConfig SwapOptions) (float64, error)
}

type PortionProvider struct {
}
14 changes: 14 additions & 0 deletions core/protocol_pool_selection.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package core

type ProtocolPoolSelection struct {
topN int
topNDirectSwaps int
topNTokenInOut int
topNSecondHop int
topNWithEachBaseToken int
topNWithBaseToken int

// selectable variable
//topNSecondHopForTokenAddress
//tokensToAvoidOnSecondHops
}
1 change: 1 addition & 0 deletions core/router.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package core

type SwapRoute struct {
route Routes
}

type IRouter interface {
Expand Down
4 changes: 4 additions & 0 deletions core/routes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package core

type Routes struct {
}
6 changes: 6 additions & 0 deletions core/trade.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package core

type Trade struct {
//v3Routes V3Routes
tradeType TradeType
}
9 changes: 0 additions & 9 deletions core/types.go

This file was deleted.

Loading
Loading