2019102152 김다빈

Modify transaction_fuction and User Schema & Add nodemon dependencies

...@@ -13,13 +13,14 @@ const request = require('request-promise-native'); ...@@ -13,13 +13,14 @@ const request = require('request-promise-native');
13 // const request = require('request') 13 // const request = require('request')
14 const { v4 } = require("uuid") 14 const { v4 } = require("uuid")
15 const sign = require('jsonwebtoken').sign 15 const sign = require('jsonwebtoken').sign
16 -var krw_balance=60000; 16 +var krw_balance = 60000;
17 -var divided_money=krw_balance/10; 17 +var divided_money = krw_balance / 10;
18 18
19 var sort_info = new Array(); 19 var sort_info = new Array();
20 const mongoose = require('mongoose'); 20 const mongoose = require('mongoose');
21 const config = require('./config/key'); 21 const config = require('./config/key');
22 const { json } = require('express'); 22 const { json } = require('express');
23 +const e = require('express');
23 24
24 const connect = mongoose.connect(config.mongoURI, { 25 const connect = mongoose.connect(config.mongoURI, {
25 useNewUrlParser: true, useUnifiedTopology: true 26 useNewUrlParser: true, useUnifiedTopology: true
...@@ -34,7 +35,7 @@ const access_key = process.env.access_key; ...@@ -34,7 +35,7 @@ const access_key = process.env.access_key;
34 const secret_key = process.env.secret_key; 35 const secret_key = process.env.secret_key;
35 const server_url = "https://api.upbit.com" 36 const server_url = "https://api.upbit.com"
36 37
37 -function get_asset() { 38 +function get_asset(market) {
38 const payload = { 39 const payload = {
39 access_key: access_key, 40 access_key: access_key,
40 nonce: v4(), 41 nonce: v4(),
...@@ -45,21 +46,16 @@ function get_asset() { ...@@ -45,21 +46,16 @@ function get_asset() {
45 url: server_url + "/v1/accounts", 46 url: server_url + "/v1/accounts",
46 headers: { Authorization: `Bearer ${token}` }, 47 headers: { Authorization: `Bearer ${token}` },
47 } 48 }
48 - return new Promise(resolve => { 49 + const result = (market) => new Promise((resolve) => {
49 request(options, function (err, res, body) { 50 request(options, function (err, res, body) {
50 if (err) throw new Error(err) 51 if (err) throw new Error(err)
51 - // test=res.json(); 52 + var empty = new Object();
52 data = JSON.parse(body); 53 data = JSON.parse(body);
53 - // console.log(data[0].currency) 54 + resolve(data);
54 - data.filter(function (item) {
55 - if (item.currency == "PLA") {
56 - resolve(item);
57 - }
58 - })
59 }) 55 })
60 }) 56 })
57 + return result(market);
61 } 58 }
62 -
63 async function get_marketName() { 59 async function get_marketName() {
64 var data = new Array(); 60 var data = new Array();
65 //전체 암호화폐 리스트 불러오기 61 //전체 암호화폐 리스트 불러오기
...@@ -74,6 +70,13 @@ async function get_marketName() { ...@@ -74,6 +70,13 @@ async function get_marketName() {
74 return data; 70 return data;
75 } 71 }
76 async function transaction_coin(coin_name, side, volume, price, ord_type) { 72 async function transaction_coin(coin_name, side, volume, price, ord_type) {
73 + var volume=volume;
74 + if(side=="ask"){
75 + await User.findOne({uid:1}).then((result)=>{
76 + console.log(result.volume);
77 + volume=String(result.volume);
78 + }).catch(err=>{console.log(err.error)});
79 + }
77 const body = { 80 const body = {
78 market: coin_name, 81 market: coin_name,
79 side: side, 82 side: side,
...@@ -84,31 +87,29 @@ async function transaction_coin(coin_name, side, volume, price, ord_type) { ...@@ -84,31 +87,29 @@ async function transaction_coin(coin_name, side, volume, price, ord_type) {
84 //시장가 매수인 경우 price를 얼마치 살건지 입력 87 //시장가 매수인 경우 price를 얼마치 살건지 입력
85 //시장가 매도인경우 volume에 몇개를 팔건지 입력 88 //시장가 매도인경우 volume에 몇개를 팔건지 입력
86 const query = queryEncode(body) 89 const query = queryEncode(body)
87 -
88 const hash = crypto.createHash('sha512') 90 const hash = crypto.createHash('sha512')
89 const queryHash = hash.update(query, 'utf-8').digest('hex') 91 const queryHash = hash.update(query, 'utf-8').digest('hex')
90 -
91 const payload = { 92 const payload = {
92 access_key: access_key, 93 access_key: access_key,
93 nonce: v4(), 94 nonce: v4(),
94 query_hash: queryHash, 95 query_hash: queryHash,
95 query_hash_alg: 'SHA512', 96 query_hash_alg: 'SHA512',
96 } 97 }
97 -
98 const token = sign(payload, secret_key) 98 const token = sign(payload, secret_key)
99 -
100 const options = { 99 const options = {
101 method: "POST", 100 method: "POST",
102 url: server_url + "/v1/orders", 101 url: server_url + "/v1/orders",
103 headers: { Authorization: `Bearer ${token}` }, 102 headers: { Authorization: `Bearer ${token}` },
104 json: body 103 json: body
105 } 104 }
106 - await request(options).then(async (result)=>{ 105 + await request(options).then(async (result) => {
107 - console.log(result.body); 106 + const asset=(delay)=>new Promise((resolve)=>{
108 - const user = new User({ krw_balance: 100, market: "KRW-BTC", count: 1, avg_price: 100 }); 107 + setTimeout(async ()=>{
109 - await user.save().then(() => { isuser = true }) 108 + resolve(await get_asset(result.market));
110 - },).catch((err)=>{console.log(err.error)}) 109 + },delay)
111 - 110 + })
111 + var my_asset=await asset(1000);
112 + }).catch((err) => { console.log(err.error) })
112 } 113 }
113 async function get_marketInfo() { 114 async function get_marketInfo() {
114 //각 암호화폐 정보 조회 115 //각 암호화폐 정보 조회
...@@ -191,7 +192,7 @@ async function check_coin(t1) { ...@@ -191,7 +192,7 @@ async function check_coin(t1) {
191 } else { 192 } else {
192 console.log("***" + result.korean_name + "은(는)" + result.count * 5 + "분 동안 하락중"); 193 console.log("***" + result.korean_name + "은(는)" + result.count * 5 + "분 동안 하락중");
193 if (result.count >= 3) { 194 if (result.count >= 3) {
194 - // transaction_coin(result.name,"bid",null,divided_money,"price"); 195 + transaction_coin(result.name, "bid", null, divided_money, "price");
195 } 196 }
196 } 197 }
197 }) 198 })
...@@ -201,17 +202,16 @@ async function check_coin(t1) { ...@@ -201,17 +202,16 @@ async function check_coin(t1) {
201 //특정 조건... 202 //특정 조건...
202 const user = new User({ krw_balance: 100, market: "KRW-BTC", count: 1, avg_price: 100 }); 203 const user = new User({ krw_balance: 100, market: "KRW-BTC", count: 1, avg_price: 100 });
203 await user.save().then(() => { isuser = true }) 204 await user.save().then(() => { isuser = true })
204 - // transaction_coin(result.name,"ask","몇개를 팔건지",null,"market");
205 console.log(result.korean_name + "은(는)" + result.count * 5 + "분 동안 상승 혹은 정체중"); 205 console.log(result.korean_name + "은(는)" + result.count * 5 + "분 동안 상승 혹은 정체중");
206 }) 206 })
207 } 207 }
208 }) 208 })
209 - }else { 209 + } else {
210 console.log("메롱"); 210 console.log("메롱");
211 } 211 }
212 } 212 }
213 } else { 213 } else {
214 - console.log(result); 214 + console.log(user_data);
215 } 215 }
216 }).catch((err) => { 216 }).catch((err) => {
217 console.log(err); 217 console.log(err);
...@@ -256,9 +256,8 @@ app.listen(5000, async () => { ...@@ -256,9 +256,8 @@ app.listen(5000, async () => {
256 console.log('server start') 256 console.log('server start')
257 //coin 이름,가격,거래대금 저장 , DB 최신화 1시간마다 반복 257 //coin 이름,가격,거래대금 저장 , DB 최신화 1시간마다 반복
258 //5분마다 현재 가격 가져와서 db랑 비교후 매수 매도 기준잡기 258 //5분마다 현재 가격 가져와서 db랑 비교후 매수 매도 기준잡기
259 - // var t1 = new Array(); 259 + var t1 = new Array();
260 - // test_data = await (latest_repeat(t1)); 260 + test_data = await (latest_repeat(t1));
261 //계좌 정보 db 최신화 261 //계좌 정보 db 최신화
262 // console.log(await get_asset()); 262 // console.log(await get_asset());
263 - await transaction_coin("KRW-BTC","bid",null,divided_money,"price");
264 }) 263 })
...\ No newline at end of file ...\ No newline at end of file
......
1 const mongoose=require('mongoose'); 1 const mongoose=require('mongoose');
2 2
3 const userSchema=mongoose.Schema({ 3 const userSchema=mongoose.Schema({
4 + uid:{
5 + type:Number
6 + },
4 krw_balance:{ 7 krw_balance:{
5 type:Number, 8 type:Number,
6 }, 9 },
...@@ -12,6 +15,9 @@ const userSchema=mongoose.Schema({ ...@@ -12,6 +15,9 @@ const userSchema=mongoose.Schema({
12 }, 15 },
13 avg_buy_price:{ 16 avg_buy_price:{
14 type:Number 17 type:Number
18 + },
19 + volume:{
20 + type:Number
15 } 21 }
16 }) 22 })
17 23
......
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
23 "uuid": "^8.3.2" 23 "uuid": "^8.3.2"
24 }, 24 },
25 "devDependencies": { 25 "devDependencies": {
26 - "nodemon": "^2.0.14" 26 + "nodemon": "^2.0.15"
27 } 27 }
28 }, 28 },
29 "node_modules/@sindresorhus/is": { 29 "node_modules/@sindresorhus/is": {
...@@ -1726,21 +1726,21 @@ ...@@ -1726,21 +1726,21 @@
1726 "integrity": "sha512-0bA08/2RKWxw6pMkOVd3KP+0F5+ifQLMMTDxrCgxlgkoU1N8DhCbCSAYEqpgaVYM2smvbVVewiXjW+8AyoLfxQ==" 1726 "integrity": "sha512-0bA08/2RKWxw6pMkOVd3KP+0F5+ifQLMMTDxrCgxlgkoU1N8DhCbCSAYEqpgaVYM2smvbVVewiXjW+8AyoLfxQ=="
1727 }, 1727 },
1728 "node_modules/nodemon": { 1728 "node_modules/nodemon": {
1729 - "version": "2.0.14", 1729 + "version": "2.0.15",
1730 - "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-2.0.14.tgz", 1730 + "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-2.0.15.tgz",
1731 - "integrity": "sha512-frcpDx+PviKEQRSYzwhckuO2zoHcBYLHI754RE9z5h1RGtrngerc04mLpQQCPWBkH/2ObrX7We9YiwVSYZpFJQ==", 1731 + "integrity": "sha512-gdHMNx47Gw7b3kWxJV64NI+Q5nfl0y5DgDbiVtShiwa7Z0IZ07Ll4RLFo6AjrhzMtoEZn5PDE3/c2AbVsiCkpA==",
1732 "dev": true, 1732 "dev": true,
1733 "hasInstallScript": true, 1733 "hasInstallScript": true,
1734 "dependencies": { 1734 "dependencies": {
1735 - "chokidar": "^3.2.2", 1735 + "chokidar": "^3.5.2",
1736 - "debug": "^3.2.6", 1736 + "debug": "^3.2.7",
1737 "ignore-by-default": "^1.0.1", 1737 "ignore-by-default": "^1.0.1",
1738 "minimatch": "^3.0.4", 1738 "minimatch": "^3.0.4",
1739 - "pstree.remy": "^1.1.7", 1739 + "pstree.remy": "^1.1.8",
1740 "semver": "^5.7.1", 1740 "semver": "^5.7.1",
1741 "supports-color": "^5.5.0", 1741 "supports-color": "^5.5.0",
1742 "touch": "^3.1.0", 1742 "touch": "^3.1.0",
1743 - "undefsafe": "^2.0.3", 1743 + "undefsafe": "^2.0.5",
1744 "update-notifier": "^5.1.0" 1744 "update-notifier": "^5.1.0"
1745 }, 1745 },
1746 "bin": { 1746 "bin": {
...@@ -4041,20 +4041,20 @@ ...@@ -4041,20 +4041,20 @@
4041 "integrity": "sha512-0bA08/2RKWxw6pMkOVd3KP+0F5+ifQLMMTDxrCgxlgkoU1N8DhCbCSAYEqpgaVYM2smvbVVewiXjW+8AyoLfxQ==" 4041 "integrity": "sha512-0bA08/2RKWxw6pMkOVd3KP+0F5+ifQLMMTDxrCgxlgkoU1N8DhCbCSAYEqpgaVYM2smvbVVewiXjW+8AyoLfxQ=="
4042 }, 4042 },
4043 "nodemon": { 4043 "nodemon": {
4044 - "version": "2.0.14", 4044 + "version": "2.0.15",
4045 - "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-2.0.14.tgz", 4045 + "resolved": "https://registry.npmjs.org/nodemon/-/nodemon-2.0.15.tgz",
4046 - "integrity": "sha512-frcpDx+PviKEQRSYzwhckuO2zoHcBYLHI754RE9z5h1RGtrngerc04mLpQQCPWBkH/2ObrX7We9YiwVSYZpFJQ==", 4046 + "integrity": "sha512-gdHMNx47Gw7b3kWxJV64NI+Q5nfl0y5DgDbiVtShiwa7Z0IZ07Ll4RLFo6AjrhzMtoEZn5PDE3/c2AbVsiCkpA==",
4047 "dev": true, 4047 "dev": true,
4048 "requires": { 4048 "requires": {
4049 - "chokidar": "^3.2.2", 4049 + "chokidar": "^3.5.2",
4050 - "debug": "^3.2.6", 4050 + "debug": "^3.2.7",
4051 "ignore-by-default": "^1.0.1", 4051 "ignore-by-default": "^1.0.1",
4052 "minimatch": "^3.0.4", 4052 "minimatch": "^3.0.4",
4053 - "pstree.remy": "^1.1.7", 4053 + "pstree.remy": "^1.1.8",
4054 "semver": "^5.7.1", 4054 "semver": "^5.7.1",
4055 "supports-color": "^5.5.0", 4055 "supports-color": "^5.5.0",
4056 "touch": "^3.1.0", 4056 "touch": "^3.1.0",
4057 - "undefsafe": "^2.0.3", 4057 + "undefsafe": "^2.0.5",
4058 "update-notifier": "^5.1.0" 4058 "update-notifier": "^5.1.0"
4059 }, 4059 },
4060 "dependencies": { 4060 "dependencies": {
......
...@@ -28,6 +28,6 @@ ...@@ -28,6 +28,6 @@
28 "uuid": "^8.3.2" 28 "uuid": "^8.3.2"
29 }, 29 },
30 "devDependencies": { 30 "devDependencies": {
31 - "nodemon": "^2.0.14" 31 + "nodemon": "^2.0.15"
32 } 32 }
33 } 33 }
......