hedge-web3 0.2.20 → 0.2.24
Sign up to get free protection for your applications and to get access to all the features.
- package/declarations/Constants.d.ts +15 -0
- package/declarations/idl/pyth.d.ts +44 -24
- package/declarations/idl/switchboard.d.ts +47 -0
- package/declarations/idl/vault.d.ts +857 -157
- package/declarations/index.d.ts +26 -20
- package/declarations/instructions/closeLiquidationPoolPosition.d.ts +2 -2
- package/declarations/instructions/createReferralAccount.d.ts +17 -0
- package/declarations/instructions/createUserReferralAccount.d.ts +5 -0
- package/declarations/instructions/depositLiquidationPool.d.ts +2 -2
- package/declarations/instructions/loanVault.d.ts +2 -2
- package/declarations/instructions/psmEditAccount.d.ts +12 -0
- package/declarations/instructions/psmMintUsh.d.ts +2 -2
- package/declarations/instructions/psmRedeemUsh.d.ts +2 -2
- package/declarations/instructions/referralClaimFees.d.ts +16 -0
- package/declarations/instructions/transferVault.d.ts +3 -17
- package/declarations/instructions/updateReferralAccount.d.ts +13 -0
- package/declarations/instructions/updateReferralState.d.ts +20 -0
- package/declarations/instructions/updateVaultType.d.ts +1 -0
- package/declarations/state/VaultAccount.d.ts +2 -1
- package/lib/Constants.js +36 -1
- package/lib/idl/pyth.js +44 -24
- package/lib/idl/switchboard.js +49 -0
- package/lib/idl/vault.js +850 -150
- package/lib/index.js +26 -20
- package/lib/instructions/closeLiquidationPoolPosition.js +16 -3
- package/lib/instructions/createReferralAccount.js +80 -0
- package/lib/instructions/createUserReferralAccount.js +61 -0
- package/lib/instructions/depositLiquidationPool.js +16 -5
- package/lib/instructions/loanVault.js +16 -3
- package/lib/instructions/psmEditAccount.js +60 -0
- package/lib/instructions/psmMintUsh.js +14 -3
- package/lib/instructions/psmRedeemUsh.js +14 -3
- package/lib/instructions/referralClaimFees.js +84 -0
- package/lib/instructions/transferVault.js +10 -24
- package/lib/instructions/updateReferralAccount.js +50 -0
- package/lib/instructions/updateReferralState.js +57 -0
- package/lib/instructions/updateVaultType.js +10 -9
- package/package.json +1 -1
- package/src/Constants.ts +42 -0
- package/src/idl/pyth.ts +88 -48
- package/src/idl/switchboard.ts +93 -0
- package/src/idl/vault.ts +1762 -362
- package/src/index.ts +28 -24
- package/src/instructions/closeLiquidationPoolPosition.ts +25 -1
- package/src/instructions/createReferralAccount.ts +127 -0
- package/src/instructions/createUserReferralAccount.ts +86 -0
- package/src/instructions/depositLiquidationPool.ts +24 -4
- package/src/instructions/loanVault.ts +20 -1
- package/src/instructions/{psmEditAccount.rs → psmEditAccount.ts} +0 -0
- package/src/instructions/psmMintUsh.ts +20 -1
- package/src/instructions/psmRedeemUsh.ts +20 -1
- package/src/instructions/referralClaimFees.ts +159 -0
- package/src/instructions/transferVault.ts +21 -58
- package/src/instructions/updateReferralAccount.ts +77 -0
- package/src/instructions/updateReferralState.ts +93 -0
- package/src/instructions/updateVaultType.ts +3 -0
- package/src/state/VaultAccount.ts +1 -1
package/lib/index.js
CHANGED
@@ -10,36 +10,42 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
10
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
11
11
|
};
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
13
|
-
__exportStar(require("./
|
14
|
-
__exportStar(require("./
|
15
|
-
__exportStar(require("./
|
16
|
-
__exportStar(require("./instructions/claimStakingPoolPosition"), exports);
|
17
|
-
__exportStar(require("./instructions/depositLiquidationPool"), exports);
|
18
|
-
__exportStar(require("./instructions/closeLiquidationPoolPosition"), exports);
|
13
|
+
__exportStar(require("./Constants"), exports);
|
14
|
+
__exportStar(require("./HedgeDecimal"), exports);
|
15
|
+
__exportStar(require("./idl/vault"), exports);
|
19
16
|
__exportStar(require("./instructions/claimLiquidationPoolPosition"), exports);
|
17
|
+
__exportStar(require("./instructions/claimStakingPoolPosition"), exports);
|
20
18
|
__exportStar(require("./instructions/closeClaimedLiquidationPoolPosition"), exports);
|
19
|
+
__exportStar(require("./instructions/closeLiquidationPoolPosition"), exports);
|
20
|
+
__exportStar(require("./instructions/createReferralAccount"), exports);
|
21
|
+
__exportStar(require("./instructions/createStakingPool"), exports);
|
22
|
+
__exportStar(require("./instructions/createUserReferralAccount"), exports);
|
21
23
|
__exportStar(require("./instructions/createVault"), exports);
|
22
|
-
__exportStar(require("./instructions/
|
23
|
-
__exportStar(require("./instructions/
|
24
|
+
__exportStar(require("./instructions/depositLiquidationPool"), exports);
|
25
|
+
__exportStar(require("./instructions/depositStakingPool"), exports);
|
24
26
|
__exportStar(require("./instructions/depositVault"), exports);
|
25
|
-
__exportStar(require("./instructions/
|
27
|
+
__exportStar(require("./instructions/initHedgeFoundation"), exports);
|
28
|
+
__exportStar(require("./instructions/initHedgeFoundationTokens"), exports);
|
29
|
+
__exportStar(require("./instructions/liquidateVault"), exports);
|
26
30
|
__exportStar(require("./instructions/loanVault"), exports);
|
27
|
-
__exportStar(require("./instructions/
|
31
|
+
__exportStar(require("./instructions/psmMintUsh"), exports);
|
32
|
+
__exportStar(require("./instructions/psmRedeemUsh"), exports);
|
28
33
|
__exportStar(require("./instructions/redeemVault"), exports);
|
29
|
-
__exportStar(require("./instructions/
|
34
|
+
__exportStar(require("./instructions/referralClaimFees"), exports);
|
30
35
|
__exportStar(require("./instructions/refreshOraclePrice"), exports);
|
31
|
-
__exportStar(require("./instructions/
|
32
|
-
__exportStar(require("./instructions/initHedgeFoundationTokens"), exports);
|
36
|
+
__exportStar(require("./instructions/repayVault"), exports);
|
33
37
|
__exportStar(require("./instructions/setHalted"), exports);
|
38
|
+
__exportStar(require("./instructions/transferVault"), exports);
|
39
|
+
__exportStar(require("./instructions/updateReferralAccount"), exports);
|
40
|
+
__exportStar(require("./instructions/updateReferralState"), exports);
|
34
41
|
__exportStar(require("./instructions/updateVaultType"), exports);
|
35
|
-
__exportStar(require("./
|
36
|
-
__exportStar(require("./
|
37
|
-
__exportStar(require("./state/VaultAccount"), exports);
|
38
|
-
__exportStar(require("./state/VaultHistoryEvent"), exports);
|
39
|
-
__exportStar(require("./state/StakingPool"), exports);
|
40
|
-
__exportStar(require("./state/StakingPoolPosition"), exports);
|
42
|
+
__exportStar(require("./instructions/withdrawStakingPool"), exports);
|
43
|
+
__exportStar(require("./instructions/withdrawVault"), exports);
|
41
44
|
__exportStar(require("./state/LiquidationPoolEra"), exports);
|
42
45
|
__exportStar(require("./state/LiquidationPoolState"), exports);
|
43
46
|
__exportStar(require("./state/LiquidationPosition"), exports);
|
47
|
+
__exportStar(require("./state/StakingPool"), exports);
|
48
|
+
__exportStar(require("./state/StakingPoolPosition"), exports);
|
49
|
+
__exportStar(require("./state/VaultAccount"), exports);
|
50
|
+
__exportStar(require("./state/VaultHistoryEvent"), exports);
|
44
51
|
__exportStar(require("./utils/getLinkedListAccounts"), exports);
|
45
|
-
__exportStar(require("./idl/vault"), exports);
|
@@ -19,23 +19,33 @@ const web3_js_1 = require("@solana/web3.js");
|
|
19
19
|
const Errors_1 = require("../utils/Errors");
|
20
20
|
const Constants_1 = require("../Constants");
|
21
21
|
const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
|
22
|
-
function closeLiquidationPoolPosition(program, provider, poolPosition, payer, overrideStartTime) {
|
22
|
+
function closeLiquidationPoolPosition(program, provider, poolPosition, payer, overrideStartTime, referrer) {
|
23
23
|
return __awaiter(this, void 0, void 0, function* () {
|
24
24
|
const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
|
25
25
|
const payerUshAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, payer.publicKey);
|
26
26
|
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
27
|
+
const referralStatePublicKey = yield (0, Constants_1.getReferralStatePublicKey)();
|
27
28
|
const liquidationPositionAccount = yield program.account.liquidationPosition.fetch(poolPosition);
|
28
29
|
const poolEra = liquidationPositionAccount.era;
|
29
30
|
const hedgeMint = yield (0, Constants_1.getHedgeMintPublicKey)();
|
30
31
|
const payerAssociatedHedgeAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, hedgeMint, payer.publicKey);
|
31
32
|
const communityAssociatedHedgeTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, hedgeMint, vaultSystemStatePublicKey, true);
|
32
|
-
|
33
|
+
let referralAccountPublicKey = null;
|
34
|
+
if (typeof referrer === 'undefined') {
|
35
|
+
referralAccountPublicKey = yield (0, Constants_1.getReferralAccountPublicKey)(vaultSystemStatePublicKey);
|
36
|
+
}
|
37
|
+
else {
|
38
|
+
referralAccountPublicKey = yield (0, Constants_1.getReferralAccountPublicKey)(referrer);
|
39
|
+
}
|
40
|
+
// Derive the user referral account public key
|
41
|
+
const userReferralAccountPublicKey = yield (0, Constants_1.getUserReferralAccountPublicKey)(payer.publicKey);
|
42
|
+
const transaction = new web3_js_1.Transaction().add(yield closeLiquidationPoolPositionInstruction(program, poolEra, poolPosition, payer.publicKey, payerUshAccount.address, payerAssociatedHedgeAccount.address, communityAssociatedHedgeTokenAccount.address, userReferralAccountPublicKey, referralAccountPublicKey, referralStatePublicKey, overrideStartTime));
|
33
43
|
yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer]).catch(Errors_1.parseAnchorErrors);
|
34
44
|
return poolPosition;
|
35
45
|
});
|
36
46
|
}
|
37
47
|
exports.closeLiquidationPoolPosition = closeLiquidationPoolPosition;
|
38
|
-
function closeLiquidationPoolPositionInstruction(program, poolEra, poolPosition, payerPublicKey, payerUshAccount, payerAssociatedHedgeAccount, communityAssociatedHedgeTokenAccount, overrideStartTime) {
|
48
|
+
function closeLiquidationPoolPositionInstruction(program, poolEra, poolPosition, payerPublicKey, payerUshAccount, payerAssociatedHedgeAccount, communityAssociatedHedgeTokenAccount, userReferralAccountPublicKey, referralAccountPublicKey, referralStatePublicKey, overrideStartTime) {
|
39
49
|
return __awaiter(this, void 0, void 0, function* () {
|
40
50
|
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
41
51
|
const ushMint = yield (0, Constants_1.getUshMintPublicKey)();
|
@@ -57,6 +67,9 @@ function closeLiquidationPoolPositionInstruction(program, poolEra, poolPosition,
|
|
57
67
|
ushMint: ushMint,
|
58
68
|
payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
|
59
69
|
communityAssociatedHedgeTokenAccount: communityAssociatedHedgeTokenAccount,
|
70
|
+
userReferralAccount: userReferralAccountPublicKey,
|
71
|
+
referralAccount: referralAccountPublicKey,
|
72
|
+
referralState: referralStatePublicKey,
|
60
73
|
associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
|
61
74
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
62
75
|
systemProgram: web3_js_1.SystemProgram.programId,
|
@@ -0,0 +1,80 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
13
|
+
};
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
15
|
+
exports.createReferralAccountInstruction = exports.createReferralAccount = void 0;
|
16
|
+
const anchor_1 = require("@project-serum/anchor");
|
17
|
+
const spl_token_1 = require("@solana/spl-token");
|
18
|
+
const web3_js_1 = require("@solana/web3.js");
|
19
|
+
const Constants_1 = require("../Constants");
|
20
|
+
const Errors_1 = require("../utils/Errors");
|
21
|
+
const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
|
22
|
+
/** @type {Function} - Creates a new referral account.
|
23
|
+
* This checks the user has enough HDG in their wallet or staked and
|
24
|
+
* then allows them to create a referral account. If they meet the PSM referral threshold,
|
25
|
+
* they will be eligible for PSM cut.
|
26
|
+
* Params:
|
27
|
+
* - program: Program<Vault> : The program instance of Hedge Vault program
|
28
|
+
* - provider: Provider : Current connection
|
29
|
+
* - payer: Signer : who we are creating the referral account for
|
30
|
+
* - poolPublicKey: PublicKey : a key to a pool position. If the position is closed or does not belong to the signer it will be ignored
|
31
|
+
* - referrer: PublicKey
|
32
|
+
* - overrideTime?: number
|
33
|
+
*/
|
34
|
+
function createReferralAccount(program, provider, payer, poolPosition, overrideTime) {
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
36
|
+
// setup transaction
|
37
|
+
const transaction = new web3_js_1.Transaction();
|
38
|
+
const signers = [payer];
|
39
|
+
const referrer = payer.publicKey;
|
40
|
+
// Setup public keys
|
41
|
+
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
42
|
+
// Find referral account
|
43
|
+
let referralAccountPublicKey = yield (0, Constants_1.getReferralAccountPublicKey)(referrer);
|
44
|
+
// Get the referral state PDA
|
45
|
+
const referallStatePublicKey = yield (0, Constants_1.getReferralStatePublicKey)();
|
46
|
+
// Get HDG mint public key
|
47
|
+
const hedgeMintPublicKey = yield (0, Constants_1.getHedgeMintPublicKey)();
|
48
|
+
// Get HDG account of signer
|
49
|
+
const payerHdgAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, hedgeMintPublicKey, payer.publicKey);
|
50
|
+
// Derive the user referral account public key
|
51
|
+
const userReferralAccountPublicKey = yield (0, Constants_1.getUserReferralAccountPublicKey)(payer.publicKey);
|
52
|
+
transaction.add(yield createReferralAccountInstruction(program, payer.publicKey, poolPosition, referralAccountPublicKey, referallStatePublicKey, hedgeMintPublicKey, payerHdgAssociatedTokenAccount.address, userReferralAccountPublicKey, overrideTime));
|
53
|
+
yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, signers).catch(Errors_1.parseAnchorErrors);
|
54
|
+
return referralAccountPublicKey;
|
55
|
+
});
|
56
|
+
}
|
57
|
+
exports.createReferralAccount = createReferralAccount;
|
58
|
+
function createReferralAccountInstruction(program, payerPublicKey, poolPositionPublicKey, referralAccountPublicKey, referralStatePublicKey, hedgeMintPublicKey, hdgAssociatedTokenAccountPublicKey, userReferralAccountPublicKey, overrideTime) {
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
60
|
+
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
61
|
+
return yield program.methods
|
62
|
+
.createReferralAccount(new anchor_1.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000)) // override override times
|
63
|
+
)
|
64
|
+
.accounts({
|
65
|
+
signer: payerPublicKey,
|
66
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
67
|
+
referralState: referralStatePublicKey,
|
68
|
+
referralAccount: referralAccountPublicKey,
|
69
|
+
userReferralAccount: userReferralAccountPublicKey,
|
70
|
+
poolPosition: poolPositionPublicKey,
|
71
|
+
hedgeMint: hedgeMintPublicKey,
|
72
|
+
hdgAta: hdgAssociatedTokenAccountPublicKey,
|
73
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
74
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
75
|
+
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
76
|
+
})
|
77
|
+
.instruction();
|
78
|
+
});
|
79
|
+
}
|
80
|
+
exports.createReferralAccountInstruction = createReferralAccountInstruction;
|
@@ -0,0 +1,61 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
13
|
+
};
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
15
|
+
exports.createUserReferralAccountInstruction = exports.createUserReferralAccount = void 0;
|
16
|
+
const anchor_1 = require("@project-serum/anchor");
|
17
|
+
const spl_token_1 = require("@solana/spl-token");
|
18
|
+
const web3_js_1 = require("@solana/web3.js");
|
19
|
+
const Constants_1 = require("../Constants");
|
20
|
+
const Errors_1 = require("../utils/Errors");
|
21
|
+
const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
|
22
|
+
function createUserReferralAccount(program, provider, payer, referrer, overrideTime) {
|
23
|
+
return __awaiter(this, void 0, void 0, function* () {
|
24
|
+
// setup transaction
|
25
|
+
const transaction = new web3_js_1.Transaction();
|
26
|
+
const signers = [payer];
|
27
|
+
// Find referrer account
|
28
|
+
let referralAccountPublicKey = null;
|
29
|
+
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
30
|
+
if (typeof referrer === 'undefined') {
|
31
|
+
referralAccountPublicKey = yield (0, Constants_1.getReferralAccountPublicKey)(vaultSystemStatePublicKey);
|
32
|
+
}
|
33
|
+
else {
|
34
|
+
referralAccountPublicKey = yield (0, Constants_1.getReferralAccountPublicKey)(referrer);
|
35
|
+
}
|
36
|
+
// Derive the user referral account public key
|
37
|
+
const userReferralAccountPublicKey = yield (0, Constants_1.getUserReferralAccountPublicKey)(payer.publicKey);
|
38
|
+
transaction.add(yield createUserReferralAccountInstruction(program, payer.publicKey, referralAccountPublicKey, userReferralAccountPublicKey, overrideTime));
|
39
|
+
yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, signers).catch(Errors_1.parseAnchorErrors);
|
40
|
+
return userReferralAccountPublicKey;
|
41
|
+
});
|
42
|
+
}
|
43
|
+
exports.createUserReferralAccount = createUserReferralAccount;
|
44
|
+
function createUserReferralAccountInstruction(program, payerPublicKey, referralAccount, userReferralAccount, overrideTime) {
|
45
|
+
return __awaiter(this, void 0, void 0, function* () {
|
46
|
+
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
47
|
+
return yield program.methods
|
48
|
+
.createUserReferralAccount(new anchor_1.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000)) // override override time
|
49
|
+
)
|
50
|
+
.accounts({
|
51
|
+
signer: payerPublicKey,
|
52
|
+
userReferralAccount: userReferralAccount,
|
53
|
+
referralAccount: referralAccount,
|
54
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
55
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
56
|
+
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
57
|
+
})
|
58
|
+
.instruction();
|
59
|
+
});
|
60
|
+
}
|
61
|
+
exports.createUserReferralAccountInstruction = createUserReferralAccountInstruction;
|
@@ -19,29 +19,38 @@ const web3_js_1 = require("@solana/web3.js");
|
|
19
19
|
const Errors_1 = require("../utils/Errors");
|
20
20
|
const Constants_1 = require("../Constants");
|
21
21
|
const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
|
22
|
-
function depositLiquidationPool(program, provider, payer, depositAmount, overrideStartTime) {
|
22
|
+
function depositLiquidationPool(program, provider, payer, depositAmount, overrideStartTime, referrer) {
|
23
23
|
return __awaiter(this, void 0, void 0, function* () {
|
24
24
|
const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
|
25
25
|
const payerUshAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, payer.publicKey);
|
26
|
+
let referralAccountPublicKey = null;
|
27
|
+
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
28
|
+
if (typeof referrer === 'undefined') {
|
29
|
+
referralAccountPublicKey = yield (0, Constants_1.getReferralAccountPublicKey)(vaultSystemStatePublicKey);
|
30
|
+
}
|
31
|
+
else {
|
32
|
+
referralAccountPublicKey = yield (0, Constants_1.getReferralAccountPublicKey)(referrer);
|
33
|
+
}
|
34
|
+
// Derive the user referral account public key
|
35
|
+
const userReferralAccountPublicKey = yield (0, Constants_1.getUserReferralAccountPublicKey)(payer.publicKey);
|
26
36
|
const poolPosition = web3_js_1.Keypair.generate();
|
27
|
-
const transaction = new web3_js_1.Transaction().add(yield depositLiquidationPoolInstruction(program, payer.publicKey, payerUshAccount.address, poolPosition.publicKey, new anchor_1.BN(depositAmount), overrideStartTime));
|
37
|
+
const transaction = new web3_js_1.Transaction().add(yield depositLiquidationPoolInstruction(program, payer.publicKey, vaultSystemStatePublicKey, payerUshAccount.address, poolPosition.publicKey, new anchor_1.BN(depositAmount), userReferralAccountPublicKey, referralAccountPublicKey, overrideStartTime));
|
28
38
|
yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer, poolPosition]).catch(Errors_1.parseAnchorErrors);
|
29
39
|
return poolPosition.publicKey;
|
30
40
|
});
|
31
41
|
}
|
32
42
|
exports.depositLiquidationPool = depositLiquidationPool;
|
33
|
-
function depositLiquidationPoolInstruction(program, payerPublicKey, payerUshAccount, poolPositionPublicKey, depositAmount, overrideStartTime) {
|
43
|
+
function depositLiquidationPoolInstruction(program, payerPublicKey, vaultSystemStatePublicKey, payerUshAccount, poolPositionPublicKey, depositAmount, userReferralAccountPublicKey, referralAccountPublicKey, overrideStartTime) {
|
34
44
|
return __awaiter(this, void 0, void 0, function* () {
|
35
45
|
const liquidationPoolStatePublicKey = yield (0, Constants_1.getLiquidationPoolStatePublicKey)();
|
36
46
|
const liquidationPoolState = yield program.account.liquidationPoolState.fetch(liquidationPoolStatePublicKey);
|
37
47
|
const poolUSHAccount = yield (0, Constants_1.getLiquidationPoolUshAccountPublicKey)();
|
38
48
|
const ushMint = yield (0, Constants_1.getUshMintPublicKey)();
|
39
|
-
const vaultSystemState = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
40
49
|
return yield program.methods
|
41
50
|
.depositLiquidationPool(depositAmount, new anchor_1.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Math.floor(Date.now() / 1000)) // override current time
|
42
51
|
)
|
43
52
|
.accounts({
|
44
|
-
vaultSystemState:
|
53
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
45
54
|
poolState: liquidationPoolStatePublicKey,
|
46
55
|
poolUshAccount: poolUSHAccount,
|
47
56
|
poolEra: liquidationPoolState.currentEra,
|
@@ -49,6 +58,8 @@ function depositLiquidationPoolInstruction(program, payerPublicKey, payerUshAcco
|
|
49
58
|
ushMint: ushMint,
|
50
59
|
payer: payerPublicKey,
|
51
60
|
ownerUshAccount: payerUshAccount,
|
61
|
+
userReferralAccount: userReferralAccountPublicKey,
|
62
|
+
referralAccount: referralAccountPublicKey,
|
52
63
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
53
64
|
systemProgram: web3_js_1.SystemProgram.programId,
|
54
65
|
})
|
@@ -19,7 +19,7 @@ const web3_js_1 = require("@solana/web3.js");
|
|
19
19
|
const getLinkedListAccounts_1 = require("../utils/getLinkedListAccounts");
|
20
20
|
const Constants_1 = require("../Constants");
|
21
21
|
const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
|
22
|
-
function loanVault(program, provider, payer, vaultPublicKey, loanAmount, overrideTime) {
|
22
|
+
function loanVault(program, provider, payer, vaultPublicKey, loanAmount, overrideTime, referrer) {
|
23
23
|
return __awaiter(this, void 0, void 0, function* () {
|
24
24
|
const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
|
25
25
|
const payerUshAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, payer.publicKey);
|
@@ -28,20 +28,30 @@ function loanVault(program, provider, payer, vaultPublicKey, loanAmount, overrid
|
|
28
28
|
const vaultTypeAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccount.collateralMint, vaultAccount.vaultType, true);
|
29
29
|
const vaultAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, vaultTypeAccount.collateralMint, vaultPublicKey, true);
|
30
30
|
const [oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey] = yield (0, getLinkedListAccounts_1.getLinkedListAccounts)(program, vaultAccount.vaultType, vaultPublicKey, new anchor_1.BN(0), new anchor_1.BN(0), new anchor_1.BN(loanAmount), new anchor_1.BN(0), false, false);
|
31
|
+
let referralAccountPublicKey = null;
|
32
|
+
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
33
|
+
if (typeof referrer === 'undefined') {
|
34
|
+
referralAccountPublicKey = yield (0, Constants_1.getReferralAccountPublicKey)(vaultSystemStatePublicKey);
|
35
|
+
}
|
36
|
+
else {
|
37
|
+
referralAccountPublicKey = yield (0, Constants_1.getReferralAccountPublicKey)(referrer);
|
38
|
+
}
|
31
39
|
const history = web3_js_1.Keypair.generate();
|
32
|
-
const transaction = new web3_js_1.Transaction().add(yield loanVaultInstruction(program, payer.publicKey, payerUshAccount.address, vaultPublicKey, vaultAssociatedTokenAccount.address, history.publicKey, vaultAccount.vaultType, vaultTypeAssociatedTokenAccount.address, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, new anchor_1.BN(loanAmount), overrideTime));
|
40
|
+
const transaction = new web3_js_1.Transaction().add(yield loanVaultInstruction(program, payer.publicKey, payerUshAccount.address, vaultPublicKey, vaultAssociatedTokenAccount.address, history.publicKey, vaultAccount.vaultType, vaultTypeAssociatedTokenAccount.address, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, new anchor_1.BN(loanAmount), referralAccountPublicKey, overrideTime));
|
33
41
|
yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer, history]);
|
34
42
|
return vaultPublicKey;
|
35
43
|
});
|
36
44
|
}
|
37
45
|
exports.loanVault = loanVault;
|
38
|
-
function loanVaultInstruction(program, payerPublicKey, ownerUshAccount, vaultPublickey, vaultAssociatedTokenAccount, historyPublicKey, vaultTypeAccount, vaultTypeAssociatedTokenAccount, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, loanAmount, overrideTime) {
|
46
|
+
function loanVaultInstruction(program, payerPublicKey, ownerUshAccount, vaultPublickey, vaultAssociatedTokenAccount, historyPublicKey, vaultTypeAccount, vaultTypeAssociatedTokenAccount, oldSmallerPublicKey, newSmallerPublicKey, newLargerPublicKey, loanAmount, referralAccountPublicKey, overrideTime) {
|
39
47
|
return __awaiter(this, void 0, void 0, function* () {
|
40
48
|
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
41
49
|
const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
|
42
50
|
const hedgeMintPublickey = yield (0, Constants_1.getHedgeMintPublicKey)();
|
43
51
|
const [hedgeStakingPoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(hedgeMintPublickey);
|
44
52
|
const hedgeStakingPoolAssociatedUshTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(hedgeStakingPoolPublicKey, ushMintPublickey);
|
53
|
+
const referralStatePublicKey = yield (0, Constants_1.getReferralStatePublicKey)();
|
54
|
+
const userReferralAccountPublicKey = yield (0, Constants_1.getUserReferralAccountPublicKey)(payerPublicKey);
|
45
55
|
return yield program.methods
|
46
56
|
.loanVault(loanAmount, new anchor_1.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000)) // override override time
|
47
57
|
)
|
@@ -60,6 +70,9 @@ function loanVaultInstruction(program, payerPublicKey, ownerUshAccount, vaultPub
|
|
60
70
|
oldSmallerVaultInfo: oldSmallerPublicKey,
|
61
71
|
newSmallerVaultInfo: newSmallerPublicKey,
|
62
72
|
newLargerVaultInfo: newLargerPublicKey,
|
73
|
+
referralState: referralStatePublicKey,
|
74
|
+
referralAccount: referralAccountPublicKey,
|
75
|
+
userReferralAccount: userReferralAccountPublicKey,
|
63
76
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
64
77
|
systemProgram: web3_js_1.SystemProgram.programId,
|
65
78
|
})
|
@@ -0,0 +1,60 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
13
|
+
};
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
15
|
+
exports.psmEditAccountInstruction = exports.psmEditAccount = exports.psmStatus = void 0;
|
16
|
+
const anchor_1 = require("@project-serum/anchor");
|
17
|
+
const spl_token_1 = require("@solana/spl-token");
|
18
|
+
const web3_js_1 = require("@solana/web3.js");
|
19
|
+
const Constants_1 = require("../Constants");
|
20
|
+
const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
|
21
|
+
var psmStatus;
|
22
|
+
(function (psmStatus) {
|
23
|
+
psmStatus[psmStatus["PsmEnabled"] = 0] = "PsmEnabled";
|
24
|
+
psmStatus[psmStatus["PsmDisabled"] = 1] = "PsmDisabled";
|
25
|
+
psmStatus[psmStatus["MintOnly"] = 2] = "MintOnly";
|
26
|
+
psmStatus[psmStatus["RedeemOnly"] = 3] = "RedeemOnly";
|
27
|
+
})(psmStatus = exports.psmStatus || (exports.psmStatus = {}));
|
28
|
+
function psmEditAccount(program, provider, payer, collateralMint, mintFee, redeemFee, debtLimit, minSwap, overrideTime) {
|
29
|
+
return __awaiter(this, void 0, void 0, function* () {
|
30
|
+
const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
|
31
|
+
const payerUshAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, payer.publicKey);
|
32
|
+
const payerCollateralAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, collateralMint, payer.publicKey);
|
33
|
+
const enc = new TextEncoder();
|
34
|
+
const [psmAccount] = yield web3_js_1.PublicKey.findProgramAddress([enc.encode(collateralMint.toString().slice(0, 12)), enc.encode('PSM')], Constants_1.HEDGE_PROGRAM_PUBLICKEY);
|
35
|
+
const psmAccountAta = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, collateralMint, psmAccount, true);
|
36
|
+
const transaction = new web3_js_1.Transaction().add(yield psmEditAccountInstruction(program, payer.publicKey, psmAccount, collateralMint, new anchor_1.BN(mintFee), new anchor_1.BN(redeemFee), new anchor_1.BN(debtLimit), new anchor_1.BN(minSwap), overrideTime));
|
37
|
+
yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer]);
|
38
|
+
return payer.publicKey;
|
39
|
+
});
|
40
|
+
}
|
41
|
+
exports.psmEditAccount = psmEditAccount;
|
42
|
+
function psmEditAccountInstruction(program, payerPublicKey, psmAccount, collateralMint, mintFee, redeemFee, debtLimit, minSwap, overrideTime) {
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
44
|
+
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
45
|
+
const hedgeMintPublickey = yield (0, Constants_1.getHedgeMintPublicKey)();
|
46
|
+
const [hedgeStakingPoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(hedgeMintPublickey);
|
47
|
+
return yield program.methods
|
48
|
+
.psmEditAccount(mintFee, redeemFee, debtLimit, minSwap, new anchor_1.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000)) // override override time
|
49
|
+
)
|
50
|
+
.accounts({
|
51
|
+
payer: payerPublicKey,
|
52
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
53
|
+
psmAccount: psmAccount,
|
54
|
+
collateralMint: collateralMint,
|
55
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
56
|
+
})
|
57
|
+
.instruction();
|
58
|
+
});
|
59
|
+
}
|
60
|
+
exports.psmEditAccountInstruction = psmEditAccountInstruction;
|
@@ -18,7 +18,7 @@ const spl_token_1 = require("@solana/spl-token");
|
|
18
18
|
const web3_js_1 = require("@solana/web3.js");
|
19
19
|
const Constants_1 = require("../Constants");
|
20
20
|
const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
|
21
|
-
function psmMintUsh(program, provider, payer, collateralMint, collateralAmount, overrideTime) {
|
21
|
+
function psmMintUsh(program, provider, payer, collateralMint, collateralAmount, overrideTime, referrer) {
|
22
22
|
return __awaiter(this, void 0, void 0, function* () {
|
23
23
|
const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
|
24
24
|
const payerUshAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, payer.publicKey);
|
@@ -26,13 +26,22 @@ function psmMintUsh(program, provider, payer, collateralMint, collateralAmount,
|
|
26
26
|
const enc = new TextEncoder();
|
27
27
|
const [psmAccount] = yield web3_js_1.PublicKey.findProgramAddress([enc.encode(collateralMint.toString().slice(0, 12)), enc.encode('PSM')], Constants_1.HEDGE_PROGRAM_PUBLICKEY);
|
28
28
|
const psmAccountAta = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, collateralMint, psmAccount, true);
|
29
|
-
|
29
|
+
let referralAccountPublicKey;
|
30
|
+
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
31
|
+
if (!referrer) {
|
32
|
+
referralAccountPublicKey = yield (0, Constants_1.getReferralAccountPublicKey)(vaultSystemStatePublicKey);
|
33
|
+
}
|
34
|
+
else {
|
35
|
+
referralAccountPublicKey = yield (0, Constants_1.getReferralAccountPublicKey)(referrer);
|
36
|
+
}
|
37
|
+
const referralStatePublicKey = yield (0, Constants_1.getReferralStatePublicKey)();
|
38
|
+
const transaction = new web3_js_1.Transaction().add(yield psmMintUshInstruction(program, payer.publicKey, psmAccount, collateralMint, new anchor_1.BN(collateralAmount), referralStatePublicKey, referralAccountPublicKey, overrideTime));
|
30
39
|
yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer]);
|
31
40
|
return payer.publicKey;
|
32
41
|
});
|
33
42
|
}
|
34
43
|
exports.psmMintUsh = psmMintUsh;
|
35
|
-
function psmMintUshInstruction(program, payerPublicKey, psmAccount, collateralMint, collateralAmount, overrideTime) {
|
44
|
+
function psmMintUshInstruction(program, payerPublicKey, psmAccount, collateralMint, collateralAmount, referralStatePublicKey, referralAccountPublicKey, overrideTime) {
|
36
45
|
return __awaiter(this, void 0, void 0, function* () {
|
37
46
|
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
38
47
|
const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
|
@@ -56,6 +65,8 @@ function psmMintUshInstruction(program, payerPublicKey, psmAccount, collateralMi
|
|
56
65
|
ownerCollateralAccount: ownerCollateralAccount,
|
57
66
|
collateralMint: collateralMint,
|
58
67
|
ushMint: ushMintPublickey,
|
68
|
+
referralState: referralStatePublicKey,
|
69
|
+
referralAccount: referralAccountPublicKey,
|
59
70
|
systemProgram: web3_js_1.SystemProgram.programId,
|
60
71
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
61
72
|
})
|
@@ -18,7 +18,7 @@ const spl_token_1 = require("@solana/spl-token");
|
|
18
18
|
const web3_js_1 = require("@solana/web3.js");
|
19
19
|
const Constants_1 = require("../Constants");
|
20
20
|
const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
|
21
|
-
function psmRedeemUsh(program, provider, payer, collateralMint, ushAmount, overrideTime) {
|
21
|
+
function psmRedeemUsh(program, provider, payer, collateralMint, ushAmount, overrideTime, referrer) {
|
22
22
|
return __awaiter(this, void 0, void 0, function* () {
|
23
23
|
const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
|
24
24
|
const payerUshAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublickey, payer.publicKey);
|
@@ -26,13 +26,22 @@ function psmRedeemUsh(program, provider, payer, collateralMint, ushAmount, overr
|
|
26
26
|
const enc = new TextEncoder();
|
27
27
|
const [psmAccount] = yield web3_js_1.PublicKey.findProgramAddress([enc.encode(collateralMint.toString().slice(0, 12)), enc.encode('PSM')], Constants_1.HEDGE_PROGRAM_PUBLICKEY);
|
28
28
|
const psmAccountAta = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, collateralMint, psmAccount, true);
|
29
|
-
|
29
|
+
let referralAccountPublicKey;
|
30
|
+
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
31
|
+
if (!referrer) {
|
32
|
+
referralAccountPublicKey = yield (0, Constants_1.getReferralAccountPublicKey)(vaultSystemStatePublicKey);
|
33
|
+
}
|
34
|
+
else {
|
35
|
+
referralAccountPublicKey = yield (0, Constants_1.getReferralAccountPublicKey)(referrer);
|
36
|
+
}
|
37
|
+
const referralStatePublicKey = yield (0, Constants_1.getReferralStatePublicKey)();
|
38
|
+
const transaction = new web3_js_1.Transaction().add(yield psmRedeemUshInstruction(program, payer.publicKey, psmAccount, collateralMint, new anchor_1.BN(ushAmount), referralStatePublicKey, referralAccountPublicKey, overrideTime));
|
30
39
|
yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer]);
|
31
40
|
return payer.publicKey;
|
32
41
|
});
|
33
42
|
}
|
34
43
|
exports.psmRedeemUsh = psmRedeemUsh;
|
35
|
-
function psmRedeemUshInstruction(program, payerPublicKey, psmAccount, collateralMint, ushAmount, overrideTime) {
|
44
|
+
function psmRedeemUshInstruction(program, payerPublicKey, psmAccount, collateralMint, ushAmount, referralStatePublicKey, referralAccountPublicKey, overrideTime) {
|
36
45
|
return __awaiter(this, void 0, void 0, function* () {
|
37
46
|
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
38
47
|
const ushMintPublickey = yield (0, Constants_1.getUshMintPublicKey)();
|
@@ -56,6 +65,8 @@ function psmRedeemUshInstruction(program, payerPublicKey, psmAccount, collateral
|
|
56
65
|
ownerCollateralAccount: ownerCollateralAccount,
|
57
66
|
collateralMint: collateralMint,
|
58
67
|
ushMint: ushMintPublickey,
|
68
|
+
referralState: referralStatePublicKey,
|
69
|
+
referralAccount: referralAccountPublicKey,
|
59
70
|
systemProgram: web3_js_1.SystemProgram.programId,
|
60
71
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
61
72
|
})
|
@@ -0,0 +1,84 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
13
|
+
};
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
15
|
+
exports.referralClaimFeesInstruction = exports.referralClaimFees = void 0;
|
16
|
+
const spl_token_1 = require("@solana/spl-token");
|
17
|
+
const web3_js_1 = require("@solana/web3.js");
|
18
|
+
const Constants_1 = require("../Constants");
|
19
|
+
const Errors_1 = require("../utils/Errors");
|
20
|
+
const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
|
21
|
+
/** @type {Function} - Allows a referrer to claims their fees.
|
22
|
+
* This checks the user has enough HDG in their wallet or staked and
|
23
|
+
* then allows them to claim earned fees. If they meet the PSM referral threshold,
|
24
|
+
* they will be eligible for PSM cut.
|
25
|
+
* Params:
|
26
|
+
* - program: Program<Vault> : The program instance of Hedge Vault program
|
27
|
+
* - provider: Provider : Current connection
|
28
|
+
* - payer: Signer : who we are creating the referral account for
|
29
|
+
* - poolPublicKey: PublicKey : a key to a pool position. If the position is closed or does not belong to the signer it will be ignored
|
30
|
+
* - referrer: PublicKey
|
31
|
+
*/
|
32
|
+
function referralClaimFees(program, provider, payer, poolPosition) {
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
34
|
+
// setup transaction
|
35
|
+
const transaction = new web3_js_1.Transaction();
|
36
|
+
const signers = [payer];
|
37
|
+
// General variables
|
38
|
+
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
39
|
+
// Find referrer account
|
40
|
+
const referrer = yield payer.publicKey;
|
41
|
+
const referralAccountPublicKey = yield (0, Constants_1.getReferralAccountPublicKey)(referrer);
|
42
|
+
// Derive the user referral account public key
|
43
|
+
const referallStatePublicKey = yield (0, Constants_1.getReferralStatePublicKey)();
|
44
|
+
const hedgeMintPublicKey = yield (0, Constants_1.getHedgeMintPublicKey)();
|
45
|
+
// Get HDG account of signer
|
46
|
+
const payerHdgAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, hedgeMintPublicKey, payer.publicKey);
|
47
|
+
const ushMintPublicKey = yield (0, Constants_1.getUshMintPublicKey)();
|
48
|
+
// Get USH account of signer
|
49
|
+
const payerUshAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublicKey, payer.publicKey);
|
50
|
+
// Get community_associated_hedge_token_account
|
51
|
+
const communityAssociatedHedgeTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, hedgeMintPublicKey, vaultSystemStatePublicKey, true);
|
52
|
+
const [feePoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(hedgeMintPublicKey);
|
53
|
+
const feePoolAssociatedUshTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, ushMintPublicKey, feePoolPublicKey, true);
|
54
|
+
transaction.add(yield referralClaimFeesInstruction(program, payer.publicKey, vaultSystemStatePublicKey, poolPosition, referralAccountPublicKey, referallStatePublicKey, hedgeMintPublicKey, payerHdgAssociatedTokenAccount.address, ushMintPublicKey, payerUshAssociatedTokenAccount.address, communityAssociatedHedgeTokenAccount.address, feePoolPublicKey, feePoolAssociatedUshTokenAccount.address));
|
55
|
+
yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, signers).catch(Errors_1.parseAnchorErrors);
|
56
|
+
return referralAccountPublicKey;
|
57
|
+
});
|
58
|
+
}
|
59
|
+
exports.referralClaimFees = referralClaimFees;
|
60
|
+
function referralClaimFeesInstruction(program, payerPublicKey, vaultSystemStatePublicKey, poolPositionPublicKey, referralAccountPublicKey, referralStatePublicKey, hedgeMintPublicKey, hdgAssociatedTokenAccountPublicKey, ushMintPublicKey, ushAssociatedTokenAccountPublicKey, communityAssociatedHedgeTokenAccountPublicKey, feePoolPublicKey, feePoolAssociatedUshTokenAccountPublicKey) {
|
61
|
+
return __awaiter(this, void 0, void 0, function* () {
|
62
|
+
return yield program.methods
|
63
|
+
.referralClaimFees()
|
64
|
+
.accounts({
|
65
|
+
signer: payerPublicKey,
|
66
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
67
|
+
referralState: referralStatePublicKey,
|
68
|
+
referralAccount: referralAccountPublicKey,
|
69
|
+
poolPosition: poolPositionPublicKey,
|
70
|
+
hedgeMint: hedgeMintPublicKey,
|
71
|
+
signerHdgAta: hdgAssociatedTokenAccountPublicKey,
|
72
|
+
ushMint: ushMintPublicKey,
|
73
|
+
signerUshAta: ushAssociatedTokenAccountPublicKey,
|
74
|
+
communityAssociatedHedgeTokenAccount: communityAssociatedHedgeTokenAccountPublicKey,
|
75
|
+
feePool: feePoolPublicKey,
|
76
|
+
feePoolAssociatedUshTokenAccount: feePoolAssociatedUshTokenAccountPublicKey,
|
77
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
78
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
79
|
+
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
80
|
+
})
|
81
|
+
.instruction();
|
82
|
+
});
|
83
|
+
}
|
84
|
+
exports.referralClaimFeesInstruction = referralClaimFeesInstruction;
|