Skip to content

Commit

Permalink
chore(): Finalised examples
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ-Cro committed Jun 12, 2024
1 parent e66b05d commit 4124513
Show file tree
Hide file tree
Showing 15 changed files with 191 additions and 198 deletions.
13 changes: 8 additions & 5 deletions examples/futures/getBalances.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { RestClient } from '../../src';

// Define the account object with API key and secret
const account = {
key: process.env.API_KEY || 'yourApiHere',
secret: process.env.API_SECRET || 'yourSecretHere',
key: process.env.API_KEY || 'yourApiHere', // Replace 'yourApiHere' with your actual API key
secret: process.env.API_SECRET || 'yourSecretHere', // Replace 'yourSecretHere' with your actual API secret
};

// Initialize the RestClient with the API credentials
const gateRestClient = new RestClient({
apiKey: account.key,
apiSecret: account.secret,
Expand All @@ -14,12 +16,13 @@ async function getFuturesBalances() {
try {
console.log('Using API keys:', account);

// GET specific ticker
// Fetch the futures account balance for USDT settlement
const ticker = await gateRestClient.getFuturesAccount({ settle: 'usdt' });
console.log('Response: ', ticker);
console.log('Response: ', ticker); // Log the response to the console
} catch (e) {
console.error(`Error in execution: `, e);
console.error(`Error in execution: `, e); // Log any errors that occur
}
}

// Execute the function to get futures balances
getFuturesBalances();
25 changes: 14 additions & 11 deletions examples/futures/getOrders.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { RestClient } from '../../src';

// Define the account object with API key and secret
const account = {
key: process.env.API_KEY || 'yourApiHere',
secret: process.env.API_SECRET || 'yourSecretHere',
key: process.env.API_KEY || 'yourApiHere', // Replace 'yourApiHere' with your actual API key or use environment variables
secret: process.env.API_SECRET || 'yourSecretHere', // Replace 'yourSecretHere' with your actual API secret or use environment variables
};

// Initialize the RestClient with the API credentials
const gateRestClient = new RestClient({
apiKey: account.key,
apiSecret: account.secret,
Expand All @@ -14,22 +16,23 @@ async function getFuturesOrders() {
try {
console.log('Using API keys:', account);

// get open orders
// Fetch open futures orders with USDT settlement
const orders = await gateRestClient.getFuturesOrders({
settle: 'usdt',
status: 'open',
settle: 'usdt', // Specify the settlement currency
status: 'open', // Specify the status of the orders to fetch
});
console.log('Response: ', orders);
console.log('Response: ', orders); // Log the response to the console

// get finished orders
// Fetch finished futures orders with USDT settlement
const orders1 = await gateRestClient.getFuturesOrders({
settle: 'usdt',
status: 'finished',
settle: 'usdt', // Specify the settlement currency
status: 'finished', // Specify the status of the orders to fetch
});
console.log('Response: ', orders1);
console.log('Response: ', orders1); // Log the response to the console
} catch (e) {
console.error(`Error in execution: `, e);
console.error(`Error in execution: `, e); // Log any errors that occur
}
}

// Execute the function to get futures orders
getFuturesOrders();
29 changes: 16 additions & 13 deletions examples/futures/getTickers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { RestClient } from '../../src';
import { RestClient } from '../../src'; // Import the RestClient from the src directory

// Define the account object with API key and secret
const account = {
key: process.env.API_KEY || 'yourApiHere',
secret: process.env.API_SECRET || 'yourSecretHere',
key: process.env.API_KEY || 'yourApiHere', // Replace 'yourApiHere' with your actual API key
secret: process.env.API_SECRET || 'yourSecretHere', // Replace 'yourSecretHere' with your actual API secret
};

// Initialize the RestClient with the API credentials
const gateRestClient = new RestClient({
apiKey: account.key,
apiSecret: account.secret,
Expand All @@ -14,21 +16,22 @@ async function getFuturesTicker() {
try {
console.log('Using API keys:', account);

// GET specific ticker
const ticker = await gateRestClient.getFuturesTickers({
settle: 'usdt',
// Fetch all futures tickers with USDT settlement
const allTickers = await gateRestClient.getFuturesTickers({
settle: 'usdt', // Specify the settlement currency
});
console.log('Response: ', ticker);
console.log('Response: ', allTickers); // Log the response to the console

/* GET all tickers */
const allTickers = await gateRestClient.getFuturesTickers({
settle: 'usdt',
contract: 'BTC_USDT',
// Fetch a specific futures ticker with USDT settlement
const ticker = await gateRestClient.getFuturesTickers({
settle: 'usdt', // Specify the settlement currency
contract: 'BTC_USDT', // Specify the contract
});
console.log('Response: ', allTickers);
console.log('Response: ', ticker); // Log the response to the console
} catch (e) {
console.error(`Error in execution: `, e);
console.error(`Error in execution: `, e); // Log any errors that occur
}
}

// Execute the function to get futures tickers
getFuturesTicker();
24 changes: 14 additions & 10 deletions examples/futures/submitLimitOrder.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { RestClient } from '../../src';
import { RestClient } from '../../src'; // Import the RestClient from the src directory

// Define the account object with API key and secret
const account = {
key: process.env.API_KEY || 'yourApiHere',
secret: process.env.API_SECRET || 'yourSecretHere',
key: process.env.API_KEY || 'yourApiHere', // Replace 'yourApiHere' with your actual API key
secret: process.env.API_SECRET || 'yourSecretHere', // Replace 'yourSecretHere' with your actual API secret
};

// Initialize the RestClient with the API credentials
const gateRestClient = new RestClient({
apiKey: account.key,
apiSecret: account.secret,
Expand All @@ -14,18 +16,20 @@ async function submitFuturesOrder() {
try {
console.log('Using API keys:', account);

// Submit a limit order for futures trading
const result = await gateRestClient.submitFuturesOrder({
settle: 'usdt',
contract: 'BTC_USDT',
size: 10, // positive for long, negative for short
price: '45000',
tif: 'gtc',
settle: 'usdt', // Specify the settlement currency
contract: 'BTC_USDT', // Specify the contract
size: 10, // Order size: positive for long, negative for short
price: '45000', // Limit price for the order
tif: 'gtc', // Time in force: 'gtc' (Good Till Cancelled)
});

console.log('Response: ', result);
console.log('Response: ', result); // Log the response to the console
} catch (e) {
console.error(`Error in execution: `, e);
console.error(`Error in execution: `, e); // Log any errors that occur
}
}

// Execute the function to submit a futures order
submitFuturesOrder();
24 changes: 14 additions & 10 deletions examples/futures/submitMarketOrder.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { RestClient } from '../../src';
import { RestClient } from '../../src'; // Import the RestClient from the src directory

// Define the account object with API key and secret
const account = {
key: process.env.API_KEY || 'yourApiHere',
secret: process.env.API_SECRET || 'yourSecretHere',
key: process.env.API_KEY || 'yourApiHere', // Replace 'yourApiHere' with your actual API key
secret: process.env.API_SECRET || 'yourSecretHere', // Replace 'yourSecretHere' with your actual API secret
};

// Initialize the RestClient with the API credentials
const gateRestClient = new RestClient({
apiKey: account.key,
apiSecret: account.secret,
Expand All @@ -14,18 +16,20 @@ async function submitFuturesOrder() {
try {
console.log('Using API keys:', account);

// Submit a market order for futures trading
const result = await gateRestClient.submitFuturesOrder({
settle: 'usdt',
contract: 'BTC_USDT',
size: 20, // positive for long, negative for short
price: '0',
tif: 'ioc',
settle: 'usdt', // Specify the settlement currency
contract: 'BTC_USDT', // Specify the contract
size: 20, // Order size: positive for long, negative for short
price: '0', // Market order, so price is set to '0'
tif: 'ioc', // Time in force: 'ioc' (Immediate Or Cancel)
});

console.log('Response: ', result);
console.log('Response: ', result); // Log the response to the console
} catch (e) {
console.error(`Error in execution: `, e);
console.error(`Error in execution: `, e); // Log any errors that occur
}
}

// Execute the function to submit a futures order
submitFuturesOrder();
108 changes: 27 additions & 81 deletions examples/rest-private.ts
Original file line number Diff line number Diff line change
@@ -1,90 +1,36 @@
import { RestClient } from '../src';
import { RestClient } from '../src'; // Import the RestClient from the src directory

async function start() {
try {
const account = {
key: process.env.API_KEY || 'yourApiHere',
secret: process.env.API_SECRET || 'YourSecretHere',
};

console.log('using creds: ', account);
const rest = new RestClient({
apiKey: account.key,
apiSecret: account.secret,
});
// Define the account object with API key and secret
const account = {
key: process.env.API_KEY || 'yourApiHere', // Replace 'yourApiHere' with your actual API key
secret: process.env.API_SECRET || 'yourSecretHere', // Replace 'yourSecretHere' with your actual API secret
};

/* const res1 = await rest.submitSpotOrder({
currency_pair: 'BTC_USDT',
side: 'buy',
type: 'limit',
amount: '10',
time_in_force: 'gtc',
price: '1',
}); */
// Initialize the RestClient with the API credentials
const gateRestClient = new RestClient({
apiKey: account.key,
apiSecret: account.secret,
});

const res1 = await rest.getDeliveryTradingHistory({
contract: 'BTC_USDT',
settle: 'usdt',
async function restPrivateExample() {
try {
console.log('Using credentials: ', account);

// Submit a limit order for spot trading
const result = await gateRestClient.submitSpotOrder({
currency_pair: 'BTC_USDT', // Specify the currency pair
side: 'buy', // Specify the order side: 'buy' or 'sell'
type: 'limit', // Specify the order type: 'limit'
amount: '0.001', // Specify the amount to buy
price: '45000', // Specify the limit price
time_in_force: 'gtc', // Time in force: 'gtc' (Good Till Cancelled)
});

/* const res2 = await rest.getIndexConstituents({
settle: 'usdt',
index: 'BTC_USDT',
}); */

/* const res3 = await rest.portfolioMarginCalculator({
spot_balances: [
{
currency: 'BTC',
equity: '-1',
},
],
spot_orders: [
{
currency_pairs: 'BTC_USDT',
order_price: '344',
left: '100',
type: 'sell',
},
],
futures_positions: [
{
contract: 'BTC_USDT',
size: '100',
},
],
futures_orders: [
{
contract: 'BTC_USDT',
size: '10',
left: '8',
},
],
options_positions: [
{
options_name: 'BTC_USDT-20240329-32000-C',
size: '10',
},
],
options_orders: [
{
options_name: 'BTC_USDT-20240329-32000-C',
size: '100',
left: '80',
},
],
spot_hedge: false,
}); */

/* const res4 = await rest.getDeliveryContract({
settle: 'usdt',
contract: 'BTC_USDT',
}); */
// const res1 = await rest.getSystemMaintenanceStatus();
console.log('res: ', JSON.stringify(res1, null, 2));
console.log('Result: ', JSON.stringify(result, null, 2)); // Log the result to the console
} catch (e) {
console.error(`Error in execution: `, e);
console.error(`Error in execution: `, e); // Log any errors that occur
}
}

start();
// Execute the function to demonstrate the RestClient usage
restPrivateExample();
17 changes: 10 additions & 7 deletions examples/spot/getBalances.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { RestClient } from '../../src';
import { RestClient } from '../../src'; // Import the RestClient from the src directory

// Define the account object with API key and secret
const account = {
key: process.env.API_KEY || 'yourApiHere',
secret: process.env.API_SECRET || 'yourSecretHere',
key: process.env.API_KEY || 'yourApiHere', // Replace 'yourApiHere' with your actual API key
secret: process.env.API_SECRET || 'yourSecretHere', // Replace 'yourSecretHere' with your actual API secret
};

// Initialize the RestClient with the API credentials
const gateRestClient = new RestClient({
apiKey: account.key,
apiSecret: account.secret,
Expand All @@ -14,12 +16,13 @@ async function getSpotBalances() {
try {
console.log('Using API keys:', account);

// GET specific ticker
const ticker = await gateRestClient.getSpotAccounts();
console.log('Response: ', ticker);
// Fetch the spot account balances
const balances = await gateRestClient.getSpotAccounts();
console.log('Response: ', balances); // Log the response to the console
} catch (e) {
console.error(`Error in execution: `, e);
console.error(`Error in execution: `, e); // Log any errors that occur
}
}

// Execute the function to get spot balances
getSpotBalances();
Loading

0 comments on commit 4124513

Please sign in to comment.