@wireio/stake 0.2.3 → 0.2.4
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/lib/stake.browser.js +270 -96
- package/lib/stake.browser.js.map +1 -1
- package/lib/stake.d.ts +176 -84
- package/lib/stake.js +282 -102
- package/lib/stake.js.map +1 -1
- package/lib/stake.m.js +270 -96
- package/lib/stake.m.js.map +1 -1
- package/package.json +1 -1
- package/src/networks/solana/clients/token.client.ts +72 -98
- package/src/networks/solana/solana.ts +284 -184
- package/src/networks/solana/utils.ts +209 -5
- package/src/types.ts +68 -30
package/package.json
CHANGED
|
@@ -1,54 +1,54 @@
|
|
|
1
1
|
import { AnchorProvider, BN, Program } from '@coral-xyz/anchor';
|
|
2
|
-
import type { TransactionInstruction } from '@solana/web3.js';
|
|
2
|
+
import type { TransactionInstruction, Connection } from '@solana/web3.js';
|
|
3
3
|
import { PublicKey, SystemProgram } from '@solana/web3.js';
|
|
4
4
|
import {
|
|
5
5
|
TOKEN_2022_PROGRAM_ID,
|
|
6
6
|
ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
7
7
|
} from '@solana/spl-token';
|
|
8
8
|
|
|
9
|
-
import { buildOutpostAccounts, type OutpostAccounts } from '../utils';
|
|
9
|
+
import { buildOutpostAccounts, type OutpostAccounts, buildSolanaTrancheSnapshot } from '../utils';
|
|
10
10
|
import { SolanaProgramService } from '../program';
|
|
11
11
|
import { LiqsolCore } from '../../../assets/solana/types/liqsol_core';
|
|
12
|
-
import {
|
|
12
|
+
import {
|
|
13
|
+
GlobalState,
|
|
14
|
+
PriceHistory,
|
|
15
|
+
TrancheState,
|
|
16
|
+
UserWarrantRecord,
|
|
17
|
+
WalletLike,
|
|
18
|
+
WireReceipt,
|
|
19
|
+
} from '../types';
|
|
13
20
|
import { derivePriceHistoryPda } from '../constants';
|
|
21
|
+
import type { } from '../types';
|
|
22
|
+
import { ChainID, SolChainID } from '@wireio/core';
|
|
23
|
+
import { PurchaseAsset, PurchaseQuote, TrancheSnapshot } from '../../../types';
|
|
14
24
|
|
|
15
|
-
/**
|
|
16
|
-
* Client for interacting with the Pretoken (Outpost) program on Solana.
|
|
17
|
-
*
|
|
18
|
-
* Provides account fetching and instruction building for pretoken operations.
|
|
19
|
-
* Does NOT send or confirm transactions; keeps SDK composable.
|
|
20
|
-
*
|
|
21
|
-
* TODO: Update to $WIRE Token implementation Post-Launch
|
|
22
|
-
*/
|
|
23
25
|
export class TokenClient {
|
|
24
26
|
private readonly program: Program<LiqsolCore>;
|
|
25
27
|
|
|
26
|
-
get wallet(): WalletLike {
|
|
28
|
+
get wallet(): WalletLike {
|
|
29
|
+
return this.provider.wallet;
|
|
30
|
+
}
|
|
27
31
|
|
|
28
32
|
constructor(private readonly provider: AnchorProvider) {
|
|
29
33
|
const svc = new SolanaProgramService(provider);
|
|
30
34
|
this.program = svc.getProgram('liqsolCore');
|
|
31
35
|
}
|
|
32
36
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
+
// ---------------------------------------------------------------------------
|
|
38
|
+
// Account helpers
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
|
|
37
41
|
async getAccounts(user: PublicKey): Promise<OutpostAccounts> {
|
|
38
42
|
return buildOutpostAccounts(this.provider.connection, user);
|
|
39
43
|
}
|
|
40
44
|
|
|
41
|
-
/**
|
|
42
|
-
* Lightweight, UI-friendly snapshot fetchers.
|
|
43
|
-
* (No decoding assumptions beyond what Anchor already provides.)
|
|
44
|
-
*/
|
|
45
45
|
async fetchGlobalState(): Promise<GlobalState> {
|
|
46
|
-
const { globalState } = await this.getAccounts(this.
|
|
46
|
+
const { globalState } = await this.getAccounts(this.wallet.publicKey);
|
|
47
47
|
return this.program.account.globalState.fetch(globalState);
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
async fetchTrancheState(): Promise<TrancheState> {
|
|
51
|
-
const { trancheState } = await this.getAccounts(this.
|
|
51
|
+
const { trancheState } = await this.getAccounts(this.wallet.publicKey);
|
|
52
52
|
return this.program.account.trancheState.fetch(trancheState);
|
|
53
53
|
}
|
|
54
54
|
|
|
@@ -63,28 +63,26 @@ export class TokenClient {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
// ---------------------------------------------------------------------------
|
|
66
|
-
//
|
|
66
|
+
// Tranche snapshot / quoting
|
|
67
67
|
// ---------------------------------------------------------------------------
|
|
68
68
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
69
|
+
// ---------------------------------------------------------------------------
|
|
70
|
+
// Instruction builders (no send / confirm)
|
|
71
|
+
// ---------------------------------------------------------------------------
|
|
72
|
+
|
|
73
|
+
async buildPurchaseWithSolIx(
|
|
74
|
+
amountLamports: bigint,
|
|
75
|
+
user = this.wallet.publicKey,
|
|
76
|
+
): Promise<TransactionInstruction> {
|
|
75
77
|
const a = await this.getAccounts(user);
|
|
76
78
|
|
|
77
79
|
return this.program.methods
|
|
78
80
|
.purchaseWithSol(new BN(amountLamports.toString()))
|
|
79
81
|
.accounts({
|
|
80
|
-
// signer
|
|
81
82
|
user: a.user,
|
|
82
|
-
|
|
83
|
-
// core state
|
|
84
83
|
liqsolMint: a.liqsolMint,
|
|
85
84
|
globalState: a.globalState,
|
|
86
85
|
|
|
87
|
-
// liqSOL pool + distribution plumbing
|
|
88
86
|
poolAuthority: a.poolAuthority,
|
|
89
87
|
liqsolPoolAta: a.liqsolPoolAta,
|
|
90
88
|
liqsolPoolUserRecord: a.poolUserRecord,
|
|
@@ -94,67 +92,53 @@ export class TokenClient {
|
|
|
94
92
|
bucketTokenAccount: a.bucketTokenAccount,
|
|
95
93
|
solBucket: a.solBucket,
|
|
96
94
|
|
|
97
|
-
//
|
|
95
|
+
// IDL name is warrantDepositRecord (wireReceipt PDA)
|
|
98
96
|
warrantDepositRecord: a.wireReceipt,
|
|
99
97
|
|
|
100
|
-
// pretoken state
|
|
101
98
|
trancheState: a.trancheState,
|
|
102
99
|
userWarrantRecord: a.userWarrantRecord,
|
|
103
100
|
|
|
104
|
-
// Chainlink (IDL names are chainlinkFeed / chainlinkProgram in old utils)
|
|
105
101
|
chainlinkFeed: a.chainLinkFeed,
|
|
106
102
|
chainlinkProgram: a.chainLinkProgram,
|
|
107
103
|
|
|
108
|
-
// programs
|
|
109
104
|
tokenProgram: TOKEN_2022_PROGRAM_ID,
|
|
110
105
|
systemProgram: SystemProgram.programId,
|
|
111
106
|
})
|
|
112
107
|
.instruction();
|
|
113
108
|
}
|
|
114
109
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
*/
|
|
120
|
-
async buildPurchaseWithLiqsolIx(amountLamports: bigint, user = this.wallet.publicKey): Promise<TransactionInstruction> {
|
|
110
|
+
async buildPurchaseWithLiqsolIx(
|
|
111
|
+
amountLamports: bigint,
|
|
112
|
+
user = this.wallet.publicKey,
|
|
113
|
+
): Promise<TransactionInstruction> {
|
|
121
114
|
const a = await this.getAccounts(user);
|
|
122
115
|
|
|
123
116
|
return this.program.methods
|
|
124
117
|
.purchaseWithLiqsol(new BN(amountLamports.toString()))
|
|
125
118
|
.accounts({
|
|
126
|
-
// signer
|
|
127
119
|
user: a.user,
|
|
128
|
-
|
|
129
|
-
// core state
|
|
130
120
|
liqsolMint: a.liqsolMint,
|
|
131
121
|
globalState: a.globalState,
|
|
132
122
|
|
|
133
|
-
|
|
134
|
-
buyerAta: a.userAta, // Token-2022 ATA for user
|
|
123
|
+
buyerAta: a.userAta,
|
|
135
124
|
poolAuthority: a.poolAuthority,
|
|
136
125
|
liqsolPoolAta: a.liqsolPoolAta,
|
|
137
126
|
|
|
138
|
-
// IMPORTANT: IDL name (not wireReceipt)
|
|
139
127
|
warrantDepositRecord: a.wireReceipt,
|
|
140
128
|
|
|
141
|
-
|
|
142
|
-
liqsolPoolUserRecord: a.poolUserRecord, // pool user_record PDA
|
|
129
|
+
liqsolPoolUserRecord: a.poolUserRecord,
|
|
143
130
|
distributionState: a.distributionState,
|
|
144
131
|
payRateHistory: a.payRateHistory,
|
|
145
132
|
bucketAuthority: a.bucketAuthority,
|
|
146
133
|
bucketTokenAccount: a.bucketTokenAccount,
|
|
147
134
|
solBucket: a.solBucket,
|
|
148
135
|
|
|
149
|
-
// pretoken state
|
|
150
136
|
trancheState: a.trancheState,
|
|
151
137
|
userWarrantRecord: a.userWarrantRecord,
|
|
152
138
|
|
|
153
|
-
// Chainlink
|
|
154
139
|
chainlinkFeed: a.chainLinkFeed,
|
|
155
140
|
chainlinkProgram: a.chainLinkProgram,
|
|
156
141
|
|
|
157
|
-
// programs
|
|
158
142
|
tokenProgram: TOKEN_2022_PROGRAM_ID,
|
|
159
143
|
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
160
144
|
systemProgram: SystemProgram.programId,
|
|
@@ -162,21 +146,15 @@ export class TokenClient {
|
|
|
162
146
|
.instruction();
|
|
163
147
|
}
|
|
164
148
|
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
* No amount arg; it consumes tracked yield according to on-chain rules.
|
|
169
|
-
*/
|
|
170
|
-
async buildPurchaseFromYieldIx(user = this.wallet.publicKey): Promise<TransactionInstruction> {
|
|
149
|
+
async buildPurchaseFromYieldIx(
|
|
150
|
+
user = this.wallet.publicKey,
|
|
151
|
+
): Promise<TransactionInstruction> {
|
|
171
152
|
const a = await this.getAccounts(user);
|
|
172
153
|
|
|
173
154
|
return this.program.methods
|
|
174
155
|
.purchaseWarrantsFromYield()
|
|
175
156
|
.accounts({
|
|
176
|
-
// signer
|
|
177
157
|
user: a.user,
|
|
178
|
-
|
|
179
|
-
// core state
|
|
180
158
|
globalState: a.globalState,
|
|
181
159
|
liqsolMint: a.liqsolMint,
|
|
182
160
|
poolAuthority: a.poolAuthority,
|
|
@@ -184,68 +162,64 @@ export class TokenClient {
|
|
|
184
162
|
solBucket: a.solBucket,
|
|
185
163
|
|
|
186
164
|
liqsolPoolUserRecord: a.poolUserRecord,
|
|
187
|
-
|
|
188
165
|
distributionState: a.distributionState,
|
|
189
166
|
payRateHistory: a.payRateHistory,
|
|
190
167
|
bucketAuthority: a.bucketAuthority,
|
|
191
168
|
bucketTokenAccount: a.bucketTokenAccount,
|
|
192
169
|
|
|
193
|
-
// programs
|
|
194
|
-
tokenProgram: TOKEN_2022_PROGRAM_ID,
|
|
195
|
-
systemProgram: SystemProgram.programId,
|
|
196
|
-
|
|
197
|
-
// pretoken state + chainlink (per old utils)
|
|
198
170
|
trancheState: a.trancheState,
|
|
199
171
|
userWarrantRecord: a.userWarrantRecord,
|
|
200
172
|
chainlinkFeed: a.chainLinkFeed,
|
|
201
173
|
chainlinkProgram: a.chainLinkProgram,
|
|
174
|
+
|
|
175
|
+
tokenProgram: TOKEN_2022_PROGRAM_ID,
|
|
176
|
+
systemProgram: SystemProgram.programId,
|
|
202
177
|
})
|
|
203
178
|
.instruction();
|
|
204
179
|
}
|
|
205
180
|
|
|
181
|
+
// ---------------------------------------------------------------------------
|
|
182
|
+
// Price helpers (SOL/USD via priceHistory account)
|
|
183
|
+
// ---------------------------------------------------------------------------
|
|
206
184
|
|
|
207
|
-
|
|
185
|
+
async getSolPriceUsdSafe(): Promise<{ price?: bigint; timestamp?: number }> {
|
|
186
|
+
try {
|
|
187
|
+
const price = await this.getSolPriceUsd();
|
|
188
|
+
// current priceHistory account has no timestamp; keep optional
|
|
189
|
+
return { price, timestamp: undefined };
|
|
190
|
+
} catch {
|
|
191
|
+
return { price: undefined, timestamp: undefined };
|
|
192
|
+
}
|
|
193
|
+
}
|
|
208
194
|
|
|
209
195
|
/**
|
|
210
|
-
* Fetch
|
|
211
|
-
*
|
|
212
|
-
* This uses the same ring-buffer semantics as the on-chain program:
|
|
213
|
-
* the latest price is the entry just before `nextIndex`.
|
|
196
|
+
* Fetch latest SOL/USD price (1e8 scale) from liqsol_core.priceHistory.
|
|
197
|
+
* Uses the ring-buffer semantics from earlier SDK code.
|
|
214
198
|
*/
|
|
215
|
-
async getSolPriceUsd(): Promise<
|
|
199
|
+
async getSolPriceUsd(): Promise<bigint> {
|
|
216
200
|
const priceHistoryPda = derivePriceHistoryPda();
|
|
201
|
+
const history = (await this.program.account.priceHistory.fetch(
|
|
202
|
+
priceHistoryPda,
|
|
203
|
+
)) as PriceHistory;
|
|
217
204
|
|
|
218
|
-
const
|
|
219
|
-
priceHistoryPda
|
|
220
|
-
);
|
|
221
|
-
|
|
222
|
-
console.log('PRICE HISTORY', history);
|
|
205
|
+
const { prices, nextIndex, count, windowSize } = history;
|
|
223
206
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
if (!prices || prices.length === 0 || count === 0) {
|
|
227
|
-
throw new Error("Price history is empty – no SOL price available");
|
|
207
|
+
if (!prices || prices.length === 0 || !count) {
|
|
208
|
+
throw new Error('Price history is empty – no SOL price available');
|
|
228
209
|
}
|
|
229
210
|
|
|
230
|
-
// Use the actual buffer length as capacity (should match windowSize)
|
|
231
211
|
const capacity = prices.length || windowSize;
|
|
232
|
-
if (capacity
|
|
233
|
-
throw new Error(
|
|
212
|
+
if (!capacity) {
|
|
213
|
+
throw new Error('Price history capacity is zero – check account layout');
|
|
234
214
|
}
|
|
235
215
|
|
|
236
|
-
|
|
237
|
-
const
|
|
238
|
-
nextIndex === 0
|
|
239
|
-
? capacity - 1
|
|
240
|
-
: nextIndex - 1;
|
|
241
|
-
|
|
242
|
-
const priceUsd = prices[lastIndex];
|
|
216
|
+
const lastIndex = nextIndex === 0 ? capacity - 1 : nextIndex - 1;
|
|
217
|
+
const latest = prices[lastIndex];
|
|
243
218
|
|
|
244
|
-
if (!BN.isBN(
|
|
245
|
-
throw new Error(
|
|
219
|
+
if (!BN.isBN(latest)) {
|
|
220
|
+
throw new Error('Latest price entry is not a BN – check IDL/decoder');
|
|
246
221
|
}
|
|
247
222
|
|
|
248
|
-
|
|
249
|
-
return priceUsd;
|
|
223
|
+
return BigInt(latest.toString());
|
|
250
224
|
}
|
|
251
225
|
}
|