Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(): added adv trade and cbapp examples #34

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions examples/AdvancedTrade/Private/getAccounts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { CBAdvancedTradeClient } from '../../../src/index.js';
// import { CBAdvancedTradeClient } from 'coinbase-api';

// initialise the client
const client = new CBAdvancedTradeClient({
apiKey: process.env.API_KEY_NAME || 'insert_api_key_here',
apiSecret: process.env.API_PRIVATE_KEY || 'insert_api_secret_here',
});

async function getAccounts() {
try {
// Get all accounts
const accounts = await client.getAccounts({ limit: 10 });
console.log('Accounts: ', accounts);

// Get specific account details
if (accounts.accounts.length > 0) {
const accountId = accounts.accounts[0].uuid;
const accountDetails = await client.getAccount({ account_id: accountId });
console.log('Account Details: ', accountDetails);
}
} catch (e) {
console.error('Get accounts error: ', e);
}
}

getAccounts();
27 changes: 27 additions & 0 deletions examples/AdvancedTrade/Private/getOrders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { CBAdvancedTradeClient } from '../../../src/index.js';
// import { CBAdvancedTradeClient } from 'coinbase-api';

// initialise the client
const client = new CBAdvancedTradeClient({
apiKey: process.env.API_KEY_NAME || 'insert_api_key_here',
apiSecret: process.env.API_PRIVATE_KEY || 'insert_api_secret_here',
});

async function getOrders() {
try {
// Get all orders
const orders = await client.getOrders({ limit: 5 });
console.log('Orders: ', orders);

// Get order details
if (orders.orders.length > 0) {
const orderId = orders.orders[0].order_id;
const orderDetails = await client.getOrder({ order_id: orderId });
console.log('Order Details: ', orderDetails);
}
} catch (e) {
console.error('Error: ', e);
}
}

getOrders();
49 changes: 49 additions & 0 deletions examples/AdvancedTrade/Private/submitOrderPerps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { CBAdvancedTradeClient } from '../../../src/index.js';
// import { CBAdvancedTradeClient } from 'coinbase-api';

// initialise the client
const client = new CBAdvancedTradeClient({
apiKey: process.env.API_KEY_NAME || 'insert_api_key_here',
apiSecret: process.env.API_PRIVATE_KEY || 'insert_api_secret_here',
});

async function submitOrder() {
try {
// submit market futures order
const newOrder = await client.submitOrder({
product_id: 'BTC PERP',
order_configuration: { market_market_ioc: { base_size: '0.001' } },
side: 'SELL',
client_order_id: client.generateNewOrderId(),
});
console.log('Result: ', newOrder);
} catch (e) {
console.error('Send new order error: ', e);
}

//
}

async function submitLimitOrder() {
try {
// Submit limit futures order
const limitOrder = await client.submitOrder({
product_id: 'BTC PERP',
order_configuration: {
limit_limit_gtc: {
base_size: '0.001',
limit_price: '50000.00',
},
},
side: 'BUY',
client_order_id: client.generateNewOrderId(),
});
console.log('Limit Order Result: ', limitOrder);
} catch (e) {
console.error('Submit limit order error: ', e);
}
}

submitLimitOrder();

submitOrder();
49 changes: 49 additions & 0 deletions examples/AdvancedTrade/Private/submitOrderSpot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { CBAdvancedTradeClient } from '../../../src/index.js';
// import { CBAdvancedTradeClient } from 'coinbase-api';

// initialise the client
const client = new CBAdvancedTradeClient({
apiKey: process.env.API_KEY_NAME || 'insert_api_key_here',
apiSecret: process.env.API_PRIVATE_KEY || 'insert_api_secret_here',
});

async function submitOrder() {
try {
// submit market spot order
const newOrder = await client.submitOrder({
product_id: 'BTC-USDT',
order_configuration: { market_market_ioc: { base_size: '0.001' } },
side: 'SELL',
client_order_id: client.generateNewOrderId(),
});
console.log('Result: ', newOrder);
} catch (e) {
console.error('Send new order error: ', e);
}

//
}

async function submitLimitOrder() {
try {
// Submit limit spot order
const limitOrder = await client.submitOrder({
product_id: 'BTC-USDT',
order_configuration: {
limit_limit_gtc: {
base_size: '0.001',
limit_price: '50000.00',
},
},
side: 'BUY',
client_order_id: client.generateNewOrderId(),
});
console.log('Limit Order Result: ', limitOrder);
} catch (e) {
console.error('Submit limit order error: ', e);
}
}

submitLimitOrder();

submitOrder();
50 changes: 50 additions & 0 deletions examples/AdvancedTrade/Public/advanced-public-rest-all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { CBAdvancedTradeClient } from '../../../src/index.js';
// import { CBAdvancedTradeClient } from 'coinbase-api';

// you can initialise public client without api keys as public calls do not require auth
const client = new CBAdvancedTradeClient({});

async function publicCalls() {
try {
// Get server time
const serverTime = await client.getServerTime();
console.log('Server Time: ', serverTime);

// Get public product book
const productBook = await client.getPublicProductBook({
product_id: 'BTC-USD',
limit: 10,
});
console.log('Public Product Book: ', productBook);

// List all public products
const publicProducts = await client.getPublicProducts();
console.log('Public Products: ', publicProducts);

// Get single public product
const publicProduct = await client.getPublicProduct({
product_id: 'BTC-USD',
});
console.log('Public Product: ', publicProduct);

// Get public product candles
const productCandles = await client.getPublicProductCandles({
product_id: 'BTC-USD',
granularity: 'ONE_MINUTE',
start: '1725976550',
end: '1725977550',
});
console.log('Public Product Candles: ', productCandles);

// Get public market trades
const marketTrades = await client.getPublicMarketTrades({
product_id: 'BTC-USD',
limit: 10,
});
console.log('Public Market Trades: ', marketTrades);
} catch (e) {
console.error('Error: ', e);
}
}

publicCalls();
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CBInternationalClient } from '../src/index.js';
import { CBInternationalClient } from '../../src/index.js';

const coinbaseInternational = new CBInternationalClient();

Expand Down
54 changes: 54 additions & 0 deletions examples/CoinbaseApp/Private/cb-app-private.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { CBAppClient } from '../../../src/index.js';
// import { CBAppClient } from 'coinbase-api';

// Initialize the client
const client = new CBAppClient({
apiKey: process.env.API_KEY_NAME || 'insert_api_key_here',
apiSecret: process.env.API_PRIVATE_KEY || 'insert_api_secret_here',
});

async function main() {
try {
// Deposit funds to your fiat account
const depositResult = await client.depositFunds({
account_id: 'your_fiat_account_id',
amount: '100.00',
currency: 'USD',
payment_method: 'your_payment_method_id',
});
console.log('Deposit Result: ', depositResult);

// Withdraw funds from fiat account
const withdrawResult = await client.withdrawFunds({
account_id: 'your_fiat_account_id',
amount: '50.00',
currency: 'USD',
payment_method: 'your_payment_method_id',
});
console.log('Withdraw Result: ', withdrawResult);

// Send money to another user
const sendMoneyResult = await client.sendMoney({
account_id: 'your_crypto_account_id',
type: 'send',
to: 'recipient_address_or_email',
amount: '0.01',
currency: 'BTC',
});
console.log('Send Money Result: ', sendMoneyResult);

// Transfer money between your own accounts
const transferMoneyResult = await client.transferMoney({
account_id: 'your_source_account_id',
type: 'transfer',
to: 'your_destination_account_id',
amount: '0.01',
currency: 'BTC',
});
console.log('Transfer Money Result: ', transferMoneyResult);
} catch (e) {
console.error('Error: ', e);
}
}

main();
25 changes: 25 additions & 0 deletions examples/CoinbaseApp/Private/depositFunds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { CBAppClient } from '../../../src/index.js';
// import { CBAppClient } from 'coinbase-api';

// Initialize the client
const client = new CBAppClient({
apiKey: process.env.API_KEY_NAME || 'insert_api_key_here',
apiSecret: process.env.API_PRIVATE_KEY || 'insert_api_secret_here',
});

async function depositFunds() {
try {
// Deposit funds to your fiat account
const depositResult = await client.depositFunds({
account_id: 'your_fiat_account_id',
amount: '100.00',
currency: 'USD',
payment_method: 'your_payment_method_id',
});
console.log('Deposit Result: ', depositResult);
} catch (e) {
console.error('Deposit funds error: ', e);
}
}

depositFunds();
26 changes: 26 additions & 0 deletions examples/CoinbaseApp/Private/sendMoney.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { CBAppClient } from '../../../src/index.js';
// import { CBAppClient } from 'coinbase-api';

// Initialize the client
const client = new CBAppClient({
apiKey: process.env.API_KEY_NAME || 'insert_api_key_here',
apiSecret: process.env.API_PRIVATE_KEY || 'insert_api_secret_here',
});

async function sendMoney() {
try {
// Send money to another user
const sendMoneyResult = await client.sendMoney({
account_id: 'your_crypto_account_id',
type: 'send',
to: 'recipient_address_or_email',
amount: '0.01',
currency: 'BTC',
});
console.log('Send Money Result: ', sendMoneyResult);
} catch (e) {
console.error('Send money error: ', e);
}
}

sendMoney();
26 changes: 26 additions & 0 deletions examples/CoinbaseApp/Private/transferMoney.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { CBAppClient } from '../../../src/index.js';
// import { CBAppClient } from 'coinbase-api';

// Initialize the client
const client = new CBAppClient({
apiKey: process.env.API_KEY_NAME || 'insert_api_key_here',
apiSecret: process.env.API_PRIVATE_KEY || 'insert_api_secret_here',
});

async function transferMoney() {
try {
// Transfer money between your own accounts
const transferMoneyResult = await client.transferMoney({
account_id: 'your_source_account_id',
type: 'transfer',
to: 'your_destination_account_id',
amount: '0.01',
currency: 'BTC',
});
console.log('Transfer Money Result: ', transferMoneyResult);
} catch (e) {
console.error('Transfer money error: ', e);
}
}

transferMoney();
25 changes: 25 additions & 0 deletions examples/CoinbaseApp/Private/withdrawFunds.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { CBAppClient } from '../../../src/index.js';
// import { CBAppClient } from 'coinbase-api';

// Initialize the client
const client = new CBAppClient({
apiKey: process.env.API_KEY_NAME || 'insert_api_key_here',
apiSecret: process.env.API_PRIVATE_KEY || 'insert_api_secret_here',
});

async function withdrawFunds() {
try {
// Withdraw funds from your fiat account
const withdrawResult = await client.withdrawFunds({
account_id: 'your_fiat_account_id',
amount: '50.00',
currency: 'USD',
payment_method: 'your_payment_method_id',
});
console.log('Withdraw Result: ', withdrawResult);
} catch (e) {
console.error('Withdraw funds error: ', e);
}
}

withdrawFunds();
Loading