forked from Axionvera/axionvera-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoriginal_contractHelpers_true.ts
More file actions
208 lines (182 loc) · 14.9 KB
/
Copy pathoriginal_contractHelpers_true.ts
File metadata and controls
208 lines (182 loc) · 14.9 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import type { StellarNetwork } from "@/utils/networkConfig";
import { withApiResilience, withErrorHandling, safeApiCall, ApiCallOptions } from "./apiResilience";
export type VaultTxType = "deposit" | "withdraw" | "claim";
export type VaultTxStatus = "pending" | "success" | "failed";
export type VaultTx = {
id: string;
type: VaultTxType;
amount: string;
status: VaultTxStatus;
createdAt: string;
hash?: string;
};
export type VaultBalances = {
balance: string;
rewards: string;
};
export type AxionveraVaultSdk = {
getBalances: (args: { walletAddress: string; network: StellarNetwork }, options?: ApiCallOptions) => Promise<VaultBalances>;
getTransactions: (args: { walletAddress: string; network: StellarNetwork }, options?: ApiCallOptions) => Promise<VaultTx[]>;
deposit: (args: { walletAddress: string; network: StellarNetwork; amount: string }, options?: ApiCallOptions) => Promise<VaultTx>;
withdraw: (args: { walletAddress: string; network: StellarNetwork; amount: string }, options?: ApiCallOptions) => Promise<VaultTx>;
claimRewards: (args: { walletAddress: string; network: StellarNetwork }, options?: ApiCallOptions) => Promise<VaultTx>;
};
export function shortenAddress(address: string, chars = 6) {
if (!address) return "";
if (address.length <= chars * 2 + 3) return address;
return `${address.slice(0, chars)}...${address.slice(-chars)}`;
}
export function formatAmount(amount: string) {
const n = Number(amount);
if (!Number.isFinite(n)) return amount;
return new Intl.NumberFormat(undefined, { maximumFractionDigits: 7 }).format(n);
}
export function parsePositiveAmount(input: string) {
const trimmed = input.trim();
const value = Number(trimmed);
if (!Number.isFinite(value) || value <= 0) return null;
return trimmed;
}
function getStorageKey(walletAddress: string, network: StellarNetwork) {
return `axionvera:vault:${network}:${walletAddress}`;
}
type StoredVault = {
balance: string;
rewards: string;
txs: VaultTx[];
};
function sleep(ms: number) {
return new Promise<void>((resolve) => setTimeout(resolve, ms));
}
function createId() {
if (typeof crypto !== "undefined" && "randomUUID" in crypto) return crypto.randomUUID();
return `${Date.now()}-${Math.random().toString(16).slice(2)}`;
}
function loadVault(walletAddress: string, network: StellarNetwork): StoredVault {
if (typeof window === "undefined") return { balance: "0", rewards: "0", txs: [] };
const raw = window.localStorage.getItem(getStorageKey(walletAddress, network));
if (!raw) return { balance: "0", rewards: "0", txs: [] };
try {
const parsed = JSON.parse(raw) as StoredVault;
return {
balance: typeof parsed.balance === "string" ? parsed.balance : "0",
rewards: typeof parsed.rewards === "string" ? parsed.rewards : "0",
txs: Array.isArray(parsed.txs) ? parsed.txs : []
};
} catch {
return { balance: "0", rewards: "0", txs: [] };
}
}
function saveVault(walletAddress: string, network: StellarNetwork, vault: StoredVault) {
if (typeof window === "undefined") return;
window.localStorage.setItem(getStorageKey(walletAddress, network), JSON.stringify(vault));
}
function toFixedString(n: number) {
return n.toString();
}
export function createAxionveraVaultSdk(): AxionveraVaultSdk {
// Base implementations without resilience
const baseSdk = {
async getBalances({ walletAddress, network }: { walletAddress: string; network: StellarNetwork }) {
await sleep(150);
const vault = loadVault(walletAddress, network);
return { balance: vault.balance, rewards: vault.rewards };
},
async getTransactions({ walletAddress, network }: { walletAddress: string; network: StellarNetwork }) {
await sleep(150);
const vault = loadVault(walletAddress, network);
return vault.txs;
},
async deposit({ walletAddress, network, amount }: { walletAddress: string; network: StellarNetwork; amount: string }) {
const tx: VaultTx = {
id: createId(),
type: "deposit",
amount,
status: "pending",
createdAt: new Date().toISOString()
};
const vault = loadVault(walletAddress, network);
vault.txs = [tx, ...vault.txs].slice(0, 25);
saveVault(walletAddress, network, vault);
await sleep(450);
const balance = Number(vault.balance) + Number(amount);
const rewards = Number(vault.rewards) + Number(amount) * 0.01;
const completed: VaultTx = { ...tx, status: "success", hash: `SIM-${createId()}` };
const next: StoredVault = {
balance: toFixedString(balance),
rewards: toFixedString(rewards),
txs: [completed, ...vault.txs.filter((t) => t.id !== tx.id)].slice(0, 25)
};
saveVault(walletAddress, network, next);
return completed;
},
async withdraw({ walletAddress, network, amount }: { walletAddress: string; network: StellarNetwork; amount: string }) {
const tx: VaultTx = {
id: createId(),
type: "withdraw",
amount,
status: "pending",
createdAt: new Date().toISOString()
};
const vault = loadVault(walletAddress, network);
vault.txs = [tx, ...vault.txs].slice(0, 25);
saveVault(walletAddress, network, vault);
await sleep(450);
const balance = Math.max(0, Number(vault.balance) - Number(amount));
const completed: VaultTx = { ...tx, status: "success", hash: `SIM-${createId()}` };
const next: StoredVault = {
balance: toFixedString(balance),
rewards: vault.rewards,
txs: [completed, ...vault.txs.filter((t) => t.id !== tx.id)].slice(0, 25)
};
saveVault(walletAddress, network, next);
return completed;
},
async claimRewards({ walletAddress, network }: { walletAddress: string; network: StellarNetwork }) {
const vault = loadVault(walletAddress, network);
const amount = vault.rewards;
const tx: VaultTx = {
id: createId(),
type: "claim",
amount,
status: "pending",
createdAt: new Date().toISOString()
};
vault.txs = [tx, ...vault.txs].slice(0, 25);
saveVault(walletAddress, network, vault);
await sleep(450);
const balance = Number(vault.balance) + Number(vault.rewards);
const completed: VaultTx = { ...tx, status: "success", hash: `SIM-${createId()}` };
const next: StoredVault = {
balance: toFixedString(balance),
rewards: "0",
txs: [completed, ...vault.txs.filter((t) => t.id !== tx.id)].slice(0, 25)
};
saveVault(walletAddress, network, next);
return completed;
}
};
// Wrap all methods with resilience and error handling
return {
getBalances: withErrorHandling(
withApiResilience(baseSdk.getBalances, { timeout: 5000, retries: 2 }),
'getBalances'
),
getTransactions: withErrorHandling(
withApiResilience(baseSdk.getTransactions, { timeout: 5000, retries: 2 }),
'getTransactions'
),
deposit: withErrorHandling(
withApiResilience(baseSdk.deposit, { timeout: 10000, retries: 1 }),
'deposit'
),
withdraw: withErrorHandling(
withApiResilience(baseSdk.withdraw, { timeout: 10000, retries: 1 }),
'withdraw'
),
claimRewards: withErrorHandling(
withApiResilience(baseSdk.claimRewards, { timeout: 10000, retries: 1 }),
'claimRewards'
)
};
}