forked from Tag0Mag0/gdax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
131 lines (115 loc) · 3.11 KB
/
app.js
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
128
129
130
131
const dotenv = require('dotenv');
const Gdax = require('gdax');
dotenv.load();
// Set this to false to access actual API
const sandbox = true;
const apiURI = sandbox ? 'https://api-public.sandbox.gdax.com' : 'https://api.gdax.com';
const key = sandbox ? process.env.GDAX_SANDBOX_API_KEY : process.env.GDAX_API_KEY;
const b64secret = sandbox ? process.env.GDAX_SANDBOX_API_SECRET : process.env.GDAX_API_SECRET;
const passphrase = sandbox ? process.env.GDAX_SANDBOX_API_PASSPHRASE : process.env.GDAX_API_PASSPHRASE;
const publicClient = new Gdax.PublicClient('BTC-USD', apiURI);
const authedClient = new Gdax.AuthenticatedClient(key, b64secret, passphrase, apiURI);
/**
* @function [getProducts]
* @returns {Json} products
*/
const getProducts = () => {
publicClient.getProducts()
.then(data => {
console.log(data)
})
.catch(error => {
console.log(error)
});
};
/**
* @function [getCoinbaseAccounts]
* @returns {Json} coinbaseAccounts
*/
const getCoinbaseAccounts = () => {
authedClient.getCoinbaseAccounts()
.then(data => {
console.log(data)
})
.catch(error => {
console.log(error)
});
};
/**
* @function [getAccounts]
* @returns {Json} accounts
*/
const getAccounts = () => {
authedClient.getAccounts()
.then(data => {
console.log(data)
})
.catch(error => {
console.log(error)
});
};
/**
* @function [getAccount]
* @returns {Json} account
*/
const getAccount = (currencyType) => {
return new Promise((resolve, reject) => {
const accountId = sandbox ? process.env[`SANDBOX_${currencyType}`] : process.env[currencyType]
if (accountId) {
authedClient.getAccount(accountId)
.then(data => {
console.log(data)
})
.catch(error => {
console.log(error)
});
} else {
throw 'no currency matches that type'
}
})
}
const getCoinbaseAccount = (currencyType) => {
return new Promise((resolve, reject) => {
const findCoinbaseAccount = (coinbaseAccount) => {
return coinbaseAccount.currency === currencyType;
}
authedClient.getCoinbaseAccounts()
.then(data => {
const account = data.find(findCoinbaseAccount)
console.log(account)
return account;
})
})
}
const getAccountHistory = (currencyType) => {
getAccount(currencyType)
.then(account => {
authedClient.getAccountHistory(account.id)
.then(data => {
console.log(data)
})
.catch(error => {
console.log(error)
});
});
};
// Deposit to your Exchange USD account from your Coinbase USD account.
const depositUSD = (amount) => {
getCoinbaseAccount('USD')
.then(account => {
const depositParamsUSD = {
'amount': amount,
'currency': 'USD',
'coinbase_account_id': account.id, // USD Coinbase Account ID
};
authedClient.deposit(depositParamsUSD)
.then(data => {
console.log(data)
})
.catch(error => {
console.log(error)
});
});
};
// Export all methods
module.exports = { getProducts, getAccounts, getAccountHistory, getCoinbaseAccount, getCoinbaseAccounts, getAccount, depositUSD };