@epicentral/sos-sdk 0.1.1 → 0.2.1
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/accounts/fetchers.ts +0 -9
- package/accounts/list.ts +0 -13
- package/accounts/pdas.ts +0 -16
- package/accounts/resolve-option.ts +104 -0
- package/generated/accounts/index.ts +0 -1
- package/generated/accounts/optionPool.ts +5 -1
- package/generated/accounts/writerPosition.ts +19 -13
- package/generated/instructions/claimTheta.ts +375 -0
- package/generated/instructions/index.ts +1 -3
- package/generated/programs/optionProgram.ts +301 -50
- package/index.ts +2 -1
- package/long/builders.ts +143 -1
- package/package.json +3 -1
- package/shared/transactions.ts +47 -3
- package/short/builders.ts +158 -2
- package/short/claim-theta.ts +38 -0
- package/short/pool.ts +0 -81
- package/tsconfig.json +2 -1
package/accounts/fetchers.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import {
|
|
2
2
|
getCollateralPoolDecoder,
|
|
3
3
|
getLenderPositionDecoder,
|
|
4
|
-
getMakerPoolShareDecoder,
|
|
5
4
|
getMarketDataAccountDecoder,
|
|
6
5
|
getOptionAccountDecoder,
|
|
7
6
|
getOptionPoolDecoder,
|
|
@@ -11,7 +10,6 @@ import {
|
|
|
11
10
|
getWriterPositionDecoder,
|
|
12
11
|
type CollateralPool,
|
|
13
12
|
type LenderPosition,
|
|
14
|
-
type MakerPoolShare,
|
|
15
13
|
type MarketDataAccount,
|
|
16
14
|
type OptionAccount,
|
|
17
15
|
type OptionPool,
|
|
@@ -104,13 +102,6 @@ export async function fetchMarketDataAccount(
|
|
|
104
102
|
return decodeAccount(rpc, marketData, getMarketDataAccountDecoder());
|
|
105
103
|
}
|
|
106
104
|
|
|
107
|
-
export async function fetchMakerPoolShare(
|
|
108
|
-
rpc: KitRpc,
|
|
109
|
-
makerPoolShare: AddressLike
|
|
110
|
-
): Promise<MakerPoolShare | null> {
|
|
111
|
-
return decodeAccount(rpc, makerPoolShare, getMakerPoolShareDecoder());
|
|
112
|
-
}
|
|
113
|
-
|
|
114
105
|
export async function fetchPoolLoan(
|
|
115
106
|
rpc: KitRpc,
|
|
116
107
|
poolLoan: AddressLike
|
package/accounts/list.ts
CHANGED
|
@@ -1,19 +1,16 @@
|
|
|
1
1
|
import bs58 from "bs58";
|
|
2
2
|
import type { Address } from "@solana/kit";
|
|
3
3
|
import {
|
|
4
|
-
MAKER_POOL_SHARE_DISCRIMINATOR,
|
|
5
4
|
OPTION_POOL_DISCRIMINATOR,
|
|
6
5
|
POOL_LOAN_DISCRIMINATOR,
|
|
7
6
|
POSITION_ACCOUNT_DISCRIMINATOR,
|
|
8
7
|
VAULT_DISCRIMINATOR,
|
|
9
8
|
WRITER_POSITION_DISCRIMINATOR,
|
|
10
|
-
getMakerPoolShareDecoder,
|
|
11
9
|
getOptionPoolDecoder,
|
|
12
10
|
getPoolLoanDecoder,
|
|
13
11
|
getPositionAccountDecoder,
|
|
14
12
|
getVaultDecoder,
|
|
15
13
|
getWriterPositionDecoder,
|
|
16
|
-
type MakerPoolShare,
|
|
17
14
|
type OptionPool,
|
|
18
15
|
type PoolLoan,
|
|
19
16
|
type PositionAccount,
|
|
@@ -91,16 +88,6 @@ async function fetchAndDecodeProgramAccounts<T>(
|
|
|
91
88
|
});
|
|
92
89
|
}
|
|
93
90
|
|
|
94
|
-
export async function fetchMakerPoolSharesByMaker(
|
|
95
|
-
rpc: KitRpc,
|
|
96
|
-
maker: AddressLike
|
|
97
|
-
): Promise<Array<ListedAccount<MakerPoolShare>>> {
|
|
98
|
-
return fetchAndDecodeProgramAccounts(rpc, getMakerPoolShareDecoder(), [
|
|
99
|
-
discriminatorFilter(MAKER_POOL_SHARE_DISCRIMINATOR),
|
|
100
|
-
ownerFilter(maker),
|
|
101
|
-
]);
|
|
102
|
-
}
|
|
103
|
-
|
|
104
91
|
export async function fetchWriterPositionsByWriter(
|
|
105
92
|
rpc: KitRpc,
|
|
106
93
|
writer: AddressLike
|
package/accounts/pdas.ts
CHANGED
|
@@ -159,22 +159,6 @@ export async function deriveMakerCollateralSharePda(
|
|
|
159
159
|
});
|
|
160
160
|
}
|
|
161
161
|
|
|
162
|
-
export async function deriveMakerPoolSharePda(
|
|
163
|
-
optionPool: AddressLike,
|
|
164
|
-
maker: AddressLike,
|
|
165
|
-
programId: AddressLike = PROGRAM_ID
|
|
166
|
-
): Promise<readonly [Address, number]> {
|
|
167
|
-
const addressEncoder = getAddressEncoder();
|
|
168
|
-
return getProgramDerivedAddress({
|
|
169
|
-
programAddress: toAddress(programId),
|
|
170
|
-
seeds: [
|
|
171
|
-
new TextEncoder().encode("maker_pool_share"),
|
|
172
|
-
addressEncoder.encode(toAddress(optionPool)),
|
|
173
|
-
addressEncoder.encode(toAddress(maker)),
|
|
174
|
-
],
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
|
|
178
162
|
export async function deriveBuyerPositionPda(
|
|
179
163
|
buyer: AddressLike,
|
|
180
164
|
optionAccount: AddressLike,
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import type { Address } from "@solana/kit";
|
|
2
|
+
import { OptionType } from "../generated/types";
|
|
3
|
+
import { toAddress } from "../client/program";
|
|
4
|
+
import type { AddressLike, KitRpc } from "../client/types";
|
|
5
|
+
import {
|
|
6
|
+
deriveCollateralPoolPda,
|
|
7
|
+
deriveLongMintPda,
|
|
8
|
+
deriveMarketDataPda,
|
|
9
|
+
deriveMintAuthorityPda,
|
|
10
|
+
deriveOptionAccountPda,
|
|
11
|
+
deriveOptionPoolPda,
|
|
12
|
+
deriveShortMintPda,
|
|
13
|
+
} from "./pdas";
|
|
14
|
+
import { fetchCollateralPool, fetchOptionAccount, fetchOptionPool } from "./fetchers";
|
|
15
|
+
|
|
16
|
+
export interface ResolveOptionAccountsParams {
|
|
17
|
+
underlyingAsset: AddressLike;
|
|
18
|
+
optionType: OptionType;
|
|
19
|
+
strikePrice: number;
|
|
20
|
+
expirationDate: bigint | number;
|
|
21
|
+
programId?: AddressLike;
|
|
22
|
+
rpc?: KitRpc;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface ResolvedOptionAccounts {
|
|
26
|
+
optionAccount: Address;
|
|
27
|
+
longMint: Address;
|
|
28
|
+
shortMint: Address;
|
|
29
|
+
optionPool: Address;
|
|
30
|
+
marketData: Address;
|
|
31
|
+
collateralPool: Address;
|
|
32
|
+
mintAuthority: Address;
|
|
33
|
+
underlyingMint?: Address;
|
|
34
|
+
escrowLongAccount?: Address;
|
|
35
|
+
premiumVault?: Address;
|
|
36
|
+
collateralVault?: Address;
|
|
37
|
+
optionPoolData?: Awaited<ReturnType<typeof fetchOptionPool>>;
|
|
38
|
+
optionAccountData?: Awaited<ReturnType<typeof fetchOptionAccount>>;
|
|
39
|
+
collateralPoolData?: Awaited<ReturnType<typeof fetchCollateralPool>>;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Resolves all derived and optionally fetched accounts for an option.
|
|
44
|
+
* Given option identity (underlyingAsset, optionType, strikePrice, expirationDate),
|
|
45
|
+
* returns PDAs and, when rpc is provided, fetches OptionPool and CollateralPool
|
|
46
|
+
* to expose escrowLongAccount, premiumVault, collateralVault, underlyingMint.
|
|
47
|
+
*/
|
|
48
|
+
export async function resolveOptionAccounts(
|
|
49
|
+
params: ResolveOptionAccountsParams
|
|
50
|
+
): Promise<ResolvedOptionAccounts> {
|
|
51
|
+
const programId = params.programId;
|
|
52
|
+
|
|
53
|
+
const [optionAccount] = await deriveOptionAccountPda({
|
|
54
|
+
underlyingAsset: params.underlyingAsset,
|
|
55
|
+
optionType: params.optionType,
|
|
56
|
+
strikePrice: params.strikePrice,
|
|
57
|
+
expirationDate: params.expirationDate,
|
|
58
|
+
programId,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const [longMint] = await deriveLongMintPda(optionAccount, programId);
|
|
62
|
+
const [shortMint] = await deriveShortMintPda(optionAccount, programId);
|
|
63
|
+
const [optionPool] = await deriveOptionPoolPda(optionAccount, programId);
|
|
64
|
+
const [marketData] = await deriveMarketDataPda(params.underlyingAsset, programId);
|
|
65
|
+
const [collateralPool] = await deriveCollateralPoolPda(optionAccount, programId);
|
|
66
|
+
const [mintAuthority] = await deriveMintAuthorityPda(programId);
|
|
67
|
+
|
|
68
|
+
const result: ResolvedOptionAccounts = {
|
|
69
|
+
optionAccount,
|
|
70
|
+
longMint,
|
|
71
|
+
shortMint,
|
|
72
|
+
optionPool,
|
|
73
|
+
marketData,
|
|
74
|
+
collateralPool,
|
|
75
|
+
mintAuthority,
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
if (params.rpc) {
|
|
79
|
+
const [optionPoolFetched, optionAccountFetched, collateralPoolFetched] =
|
|
80
|
+
await Promise.all([
|
|
81
|
+
fetchOptionPool(params.rpc, optionPool),
|
|
82
|
+
fetchOptionAccount(params.rpc, optionAccount),
|
|
83
|
+
fetchCollateralPool(params.rpc, collateralPool),
|
|
84
|
+
]);
|
|
85
|
+
|
|
86
|
+
if (optionPoolFetched) {
|
|
87
|
+
result.optionPoolData = optionPoolFetched;
|
|
88
|
+
result.escrowLongAccount = optionPoolFetched.escrowLongAccount;
|
|
89
|
+
result.premiumVault = optionPoolFetched.premiumVault;
|
|
90
|
+
result.underlyingMint = optionPoolFetched.underlyingMint;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (optionAccountFetched) {
|
|
94
|
+
result.optionAccountData = optionAccountFetched;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (collateralPoolFetched) {
|
|
98
|
+
result.collateralPoolData = collateralPoolFetched;
|
|
99
|
+
result.collateralVault = collateralPoolFetched.collateralVault;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return result;
|
|
104
|
+
}
|
|
@@ -13,7 +13,6 @@ export * from "./escrowState";
|
|
|
13
13
|
export * from "./lenderPosition";
|
|
14
14
|
export * from "./liquidityRouter";
|
|
15
15
|
export * from "./makerCollateralShare";
|
|
16
|
-
export * from "./makerPoolShare";
|
|
17
16
|
export * from "./marketDataAccount";
|
|
18
17
|
export * from "./optionAccount";
|
|
19
18
|
export * from "./optionPool";
|
|
@@ -79,6 +79,7 @@ export type OptionPool = {
|
|
|
79
79
|
totalOpenInterestQty: bigint;
|
|
80
80
|
accClosedPerOiFp: bigint;
|
|
81
81
|
accPayoutPerOiFp: bigint;
|
|
82
|
+
accThetaPerOiFp: bigint;
|
|
82
83
|
makerCount: number;
|
|
83
84
|
createdAt: bigint;
|
|
84
85
|
lastUpdated: bigint;
|
|
@@ -109,6 +110,7 @@ export type OptionPoolArgs = {
|
|
|
109
110
|
totalOpenInterestQty: number | bigint;
|
|
110
111
|
accClosedPerOiFp: number | bigint;
|
|
111
112
|
accPayoutPerOiFp: number | bigint;
|
|
113
|
+
accThetaPerOiFp: number | bigint;
|
|
112
114
|
makerCount: number;
|
|
113
115
|
createdAt: number | bigint;
|
|
114
116
|
lastUpdated: number | bigint;
|
|
@@ -143,6 +145,7 @@ export function getOptionPoolEncoder(): FixedSizeEncoder<OptionPoolArgs> {
|
|
|
143
145
|
["totalOpenInterestQty", getU64Encoder()],
|
|
144
146
|
["accClosedPerOiFp", getU128Encoder()],
|
|
145
147
|
["accPayoutPerOiFp", getU128Encoder()],
|
|
148
|
+
["accThetaPerOiFp", getU128Encoder()],
|
|
146
149
|
["makerCount", getU32Encoder()],
|
|
147
150
|
["createdAt", getI64Encoder()],
|
|
148
151
|
["lastUpdated", getI64Encoder()],
|
|
@@ -179,6 +182,7 @@ export function getOptionPoolDecoder(): FixedSizeDecoder<OptionPool> {
|
|
|
179
182
|
["totalOpenInterestQty", getU64Decoder()],
|
|
180
183
|
["accClosedPerOiFp", getU128Decoder()],
|
|
181
184
|
["accPayoutPerOiFp", getU128Decoder()],
|
|
185
|
+
["accThetaPerOiFp", getU128Decoder()],
|
|
182
186
|
["makerCount", getU32Decoder()],
|
|
183
187
|
["createdAt", getI64Decoder()],
|
|
184
188
|
["lastUpdated", getI64Decoder()],
|
|
@@ -249,5 +253,5 @@ export async function fetchAllMaybeOptionPool(
|
|
|
249
253
|
}
|
|
250
254
|
|
|
251
255
|
export function getOptionPoolSize(): number {
|
|
252
|
-
return
|
|
256
|
+
return 382;
|
|
253
257
|
}
|
|
@@ -77,10 +77,12 @@ export type WriterPosition = {
|
|
|
77
77
|
borrowedPrincipal: bigint;
|
|
78
78
|
/** Number of active PoolLoan accounts for this writer */
|
|
79
79
|
activeLoanCount: number;
|
|
80
|
-
/**
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
|
|
80
|
+
/** Theta (time-decay) allocated to this writer (from pool accumulator) */
|
|
81
|
+
thetaEarned: bigint;
|
|
82
|
+
/** Theta already claimed/withdrawn */
|
|
83
|
+
thetaClaimed: bigint;
|
|
84
|
+
/** Snapshot of pool's acc_theta_per_oi_fp at last claim (reward-debt pattern) */
|
|
85
|
+
lastPoolAccThetaFp: bigint;
|
|
84
86
|
/** Snapshot of pool's acc_closed_per_oi_fp at last sync */
|
|
85
87
|
lastPoolAccClosedFp: bigint;
|
|
86
88
|
/** Snapshot of pool's acc_payout_per_oi_fp at last sync */
|
|
@@ -127,10 +129,12 @@ export type WriterPositionArgs = {
|
|
|
127
129
|
borrowedPrincipal: number | bigint;
|
|
128
130
|
/** Number of active PoolLoan accounts for this writer */
|
|
129
131
|
activeLoanCount: number;
|
|
130
|
-
/**
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
|
|
132
|
+
/** Theta (time-decay) allocated to this writer (from pool accumulator) */
|
|
133
|
+
thetaEarned: number | bigint;
|
|
134
|
+
/** Theta already claimed/withdrawn */
|
|
135
|
+
thetaClaimed: number | bigint;
|
|
136
|
+
/** Snapshot of pool's acc_theta_per_oi_fp at last claim (reward-debt pattern) */
|
|
137
|
+
lastPoolAccThetaFp: number | bigint;
|
|
134
138
|
/** Snapshot of pool's acc_closed_per_oi_fp at last sync */
|
|
135
139
|
lastPoolAccClosedFp: number | bigint;
|
|
136
140
|
/** Snapshot of pool's acc_payout_per_oi_fp at last sync */
|
|
@@ -171,8 +175,9 @@ export function getWriterPositionEncoder(): FixedSizeEncoder<WriterPositionArgs>
|
|
|
171
175
|
["collateralDeposited", getU64Encoder()],
|
|
172
176
|
["borrowedPrincipal", getU64Encoder()],
|
|
173
177
|
["activeLoanCount", getU8Encoder()],
|
|
174
|
-
["
|
|
175
|
-
["
|
|
178
|
+
["thetaEarned", getU64Encoder()],
|
|
179
|
+
["thetaClaimed", getU64Encoder()],
|
|
180
|
+
["lastPoolAccThetaFp", getU128Encoder()],
|
|
176
181
|
["lastPoolAccClosedFp", getU128Encoder()],
|
|
177
182
|
["lastPoolAccPayoutFp", getU128Encoder()],
|
|
178
183
|
["closedQtyDebtFp", getU128Encoder()],
|
|
@@ -205,8 +210,9 @@ export function getWriterPositionDecoder(): FixedSizeDecoder<WriterPosition> {
|
|
|
205
210
|
["collateralDeposited", getU64Decoder()],
|
|
206
211
|
["borrowedPrincipal", getU64Decoder()],
|
|
207
212
|
["activeLoanCount", getU8Decoder()],
|
|
208
|
-
["
|
|
209
|
-
["
|
|
213
|
+
["thetaEarned", getU64Decoder()],
|
|
214
|
+
["thetaClaimed", getU64Decoder()],
|
|
215
|
+
["lastPoolAccThetaFp", getU128Decoder()],
|
|
210
216
|
["lastPoolAccClosedFp", getU128Decoder()],
|
|
211
217
|
["lastPoolAccPayoutFp", getU128Decoder()],
|
|
212
218
|
["closedQtyDebtFp", getU128Decoder()],
|
|
@@ -293,5 +299,5 @@ export async function fetchAllMaybeWriterPosition(
|
|
|
293
299
|
}
|
|
294
300
|
|
|
295
301
|
export function getWriterPositionSize(): number {
|
|
296
|
-
return
|
|
302
|
+
return 300;
|
|
297
303
|
}
|
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This code was AUTOGENERATED using the Codama library.
|
|
3
|
+
* Please DO NOT EDIT THIS FILE, instead use visitors
|
|
4
|
+
* to add features, then rerun Codama to update it.
|
|
5
|
+
*
|
|
6
|
+
* @see https://github.com/codama-idl/codama
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import {
|
|
10
|
+
combineCodec,
|
|
11
|
+
fixDecoderSize,
|
|
12
|
+
fixEncoderSize,
|
|
13
|
+
getAddressEncoder,
|
|
14
|
+
getBytesDecoder,
|
|
15
|
+
getBytesEncoder,
|
|
16
|
+
getProgramDerivedAddress,
|
|
17
|
+
getStructDecoder,
|
|
18
|
+
getStructEncoder,
|
|
19
|
+
transformEncoder,
|
|
20
|
+
type AccountMeta,
|
|
21
|
+
type AccountSignerMeta,
|
|
22
|
+
type Address,
|
|
23
|
+
type FixedSizeCodec,
|
|
24
|
+
type FixedSizeDecoder,
|
|
25
|
+
type FixedSizeEncoder,
|
|
26
|
+
type Instruction,
|
|
27
|
+
type InstructionWithAccounts,
|
|
28
|
+
type InstructionWithData,
|
|
29
|
+
type ReadonlyAccount,
|
|
30
|
+
type ReadonlyUint8Array,
|
|
31
|
+
type TransactionSigner,
|
|
32
|
+
type WritableAccount,
|
|
33
|
+
type WritableSignerAccount,
|
|
34
|
+
} from "@solana/kit";
|
|
35
|
+
import { OPTION_PROGRAM_PROGRAM_ADDRESS } from "../programs";
|
|
36
|
+
import {
|
|
37
|
+
expectAddress,
|
|
38
|
+
getAccountMetaFactory,
|
|
39
|
+
type ResolvedAccount,
|
|
40
|
+
} from "../shared";
|
|
41
|
+
|
|
42
|
+
export const CLAIM_THETA_DISCRIMINATOR = new Uint8Array([
|
|
43
|
+
151, 147, 69, 241, 135, 166, 23, 62,
|
|
44
|
+
]);
|
|
45
|
+
|
|
46
|
+
export function getClaimThetaDiscriminatorBytes() {
|
|
47
|
+
return fixEncoderSize(getBytesEncoder(), 8).encode(CLAIM_THETA_DISCRIMINATOR);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type ClaimThetaInstruction<
|
|
51
|
+
TProgram extends string = typeof OPTION_PROGRAM_PROGRAM_ADDRESS,
|
|
52
|
+
TAccountOptionPool extends string | AccountMeta<string> = string,
|
|
53
|
+
TAccountWriterPosition extends string | AccountMeta<string> = string,
|
|
54
|
+
TAccountWriterPaymentAccount extends string | AccountMeta<string> = string,
|
|
55
|
+
TAccountPremiumVault extends string | AccountMeta<string> = string,
|
|
56
|
+
TAccountWriter extends string | AccountMeta<string> = string,
|
|
57
|
+
TAccountTokenProgram extends string | AccountMeta<string> =
|
|
58
|
+
"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
|
|
59
|
+
TRemainingAccounts extends readonly AccountMeta<string>[] = [],
|
|
60
|
+
> = Instruction<TProgram> &
|
|
61
|
+
InstructionWithData<ReadonlyUint8Array> &
|
|
62
|
+
InstructionWithAccounts<
|
|
63
|
+
[
|
|
64
|
+
TAccountOptionPool extends string
|
|
65
|
+
? WritableAccount<TAccountOptionPool>
|
|
66
|
+
: TAccountOptionPool,
|
|
67
|
+
TAccountWriterPosition extends string
|
|
68
|
+
? WritableAccount<TAccountWriterPosition>
|
|
69
|
+
: TAccountWriterPosition,
|
|
70
|
+
TAccountWriterPaymentAccount extends string
|
|
71
|
+
? WritableAccount<TAccountWriterPaymentAccount>
|
|
72
|
+
: TAccountWriterPaymentAccount,
|
|
73
|
+
TAccountPremiumVault extends string
|
|
74
|
+
? WritableAccount<TAccountPremiumVault>
|
|
75
|
+
: TAccountPremiumVault,
|
|
76
|
+
TAccountWriter extends string
|
|
77
|
+
? WritableSignerAccount<TAccountWriter> &
|
|
78
|
+
AccountSignerMeta<TAccountWriter>
|
|
79
|
+
: TAccountWriter,
|
|
80
|
+
TAccountTokenProgram extends string
|
|
81
|
+
? ReadonlyAccount<TAccountTokenProgram>
|
|
82
|
+
: TAccountTokenProgram,
|
|
83
|
+
...TRemainingAccounts,
|
|
84
|
+
]
|
|
85
|
+
>;
|
|
86
|
+
|
|
87
|
+
export type ClaimThetaInstructionData = { discriminator: ReadonlyUint8Array };
|
|
88
|
+
|
|
89
|
+
export type ClaimThetaInstructionDataArgs = {};
|
|
90
|
+
|
|
91
|
+
export function getClaimThetaInstructionDataEncoder(): FixedSizeEncoder<ClaimThetaInstructionDataArgs> {
|
|
92
|
+
return transformEncoder(
|
|
93
|
+
getStructEncoder([["discriminator", fixEncoderSize(getBytesEncoder(), 8)]]),
|
|
94
|
+
(value) => ({ ...value, discriminator: CLAIM_THETA_DISCRIMINATOR }),
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function getClaimThetaInstructionDataDecoder(): FixedSizeDecoder<ClaimThetaInstructionData> {
|
|
99
|
+
return getStructDecoder([
|
|
100
|
+
["discriminator", fixDecoderSize(getBytesDecoder(), 8)],
|
|
101
|
+
]);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function getClaimThetaInstructionDataCodec(): FixedSizeCodec<
|
|
105
|
+
ClaimThetaInstructionDataArgs,
|
|
106
|
+
ClaimThetaInstructionData
|
|
107
|
+
> {
|
|
108
|
+
return combineCodec(
|
|
109
|
+
getClaimThetaInstructionDataEncoder(),
|
|
110
|
+
getClaimThetaInstructionDataDecoder(),
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export type ClaimThetaAsyncInput<
|
|
115
|
+
TAccountOptionPool extends string = string,
|
|
116
|
+
TAccountWriterPosition extends string = string,
|
|
117
|
+
TAccountWriterPaymentAccount extends string = string,
|
|
118
|
+
TAccountPremiumVault extends string = string,
|
|
119
|
+
TAccountWriter extends string = string,
|
|
120
|
+
TAccountTokenProgram extends string = string,
|
|
121
|
+
> = {
|
|
122
|
+
/** The pool */
|
|
123
|
+
optionPool: Address<TAccountOptionPool>;
|
|
124
|
+
/** Writer's position account */
|
|
125
|
+
writerPosition?: Address<TAccountWriterPosition>;
|
|
126
|
+
/** Writer's account to receive theta (in underlying asset) */
|
|
127
|
+
writerPaymentAccount: Address<TAccountWriterPaymentAccount>;
|
|
128
|
+
/** Pool's premium vault (source of theta) */
|
|
129
|
+
premiumVault: Address<TAccountPremiumVault>;
|
|
130
|
+
writer: TransactionSigner<TAccountWriter>;
|
|
131
|
+
tokenProgram?: Address<TAccountTokenProgram>;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export async function getClaimThetaInstructionAsync<
|
|
135
|
+
TAccountOptionPool extends string,
|
|
136
|
+
TAccountWriterPosition extends string,
|
|
137
|
+
TAccountWriterPaymentAccount extends string,
|
|
138
|
+
TAccountPremiumVault extends string,
|
|
139
|
+
TAccountWriter extends string,
|
|
140
|
+
TAccountTokenProgram extends string,
|
|
141
|
+
TProgramAddress extends Address = typeof OPTION_PROGRAM_PROGRAM_ADDRESS,
|
|
142
|
+
>(
|
|
143
|
+
input: ClaimThetaAsyncInput<
|
|
144
|
+
TAccountOptionPool,
|
|
145
|
+
TAccountWriterPosition,
|
|
146
|
+
TAccountWriterPaymentAccount,
|
|
147
|
+
TAccountPremiumVault,
|
|
148
|
+
TAccountWriter,
|
|
149
|
+
TAccountTokenProgram
|
|
150
|
+
>,
|
|
151
|
+
config?: { programAddress?: TProgramAddress },
|
|
152
|
+
): Promise<
|
|
153
|
+
ClaimThetaInstruction<
|
|
154
|
+
TProgramAddress,
|
|
155
|
+
TAccountOptionPool,
|
|
156
|
+
TAccountWriterPosition,
|
|
157
|
+
TAccountWriterPaymentAccount,
|
|
158
|
+
TAccountPremiumVault,
|
|
159
|
+
TAccountWriter,
|
|
160
|
+
TAccountTokenProgram
|
|
161
|
+
>
|
|
162
|
+
> {
|
|
163
|
+
// Program address.
|
|
164
|
+
const programAddress =
|
|
165
|
+
config?.programAddress ?? OPTION_PROGRAM_PROGRAM_ADDRESS;
|
|
166
|
+
|
|
167
|
+
// Original accounts.
|
|
168
|
+
const originalAccounts = {
|
|
169
|
+
optionPool: { value: input.optionPool ?? null, isWritable: true },
|
|
170
|
+
writerPosition: { value: input.writerPosition ?? null, isWritable: true },
|
|
171
|
+
writerPaymentAccount: {
|
|
172
|
+
value: input.writerPaymentAccount ?? null,
|
|
173
|
+
isWritable: true,
|
|
174
|
+
},
|
|
175
|
+
premiumVault: { value: input.premiumVault ?? null, isWritable: true },
|
|
176
|
+
writer: { value: input.writer ?? null, isWritable: true },
|
|
177
|
+
tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
|
|
178
|
+
};
|
|
179
|
+
const accounts = originalAccounts as Record<
|
|
180
|
+
keyof typeof originalAccounts,
|
|
181
|
+
ResolvedAccount
|
|
182
|
+
>;
|
|
183
|
+
|
|
184
|
+
// Resolve default values.
|
|
185
|
+
if (!accounts.writerPosition.value) {
|
|
186
|
+
accounts.writerPosition.value = await getProgramDerivedAddress({
|
|
187
|
+
programAddress,
|
|
188
|
+
seeds: [
|
|
189
|
+
getBytesEncoder().encode(
|
|
190
|
+
new Uint8Array([
|
|
191
|
+
119, 114, 105, 116, 101, 114, 95, 112, 111, 115, 105, 116, 105, 111,
|
|
192
|
+
110,
|
|
193
|
+
]),
|
|
194
|
+
),
|
|
195
|
+
getAddressEncoder().encode(expectAddress(accounts.optionPool.value)),
|
|
196
|
+
getAddressEncoder().encode(expectAddress(accounts.writer.value)),
|
|
197
|
+
],
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
if (!accounts.tokenProgram.value) {
|
|
201
|
+
accounts.tokenProgram.value =
|
|
202
|
+
"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
|
|
206
|
+
return Object.freeze({
|
|
207
|
+
accounts: [
|
|
208
|
+
getAccountMeta(accounts.optionPool),
|
|
209
|
+
getAccountMeta(accounts.writerPosition),
|
|
210
|
+
getAccountMeta(accounts.writerPaymentAccount),
|
|
211
|
+
getAccountMeta(accounts.premiumVault),
|
|
212
|
+
getAccountMeta(accounts.writer),
|
|
213
|
+
getAccountMeta(accounts.tokenProgram),
|
|
214
|
+
],
|
|
215
|
+
data: getClaimThetaInstructionDataEncoder().encode({}),
|
|
216
|
+
programAddress,
|
|
217
|
+
} as ClaimThetaInstruction<
|
|
218
|
+
TProgramAddress,
|
|
219
|
+
TAccountOptionPool,
|
|
220
|
+
TAccountWriterPosition,
|
|
221
|
+
TAccountWriterPaymentAccount,
|
|
222
|
+
TAccountPremiumVault,
|
|
223
|
+
TAccountWriter,
|
|
224
|
+
TAccountTokenProgram
|
|
225
|
+
>);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export type ClaimThetaInput<
|
|
229
|
+
TAccountOptionPool extends string = string,
|
|
230
|
+
TAccountWriterPosition extends string = string,
|
|
231
|
+
TAccountWriterPaymentAccount extends string = string,
|
|
232
|
+
TAccountPremiumVault extends string = string,
|
|
233
|
+
TAccountWriter extends string = string,
|
|
234
|
+
TAccountTokenProgram extends string = string,
|
|
235
|
+
> = {
|
|
236
|
+
/** The pool */
|
|
237
|
+
optionPool: Address<TAccountOptionPool>;
|
|
238
|
+
/** Writer's position account */
|
|
239
|
+
writerPosition: Address<TAccountWriterPosition>;
|
|
240
|
+
/** Writer's account to receive theta (in underlying asset) */
|
|
241
|
+
writerPaymentAccount: Address<TAccountWriterPaymentAccount>;
|
|
242
|
+
/** Pool's premium vault (source of theta) */
|
|
243
|
+
premiumVault: Address<TAccountPremiumVault>;
|
|
244
|
+
writer: TransactionSigner<TAccountWriter>;
|
|
245
|
+
tokenProgram?: Address<TAccountTokenProgram>;
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
export function getClaimThetaInstruction<
|
|
249
|
+
TAccountOptionPool extends string,
|
|
250
|
+
TAccountWriterPosition extends string,
|
|
251
|
+
TAccountWriterPaymentAccount extends string,
|
|
252
|
+
TAccountPremiumVault extends string,
|
|
253
|
+
TAccountWriter extends string,
|
|
254
|
+
TAccountTokenProgram extends string,
|
|
255
|
+
TProgramAddress extends Address = typeof OPTION_PROGRAM_PROGRAM_ADDRESS,
|
|
256
|
+
>(
|
|
257
|
+
input: ClaimThetaInput<
|
|
258
|
+
TAccountOptionPool,
|
|
259
|
+
TAccountWriterPosition,
|
|
260
|
+
TAccountWriterPaymentAccount,
|
|
261
|
+
TAccountPremiumVault,
|
|
262
|
+
TAccountWriter,
|
|
263
|
+
TAccountTokenProgram
|
|
264
|
+
>,
|
|
265
|
+
config?: { programAddress?: TProgramAddress },
|
|
266
|
+
): ClaimThetaInstruction<
|
|
267
|
+
TProgramAddress,
|
|
268
|
+
TAccountOptionPool,
|
|
269
|
+
TAccountWriterPosition,
|
|
270
|
+
TAccountWriterPaymentAccount,
|
|
271
|
+
TAccountPremiumVault,
|
|
272
|
+
TAccountWriter,
|
|
273
|
+
TAccountTokenProgram
|
|
274
|
+
> {
|
|
275
|
+
// Program address.
|
|
276
|
+
const programAddress =
|
|
277
|
+
config?.programAddress ?? OPTION_PROGRAM_PROGRAM_ADDRESS;
|
|
278
|
+
|
|
279
|
+
// Original accounts.
|
|
280
|
+
const originalAccounts = {
|
|
281
|
+
optionPool: { value: input.optionPool ?? null, isWritable: true },
|
|
282
|
+
writerPosition: { value: input.writerPosition ?? null, isWritable: true },
|
|
283
|
+
writerPaymentAccount: {
|
|
284
|
+
value: input.writerPaymentAccount ?? null,
|
|
285
|
+
isWritable: true,
|
|
286
|
+
},
|
|
287
|
+
premiumVault: { value: input.premiumVault ?? null, isWritable: true },
|
|
288
|
+
writer: { value: input.writer ?? null, isWritable: true },
|
|
289
|
+
tokenProgram: { value: input.tokenProgram ?? null, isWritable: false },
|
|
290
|
+
};
|
|
291
|
+
const accounts = originalAccounts as Record<
|
|
292
|
+
keyof typeof originalAccounts,
|
|
293
|
+
ResolvedAccount
|
|
294
|
+
>;
|
|
295
|
+
|
|
296
|
+
// Resolve default values.
|
|
297
|
+
if (!accounts.tokenProgram.value) {
|
|
298
|
+
accounts.tokenProgram.value =
|
|
299
|
+
"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address<"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA">;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
const getAccountMeta = getAccountMetaFactory(programAddress, "programId");
|
|
303
|
+
return Object.freeze({
|
|
304
|
+
accounts: [
|
|
305
|
+
getAccountMeta(accounts.optionPool),
|
|
306
|
+
getAccountMeta(accounts.writerPosition),
|
|
307
|
+
getAccountMeta(accounts.writerPaymentAccount),
|
|
308
|
+
getAccountMeta(accounts.premiumVault),
|
|
309
|
+
getAccountMeta(accounts.writer),
|
|
310
|
+
getAccountMeta(accounts.tokenProgram),
|
|
311
|
+
],
|
|
312
|
+
data: getClaimThetaInstructionDataEncoder().encode({}),
|
|
313
|
+
programAddress,
|
|
314
|
+
} as ClaimThetaInstruction<
|
|
315
|
+
TProgramAddress,
|
|
316
|
+
TAccountOptionPool,
|
|
317
|
+
TAccountWriterPosition,
|
|
318
|
+
TAccountWriterPaymentAccount,
|
|
319
|
+
TAccountPremiumVault,
|
|
320
|
+
TAccountWriter,
|
|
321
|
+
TAccountTokenProgram
|
|
322
|
+
>);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export type ParsedClaimThetaInstruction<
|
|
326
|
+
TProgram extends string = typeof OPTION_PROGRAM_PROGRAM_ADDRESS,
|
|
327
|
+
TAccountMetas extends readonly AccountMeta[] = readonly AccountMeta[],
|
|
328
|
+
> = {
|
|
329
|
+
programAddress: Address<TProgram>;
|
|
330
|
+
accounts: {
|
|
331
|
+
/** The pool */
|
|
332
|
+
optionPool: TAccountMetas[0];
|
|
333
|
+
/** Writer's position account */
|
|
334
|
+
writerPosition: TAccountMetas[1];
|
|
335
|
+
/** Writer's account to receive theta (in underlying asset) */
|
|
336
|
+
writerPaymentAccount: TAccountMetas[2];
|
|
337
|
+
/** Pool's premium vault (source of theta) */
|
|
338
|
+
premiumVault: TAccountMetas[3];
|
|
339
|
+
writer: TAccountMetas[4];
|
|
340
|
+
tokenProgram: TAccountMetas[5];
|
|
341
|
+
};
|
|
342
|
+
data: ClaimThetaInstructionData;
|
|
343
|
+
};
|
|
344
|
+
|
|
345
|
+
export function parseClaimThetaInstruction<
|
|
346
|
+
TProgram extends string,
|
|
347
|
+
TAccountMetas extends readonly AccountMeta[],
|
|
348
|
+
>(
|
|
349
|
+
instruction: Instruction<TProgram> &
|
|
350
|
+
InstructionWithAccounts<TAccountMetas> &
|
|
351
|
+
InstructionWithData<ReadonlyUint8Array>,
|
|
352
|
+
): ParsedClaimThetaInstruction<TProgram, TAccountMetas> {
|
|
353
|
+
if (instruction.accounts.length < 6) {
|
|
354
|
+
// TODO: Coded error.
|
|
355
|
+
throw new Error("Not enough accounts");
|
|
356
|
+
}
|
|
357
|
+
let accountIndex = 0;
|
|
358
|
+
const getNextAccount = () => {
|
|
359
|
+
const accountMeta = (instruction.accounts as TAccountMetas)[accountIndex]!;
|
|
360
|
+
accountIndex += 1;
|
|
361
|
+
return accountMeta;
|
|
362
|
+
};
|
|
363
|
+
return {
|
|
364
|
+
programAddress: instruction.programAddress,
|
|
365
|
+
accounts: {
|
|
366
|
+
optionPool: getNextAccount(),
|
|
367
|
+
writerPosition: getNextAccount(),
|
|
368
|
+
writerPaymentAccount: getNextAccount(),
|
|
369
|
+
premiumVault: getNextAccount(),
|
|
370
|
+
writer: getNextAccount(),
|
|
371
|
+
tokenProgram: getNextAccount(),
|
|
372
|
+
},
|
|
373
|
+
data: getClaimThetaInstructionDataDecoder().decode(instruction.data),
|
|
374
|
+
};
|
|
375
|
+
}
|