-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhip3-trading.ts
More file actions
165 lines (137 loc) · 5.32 KB
/
hip3-trading.ts
File metadata and controls
165 lines (137 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* HIP-3 Assets Trading Example
*
* Demonstrates how to:
* - Fetch HIP-3 perp DEXes
* - List all HIP-3 assets
* - Trade pre-IPO stocks and commodities
* - Work with DEX-specific features
*/
import { GdexClient, HLAsset } from '@gdex/sdk';
const client = new GdexClient({
apiKey: process.env.GDEX_API_KEY!,
});
const USER_ID = 'your-user-id';
const SIGNATURE = 'your-signature';
const WALLET = '0xYourWalletAddress';
async function main() {
console.log('=== HIP-3 Assets Trading Example ===\n');
// 1. Fetch all perp DEXes
console.log('1. Available HIP-3 Perp DEXes:');
const dexes = await client.getPerpDexes();
dexes.forEach(dex => {
console.log(` ${dex.name.toUpperCase()}: ${dex.fullName || dex.name}`);
});
console.log('');
// 2. Fetch all assets
console.log('2. Fetching all assets...');
const assets = await client.getAssets();
// 3. Filter HIP-3 assets
const hip3Assets = assets.filter(a => client.isHip3Asset(a.coin));
console.log(` Total assets: ${assets.length}`);
console.log(` HIP-3 assets: ${hip3Assets.length}\n`);
// 4. Group by DEX
console.log('3. HIP-3 Assets by DEX:');
const byDex = hip3Assets.reduce((acc, asset) => {
const { dex } = client.parseCoinName(asset.coin);
if (!acc[dex]) acc[dex] = [];
acc[dex].push(asset);
return acc;
}, {} as Record<string, HLAsset[]>);
Object.entries(byDex).forEach(([dex, assets]) => {
console.log(`\n ${dex.toUpperCase()} DEX (${assets.length} assets):`);
// Show first 5 assets
assets.slice(0, 5).forEach(a => {
const price = a.markPx ? `$${parseFloat(a.markPx).toFixed(2)}` : 'N/A';
console.log(` ${a.baseCoin}: ${price} (${a.maxLeverage}x max)`);
});
if (assets.length > 5) {
console.log(` ... and ${assets.length - 5} more`);
}
});
console.log('');
// 5. Find specific HIP-3 assets
console.log('4. Finding interesting HIP-3 assets...');
// Find SPACEX on Ventuals
const spacex = hip3Assets.find(a =>
a.baseCoin.toUpperCase() === 'SPACEX' ||
a.coin.toLowerCase().includes('spacex')
);
// Find GOLD
const gold = hip3Assets.find(a =>
a.baseCoin.toUpperCase() === 'GOLD' ||
a.coin.toLowerCase().includes('gold')
);
if (spacex) {
console.log(`\n Found SPACEX: ${spacex.coin}`);
console.log(` Price: $${spacex.markPx}`);
console.log(` Max Leverage: ${spacex.maxLeverage}x`);
console.log(` Isolated Only: ${spacex.onlyIsolated}`);
}
if (gold) {
console.log(`\n Found GOLD: ${gold.coin}`);
console.log(` Price: $${gold.markPx}`);
console.log(` Max Leverage: ${gold.maxLeverage}x`);
}
console.log('');
// 6. Trade a HIP-3 asset (SPACEX or first available)
const targetAsset = spacex || hip3Assets[0];
if (targetAsset) {
const { dex, baseCoin } = client.parseCoinName(targetAsset.coin);
console.log(`5. Trading ${baseCoin} on ${dex.toUpperCase()} DEX...`);
const result = await client.createOrder(USER_ID, SIGNATURE, {
coin: targetAsset.coin, // Use full coin name: "vntl:SPACEX"
isLong: true,
price: targetAsset.markPx || '100',
size: '1',
isMarket: true,
});
if (result.isSuccess) {
const status = result.retData?.response?.data?.statuses[0];
if (status?.filled) {
console.log(` ✅ Filled ${baseCoin} at $${status.filled.avgPx}`);
} else if (status?.resting) {
console.log(` 📋 Order placed, ID: ${status.resting.oid}`);
}
} else {
console.log(` ❌ Failed: ${result.error}`);
}
console.log('');
}
// 7. View HIP-3 positions
console.log('6. Checking HIP-3 positions...');
const state = await client.getAccountState(WALLET);
if (state) {
const hip3Positions = state.assetPositions.filter(p =>
client.isHip3Asset(p.position.coin)
);
if (hip3Positions.length > 0) {
console.log(` Found ${hip3Positions.length} HIP-3 position(s):`);
hip3Positions.forEach(({ position }) => {
const { dex, baseCoin } = client.parseCoinName(position.coin);
const side = parseFloat(position.szi) > 0 ? 'LONG' : 'SHORT';
const size = Math.abs(parseFloat(position.szi));
console.log(`\n ${baseCoin} on ${dex.toUpperCase()} (${side})`);
console.log(` Size: ${size}`);
console.log(` Entry: $${position.entryPx}`);
console.log(` PnL: $${position.unrealizedPnl}`);
});
} else {
console.log(' No HIP-3 positions found');
}
}
console.log('');
// 8. Demonstrate utility functions
console.log('7. Utility function examples:');
console.log('\n isHip3Asset():');
console.log(` client.isHip3Asset('BTC') = ${client.isHip3Asset('BTC')}`);
console.log(` client.isHip3Asset('vntl:SPACEX') = ${client.isHip3Asset('vntl:SPACEX')}`);
console.log('\n parseCoinName():');
const parsed = client.parseCoinName('xyz:GOLD');
console.log(` client.parseCoinName('xyz:GOLD') = { dex: '${parsed.dex}', baseCoin: '${parsed.baseCoin}' }`);
console.log('\n buildCoinName():');
console.log(` client.buildCoinName('vntl', 'SPACEX') = '${client.buildCoinName('vntl', 'SPACEX')}'`);
console.log(` client.buildCoinName('', 'BTC') = '${client.buildCoinName('', 'BTC')}'`);
console.log('\n=== Demo Complete ===');
}
main().catch(console.error);