hedge-web3 0.2.25 → 0.2.26
Sign up to get free protection for your applications and to get access to all the features.
- package/declarations/state/VaultAccount.d.ts +15 -2
- package/lib/instructions/createVault.js +1 -1
- package/lib/state/VaultAccount.js +16 -16
- package/lib/utils/getLinkedListAccounts.js +18 -1
- package/package.json +1 -1
- package/src/instructions/createVault.ts +10 -5
- package/src/state/VaultAccount.ts +31 -17
- package/src/utils/getLinkedListAccounts.ts +16 -0
@@ -3,6 +3,19 @@ import { PublicKey } from '@solana/web3.js';
|
|
3
3
|
import Decimal from 'decimal.js';
|
4
4
|
import BN from 'bn.js';
|
5
5
|
import VaultType from './VaultType';
|
6
|
+
export declare type VaultAccountData = {
|
7
|
+
vaultOwner: PublicKey;
|
8
|
+
pdaSalt: string;
|
9
|
+
deposited: BN;
|
10
|
+
denormalizedDebt: BN;
|
11
|
+
vaultNumber: BN;
|
12
|
+
debtProductSnapshotBytes: BN;
|
13
|
+
collateralAccumulatorSnapshotBytes: BN;
|
14
|
+
vaultTypeName: string;
|
15
|
+
nextVaultToRedeem: PublicKey | null;
|
16
|
+
vaultStatus: any;
|
17
|
+
vaultType: PublicKey;
|
18
|
+
};
|
6
19
|
/**
|
7
20
|
* A class that represents an on-chian vault.
|
8
21
|
*/
|
@@ -28,10 +41,10 @@ export declare class VaultAccount {
|
|
28
41
|
/** Current State of the vault ("Open", "Closed", "Liquidated") */
|
29
42
|
vaultStatus: string;
|
30
43
|
/** The public key of the next vault to redeem. */
|
31
|
-
nextVaultToRedeem: PublicKey;
|
44
|
+
nextVaultToRedeem: PublicKey | null;
|
32
45
|
/** The public key of the vault type. */
|
33
46
|
vaultType: PublicKey;
|
34
|
-
constructor(
|
47
|
+
constructor(vaultData: VaultAccountData, publicKey: PublicKey);
|
35
48
|
/**
|
36
49
|
* Check if some `PublicKey` is the owner
|
37
50
|
*
|
@@ -73,7 +73,7 @@ function buildCreateVaultTransaction(program, payerPublicKey, collateralType, de
|
|
73
73
|
const salt = (0, uuid_1.v4)().substring(0, 8);
|
74
74
|
const newVaultPublicKey = yield (0, Constants_1.findVaultAddress)(program.programId, salt);
|
75
75
|
const history = web3_js_1.Keypair.generate();
|
76
|
-
const { blockhash } = yield program.provider.connection.getLatestBlockhash();
|
76
|
+
const { blockhash, lastValidBlockHeight } = yield program.provider.connection.getLatestBlockhash();
|
77
77
|
const transaction = new web3_js_1.Transaction({
|
78
78
|
feePayer: payerPublicKey,
|
79
79
|
recentBlockhash: blockhash,
|
@@ -32,7 +32,7 @@ const bn_js_1 = __importDefault(require("bn.js"));
|
|
32
32
|
* A class that represents an on-chian vault.
|
33
33
|
*/
|
34
34
|
class VaultAccount {
|
35
|
-
constructor(
|
35
|
+
constructor(vaultData, publicKey) {
|
36
36
|
/** The deposited collateral of the vault (in SOL Lamports). */
|
37
37
|
this.deposited = new bn_js_1.default(0);
|
38
38
|
/** The outstanding debt of the vault (in USH Lamports). Denormalized to time 0. */
|
@@ -46,25 +46,25 @@ class VaultAccount {
|
|
46
46
|
/** Current State of the vault ("Open", "Closed", "Liquidated") */
|
47
47
|
this.vaultStatus = '';
|
48
48
|
this.publicKey = publicKey;
|
49
|
-
this.vaultOwner =
|
50
|
-
this.pdaSalt =
|
51
|
-
this.deposited =
|
52
|
-
this.denormalizedDebt =
|
53
|
-
if (
|
54
|
-
this.vaultNumber =
|
49
|
+
this.vaultOwner = vaultData.vaultOwner;
|
50
|
+
this.pdaSalt = vaultData.pdaSalt;
|
51
|
+
this.deposited = vaultData.deposited;
|
52
|
+
this.denormalizedDebt = vaultData.denormalizedDebt;
|
53
|
+
if (vaultData.vaultNumber) {
|
54
|
+
this.vaultNumber = vaultData.vaultNumber.toNumber();
|
55
55
|
}
|
56
|
-
if (
|
57
|
-
this.debtProductSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(
|
56
|
+
if (vaultData.debtProductSnapshotBytes) {
|
57
|
+
this.debtProductSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(vaultData.debtProductSnapshotBytes.toString());
|
58
58
|
}
|
59
|
-
if (
|
60
|
-
this.collateralAccumulatorSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(
|
59
|
+
if (vaultData.collateralAccumulatorSnapshotBytes) {
|
60
|
+
this.collateralAccumulatorSnapshotBytes = (0, HedgeDecimal_1.DecimalFromU128)(vaultData.collateralAccumulatorSnapshotBytes.toString());
|
61
61
|
}
|
62
|
-
this.vaultTypeName =
|
63
|
-
this.nextVaultToRedeem =
|
64
|
-
if (
|
65
|
-
this.vaultStatus = Object.keys(
|
62
|
+
this.vaultTypeName = vaultData.vaultTypeName;
|
63
|
+
this.nextVaultToRedeem = vaultData.nextVaultToRedeem;
|
64
|
+
if (vaultData.vaultStatus) {
|
65
|
+
this.vaultStatus = Object.keys(vaultData.vaultStatus)[0];
|
66
66
|
}
|
67
|
-
this.vaultType =
|
67
|
+
this.vaultType = vaultData.vaultType;
|
68
68
|
}
|
69
69
|
/**
|
70
70
|
* Check if some `PublicKey` is the owner
|
@@ -14,6 +14,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
15
15
|
exports.getLinkedListAccounts = void 0;
|
16
16
|
const anchor_1 = require("@project-serum/anchor");
|
17
|
+
const web3_js_1 = require("@solana/web3.js");
|
17
18
|
const underscore_1 = __importDefault(require("underscore"));
|
18
19
|
const decimal_js_1 = __importDefault(require("decimal.js"));
|
19
20
|
const VaultAccount_1 = require("../state/VaultAccount");
|
@@ -42,7 +43,23 @@ function getLinkedListAccounts(program, vaultTypeAccountPublicKey, vaultPublicKe
|
|
42
43
|
let oldSmallerPublicKey = vaultPublicKey;
|
43
44
|
let newSmallerPublicKey = vaultPublicKey;
|
44
45
|
let newLargerPublicKey = vaultPublicKey;
|
45
|
-
const thisVaultData = yield program.account.vault.fetch(vaultPublicKey)
|
46
|
+
const thisVaultData = yield program.account.vault.fetch(vaultPublicKey)
|
47
|
+
.catch((e) => {
|
48
|
+
console.log('Error fetching vault account. Assuming it does not exist yet');
|
49
|
+
return {
|
50
|
+
vaultOwner: new web3_js_1.PublicKey('11111111111111111111111111111111'),
|
51
|
+
pdaSalt: 'foo',
|
52
|
+
deposited: new anchor_1.BN(0),
|
53
|
+
denormalizedDebt: new anchor_1.BN(0),
|
54
|
+
vaultNumber: new anchor_1.BN(0),
|
55
|
+
debtProductSnapshotBytes: new anchor_1.BN(0),
|
56
|
+
collateralAccumulatorSnapshotBytes: new anchor_1.BN(0),
|
57
|
+
vaultTypeName: vaultType.name,
|
58
|
+
nextVaultToRedeem: new web3_js_1.PublicKey('11111111111111111111111111111111'),
|
59
|
+
vaultStatus: { open: true },
|
60
|
+
vaultType: vaultTypeAccountPublicKey
|
61
|
+
};
|
62
|
+
});
|
46
63
|
// const accountInfo = await program.provider.connection.getAccountInfo(vaultPublicKey)
|
47
64
|
const thisVault = new VaultAccount_1.VaultAccount(thisVaultData, vaultPublicKey);
|
48
65
|
// Load all the vaults
|
package/package.json
CHANGED
@@ -3,16 +3,21 @@ import { TokenInstructions } from '@project-serum/serum'
|
|
3
3
|
import { ASSOCIATED_TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
|
4
4
|
import {
|
5
5
|
Keypair,
|
6
|
-
PublicKey,
|
6
|
+
PublicKey,
|
7
|
+
Signer,
|
7
8
|
SystemProgram,
|
8
9
|
SYSVAR_RENT_PUBKEY,
|
9
10
|
Transaction,
|
10
|
-
TransactionInstruction
|
11
|
+
TransactionInstruction,
|
11
12
|
} from '@solana/web3.js'
|
12
13
|
import {
|
13
14
|
findAssociatedTokenAddress,
|
14
|
-
findVaultAddress,
|
15
|
-
|
15
|
+
findVaultAddress,
|
16
|
+
getHedgeMintPublicKey,
|
17
|
+
getPoolPublicKeyForMint,
|
18
|
+
getUshMintPublicKey,
|
19
|
+
getVaultSystemStatePublicKey,
|
20
|
+
getVaultTypeAccountPublicKey,
|
16
21
|
} from '../Constants'
|
17
22
|
|
18
23
|
import { v4 as uuidv4 } from 'uuid'
|
@@ -129,7 +134,7 @@ export async function buildCreateVaultTransaction(
|
|
129
134
|
const salt = uuidv4().substring(0, 8)
|
130
135
|
const newVaultPublicKey = await findVaultAddress(program.programId, salt)
|
131
136
|
const history = Keypair.generate() as Signer
|
132
|
-
const { blockhash } = await program.provider.connection.getLatestBlockhash()
|
137
|
+
const { blockhash, lastValidBlockHeight } = await program.provider.connection.getLatestBlockhash()
|
133
138
|
const transaction = new Transaction({
|
134
139
|
feePayer: payerPublicKey,
|
135
140
|
recentBlockhash: blockhash,
|
@@ -6,6 +6,20 @@ import * as borsh from '@project-serum/borsh'
|
|
6
6
|
import BN from 'bn.js'
|
7
7
|
import VaultType from './VaultType'
|
8
8
|
|
9
|
+
export type VaultAccountData = {
|
10
|
+
vaultOwner: PublicKey
|
11
|
+
pdaSalt: string
|
12
|
+
deposited: BN
|
13
|
+
denormalizedDebt: BN
|
14
|
+
vaultNumber: BN
|
15
|
+
debtProductSnapshotBytes: BN
|
16
|
+
collateralAccumulatorSnapshotBytes: BN
|
17
|
+
vaultTypeName: string
|
18
|
+
nextVaultToRedeem: PublicKey | null
|
19
|
+
vaultStatus: any
|
20
|
+
vaultType: PublicKey
|
21
|
+
}
|
22
|
+
|
9
23
|
/**
|
10
24
|
* A class that represents an on-chian vault.
|
11
25
|
*/
|
@@ -41,35 +55,35 @@ export class VaultAccount {
|
|
41
55
|
vaultStatus = ''
|
42
56
|
|
43
57
|
/** The public key of the next vault to redeem. */
|
44
|
-
nextVaultToRedeem: PublicKey
|
58
|
+
nextVaultToRedeem: PublicKey | null
|
45
59
|
|
46
60
|
/** The public key of the vault type. */
|
47
61
|
vaultType: PublicKey
|
48
62
|
|
49
|
-
constructor(
|
63
|
+
constructor(vaultData: VaultAccountData, publicKey: PublicKey) {
|
50
64
|
this.publicKey = publicKey
|
51
|
-
this.vaultOwner =
|
52
|
-
this.pdaSalt =
|
65
|
+
this.vaultOwner = vaultData.vaultOwner
|
66
|
+
this.pdaSalt = vaultData.pdaSalt
|
53
67
|
|
54
|
-
this.deposited =
|
55
|
-
this.denormalizedDebt =
|
68
|
+
this.deposited = vaultData.deposited
|
69
|
+
this.denormalizedDebt = vaultData.denormalizedDebt
|
56
70
|
|
57
|
-
if (
|
58
|
-
this.vaultNumber =
|
71
|
+
if (vaultData.vaultNumber) {
|
72
|
+
this.vaultNumber = vaultData.vaultNumber.toNumber()
|
59
73
|
}
|
60
|
-
if (
|
61
|
-
this.debtProductSnapshotBytes = DecimalFromU128(
|
74
|
+
if (vaultData.debtProductSnapshotBytes) {
|
75
|
+
this.debtProductSnapshotBytes = DecimalFromU128(vaultData.debtProductSnapshotBytes.toString())
|
62
76
|
}
|
63
|
-
if (
|
64
|
-
this.collateralAccumulatorSnapshotBytes = DecimalFromU128(
|
77
|
+
if (vaultData.collateralAccumulatorSnapshotBytes) {
|
78
|
+
this.collateralAccumulatorSnapshotBytes = DecimalFromU128(vaultData.collateralAccumulatorSnapshotBytes.toString())
|
65
79
|
}
|
66
|
-
this.vaultTypeName =
|
67
|
-
this.nextVaultToRedeem =
|
80
|
+
this.vaultTypeName = vaultData.vaultTypeName
|
81
|
+
this.nextVaultToRedeem = vaultData.nextVaultToRedeem
|
68
82
|
|
69
|
-
if (
|
70
|
-
this.vaultStatus = Object.keys(
|
83
|
+
if (vaultData.vaultStatus) {
|
84
|
+
this.vaultStatus = Object.keys(vaultData.vaultStatus)[0]
|
71
85
|
}
|
72
|
-
this.vaultType =
|
86
|
+
this.vaultType = vaultData.vaultType
|
73
87
|
}
|
74
88
|
|
75
89
|
/**
|
@@ -46,6 +46,22 @@ export async function getLinkedListAccounts(
|
|
46
46
|
let newLargerPublicKey = vaultPublicKey
|
47
47
|
|
48
48
|
const thisVaultData = await program.account.vault.fetch(vaultPublicKey)
|
49
|
+
.catch((e) => {
|
50
|
+
console.log('Error fetching vault account. Assuming it does not exist yet')
|
51
|
+
return {
|
52
|
+
vaultOwner: new PublicKey('11111111111111111111111111111111'),
|
53
|
+
pdaSalt: 'foo',
|
54
|
+
deposited: new BN(0),
|
55
|
+
denormalizedDebt: new BN(0),
|
56
|
+
vaultNumber: new BN(0),
|
57
|
+
debtProductSnapshotBytes: new BN(0),
|
58
|
+
collateralAccumulatorSnapshotBytes: new BN(0),
|
59
|
+
vaultTypeName: vaultType.name,
|
60
|
+
nextVaultToRedeem: new PublicKey('11111111111111111111111111111111'),
|
61
|
+
vaultStatus: {open: true},
|
62
|
+
vaultType: vaultTypeAccountPublicKey
|
63
|
+
}
|
64
|
+
})
|
49
65
|
// const accountInfo = await program.provider.connection.getAccountInfo(vaultPublicKey)
|
50
66
|
const thisVault = new VaultAccount(thisVaultData, vaultPublicKey)
|
51
67
|
|