hedge-web3 0.1.16 → 0.1.19
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/declarations/Constants.d.ts +2 -1
- package/declarations/instructions/createVault.d.ts +4 -3
- package/declarations/state/VaultAccount.d.ts +7 -4
- package/lib/Constants.js +9 -2
- package/lib/instructions/createVault.js +70 -10
- package/lib/state/VaultAccount.js +7 -4
- package/package.json +1 -1
- package/src/Constants.ts +5 -1
- package/src/instructions/createVault.ts +196 -39
- package/src/state/VaultAccount.ts +20 -9
@@ -1,5 +1,5 @@
|
|
1
1
|
import { PublicKey } from '@solana/web3.js';
|
2
|
-
export declare const HEDGE_PROGRAM_ID = "
|
2
|
+
export declare const HEDGE_PROGRAM_ID = "h6GZ7ZMB6Jwmj3k4mQZY1bfXMGREEWH47uJHbKvE6nd";
|
3
3
|
export declare const HEDGE_PROGRAM_PUBLICKEY: PublicKey;
|
4
4
|
export declare const CHAINLINK_SOL_USD_ID = "FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf";
|
5
5
|
export declare const CHAINLINK_SOL_USD_PUBLICKEY: PublicKey;
|
@@ -10,5 +10,6 @@ export declare function getVaultSystemStatePublicKey(): Promise<PublicKey>;
|
|
10
10
|
export declare function getHedgeMintPublicKey(): Promise<PublicKey>;
|
11
11
|
export declare function getPoolPublicKeyForMint(mintPublicKey: PublicKey): Promise<[PublicKey, number, string]>;
|
12
12
|
export declare function getVaultTypeAccountPublicKey(collateralType: string): Promise<PublicKey>;
|
13
|
+
export declare function getVaultTypeOracleAccountPublicKey(collateralType: string): Promise<PublicKey>;
|
13
14
|
export declare function findVaultAddress(vaultSalt: string): Promise<PublicKey>;
|
14
15
|
export declare function findAssociatedTokenAddress(walletAddress: PublicKey, tokenMintAddress: PublicKey): Promise<PublicKey>;
|
@@ -1,4 +1,5 @@
|
|
1
|
-
import { Program, Provider } from
|
2
|
-
import { PublicKey, Signer, TransactionInstruction } from
|
1
|
+
import { Program, Provider } from "@project-serum/anchor";
|
2
|
+
import { PublicKey, Signer, Transaction, TransactionInstruction } from "@solana/web3.js";
|
3
3
|
export declare function createVault(program: Program, provider: Provider, payer: Signer, collateralType: string, depositAmount: number, overrideTime?: number): Promise<PublicKey>;
|
4
|
-
export declare function
|
4
|
+
export declare function buildCreateVaultTransaction(program: Program, collateralType: string, depositAmount: number, overrideTime?: number): Promise<[Transaction, Signer[], PublicKey]>;
|
5
|
+
export declare function createVaultInstruction(program: Program, salt: string, payerPublicKey: PublicKey, payerTokenAccountPublicKey: PublicKey, vaultPublicKey: PublicKey, vaultAssociatedTokenAccount: PublicKey, feePool: PublicKey, feePoolAssociatedUsdhTokenAccount: PublicKey, vaultTypeAccount: PublicKey, collateralMint: PublicKey, historyPublicKey: PublicKey, usdhMintPublickey: PublicKey, depositAmount: number, overrideTime?: number): Promise<TransactionInstruction>;
|
@@ -8,12 +8,15 @@ export declare class VaultAccount {
|
|
8
8
|
publicKey: PublicKey;
|
9
9
|
/** The public key of the vault owner. */
|
10
10
|
vaultOwner: PublicKey;
|
11
|
-
/** The
|
12
|
-
|
11
|
+
/** The public key of the vault owner. */
|
12
|
+
pdaSalt: string;
|
13
13
|
/** The deposited collateral of the vault (in SOL Lamports). */
|
14
14
|
deposited: number;
|
15
|
-
/** The
|
16
|
-
|
15
|
+
/** The outstanding debt of the vault (in USDH Lamports). Denormalized to time 0. */
|
16
|
+
denormalizedDebt: number;
|
17
|
+
debtProductSnapshotBytes: Decimal;
|
18
|
+
collateralAccumulatorSnapshotBytes: Decimal;
|
19
|
+
collateralType: string;
|
17
20
|
/** Current State of the vautl ("Open", "Closed", "Liquidated") */
|
18
21
|
vaultStatus: string;
|
19
22
|
constructor(vault: any, publicKey: PublicKey);
|
package/lib/Constants.js
CHANGED
@@ -9,10 +9,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
9
9
|
});
|
10
10
|
};
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
-
exports.findAssociatedTokenAddress = exports.findVaultAddress = exports.getVaultTypeAccountPublicKey = exports.getPoolPublicKeyForMint = exports.getHedgeMintPublicKey = exports.getVaultSystemStatePublicKey = exports.getUsdhMintPublicKey = exports.getLiquidationPoolUsdhAccountPublicKey = exports.getLiquidationPoolStatePublicKey = exports.CHAINLINK_SOL_USD_PUBLICKEY = exports.CHAINLINK_SOL_USD_ID = exports.HEDGE_PROGRAM_PUBLICKEY = exports.HEDGE_PROGRAM_ID = void 0;
|
12
|
+
exports.findAssociatedTokenAddress = exports.findVaultAddress = exports.getVaultTypeOracleAccountPublicKey = exports.getVaultTypeAccountPublicKey = exports.getPoolPublicKeyForMint = exports.getHedgeMintPublicKey = exports.getVaultSystemStatePublicKey = exports.getUsdhMintPublicKey = exports.getLiquidationPoolUsdhAccountPublicKey = exports.getLiquidationPoolStatePublicKey = exports.CHAINLINK_SOL_USD_PUBLICKEY = exports.CHAINLINK_SOL_USD_ID = exports.HEDGE_PROGRAM_PUBLICKEY = exports.HEDGE_PROGRAM_ID = void 0;
|
13
13
|
const spl_token_1 = require("@solana/spl-token");
|
14
14
|
const web3_js_1 = require("@solana/web3.js");
|
15
|
-
exports.HEDGE_PROGRAM_ID = '
|
15
|
+
exports.HEDGE_PROGRAM_ID = 'h6GZ7ZMB6Jwmj3k4mQZY1bfXMGREEWH47uJHbKvE6nd';
|
16
16
|
exports.HEDGE_PROGRAM_PUBLICKEY = new web3_js_1.PublicKey(exports.HEDGE_PROGRAM_ID);
|
17
17
|
exports.CHAINLINK_SOL_USD_ID = 'FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf';
|
18
18
|
exports.CHAINLINK_SOL_USD_PUBLICKEY = new web3_js_1.PublicKey(exports.CHAINLINK_SOL_USD_ID);
|
@@ -67,6 +67,13 @@ function getVaultTypeAccountPublicKey(collateralType) {
|
|
67
67
|
});
|
68
68
|
}
|
69
69
|
exports.getVaultTypeAccountPublicKey = getVaultTypeAccountPublicKey;
|
70
|
+
function getVaultTypeOracleAccountPublicKey(collateralType) {
|
71
|
+
return __awaiter(this, void 0, void 0, function* () {
|
72
|
+
const [vaultTypeOracleAccount] = yield web3_js_1.PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('Oracle')], exports.HEDGE_PROGRAM_PUBLICKEY);
|
73
|
+
return vaultTypeOracleAccount;
|
74
|
+
});
|
75
|
+
}
|
76
|
+
exports.getVaultTypeOracleAccountPublicKey = getVaultTypeOracleAccountPublicKey;
|
70
77
|
function findVaultAddress(vaultSalt) {
|
71
78
|
return __awaiter(this, void 0, void 0, function* () {
|
72
79
|
const [vaultAddress] = yield web3_js_1.PublicKey.findProgramAddress([enc.encode('Vault'), enc.encode(vaultSalt)], exports.HEDGE_PROGRAM_PUBLICKEY);
|
@@ -9,7 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
9
9
|
});
|
10
10
|
};
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
-
exports.createVaultInstruction = exports.createVault = void 0;
|
12
|
+
exports.createVaultInstruction = exports.buildCreateVaultTransaction = exports.createVault = void 0;
|
13
13
|
const anchor_1 = require("@project-serum/anchor");
|
14
14
|
const serum_1 = require("@project-serum/serum");
|
15
15
|
const spl_token_1 = require("@solana/spl-token");
|
@@ -29,11 +29,11 @@ function createVault(program, provider, payer, collateralType, depositAmount, ov
|
|
29
29
|
const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
|
30
30
|
const payerTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(payer.publicKey, vaultTypeAccountInfo.collateralMint);
|
31
31
|
const vaultAssociatedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(newVaultPublicKey, vaultTypeAccountInfo.collateralMint);
|
32
|
-
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
33
32
|
const wrappedSolAccount = web3_js_1.Keypair.generate();
|
34
33
|
const transaction = new web3_js_1.Transaction();
|
35
34
|
const signers = [payer, history];
|
36
|
-
const isWrappedSol = vaultTypeAccountInfo.collateralMint.toString() ===
|
35
|
+
const isWrappedSol = vaultTypeAccountInfo.collateralMint.toString() ===
|
36
|
+
serum_1.TokenInstructions.WRAPPED_SOL_MINT.toString();
|
37
37
|
const [hedgeStakingPoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(yield (0, Constants_1.getHedgeMintPublicKey)());
|
38
38
|
const feePoolAssociatedUsdhTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(hedgeStakingPoolPublicKey, usdhMintPublickey);
|
39
39
|
if (isWrappedSol) {
|
@@ -42,20 +42,20 @@ function createVault(program, provider, payer, collateralType, depositAmount, ov
|
|
42
42
|
lamports: depositAmount + 2.04e6,
|
43
43
|
newAccountPubkey: wrappedSolAccount.publicKey,
|
44
44
|
programId: spl_token_1.TOKEN_PROGRAM_ID,
|
45
|
-
space: 165
|
45
|
+
space: 165,
|
46
46
|
}), serum_1.TokenInstructions.initializeAccount({
|
47
47
|
account: wrappedSolAccount.publicKey,
|
48
48
|
mint: serum_1.TokenInstructions.WRAPPED_SOL_MINT,
|
49
|
-
owner: payer.publicKey
|
49
|
+
owner: payer.publicKey,
|
50
50
|
}));
|
51
51
|
signers.push(wrappedSolAccount);
|
52
52
|
}
|
53
|
-
transaction.add(yield createVaultInstruction(program, salt,
|
53
|
+
transaction.add(yield createVaultInstruction(program, salt, payer.publicKey, isWrappedSol ? wrappedSolAccount.publicKey : payerTokenAccount, newVaultPublicKey, vaultAssociatedTokenAccount, hedgeStakingPoolPublicKey, feePoolAssociatedUsdhTokenAccount, vaultTypeAccountPublicKey, vaultTypeAccountInfo.collateralMint, history.publicKey, usdhMintPublickey, depositAmount, overrideTime));
|
54
54
|
if (isWrappedSol) {
|
55
55
|
transaction.add(serum_1.TokenInstructions.closeAccount({
|
56
56
|
source: wrappedSolAccount.publicKey,
|
57
57
|
destination: payer.publicKey,
|
58
|
-
owner: payer.publicKey
|
58
|
+
owner: payer.publicKey,
|
59
59
|
}));
|
60
60
|
}
|
61
61
|
yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, signers, provider === null || provider === void 0 ? void 0 : provider.opts).catch(Errors_1.parseAnchorErrors);
|
@@ -63,8 +63,68 @@ function createVault(program, provider, payer, collateralType, depositAmount, ov
|
|
63
63
|
});
|
64
64
|
}
|
65
65
|
exports.createVault = createVault;
|
66
|
-
function
|
66
|
+
function buildCreateVaultTransaction(program, collateralType, depositAmount, overrideTime) {
|
67
|
+
return __awaiter(this, void 0, void 0, function* () {
|
68
|
+
console.log("HEDGE SDK: buildCreateVaultTransaction");
|
69
|
+
const usdhMintPublickey = yield (0, Constants_1.getUsdhMintPublicKey)();
|
70
|
+
const payerPublicKey = program.provider.wallet.publicKey;
|
71
|
+
const salt = (0, uuid_1.v4)().substring(0, 8);
|
72
|
+
const newVaultPublicKey = yield (0, Constants_1.findVaultAddress)(salt);
|
73
|
+
const history = web3_js_1.Keypair.generate();
|
74
|
+
const { blockhash } = yield program.provider.connection.getLatestBlockhash();
|
75
|
+
const transaction = new web3_js_1.Transaction({
|
76
|
+
feePayer: payerPublicKey,
|
77
|
+
recentBlockhash: blockhash,
|
78
|
+
});
|
79
|
+
const signers = [history];
|
80
|
+
const wrappedSolAccount = web3_js_1.Keypair.generate();
|
81
|
+
console.log('Lookup getVaultTypeAccountPublicKey', collateralType);
|
82
|
+
// Lookup the vault type info
|
83
|
+
const vaultTypeAccountPublicKey = yield (0, Constants_1.getVaultTypeAccountPublicKey)(collateralType);
|
84
|
+
console.log('Lookup vaultTypeAccountPublicKey', vaultTypeAccountPublicKey.toString());
|
85
|
+
const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultTypeAccountPublicKey);
|
86
|
+
console.log('Lookup vaultTypeAccountInfo', vaultTypeAccountInfo);
|
87
|
+
const isWrappedSol = vaultTypeAccountInfo.collateralMint.toString() ===
|
88
|
+
serum_1.TokenInstructions.WRAPPED_SOL_MINT.toString();
|
89
|
+
const payerTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(payerPublicKey, vaultTypeAccountInfo.collateralMint);
|
90
|
+
const vaultAssociatedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(newVaultPublicKey, vaultTypeAccountInfo.collateralMint);
|
91
|
+
const [hedgeStakingPoolPublicKey] = yield (0, Constants_1.getPoolPublicKeyForMint)(yield (0, Constants_1.getHedgeMintPublicKey)());
|
92
|
+
const feePoolAssociatedUsdhTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(hedgeStakingPoolPublicKey, usdhMintPublickey);
|
93
|
+
console.log('about to start building transaction');
|
94
|
+
// If wrapped SOL collateral, we convert the SOL to wSOL to use in the instruction
|
95
|
+
if (isWrappedSol) {
|
96
|
+
transaction.add(web3_js_1.SystemProgram.createAccount({
|
97
|
+
fromPubkey: payerPublicKey,
|
98
|
+
lamports: depositAmount + 2.04e6,
|
99
|
+
newAccountPubkey: wrappedSolAccount.publicKey,
|
100
|
+
programId: spl_token_1.TOKEN_PROGRAM_ID,
|
101
|
+
space: 165,
|
102
|
+
}), serum_1.TokenInstructions.initializeAccount({
|
103
|
+
account: wrappedSolAccount.publicKey,
|
104
|
+
mint: serum_1.TokenInstructions.WRAPPED_SOL_MINT,
|
105
|
+
owner: payerPublicKey,
|
106
|
+
}));
|
107
|
+
signers.push(wrappedSolAccount);
|
108
|
+
}
|
109
|
+
console.log("hedgeStakingPoolPublicKey", hedgeStakingPoolPublicKey.toString());
|
110
|
+
transaction.add(yield createVaultInstruction(program, salt, payerPublicKey, isWrappedSol ? wrappedSolAccount.publicKey : payerTokenAccount, newVaultPublicKey, vaultAssociatedTokenAccount, hedgeStakingPoolPublicKey, feePoolAssociatedUsdhTokenAccount, vaultTypeAccountPublicKey, vaultTypeAccountInfo.collateralMint, history.publicKey, usdhMintPublickey, depositAmount, overrideTime));
|
111
|
+
if (isWrappedSol) {
|
112
|
+
transaction.add(serum_1.TokenInstructions.closeAccount({
|
113
|
+
source: wrappedSolAccount.publicKey,
|
114
|
+
destination: payerPublicKey,
|
115
|
+
owner: payerPublicKey,
|
116
|
+
}));
|
117
|
+
}
|
118
|
+
transaction.partialSign(wrappedSolAccount);
|
119
|
+
transaction.partialSign(history);
|
120
|
+
console.log("transaction", transaction);
|
121
|
+
return [transaction, signers, newVaultPublicKey];
|
122
|
+
});
|
123
|
+
}
|
124
|
+
exports.buildCreateVaultTransaction = buildCreateVaultTransaction;
|
125
|
+
function createVaultInstruction(program, salt, payerPublicKey, payerTokenAccountPublicKey, vaultPublicKey, vaultAssociatedTokenAccount, feePool, feePoolAssociatedUsdhTokenAccount, vaultTypeAccount, collateralMint, historyPublicKey, usdhMintPublickey, depositAmount, overrideTime) {
|
67
126
|
return __awaiter(this, void 0, void 0, function* () {
|
127
|
+
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
68
128
|
const ix = program.instruction.createVault(salt, new anchor_1.BN(depositAmount), new anchor_1.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000)), // override override time
|
69
129
|
{
|
70
130
|
accounts: {
|
@@ -82,9 +142,9 @@ function createVaultInstruction(program, salt, vaultSystemStatePublicKey, payerP
|
|
82
142
|
systemProgram: web3_js_1.SystemProgram.programId,
|
83
143
|
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
84
144
|
associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
|
85
|
-
rent: web3_js_1.SYSVAR_RENT_PUBKEY
|
145
|
+
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
86
146
|
},
|
87
|
-
signers: []
|
147
|
+
signers: [],
|
88
148
|
});
|
89
149
|
return ix;
|
90
150
|
});
|
@@ -10,9 +10,12 @@ class VaultAccount {
|
|
10
10
|
constructor(vault, publicKey) {
|
11
11
|
this.publicKey = publicKey;
|
12
12
|
this.vaultOwner = vault.vaultOwner;
|
13
|
-
this.
|
13
|
+
this.pdaSalt = vault.pdaSalt;
|
14
14
|
this.deposited = vault.deposited.toNumber();
|
15
|
-
this.
|
15
|
+
this.denormalizedDebt = vault.denormalizedDebt.toNumber();
|
16
|
+
this.debtProductSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(vault.debtProductSnapshotBytes.toString());
|
17
|
+
this.collateralAccumulatorSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(vault.collateralAccumulatorSnapshotBytes.toString());
|
18
|
+
this.collateralType = vault.collateralType;
|
16
19
|
this.vaultStatus = Object.keys(vault.vaultStatus)[0];
|
17
20
|
}
|
18
21
|
/**
|
@@ -22,7 +25,7 @@ class VaultAccount {
|
|
22
25
|
* @returns true if publicKey matches the owner publicKey
|
23
26
|
*/
|
24
27
|
isOwnedBy(publicKey) {
|
25
|
-
return publicKey.toString() === this.vaultOwner.toString();
|
28
|
+
return publicKey && publicKey.toString() === this.vaultOwner.toString();
|
26
29
|
}
|
27
30
|
/**
|
28
31
|
* Get the collateral value in SOL
|
@@ -38,7 +41,7 @@ class VaultAccount {
|
|
38
41
|
* @returns debt value in USDH
|
39
42
|
*/
|
40
43
|
inUsd() {
|
41
|
-
return this.
|
44
|
+
return this.denormalizedDebt / web3_js_1.LAMPORTS_PER_SOL;
|
42
45
|
}
|
43
46
|
/**
|
44
47
|
* Pretty print the vault publickey for easy display
|
package/package.json
CHANGED
package/src/Constants.ts
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
2
2
|
import { PublicKey } from '@solana/web3.js'
|
3
3
|
|
4
|
-
export const HEDGE_PROGRAM_ID = '
|
4
|
+
export const HEDGE_PROGRAM_ID = 'h6GZ7ZMB6Jwmj3k4mQZY1bfXMGREEWH47uJHbKvE6nd'
|
5
5
|
export const HEDGE_PROGRAM_PUBLICKEY = new PublicKey(HEDGE_PROGRAM_ID)
|
6
6
|
|
7
7
|
export const CHAINLINK_SOL_USD_ID = 'FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf'
|
@@ -43,6 +43,10 @@ export async function getVaultTypeAccountPublicKey (collateralType: string): Pro
|
|
43
43
|
const [vaultTypeAccount] = await PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('State')], HEDGE_PROGRAM_PUBLICKEY)
|
44
44
|
return vaultTypeAccount
|
45
45
|
}
|
46
|
+
export async function getVaultTypeOracleAccountPublicKey (collateralType: string): Promise<PublicKey> {
|
47
|
+
const [vaultTypeOracleAccount] = await PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('Oracle')], HEDGE_PROGRAM_PUBLICKEY)
|
48
|
+
return vaultTypeOracleAccount
|
49
|
+
}
|
46
50
|
export async function findVaultAddress (vaultSalt: string): Promise<PublicKey> {
|
47
51
|
const [vaultAddress] = await PublicKey.findProgramAddress([enc.encode('Vault'), enc.encode(vaultSalt)], HEDGE_PROGRAM_PUBLICKEY)
|
48
52
|
return vaultAddress
|
@@ -1,13 +1,34 @@
|
|
1
|
-
import { BN, Program, Provider } from
|
2
|
-
import { TokenInstructions } from
|
3
|
-
import {
|
4
|
-
|
5
|
-
|
1
|
+
import { BN, Idl, Program, Provider } from "@project-serum/anchor";
|
2
|
+
import { TokenInstructions } from "@project-serum/serum";
|
3
|
+
import {
|
4
|
+
ASSOCIATED_TOKEN_PROGRAM_ID,
|
5
|
+
TOKEN_PROGRAM_ID,
|
6
|
+
getOrCreateAssociatedTokenAccount,
|
7
|
+
} from "@solana/spl-token";
|
8
|
+
import {
|
9
|
+
Keypair,
|
10
|
+
PublicKey,
|
11
|
+
sendAndConfirmTransaction,
|
12
|
+
Signer,
|
13
|
+
SystemProgram,
|
14
|
+
SYSVAR_RENT_PUBKEY,
|
15
|
+
Transaction,
|
16
|
+
TransactionInstruction,
|
17
|
+
} from "@solana/web3.js";
|
18
|
+
import {
|
19
|
+
findAssociatedTokenAddress,
|
20
|
+
findVaultAddress,
|
21
|
+
getVaultTypeAccountPublicKey,
|
22
|
+
getUsdhMintPublicKey,
|
23
|
+
getVaultSystemStatePublicKey,
|
24
|
+
getPoolPublicKeyForMint,
|
25
|
+
getHedgeMintPublicKey,
|
26
|
+
} from "../Constants";
|
6
27
|
|
7
|
-
import { v4 as uuidv4 } from
|
8
|
-
import { parseAnchorErrors } from
|
28
|
+
import { v4 as uuidv4 } from "uuid";
|
29
|
+
import { parseAnchorErrors } from "../utils/Errors";
|
9
30
|
|
10
|
-
export async function createVault
|
31
|
+
export async function createVault(
|
11
32
|
program: Program,
|
12
33
|
provider: Provider,
|
13
34
|
payer: Signer,
|
@@ -15,34 +36,52 @@ export async function createVault (
|
|
15
36
|
depositAmount: number,
|
16
37
|
overrideTime?: number
|
17
38
|
): Promise<PublicKey> {
|
18
|
-
const usdhMintPublickey = await getUsdhMintPublicKey()
|
39
|
+
const usdhMintPublickey = await getUsdhMintPublicKey();
|
19
40
|
|
20
|
-
const salt = uuidv4().substring(0, 8)
|
21
|
-
const newVaultPublicKey = await findVaultAddress(salt)
|
22
|
-
const history = Keypair.generate()
|
41
|
+
const salt = uuidv4().substring(0, 8);
|
42
|
+
const newVaultPublicKey = await findVaultAddress(salt);
|
43
|
+
const history = Keypair.generate();
|
23
44
|
|
24
45
|
// Prep the user to get USDH back out at some point
|
25
|
-
await getOrCreateAssociatedTokenAccount(
|
46
|
+
await getOrCreateAssociatedTokenAccount(
|
47
|
+
provider.connection,
|
48
|
+
payer,
|
49
|
+
usdhMintPublickey,
|
50
|
+
payer.publicKey
|
51
|
+
);
|
26
52
|
|
27
|
-
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
28
|
-
|
53
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
54
|
+
collateralType
|
55
|
+
);
|
56
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(
|
57
|
+
vaultTypeAccountPublicKey
|
58
|
+
);
|
29
59
|
|
30
|
-
const payerTokenAccount = await findAssociatedTokenAddress(
|
31
|
-
|
32
|
-
|
60
|
+
const payerTokenAccount = await findAssociatedTokenAddress(
|
61
|
+
payer.publicKey,
|
62
|
+
vaultTypeAccountInfo.collateralMint
|
63
|
+
);
|
64
|
+
const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(
|
65
|
+
newVaultPublicKey,
|
66
|
+
vaultTypeAccountInfo.collateralMint
|
67
|
+
);
|
33
68
|
|
34
|
-
const wrappedSolAccount = Keypair.generate()
|
69
|
+
const wrappedSolAccount = Keypair.generate();
|
35
70
|
|
36
|
-
const transaction = new Transaction()
|
37
|
-
const signers = [payer, history]
|
71
|
+
const transaction = new Transaction();
|
72
|
+
const signers = [payer, history];
|
38
73
|
|
39
|
-
const isWrappedSol =
|
74
|
+
const isWrappedSol =
|
75
|
+
vaultTypeAccountInfo.collateralMint.toString() ===
|
76
|
+
TokenInstructions.WRAPPED_SOL_MINT.toString();
|
40
77
|
|
41
|
-
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
78
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
79
|
+
await getHedgeMintPublicKey()
|
80
|
+
);
|
42
81
|
const feePoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
|
43
82
|
hedgeStakingPoolPublicKey,
|
44
83
|
usdhMintPublickey
|
45
|
-
)
|
84
|
+
);
|
46
85
|
|
47
86
|
if (isWrappedSol) {
|
48
87
|
transaction.add(
|
@@ -51,21 +90,20 @@ export async function createVault (
|
|
51
90
|
lamports: depositAmount + 2.04e6,
|
52
91
|
newAccountPubkey: wrappedSolAccount.publicKey,
|
53
92
|
programId: TOKEN_PROGRAM_ID,
|
54
|
-
space: 165
|
93
|
+
space: 165,
|
55
94
|
}),
|
56
95
|
TokenInstructions.initializeAccount({
|
57
96
|
account: wrappedSolAccount.publicKey,
|
58
97
|
mint: TokenInstructions.WRAPPED_SOL_MINT,
|
59
|
-
owner: payer.publicKey
|
98
|
+
owner: payer.publicKey,
|
60
99
|
})
|
61
|
-
)
|
62
|
-
signers.push(wrappedSolAccount)
|
100
|
+
);
|
101
|
+
signers.push(wrappedSolAccount);
|
63
102
|
}
|
64
103
|
transaction.add(
|
65
104
|
await createVaultInstruction(
|
66
105
|
program,
|
67
106
|
salt,
|
68
|
-
vaultSystemStatePublicKey,
|
69
107
|
payer.publicKey,
|
70
108
|
isWrappedSol ? wrappedSolAccount.publicKey : payerTokenAccount,
|
71
109
|
newVaultPublicKey,
|
@@ -79,25 +117,141 @@ export async function createVault (
|
|
79
117
|
depositAmount,
|
80
118
|
overrideTime
|
81
119
|
)
|
82
|
-
)
|
120
|
+
);
|
83
121
|
if (isWrappedSol) {
|
84
122
|
transaction.add(
|
85
123
|
TokenInstructions.closeAccount({
|
86
124
|
source: wrappedSolAccount.publicKey,
|
87
125
|
destination: payer.publicKey,
|
88
|
-
owner: payer.publicKey
|
126
|
+
owner: payer.publicKey,
|
127
|
+
})
|
128
|
+
);
|
129
|
+
}
|
130
|
+
|
131
|
+
await sendAndConfirmTransaction(
|
132
|
+
provider.connection,
|
133
|
+
transaction,
|
134
|
+
signers,
|
135
|
+
provider?.opts
|
136
|
+
).catch(parseAnchorErrors);
|
137
|
+
return newVaultPublicKey;
|
138
|
+
}
|
139
|
+
|
140
|
+
export async function buildCreateVaultTransaction(
|
141
|
+
program: Program,
|
142
|
+
collateralType: string,
|
143
|
+
depositAmount: number,
|
144
|
+
overrideTime?: number
|
145
|
+
): Promise<[Transaction, Signer[], PublicKey]> {
|
146
|
+
|
147
|
+
console.log("HEDGE SDK: buildCreateVaultTransaction")
|
148
|
+
const usdhMintPublickey = await getUsdhMintPublicKey();
|
149
|
+
const payerPublicKey = program.provider.wallet.publicKey;
|
150
|
+
|
151
|
+
const salt = uuidv4().substring(0, 8);
|
152
|
+
const newVaultPublicKey = await findVaultAddress(salt);
|
153
|
+
const history = Keypair.generate() as Signer;
|
154
|
+
const { blockhash } = await program.provider.connection.getLatestBlockhash()
|
155
|
+
const transaction = new Transaction({
|
156
|
+
feePayer: payerPublicKey,
|
157
|
+
recentBlockhash: blockhash,
|
158
|
+
})
|
159
|
+
const signers = [history];
|
160
|
+
const wrappedSolAccount = Keypair.generate();
|
161
|
+
|
162
|
+
console.log('Lookup getVaultTypeAccountPublicKey', collateralType)
|
163
|
+
|
164
|
+
// Lookup the vault type info
|
165
|
+
const vaultTypeAccountPublicKey = await getVaultTypeAccountPublicKey(
|
166
|
+
collateralType
|
167
|
+
);
|
168
|
+
|
169
|
+
console.log('Lookup vaultTypeAccountPublicKey', vaultTypeAccountPublicKey.toString())
|
170
|
+
|
171
|
+
const vaultTypeAccountInfo = await program.account.vaultType.fetch(
|
172
|
+
vaultTypeAccountPublicKey
|
173
|
+
);
|
174
|
+
console.log('Lookup vaultTypeAccountInfo', vaultTypeAccountInfo)
|
175
|
+
|
176
|
+
const isWrappedSol =
|
177
|
+
vaultTypeAccountInfo.collateralMint.toString() ===
|
178
|
+
TokenInstructions.WRAPPED_SOL_MINT.toString();
|
179
|
+
|
180
|
+
const payerTokenAccount = await findAssociatedTokenAddress(
|
181
|
+
payerPublicKey,
|
182
|
+
vaultTypeAccountInfo.collateralMint
|
183
|
+
);
|
184
|
+
const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(
|
185
|
+
newVaultPublicKey,
|
186
|
+
vaultTypeAccountInfo.collateralMint
|
187
|
+
);
|
188
|
+
|
189
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(
|
190
|
+
await getHedgeMintPublicKey()
|
191
|
+
);
|
192
|
+
const feePoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(
|
193
|
+
hedgeStakingPoolPublicKey,
|
194
|
+
usdhMintPublickey
|
195
|
+
);
|
196
|
+
|
197
|
+
console.log('about to start building transaction')
|
198
|
+
|
199
|
+
// If wrapped SOL collateral, we convert the SOL to wSOL to use in the instruction
|
200
|
+
if (isWrappedSol) {
|
201
|
+
transaction.add(
|
202
|
+
SystemProgram.createAccount({
|
203
|
+
fromPubkey: payerPublicKey,
|
204
|
+
lamports: depositAmount + 2.04e6,
|
205
|
+
newAccountPubkey: wrappedSolAccount.publicKey,
|
206
|
+
programId: TOKEN_PROGRAM_ID,
|
207
|
+
space: 165,
|
208
|
+
}),
|
209
|
+
TokenInstructions.initializeAccount({
|
210
|
+
account: wrappedSolAccount.publicKey,
|
211
|
+
mint: TokenInstructions.WRAPPED_SOL_MINT,
|
212
|
+
owner: payerPublicKey,
|
89
213
|
})
|
214
|
+
);
|
215
|
+
signers.push(wrappedSolAccount);
|
216
|
+
}
|
217
|
+
console.log("hedgeStakingPoolPublicKey", hedgeStakingPoolPublicKey.toString())
|
218
|
+
transaction.add(
|
219
|
+
await createVaultInstruction(
|
220
|
+
program,
|
221
|
+
salt,
|
222
|
+
payerPublicKey,
|
223
|
+
isWrappedSol ? wrappedSolAccount.publicKey : payerTokenAccount,
|
224
|
+
newVaultPublicKey,
|
225
|
+
vaultAssociatedTokenAccount,
|
226
|
+
hedgeStakingPoolPublicKey,
|
227
|
+
feePoolAssociatedUsdhTokenAccount,
|
228
|
+
vaultTypeAccountPublicKey,
|
229
|
+
vaultTypeAccountInfo.collateralMint,
|
230
|
+
history.publicKey,
|
231
|
+
usdhMintPublickey,
|
232
|
+
depositAmount,
|
233
|
+
overrideTime
|
90
234
|
)
|
235
|
+
);
|
236
|
+
if (isWrappedSol) {
|
237
|
+
transaction.add(
|
238
|
+
TokenInstructions.closeAccount({
|
239
|
+
source: wrappedSolAccount.publicKey,
|
240
|
+
destination: payerPublicKey,
|
241
|
+
owner: payerPublicKey,
|
242
|
+
})
|
243
|
+
);
|
91
244
|
}
|
245
|
+
transaction.partialSign(wrappedSolAccount)
|
246
|
+
transaction.partialSign(history)
|
247
|
+
console.log("transaction", transaction)
|
92
248
|
|
93
|
-
|
94
|
-
return newVaultPublicKey
|
249
|
+
return [transaction, signers, newVaultPublicKey];
|
95
250
|
}
|
96
251
|
|
97
|
-
export async function createVaultInstruction
|
252
|
+
export async function createVaultInstruction(
|
98
253
|
program: Program,
|
99
254
|
salt: string,
|
100
|
-
vaultSystemStatePublicKey: PublicKey,
|
101
255
|
payerPublicKey: PublicKey,
|
102
256
|
payerTokenAccountPublicKey: PublicKey,
|
103
257
|
vaultPublicKey: PublicKey,
|
@@ -111,6 +265,8 @@ export async function createVaultInstruction (
|
|
111
265
|
depositAmount: number,
|
112
266
|
overrideTime?: number
|
113
267
|
): Promise<TransactionInstruction> {
|
268
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey();
|
269
|
+
|
114
270
|
const ix = program.instruction.createVault(
|
115
271
|
salt,
|
116
272
|
new BN(depositAmount),
|
@@ -131,9 +287,10 @@ export async function createVaultInstruction (
|
|
131
287
|
systemProgram: SystemProgram.programId,
|
132
288
|
tokenProgram: TOKEN_PROGRAM_ID,
|
133
289
|
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
|
134
|
-
rent: SYSVAR_RENT_PUBKEY
|
290
|
+
rent: SYSVAR_RENT_PUBKEY,
|
135
291
|
},
|
136
|
-
signers: []
|
137
|
-
}
|
138
|
-
|
292
|
+
signers: [],
|
293
|
+
}
|
294
|
+
);
|
295
|
+
return ix;
|
139
296
|
}
|
@@ -12,14 +12,20 @@ export class VaultAccount {
|
|
12
12
|
/** The public key of the vault owner. */
|
13
13
|
vaultOwner: PublicKey
|
14
14
|
|
15
|
-
/** The
|
16
|
-
|
17
|
-
|
15
|
+
/** The public key of the vault owner. */
|
16
|
+
pdaSalt: string
|
17
|
+
|
18
18
|
/** The deposited collateral of the vault (in SOL Lamports). */
|
19
19
|
deposited: number
|
20
20
|
|
21
|
-
/** The
|
22
|
-
|
21
|
+
/** The outstanding debt of the vault (in USDH Lamports). Denormalized to time 0. */
|
22
|
+
denormalizedDebt: number
|
23
|
+
|
24
|
+
debtProductSnapshotBytes: Decimal
|
25
|
+
|
26
|
+
collateralAccumulatorSnapshotBytes: Decimal
|
27
|
+
|
28
|
+
collateralType: string
|
23
29
|
|
24
30
|
/** Current State of the vautl ("Open", "Closed", "Liquidated") */
|
25
31
|
vaultStatus: string
|
@@ -27,9 +33,13 @@ export class VaultAccount {
|
|
27
33
|
constructor (vault: any, publicKey: PublicKey) {
|
28
34
|
this.publicKey = publicKey
|
29
35
|
this.vaultOwner = vault.vaultOwner
|
30
|
-
this.
|
36
|
+
this.pdaSalt = vault.pdaSalt
|
31
37
|
this.deposited = vault.deposited.toNumber()
|
32
|
-
this.
|
38
|
+
this.denormalizedDebt = vault.denormalizedDebt.toNumber()
|
39
|
+
this.debtProductSnapshotBytes = DecimalFromU128(vault.debtProductSnapshotBytes.toString())
|
40
|
+
this.collateralAccumulatorSnapshotBytes = DecimalFromU128(vault.collateralAccumulatorSnapshotBytes.toString())
|
41
|
+
this.collateralType = vault.collateralType
|
42
|
+
|
33
43
|
this.vaultStatus = Object.keys(vault.vaultStatus)[0]
|
34
44
|
}
|
35
45
|
|
@@ -40,7 +50,8 @@ export class VaultAccount {
|
|
40
50
|
* @returns true if publicKey matches the owner publicKey
|
41
51
|
*/
|
42
52
|
public isOwnedBy (publicKey: PublicKey): boolean {
|
43
|
-
|
53
|
+
|
54
|
+
return publicKey && publicKey.toString() === this.vaultOwner.toString()
|
44
55
|
}
|
45
56
|
|
46
57
|
/**
|
@@ -58,7 +69,7 @@ export class VaultAccount {
|
|
58
69
|
* @returns debt value in USDH
|
59
70
|
*/
|
60
71
|
public inUsd (): number {
|
61
|
-
return this.
|
72
|
+
return this.denormalizedDebt / LAMPORTS_PER_SOL
|
62
73
|
}
|
63
74
|
|
64
75
|
/**
|