Skip to content

Commit

Permalink
issue: router/swap_router/core/entities/currency_amount.go의 제네릭 사용
Browse files Browse the repository at this point in the history
  • Loading branch information
tolelom committed Sep 9, 2024
1 parent 7f6730b commit 5b7d48e
Show file tree
Hide file tree
Showing 57 changed files with 744 additions and 261 deletions.
17 changes: 0 additions & 17 deletions core/constants.go

This file was deleted.

37 changes: 0 additions & 37 deletions core/currency/base_currency.go

This file was deleted.

6 changes: 0 additions & 6 deletions core/currency/currency.go

This file was deleted.

9 changes: 0 additions & 9 deletions core/currency/native_currency.go

This file was deleted.

25 changes: 0 additions & 25 deletions core/currency/token.go

This file was deleted.

12 changes: 0 additions & 12 deletions core/providers/v3_pool_accessor.go

This file was deleted.

34 changes: 0 additions & 34 deletions core/providers/v3_pool_provider.go

This file was deleted.

8 changes: 0 additions & 8 deletions core/quote.go

This file was deleted.

15 changes: 0 additions & 15 deletions core/router.go

This file was deleted.

17 changes: 0 additions & 17 deletions core/tokens/gnot.go

This file was deleted.

6 changes: 4 additions & 2 deletions database/database.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package database

import "context"

type Database interface {
GetPools() []PoolV0
GetTokens() []TokenV0
//query(sql string) T ????
//commit(string, any[][]) bool ??????
Query(ctx context.Context, sql string) (interface{}, error) // 제너릭 타입은 Go에서 다루기 복잡하므로 interface{} 사용
Commit(ctx context.Context, sql string, data [][]interface{}) (bool, error)
}
5 changes: 5 additions & 0 deletions database/gnoswap_db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package database

type GnoswapDB struct {
Database
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
module router

go 1.18
55 changes: 31 additions & 24 deletions core/alpha_router.go → swap_router/alpha_router.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package core
package swap_router

import (
"router/core/currency"
"router/core/math"
currency2 "router/swap_router/core/entities/currency"
"router/swap_router/core/entities/fractions/math"
"router/swap_router/providers"
"router/swap_router/quoter"
)

type AlphaRouter struct {
chainId ChainId
portionProvider IPortionProvider
chainId ChainId
subgraphProvider providers.IV3SubgraphProvider
poolProvider providers.IV3PoolProvider
onChainQuoteProvider providers.IOnChainQuoteProvider
tokenProvider providers.ITokenProvider
quoter quoter.V3Quoter
portionProvider IPortionProvider
}

func NewAlphaRouter(params AlphaRouterParams) *AlphaRouter {
Expand All @@ -16,8 +23,8 @@ func NewAlphaRouter(params AlphaRouterParams) *AlphaRouter {

// Todo: goroutine
func (a *AlphaRouter) route(
baseCurrency currency.Currency,
quoteCurrency currency.Currency, // 여기는 왜 quote를 쓰지
baseCurrency currency2.ICurrency,
quoteCurrency currency2.ICurrency, // 여기는 왜 quote를 쓰지
amount math.Fraction,
tradeType TradeType,
swapConfig SwapOptions,
Expand All @@ -33,18 +40,18 @@ func (a *AlphaRouter) route(

// 왠만하면 함수로 뺄 것
// TODO: 이해하기
//if tradeType == EXACT_OUTPUT {
// portionAmount, portionErr := a.portionProvider.GetPortionAmount(amount, tradeType, swapConfig)
// if portionErr == nil && portionAmount > 0 {
// // 정확한 교환의 경우 라우팅하기 전에 다음 사항을 확인해야 합니다.
// // 토큰 아웃 금액은 고정 부분을 차지하며, 최상의 스왑 경로 이후의 토큰 인 금액에는 해당 부분에 해당하는 토큰이 포함됩니다.
// // 즉, 풀의 LP 수수료 bps가 부분 bps(v3의 경우 0.01%/0.05%)보다 낮은 경우 풀은 파산할 수 있습니다.
// // 그 이유는 스왑퍼가 해당 부분을 담당하는 대신,
// // 대신 풀이 해당 부분을 담당합니다.
// // 아래 추가 내용은 이러한 상황을 방지합니다.
// amount.Add(portionAmount)
// }
//}
if tradeType == EXACT_OUTPUT {
portionAmount, portionErr := a.portionProvider.GetPortionAmount(amount, tradeType, swapConfig)
if portionErr == nil && portionAmount > 0 {
// 정확한 교환의 경우 라우팅하기 전에 다음 사항을 확인해야 합니다.
// 토큰 아웃 금액은 고정 부분을 차지하며, 최상의 스왑 경로 이후의 토큰 인 금액에는 해당 부분에 해당하는 토큰이 포함됩니다.
// 즉, 풀의 LP 수수료 bps가 부분 bps(v3의 경우 0.01%/0.05%)보다 낮은 경우 풀은 파산할 수 있습니다.
// 그 이유는 스왑퍼가 해당 부분을 담당하는 대신,
// 대신 풀이 해당 부분을 담당합니다.
// 아래 추가 내용은 이러한 상황을 방지합니다.
amount.Add(portionAmount)
}
}

// routing config merge다루는 부분 패스
// 이름 리팩토링
Expand All @@ -70,9 +77,9 @@ func (a *AlphaRouter) route(

func (a *AlphaRouter) determineCurrencyInOutFromTradeType(
tradeType TradeType,
baseCurrency currency.Currency,
quoteCurrency currency.Currency,
) (currency.Currency, currency.Currency) {
baseCurrency currency2.ICurrency,
quoteCurrency currency2.ICurrency,
) (currency2.ICurrency, currency2.ICurrency) {
if tradeType == EXACT_INPUT {
return baseCurrency, quoteCurrency
}
Expand All @@ -81,7 +88,7 @@ func (a *AlphaRouter) determineCurrencyInOutFromTradeType(

// todo: goroutine
// quoteToken Token을 getSwapRouteFromChain에 매개변수로 넘기면 계산 효율은 좋아진다(아주 미세하게), 하지만 quoteToken은 지금의 매개변수로 간단하게 알 수 있다.
func (a *AlphaRouter) getSwapRouteFromChain(tokenIn, tokenOut currency.Token, amount math.Fraction, tradeType TradeType, routerConfig AlphaRouterConfig) *BestSwapRoute {
func (a *AlphaRouter) getSwapRouteFromChain(tokenIn, tokenOut currency2.Token, amount math.Fraction, tradeType TradeType, routerConfig AlphaRouterConfig) *BestSwapRoute {
// 금액 분포, 즉 입력 금액의 일부를 생성합니다.
// 다양한 경로에 대한 입력 금액의 일부에 대한 견적을 받은 다음
// 결합하여 분할 경로를 생성합니다.
Expand Down Expand Up @@ -113,7 +120,7 @@ func (a *AlphaRouter) getAmountDistribution(amount math.Fraction, distributionPe
return quotes
}

func (a *AlphaRouter) buildTrade(currencyIn currency.Currency, currencyOut currency.Currency, tradeType TradeType, routes []V3RouteWithValidQuote) Trade {
func (a *AlphaRouter) buildTrade(currencyIn currency2.ICurrency, currencyOut currency2.ICurrency, tradeType TradeType, routes []V3RouteWithValidQuote) Trade {

return Trade{}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package swap_router

type AlphaRouterConfig struct {
v3ProtocolPoolSelection ProtocolPoolSelection
Expand All @@ -15,7 +15,9 @@ type AlphaRouterConfig struct {
* 55% of input => Route 2
* 40% of input => Route 3
*/
distributionPercent int
distributionPercent int

//
useCachedRoutes bool
writeToCachedRoutes bool
optimisticCachedRoutes bool
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package core
package swap_router

import "router/core/providers"
import "router/swap_router/providers"

type AlphaRouterParams struct {
// The chain id for this instance of the Alpha Router.
Expand Down
7 changes: 4 additions & 3 deletions core/best_swap_route.go → swap_router/best_swap_route.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package core
package swap_router

import "router/core/math"
import (
"router/swap_router/core/entities/fractions/math"
)

type BestSwapRoute struct {
quote math.Fraction
routeWithValidQuote []V3RouteWithValidQuote
}


// todo: goroutine
// 모든 경로에 대한 모든 금액에 대한 모든 견적이 주어지면 최상의 조합을 찾으십시오.
func getBestSwapRoute(amount math.Fraction, routesWithValidQuotes []V3RouteWithValidQuote, tradeType TradeType, routerConfig AlphaRouterConfig) *BestSwapRoute {
Expand Down
2 changes: 1 addition & 1 deletion core/chain_id.go → swap_router/chain_id.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package swap_router

type ChainId int

Expand Down
19 changes: 19 additions & 0 deletions swap_router/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package swap_router

type BigintIsh interface {
}

type TradeType int

const (
EXACT_INPUT = iota
EXACT_OUTPUT
)

var MIN_PRICE_X96 = 4295128740
var MAX_PRICE_X96 = 1461446703485210103287273052203988822378723970341

const MIN_TICK = -887272
const MAX_TICK = 887272

const DEFAULT_SWAP_FEE = 0.15
9 changes: 9 additions & 0 deletions swap_router/core/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package core

type Rounding int

const (
ROUND_DOWN Rounding = iota
ROUND_HALF_UP
ROUND_UP
)
Loading

0 comments on commit 5b7d48e

Please sign in to comment.