@exponent-labs/exponent-sdk 0.1.7 → 0.1.8
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/build/EventDecoderV2.d.ts +31 -0
- package/build/EventDecoderV2.js +76 -0
- package/build/EventDecoderV2.js.map +1 -0
- package/build/addressLookupTableUtil.d.ts +17 -1
- package/build/addressLookupTableUtil.js +35 -1
- package/build/addressLookupTableUtil.js.map +1 -1
- package/build/clmm/events.d.ts +10 -0
- package/build/clmm/events.js +10 -0
- package/build/clmm/events.js.map +1 -0
- package/build/clmm/index.d.ts +1 -0
- package/build/clmm/index.js +18 -0
- package/build/clmm/index.js.map +1 -0
- package/build/events.d.ts +200 -9
- package/build/events.js +73 -24
- package/build/events.js.map +1 -1
- package/build/eventsV2.d.ts +7 -0
- package/build/eventsV2.js +10 -0
- package/build/eventsV2.js.map +1 -0
- package/build/flavors.d.ts +2 -0
- package/build/flavors.js +81 -27
- package/build/flavors.js.map +1 -1
- package/build/index.d.ts +6 -0
- package/build/index.js +14 -4
- package/build/index.js.map +1 -1
- package/build/lpPosition.js +4 -1
- package/build/lpPosition.js.map +1 -1
- package/build/market.d.ts +14 -2
- package/build/market.js +70 -29
- package/build/market.js.map +1 -1
- package/build/marketThree.d.ts +664 -0
- package/build/marketThree.js +1415 -0
- package/build/marketThree.js.map +1 -0
- package/build/marketThree.test.d.ts +1 -0
- package/build/marketThree.test.js +166 -0
- package/build/marketThree.test.js.map +1 -0
- package/build/orderbook/events.d.ts +7 -0
- package/build/orderbook/events.js +10 -0
- package/build/orderbook/events.js.map +1 -0
- package/build/orderbook/index.d.ts +4 -0
- package/build/orderbook/index.js +41 -0
- package/build/orderbook/index.js.map +1 -0
- package/build/orderbook/math.d.ts +26 -0
- package/build/orderbook/math.js +111 -0
- package/build/orderbook/math.js.map +1 -0
- package/build/orderbook/orderbook.d.ts +175 -0
- package/build/orderbook/orderbook.js +756 -0
- package/build/orderbook/orderbook.js.map +1 -0
- package/build/orderbook/types.d.ts +49 -0
- package/build/orderbook/types.js +27 -0
- package/build/orderbook/types.js.map +1 -0
- package/build/orderbook/utils.d.ts +18 -0
- package/build/orderbook/utils.js +74 -0
- package/build/orderbook/utils.js.map +1 -0
- package/build/router.d.ts +92 -0
- package/build/router.js +214 -0
- package/build/router.js.map +1 -0
- package/build/syPosition.js +6 -0
- package/build/syPosition.js.map +1 -1
- package/build/utils/index.d.ts +3 -2
- package/build/utils/index.js +22 -1
- package/build/utils/index.js.map +1 -1
- package/build/vault.d.ts +3 -1
- package/build/vault.js +98 -62
- package/build/vault.js.map +1 -1
- package/build/ytPosition.d.ts +2 -0
- package/build/ytPosition.js +18 -5
- package/build/ytPosition.js.map +1 -1
- package/package.json +28 -23
- package/src/EventDecoderV2.ts +96 -0
- package/src/addressLookupTableUtil.ts +42 -1
- package/src/clmm/events.ts +17 -0
- package/src/clmm/index.ts +1 -0
- package/src/events.ts +280 -27
- package/src/eventsV2.ts +13 -0
- package/src/flavors.ts +97 -27
- package/src/index.ts +6 -0
- package/src/lpPosition.ts +5 -2
- package/src/market.ts +100 -31
- package/src/marketThree.test.ts +208 -0
- package/src/marketThree.ts +2430 -0
- package/src/orderbook/events.ts +13 -0
- package/src/orderbook/index.ts +12 -0
- package/src/orderbook/math.ts +122 -0
- package/src/orderbook/orderbook.ts +1153 -0
- package/src/orderbook/types.ts +45 -0
- package/src/orderbook/utils.ts +74 -0
- package/src/router.ts +360 -0
- package/src/syPosition.ts +4 -0
- package/src/utils/index.ts +27 -2
- package/src/vault.ts +100 -62
- package/src/ytPosition.ts +28 -7
- package/tsconfig.json +4 -1
|
@@ -0,0 +1,756 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Orderbook = void 0;
|
|
4
|
+
const anchor_1 = require("@coral-xyz/anchor");
|
|
5
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
6
|
+
const exponent_admin_pda_1 = require("@exponent-labs/exponent-admin-pda");
|
|
7
|
+
const exponent_fetcher_1 = require("@exponent-labs/exponent-fetcher");
|
|
8
|
+
const exponent_idl_1 = require("@exponent-labs/exponent-idl");
|
|
9
|
+
const exponent_orderbook_idl_1 = require("@exponent-labs/exponent-orderbook-idl");
|
|
10
|
+
const exponent_pda_1 = require("@exponent-labs/exponent-pda");
|
|
11
|
+
const addressLookupTableUtil_1 = require("../addressLookupTableUtil");
|
|
12
|
+
const flavors_1 = require("../flavors");
|
|
13
|
+
const market_1 = require("../market");
|
|
14
|
+
const utils_1 = require("../utils");
|
|
15
|
+
const vault_1 = require("../vault");
|
|
16
|
+
const math_1 = require("./math");
|
|
17
|
+
const types_1 = require("./types");
|
|
18
|
+
const utils_2 = require("./utils");
|
|
19
|
+
const market_math_1 = require("@exponent-labs/market-math");
|
|
20
|
+
class Orderbook {
|
|
21
|
+
state;
|
|
22
|
+
selfAddress;
|
|
23
|
+
env;
|
|
24
|
+
connection;
|
|
25
|
+
vaulLookupTable;
|
|
26
|
+
orderbookProgram;
|
|
27
|
+
coreProgram;
|
|
28
|
+
constructor(state, selfAddress, env, connection, vaulLookupTable) {
|
|
29
|
+
this.state = state;
|
|
30
|
+
this.selfAddress = selfAddress;
|
|
31
|
+
this.env = env;
|
|
32
|
+
this.connection = connection;
|
|
33
|
+
this.vaulLookupTable = vaulLookupTable;
|
|
34
|
+
this.orderbookProgram = new anchor_1.Program(exponent_orderbook_idl_1.IDL, new anchor_1.AnchorProvider(connection, new market_1.MyWallet(anchor_1.web3.Keypair.generate())));
|
|
35
|
+
this.coreProgram = new anchor_1.Program(exponent_idl_1.IDL, new anchor_1.AnchorProvider(connection, new market_1.MyWallet(anchor_1.web3.Keypair.generate())));
|
|
36
|
+
}
|
|
37
|
+
static async load(env, connection, address, options = {}, vault) {
|
|
38
|
+
const fetcher = new exponent_fetcher_1.ExponentFetcher({ connection });
|
|
39
|
+
const orderbook = await fetcher.fetchOrderbook(address);
|
|
40
|
+
const pda = new exponent_pda_1.ExponentOrderbookPDA();
|
|
41
|
+
const orderbookCpiAccounts = await fetcher.fetchOrderbookCpiAccounts(pda.orderbookCpiAccounts({ orderbook: address }));
|
|
42
|
+
const altOrderbook = await (0, addressLookupTableUtil_1.fetchAddressLookupTable)(connection, orderbook.addressLookupTable);
|
|
43
|
+
const cpiAccounts = (0, addressLookupTableUtil_1.makeCpiAccountMetaLists)(altOrderbook, orderbookCpiAccounts.syCpiAccounts);
|
|
44
|
+
const cpiCoreAccounts = (0, addressLookupTableUtil_1.makeCoreCpiAccountMetaLists)(altOrderbook, orderbookCpiAccounts.exponentCoreCpiAccounts);
|
|
45
|
+
const loadedVault = vault || (await vault_1.Vault.load(env, connection, orderbook.vault));
|
|
46
|
+
const flavor = (() => {
|
|
47
|
+
switch (loadedVault.flavor.flavor) {
|
|
48
|
+
case "marginfi":
|
|
49
|
+
return (0, flavors_1.makeFlavorMarginfiSync)(loadedVault.flavor);
|
|
50
|
+
case "kamino":
|
|
51
|
+
return (0, flavors_1.makeFlavorKaminoSync)(loadedVault.flavor);
|
|
52
|
+
case "jitoRestaking":
|
|
53
|
+
return (0, flavors_1.makeFlavorJitoRestakingSync)(loadedVault.flavor);
|
|
54
|
+
case "perena":
|
|
55
|
+
return (0, flavors_1.makeFlavorPerenaSync)(loadedVault.flavor);
|
|
56
|
+
case "generic":
|
|
57
|
+
return (0, flavors_1.makeFlavorGenericSync)(loadedVault.flavor, options.syConfig);
|
|
58
|
+
default:
|
|
59
|
+
throw new Error(`Unknown flavor: ${loadedVault.flavor}`);
|
|
60
|
+
}
|
|
61
|
+
})();
|
|
62
|
+
const state = {
|
|
63
|
+
...orderbook,
|
|
64
|
+
syCpiAccounts: cpiAccounts,
|
|
65
|
+
exponentCoreCpiAccounts: cpiCoreAccounts,
|
|
66
|
+
flavor,
|
|
67
|
+
};
|
|
68
|
+
return new Orderbook(state, address, env, connection, loadedVault.addressLookupTable);
|
|
69
|
+
}
|
|
70
|
+
async reload(conn = this.connection) {
|
|
71
|
+
const v = await Orderbook.load(this.env, conn, this.selfAddress);
|
|
72
|
+
this.state = v.state;
|
|
73
|
+
return v;
|
|
74
|
+
}
|
|
75
|
+
get corePda() {
|
|
76
|
+
return new exponent_pda_1.ExponentPDA();
|
|
77
|
+
}
|
|
78
|
+
get pda() {
|
|
79
|
+
return new exponent_pda_1.ExponentOrderbookPDA(this.orderbookProgram.programId);
|
|
80
|
+
}
|
|
81
|
+
get adminPda() {
|
|
82
|
+
return new exponent_admin_pda_1.ExponentAdminPda(exponent_admin_pda_1.EXPONENT_ADMIN_PROGRAM_ID);
|
|
83
|
+
}
|
|
84
|
+
get eventAuthority() {
|
|
85
|
+
return (0, utils_1.emitEventAuthority)(this.orderbookProgram.programId);
|
|
86
|
+
}
|
|
87
|
+
get coreEventAuthority() {
|
|
88
|
+
return (0, utils_1.emitEventAuthority)(this.state.exponentCoreProgram);
|
|
89
|
+
}
|
|
90
|
+
get mintYt() {
|
|
91
|
+
return this.corePda.mintYt({ vault: this.state.vault });
|
|
92
|
+
}
|
|
93
|
+
get mintPt() {
|
|
94
|
+
return this.corePda.mintPt({ vault: this.state.vault });
|
|
95
|
+
}
|
|
96
|
+
get syCpiAccounts() {
|
|
97
|
+
return this.state.syCpiAccounts;
|
|
98
|
+
}
|
|
99
|
+
get exponentCoreCpiAccounts() {
|
|
100
|
+
return this.state.exponentCoreCpiAccounts;
|
|
101
|
+
}
|
|
102
|
+
get flavor() {
|
|
103
|
+
return this.state.flavor;
|
|
104
|
+
}
|
|
105
|
+
get secondsRemaining() {
|
|
106
|
+
return Math.max(this.state.financials.expirationTs - Math.floor(Date.now() / 1000), 0);
|
|
107
|
+
}
|
|
108
|
+
/** Fee rate is 1 + the actual fee rate - is is for convenience with calculations
|
|
109
|
+
* A feeRate of 1.001 means the fee is 0.1%
|
|
110
|
+
* A feeRate of 1 means there is no fee
|
|
111
|
+
*/
|
|
112
|
+
get makerFeeRate() {
|
|
113
|
+
if (this.secondsRemaining == 0) {
|
|
114
|
+
return 1;
|
|
115
|
+
}
|
|
116
|
+
return (0, market_math_1.feeRate)({
|
|
117
|
+
lnFeeRateRoot: this.state.configurationOptions.lnMakerFeeRate,
|
|
118
|
+
secondsRemaining: this.secondsRemaining,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
get takerFeeRate() {
|
|
122
|
+
if (this.secondsRemaining == 0) {
|
|
123
|
+
return 1;
|
|
124
|
+
}
|
|
125
|
+
return (0, market_math_1.feeRate)({
|
|
126
|
+
lnFeeRateRoot: this.state.configurationOptions.lnTakerFeeRate,
|
|
127
|
+
secondsRemaining: this.secondsRemaining,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
/** Post offer instruction */
|
|
131
|
+
ixPostOffer({ trader, price, amount, offerType, offerOption, virtualOffer, expirySeconds, mintSy, ptSrc: ptSrcParam, ytSrc: ytSrcParam, sySrc: sySrcParam, }) {
|
|
132
|
+
const ptSrc = ptSrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(this.mintPt, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
133
|
+
const ytSrc = ytSrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(this.mintYt, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
134
|
+
const sySrc = sySrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(mintSy, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
135
|
+
const impliedRate = (0, utils_2.priceToImpliedRate)(price);
|
|
136
|
+
const cpiAccountOrderbook = this.pda.orderbookCpiAccounts({ orderbook: this.selfAddress });
|
|
137
|
+
const remainingAccounts = (0, utils_1.uniqueRemainingAccounts)([
|
|
138
|
+
...this.syCpiAccounts.getSyState,
|
|
139
|
+
...this.exponentCoreCpiAccounts.mergeSy,
|
|
140
|
+
...this.exponentCoreCpiAccounts.stripSy,
|
|
141
|
+
...this.exponentCoreCpiAccounts.depositYt,
|
|
142
|
+
...this.exponentCoreCpiAccounts.withdrawYt,
|
|
143
|
+
]);
|
|
144
|
+
return this.orderbookProgram.methods
|
|
145
|
+
.postOffer(impliedRate, amount, offerType, virtualOffer, expirySeconds, offerOption)
|
|
146
|
+
.accountsStrict({
|
|
147
|
+
orderbook: this.selfAddress,
|
|
148
|
+
vault: this.state.vault,
|
|
149
|
+
tokenEscrowSy: this.pda.orderbookEscrowSy({ orderbook: this.selfAddress }),
|
|
150
|
+
tokenEscrowYt: this.pda.orderbookEscrowYt({ orderbook: this.selfAddress }),
|
|
151
|
+
tokenEscrowPt: this.pda.orderbookEscrowPt({ orderbook: this.selfAddress }),
|
|
152
|
+
yieldPosition: this.corePda.yieldPosition({ vault: this.state.vault, owner: this.selfAddress }),
|
|
153
|
+
systemProgram: anchor_1.web3.SystemProgram.programId,
|
|
154
|
+
syProgram: this.state.syProgram,
|
|
155
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
156
|
+
exponentCore: this.state.exponentCoreProgram,
|
|
157
|
+
addressLookupTable: this.state.addressLookupTable,
|
|
158
|
+
trader,
|
|
159
|
+
tokenSyTrader: sySrc,
|
|
160
|
+
tokenYtTrader: ytSrc,
|
|
161
|
+
tokenPtTrader: ptSrc,
|
|
162
|
+
eventAuthority: this.eventAuthority,
|
|
163
|
+
program: this.orderbookProgram.programId,
|
|
164
|
+
cpiAccountOrderbook,
|
|
165
|
+
})
|
|
166
|
+
.remainingAccounts(remainingAccounts)
|
|
167
|
+
.instruction();
|
|
168
|
+
}
|
|
169
|
+
/** Market offer instruction */
|
|
170
|
+
ixMarketOffer({ trader, maxPriceApy, amount, minAmountOut, offerType, virtualOffer, mintSy, ptSrc: ptSrcParam, ytSrc: ytSrcParam, sySrc: sySrcParam, }) {
|
|
171
|
+
const ptSrc = ptSrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(this.mintPt, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
172
|
+
const ytSrc = ytSrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(this.mintYt, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
173
|
+
const sySrc = sySrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(mintSy, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
174
|
+
const impliedRate = (0, utils_2.priceToImpliedRate)(maxPriceApy);
|
|
175
|
+
const cpiAccountOrderbook = this.pda.orderbookCpiAccounts({ orderbook: this.selfAddress });
|
|
176
|
+
const remainingAccounts = (0, utils_1.uniqueRemainingAccounts)([
|
|
177
|
+
...this.syCpiAccounts.getSyState,
|
|
178
|
+
...this.exponentCoreCpiAccounts.mergeSy,
|
|
179
|
+
...this.exponentCoreCpiAccounts.stripSy,
|
|
180
|
+
...this.exponentCoreCpiAccounts.depositYt,
|
|
181
|
+
...this.exponentCoreCpiAccounts.withdrawYt,
|
|
182
|
+
]);
|
|
183
|
+
return this.orderbookProgram.methods
|
|
184
|
+
.marketOffer(impliedRate, amount, minAmountOut, offerType, virtualOffer)
|
|
185
|
+
.accountsStrict({
|
|
186
|
+
orderbook: this.selfAddress,
|
|
187
|
+
vault: this.state.vault,
|
|
188
|
+
tokenEscrowSy: this.pda.orderbookEscrowSy({ orderbook: this.selfAddress }),
|
|
189
|
+
tokenEscrowYt: this.pda.orderbookEscrowYt({ orderbook: this.selfAddress }),
|
|
190
|
+
tokenEscrowPt: this.pda.orderbookEscrowPt({ orderbook: this.selfAddress }),
|
|
191
|
+
yieldPosition: this.corePda.yieldPosition({ vault: this.state.vault, owner: this.selfAddress }),
|
|
192
|
+
systemProgram: anchor_1.web3.SystemProgram.programId,
|
|
193
|
+
syProgram: this.state.syProgram,
|
|
194
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
195
|
+
exponentCore: this.state.exponentCoreProgram,
|
|
196
|
+
addressLookupTable: this.state.addressLookupTable,
|
|
197
|
+
trader,
|
|
198
|
+
tokenSyTrader: sySrc,
|
|
199
|
+
tokenYtTrader: ytSrc,
|
|
200
|
+
tokenPtTrader: ptSrc,
|
|
201
|
+
eventAuthority: this.eventAuthority,
|
|
202
|
+
program: this.orderbookProgram.programId,
|
|
203
|
+
cpiAccountOrderbook,
|
|
204
|
+
})
|
|
205
|
+
.remainingAccounts(remainingAccounts)
|
|
206
|
+
.instruction();
|
|
207
|
+
}
|
|
208
|
+
/** High trust set configuration options instruction */
|
|
209
|
+
ixSetConfigurationOptions({ admin, configurationOptions, }) {
|
|
210
|
+
return this.orderbookProgram.methods
|
|
211
|
+
.setConfigurationOptions({ thresholdAmount: { 0: new anchor_1.BN(configurationOptions.thresholdAmount.toString()) } })
|
|
212
|
+
.accountsStrict({
|
|
213
|
+
orderbook: this.selfAddress,
|
|
214
|
+
adminSigner: admin,
|
|
215
|
+
})
|
|
216
|
+
.instruction();
|
|
217
|
+
}
|
|
218
|
+
getOffers() {
|
|
219
|
+
const { offers, prices, userEscrows } = this.state;
|
|
220
|
+
return offers.map((o) => {
|
|
221
|
+
//? pricePointer - 1 because of TreeNode allocator specifics
|
|
222
|
+
const priceApy = prices[o.pricePointer - 1].key / math_1.PRICE_BASE_POINTS;
|
|
223
|
+
const userEscrow = userEscrows[o.userVaultPointer - 1].user;
|
|
224
|
+
const type = (0, utils_2.getOfferType)(o.orderTypeFlag);
|
|
225
|
+
return {
|
|
226
|
+
type,
|
|
227
|
+
priceApy,
|
|
228
|
+
amount: Number(o.amount.toString()),
|
|
229
|
+
userEscrow: userEscrow,
|
|
230
|
+
expiryAt: o.expiryAt,
|
|
231
|
+
createdAt: o.createdAt,
|
|
232
|
+
isVirtual: o.virtualOffer,
|
|
233
|
+
};
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
/** Remove offer instruction */
|
|
237
|
+
ixRemoveOffer({ trader, offerIdx, mintSy, ptSrc: ptSrcParam, ytSrc: ytSrcParam, sySrc: sySrcParam, }) {
|
|
238
|
+
const ptSrc = ptSrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(this.mintPt, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
239
|
+
const ytSrc = ytSrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(this.mintYt, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
240
|
+
const sySrc = sySrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(mintSy, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
241
|
+
const cpiAccountOrderbook = this.pda.orderbookCpiAccounts({ orderbook: this.selfAddress });
|
|
242
|
+
return this.orderbookProgram.methods
|
|
243
|
+
.removeOffer(offerIdx)
|
|
244
|
+
.accountsStrict({
|
|
245
|
+
orderbook: this.selfAddress,
|
|
246
|
+
tokenEscrowSy: this.pda.orderbookEscrowSy({ orderbook: this.selfAddress }),
|
|
247
|
+
tokenEscrowYt: this.pda.orderbookEscrowYt({ orderbook: this.selfAddress }),
|
|
248
|
+
tokenEscrowPt: this.pda.orderbookEscrowPt({ orderbook: this.selfAddress }),
|
|
249
|
+
systemProgram: anchor_1.web3.SystemProgram.programId,
|
|
250
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
251
|
+
trader,
|
|
252
|
+
tokenSyTrader: sySrc,
|
|
253
|
+
tokenYtTrader: ytSrc,
|
|
254
|
+
tokenPtTrader: ptSrc,
|
|
255
|
+
eventAuthority: this.eventAuthority,
|
|
256
|
+
program: this.orderbookProgram.programId,
|
|
257
|
+
syProgram: this.state.syProgram,
|
|
258
|
+
addressLookupTable: this.state.addressLookupTable,
|
|
259
|
+
cpiAccountOrderbook: cpiAccountOrderbook,
|
|
260
|
+
exponentCore: this.state.exponentCoreProgram,
|
|
261
|
+
})
|
|
262
|
+
.remainingAccounts(this.syCpiAccounts.getSyState)
|
|
263
|
+
.instruction();
|
|
264
|
+
}
|
|
265
|
+
/** Post offer instruction */
|
|
266
|
+
async ixWrapperPostOffer({ trader, price, amount, offerType, offerOption, virtualOffer, expirySeconds, mintSy, ptSrc: ptSrcParam, ytSrc: ytSrcParam, sySrc: sySrcParam, tokenBaseTrader: tokenBaseTraderParam, }) {
|
|
267
|
+
const ptSrc = ptSrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(this.mintPt, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
268
|
+
const ytSrc = ytSrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(this.mintYt, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
269
|
+
const sySrc = sySrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(mintSy, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
270
|
+
const tokenBaseTrader = tokenBaseTraderParam ||
|
|
271
|
+
(0, spl_token_1.getAssociatedTokenAddressSync)(this.flavor.mintBase, trader, true, this.flavor.baseTokenProgram);
|
|
272
|
+
const impliedRate = (0, utils_2.priceToImpliedRate)(price);
|
|
273
|
+
const cpiAccountOrderbook = this.pda.orderbookCpiAccounts({ orderbook: this.selfAddress });
|
|
274
|
+
const remainingAccounts = (0, utils_1.uniqueRemainingAccounts)([
|
|
275
|
+
...this.syCpiAccounts.getSyState,
|
|
276
|
+
...this.exponentCoreCpiAccounts.mergeSy,
|
|
277
|
+
...this.exponentCoreCpiAccounts.stripSy,
|
|
278
|
+
...this.exponentCoreCpiAccounts.depositYt,
|
|
279
|
+
...this.exponentCoreCpiAccounts.withdrawYt,
|
|
280
|
+
]);
|
|
281
|
+
const mintSyIx = await this.flavor.ixMintSy({
|
|
282
|
+
amountBase: "0",
|
|
283
|
+
depositor: trader,
|
|
284
|
+
depositorBaseTokenAccount: tokenBaseTrader,
|
|
285
|
+
depositorSyTokenAccount: sySrc,
|
|
286
|
+
});
|
|
287
|
+
const mintSyRemAccounts = mintSyIx.keys;
|
|
288
|
+
return this.orderbookProgram.methods
|
|
289
|
+
.wrapperPostOffer(impliedRate, amount, offerType, virtualOffer, expirySeconds, offerOption, mintSyRemAccounts.length - 1)
|
|
290
|
+
.accountsStrict({
|
|
291
|
+
orderbook: this.selfAddress,
|
|
292
|
+
vault: this.state.vault,
|
|
293
|
+
tokenEscrowSy: this.pda.orderbookEscrowSy({ orderbook: this.selfAddress }),
|
|
294
|
+
tokenEscrowYt: this.pda.orderbookEscrowYt({ orderbook: this.selfAddress }),
|
|
295
|
+
tokenEscrowPt: this.pda.orderbookEscrowPt({ orderbook: this.selfAddress }),
|
|
296
|
+
yieldPosition: this.corePda.yieldPosition({ vault: this.state.vault, owner: this.selfAddress }),
|
|
297
|
+
systemProgram: anchor_1.web3.SystemProgram.programId,
|
|
298
|
+
syProgram: this.state.syProgram,
|
|
299
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
300
|
+
exponentCore: this.state.exponentCoreProgram,
|
|
301
|
+
addressLookupTable: this.state.addressLookupTable,
|
|
302
|
+
trader,
|
|
303
|
+
tokenSyTrader: sySrc,
|
|
304
|
+
tokenYtTrader: ytSrc,
|
|
305
|
+
tokenPtTrader: ptSrc,
|
|
306
|
+
eventAuthority: this.eventAuthority,
|
|
307
|
+
program: this.orderbookProgram.programId,
|
|
308
|
+
cpiAccountOrderbook,
|
|
309
|
+
})
|
|
310
|
+
.remainingAccounts([...mintSyRemAccounts, ...remainingAccounts])
|
|
311
|
+
.instruction();
|
|
312
|
+
}
|
|
313
|
+
/** Market offer instruction */
|
|
314
|
+
async ixWrapperMarketOffer({ trader, maxPriceApy, amount, offerType, minAmountOut, virtualOffer, mintSy, ptSrc: ptSrcParam, ytSrc: ytSrcParam, sySrc: sySrcParam, tokenBaseTrader: tokenBaseTraderParam, }) {
|
|
315
|
+
const ptSrc = ptSrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(this.mintPt, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
316
|
+
const ytSrc = ytSrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(this.mintYt, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
317
|
+
const sySrc = sySrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(mintSy, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
318
|
+
const tokenBaseTrader = tokenBaseTraderParam ||
|
|
319
|
+
(0, spl_token_1.getAssociatedTokenAddressSync)(this.flavor.mintBase, trader, true, this.flavor.baseTokenProgram);
|
|
320
|
+
const impliedRate = (0, utils_2.priceToImpliedRate)(maxPriceApy);
|
|
321
|
+
const cpiAccountOrderbook = this.pda.orderbookCpiAccounts({ orderbook: this.selfAddress });
|
|
322
|
+
const remainingAccounts = (0, utils_1.uniqueRemainingAccounts)([
|
|
323
|
+
...this.syCpiAccounts.getSyState,
|
|
324
|
+
...this.exponentCoreCpiAccounts.mergeSy,
|
|
325
|
+
...this.exponentCoreCpiAccounts.stripSy,
|
|
326
|
+
...this.exponentCoreCpiAccounts.depositYt,
|
|
327
|
+
...this.exponentCoreCpiAccounts.withdrawYt,
|
|
328
|
+
]);
|
|
329
|
+
const mintSyIx = await this.flavor.ixMintSy({
|
|
330
|
+
amountBase: "0",
|
|
331
|
+
depositor: trader,
|
|
332
|
+
depositorBaseTokenAccount: tokenBaseTrader,
|
|
333
|
+
depositorSyTokenAccount: sySrc,
|
|
334
|
+
});
|
|
335
|
+
const redeemSyIx = await this.flavor.ixRedeemSy({
|
|
336
|
+
amountSy: "0",
|
|
337
|
+
redeemer: trader,
|
|
338
|
+
redeemerBaseTokenAccount: tokenBaseTrader,
|
|
339
|
+
redeemerSyTokenAccount: sySrc,
|
|
340
|
+
});
|
|
341
|
+
const depositYtAccounts = await this.depositYtAccounts({
|
|
342
|
+
owner: trader,
|
|
343
|
+
ytSrc: ytSrc,
|
|
344
|
+
});
|
|
345
|
+
const offerTypeEnum = offerType.sellYt ? types_1.OfferType.SellYT : types_1.OfferType.BuyYT;
|
|
346
|
+
const redeem_sy_or_deposit_yt_accounts = (!virtualOffer && offerTypeEnum == types_1.OfferType.SellYT) || (virtualOffer && offerTypeEnum == types_1.OfferType.BuyYT)
|
|
347
|
+
? redeemSyIx.keys
|
|
348
|
+
: depositYtAccounts.keys;
|
|
349
|
+
const mintSyRemAccounts = mintSyIx.keys;
|
|
350
|
+
const redeem_sy_or_deposit_yt_accounts_untill = mintSyRemAccounts.length + redeem_sy_or_deposit_yt_accounts.length;
|
|
351
|
+
return this.orderbookProgram.methods
|
|
352
|
+
.wrapperMarketOffer(impliedRate, amount, minAmountOut, offerType, virtualOffer, mintSyRemAccounts.length, redeem_sy_or_deposit_yt_accounts_untill)
|
|
353
|
+
.accountsStrict({
|
|
354
|
+
orderbook: this.selfAddress,
|
|
355
|
+
vault: this.state.vault,
|
|
356
|
+
tokenEscrowSy: this.pda.orderbookEscrowSy({ orderbook: this.selfAddress }),
|
|
357
|
+
tokenEscrowYt: this.pda.orderbookEscrowYt({ orderbook: this.selfAddress }),
|
|
358
|
+
tokenEscrowPt: this.pda.orderbookEscrowPt({ orderbook: this.selfAddress }),
|
|
359
|
+
yieldPosition: this.corePda.yieldPosition({ vault: this.state.vault, owner: this.selfAddress }),
|
|
360
|
+
systemProgram: anchor_1.web3.SystemProgram.programId,
|
|
361
|
+
syProgram: this.state.syProgram,
|
|
362
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
363
|
+
exponentCore: this.state.exponentCoreProgram,
|
|
364
|
+
addressLookupTable: this.state.addressLookupTable,
|
|
365
|
+
trader,
|
|
366
|
+
tokenSyTrader: sySrc,
|
|
367
|
+
tokenYtTrader: ytSrc,
|
|
368
|
+
tokenPtTrader: ptSrc,
|
|
369
|
+
eventAuthority: this.eventAuthority,
|
|
370
|
+
program: this.orderbookProgram.programId,
|
|
371
|
+
cpiAccountOrderbook,
|
|
372
|
+
})
|
|
373
|
+
.remainingAccounts(mintSyRemAccounts.concat(redeem_sy_or_deposit_yt_accounts).concat(remainingAccounts))
|
|
374
|
+
.instruction();
|
|
375
|
+
}
|
|
376
|
+
/** Wrapper remove offer instruction */
|
|
377
|
+
async ixWrapperRemoveOffer({ trader, offerIdx, mintSy, ptSrc: ptSrcParam, ytSrc: ytSrcParam, sySrc: sySrcParam, tokenBaseTrader: tokenBaseTraderParam, }) {
|
|
378
|
+
const ptSrc = ptSrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(this.mintPt, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
379
|
+
const ytSrc = ytSrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(this.mintYt, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
380
|
+
const sySrc = sySrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(mintSy, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
381
|
+
const cpiAccountOrderbook = this.pda.orderbookCpiAccounts({ orderbook: this.selfAddress });
|
|
382
|
+
const tokenBaseTrader = tokenBaseTraderParam ||
|
|
383
|
+
(0, spl_token_1.getAssociatedTokenAddressSync)(this.flavor.mintBase, trader, true, this.flavor.baseTokenProgram);
|
|
384
|
+
const redeemSyIx = await this.flavor.ixRedeemSy({
|
|
385
|
+
amountSy: "0",
|
|
386
|
+
redeemer: trader,
|
|
387
|
+
redeemerBaseTokenAccount: tokenBaseTrader,
|
|
388
|
+
redeemerSyTokenAccount: sySrc,
|
|
389
|
+
});
|
|
390
|
+
const remainingAccounts = (0, utils_1.uniqueRemainingAccounts)([
|
|
391
|
+
...this.syCpiAccounts.getSyState,
|
|
392
|
+
...this.exponentCoreCpiAccounts.withdrawYt,
|
|
393
|
+
]);
|
|
394
|
+
const redeemSyRemAccounts = redeemSyIx.keys;
|
|
395
|
+
return this.orderbookProgram.methods
|
|
396
|
+
.wrapperRemoveOffer(offerIdx, redeemSyIx.keys.length - 1)
|
|
397
|
+
.accountsStrict({
|
|
398
|
+
orderbook: this.selfAddress,
|
|
399
|
+
tokenEscrowSy: this.pda.orderbookEscrowSy({ orderbook: this.selfAddress }),
|
|
400
|
+
tokenEscrowYt: this.pda.orderbookEscrowYt({ orderbook: this.selfAddress }),
|
|
401
|
+
tokenEscrowPt: this.pda.orderbookEscrowPt({ orderbook: this.selfAddress }),
|
|
402
|
+
systemProgram: anchor_1.web3.SystemProgram.programId,
|
|
403
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
404
|
+
trader,
|
|
405
|
+
tokenSyTrader: sySrc,
|
|
406
|
+
tokenYtTrader: ytSrc,
|
|
407
|
+
tokenPtTrader: ptSrc,
|
|
408
|
+
eventAuthority: this.eventAuthority,
|
|
409
|
+
program: this.orderbookProgram.programId,
|
|
410
|
+
syProgram: this.state.syProgram,
|
|
411
|
+
addressLookupTable: this.state.addressLookupTable,
|
|
412
|
+
cpiAccountOrderbook: cpiAccountOrderbook,
|
|
413
|
+
exponentCore: this.state.exponentCoreProgram,
|
|
414
|
+
vault: this.state.vault,
|
|
415
|
+
})
|
|
416
|
+
.remainingAccounts([...redeemSyRemAccounts, ...remainingAccounts])
|
|
417
|
+
.instruction();
|
|
418
|
+
}
|
|
419
|
+
/** Wrapper collect interest instruction */
|
|
420
|
+
async ixWrapperCollectInterest({ trader, mintSy, sySrc: sySrcParam, tokenBaseTrader: tokenBaseTraderParam, }) {
|
|
421
|
+
const sySrc = sySrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(mintSy, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
422
|
+
const cpiAccountOrderbook = this.pda.orderbookCpiAccounts({ orderbook: this.selfAddress });
|
|
423
|
+
const tokenBaseTrader = tokenBaseTraderParam ||
|
|
424
|
+
(0, spl_token_1.getAssociatedTokenAddressSync)(this.flavor.mintBase, trader, true, this.flavor.baseTokenProgram);
|
|
425
|
+
const redeemSyIx = await this.flavor.ixRedeemSy({
|
|
426
|
+
amountSy: "0",
|
|
427
|
+
redeemer: trader,
|
|
428
|
+
redeemerBaseTokenAccount: tokenBaseTrader,
|
|
429
|
+
redeemerSyTokenAccount: sySrc,
|
|
430
|
+
});
|
|
431
|
+
const remainingAccounts = (0, utils_1.uniqueRemainingAccounts)([
|
|
432
|
+
...this.syCpiAccounts.getSyState,
|
|
433
|
+
...this.exponentCoreCpiAccounts.collectInterest,
|
|
434
|
+
...this.syCpiAccounts.withdrawSy,
|
|
435
|
+
]);
|
|
436
|
+
const redeemSyRemAccounts = redeemSyIx.keys;
|
|
437
|
+
return this.orderbookProgram.methods
|
|
438
|
+
.wrapperCollectInterest(redeemSyIx.keys.length - 1)
|
|
439
|
+
.accountsStrict({
|
|
440
|
+
orderbook: this.selfAddress,
|
|
441
|
+
tokenEscrowSy: this.pda.orderbookEscrowSy({ orderbook: this.selfAddress }),
|
|
442
|
+
systemProgram: anchor_1.web3.SystemProgram.programId,
|
|
443
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
444
|
+
trader,
|
|
445
|
+
tokenSyTrader: sySrc,
|
|
446
|
+
eventAuthority: this.eventAuthority,
|
|
447
|
+
program: this.orderbookProgram.programId,
|
|
448
|
+
syProgram: this.state.syProgram,
|
|
449
|
+
addressLookupTable: this.state.addressLookupTable,
|
|
450
|
+
cpiAccountOrderbook: cpiAccountOrderbook,
|
|
451
|
+
exponentCore: this.state.exponentCoreProgram,
|
|
452
|
+
vault: this.state.vault,
|
|
453
|
+
})
|
|
454
|
+
.remainingAccounts([...redeemSyRemAccounts, ...remainingAccounts])
|
|
455
|
+
.instruction();
|
|
456
|
+
}
|
|
457
|
+
/** Wrapper withdraw funds instruction */
|
|
458
|
+
async ixWrapperWithdrawFunds({ trader, mintSy, ptAmount, ytAmount, syAmount, ptSrc: ptSrcParam, ytSrc: ytSrcParam, sySrc: sySrcParam, tokenBaseTrader: tokenBaseTraderParam, }) {
|
|
459
|
+
const ptSrc = ptSrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(this.mintPt, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
460
|
+
const ytSrc = ytSrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(this.mintYt, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
461
|
+
const sySrc = sySrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(mintSy, trader, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
462
|
+
const cpiAccountOrderbook = this.pda.orderbookCpiAccounts({ orderbook: this.selfAddress });
|
|
463
|
+
const tokenBaseTrader = tokenBaseTraderParam ||
|
|
464
|
+
(0, spl_token_1.getAssociatedTokenAddressSync)(this.flavor.mintBase, trader, true, this.flavor.baseTokenProgram);
|
|
465
|
+
const redeemSyIx = await this.flavor.ixRedeemSy({
|
|
466
|
+
amountSy: "0",
|
|
467
|
+
redeemer: trader,
|
|
468
|
+
redeemerBaseTokenAccount: tokenBaseTrader,
|
|
469
|
+
redeemerSyTokenAccount: sySrc,
|
|
470
|
+
});
|
|
471
|
+
const redeemSyRemAccounts = redeemSyIx.keys;
|
|
472
|
+
const remainingAccounts = (0, utils_1.uniqueRemainingAccounts)([
|
|
473
|
+
...this.syCpiAccounts.getSyState,
|
|
474
|
+
...this.exponentCoreCpiAccounts.withdrawYt,
|
|
475
|
+
]);
|
|
476
|
+
return this.orderbookProgram.methods
|
|
477
|
+
.wrapperWithdrawFunds(ptAmount, ytAmount, syAmount, redeemSyIx.keys.length - 1)
|
|
478
|
+
.accountsStrict({
|
|
479
|
+
orderbook: this.selfAddress,
|
|
480
|
+
tokenEscrowSy: this.pda.orderbookEscrowSy({ orderbook: this.selfAddress }),
|
|
481
|
+
systemProgram: anchor_1.web3.SystemProgram.programId,
|
|
482
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
483
|
+
trader,
|
|
484
|
+
tokenSyTrader: sySrc,
|
|
485
|
+
eventAuthority: this.eventAuthority,
|
|
486
|
+
program: this.orderbookProgram.programId,
|
|
487
|
+
syProgram: this.state.syProgram,
|
|
488
|
+
addressLookupTable: this.state.addressLookupTable,
|
|
489
|
+
cpiAccountOrderbook: cpiAccountOrderbook,
|
|
490
|
+
exponentCore: this.state.exponentCoreProgram,
|
|
491
|
+
vault: this.state.vault,
|
|
492
|
+
tokenEscrowYt: this.pda.orderbookEscrowYt({ orderbook: this.selfAddress }),
|
|
493
|
+
tokenEscrowPt: this.pda.orderbookEscrowPt({ orderbook: this.selfAddress }),
|
|
494
|
+
tokenYtTrader: ytSrc,
|
|
495
|
+
tokenPtTrader: ptSrc,
|
|
496
|
+
})
|
|
497
|
+
.remainingAccounts(redeemSyRemAccounts.concat(remainingAccounts))
|
|
498
|
+
.instruction();
|
|
499
|
+
}
|
|
500
|
+
/** Wrapper collect admin fee instruction */
|
|
501
|
+
async ixWrapperCollectAdminFee({ admin, mintSy }) {
|
|
502
|
+
const ptSrc = (0, spl_token_1.getAssociatedTokenAddressSync)(this.mintPt, admin, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
503
|
+
const ytSrc = (0, spl_token_1.getAssociatedTokenAddressSync)(this.mintYt, admin, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
504
|
+
const sySrc = (0, spl_token_1.getAssociatedTokenAddressSync)(mintSy, admin, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
505
|
+
const cpiAccountOrderbook = this.pda.orderbookCpiAccounts({ orderbook: this.selfAddress });
|
|
506
|
+
const tokenBaseTrader = (0, spl_token_1.getAssociatedTokenAddressSync)(this.flavor.mintBase, admin, true, this.flavor.baseTokenProgram);
|
|
507
|
+
const redeemSyIx = await this.flavor.ixRedeemSy({
|
|
508
|
+
amountSy: "0",
|
|
509
|
+
redeemer: admin,
|
|
510
|
+
redeemerBaseTokenAccount: tokenBaseTrader,
|
|
511
|
+
redeemerSyTokenAccount: sySrc,
|
|
512
|
+
});
|
|
513
|
+
const redeemSyRemAccounts = redeemSyIx.keys;
|
|
514
|
+
const remainingAccounts = (0, utils_1.uniqueRemainingAccounts)([
|
|
515
|
+
...this.syCpiAccounts.getSyState,
|
|
516
|
+
...this.exponentCoreCpiAccounts.collectInterest,
|
|
517
|
+
...this.exponentCoreCpiAccounts.withdrawYt,
|
|
518
|
+
]);
|
|
519
|
+
return this.orderbookProgram.methods
|
|
520
|
+
.wrapperCollectAdminFee(redeemSyIx.keys.length - 1)
|
|
521
|
+
.accountsStrict({
|
|
522
|
+
orderbook: this.selfAddress,
|
|
523
|
+
tokenEscrowSy: this.pda.orderbookEscrowSy({ orderbook: this.selfAddress }),
|
|
524
|
+
systemProgram: anchor_1.web3.SystemProgram.programId,
|
|
525
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
526
|
+
admin,
|
|
527
|
+
tokenSyAdmin: sySrc,
|
|
528
|
+
eventAuthority: this.eventAuthority,
|
|
529
|
+
program: this.orderbookProgram.programId,
|
|
530
|
+
syProgram: this.state.syProgram,
|
|
531
|
+
addressLookupTable: this.state.addressLookupTable,
|
|
532
|
+
cpiAccountOrderbook: cpiAccountOrderbook,
|
|
533
|
+
exponentCore: this.state.exponentCoreProgram,
|
|
534
|
+
vault: this.state.vault,
|
|
535
|
+
tokenEscrowYt: this.pda.orderbookEscrowYt({ orderbook: this.selfAddress }),
|
|
536
|
+
tokenEscrowPt: this.pda.orderbookEscrowPt({ orderbook: this.selfAddress }),
|
|
537
|
+
tokenYtAdmin: ytSrc,
|
|
538
|
+
tokenPtAdmin: ptSrc,
|
|
539
|
+
})
|
|
540
|
+
.remainingAccounts(redeemSyRemAccounts.concat(remainingAccounts))
|
|
541
|
+
.instruction();
|
|
542
|
+
}
|
|
543
|
+
/** Wrapper collect admin emission instruction */
|
|
544
|
+
async ixCollectAdminEmission({ emissionEscrow, amount, index, treasuryEmissionTokenAccount, tokenEmissionAdmin, }) {
|
|
545
|
+
const claimEmissionAccounts = this.collectAdminEmissionCoreAccounts({
|
|
546
|
+
emissionDst: tokenEmissionAdmin,
|
|
547
|
+
emissionEscrow,
|
|
548
|
+
treasuryEmissionTokenAccount,
|
|
549
|
+
});
|
|
550
|
+
const remainingAccounts = [...claimEmissionAccounts, ...this.syCpiAccounts.claimEmission[index]];
|
|
551
|
+
return this.orderbookProgram.methods
|
|
552
|
+
.collectAdminEmission(amount, index)
|
|
553
|
+
.accountsStrict({
|
|
554
|
+
orderbook: this.selfAddress,
|
|
555
|
+
eventAuthority: this.eventAuthority,
|
|
556
|
+
program: this.orderbookProgram.programId,
|
|
557
|
+
exponentCore: this.state.exponentCoreProgram,
|
|
558
|
+
admin: this.state.admin,
|
|
559
|
+
})
|
|
560
|
+
.remainingAccounts(remainingAccounts)
|
|
561
|
+
.instruction();
|
|
562
|
+
}
|
|
563
|
+
collectAdminEmissionCoreAccounts({ emissionDst, emissionEscrow, treasuryEmissionTokenAccount, }) {
|
|
564
|
+
return [
|
|
565
|
+
{
|
|
566
|
+
pubkey: this.selfAddress,
|
|
567
|
+
isSigner: false,
|
|
568
|
+
isWritable: true,
|
|
569
|
+
},
|
|
570
|
+
{
|
|
571
|
+
pubkey: this.state.vault,
|
|
572
|
+
isSigner: false,
|
|
573
|
+
isWritable: true,
|
|
574
|
+
},
|
|
575
|
+
{
|
|
576
|
+
pubkey: this.state.yieldPosition,
|
|
577
|
+
isSigner: false,
|
|
578
|
+
isWritable: true,
|
|
579
|
+
},
|
|
580
|
+
{
|
|
581
|
+
pubkey: this.state.syProgram,
|
|
582
|
+
isSigner: false,
|
|
583
|
+
isWritable: false,
|
|
584
|
+
},
|
|
585
|
+
{
|
|
586
|
+
pubkey: emissionEscrow,
|
|
587
|
+
isSigner: false,
|
|
588
|
+
isWritable: true,
|
|
589
|
+
},
|
|
590
|
+
{
|
|
591
|
+
pubkey: emissionDst,
|
|
592
|
+
isSigner: false,
|
|
593
|
+
isWritable: true,
|
|
594
|
+
},
|
|
595
|
+
{
|
|
596
|
+
pubkey: treasuryEmissionTokenAccount,
|
|
597
|
+
isSigner: false,
|
|
598
|
+
isWritable: true,
|
|
599
|
+
},
|
|
600
|
+
{
|
|
601
|
+
pubkey: spl_token_1.TOKEN_PROGRAM_ID,
|
|
602
|
+
isSigner: false,
|
|
603
|
+
isWritable: false,
|
|
604
|
+
},
|
|
605
|
+
{
|
|
606
|
+
pubkey: this.coreEventAuthority,
|
|
607
|
+
isSigner: false,
|
|
608
|
+
isWritable: false,
|
|
609
|
+
},
|
|
610
|
+
{
|
|
611
|
+
pubkey: this.state.exponentCoreProgram,
|
|
612
|
+
isSigner: false,
|
|
613
|
+
isWritable: false,
|
|
614
|
+
},
|
|
615
|
+
];
|
|
616
|
+
}
|
|
617
|
+
/** Wrapper collect admin fee instruction */
|
|
618
|
+
async ixRemoveExpiredOffers({ admin }) {
|
|
619
|
+
const cpiAccountOrderbook = this.pda.orderbookCpiAccounts({ orderbook: this.selfAddress });
|
|
620
|
+
const remainingAccounts = (0, utils_1.uniqueRemainingAccounts)([...this.syCpiAccounts.getSyState]);
|
|
621
|
+
return this.orderbookProgram.methods
|
|
622
|
+
.removeExpiredOffers()
|
|
623
|
+
.accountsStrict({
|
|
624
|
+
orderbook: this.selfAddress,
|
|
625
|
+
admin,
|
|
626
|
+
eventAuthority: this.eventAuthority,
|
|
627
|
+
program: this.orderbookProgram.programId,
|
|
628
|
+
syProgram: this.state.syProgram,
|
|
629
|
+
addressLookupTable: this.state.addressLookupTable,
|
|
630
|
+
cpiAccountOrderbook: cpiAccountOrderbook,
|
|
631
|
+
})
|
|
632
|
+
.remainingAccounts(remainingAccounts)
|
|
633
|
+
.instruction();
|
|
634
|
+
}
|
|
635
|
+
getQuote({ priceApy: rawPriceApy, inAmount: inAmountRaw, direction, unixNow, syExchangeRate, }) {
|
|
636
|
+
const offerDirection = (0, utils_2.getOfferDirectionFromQuoteDirection)(direction);
|
|
637
|
+
const { isVirtual: isQuoteVirtualOffer, offerType: quoteOfferType } = (0, utils_2.getOfferTypeFromTradeDirection)(offerDirection);
|
|
638
|
+
//? If inAmount is in base, convert Base to SY
|
|
639
|
+
const inAmount = (0, utils_2.isQuoteInputTokenBase)(direction) ? inAmountRaw / syExchangeRate : inAmountRaw;
|
|
640
|
+
//? Offer on contract stores its APY price in ln(1+ apy) view. Convert it before all calculations
|
|
641
|
+
const priceApy = rawPriceApy &&
|
|
642
|
+
(0, math_1.roundPriceApy)((0, utils_2.priceToImpliedRate)(rawPriceApy), this.state.configurationOptions.priceDecimals) / math_1.PRICE_BASE_POINTS;
|
|
643
|
+
const secUntilMaturity = this.state.financials.expirationTs - unixNow;
|
|
644
|
+
//? If offerType === BuyYT, sort by priceApy asc, otherwise by priceApy desc
|
|
645
|
+
const suitableOffersByType = this.getOffers()
|
|
646
|
+
.filter(({ type, expiryAt, priceApy: offerPriceApy }) => {
|
|
647
|
+
const isExpired = expiryAt < unixNow;
|
|
648
|
+
const satisfyPriceApy = (() => {
|
|
649
|
+
if (!priceApy)
|
|
650
|
+
return true;
|
|
651
|
+
if (quoteOfferType === types_1.OfferType.BuyYT)
|
|
652
|
+
return offerPriceApy <= priceApy;
|
|
653
|
+
return offerPriceApy >= priceApy;
|
|
654
|
+
})();
|
|
655
|
+
return type !== quoteOfferType && !isExpired && satisfyPriceApy;
|
|
656
|
+
})
|
|
657
|
+
.sort((a, b) => {
|
|
658
|
+
const expiryCompare = a.expiryAt - b.expiryAt;
|
|
659
|
+
//? If we buyYT -- sort priceApy ascending, otherwise descending
|
|
660
|
+
if (quoteOfferType === types_1.OfferType.BuyYT) {
|
|
661
|
+
return a.priceApy - b.priceApy || expiryCompare;
|
|
662
|
+
}
|
|
663
|
+
return b.priceApy - a.priceApy || expiryCompare;
|
|
664
|
+
});
|
|
665
|
+
let remainingAmount = inAmount;
|
|
666
|
+
let outAmount = 0;
|
|
667
|
+
let offersUsed = 0;
|
|
668
|
+
let makerFees = 0;
|
|
669
|
+
let takerFees = 0;
|
|
670
|
+
suitableOffersByType.forEach((counterOffer) => {
|
|
671
|
+
if (remainingAmount <= 0)
|
|
672
|
+
return;
|
|
673
|
+
if (offersUsed > math_1.MAX_FILLED_OFFERS)
|
|
674
|
+
return;
|
|
675
|
+
//? How much counter type tokens we request using current counter offer price+type
|
|
676
|
+
//? Returns YT amount for SY->YT quote
|
|
677
|
+
//? Returns SY amount for quote YT->SY quote
|
|
678
|
+
//? Returns SY amount for SY->PT quote. How much SY should be (in theory) in counter offer to satisfy strip: SY_get->PT+YT
|
|
679
|
+
//? Returns YT amount for PT->SY quote. YT amount in merge operation: PT+YT->SY
|
|
680
|
+
const remainingAmountInCounterOfferToken = (0, math_1.getOfferAmountToGet)({
|
|
681
|
+
type: quoteOfferType,
|
|
682
|
+
amount: remainingAmount,
|
|
683
|
+
priceApy: counterOffer.priceApy,
|
|
684
|
+
isVirtual: isQuoteVirtualOffer,
|
|
685
|
+
userEscrow: anchor_1.web3.PublicKey.default,
|
|
686
|
+
expiryAt: Infinity,
|
|
687
|
+
createdAt: unixNow,
|
|
688
|
+
}, syExchangeRate, secUntilMaturity);
|
|
689
|
+
//? YT amount for SY->YT quote
|
|
690
|
+
//? SY amount for quote YT->SY quote
|
|
691
|
+
//? SY amount for SY->PT quote
|
|
692
|
+
//? YT amount for PT->SY quote
|
|
693
|
+
const offerSize = (0, math_1.getOfferSize)(counterOffer, secUntilMaturity, syExchangeRate);
|
|
694
|
+
if (remainingAmountInCounterOfferToken > offerSize) {
|
|
695
|
+
//? How much token you can sell into this offer. To extract all the liquidity (offer.amount)
|
|
696
|
+
const offerToGet = (0, math_1.getOfferAmountToGetNoVirtual)({
|
|
697
|
+
...counterOffer,
|
|
698
|
+
amount: offerSize,
|
|
699
|
+
}, syExchangeRate, secUntilMaturity);
|
|
700
|
+
const remainingAmountFromCounterOffer = (0, math_1.calcRemainingAmountOnCounterOfferFilling)(offerToGet, offerSize, isQuoteVirtualOffer, quoteOfferType, syExchangeRate);
|
|
701
|
+
const outAmountFromCounterOffer = (0, math_1.calcOutAmountOnCounterOfferFilling)(offerToGet, offerSize, isQuoteVirtualOffer, quoteOfferType, syExchangeRate, this.makerFeeRate);
|
|
702
|
+
remainingAmount -= remainingAmountFromCounterOffer;
|
|
703
|
+
outAmount += outAmountFromCounterOffer.outAmount;
|
|
704
|
+
makerFees += outAmountFromCounterOffer.tradeFee;
|
|
705
|
+
}
|
|
706
|
+
else {
|
|
707
|
+
//? How much token you can sell into this offer. To extract all the liquidity (offer.amount)
|
|
708
|
+
const offerToGet = (0, math_1.getOfferAmountToGetNoVirtual)({
|
|
709
|
+
...counterOffer,
|
|
710
|
+
amount: remainingAmountInCounterOfferToken,
|
|
711
|
+
}, syExchangeRate, secUntilMaturity);
|
|
712
|
+
const outAmountFromCounterOffer = (0, math_1.calcOutAmountOnCounterOfferFilling)(offerToGet, remainingAmountInCounterOfferToken, isQuoteVirtualOffer, quoteOfferType, syExchangeRate, this.makerFeeRate);
|
|
713
|
+
remainingAmount = 0;
|
|
714
|
+
outAmount += outAmountFromCounterOffer.outAmount;
|
|
715
|
+
makerFees += outAmountFromCounterOffer.tradeFee;
|
|
716
|
+
}
|
|
717
|
+
offersUsed++;
|
|
718
|
+
});
|
|
719
|
+
//? If output is in base, convert SY to Base
|
|
720
|
+
const outAmountAdjusted = (0, utils_2.isQuoteOutputTokenBase)(direction) ? outAmount * syExchangeRate : outAmount;
|
|
721
|
+
return {
|
|
722
|
+
outAmount: Math.floor(outAmountAdjusted),
|
|
723
|
+
makerFees: Math.floor(makerFees),
|
|
724
|
+
takerFees: Math.floor(takerFees),
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
async depositYtAccounts({ owner, ytSrc: ytSrcParam, }) {
|
|
728
|
+
const ytSrc = ytSrcParam || (0, spl_token_1.getAssociatedTokenAddressSync)(this.mintYt, owner, true, spl_token_1.TOKEN_PROGRAM_ID);
|
|
729
|
+
const userYieldPosition = this.corePda.yieldPosition({ vault: this.state.vault, owner });
|
|
730
|
+
const yieldPosition = this.corePda.vaultYieldPosition({ vault: this.state.vault });
|
|
731
|
+
const escrowYt = this.corePda.escrowYt({ vault: this.state.vault });
|
|
732
|
+
const mainAccounts = {
|
|
733
|
+
depositor: owner,
|
|
734
|
+
ytSrc,
|
|
735
|
+
vault: this.state.vault,
|
|
736
|
+
userYieldPosition: userYieldPosition,
|
|
737
|
+
escrowYt: escrowYt,
|
|
738
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
739
|
+
syProgram: this.state.syProgram,
|
|
740
|
+
addressLookupTable: this.vaulLookupTable,
|
|
741
|
+
yieldPosition,
|
|
742
|
+
systemProgram: anchor_1.web3.SystemProgram.programId,
|
|
743
|
+
eventAuthority: this.coreEventAuthority,
|
|
744
|
+
program: this.state.exponentCoreProgram,
|
|
745
|
+
};
|
|
746
|
+
const remainingAccounts = this.syCpiAccounts.getSyState;
|
|
747
|
+
const depositIx = await this.coreProgram.methods
|
|
748
|
+
.depositYt(new anchor_1.BN(0))
|
|
749
|
+
.accountsStrict(mainAccounts)
|
|
750
|
+
.remainingAccounts(remainingAccounts)
|
|
751
|
+
.instruction();
|
|
752
|
+
return depositIx;
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
exports.Orderbook = Orderbook;
|
|
756
|
+
//# sourceMappingURL=orderbook.js.map
|