-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from ordinalsbot/coinbase
add coinbase api
- Loading branch information
Showing
5 changed files
with
210 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
const axios = require('axios'); | ||
const crypto = require('crypto'); | ||
|
||
class CoinbaseAPI { | ||
constructor(apiKey, apiSecret) { | ||
this.apiKey = apiKey; | ||
this.apiSecret = apiSecret; | ||
this.baseURL = 'https://api.coinbase.com'; | ||
this.accountId = null; | ||
} | ||
|
||
async getPublicEndpoint(endpoint) { | ||
try { | ||
const response = await axios.get(`${this.baseURL}${endpoint}`); | ||
return response.data; | ||
} catch (error) { | ||
console.error('Error:', error.message); | ||
throw error; | ||
} | ||
} | ||
|
||
async getAuthEndpoint(endpoint, body) { | ||
try { | ||
const headers = await this.signMessage('GET', endpoint, body); | ||
const response = await axios.get(`${this.baseURL}${endpoint}`, { headers }); | ||
return response.data; | ||
} catch (error) { | ||
console.error('Error:', error.message); | ||
throw error; | ||
} | ||
} | ||
|
||
async postAuthEndpoint(endpoint, body) { | ||
try { | ||
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) { | ||
console.error('Error:', error.message); | ||
throw error; | ||
} | ||
} | ||
|
||
async signMessage(method, endpoint, body = '') { | ||
const timestamp = Math.floor(Date.now() / 1000); // Unix time in seconds | ||
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-VERSION': '2024-03-22', | ||
'Content-Type': 'application/json', | ||
}; | ||
} | ||
|
||
async getSystemStatus() { | ||
throw new Error('Not implemented'); | ||
} | ||
|
||
async getAccountId() { | ||
const accounts = await this.getAuthEndpoint('/v2/accounts/BTC'); | ||
return accounts.data.id; | ||
} | ||
|
||
async getAccountBalance() { | ||
if (!this.accountId) { | ||
this.accountId = await this.getAccountId(); | ||
} | ||
return this.getAuthEndpoint(`/v2/accounts/${this.accountId}`); | ||
} | ||
|
||
async withdrawFunds(amount, currency, address) { | ||
if (!this.accountId) { | ||
this.accountId = await this.getAccountId(); | ||
} | ||
const body = { | ||
type: 'send', | ||
amount, | ||
currency, | ||
to: address, | ||
to_financial_institution: false, | ||
}; | ||
return this.postAuthEndpoint(`/v2/accounts/${this.accountId}/transactions`, body); | ||
} | ||
|
||
async getServerTime() { | ||
return this.getPublicEndpoint('/v2/time'); | ||
} | ||
} | ||
|
||
module.exports = CoinbaseAPI; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
const CoinbaseAPI = require('../../api/coinbase'); | ||
|
||
/** | ||
* Rotates funds from a Coinbase account | ||
*/ | ||
class CoinbaseTumbler { | ||
/** | ||
* @param {CoinbaseAPI} coinbaseClient | ||
* @param {number} minWithdrawalAmount | ||
* @param {number} maxWithdrawalAmount | ||
* @param {string} withdrawWallet | ||
* @param {string} withdrawCurrency | ||
*/ | ||
constructor( | ||
coinbaseClient, | ||
minWithdrawalAmount, | ||
maxWithdrawalAmount, | ||
withdrawWallet, | ||
withdrawCurrency, | ||
) { | ||
this.coinbaseClient = coinbaseClient; | ||
this.minWithdrawalAmount = minWithdrawalAmount; | ||
this.maxWithdrawalAmount = maxWithdrawalAmount; | ||
this.withdrawWallet = withdrawWallet; | ||
this.withdrawCurrency = withdrawCurrency; | ||
} | ||
|
||
withdrawAvailableFunds = async () => { | ||
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; | ||
} | ||
|
||
let withdrawalAmount = Number(btcBalance).toFixed(8);; | ||
if (btcBalance > this.maxWithdrawalAmount) { | ||
withdrawalAmount = this.maxWithdrawalAmount; | ||
} | ||
// 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, | ||
); | ||
|
||
if (!res.data?.id) { | ||
console.error('error calling coinbase api', res); | ||
return false; | ||
} | ||
|
||
console.log('successful withdrawal from coinbase'); | ||
return true; | ||
}; | ||
} | ||
|
||
module.exports = CoinbaseTumbler; |