diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..67e3a6e --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,21 @@ +# This workflow will build a golang project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go +name: Go +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Set up Go + uses: actions/setup-go@v4 + with: + go-version: '1.22.X' + - name: Build + run: go build -v ./... + - name: Test + run: go test -v ./... diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..85b25be --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +# If you prefer the allow list template instead of the deny list, see community template: +# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore +# +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ + +# Go workspace file +go.work +go.work.sum + +# env file +.env + +.idea + +.DS_Store diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b3c8eb5 --- /dev/null +++ b/go.mod @@ -0,0 +1 @@ +module router diff --git a/main.go b/main.go new file mode 100644 index 0000000..725c460 --- /dev/null +++ b/main.go @@ -0,0 +1,11 @@ +package main + +import ( + "fmt" +) + +func main() { + s := "gopher" + fmt.Println("Hello and welcome, %s!", s) + +} diff --git a/router/alpha-router-params.go b/router/alpha-router-params.go new file mode 100644 index 0000000..6c258dd --- /dev/null +++ b/router/alpha-router-params.go @@ -0,0 +1,4 @@ +package router + +type AlphaRouterParams struct { +} diff --git a/router/alpha-router.go b/router/alpha-router.go new file mode 100644 index 0000000..a388b5c --- /dev/null +++ b/router/alpha-router.go @@ -0,0 +1,47 @@ +package router + +import ( + "router/router/core" + "router/router/core/entities" + "router/router/core/entities/fractions" +) + +type AlphaRouter struct { +} + +func NewAlphaRouter(params AlphaRouterParams) *AlphaRouter { + return &AlphaRouter{} +} + +// TODO: 원본 코드에서는 async 함수 +// 라우트 한 결과는 SwapRoute +func (a AlphaRouter) route( + amount fractions.CurrencyAmount, + quoteCurrency entities.Currency, + tradeType core.TradeType, +) SwapRoute { + originalAmount := amount + + currencyIn, currencyOut := a.determineCurrencyInOutFromTradeType(tradeType, amount, quoteCurrency) + + // currencyIn, currencyOut은 Currency 타입이고 + // Currency 타입은 NativeCurrency이거나 Token 타입이다. + // 아래에서 Token 타입이길 원하는 듯하다. + tokenIn := currencyIn + tokenOut := currencyOut + + swapRoute := SwapRoute{} + return swapRoute +} + +func (a AlphaRouter) determineCurrencyInOutFromTradeType( + tradeType core.TradeType, + amount fractions.CurrencyAmount, + quoteCurrency entities.Currency, +) (entities.Currency, entities.Currency) { + if tradeType == core.EXACT_INPUT { + return amount.Currency, quoteCurrency + } else { + return quoteCurrency, amount.Currency + } +} diff --git a/router/core/constants.go b/router/core/constants.go new file mode 100644 index 0000000..efe32d3 --- /dev/null +++ b/router/core/constants.go @@ -0,0 +1,8 @@ +package core + +type TradeType int + +const ( + EXACT_INPUT = iota + EXACT_OUTPUT +) diff --git a/router/core/entities/currency.go b/router/core/entities/currency.go new file mode 100644 index 0000000..30a6b32 --- /dev/null +++ b/router/core/entities/currency.go @@ -0,0 +1,8 @@ +package entities + +// 원문에서는 Currency는 Token | NativeCurrency 이고 +// Token과 NativeCurrency 모두 BaseCurrency를 상속받는다. +// 하지만 여기서의 Currency는 NativeCurrency와 Token에 포함된다. +type Currency interface { + wrapped() Token +} diff --git a/router/core/entities/fractions/currencyAmount.go b/router/core/entities/fractions/currencyAmount.go new file mode 100644 index 0000000..2fafe19 --- /dev/null +++ b/router/core/entities/fractions/currencyAmount.go @@ -0,0 +1,7 @@ +package fractions + +import "router/router/core/entities" + +type CurrencyAmount struct { + Currency entities.Currency +} diff --git a/router/core/entities/token.go b/router/core/entities/token.go new file mode 100644 index 0000000..cc76145 --- /dev/null +++ b/router/core/entities/token.go @@ -0,0 +1,5 @@ +package entities + +type Token struct { + Currency +} diff --git a/router/router.go b/router/router.go new file mode 100644 index 0000000..70026b2 --- /dev/null +++ b/router/router.go @@ -0,0 +1,4 @@ +package router + +type SwapRoute struct { +}