From da49973cddfc3620a21a5dba694c3634849ed539 Mon Sep 17 00:00:00 2001 From: ordinariusprof Date: Thu, 28 Mar 2024 06:33:31 +0100 Subject: [PATCH] coinbase ready --- api/coinbase.js | 28 ++++++++++++++++++---------- index.js | 1 - model/exchanges/coinbase.js | 19 ++++++++----------- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/api/coinbase.js b/api/coinbase.js index 918cb82..1c0e525 100644 --- a/api/coinbase.js +++ b/api/coinbase.js @@ -6,6 +6,7 @@ class CoinbaseAPI { this.apiKey = apiKey; this.apiSecret = apiSecret; this.baseURL = 'https://api.coinbase.com'; + this.accountId = null; } async getPublicEndpoint(endpoint) { @@ -31,7 +32,7 @@ class CoinbaseAPI { async postAuthEndpoint(endpoint, body) { try { - const headers = await this.signMessage('POST', endpoint, body); + const headers = await this.signMessage('POST', endpoint, JSON.stringify(body)); const response = await axios.post(`${this.baseURL}${endpoint}`, body, { headers }); return response.data; } catch (error) { @@ -40,14 +41,15 @@ class CoinbaseAPI { } } - async signMessage(method, endpoint, body) { + async signMessage(method, endpoint, body = '') { const timestamp = Math.floor(Date.now() / 1000); // Unix time in seconds - const message = `${timestamp}${method}${endpoint}${JSON.stringify(body)}`; + const message = `${timestamp}${method}${endpoint}${body}`; const signature = crypto.createHmac('sha256', this.apiSecret).update(message).digest('hex'); return { 'CB-ACCESS-KEY': this.apiKey, 'CB-ACCESS-SIGN': signature, - 'CB-ACCESS-TIMESTAMP': timestamp, + 'CB-ACCESS-TIMESTAMP': `${timestamp}`, + 'CB-VERSION': '2024-03-22', 'Content-Type': 'application/json', }; } @@ -58,22 +60,28 @@ class CoinbaseAPI { async getAccountId() { const accounts = await this.getAuthEndpoint('/v2/accounts/BTC'); - return accounts.data.data.id; + return accounts.data.id; } async getAccountBalance() { - const accountId = await this.getAccountId(); - return this.getAuthEndpoint(`/v2/accounts/${accountId}`); + if (!this.accountId) { + this.accountId = await this.getAccountId(); + } + return this.getAuthEndpoint(`/v2/accounts/${this.accountId}`); } async withdrawFunds(amount, currency, address) { - const accountId = await this.getAccountId(); + if (!this.accountId) { + this.accountId = await this.getAccountId(); + } const body = { + type: 'send', amount, currency, - crypto_address: address, + to: address, + to_financial_institution: false, }; - return this.postAuthEndpoint(`/v2/accounts/${accountId}/transactions`, body); + return this.postAuthEndpoint(`/v2/accounts/${this.accountId}/transactions`, body); } async getServerTime() { diff --git a/index.js b/index.js index 44dff39..4fd3e04 100644 --- a/index.js +++ b/index.js @@ -61,7 +61,6 @@ const { KRAKEN_API_SECRET, KRAKEN_WITHDRAWAL_WALLET, KRAKEN_WITHDRAW_CURRENCY, - EXCHANGE_DEPOSIT_WALLET, OKCOIN_API_KEY, OKCOIN_API_SECRET, OKCOIN_API_PASSPHRASE, diff --git a/model/exchanges/coinbase.js b/model/exchanges/coinbase.js index e6f612e..0af8a2c 100644 --- a/model/exchanges/coinbase.js +++ b/model/exchanges/coinbase.js @@ -26,7 +26,9 @@ class CoinbaseTumbler { } withdrawAvailableFunds = async () => { - const btcBalance = balance.data.find((b) => b.ccy === 'BTC').availBal; + const balance = await this.coinbaseClient.getAccountBalance(); + + const btcBalance = balance.data.balance.amount; if (btcBalance < this.minWithdrawalAmount) { console.log(`insufficient funds to withdraw, account balance ${btcBalance}`); return false; @@ -36,11 +38,8 @@ class CoinbaseTumbler { if (btcBalance > this.maxWithdrawalAmount) { withdrawalAmount = this.maxWithdrawalAmount; } - - const withdrawalFees = await this.coinbaseClient.getWithdrawalFee(this.withdrawCurrency); - let btcFee = withdrawalFees.data.find((f) => f.chain === 'BTC-Bitcoin').maxFee; - btcFee = parseFloat(btcFee); - withdrawalAmount -= btcFee; + // deduct some random fee + withdrawalAmount -= 0.001; withdrawalAmount = Number(withdrawalAmount).toFixed(8); console.log( @@ -48,15 +47,13 @@ class CoinbaseTumbler { ); const res = await this.coinbaseClient.withdrawFunds( + `${withdrawalAmount}`, this.withdrawCurrency, this.withdrawWallet, - withdrawalAmount, - btcFee, ); - console.log('response from coinbase', res); - if (res.msg) { - console.error('error calling coinbase api', res.msg); + if (!res.data?.id) { + console.error('error calling coinbase api', res); return false; }