Showing
4 changed files
with
126 additions
and
33 deletions
| 1 | const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args)); | 1 | const fetch = (...args) => import('node-fetch').then(({ default: fetch }) => fetch(...args)); |
| 2 | -const url1 = 'https://api.upbit.com/v1/market/all'; | ||
| 3 | 2 | ||
| 4 | const options = { method: 'GET', headers: { Accept: 'application/json' } }; | 3 | const options = { method: 'GET', headers: { Accept: 'application/json' } }; |
| 5 | const express = require('express'); | 4 | const express = require('express'); |
| 6 | const app = express(); | 5 | const app = express(); |
| 7 | const { Coin } = require("./models/Coin"); | 6 | const { Coin } = require("./models/Coin"); |
| 7 | +const {User}=require('./models/User'); | ||
| 8 | + | ||
| 9 | +require("dotenv").config(); | ||
| 10 | + | ||
| 11 | +const crypto=require('crypto'); | ||
| 12 | +const queryEncode=require('querystring').encode; | ||
| 13 | + | ||
| 14 | + | ||
| 15 | + | ||
| 16 | + | ||
| 17 | +const request = require('request') | ||
| 18 | +const {v4} = require("uuid") | ||
| 19 | +const sign = require('jsonwebtoken').sign | ||
| 20 | + | ||
| 8 | 21 | ||
| 9 | var sort_info = new Array(); | 22 | var sort_info = new Array(); |
| 10 | const mongoose = require('mongoose'); | 23 | const mongoose = require('mongoose'); |
| 11 | const config = require('./config/key'); | 24 | const config = require('./config/key'); |
| 25 | +const { json } = require('express'); | ||
| 26 | + | ||
| 12 | const connect = mongoose.connect(config.mongoURI, { | 27 | const connect = mongoose.connect(config.mongoURI, { |
| 13 | useNewUrlParser: true, useUnifiedTopology: true | 28 | useNewUrlParser: true, useUnifiedTopology: true |
| 14 | }) | 29 | }) |
| 15 | .then(() => console.log('디비연결 성공')) | 30 | .then(() => console.log('디비연결 성공')) |
| 16 | .catch((err) => console.log(err)); | 31 | .catch((err) => console.log(err)); |
| 17 | 32 | ||
| 33 | + | ||
| 34 | + | ||
| 18 | var korean_name = new Object(); | 35 | var korean_name = new Object(); |
| 36 | +const access_key = process.env.access_key; | ||
| 37 | +const secret_key = process.env.secret_key; | ||
| 38 | +const server_url = "https://api.upbit.com" | ||
| 39 | + | ||
| 40 | +function get_asset(){ | ||
| 41 | + const payload = { | ||
| 42 | + access_key: access_key, | ||
| 43 | + nonce: v4(), | ||
| 44 | + } | ||
| 45 | + const token = sign(payload, secret_key) | ||
| 46 | + const options = { | ||
| 47 | + method: "GET", | ||
| 48 | + url: server_url + "/v1/accounts", | ||
| 49 | + headers: {Authorization: `Bearer ${token}`}, | ||
| 50 | + } | ||
| 51 | + return new Promise(resolve=>{ | ||
| 52 | + request(options,function(err,res,body){ | ||
| 53 | + if (err) throw new Error(err) | ||
| 54 | + // test=res.json(); | ||
| 55 | + data=JSON.parse(body); | ||
| 56 | + // console.log(data[0].currency) | ||
| 57 | + data.filter(function(item){ | ||
| 58 | + if(item.currency=="PLA"){ | ||
| 59 | + resolve(item); | ||
| 60 | + } | ||
| 61 | + }) | ||
| 62 | + }) | ||
| 63 | + }) | ||
| 64 | +} | ||
| 19 | 65 | ||
| 20 | async function get_marketName() { | 66 | async function get_marketName() { |
| 21 | var data = new Array(); | 67 | var data = new Array(); |
| 22 | //전체 암호화폐 리스트 불러오기 | 68 | //전체 암호화폐 리스트 불러오기 |
| 23 | - let response = await fetch(url1, options) | 69 | + let response = await fetch(`${server_url}/v1/market/all`, options) |
| 24 | .then(res => res.json()) | 70 | .then(res => res.json()) |
| 25 | .then(json => { | 71 | .then(json => { |
| 26 | for (i in json) { | 72 | for (i in json) { |
| ... | @@ -30,10 +76,45 @@ async function get_marketName() { | ... | @@ -30,10 +76,45 @@ async function get_marketName() { |
| 30 | }) | 76 | }) |
| 31 | return data; | 77 | return data; |
| 32 | } | 78 | } |
| 79 | +async function transaction_coin(coin_name,side,volume,price,ord_type){ | ||
| 80 | + const body = { | ||
| 81 | + market: coin_name, | ||
| 82 | + side: side, | ||
| 83 | + volume: volume, | ||
| 84 | + price: price, | ||
| 85 | + ord_type: ord_type, | ||
| 86 | + } | ||
| 87 | + //시장가 매수인 경우 price를 얼마치 살건지 입력 | ||
| 88 | + //시장가 매도인경우 volume에 몇개를 팔건지 입력 | ||
| 89 | + const query = queryEncode(body) | ||
| 90 | + | ||
| 91 | + const hash = crypto.createHash('sha512') | ||
| 92 | + const queryHash = hash.update(query, 'utf-8').digest('hex') | ||
| 93 | + | ||
| 94 | + const payload = { | ||
| 95 | + access_key: access_key, | ||
| 96 | + nonce: v4(), | ||
| 97 | + query_hash: queryHash, | ||
| 98 | + query_hash_alg: 'SHA512', | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + const token = sign(payload, secret_key) | ||
| 102 | + | ||
| 103 | + const options = { | ||
| 104 | + method: "POST", | ||
| 105 | + url: server_url + "/v1/orders", | ||
| 106 | + headers: {Authorization: `Bearer ${token}`}, | ||
| 107 | + json: body | ||
| 108 | + } | ||
| 109 | + request(options, (error, response, body) => { | ||
| 110 | + if (error) throw new Error(error) | ||
| 111 | + console.log(body) | ||
| 112 | + }) | ||
| 113 | +} | ||
| 33 | async function get_marketInfo() { | 114 | async function get_marketInfo() { |
| 34 | //각 암호화폐 정보 조회 | 115 | //각 암호화폐 정보 조회 |
| 35 | var name_list = await get_marketName(); | 116 | var name_list = await get_marketName(); |
| 36 | - const url2 = `https://api.upbit.com/v1/ticker/?markets=${name_list}`; | 117 | + const url2 = `${server_url}/v1/ticker/?markets=${name_list}`; |
| 37 | var arr = new Array(); | 118 | var arr = new Array(); |
| 38 | let response2 = await fetch(url2, options) | 119 | let response2 = await fetch(url2, options) |
| 39 | .then(res => res.json()) | 120 | .then(res => res.json()) |
| ... | @@ -106,6 +187,9 @@ async function check_coin(t1) { | ... | @@ -106,6 +187,9 @@ async function check_coin(t1) { |
| 106 | console.log(err); | 187 | console.log(err); |
| 107 | } else { | 188 | } else { |
| 108 | console.log("***" + result.korean_name + "은(는)" + result.count * 5 + "분 동안 하락중"); | 189 | console.log("***" + result.korean_name + "은(는)" + result.count * 5 + "분 동안 하락중"); |
| 190 | + if(count>=3){ | ||
| 191 | + transaction_coin(result.name,"bid",null,"얼마치 살건지","price"); | ||
| 192 | + } | ||
| 109 | } | 193 | } |
| 110 | }) | 194 | }) |
| 111 | }//그대로 이거나 올랐을때 | 195 | }//그대로 이거나 올랐을때 |
| ... | @@ -114,6 +198,8 @@ async function check_coin(t1) { | ... | @@ -114,6 +198,8 @@ async function check_coin(t1) { |
| 114 | if (err) { | 198 | if (err) { |
| 115 | console.log(err); | 199 | console.log(err); |
| 116 | } else { | 200 | } else { |
| 201 | + //특정 조건... | ||
| 202 | + transaction_coin(result.name,"ask","몇개를 팔건지",null,"market"); | ||
| 117 | console.log(result.korean_name + "은(는)" + result.count * 5 + "분 동안 상승 혹은 정체중"); | 203 | console.log(result.korean_name + "은(는)" + result.count * 5 + "분 동안 상승 혹은 정체중"); |
| 118 | } | 204 | } |
| 119 | }) | 205 | }) |
| ... | @@ -122,29 +208,6 @@ async function check_coin(t1) { | ... | @@ -122,29 +208,6 @@ async function check_coin(t1) { |
| 122 | }) | 208 | }) |
| 123 | } | 209 | } |
| 124 | } | 210 | } |
| 125 | -// async function repeat_check(t1) { | ||
| 126 | -// await Coin.find().sort({ tid: 1 }).then(result => { | ||
| 127 | -// for (var key in result) { | ||
| 128 | -// t1.push(result[key].name) | ||
| 129 | -// } | ||
| 130 | -// }) | ||
| 131 | -// let check_time = setInterval(() => { | ||
| 132 | -// let today = new Date(); | ||
| 133 | -// let minutes = today.getMinutes(); | ||
| 134 | -// let seconds = today.getSeconds(); | ||
| 135 | -// if (minutes % 5 == 0 && seconds == 0) { | ||
| 136 | -// clearInterval(check_time); | ||
| 137 | -// console.log("현재 시간은 " + today.toLocaleTimeString()); | ||
| 138 | -// check_coin(t1); | ||
| 139 | -// setInterval(async () => { | ||
| 140 | -// let today = new Date(); | ||
| 141 | -// console.log("현재 시간은 " + today.toLocaleTimeString()); | ||
| 142 | -// check_coin(t1); | ||
| 143 | -// }, 60000 * 5); | ||
| 144 | -// } | ||
| 145 | -// }, 1000) | ||
| 146 | - | ||
| 147 | -// } | ||
| 148 | async function latest_repeat(t1) { | 211 | async function latest_repeat(t1) { |
| 149 | await Coin.find().sort({ tid: 1 }).then(result => { | 212 | await Coin.find().sort({ tid: 1 }).then(result => { |
| 150 | for (var key in result) { | 213 | for (var key in result) { |
| ... | @@ -161,16 +224,19 @@ async function latest_repeat(t1) { | ... | @@ -161,16 +224,19 @@ async function latest_repeat(t1) { |
| 161 | sort_info = (await sort_data()); | 224 | sort_info = (await sort_data()); |
| 162 | (await refresh_db()); | 225 | (await refresh_db()); |
| 163 | console.log("현재 시간은 " + today.toLocaleTimeString()); | 226 | console.log("현재 시간은 " + today.toLocaleTimeString()); |
| 227 | + var count=0; | ||
| 164 | let coin=setInterval(async () => { | 228 | let coin=setInterval(async () => { |
| 165 | let today = new Date(); | 229 | let today = new Date(); |
| 166 | let minutes=today.getMinutes(); | 230 | let minutes=today.getMinutes(); |
| 167 | let seconds=today.getSeconds(); | 231 | let seconds=today.getSeconds(); |
| 168 | console.log("현재 시간은 " + today.toLocaleTimeString()); | 232 | console.log("현재 시간은 " + today.toLocaleTimeString()); |
| 169 | - await (check_coin(t1)); | 233 | + await (check_coin(t1).then(count++)); |
| 170 | - if(minutes==0&&seconds==0){ | 234 | + //1시간마다 db 최신화... |
| 171 | - sort_info = (await sort_data()); | 235 | + if(count==12){ |
| 236 | + count=0; | ||
| 237 | + sort_info=(await sort_data()); | ||
| 172 | (await refresh_db()); | 238 | (await refresh_db()); |
| 173 | - console.log("db 최신화"); | 239 | + console.log("db최신화"); |
| 174 | } | 240 | } |
| 175 | }, 60000*5); | 241 | }, 60000*5); |
| 176 | } | 242 | } |
| ... | @@ -182,5 +248,6 @@ app.listen(5000, async () => { | ... | @@ -182,5 +248,6 @@ app.listen(5000, async () => { |
| 182 | //5분마다 현재 가격 가져와서 db랑 비교후 매수 매도 기준잡기 | 248 | //5분마다 현재 가격 가져와서 db랑 비교후 매수 매도 기준잡기 |
| 183 | var t1 = new Array(); | 249 | var t1 = new Array(); |
| 184 | test_data=await (latest_repeat(t1)); | 250 | test_data=await (latest_repeat(t1)); |
| 185 | - //반복 | 251 | + //계좌 정보 db 최신화 |
| 252 | + console.log(await get_asset()); | ||
| 186 | }) | 253 | }) |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
models/User.js
0 → 100644
| 1 | +const mongoose=require('mongoose'); | ||
| 2 | + | ||
| 3 | +const userSchema=mongoose.Schema({ | ||
| 4 | + krw_balance:{ | ||
| 5 | + type:Number, | ||
| 6 | + }, | ||
| 7 | + market:{ | ||
| 8 | + type:String, | ||
| 9 | + }, | ||
| 10 | + count:{ | ||
| 11 | + type:Number, | ||
| 12 | + }, | ||
| 13 | + avg_buy_price:{ | ||
| 14 | + type:Number | ||
| 15 | + } | ||
| 16 | +}) | ||
| 17 | + | ||
| 18 | +const User=mongoose.model("User",userSchema); | ||
| 19 | +module.exports={User}; | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
This diff is collapsed. Click to expand it.
| ... | @@ -5,7 +5,7 @@ | ... | @@ -5,7 +5,7 @@ |
| 5 | "main": "index.js", | 5 | "main": "index.js", |
| 6 | "scripts": { | 6 | "scripts": { |
| 7 | "test": "echo \"Error: no test specified\" && exit 1", | 7 | "test": "echo \"Error: no test specified\" && exit 1", |
| 8 | - "start":"node index.js" | 8 | + "start": "node index.js" |
| 9 | }, | 9 | }, |
| 10 | "repository": { | 10 | "repository": { |
| 11 | "type": "git", | 11 | "type": "git", |
| ... | @@ -14,9 +14,16 @@ | ... | @@ -14,9 +14,16 @@ |
| 14 | "author": "", | 14 | "author": "", |
| 15 | "license": "ISC", | 15 | "license": "ISC", |
| 16 | "dependencies": { | 16 | "dependencies": { |
| 17 | + "crypto": "^1.0.1", | ||
| 18 | + "dotenv": "^10.0.0", | ||
| 17 | "express": "^4.17.1", | 19 | "express": "^4.17.1", |
| 20 | + "jsonwebtoken": "^8.5.1", | ||
| 18 | "mongoose": "^6.0.12", | 21 | "mongoose": "^6.0.12", |
| 19 | - "node-fetch": "^3.0.0" | 22 | + "node-fetch": "^3.0.0", |
| 23 | + "querystring": "^0.2.1", | ||
| 24 | + "request": "^2.88.2", | ||
| 25 | + "requests": "^0.3.0", | ||
| 26 | + "uuid": "^8.3.2" | ||
| 20 | }, | 27 | }, |
| 21 | "devDependencies": { | 28 | "devDependencies": { |
| 22 | "nodemon": "^2.0.14" | 29 | "nodemon": "^2.0.14" | ... | ... |
-
Please register or login to post a comment