@kehtus/proportion-sdk 0.1.1 → 0.1.3
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/LICENSE +21 -0
- package/dist/abi/factory.d.ts +1 -0
- package/dist/abi/factory.d.ts.map +1 -0
- package/dist/abi/factory.js +1 -0
- package/dist/abi/factory.js.map +1 -0
- package/dist/abi/forwarder.d.ts +1 -0
- package/dist/abi/forwarder.d.ts.map +1 -0
- package/dist/abi/forwarder.js +1 -0
- package/dist/abi/forwarder.js.map +1 -0
- package/dist/abi/fundedAccount.d.ts +1 -0
- package/dist/abi/fundedAccount.d.ts.map +1 -0
- package/dist/abi/fundedAccount.js +1 -0
- package/dist/abi/fundedAccount.js.map +1 -0
- package/dist/abi/mainVault.d.ts +198 -16
- package/dist/abi/mainVault.d.ts.map +1 -0
- package/dist/abi/mainVault.js +253 -19
- package/dist/abi/mainVault.js.map +1 -0
- package/dist/account.d.ts +3 -2
- package/dist/account.d.ts.map +1 -0
- package/dist/account.js +46 -43
- package/dist/account.js.map +1 -0
- package/dist/base.d.ts +9 -0
- package/dist/base.d.ts.map +1 -0
- package/dist/base.js +19 -0
- package/dist/base.js.map +1 -0
- package/dist/constants.d.ts +5 -4
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +5 -4
- package/dist/constants.js.map +1 -0
- package/dist/factory.d.ts +1 -0
- package/dist/factory.d.ts.map +1 -0
- package/dist/factory.js +56 -10
- package/dist/factory.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -0
- package/dist/indexer.d.ts +8 -3
- package/dist/indexer.d.ts.map +1 -0
- package/dist/indexer.js +81 -136
- package/dist/indexer.js.map +1 -0
- package/dist/sdk.d.ts +1 -0
- package/dist/sdk.d.ts.map +1 -0
- package/dist/sdk.js +1 -0
- package/dist/sdk.js.map +1 -0
- package/dist/types.d.ts +5 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +1 -0
- package/dist/types.js.map +1 -0
- package/dist/utils.d.ts +8 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +10 -0
- package/dist/utils.js.map +1 -0
- package/dist/vault.d.ts +7 -4
- package/dist/vault.d.ts.map +1 -0
- package/dist/vault.js +76 -51
- package/dist/vault.js.map +1 -0
- package/package.json +12 -1
- package/SDK_IMPROVEMENT_PLAN.md +0 -197
- package/arch.md +0 -455
- package/dist/__tests__/account.test.d.ts +0 -1
- package/dist/__tests__/account.test.js +0 -36
- package/dist/__tests__/indexer.test.d.ts +0 -1
- package/dist/__tests__/indexer.test.js +0 -230
- package/dist/__tests__/lifecycle.test.d.ts +0 -1
- package/dist/__tests__/lifecycle.test.js +0 -186
- package/dist/__tests__/reads.test.d.ts +0 -1
- package/dist/__tests__/reads.test.js +0 -184
- package/dist/__tests__/utils.test.d.ts +0 -1
- package/dist/__tests__/utils.test.js +0 -185
- package/src/__tests__/account.test.ts +0 -40
- package/src/__tests__/indexer.test.ts +0 -255
- package/src/__tests__/lifecycle.test.ts +0 -231
- package/src/__tests__/reads.test.ts +0 -208
- package/src/__tests__/utils.test.ts +0 -245
- package/src/abi/factory.ts +0 -417
- package/src/abi/forwarder.ts +0 -373
- package/src/abi/fundedAccount.ts +0 -1296
- package/src/abi/mainVault.ts +0 -1852
- package/src/account.ts +0 -325
- package/src/constants.ts +0 -51
- package/src/factory.ts +0 -157
- package/src/index.ts +0 -16
- package/src/indexer.ts +0 -636
- package/src/sdk.ts +0 -93
- package/src/types.ts +0 -421
- package/src/utils.ts +0 -143
- package/src/vault.ts +0 -479
- package/tsconfig.json +0 -19
- package/vitest.config.ts +0 -8
package/src/account.ts
DELETED
|
@@ -1,325 +0,0 @@
|
|
|
1
|
-
import type { Address, Chain, Hash, PublicClient, WalletClient } from "viem";
|
|
2
|
-
import { fundedAccountAbi } from "./abi/fundedAccount.js";
|
|
3
|
-
import { AccountState, type AccountDetails } from "./types.js";
|
|
4
|
-
|
|
5
|
-
type RawCheckpoint = {
|
|
6
|
-
accountValue: bigint | number;
|
|
7
|
-
timestamp: bigint | number;
|
|
8
|
-
profitable: boolean;
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export function normalizeCheckpoints(
|
|
12
|
-
rawCheckpoints: readonly RawCheckpoint[],
|
|
13
|
-
totalCheckpoints: number,
|
|
14
|
-
checkpointIndex: number,
|
|
15
|
-
): Array<{ accountValue: bigint; timestamp: number; profitable: boolean }> {
|
|
16
|
-
if (totalCheckpoints <= 0) return [];
|
|
17
|
-
|
|
18
|
-
const count = Math.min(totalCheckpoints, rawCheckpoints.length);
|
|
19
|
-
const start = count === rawCheckpoints.length ? checkpointIndex % count : 0;
|
|
20
|
-
|
|
21
|
-
return Array.from({ length: count }, (_, offset) => rawCheckpoints[(start + offset) % count]).map((cp) => ({
|
|
22
|
-
accountValue: BigInt(cp.accountValue),
|
|
23
|
-
timestamp: Number(cp.timestamp),
|
|
24
|
-
profitable: cp.profitable,
|
|
25
|
-
}));
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export class AccountModule {
|
|
29
|
-
constructor(
|
|
30
|
-
private publicClient: PublicClient,
|
|
31
|
-
private walletClient: WalletClient | undefined,
|
|
32
|
-
private chain: Chain,
|
|
33
|
-
) {}
|
|
34
|
-
|
|
35
|
-
// ── READS ───────────────────────────────────────────
|
|
36
|
-
|
|
37
|
-
async getDetails(account: Address): Promise<AccountDetails> {
|
|
38
|
-
const contract = { address: account, abi: fundedAccountAbi } as const;
|
|
39
|
-
const results = await this.publicClient.multicall({
|
|
40
|
-
contracts: [
|
|
41
|
-
{ ...contract, functionName: "state" },
|
|
42
|
-
{ ...contract, functionName: "owner" },
|
|
43
|
-
{ ...contract, functionName: "builder" },
|
|
44
|
-
{ ...contract, functionName: "accountSize" },
|
|
45
|
-
{ ...contract, functionName: "drawdownBps" },
|
|
46
|
-
{ ...contract, functionName: "dailyDrawdownEnabled" },
|
|
47
|
-
{ ...contract, functionName: "subscriptionFeeMultiplier" },
|
|
48
|
-
{ ...contract, functionName: "highWaterMark" },
|
|
49
|
-
{ ...contract, functionName: "dayStartValue" },
|
|
50
|
-
{ ...contract, functionName: "activatedAt" },
|
|
51
|
-
{ ...contract, functionName: "subscriptionPaidUntil" },
|
|
52
|
-
{ ...contract, functionName: "subscriptionCancelled" },
|
|
53
|
-
{ ...contract, functionName: "pendingSubscriptionFee" },
|
|
54
|
-
{ ...contract, functionName: "apiWallet" },
|
|
55
|
-
{ ...contract, functionName: "checkpointIndex" },
|
|
56
|
-
{ ...contract, functionName: "profitableDaysCount" },
|
|
57
|
-
{ ...contract, functionName: "totalCheckpoints" },
|
|
58
|
-
{ ...contract, functionName: "lastCheckpointDay" },
|
|
59
|
-
{ ...contract, functionName: "closingInitiatedAt" },
|
|
60
|
-
{ ...contract, functionName: "returnInitiatedAt" },
|
|
61
|
-
{ ...contract, functionName: "pendingWithdrawal" },
|
|
62
|
-
{ ...contract, functionName: "getTrailingFloor" },
|
|
63
|
-
{ ...contract, functionName: "getDailyFloor" },
|
|
64
|
-
{ ...contract, functionName: "getSubscriptionFee" },
|
|
65
|
-
{ ...contract, functionName: "isSubscriptionActive" },
|
|
66
|
-
{ ...contract, functionName: "canWithdrawProfit" },
|
|
67
|
-
{ ...contract, functionName: "getAllCheckpoints" },
|
|
68
|
-
],
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
const checkpointIndex = results[14].result!;
|
|
72
|
-
const totalCp = results[16].result!;
|
|
73
|
-
const profit = results[20].result! as bigint;
|
|
74
|
-
const rawCheckpoints = results[26].result!;
|
|
75
|
-
const checkpoints = normalizeCheckpoints(rawCheckpoints, totalCp, checkpointIndex);
|
|
76
|
-
|
|
77
|
-
return {
|
|
78
|
-
address: account,
|
|
79
|
-
state: results[0].result! as AccountState,
|
|
80
|
-
owner: results[1].result!,
|
|
81
|
-
builder: results[2].result!,
|
|
82
|
-
accountSize: results[3].result!,
|
|
83
|
-
drawdownBps: results[4].result!,
|
|
84
|
-
dailyDrawdownEnabled: results[5].result!,
|
|
85
|
-
subscriptionFeeMultiplier: results[6].result!,
|
|
86
|
-
highWaterMark: results[7].result!,
|
|
87
|
-
dayStartValue: results[8].result!,
|
|
88
|
-
activatedAt: results[9].result!,
|
|
89
|
-
subscriptionPaidUntil: results[10].result!,
|
|
90
|
-
subscriptionCancelled: results[11].result!,
|
|
91
|
-
pendingSubscriptionFee: results[12].result!,
|
|
92
|
-
apiWallet: results[13].result!,
|
|
93
|
-
checkpointIndex,
|
|
94
|
-
profitableDaysCount: results[15].result!,
|
|
95
|
-
totalCheckpoints: totalCp,
|
|
96
|
-
lastCheckpointDay: results[17].result!,
|
|
97
|
-
closingInitiatedAt: Number(results[18].result!),
|
|
98
|
-
returnInitiatedAt: Number(results[19].result!),
|
|
99
|
-
pendingWithdrawal: { profit },
|
|
100
|
-
trailingFloor: results[21].result!,
|
|
101
|
-
dailyFloor: results[22].result!,
|
|
102
|
-
subscriptionFee: results[23].result!,
|
|
103
|
-
isSubscriptionActive: results[24].result!,
|
|
104
|
-
canWithdrawProfit: results[25].result!,
|
|
105
|
-
checkpoints,
|
|
106
|
-
};
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
async getState(account: Address): Promise<AccountState> {
|
|
110
|
-
const raw = await this.publicClient.readContract({
|
|
111
|
-
address: account,
|
|
112
|
-
abi: fundedAccountAbi,
|
|
113
|
-
functionName: "state",
|
|
114
|
-
});
|
|
115
|
-
return raw as AccountState;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
async getTrailingFloor(account: Address): Promise<bigint> {
|
|
119
|
-
return this.publicClient.readContract({
|
|
120
|
-
address: account,
|
|
121
|
-
abi: fundedAccountAbi,
|
|
122
|
-
functionName: "getTrailingFloor",
|
|
123
|
-
});
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
async getDailyFloor(account: Address): Promise<bigint> {
|
|
127
|
-
return this.publicClient.readContract({
|
|
128
|
-
address: account,
|
|
129
|
-
abi: fundedAccountAbi,
|
|
130
|
-
functionName: "getDailyFloor",
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
async canWithdrawProfit(account: Address): Promise<boolean> {
|
|
135
|
-
return this.publicClient.readContract({
|
|
136
|
-
address: account,
|
|
137
|
-
abi: fundedAccountAbi,
|
|
138
|
-
functionName: "canWithdrawProfit",
|
|
139
|
-
});
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
async isSubscriptionActive(account: Address): Promise<boolean> {
|
|
143
|
-
return this.publicClient.readContract({
|
|
144
|
-
address: account,
|
|
145
|
-
abi: fundedAccountAbi,
|
|
146
|
-
functionName: "isSubscriptionActive",
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
async getCheckpoints(
|
|
151
|
-
account: Address,
|
|
152
|
-
): Promise<Array<{ accountValue: bigint; timestamp: number; profitable: boolean }>> {
|
|
153
|
-
const [all, total, checkpointIndex] = await Promise.all([
|
|
154
|
-
this.publicClient.readContract({
|
|
155
|
-
address: account,
|
|
156
|
-
abi: fundedAccountAbi,
|
|
157
|
-
functionName: "getAllCheckpoints",
|
|
158
|
-
}),
|
|
159
|
-
this.publicClient.readContract({
|
|
160
|
-
address: account,
|
|
161
|
-
abi: fundedAccountAbi,
|
|
162
|
-
functionName: "totalCheckpoints",
|
|
163
|
-
}),
|
|
164
|
-
this.publicClient.readContract({
|
|
165
|
-
address: account,
|
|
166
|
-
abi: fundedAccountAbi,
|
|
167
|
-
functionName: "checkpointIndex",
|
|
168
|
-
}),
|
|
169
|
-
]);
|
|
170
|
-
return normalizeCheckpoints(all, Number(total), Number(checkpointIndex));
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
async getPendingWithdrawal(account: Address): Promise<{ profit: bigint }> {
|
|
174
|
-
const profit = await this.publicClient.readContract({
|
|
175
|
-
address: account,
|
|
176
|
-
abi: fundedAccountAbi,
|
|
177
|
-
functionName: "pendingWithdrawal",
|
|
178
|
-
}) as bigint;
|
|
179
|
-
return { profit };
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
async getAccountConfig(account: Address): Promise<{
|
|
183
|
-
usdc: Address;
|
|
184
|
-
vault: Address;
|
|
185
|
-
subscriptionFeeMultiplier: number;
|
|
186
|
-
}> {
|
|
187
|
-
const contract = { address: account, abi: fundedAccountAbi } as const;
|
|
188
|
-
const results = await this.publicClient.multicall({
|
|
189
|
-
contracts: [
|
|
190
|
-
{ ...contract, functionName: "usdc" },
|
|
191
|
-
{ ...contract, functionName: "vault" },
|
|
192
|
-
{ ...contract, functionName: "subscriptionFeeMultiplier" },
|
|
193
|
-
],
|
|
194
|
-
});
|
|
195
|
-
return {
|
|
196
|
-
usdc: results[0].result!,
|
|
197
|
-
vault: results[1].result!,
|
|
198
|
-
subscriptionFeeMultiplier: results[2].result!,
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
// ── WRITES ──────────────────────────────────────────
|
|
203
|
-
|
|
204
|
-
private requireWallet(): WalletClient {
|
|
205
|
-
if (!this.walletClient) throw new Error("WalletClient required for write operations");
|
|
206
|
-
return this.walletClient;
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
210
|
-
private async write(params: Record<string, unknown>): Promise<Hash> {
|
|
211
|
-
const wallet = this.requireWallet();
|
|
212
|
-
return wallet.writeContract({ chain: this.chain, ...params } as any);
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// ── OWNER WRITES ────────────────────────────────────
|
|
216
|
-
|
|
217
|
-
async setApiWallet(account: Address, wallet: Address): Promise<Hash> {
|
|
218
|
-
return this.write({
|
|
219
|
-
address: account,
|
|
220
|
-
abi: fundedAccountAbi,
|
|
221
|
-
functionName: "setApiWallet",
|
|
222
|
-
args: [wallet],
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
async requestWithdrawProfit(account: Address): Promise<Hash> {
|
|
227
|
-
return this.write({
|
|
228
|
-
address: account,
|
|
229
|
-
abi: fundedAccountAbi,
|
|
230
|
-
functionName: "requestWithdrawProfit",
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
async cancelSubscription(account: Address): Promise<Hash> {
|
|
235
|
-
return this.write({
|
|
236
|
-
address: account,
|
|
237
|
-
abi: fundedAccountAbi,
|
|
238
|
-
functionName: "cancelSubscription",
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// ── PERMISSIONLESS WRITES ───────────────────────────
|
|
243
|
-
|
|
244
|
-
async checkpoint(account: Address): Promise<Hash> {
|
|
245
|
-
return this.write({
|
|
246
|
-
address: account,
|
|
247
|
-
abi: fundedAccountAbi,
|
|
248
|
-
functionName: "checkpoint",
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
async initiateClose(account: Address): Promise<Hash> {
|
|
253
|
-
return this.write({
|
|
254
|
-
address: account,
|
|
255
|
-
abi: fundedAccountAbi,
|
|
256
|
-
functionName: "initiateClose",
|
|
257
|
-
});
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
async initiateCloseForPerp(account: Address, perpIndex: number): Promise<Hash> {
|
|
261
|
-
return this.write({
|
|
262
|
-
address: account,
|
|
263
|
-
abi: fundedAccountAbi,
|
|
264
|
-
functionName: "initiateClose",
|
|
265
|
-
args: [perpIndex],
|
|
266
|
-
});
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
async initiateCloseAdmin(account: Address): Promise<Hash> {
|
|
270
|
-
return this.write({
|
|
271
|
-
address: account,
|
|
272
|
-
abi: fundedAccountAbi,
|
|
273
|
-
functionName: "initiateCloseAdmin",
|
|
274
|
-
});
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
async closeAndReturn(account: Address): Promise<Hash> {
|
|
278
|
-
return this.write({
|
|
279
|
-
address: account,
|
|
280
|
-
abi: fundedAccountAbi,
|
|
281
|
-
functionName: "closeAndReturn",
|
|
282
|
-
});
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
async bridgeToEvm(account: Address): Promise<Hash> {
|
|
286
|
-
return this.write({
|
|
287
|
-
address: account,
|
|
288
|
-
abi: fundedAccountAbi,
|
|
289
|
-
functionName: "bridgeToEvm",
|
|
290
|
-
});
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
async finalizeReturn(account: Address): Promise<Hash> {
|
|
294
|
-
return this.write({
|
|
295
|
-
address: account,
|
|
296
|
-
abi: fundedAccountAbi,
|
|
297
|
-
functionName: "finalizeReturn",
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
async claimWithdrawal(account: Address): Promise<Hash> {
|
|
302
|
-
return this.write({
|
|
303
|
-
address: account,
|
|
304
|
-
abi: fundedAccountAbi,
|
|
305
|
-
functionName: "claimWithdrawal",
|
|
306
|
-
});
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
async claimSubscriptionFee(account: Address): Promise<Hash> {
|
|
310
|
-
return this.write({
|
|
311
|
-
address: account,
|
|
312
|
-
abi: fundedAccountAbi,
|
|
313
|
-
functionName: "claimSubscriptionFee",
|
|
314
|
-
});
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
async rescueStuckFunds(account: Address): Promise<Hash> {
|
|
318
|
-
return this.write({
|
|
319
|
-
address: account,
|
|
320
|
-
abi: fundedAccountAbi,
|
|
321
|
-
functionName: "rescueStuckFunds",
|
|
322
|
-
});
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
}
|
package/src/constants.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import type { Chain } from "viem";
|
|
2
|
-
|
|
3
|
-
export const CHAIN_ID = 999;
|
|
4
|
-
|
|
5
|
-
export const hyperEvm = {
|
|
6
|
-
id: 999,
|
|
7
|
-
name: "HyperEVM",
|
|
8
|
-
nativeCurrency: { name: "HYPE", symbol: "HYPE", decimals: 18 },
|
|
9
|
-
rpcUrls: { default: { http: ["https://rpc.hyperliquid.xyz/evm"] } },
|
|
10
|
-
contracts: {
|
|
11
|
-
multicall3: { address: "0xcA11bde05977b3631167028862bE2a173976CA11" },
|
|
12
|
-
},
|
|
13
|
-
} as const satisfies Chain;
|
|
14
|
-
|
|
15
|
-
export const ADDRESSES = {
|
|
16
|
-
MAIN_VAULT: "0xA89F85eC9350e64295B199ee8011454d891e19B1",
|
|
17
|
-
FACTORY: "0xE056F0D2A476CCd9A24230C7DB7c9d6AF44a0161",
|
|
18
|
-
FORWARDER: "0x4fDc2489dE19BECC9FAeA59672F377bf740E4ecF",
|
|
19
|
-
USDC: "0xb88339CB7199b77E23DB6E890353E22632Ba630f",
|
|
20
|
-
} as const;
|
|
21
|
-
|
|
22
|
-
// BPS
|
|
23
|
-
export const BPS = 10_000n;
|
|
24
|
-
export const MIN_DRAWDOWN_BPS = 500;
|
|
25
|
-
export const MAX_DRAWDOWN_BPS = 2500;
|
|
26
|
-
export const DAILY_DD_DISCOUNT_BPS = 2000n;
|
|
27
|
-
export const PROTOCOL_FEE_BPS = 1000n;
|
|
28
|
-
export const OWNER_PROFIT_BPS = 8000n;
|
|
29
|
-
export const PROTOCOL_PROFIT_BPS = 700n;
|
|
30
|
-
export const BUILDER_LEVERAGE = 10n;
|
|
31
|
-
export const MAX_UTILIZATION_BPS = 8000n;
|
|
32
|
-
export const MAX_SINGLE_ACCOUNT_BPS = 1000n;
|
|
33
|
-
export const HYPE_FOR_GAS = 1_000_000_000_000_000n; // 0.001 ether
|
|
34
|
-
|
|
35
|
-
// Decimals
|
|
36
|
-
export const USDC_DECIMALS = 6;
|
|
37
|
-
export const ACCOUNT_VALUE_DECIMALS = 8; // spotBalance, spotSend
|
|
38
|
-
export const DECIMALS_MULTIPLIER = 100n; // 6dec→8dec (used for spotSend conversion)
|
|
39
|
-
// Note: accountValue, HWM, dayStartValue, floors are all 6dec (accountMarginSummary returns 6dec)
|
|
40
|
-
|
|
41
|
-
// Time (seconds)
|
|
42
|
-
export const SUBSCRIPTION_PERIOD = 30 * 86400;
|
|
43
|
-
export const BRIDGE_DELAY = 30;
|
|
44
|
-
export const CLOSING_TIMEOUT = 3 * 86400;
|
|
45
|
-
export const CHECKPOINT_INTERVAL = 86400;
|
|
46
|
-
|
|
47
|
-
// Trading rules
|
|
48
|
-
export const TRADING_DAYS_REQUIRED = 7;
|
|
49
|
-
export const PROFITABLE_DAYS_REQUIRED = 3;
|
|
50
|
-
export const MIN_PROFIT_BPS = 50;
|
|
51
|
-
export const MAX_LEVERAGE = 5;
|
package/src/factory.ts
DELETED
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
import type { Address, PublicClient } from "viem";
|
|
2
|
-
import { factoryAbi } from "./abi/factory.js";
|
|
3
|
-
|
|
4
|
-
export class FactoryModule {
|
|
5
|
-
constructor(
|
|
6
|
-
private publicClient: PublicClient,
|
|
7
|
-
private factoryAddress: Address,
|
|
8
|
-
) {}
|
|
9
|
-
|
|
10
|
-
private isOutOfBoundsError(error: unknown): boolean {
|
|
11
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
12
|
-
return /out of bounds|execution reverted|returned no data|position out of bounds/i.test(message);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
private async accountExistsAt(index: bigint): Promise<boolean> {
|
|
16
|
-
try {
|
|
17
|
-
await this.getAccountAt(index);
|
|
18
|
-
return true;
|
|
19
|
-
} catch (error) {
|
|
20
|
-
if (this.isOutOfBoundsError(error)) return false;
|
|
21
|
-
throw error;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async predictAddress(owner: Address, builder: Address): Promise<Address> {
|
|
26
|
-
return this.publicClient.readContract({
|
|
27
|
-
address: this.factoryAddress,
|
|
28
|
-
abi: factoryAbi,
|
|
29
|
-
functionName: "predictFundedAccountAddress",
|
|
30
|
-
args: [owner, builder],
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async getAccountAt(index: bigint): Promise<Address> {
|
|
35
|
-
return this.publicClient.readContract({
|
|
36
|
-
address: this.factoryAddress,
|
|
37
|
-
abi: factoryAbi,
|
|
38
|
-
functionName: "allAccounts",
|
|
39
|
-
args: [index],
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
async getAccountIndex(account: Address): Promise<bigint> {
|
|
44
|
-
return this.publicClient.readContract({
|
|
45
|
-
address: this.factoryAddress,
|
|
46
|
-
abi: factoryAbi,
|
|
47
|
-
functionName: "allAccountsIndex",
|
|
48
|
-
args: [account],
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
async hasAccount(account: Address): Promise<boolean> {
|
|
53
|
-
try {
|
|
54
|
-
const index = await this.getAccountIndex(account);
|
|
55
|
-
const candidate = await this.getAccountAt(index);
|
|
56
|
-
return candidate.toLowerCase() === account.toLowerCase();
|
|
57
|
-
} catch {
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
async findAccountIndex(account: Address): Promise<bigint | null> {
|
|
63
|
-
const index = await this.getAccountIndex(account);
|
|
64
|
-
const candidate = await this.getAccountAt(index).catch(() => null);
|
|
65
|
-
if (!candidate || candidate.toLowerCase() !== account.toLowerCase()) return null;
|
|
66
|
-
return index;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
async getTraderBuilderNonce(owner: Address, builder: Address): Promise<bigint> {
|
|
70
|
-
return this.publicClient.readContract({
|
|
71
|
-
address: this.factoryAddress,
|
|
72
|
-
abi: factoryAbi,
|
|
73
|
-
functionName: "traderBuilderNonce",
|
|
74
|
-
args: [owner, builder],
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
async getAccountsCount(maxScan: bigint = 10_000n): Promise<bigint> {
|
|
79
|
-
if (!(await this.accountExistsAt(0n))) return 0n;
|
|
80
|
-
|
|
81
|
-
let lo = 0n;
|
|
82
|
-
let hi = 1n;
|
|
83
|
-
|
|
84
|
-
while (hi < maxScan && await this.accountExistsAt(hi)) {
|
|
85
|
-
lo = hi;
|
|
86
|
-
hi *= 2n;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
if (hi >= maxScan && await this.accountExistsAt(hi)) {
|
|
90
|
-
throw new Error(`Factory account count exceeds scan limit ${maxScan}`);
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
while (lo + 1n < hi) {
|
|
94
|
-
const mid = (lo + hi) / 2n;
|
|
95
|
-
if (await this.accountExistsAt(mid)) {
|
|
96
|
-
lo = mid;
|
|
97
|
-
} else {
|
|
98
|
-
hi = mid;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return lo + 1n;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
async getAccounts(offset: bigint, limit: bigint): Promise<Address[]> {
|
|
106
|
-
if (limit <= 0n) return [];
|
|
107
|
-
|
|
108
|
-
const contracts = [];
|
|
109
|
-
for (let i = 0n; i < limit; i++) {
|
|
110
|
-
contracts.push({
|
|
111
|
-
address: this.factoryAddress,
|
|
112
|
-
abi: factoryAbi,
|
|
113
|
-
functionName: "allAccounts",
|
|
114
|
-
args: [offset + i],
|
|
115
|
-
} as const);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const results = await this.publicClient.multicall({
|
|
119
|
-
contracts,
|
|
120
|
-
allowFailure: true,
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
const accounts: Address[] = [];
|
|
124
|
-
for (const result of results) {
|
|
125
|
-
if (result.status !== "success") break;
|
|
126
|
-
accounts.push(result.result);
|
|
127
|
-
}
|
|
128
|
-
return accounts;
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
async getConfig(): Promise<{
|
|
132
|
-
implementation: Address;
|
|
133
|
-
owner: Address;
|
|
134
|
-
pendingOwner: Address;
|
|
135
|
-
usdc: Address;
|
|
136
|
-
vault: Address;
|
|
137
|
-
}> {
|
|
138
|
-
const contract = { address: this.factoryAddress, abi: factoryAbi } as const;
|
|
139
|
-
const results = await this.publicClient.multicall({
|
|
140
|
-
contracts: [
|
|
141
|
-
{ ...contract, functionName: "implementation" },
|
|
142
|
-
{ ...contract, functionName: "owner" },
|
|
143
|
-
{ ...contract, functionName: "pendingOwner" },
|
|
144
|
-
{ ...contract, functionName: "usdc" },
|
|
145
|
-
{ ...contract, functionName: "vault" },
|
|
146
|
-
],
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
return {
|
|
150
|
-
implementation: results[0].result!,
|
|
151
|
-
owner: results[1].result!,
|
|
152
|
-
pendingOwner: results[2].result!,
|
|
153
|
-
usdc: results[3].result!,
|
|
154
|
-
vault: results[4].result!,
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
}
|
package/src/index.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
// Main
|
|
2
|
-
export { ProportionSDK } from "./sdk.js";
|
|
3
|
-
|
|
4
|
-
// Modules
|
|
5
|
-
export { VaultModule } from "./vault.js";
|
|
6
|
-
export { AccountModule } from "./account.js";
|
|
7
|
-
export { FactoryModule } from "./factory.js";
|
|
8
|
-
export { IndexerModule } from "./indexer.js";
|
|
9
|
-
|
|
10
|
-
// Core
|
|
11
|
-
export * from "./types.js";
|
|
12
|
-
export * from "./constants.js";
|
|
13
|
-
export * from "./utils.js";
|
|
14
|
-
export { mainVaultAbi } from "./abi/mainVault.js";
|
|
15
|
-
export { fundedAccountAbi } from "./abi/fundedAccount.js";
|
|
16
|
-
export { factoryAbi } from "./abi/factory.js";
|