Skip to content

Commit

Permalink
coinbase ready
Browse files Browse the repository at this point in the history
  • Loading branch information
ordinariusprof committed Mar 28, 2024
1 parent 1a350e4 commit da49973
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
28 changes: 18 additions & 10 deletions api/coinbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class CoinbaseAPI {
this.apiKey = apiKey;
this.apiSecret = apiSecret;
this.baseURL = 'https://api.coinbase.com';
this.accountId = null;
}

async getPublicEndpoint(endpoint) {
Expand All @@ -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) {
Expand All @@ -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',
};
}
Expand All @@ -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() {
Expand Down
1 change: 0 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 8 additions & 11 deletions model/exchanges/coinbase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,27 +38,22 @@ 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(
`withdrawing ${withdrawalAmount} ${this.withdrawCurrency} to wallet ${this.withdrawWallet}`,
);

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;
}

Expand Down

0 comments on commit da49973

Please sign in to comment.