@@ -35,14 +35,14 @@ public class UpbitApiClient implements ExchangeApiClient {
3535 private final JwtApiUtil jwtApiUtil ;
3636
3737 @ Override
38- public MaxOrderInfoDTO getMaxOrderInfo (String phoneNumber , ExchangeType exchangeType , String coinType , String unitPrice ) {
38+ public MaxOrderInfoDTO getMaxOrderInfo (String phoneNumber , ExchangeType exchangeType , String coinType , String unitPrice , String orderType , String side ) {
3939 try {
4040 // coinType์ ์
๋นํธ ํ์์ผ๋ก ์ ๊ทํ (KRW-BTC ํ์์ผ๋ก ํต์ผ)
4141 String normalizedCoinType = normalizeCoinType (coinType );
4242
4343 String authorization = jwtApiUtil .createUpBitJwt (phoneNumber , null , null );
44- log .info ("์
๋นํธ ์ต๋ ์ฃผ๋ฌธ ์ ๋ณด ์กฐํ API ํธ์ถ ์์ - phoneNumber: {}, coinType: {} (์ ๊ทํ: {}), unitPrice: {}" ,
45- phoneNumber , coinType , normalizedCoinType , unitPrice );
44+ log .info ("์
๋นํธ ์ต๋ ์ฃผ๋ฌธ ์ ๋ณด ์กฐํ API ํธ์ถ ์์ - phoneNumber: {}, coinType: {} (์ ๊ทํ: {}), unitPrice: {}, orderType: {}, side: {} " ,
45+ phoneNumber , coinType , normalizedCoinType , unitPrice , orderType , side );
4646
4747 // Feign Client๊ฐ ์๋์ผ๋ก List<Account>๋ก ๋ณํํด์ค (ObjectMapper ๋ถํ์!)
4848 List <UpbitResDTO .Account > accounts = upbitFeignClient .getAccounts (authorization );
@@ -54,7 +54,7 @@ public MaxOrderInfoDTO getMaxOrderInfo(String phoneNumber, ExchangeType exchange
5454 account .currency (), account .balance (), account .locked (), account .available ());
5555 }
5656
57- return parseMaxOrderInfoResponse (accounts , normalizedCoinType , unitPrice );
57+ return parseMaxOrderInfoResponse (accounts , normalizedCoinType , unitPrice , orderType , side );
5858
5959 } catch (MemberException e ) {
6060 log .error ("์
๋นํธ API ํค๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค - phoneNumber: {}" , phoneNumber , e );
@@ -148,21 +148,53 @@ public MaxOrderInfoDTO getMaxOrderInfo(String phoneNumber, ExchangeType exchange
148148 }
149149 }
150150
151- private MaxOrderInfoDTO parseMaxOrderInfoResponse (List <UpbitResDTO .Account > accounts , String coinType , String unitPrice ) {
151+ private MaxOrderInfoDTO parseMaxOrderInfoResponse (List <UpbitResDTO .Account > accounts , String coinType , String unitPrice , String orderType , String side ) {
152152 try {
153- // coinType์ด KRW-BTC ํ์์ด๋ฉด, ๋งค์ ์ KRW ์์ก์ ์กฐํํด์ผ ํจ
154153 String currency ;
155- if (coinType .contains ("-" )) {
156- String [] parts = coinType .split ("-" );
157- // KRW-BTC ํ์์ด๋ฉด KRW ์์ก ์กฐํ (๋งค์ ๊ฐ๋ฅ ๊ธ์ก)
158- currency = parts [0 ]; // KRW
154+ String targetCurrency ; // ์กฐํํ ํํ (KRW ๋๋ ์ฝ์ธ)
155+
156+ // ์์ฅ๊ฐ ์ฃผ๋ฌธ์ธ ๊ฒฝ์ฐ
157+ boolean isMarketOrder = "price" .equals (orderType ) || "market" .equals (orderType );
158+
159+ if (isMarketOrder ) {
160+ // side ํ๋ผ๋ฏธํฐ๋ฅผ ๊ธฐ์ค์ผ๋ก ๋งค์/๋งค๋ ํ๋จ
161+ if ("bid" .equals (side )) {
162+ // ๋งค์: KRW ์์ก ์กฐํ
163+ targetCurrency = "KRW" ;
164+ currency = "KRW" ;
165+ log .info ("์
๋นํธ ์์ฅ๊ฐ ๋งค์ - {}๋ฅผ ๋งค์ํ๊ธฐ ์ํด KRW ์์ก ์กฐํ" , coinType );
166+ } else if ("ask" .equals (side )) {
167+ // ๋งค๋: ์ฝ์ธ ์์ก ์กฐํ
168+ if (coinType .contains ("-" )) {
169+ String [] parts = coinType .split ("-" );
170+ targetCurrency = parts [1 ]; // KRW-BTC -> BTC
171+ currency = parts [1 ];
172+ } else {
173+ targetCurrency = coinType ;
174+ currency = coinType ;
175+ }
176+ log .info ("์
๋นํธ ์์ฅ๊ฐ ๋งค๋ - {} ์์ก ์กฐํ" , targetCurrency );
177+ } else {
178+ // side๊ฐ ์๊ฑฐ๋ ์๋ชป๋ ๊ฒฝ์ฐ ๊ธฐ๋ณธ๊ฐ (๋งค์)
179+ targetCurrency = "KRW" ;
180+ currency = "KRW" ;
181+ log .warn ("์
๋นํธ ์์ฅ๊ฐ ์ฃผ๋ฌธ - side ํ๋ผ๋ฏธํฐ๊ฐ ์๊ฑฐ๋ ์๋ชป๋จ ({}), ๊ธฐ๋ณธ๊ฐ์ผ๋ก ๋งค์ ์ฒ๋ฆฌ" , side );
182+ }
159183 } else {
160- currency = coinType ; // BTC ๋ฑ ๋จ์ผ ์ฝ์ธ
184+ // ์ง์ ๊ฐ ์ฃผ๋ฌธ
185+ if (coinType .contains ("-" )) {
186+ String [] parts = coinType .split ("-" );
187+ currency = parts [0 ]; // KRW
188+ targetCurrency = parts [0 ];
189+ } else {
190+ currency = coinType ;
191+ targetCurrency = coinType ;
192+ }
161193 }
162194
163195 String balance = "0" ;
164196
165- // ๋จผ์ ํด๋น currency๋ก ๊ณ์ข ์ฐพ๊ธฐ
197+ // ํด๋น currency๋ก ๊ณ์ข ์ฐพ๊ธฐ
166198 for (UpbitResDTO .Account account : accounts ) {
167199 if (currency .equals (account .currency ())) {
168200 // available์ด ์์ผ๋ฉด available ์ฌ์ฉ (๋งค์ ๊ฐ๋ฅ ๊ธ์ก)
@@ -189,28 +221,76 @@ private MaxOrderInfoDTO parseMaxOrderInfoResponse(List<UpbitResDTO.Account> acco
189221 }
190222 }
191223
192- // unitPrice๊ฐ ์์ผ๋ฉด ์ต๋ ์ฃผ๋ฌธ ์๋ ๊ณ์ฐ (balance / unitPrice)
193- // ์์์ ์ ์ฌํ์ฌ ์ ์๋ก ๋ฐํ (0.8๊ฐ โ 0๊ฐ, 1.2๊ฐ โ 1๊ฐ)
194224 String maxQuantity = null ;
195- if (unitPrice != null && !unitPrice .isEmpty ()) {
196- try {
197- BigDecimal balanceDecimal = new BigDecimal (balance );
198- BigDecimal unitPriceDecimal = new BigDecimal (unitPrice );
199-
200- if (unitPriceDecimal .compareTo (BigDecimal .ZERO ) > 0 ) {
201- BigDecimal quantity = balanceDecimal .divide (unitPriceDecimal , 8 , RoundingMode .DOWN );
202- // ์์์ ์ ์ฌํ์ฌ ์ ์๋ก ๋ณํ
203- maxQuantity = quantity .setScale (0 , RoundingMode .DOWN ).toPlainString ();
204- log .info ("์
๋นํธ ์ต๋ ์ฃผ๋ฌธ ์๋ ๊ณ์ฐ - balance: {}, unitPrice: {}, maxQuantity: {} (์ ์)" , balance , unitPrice , maxQuantity );
205- } else {
206- log .warn ("๋จ์ ๊ฐ๊ฒฉ์ด 0 ์ดํ์
๋๋ค. ์ต๋ ์ฃผ๋ฌธ ์๋์ ๊ณ์ฐํ ์ ์์ต๋๋ค." );
225+
226+ // ์์ฅ๊ฐ ์ฃผ๋ฌธ ์ฒ๋ฆฌ
227+ if (isMarketOrder ) {
228+ if ("bid" .equals (side )) {
229+ // ์์ฅ๊ฐ ๋งค์: ํ์ฌ๊ฐ ์กฐํํ์ฌ ๋๋ต์ ์ธ ์๋ ๊ณ์ฐ
230+ try {
231+ List <UpbitResDTO .Ticker > tickers = upbitFeignClient .getTicker (coinType );
232+
233+ if (tickers != null && !tickers .isEmpty ()) {
234+ UpbitResDTO .Ticker ticker = tickers .get (0 );
235+ if (ticker .trade_price () != null && ticker .trade_price () > 0 ) {
236+ BigDecimal balanceDecimal = new BigDecimal (balance );
237+ BigDecimal currentPrice = BigDecimal .valueOf (ticker .trade_price ());
238+ BigDecimal quantity = balanceDecimal .divide (currentPrice , 8 , RoundingMode .DOWN );
239+ // ์์์ ์ ์ฌํ์ฌ ์ ์๋ก ๋ณํ
240+ maxQuantity = quantity .setScale (0 , RoundingMode .DOWN ).toPlainString ();
241+ log .info ("์
๋นํธ ์์ฅ๊ฐ ๋งค์ - KRW ์์ก: {}, ํ์ฌ๊ฐ: {}, ์ต๋ ๋งค์ ๊ฐ๋ฅ ์๋: {} (์ ์)" ,
242+ balance , currentPrice , maxQuantity );
243+ } else {
244+ log .warn ("์
๋นํธ ํ์ฌ๊ฐ ์กฐํ ์คํจ ๋๋ ๊ฐ๊ฒฉ์ด 0 ์ดํ - market: {}" , coinType );
245+ maxQuantity = null ;
246+ }
247+ } else {
248+ log .warn ("์
๋นํธ ํ์ฌ๊ฐ ์กฐํ ์คํจ - ์๋ต์ด ๋น์ด์์: {}" , coinType );
249+ maxQuantity = null ;
250+ }
251+ } catch (Exception e ) {
252+ log .warn ("์
๋นํธ ํ์ฌ๊ฐ ์กฐํ ์คํจ - ์์ฅ๊ฐ ๋งค์ ์๋ ๊ณ์ฐ ๋ถ๊ฐ: {}" , e .getMessage ());
253+ maxQuantity = null ;
254+ }
255+ } else if ("ask" .equals (side )) {
256+ // ์์ฅ๊ฐ ๋งค๋: ์ฝ์ธ ์์ก์ maxQuantity๋ก ๋ฐํ (์ต๋ ๋งค๋ ๊ฐ๋ฅ ์๋)
257+ // ์์์ ์ ์ฌํ์ฌ ์ ์๋ก ๋ณํ
258+ try {
259+ BigDecimal balanceDecimal = new BigDecimal (balance );
260+ maxQuantity = balanceDecimal .setScale (0 , RoundingMode .DOWN ).toPlainString ();
261+ log .info ("์
๋นํธ ์์ฅ๊ฐ ๋งค๋ - ์ฝ์ธ ์์ก: {}, ์ต๋ ๋งค๋ ๊ฐ๋ฅ ์๋: {} (์ ์)" , balance , maxQuantity );
262+ } catch (NumberFormatException e ) {
263+ log .warn ("์
๋นํธ ์์ฅ๊ฐ ๋งค๋ - ์์ก ํ์ฑ ์คํจ, ์๋ณธ ๊ฐ ์ฌ์ฉ: {}" , balance );
264+ maxQuantity = balance ;
265+ }
266+ } else {
267+ log .warn ("์
๋นํธ ์์ฅ๊ฐ ์ฃผ๋ฌธ - side ํ๋ผ๋ฏธํฐ๊ฐ ์๊ฑฐ๋ ์๋ชป๋จ ({}), maxQuantity: null" , side );
268+ maxQuantity = null ;
269+ }
270+ } else {
271+ // ์ง์ ๊ฐ ์ฃผ๋ฌธ
272+ if (unitPrice != null && !unitPrice .isEmpty ()) {
273+ try {
274+ BigDecimal balanceDecimal = new BigDecimal (balance );
275+ BigDecimal unitPriceDecimal = new BigDecimal (unitPrice );
276+
277+ if (unitPriceDecimal .compareTo (BigDecimal .ZERO ) > 0 ) {
278+ BigDecimal quantity = balanceDecimal .divide (unitPriceDecimal , 8 , RoundingMode .DOWN );
279+ // ์์์ ์ ์ฌํ์ฌ ์ ์๋ก ๋ณํ
280+ maxQuantity = quantity .setScale (0 , RoundingMode .DOWN ).toPlainString ();
281+ log .info ("์
๋นํธ ์ต๋ ์ฃผ๋ฌธ ์๋ ๊ณ์ฐ - balance: {}, unitPrice: {}, maxQuantity: {} (์ ์)" ,
282+ balance , unitPrice , maxQuantity );
283+ } else {
284+ log .warn ("๋จ์ ๊ฐ๊ฒฉ์ด 0 ์ดํ์
๋๋ค. ์ต๋ ์ฃผ๋ฌธ ์๋์ ๊ณ์ฐํ ์ ์์ต๋๋ค." );
285+ }
286+ } catch (NumberFormatException e ) {
287+ log .warn ("๋จ์ ๊ฐ๊ฒฉ ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค. ์ต๋ ์ฃผ๋ฌธ ์๋์ ๊ณ์ฐํ ์ ์์ต๋๋ค. unitPrice: {}" , unitPrice );
207288 }
208- } catch (NumberFormatException e ) {
209- log .warn ("๋จ์ ๊ฐ๊ฒฉ ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค. ์ต๋ ์ฃผ๋ฌธ ์๋์ ๊ณ์ฐํ ์ ์์ต๋๋ค. unitPrice: {}" , unitPrice );
210289 }
211290 }
212291
213- log .info ("์
๋นํธ ์ต๋ ์ฃผ๋ฌธ ์ ๋ณด ์กฐํ ์๋ฃ - coinType: {}, balance: {}, maxQuantity: {}" , coinType , balance , maxQuantity );
292+ log .info ("์
๋นํธ ์ต๋ ์ฃผ๋ฌธ ์ ๋ณด ์กฐํ ์๋ฃ - coinType: {}, balance: {}, maxQuantity: {}, orderType: {}" ,
293+ coinType , balance , maxQuantity , orderType );
214294
215295 return new MaxOrderInfoDTO (balance , maxQuantity );
216296
0 commit comments