-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupbitClient.js
More file actions
127 lines (119 loc) · 3.97 KB
/
upbitClient.js
File metadata and controls
127 lines (119 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const request = require('request')
const { v4: uuidv4 } = require('uuid'); //npm install uuidv4 --save
const sign = require('jsonwebtoken').sign
const crypto = require('crypto')
const queryEncode = require("querystring").encode
const access_key = "testjongbeop"
const secret_key = "TEST_SECRET_KET"
const server_url = "http://ubuntu.securekim.com"
async function getBalance(){
const payload = {
access_key: access_key,
nonce: uuidv4(),
}
const token = sign(payload, secret_key)
const options = {
method: "GET",
url: server_url + "/v1/accounts",
headers: {Authorization: `Bearer ${token}`},
}
return new Promise(function(resolve, reject) {
request(options, (error, response, body) => {
if (error) reject();
console.log(response.statusCode)
resolve(body)
})
});
}
//얼마너치살건지
async function API_buyImmediate(market, price){
const body = {
market: market,
side: 'bid',
volume: null,
price: price.toString(),
ord_type: 'price',
}
const query = queryEncode(body)
const hash = crypto.createHash('sha512')
const queryHash = hash.update(query, 'utf-8').digest('hex')
const payload = {
access_key: access_key,
nonce: uuidv4(),
query_hash: queryHash,
query_hash_alg: 'SHA512',
}
const token = sign(payload, secret_key)
const options = {
method: "POST",
url: server_url + "/v1/orders",
headers: {Authorization: `Bearer ${token}`},
json: body
}
return new Promise(function(resolve, reject) {
request(options, (error, response, body) => {
if (error) reject();
console.log(response.statusCode)
resolve(body)
})
});
}
//몇개팔건지
async function API_sellImmediate(market, volume){
const body = {
market: market,
side: 'ask',
volume: volume.toString(),
price: null,
ord_type: 'market',
}
const query = queryEncode(body)
const hash = crypto.createHash('sha512')
const queryHash = hash.update(query, 'utf-8').digest('hex')
const payload = {
access_key: access_key,
nonce: uuidv4(),
query_hash: queryHash,
query_hash_alg: 'SHA512',
}
const token = sign(payload, secret_key)
const options = {
method: "POST",
url: server_url + "/v1/orders",
headers: {Authorization: `Bearer ${token}`},
json: body
}
return new Promise(function(resolve, reject) {
request(options, (error, response, body) => {
if (error) reject();
console.log(response.statusCode)
resolve(body)
})
});
}
async function main(){
//// ERROR TEST - BUY ////
//body = await getBalance()
//body = await API_buyImmediate("KRW-ETH", 2000000); // 정상 구매
//
//36609686
//36604449
//1.0138627037817458
//1.014008084593923
//348583.84272261686
//353695.55995306565
body = await API_buyImmediate("KRW-VET", 10000); // 정상 구매
//body = await API_sellImmediate("KRW-ETH", 0.04604051565377532); // 정상 판매
console.log(body);
// body = await API_buyImmediate("KRW-BTC", 500000); // 정상 구매
// body = await API_buyImmediate("KRW-BTC", -1); // 범위 에러
// body = await API_buyImmediate("KRW-BTC", 1); // 최소 에러
// body = await API_buyImmediate("KRW-BTC", 5234); // 단위 에러..안남
// body = await API_buyImmediate("KRW-BTC", ""); // 가격 에러
// body = await API_buyImmediate("KRW-BTC", 100000000); // 가격 에러
// body = await API_buyImmediate("KRW-ABC", 10000); // 마켓 에러
// body = await API_buyImmediate("", 10000); // 마켓 에러
// body = await API_buyImmediate("KRW-ABC-BTC", 10000);// 마켓 에러
// body = await API_sellImmediate("KRW-BTC", 100); // 개수 에러
}
main()