Skip to content

Commit

Permalink
PoC 제작 완료 및 테스트 작성 완료(테스트 케이스는 더 추가하면 좋음)
Browse files Browse the repository at this point in the history
  • Loading branch information
tolelom committed Sep 4, 2024
1 parent ffd96ee commit c24ff18
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 33 deletions.
45 changes: 26 additions & 19 deletions core/alpha_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,49 @@ func NewAlphaRouter(params AlphaRouterParams) *AlphaRouter {
func (a *AlphaRouter) route(
baseCurrency currency.Currency,
quoteCurrency currency.Currency,
amount float64,
amount float64, // todo: float64 -> fraction
tradeType TradeType,
swapConfig SwapOptions,
routerConfig AlphaRouterConfig,
) SwapRoute {
originalAmount := amount
//originalAmount := amount // for save

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

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

// 왠만하면 함수로 뺄 것
// 내용 이해 필요
if tradeType == EXACT_OUTPUT {
portionAmount, portionErr := a.portionProvider.GetPortionAmount(
amount,
tradeType,
swapConfig,
)
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 다루는 부분 패스
routingConfig := AlphaRouterConfig{}
// routing config merge다루는 부분 패스
//routerConfig = setRouterConfig(routingConfig, chainId)

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

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

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

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

swapRoute := a.buildSwapRoute()
return swapRoute
Expand All @@ -73,7 +76,7 @@ func (a *AlphaRouter) determineCurrencyInOutFromTradeType(

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

return &BestSwapRoute{}
}
Expand All @@ -91,3 +94,7 @@ func (a *AlphaRouter) buildTrade(currencyIn currency.Currency, currencyOut curre
func (a *AlphaRouter) buildSwapRoute() SwapRoute {
return SwapRoute{}
}

func (a *AlphaRouter) setRouterConfig(routerConfig AlphaRouterConfig, chainId int) AlphaRouterConfig {
return AlphaRouterConfig{}
}
2 changes: 1 addition & 1 deletion core/trade.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package core

type Trade struct {
v3Routes V3Routes
//v3Routes V3Routes
tradeType TradeType
}
16 changes: 12 additions & 4 deletions poc/my_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"math"
)

// MyRouter
// router PoC
type MyRouter struct {
network map[string]*Pool
adj map[string][]string
Expand Down Expand Up @@ -35,11 +37,14 @@ func (m *MyRouter) Route(request SwapRequest) ([]SwapResult, error) {
//return m.findRouteV2(startTokenSymbol, endTokenSymbol, AmountIn, 1)
}

// findRouteV1
// 두 토큰을 direct swap한다
func (m *MyRouter) findRouteV1(request SwapRequest) ([]SwapResult, error) {
return m.swap(request.FromTokenSymbol, request.ToTokenSymbol, request.AmountIn)
}

// 경로가 maxLength 이하의 길이인 경로를 탐색해 route를 구한다.
// findRouteV2
// 경로가 maxLength 이하의 길이인 경로를 탐색해 route를 구한다
func (m *MyRouter) findRouteV2(request SwapRequest, maxLength int, routes []SwapResult) ([]SwapResult, error) {
startTokenSymbol, beforeTokenSymbol, amountIn := m.setSymbolAndAmountIn(request, routes)
if startTokenSymbol == request.ToTokenSymbol {
Expand All @@ -60,8 +65,10 @@ func (m *MyRouter) findRouteV2(request SwapRequest, maxLength int, routes []Swap
continue
}

workablePath, _ := m.findRouteV2(request, maxLength, append(routes, route...))
// TODO: 여기에 스왑한 내용 복구가 필요합니다!
workablePath, findRouteErr := m.findRouteV2(request, maxLength, append(routes, route...))
if findRouteErr != nil {
continue
}

if len(workablePath) != 0 && (bestPath == nil || (bestPath[len(bestPath)-1].AmountOut < workablePath[len(workablePath)-1].AmountOut)) {
bestPath = workablePath
Expand All @@ -71,6 +78,7 @@ func (m *MyRouter) findRouteV2(request SwapRequest, maxLength int, routes []Swap
return bestPath, nil
}

// setSymbolAndAmountIn
func (m *MyRouter) setSymbolAndAmountIn(request SwapRequest, routes []SwapResult) (string, string, float64) {
if routes == nil { // 처음 함수가 호출된 거라면
return request.FromTokenSymbol, "", request.AmountIn
Expand All @@ -86,7 +94,6 @@ func (m *MyRouter) swap(fromTokenSymbol string, toTokenSymbol string, amountIn f
poolName := fromTokenSymbol + ":" + toTokenSymbol

if pool, ok := m.network[poolName]; ok {
//fmt.Printf("pool found: %v\n", pool) // for debug
reserveFromToken, reserveToToken := m.getReserveOfTokenFromPool(fromTokenSymbol, toTokenSymbol, *pool)
amountOut := m.calculateAmountOfToToken(reserveFromToken, reserveToToken, amountIn, *pool)

Expand All @@ -104,6 +111,7 @@ func (m *MyRouter) swap(fromTokenSymbol string, toTokenSymbol string, amountIn f
return nil, fmt.Errorf("pool %s not found", poolName)
}

// saveSwap
func (m *MyRouter) saveSwap(fromTokenSymbol string, amountIn, amountOut float64, pool *Pool) {
if pool.TokenA.Symbol == fromTokenSymbol {
pool.ReserveA += amountIn
Expand Down
24 changes: 19 additions & 5 deletions poc/my_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ func TestMyRouterV2(t *testing.T) {
}

tests := []struct {
edges []*Pool
requests []SwapRequest
results []SwapResult
edges []*Pool
requests []SwapRequest
results []SwapResult
maxSearchLength int
}{
{
[]*Pool{
Expand All @@ -76,8 +77,21 @@ func TestMyRouterV2(t *testing.T) {
[]SwapRequest{
{"a", "c", 2000}},
[]SwapResult{
{"a", "c", 2000.0, 571.4285714285},
{"a", "c", 2000, 571.4285714285},
},
2,
},
{
[]*Pool{
{"a:b", tokens["a"], tokens["b"], 4000, 1000},
{"a:c", tokens["a"], tokens["c"], 2000, 1000},
{"b:c", tokens["b"], tokens["c"], 2000, 4000}},
[]SwapRequest{
{"a", "c", 2000}},
[]SwapResult{
{"a", "c", 2000, 500},
},
1,
},
}

Expand All @@ -86,7 +100,7 @@ func TestMyRouterV2(t *testing.T) {
router := NewMyRouter(test.edges)

for i, request := range test.requests {
result, err := router.findRouteV2(request, 10, nil)
result, err := router.findRouteV2(request, test.maxSearchLength, nil)
if err != nil {
t.Fatalf("Router: can't find path: %v:%v", request.FromTokenSymbol, request.ToTokenSymbol)
}
Expand Down
4 changes: 1 addition & 3 deletions poc/pool.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package poc

// Pool
// kind of edge
type Pool struct {
Address string

TokenA Token // 효율을 위해서는 포인터로 가져오는게 좋을만 하다.
TokenA Token // Todo: 효율을 위해서는 포인터로 가져오는게 좋을만 하다.
TokenB Token

ReserveA float64
Expand Down
2 changes: 1 addition & 1 deletion poc/swap_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ type SwapRequest struct {
ToTokenSymbol string
AmountIn float64
//MinAmountOut int
//UserAddress string // option
//UserAddress string // optional
}

0 comments on commit c24ff18

Please sign in to comment.