@megatao/sdk 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.env.example +37 -0
- package/CHANGELOG.md +19 -0
- package/README.md +199 -0
- package/bin/alf +4 -0
- package/cli/README.md +198 -0
- package/cli/TEST_MANUAL.md +577 -0
- package/cli/commands/account.ts +545 -0
- package/cli/commands/funding.ts +481 -0
- package/cli/commands/liquidation.ts +523 -0
- package/cli/commands/market.ts +590 -0
- package/cli/commands/orders.ts +395 -0
- package/cli/commands/position.ts +1085 -0
- package/cli/commands/shared/positionUtils.ts +239 -0
- package/cli/commands/trading.ts +483 -0
- package/cli/commands/utils.ts +281 -0
- package/cli/commands/vault.ts +522 -0
- package/cli/index.ts +169 -0
- package/cli/interactive.ts +530 -0
- package/cli/utils/client.ts +457 -0
- package/cli/utils/config.ts +226 -0
- package/cli/utils/display.ts +258 -0
- package/cli/utils/index.ts +10 -0
- package/cli/utils/prompts.ts +364 -0
- package/config.example.json +23 -0
- package/dist/AlphaFuturesClient.d.ts +36 -0
- package/dist/AlphaFuturesClient.d.ts.map +1 -0
- package/dist/AlphaFuturesClient.js +116 -0
- package/dist/AlphaFuturesClient.js.map +1 -0
- package/dist/abi/Alpha.json +5987 -0
- package/dist/abi/abis.d.ts +319 -0
- package/dist/abi/abis.d.ts.map +1 -0
- package/dist/abi/abis.js +128 -0
- package/dist/abi/abis.js.map +1 -0
- package/dist/abi/index.d.ts +11 -0
- package/dist/abi/index.d.ts.map +1 -0
- package/dist/abi/index.js +15 -0
- package/dist/abi/index.js.map +1 -0
- package/dist/config/contracts.config.d.ts +70 -0
- package/dist/config/contracts.config.d.ts.map +1 -0
- package/dist/config/contracts.config.js +137 -0
- package/dist/config/contracts.config.js.map +1 -0
- package/dist/config/environments/alpha.config.d.ts +17 -0
- package/dist/config/environments/alpha.config.d.ts.map +1 -0
- package/dist/config/environments/alpha.config.js +140 -0
- package/dist/config/environments/alpha.config.js.map +1 -0
- package/dist/config/environments/beta.config.d.ts +16 -0
- package/dist/config/environments/beta.config.d.ts.map +1 -0
- package/dist/config/environments/beta.config.js +131 -0
- package/dist/config/environments/beta.config.js.map +1 -0
- package/dist/config/environments/dev.config.d.ts +13 -0
- package/dist/config/environments/dev.config.d.ts.map +1 -0
- package/dist/config/environments/dev.config.js +123 -0
- package/dist/config/environments/dev.config.js.map +1 -0
- package/dist/config/environments/index.d.ts +48 -0
- package/dist/config/environments/index.d.ts.map +1 -0
- package/dist/config/environments/index.js +81 -0
- package/dist/config/environments/index.js.map +1 -0
- package/dist/config/environments/localhost.config.d.ts +16 -0
- package/dist/config/environments/localhost.config.d.ts.map +1 -0
- package/dist/config/environments/localhost.config.js +152 -0
- package/dist/config/environments/localhost.config.js.map +1 -0
- package/dist/config/environments/prod.config.d.ts +20 -0
- package/dist/config/environments/prod.config.d.ts.map +1 -0
- package/dist/config/environments/prod.config.js +143 -0
- package/dist/config/environments/prod.config.js.map +1 -0
- package/dist/config/index.d.ts +7 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +41 -0
- package/dist/config/index.js.map +1 -0
- package/dist/constants/assets.d.ts +76 -0
- package/dist/constants/assets.d.ts.map +1 -0
- package/dist/constants/assets.js +277 -0
- package/dist/constants/assets.js.map +1 -0
- package/dist/constants/contracts.d.ts +41 -0
- package/dist/constants/contracts.d.ts.map +1 -0
- package/dist/constants/contracts.js +57 -0
- package/dist/constants/contracts.js.map +1 -0
- package/dist/constants/index.d.ts +36 -0
- package/dist/constants/index.d.ts.map +1 -0
- package/dist/constants/index.js +75 -0
- package/dist/constants/index.js.map +1 -0
- package/dist/constants/networks.d.ts +32 -0
- package/dist/constants/networks.d.ts.map +1 -0
- package/dist/constants/networks.js +174 -0
- package/dist/constants/networks.js.map +1 -0
- package/dist/contracts/index.d.ts +5 -0
- package/dist/contracts/index.d.ts.map +1 -0
- package/dist/contracts/index.js +21 -0
- package/dist/contracts/index.js.map +1 -0
- package/dist/contracts/viem/AlphaViem.d.ts +518 -0
- package/dist/contracts/viem/AlphaViem.d.ts.map +1 -0
- package/dist/contracts/viem/AlphaViem.js +1287 -0
- package/dist/contracts/viem/AlphaViem.js.map +1 -0
- package/dist/contracts/viem/PriceOracleViem.d.ts +71 -0
- package/dist/contracts/viem/PriceOracleViem.d.ts.map +1 -0
- package/dist/contracts/viem/PriceOracleViem.js +212 -0
- package/dist/contracts/viem/PriceOracleViem.js.map +1 -0
- package/dist/contracts/viem/index.d.ts +9 -0
- package/dist/contracts/viem/index.d.ts.map +1 -0
- package/dist/contracts/viem/index.js +17 -0
- package/dist/contracts/viem/index.js.map +1 -0
- package/dist/errors/index.d.ts +44 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +83 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/dist/types/alpha.d.ts +299 -0
- package/dist/types/alpha.d.ts.map +1 -0
- package/dist/types/alpha.js +6 -0
- package/dist/types/alpha.js.map +1 -0
- package/dist/types/client.d.ts +24 -0
- package/dist/types/client.d.ts.map +1 -0
- package/dist/types/client.js +13 -0
- package/dist/types/client.js.map +1 -0
- package/dist/types/contracts.d.ts +48 -0
- package/dist/types/contracts.d.ts.map +1 -0
- package/dist/types/contracts.js +6 -0
- package/dist/types/contracts.js.map +1 -0
- package/dist/types/funding.d.ts +27 -0
- package/dist/types/funding.d.ts.map +1 -0
- package/dist/types/funding.js +6 -0
- package/dist/types/funding.js.map +1 -0
- package/dist/types/index.d.ts +92 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +47 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/liquidation.d.ts +20 -0
- package/dist/types/liquidation.d.ts.map +1 -0
- package/dist/types/liquidation.js +6 -0
- package/dist/types/liquidation.js.map +1 -0
- package/dist/types/margin.d.ts +29 -0
- package/dist/types/margin.d.ts.map +1 -0
- package/dist/types/margin.js +6 -0
- package/dist/types/margin.js.map +1 -0
- package/dist/types/oracle.d.ts +21 -0
- package/dist/types/oracle.d.ts.map +1 -0
- package/dist/types/oracle.js +6 -0
- package/dist/types/oracle.js.map +1 -0
- package/dist/types/positions.d.ts +43 -0
- package/dist/types/positions.d.ts.map +1 -0
- package/dist/types/positions.js +13 -0
- package/dist/types/positions.js.map +1 -0
- package/dist/utils/calculations.d.ts +84 -0
- package/dist/utils/calculations.d.ts.map +1 -0
- package/dist/utils/calculations.js +155 -0
- package/dist/utils/calculations.js.map +1 -0
- package/dist/utils/errors.d.ts +24 -0
- package/dist/utils/errors.d.ts.map +1 -0
- package/dist/utils/errors.js +129 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/events.d.ts +40 -0
- package/dist/utils/events.d.ts.map +1 -0
- package/dist/utils/events.js +73 -0
- package/dist/utils/events.js.map +1 -0
- package/dist/utils/format.d.ts +40 -0
- package/dist/utils/format.d.ts.map +1 -0
- package/dist/utils/format.js +86 -0
- package/dist/utils/format.js.map +1 -0
- package/dist/utils/index.d.ts +10 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +26 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/network.d.ts +52 -0
- package/dist/utils/network.d.ts.map +1 -0
- package/dist/utils/network.js +192 -0
- package/dist/utils/network.js.map +1 -0
- package/dist/utils/positionCalculations.d.ts +145 -0
- package/dist/utils/positionCalculations.d.ts.map +1 -0
- package/dist/utils/positionCalculations.js +278 -0
- package/dist/utils/positionCalculations.js.map +1 -0
- package/dist/utils/validation.d.ts +28 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +68 -0
- package/dist/utils/validation.js.map +1 -0
- package/docs/README.md +40 -0
- package/docs/api/API.md +831 -0
- package/docs/guides/GETTING_STARTED.md +316 -0
- package/docs/guides/TRADING_GUIDE.md +677 -0
- package/docs/integration/INTEGRATION_GUIDE.md +1679 -0
- package/docs/integration/VIEM_INTEGRATION.md +294 -0
- package/docs/reference/CLI_QUICK_REFERENCE.md +197 -0
- package/docs/reference/TROUBLESHOOTING.md +922 -0
- package/package.json +113 -0
- package/src/AlphaFuturesClient.ts +158 -0
- package/src/abi/.gitkeep +1 -0
- package/src/abi/Alpha.json +5987 -0
- package/src/abi/README.md +99 -0
- package/src/abi/abis.ts +131 -0
- package/src/abi/index.ts +13 -0
- package/src/config/contracts.config.ts +186 -0
- package/src/config/environments/alpha.config.ts +139 -0
- package/src/config/environments/beta.config.ts +130 -0
- package/src/config/environments/dev.config.ts +122 -0
- package/src/config/environments/index.ts +87 -0
- package/src/config/environments/localhost.config.ts +153 -0
- package/src/config/environments/prod.config.ts +142 -0
- package/src/config/index.ts +29 -0
- package/src/constants/assets.ts +299 -0
- package/src/constants/contracts.ts +64 -0
- package/src/constants/index.ts +69 -0
- package/src/constants/networks.ts +182 -0
- package/src/contracts/index.ts +5 -0
- package/src/contracts/viem/AlphaViem.ts +1615 -0
- package/src/contracts/viem/PriceOracleViem.ts +272 -0
- package/src/contracts/viem/index.ts +11 -0
- package/src/errors/index.ts +87 -0
- package/src/index.ts +59 -0
- package/src/types/VIEM_TYPES_README.md +70 -0
- package/src/types/alpha.ts +358 -0
- package/src/types/client.ts +27 -0
- package/src/types/contracts.ts +74 -0
- package/src/types/funding.ts +31 -0
- package/src/types/index.ts +108 -0
- package/src/types/liquidation.ts +23 -0
- package/src/types/margin.ts +34 -0
- package/src/types/oracle.ts +24 -0
- package/src/types/positions.ts +48 -0
- package/src/utils/calculations.ts +175 -0
- package/src/utils/errors.ts +147 -0
- package/src/utils/events.ts +98 -0
- package/src/utils/format.ts +84 -0
- package/src/utils/index.ts +10 -0
- package/src/utils/network.ts +212 -0
- package/src/utils/positionCalculations.ts +317 -0
- package/src/utils/validation.ts +76 -0
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
# Viem Integration Guide
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
The Alpha Futures SDK is built with [Viem](https://viem.sh/), a modern TypeScript library for interacting with Ethereum and EVM-compatible blockchains. Viem provides automatic type inference, optimal performance, and an excellent developer experience.
|
|
6
|
+
|
|
7
|
+
## Key Benefits
|
|
8
|
+
|
|
9
|
+
### 1. **Automatic Type Inference**
|
|
10
|
+
- No code generation required
|
|
11
|
+
- Types are inferred directly from ABI definitions
|
|
12
|
+
- Always in sync with your contracts
|
|
13
|
+
|
|
14
|
+
### 2. **Better Performance**
|
|
15
|
+
- Smaller bundle sizes
|
|
16
|
+
- Faster execution
|
|
17
|
+
- Optimized for modern JavaScript environments
|
|
18
|
+
|
|
19
|
+
### 3. **Enhanced Developer Experience**
|
|
20
|
+
- Immediate IntelliSense support
|
|
21
|
+
- Better error messages
|
|
22
|
+
- Cleaner, more intuitive API
|
|
23
|
+
|
|
24
|
+
## Installation
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
npm install @alpha-futures/sdk
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Quick Start
|
|
31
|
+
|
|
32
|
+
### Basic Setup
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import { ViemContractFactory, type ContractAddresses } from '@alpha-futures/sdk';
|
|
36
|
+
|
|
37
|
+
// Define contract addresses
|
|
38
|
+
const contractAddresses: ContractAddresses = {
|
|
39
|
+
positionManager: '0x...',
|
|
40
|
+
marginAccount: '0x...',
|
|
41
|
+
fundingRate: '0x...',
|
|
42
|
+
orderBook: '0x...',
|
|
43
|
+
liquidationEngine: '0x...',
|
|
44
|
+
protocolVault: '0x...'
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// Create factory instance
|
|
48
|
+
const factory = ViemContractFactory.fromPrivateKey(
|
|
49
|
+
contractAddresses,
|
|
50
|
+
process.env.PRIVATE_KEY!,
|
|
51
|
+
process.env.RPC_URL!
|
|
52
|
+
);
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Using with MetaMask or Web3 Provider
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
// From an EIP-1193 provider (like MetaMask)
|
|
59
|
+
const factory = await ViemContractFactory.fromProvider(
|
|
60
|
+
contractAddresses,
|
|
61
|
+
window.ethereum
|
|
62
|
+
);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Manual Client Creation
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { createPublicClient, createWalletClient, http } from 'viem';
|
|
69
|
+
import { privateKeyToAccount } from 'viem/accounts';
|
|
70
|
+
|
|
71
|
+
const publicClient = createPublicClient({
|
|
72
|
+
chain: bittensorEVM,
|
|
73
|
+
transport: http(RPC_URL)
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const walletClient = createWalletClient({
|
|
77
|
+
account: privateKeyToAccount(PRIVATE_KEY as `0x${string}`),
|
|
78
|
+
chain: bittensorEVM,
|
|
79
|
+
transport: http(RPC_URL)
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Contract Interactions
|
|
84
|
+
|
|
85
|
+
### Reading Contract State
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
const positionManager = factory.createPositionManager();
|
|
89
|
+
|
|
90
|
+
// Get position details
|
|
91
|
+
const position = await positionManager.getPosition(positionId);
|
|
92
|
+
console.log('Position size:', position.size);
|
|
93
|
+
console.log('Is Long:', position.isLong);
|
|
94
|
+
|
|
95
|
+
// Get user positions
|
|
96
|
+
const positions = await positionManager.getUserPositions(userAddress);
|
|
97
|
+
console.log('Active positions:', positions);
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Writing to Contracts
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
const marginAccount = factory.createMarginAccount();
|
|
104
|
+
|
|
105
|
+
// Deposit margin
|
|
106
|
+
const depositHash = await marginAccount.deposit(
|
|
107
|
+
parseEther('100'), // amount
|
|
108
|
+
{ value: parseEther('100') } // transaction options
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
// Wait for confirmation
|
|
112
|
+
const receipt = await publicClient.waitForTransactionReceipt({
|
|
113
|
+
hash: depositHash
|
|
114
|
+
});
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Event Monitoring
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
import { watchContractEvent } from 'viem';
|
|
121
|
+
|
|
122
|
+
// Watch for position opened events
|
|
123
|
+
const unwatch = watchContractEvent(publicClient, {
|
|
124
|
+
address: contractAddresses.positionManager,
|
|
125
|
+
abi: PositionManagerABI,
|
|
126
|
+
eventName: 'PositionOpened',
|
|
127
|
+
onLogs: (logs) => {
|
|
128
|
+
logs.forEach(log => {
|
|
129
|
+
console.log('New position:', {
|
|
130
|
+
positionId: log.args.positionId,
|
|
131
|
+
trader: log.args.trader,
|
|
132
|
+
size: log.args.size,
|
|
133
|
+
isLong: log.args.isLong
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
// Stop watching
|
|
140
|
+
unwatch();
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Advanced Usage
|
|
144
|
+
|
|
145
|
+
### Batch Operations
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
// Read multiple values in one call
|
|
149
|
+
const [balance, marginRatio, positions] = await Promise.all([
|
|
150
|
+
marginAccount.getBalance(userAddress),
|
|
151
|
+
marginAccount.getMarginRatio(userAddress),
|
|
152
|
+
positionManager.getUserPositions(userAddress)
|
|
153
|
+
]);
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Gas Estimation
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
// Estimate gas before sending transaction
|
|
160
|
+
const gasEstimate = await positionManager.estimateGas.openPosition(
|
|
161
|
+
asset,
|
|
162
|
+
isLong,
|
|
163
|
+
size,
|
|
164
|
+
margin
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
// Send with custom gas limit
|
|
168
|
+
const hash = await positionManager.openPosition(
|
|
169
|
+
asset,
|
|
170
|
+
isLong,
|
|
171
|
+
size,
|
|
172
|
+
margin,
|
|
173
|
+
{ gas: gasEstimate * 120n / 100n } // 20% buffer
|
|
174
|
+
);
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
### Error Handling
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
import { ContractFunctionExecutionError } from 'viem';
|
|
181
|
+
|
|
182
|
+
try {
|
|
183
|
+
await positionManager.openPosition(...);
|
|
184
|
+
} catch (error) {
|
|
185
|
+
if (error instanceof ContractFunctionExecutionError) {
|
|
186
|
+
console.error('Contract error:', error.shortMessage);
|
|
187
|
+
// Access revert reason
|
|
188
|
+
const reason = error.reason;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
## Type Safety
|
|
194
|
+
|
|
195
|
+
### Automatic Type Inference
|
|
196
|
+
|
|
197
|
+
```typescript
|
|
198
|
+
// Types are automatically inferred from ABIs
|
|
199
|
+
const position = await positionManager.getPosition(1n);
|
|
200
|
+
// position.size is typed as bigint
|
|
201
|
+
// position.isLong is typed as boolean
|
|
202
|
+
// position.trader is typed as Address
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
### Using Type Exports
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
import type { Address, Hash, TransactionReceipt } from '@alpha-futures/sdk';
|
|
209
|
+
|
|
210
|
+
// Use imported types
|
|
211
|
+
const userAddress: Address = '0x...';
|
|
212
|
+
const txHash: Hash = '0x...';
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Best Practices
|
|
216
|
+
|
|
217
|
+
### 1. **Use Type-Safe ABIs**
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
import { PositionManagerABI } from '@alpha-futures/sdk';
|
|
221
|
+
|
|
222
|
+
// ABIs provide full type safety
|
|
223
|
+
const contract = getContract({
|
|
224
|
+
address: contractAddress,
|
|
225
|
+
abi: PositionManagerABI,
|
|
226
|
+
client
|
|
227
|
+
});
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### 2. **Handle BigInt Properly**
|
|
231
|
+
|
|
232
|
+
```typescript
|
|
233
|
+
// Viem uses native BigInt
|
|
234
|
+
const amount = parseEther('100'); // Returns bigint
|
|
235
|
+
const formatted = formatEther(amount); // Returns string
|
|
236
|
+
|
|
237
|
+
// Arithmetic operations
|
|
238
|
+
const doubled = amount * 2n; // Note the 'n' suffix
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
### 3. **Use Proper Error Handling**
|
|
242
|
+
|
|
243
|
+
```typescript
|
|
244
|
+
try {
|
|
245
|
+
const hash = await contract.write.someFunction([...args]);
|
|
246
|
+
const receipt = await publicClient.waitForTransactionReceipt({ hash });
|
|
247
|
+
|
|
248
|
+
if (receipt.status === 'reverted') {
|
|
249
|
+
throw new Error('Transaction reverted');
|
|
250
|
+
}
|
|
251
|
+
} catch (error) {
|
|
252
|
+
// Handle specific error types
|
|
253
|
+
}
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### 4. **Optimize Read Operations**
|
|
257
|
+
|
|
258
|
+
```typescript
|
|
259
|
+
// Use multicall for multiple reads
|
|
260
|
+
const results = await publicClient.multicall({
|
|
261
|
+
contracts: [
|
|
262
|
+
{
|
|
263
|
+
address: marginAccount.address,
|
|
264
|
+
abi: MarginAccountABI,
|
|
265
|
+
functionName: 'getBalance',
|
|
266
|
+
args: [userAddress]
|
|
267
|
+
},
|
|
268
|
+
{
|
|
269
|
+
address: positionManager.address,
|
|
270
|
+
abi: PositionManagerABI,
|
|
271
|
+
functionName: 'getUserPositions',
|
|
272
|
+
args: [userAddress]
|
|
273
|
+
}
|
|
274
|
+
]
|
|
275
|
+
});
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
## Migration from Ethers
|
|
279
|
+
|
|
280
|
+
If you're migrating from Ethers.js:
|
|
281
|
+
|
|
282
|
+
| Ethers.js | Viem |
|
|
283
|
+
|-----------|------|
|
|
284
|
+
| `ethers.utils.parseEther()` | `parseEther()` |
|
|
285
|
+
| `ethers.utils.formatEther()` | `formatEther()` |
|
|
286
|
+
| `BigNumber` | `bigint` |
|
|
287
|
+
| `contract.connect(signer)` | Pass client to contract creation |
|
|
288
|
+
| `await tx.wait()` | `await publicClient.waitForTransactionReceipt({ hash })` |
|
|
289
|
+
|
|
290
|
+
## Resources
|
|
291
|
+
|
|
292
|
+
- [Viem Documentation](https://viem.sh)
|
|
293
|
+
- [Alpha Futures SDK Examples](../../examples)
|
|
294
|
+
- [Contract ABIs](../../src/abi)
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
# Alpha Futures CLI Quick Reference
|
|
2
|
+
|
|
3
|
+
## Installation & Setup
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
# Install globally
|
|
7
|
+
npm install -g @alpha-futures/sdk
|
|
8
|
+
|
|
9
|
+
# Or use npx (no install needed)
|
|
10
|
+
npx @alpha-futures/sdk --help
|
|
11
|
+
|
|
12
|
+
# Configure environment
|
|
13
|
+
cp .env.example .env
|
|
14
|
+
# Edit .env with your private key and RPC URL
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Common Commands
|
|
18
|
+
|
|
19
|
+
### Account Management
|
|
20
|
+
```bash
|
|
21
|
+
alpha-futures account info # Show account summary
|
|
22
|
+
alpha-futures account balance # Check margin balance
|
|
23
|
+
alpha-futures account deposit --amount 1000 # Deposit TAO margin
|
|
24
|
+
alpha-futures account withdraw --amount 500 # Withdraw TAO margin
|
|
25
|
+
alpha-futures account history # View transaction history
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Position Management
|
|
29
|
+
```bash
|
|
30
|
+
alpha-futures position list # List all positions
|
|
31
|
+
alpha-futures position info 0x00000000000000000000000000000001 # Position details
|
|
32
|
+
alpha-futures position open --asset ALPHA --size 1000 --leverage 3 --long # Open long
|
|
33
|
+
alpha-futures position open --asset ALPHA --size 1000 --leverage 3 --short # Open short
|
|
34
|
+
alpha-futures position close 0x00000000000000000000000000000001 # Close position
|
|
35
|
+
alpha-futures position modify 0x00000000000000000000000000000001 --add 200 # Add margin
|
|
36
|
+
alpha-futures position modify 0x00000000000000000000000000000001 --remove 100 # Remove margin
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Market Data
|
|
40
|
+
```bash
|
|
41
|
+
alpha-futures market prices # Current asset prices
|
|
42
|
+
alpha-futures market funding-rates # Current funding rates
|
|
43
|
+
alpha-futures market open-interest # Total open interest
|
|
44
|
+
alpha-futures market stats # Market statistics
|
|
45
|
+
alpha-futures market info --asset ALPHA # Specific asset info
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Trading
|
|
49
|
+
```bash
|
|
50
|
+
alpha-futures trade market --asset ALPHA --size 1000 --long # Market buy
|
|
51
|
+
alpha-futures trade market --asset ALPHA --size 1000 --short # Market sell
|
|
52
|
+
alpha-futures trade limit --asset ALPHA --size 1000 --price 100 --long # Limit order
|
|
53
|
+
alpha-futures trade orders # View open orders
|
|
54
|
+
alpha-futures trade cancel --id 456 # Cancel order
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Liquidations
|
|
58
|
+
```bash
|
|
59
|
+
alpha-futures liquidation check # Check liquidation status
|
|
60
|
+
alpha-futures liquidation at-risk # View at-risk positions
|
|
61
|
+
alpha-futures liquidation execute 0x00000000000000000000000000000001 # Execute liquidation
|
|
62
|
+
alpha-futures liquidation monitor # Real-time monitoring
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Vault Operations
|
|
66
|
+
```bash
|
|
67
|
+
alpha-futures vault stats # Vault statistics
|
|
68
|
+
alpha-futures vault balance # Vault TAO balance
|
|
69
|
+
alpha-futures vault performance # Vault P&L metrics
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Interactive Mode
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
alpha-futures interactive # Launch interactive trading interface
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Features:
|
|
79
|
+
- Visual menus and prompts
|
|
80
|
+
- Real-time price updates
|
|
81
|
+
- Transaction confirmations
|
|
82
|
+
- Input validation
|
|
83
|
+
|
|
84
|
+
## Configuration Options
|
|
85
|
+
|
|
86
|
+
### Command Line Flags
|
|
87
|
+
```bash
|
|
88
|
+
-n, --network <network> # Network: mainnet, testnet, localhost
|
|
89
|
+
-r, --rpc <url> # Custom RPC endpoint
|
|
90
|
+
-k, --key <privateKey> # Private key (not recommended)
|
|
91
|
+
-c, --config <path> # Config file path
|
|
92
|
+
--no-color # Disable colored output
|
|
93
|
+
--json # JSON output format
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Examples with Flags
|
|
97
|
+
```bash
|
|
98
|
+
# Use testnet
|
|
99
|
+
alpha-futures -n testnet position list
|
|
100
|
+
|
|
101
|
+
# Custom RPC
|
|
102
|
+
alpha-futures -r https://your-rpc.com market prices
|
|
103
|
+
|
|
104
|
+
# JSON output
|
|
105
|
+
alpha-futures --json account info > account.json
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Environment Variables
|
|
109
|
+
|
|
110
|
+
```bash
|
|
111
|
+
NETWORK=mainnet # Default network
|
|
112
|
+
RPC_URL=https://... # RPC endpoint
|
|
113
|
+
PRIVATE_KEY=0x... # Wallet private key
|
|
114
|
+
DEFAULT_LEVERAGE=3 # Default leverage
|
|
115
|
+
DEFAULT_SLIPPAGE=0.5 # Default slippage %
|
|
116
|
+
GAS_PRICE_MULTIPLIER=1.1 # Gas price boost
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
## Tips & Shortcuts
|
|
120
|
+
|
|
121
|
+
### Position Shortcuts
|
|
122
|
+
```bash
|
|
123
|
+
# Quick open with defaults
|
|
124
|
+
alpha-futures position open -a ALPHA -s 1000 -l 3 --long
|
|
125
|
+
|
|
126
|
+
# Bulk close all positions
|
|
127
|
+
alpha-futures position list --json | jq '.[] | .id' | xargs -I {} alpha-futures position close --id {}
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Market Monitoring
|
|
131
|
+
```bash
|
|
132
|
+
# Watch funding rates
|
|
133
|
+
watch -n 60 alpha-futures market funding-rates
|
|
134
|
+
|
|
135
|
+
# Monitor your positions
|
|
136
|
+
watch -n 30 alpha-futures position list
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Account Safety
|
|
140
|
+
```bash
|
|
141
|
+
# Check margin ratio
|
|
142
|
+
alpha-futures account info | grep "Margin Ratio"
|
|
143
|
+
|
|
144
|
+
# List positions near liquidation
|
|
145
|
+
alpha-futures liquidation at-risk
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
## Error Codes
|
|
149
|
+
|
|
150
|
+
- `INSUFFICIENT_MARGIN` - Not enough TAO deposited
|
|
151
|
+
- `POSITION_NOT_FOUND` - Invalid position ID
|
|
152
|
+
- `LEVERAGE_TOO_HIGH` - Exceeds maximum leverage
|
|
153
|
+
- `MARKET_CLOSED` - Trading temporarily halted
|
|
154
|
+
- `SLIPPAGE_EXCEEDED` - Price moved beyond tolerance
|
|
155
|
+
|
|
156
|
+
## Getting Help
|
|
157
|
+
|
|
158
|
+
```bash
|
|
159
|
+
alpha-futures --help # General help
|
|
160
|
+
alpha-futures <command> --help # Command-specific help
|
|
161
|
+
alpha-futures help <command> # Alternative help syntax
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
## Examples
|
|
165
|
+
|
|
166
|
+
### Complete Trading Flow
|
|
167
|
+
```bash
|
|
168
|
+
# 1. Check balance
|
|
169
|
+
alpha-futures account balance
|
|
170
|
+
|
|
171
|
+
# 2. Deposit margin if needed
|
|
172
|
+
alpha-futures account deposit --amount 1000
|
|
173
|
+
|
|
174
|
+
# 3. Check market prices
|
|
175
|
+
alpha-futures market prices
|
|
176
|
+
|
|
177
|
+
# 4. Open position
|
|
178
|
+
alpha-futures position open --asset ALPHA --size 500 --leverage 3 --long
|
|
179
|
+
|
|
180
|
+
# 5. Monitor position
|
|
181
|
+
alpha-futures position list
|
|
182
|
+
|
|
183
|
+
# 6. Close with profit/loss
|
|
184
|
+
alpha-futures position close 0x00000000000000000000000000000001
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
### Risk Management
|
|
188
|
+
```bash
|
|
189
|
+
# Set up monitoring
|
|
190
|
+
alpha-futures position list --json > positions.json
|
|
191
|
+
|
|
192
|
+
# Check margin ratios
|
|
193
|
+
cat positions.json | jq '.[] | {id: .id, marginRatio: .marginRatio}'
|
|
194
|
+
|
|
195
|
+
# Add margin to risky positions
|
|
196
|
+
alpha-futures position modify 0x00000000000000000000000000000001 --add 200
|
|
197
|
+
```
|