import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import * as crypto from 'crypto';
interface WithdrawalRequest {
profile?: string;
amount: string;
currency: string;
crypto_address: string;
network?: string;
}
interface Account {
id: string;
balance: string;
holds: string;
available: string;
currency: string;
}
interface WithdrawalResponse {
id: string;
amount: string;
currency: string;
fee: number;
subtotal: number;
network: string;
}
class HTTPClient {
private client: AxiosInstance;
private apiKey: string;
private secret: string;
private password: string;
private url: string;
constructor(url: string, key: string, secret: string, passphrase: string) {
this.client = axios.create();
this.apiKey = key;
this.secret = secret;
this.password = passphrase;
this.url = url;
}
async request<T>(method: string, urlPattern: string, content: any): Promise<T> {
const url = `${this.url}${urlPattern}`;
let reqBody: string | undefined;
if (content) {
reqBody = JSON.stringify(content);
}
const [signature, timestamp] = await this.generateSignatureHeaders(method, urlPattern, reqBody || '');
const headers: Record<string, string> = {
'Content-Type': 'application/json',
'cb-access-key': this.apiKey,
'cb-access-sign': signature,
'cb-access-timestamp': timestamp,
'cb-access-passphrase': this.password
};
const config: AxiosRequestConfig = {
method: method as any,
url,
data: reqBody,
headers
};
try {
const response: AxiosResponse<T> = await this.client(config);
return response.data;
} catch (error) {
console.error('Error in request:', error);
throw error;
}
}
private async generateSignatureHeaders(method: string, path: string, body: string): Promise<[string, string]> {
const timestamp = Math.floor(Date.now() / 1000).toString();
const message = timestamp + method + path + body;
const signature = await this.generateSignature(message);
return [signature, timestamp];
}
private async generateSignature(message: string): Promise<string> {
const key = Buffer.from(this.secret, 'base64');
const hmac = crypto.createHmac('sha256', key);
hmac.update(message);
return hmac.digest('base64');
}
}
async function main() {
const passphrase = ""; // Replace with exchange API credentials.
const secret = "";
const key = "";
const url = "https://api.exchange.coinbase.com";
const client = new HTTPClient(url, key, secret, passphrase);
try {
const accounts = await client.request<Account[]>('GET', "/accounts", null);
// Validate there is a USDC account.
for (const account of accounts) {
if (account.currency !== "USDC") {
continue;
}
console.log(`ID: ${account.id}, Balance: ${account.balance}, Holds: ${account.holds}, Available: ${account.available}, Currency: ${account.currency}`);
}
const withdrawalPattern = "/withdrawals/crypto";
const withdrawalRequest: WithdrawalRequest = {
amount: "1",
currency: "USDC",
network: "base",
crypto_address: "0xWalletAddress", // Replace with your wallet address retrieved from wallet.getDefaultAddress()
};
const withdrawalResp = await client.request<WithdrawalResponse>('POST', withdrawalPattern, withdrawalRequest);
console.log("Withdrawal response: ", withdrawalResp);
} catch (error) {
console.error('Error in requesting to Exchange APIs:', error);
}
}
main();