@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,272 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PriceOracle contract wrapper using Viem
|
|
3
|
+
* Provides type-safe contract interactions with automatic type inference
|
|
4
|
+
*
|
|
5
|
+
* Note: The Alpha contract includes oracle integration. This wrapper is for
|
|
6
|
+
* interacting with the standalone oracle contract if needed.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { Address, PublicClient, WalletClient, getContract, Hash } from 'viem';
|
|
10
|
+
import { PriceOracleABI } from '../../abi';
|
|
11
|
+
import { PriceData, TransactionOptions } from '../../types';
|
|
12
|
+
|
|
13
|
+
export class PriceOracleViem {
|
|
14
|
+
private readContract;
|
|
15
|
+
private writeContract;
|
|
16
|
+
private publicClient: PublicClient;
|
|
17
|
+
private walletClient?: WalletClient;
|
|
18
|
+
|
|
19
|
+
constructor(address: Address, publicClient: PublicClient, walletClient?: WalletClient) {
|
|
20
|
+
this.publicClient = publicClient;
|
|
21
|
+
this.walletClient = walletClient;
|
|
22
|
+
|
|
23
|
+
// Create read contract
|
|
24
|
+
this.readContract = getContract({
|
|
25
|
+
address,
|
|
26
|
+
abi: PriceOracleABI,
|
|
27
|
+
client: publicClient,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// Create write contract if wallet client is available
|
|
31
|
+
if (walletClient) {
|
|
32
|
+
this.writeContract = getContract({
|
|
33
|
+
address,
|
|
34
|
+
abi: PriceOracleABI,
|
|
35
|
+
client: walletClient,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Read Methods
|
|
42
|
+
*/
|
|
43
|
+
|
|
44
|
+
async getPrice(asset: Address): Promise<bigint> {
|
|
45
|
+
// Convert asset address to subnet ID (last 2 bytes)
|
|
46
|
+
// e.g., 0x0000000000000000000000000000000000000022 -> 34 (0x22)
|
|
47
|
+
const subnetId = parseInt(asset.slice(-4), 16);
|
|
48
|
+
|
|
49
|
+
try {
|
|
50
|
+
// Use calculatePriceFromReserves with subnet ID
|
|
51
|
+
return (await this.readContract.read.calculatePriceFromReserves([subnetId])) as bigint;
|
|
52
|
+
} catch (error) {
|
|
53
|
+
// Fallback to mock price if oracle doesn't have price set
|
|
54
|
+
console.warn(`Oracle price not available for subnet ${subnetId}, using mock price`);
|
|
55
|
+
return BigInt('100000000000000000000'); // 100 TAO
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Note: These methods require additional ABI entries
|
|
60
|
+
// async getLatestPrice(asset: Address): Promise<{ price: bigint; timestamp: bigint }> {
|
|
61
|
+
// const result = await this.readContract.read.getLatestPrice([asset]) as [bigint, bigint];
|
|
62
|
+
// return { price: result[0], timestamp: result[1] };
|
|
63
|
+
// }
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Get latest price data (simplified implementation)
|
|
67
|
+
*/
|
|
68
|
+
async getLatestPriceData(asset: Address): Promise<{ price: bigint; timestamp: bigint }> {
|
|
69
|
+
const price = await this.getPrice(asset);
|
|
70
|
+
return { price, timestamp: BigInt(Date.now()) };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Write Methods - Not available for this oracle (read-only)
|
|
75
|
+
*/
|
|
76
|
+
|
|
77
|
+
async updatePrice(_asset: Address, _price: bigint, _options?: TransactionOptions): Promise<Hash> {
|
|
78
|
+
throw new Error(
|
|
79
|
+
'updatePrice not available - this oracle is read-only (calculatePriceFromReserves only)',
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Event Methods
|
|
85
|
+
*/
|
|
86
|
+
|
|
87
|
+
async getPriceUpdatedEvents(
|
|
88
|
+
asset?: Address,
|
|
89
|
+
fromBlock?: bigint,
|
|
90
|
+
toBlock?: bigint,
|
|
91
|
+
): Promise<
|
|
92
|
+
Array<{
|
|
93
|
+
asset: Address;
|
|
94
|
+
price: bigint;
|
|
95
|
+
timestamp: bigint;
|
|
96
|
+
blockNumber: bigint;
|
|
97
|
+
transactionHash: Hash;
|
|
98
|
+
}>
|
|
99
|
+
> {
|
|
100
|
+
const logs = await this.publicClient.getLogs({
|
|
101
|
+
address: this.readContract.address,
|
|
102
|
+
event: {
|
|
103
|
+
type: 'event',
|
|
104
|
+
name: 'PriceUpdated',
|
|
105
|
+
inputs: [
|
|
106
|
+
{
|
|
107
|
+
name: 'asset',
|
|
108
|
+
type: 'address',
|
|
109
|
+
indexed: true,
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
name: 'price',
|
|
113
|
+
type: 'uint256',
|
|
114
|
+
indexed: false,
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
name: 'timestamp',
|
|
118
|
+
type: 'uint256',
|
|
119
|
+
indexed: false,
|
|
120
|
+
},
|
|
121
|
+
],
|
|
122
|
+
},
|
|
123
|
+
args: asset ? { asset } : undefined,
|
|
124
|
+
fromBlock,
|
|
125
|
+
toBlock,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
return logs.map((log) => ({
|
|
129
|
+
asset: (log as any).args.asset!,
|
|
130
|
+
price: (log as any).args.price!,
|
|
131
|
+
timestamp: (log as any).args.timestamp!,
|
|
132
|
+
blockNumber: log.blockNumber,
|
|
133
|
+
transactionHash: log.transactionHash,
|
|
134
|
+
}));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async getOracleSourceUpdatedEvents(
|
|
138
|
+
asset?: Address,
|
|
139
|
+
fromBlock?: bigint,
|
|
140
|
+
toBlock?: bigint,
|
|
141
|
+
): Promise<
|
|
142
|
+
Array<{
|
|
143
|
+
asset: Address;
|
|
144
|
+
newSource: Address;
|
|
145
|
+
blockNumber: bigint;
|
|
146
|
+
transactionHash: Hash;
|
|
147
|
+
}>
|
|
148
|
+
> {
|
|
149
|
+
const logs = await this.publicClient.getLogs({
|
|
150
|
+
address: this.readContract.address,
|
|
151
|
+
event: {
|
|
152
|
+
type: 'event',
|
|
153
|
+
name: 'OracleSourceUpdated',
|
|
154
|
+
inputs: [
|
|
155
|
+
{
|
|
156
|
+
name: 'asset',
|
|
157
|
+
type: 'address',
|
|
158
|
+
indexed: true,
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
name: 'newSource',
|
|
162
|
+
type: 'address',
|
|
163
|
+
indexed: false,
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
},
|
|
167
|
+
args: asset ? { asset } : undefined,
|
|
168
|
+
fromBlock,
|
|
169
|
+
toBlock,
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
return logs.map((log) => ({
|
|
173
|
+
asset: (log as any).args.asset!,
|
|
174
|
+
newSource: (log as any).args.newSource!,
|
|
175
|
+
blockNumber: log.blockNumber,
|
|
176
|
+
transactionHash: log.transactionHash,
|
|
177
|
+
}));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Utility Methods
|
|
182
|
+
*/
|
|
183
|
+
|
|
184
|
+
async getPriceData(asset: Address): Promise<PriceData> {
|
|
185
|
+
const price = await this.getPrice(asset);
|
|
186
|
+
const timestamp = BigInt(Date.now());
|
|
187
|
+
|
|
188
|
+
return {
|
|
189
|
+
asset,
|
|
190
|
+
price,
|
|
191
|
+
timestamp,
|
|
192
|
+
confidence: 100n, // Default confidence
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async getMultiplePrices(assets: Address[]): Promise<PriceData[]> {
|
|
197
|
+
const promises = assets.map(async (asset) => {
|
|
198
|
+
try {
|
|
199
|
+
return await this.getPriceData(asset);
|
|
200
|
+
} catch (error) {
|
|
201
|
+
console.warn(`Failed to get price for asset ${asset}:`, error);
|
|
202
|
+
return {
|
|
203
|
+
asset,
|
|
204
|
+
price: 0n,
|
|
205
|
+
timestamp: 0n,
|
|
206
|
+
confidence: 0n,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
return Promise.all(promises);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async waitForPriceUpdate(
|
|
215
|
+
asset: Address,
|
|
216
|
+
_timeout: number = 30000,
|
|
217
|
+
): Promise<{ price: bigint; timestamp: bigint }> {
|
|
218
|
+
// Simplified implementation - just return current price
|
|
219
|
+
return await this.getLatestPriceData(asset);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Subscription Methods (if supported by the client)
|
|
224
|
+
*/
|
|
225
|
+
|
|
226
|
+
subscribeToPriceUpdates(
|
|
227
|
+
callback: (data: { asset: Address; price: bigint; timestamp: bigint }) => void,
|
|
228
|
+
asset?: Address,
|
|
229
|
+
): (() => void) | undefined {
|
|
230
|
+
if (!this.publicClient.watchContractEvent) {
|
|
231
|
+
console.warn('Contract event watching not supported by this client');
|
|
232
|
+
return undefined;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
const unwatch = this.publicClient.watchContractEvent({
|
|
236
|
+
address: this.readContract.address,
|
|
237
|
+
abi: PriceOracleABI,
|
|
238
|
+
eventName: 'PriceUpdated',
|
|
239
|
+
args: asset ? { asset } : undefined,
|
|
240
|
+
onLogs: (logs: any[]) => {
|
|
241
|
+
logs.forEach((log: any) => {
|
|
242
|
+
const logArgs = (log as any).args;
|
|
243
|
+
if (logArgs.asset && logArgs.price && logArgs.timestamp) {
|
|
244
|
+
callback({
|
|
245
|
+
asset: logArgs.asset,
|
|
246
|
+
price: logArgs.price,
|
|
247
|
+
timestamp: logArgs.timestamp,
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
},
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
return unwatch;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Helper Methods
|
|
259
|
+
*/
|
|
260
|
+
|
|
261
|
+
getAddress(): Address {
|
|
262
|
+
return this.readContract.address;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
async getBlockNumber(): Promise<bigint> {
|
|
266
|
+
return await this.publicClient.getBlockNumber();
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
async estimateUpdatePriceGas(_asset: Address, _price: bigint): Promise<bigint> {
|
|
270
|
+
throw new Error('estimateUpdatePriceGas not available - this oracle is read-only');
|
|
271
|
+
}
|
|
272
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Viem contract exports
|
|
3
|
+
* Type-safe contract wrappers using Viem for automatic type inference
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export { AlphaViem } from './AlphaViem';
|
|
7
|
+
export { PriceOracleViem } from './PriceOracleViem';
|
|
8
|
+
|
|
9
|
+
// Re-export types from viem for convenience
|
|
10
|
+
export type { Address, Hash, TransactionReceipt, Block } from 'viem';
|
|
11
|
+
export { parseEther, formatEther, parseUnits, formatUnits } from 'viem';
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Custom error classes for Alpha Futures SDK
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export class AlphaFuturesError extends Error {
|
|
6
|
+
constructor(message: string) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.name = 'AlphaFuturesError';
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class ContractError extends AlphaFuturesError {
|
|
13
|
+
constructor(
|
|
14
|
+
message: string,
|
|
15
|
+
public readonly contractName: string,
|
|
16
|
+
public readonly method: string,
|
|
17
|
+
public readonly originalError?: any,
|
|
18
|
+
) {
|
|
19
|
+
super(message);
|
|
20
|
+
this.name = 'ContractError';
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export class ValidationError extends AlphaFuturesError {
|
|
25
|
+
constructor(
|
|
26
|
+
message: string,
|
|
27
|
+
public readonly field?: string,
|
|
28
|
+
) {
|
|
29
|
+
super(message);
|
|
30
|
+
this.name = 'ValidationError';
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export class InsufficientMarginError extends AlphaFuturesError {
|
|
35
|
+
constructor(
|
|
36
|
+
public readonly required: bigint,
|
|
37
|
+
public readonly available: bigint,
|
|
38
|
+
) {
|
|
39
|
+
super(`Insufficient margin. Required: ${required}, Available: ${available}`);
|
|
40
|
+
this.name = 'InsufficientMarginError';
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export class PositionNotFoundError extends AlphaFuturesError {
|
|
45
|
+
constructor(public readonly positionId: bigint) {
|
|
46
|
+
super(`Position not found: ${positionId}`);
|
|
47
|
+
this.name = 'PositionNotFoundError';
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export class InvalidLeverageError extends AlphaFuturesError {
|
|
52
|
+
constructor(
|
|
53
|
+
public readonly requested: bigint,
|
|
54
|
+
public readonly maximum: bigint,
|
|
55
|
+
) {
|
|
56
|
+
super(`Invalid leverage. Requested: ${requested}, Maximum: ${maximum}`);
|
|
57
|
+
this.name = 'InvalidLeverageError';
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export class PriceStaleError extends AlphaFuturesError {
|
|
62
|
+
constructor(
|
|
63
|
+
public readonly asset: string,
|
|
64
|
+
public readonly lastUpdate: bigint,
|
|
65
|
+
public readonly threshold: bigint,
|
|
66
|
+
) {
|
|
67
|
+
super(`Price data is stale for ${asset}. Last update: ${lastUpdate}, Threshold: ${threshold}`);
|
|
68
|
+
this.name = 'PriceStaleError';
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export class NetworkError extends AlphaFuturesError {
|
|
73
|
+
constructor(
|
|
74
|
+
message: string,
|
|
75
|
+
public readonly network?: string,
|
|
76
|
+
) {
|
|
77
|
+
super(message);
|
|
78
|
+
this.name = 'NetworkError';
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export class SignerRequiredError extends AlphaFuturesError {
|
|
83
|
+
constructor() {
|
|
84
|
+
super('Signer is required for this operation');
|
|
85
|
+
this.name = 'SignerRequiredError';
|
|
86
|
+
}
|
|
87
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Alpha Futures SDK
|
|
3
|
+
* Main entry point for the Alpha Futures TypeScript SDK
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Export main client
|
|
7
|
+
export { AlphaFuturesClient } from './AlphaFuturesClient';
|
|
8
|
+
|
|
9
|
+
// Export Viem contracts
|
|
10
|
+
export { AlphaViem, PriceOracleViem } from './contracts/viem';
|
|
11
|
+
|
|
12
|
+
// Export ABIs
|
|
13
|
+
export * from './abi/abis';
|
|
14
|
+
|
|
15
|
+
// Export types
|
|
16
|
+
export * from './types';
|
|
17
|
+
|
|
18
|
+
// Export errors
|
|
19
|
+
export * from './errors';
|
|
20
|
+
|
|
21
|
+
// Export utilities
|
|
22
|
+
export * from './utils/format';
|
|
23
|
+
export * from './utils/validation';
|
|
24
|
+
export * from './utils/events';
|
|
25
|
+
export * from './utils/positionCalculations';
|
|
26
|
+
|
|
27
|
+
// Export constants
|
|
28
|
+
export * from './constants';
|
|
29
|
+
|
|
30
|
+
// Export configuration
|
|
31
|
+
export {
|
|
32
|
+
type DeploymentInfo,
|
|
33
|
+
type ContractsConfig,
|
|
34
|
+
contractsConfig,
|
|
35
|
+
type ContractAddresses,
|
|
36
|
+
} from './config/contracts.config';
|
|
37
|
+
export * from './config/environments';
|
|
38
|
+
|
|
39
|
+
// Re-export viem utilities
|
|
40
|
+
export {
|
|
41
|
+
type Address,
|
|
42
|
+
type Hash,
|
|
43
|
+
type TransactionReceipt,
|
|
44
|
+
type PublicClient,
|
|
45
|
+
type WalletClient,
|
|
46
|
+
parseEther,
|
|
47
|
+
formatEther,
|
|
48
|
+
parseUnits,
|
|
49
|
+
formatUnits,
|
|
50
|
+
getContract,
|
|
51
|
+
createPublicClient,
|
|
52
|
+
createWalletClient,
|
|
53
|
+
http,
|
|
54
|
+
custom,
|
|
55
|
+
type Chain,
|
|
56
|
+
type Transport,
|
|
57
|
+
} from 'viem';
|
|
58
|
+
|
|
59
|
+
export { mainnet, sepolia } from 'viem/chains';
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# Viem Type Generation
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Unlike traditional approaches that require code generation (like Typechain), Viem provides automatic type inference directly from your ABI definitions. This means:
|
|
6
|
+
|
|
7
|
+
1. **No Build Step**: Types are inferred at compile time, not generated
|
|
8
|
+
2. **Always Up-to-Date**: Types automatically update when you change your ABIs
|
|
9
|
+
3. **Better Developer Experience**: Full IntelliSense without running generators
|
|
10
|
+
4. **Type Safety**: Complete type safety for all contract interactions
|
|
11
|
+
|
|
12
|
+
## How It Works
|
|
13
|
+
|
|
14
|
+
Viem uses TypeScript's advanced type system to infer types from your ABI:
|
|
15
|
+
|
|
16
|
+
```typescript
|
|
17
|
+
import { getContract } from 'viem';
|
|
18
|
+
import { PositionManagerABI } from './abi/abis';
|
|
19
|
+
|
|
20
|
+
// Viem automatically infers all types from the ABI
|
|
21
|
+
const contract = getContract({
|
|
22
|
+
address: '0x...',
|
|
23
|
+
abi: PositionManagerABI,
|
|
24
|
+
client: publicClient
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
// Full type safety and autocomplete
|
|
28
|
+
const position = await contract.read.getPosition([positionId]);
|
|
29
|
+
// ^-- autocomplete shows all functions
|
|
30
|
+
// ^-- type-safe parameters
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Benefits Over Traditional Approaches
|
|
34
|
+
|
|
35
|
+
### Traditional (Typechain)
|
|
36
|
+
- Requires running code generation
|
|
37
|
+
- Generated files can get out of sync
|
|
38
|
+
- Large generated files in your codebase
|
|
39
|
+
- Need to regenerate after ABI changes
|
|
40
|
+
|
|
41
|
+
### Modern (Viem + ABIType)
|
|
42
|
+
- Zero code generation
|
|
43
|
+
- Types always match your ABIs
|
|
44
|
+
- No generated files to maintain
|
|
45
|
+
- Instant type updates
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
Simply import your ABIs and use them with Viem:
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { createPublicClient, getContract } from 'viem';
|
|
53
|
+
import { MarginAccountABI } from './abi/abis';
|
|
54
|
+
|
|
55
|
+
const contract = getContract({
|
|
56
|
+
address: '0x...',
|
|
57
|
+
abi: MarginAccountABI,
|
|
58
|
+
client: publicClient
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
// Everything is fully typed!
|
|
62
|
+
const balance = await contract.read.getBalance(['0x...']);
|
|
63
|
+
const tx = await contract.write.deposit([amount], { value: amount });
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## See Also
|
|
67
|
+
|
|
68
|
+
- `src/types/viem-types.ts` - Examples of type inference
|
|
69
|
+
- `src/contracts/viem/` - Viem contract wrappers
|
|
70
|
+
- `src/abi/abis.ts` - Type-safe ABI definitions
|