hedge-web3 0.2.19 → 0.2.20
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/idl/vault.d.ts +20 -0
- package/declarations/instructions/transferVault.d.ts +19 -0
- package/declarations/state/VaultAccount.d.ts +1 -2
- package/lib/idl/vault.js +20 -0
- package/lib/instructions/transferVault.js +55 -0
- package/lib/state/VaultAccount.js +3 -1
- package/package.json +1 -1
- package/src/idl/vault.ts +40 -0
- package/src/instructions/transferVault.ts +99 -0
- package/src/state/VaultAccount.ts +9 -10
@@ -3076,6 +3076,26 @@ export declare type Vault = {
|
|
3076
3076
|
"index": false;
|
3077
3077
|
}
|
3078
3078
|
];
|
3079
|
+
},
|
3080
|
+
{
|
3081
|
+
"name": "VaultTransferEvent";
|
3082
|
+
"fields": [
|
3083
|
+
{
|
3084
|
+
"name": "vault";
|
3085
|
+
"type": "publicKey";
|
3086
|
+
"index": false;
|
3087
|
+
},
|
3088
|
+
{
|
3089
|
+
"name": "oldOwner";
|
3090
|
+
"type": "publicKey";
|
3091
|
+
"index": false;
|
3092
|
+
},
|
3093
|
+
{
|
3094
|
+
"name": "newOwner";
|
3095
|
+
"type": "publicKey";
|
3096
|
+
"index": false;
|
3097
|
+
}
|
3098
|
+
];
|
3079
3099
|
}
|
3080
3100
|
];
|
3081
3101
|
"errors": [
|
@@ -0,0 +1,19 @@
|
|
1
|
+
/// <reference types="bn.js" />
|
2
|
+
import { BN, Program, Provider } from '@project-serum/anchor';
|
3
|
+
import { PublicKey, Signer, TransactionInstruction } from '@solana/web3.js';
|
4
|
+
import { Vault } from '../idl/vault';
|
5
|
+
export interface VaultTypeConfig {
|
6
|
+
maxDebtExtended?: BN;
|
7
|
+
minDebtPerVault?: BN;
|
8
|
+
loanInitFee?: BN;
|
9
|
+
emergencyModeThreshold?: BN;
|
10
|
+
oracleChainlink?: PublicKey;
|
11
|
+
oraclePyth?: PublicKey;
|
12
|
+
oracleSwitchboard?: PublicKey;
|
13
|
+
priorityPyth?: number;
|
14
|
+
priorityChainlink?: number;
|
15
|
+
prioritySwitchboard?: number;
|
16
|
+
deprecated?: boolean;
|
17
|
+
}
|
18
|
+
export declare function transferVault(program: Program<Vault>, provider: Provider, payer: Signer, vaultTypeAccount: PublicKey, oracleInfoAccount: PublicKey, config: VaultTypeConfig): Promise<PublicKey>;
|
19
|
+
export declare function transferVaultInstruction(program: Program<Vault>, vaultSystemStatePublicKey: PublicKey, payerPublicKey: PublicKey, vaultTypeAccount: PublicKey, oracleInfoAccount: PublicKey, vaultTypeConfig: VaultTypeConfig): Promise<TransactionInstruction>;
|
@@ -11,8 +11,7 @@ export declare class VaultAccount {
|
|
11
11
|
publicKey: PublicKey;
|
12
12
|
/** The public key of the vault owner. */
|
13
13
|
vaultOwner: PublicKey;
|
14
|
-
/** The public key of the vault owner. */
|
15
|
-
pdaSalt: string;
|
14
|
+
/** The public key of the vault owner. */ pdaSalt: string;
|
16
15
|
/** The deposited collateral of the vault (in SOL Lamports). */
|
17
16
|
deposited: BN;
|
18
17
|
/** The outstanding debt of the vault (in USH Lamports). Denormalized to time 0. */
|
package/lib/idl/vault.js
CHANGED
@@ -3079,6 +3079,26 @@ exports.IDL = {
|
|
3079
3079
|
"index": false
|
3080
3080
|
}
|
3081
3081
|
]
|
3082
|
+
},
|
3083
|
+
{
|
3084
|
+
"name": "VaultTransferEvent",
|
3085
|
+
"fields": [
|
3086
|
+
{
|
3087
|
+
"name": "vault",
|
3088
|
+
"type": "publicKey",
|
3089
|
+
"index": false
|
3090
|
+
},
|
3091
|
+
{
|
3092
|
+
"name": "oldOwner",
|
3093
|
+
"type": "publicKey",
|
3094
|
+
"index": false
|
3095
|
+
},
|
3096
|
+
{
|
3097
|
+
"name": "newOwner",
|
3098
|
+
"type": "publicKey",
|
3099
|
+
"index": false
|
3100
|
+
}
|
3101
|
+
]
|
3082
3102
|
}
|
3083
3103
|
],
|
3084
3104
|
"errors": [
|
@@ -0,0 +1,55 @@
|
|
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.transferVaultInstruction = exports.transferVault = void 0;
|
16
|
+
const web3_js_1 = require("@solana/web3.js");
|
17
|
+
const Constants_1 = require("../Constants");
|
18
|
+
const sendAndConfirmWithDebug_1 = __importDefault(require("../utils/sendAndConfirmWithDebug"));
|
19
|
+
function transferVault(program, provider, payer, vaultTypeAccount, oracleInfoAccount, config) {
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
21
|
+
const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
|
22
|
+
const transaction = new web3_js_1.Transaction().add(yield transferVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultTypeAccount, oracleInfoAccount, config));
|
23
|
+
yield (0, sendAndConfirmWithDebug_1.default)(provider.connection, transaction, [payer]);
|
24
|
+
return vaultSystemStatePublicKey;
|
25
|
+
});
|
26
|
+
}
|
27
|
+
exports.transferVault = transferVault;
|
28
|
+
function transferVaultInstruction(program, vaultSystemStatePublicKey, payerPublicKey, vaultTypeAccount, oracleInfoAccount, vaultTypeConfig) {
|
29
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
31
|
+
const config = {
|
32
|
+
maxDebtExtended: (_a = vaultTypeConfig.maxDebtExtended) !== null && _a !== void 0 ? _a : null,
|
33
|
+
minDebtPerVault: (_b = vaultTypeConfig.minDebtPerVault) !== null && _b !== void 0 ? _b : null,
|
34
|
+
loanInitFee: (_c = vaultTypeConfig.loanInitFee) !== null && _c !== void 0 ? _c : null,
|
35
|
+
emergencyModeThreshold: (_d = vaultTypeConfig.emergencyModeThreshold) !== null && _d !== void 0 ? _d : null,
|
36
|
+
oracleChainlink: (_e = vaultTypeConfig.oracleChainlink) !== null && _e !== void 0 ? _e : null,
|
37
|
+
oraclePyth: (_f = vaultTypeConfig.oraclePyth) !== null && _f !== void 0 ? _f : null,
|
38
|
+
oracleSwitchboard: (_g = vaultTypeConfig.oracleSwitchboard) !== null && _g !== void 0 ? _g : null,
|
39
|
+
deprecated: (_h = vaultTypeConfig.deprecated) !== null && _h !== void 0 ? _h : null,
|
40
|
+
priorityPyth: (_j = vaultTypeConfig.priorityPyth) !== null && _j !== void 0 ? _j : null,
|
41
|
+
priorityChainlink: (_k = vaultTypeConfig.priorityChainlink) !== null && _k !== void 0 ? _k : null,
|
42
|
+
prioritySwitchboard: (_l = vaultTypeConfig.prioritySwitchboard) !== null && _l !== void 0 ? _l : null,
|
43
|
+
};
|
44
|
+
return yield program.methods
|
45
|
+
.updateVaultType(config)
|
46
|
+
.accounts({
|
47
|
+
payer: payerPublicKey,
|
48
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
49
|
+
vaultType: vaultTypeAccount,
|
50
|
+
oracleInfoAccount: oracleInfoAccount,
|
51
|
+
})
|
52
|
+
.instruction();
|
53
|
+
});
|
54
|
+
}
|
55
|
+
exports.transferVaultInstruction = transferVaultInstruction;
|
@@ -163,7 +163,9 @@ class VaultAccount {
|
|
163
163
|
.mul(new decimal_js_1.default(this.denormalizedDebt.toString()))
|
164
164
|
.floor()
|
165
165
|
.toString());
|
166
|
-
const extraCollateralDeposited = new decimal_js_1.default(this.denormalizedDebt.toString())
|
166
|
+
const extraCollateralDeposited = new decimal_js_1.default(this.denormalizedDebt.toString())
|
167
|
+
.mul(vaultTypeAccountData.collateralRedistributionAccumulator.sub(this.collateralAccumulatorSnapshotBytes).toNumber())
|
168
|
+
.floor();
|
167
169
|
this.deposited = this.deposited.add(new bn_js_1.default(extraCollateralDeposited.toString()));
|
168
170
|
this.collateralAccumulatorSnapshotBytes = vaultTypeAccountData.collateralRedistributionAccumulator;
|
169
171
|
this.debtProductSnapshotBytes = vaultTypeAccountData.debtRedistributionProduct;
|
package/package.json
CHANGED
package/src/idl/vault.ts
CHANGED
@@ -3076,6 +3076,26 @@ export type Vault = {
|
|
3076
3076
|
"index": false
|
3077
3077
|
}
|
3078
3078
|
]
|
3079
|
+
},
|
3080
|
+
{
|
3081
|
+
"name": "VaultTransferEvent",
|
3082
|
+
"fields": [
|
3083
|
+
{
|
3084
|
+
"name": "vault",
|
3085
|
+
"type": "publicKey",
|
3086
|
+
"index": false
|
3087
|
+
},
|
3088
|
+
{
|
3089
|
+
"name": "oldOwner",
|
3090
|
+
"type": "publicKey",
|
3091
|
+
"index": false
|
3092
|
+
},
|
3093
|
+
{
|
3094
|
+
"name": "newOwner",
|
3095
|
+
"type": "publicKey",
|
3096
|
+
"index": false
|
3097
|
+
}
|
3098
|
+
]
|
3079
3099
|
}
|
3080
3100
|
],
|
3081
3101
|
"errors": [
|
@@ -6335,6 +6355,26 @@ export const IDL: Vault = {
|
|
6335
6355
|
"index": false
|
6336
6356
|
}
|
6337
6357
|
]
|
6358
|
+
},
|
6359
|
+
{
|
6360
|
+
"name": "VaultTransferEvent",
|
6361
|
+
"fields": [
|
6362
|
+
{
|
6363
|
+
"name": "vault",
|
6364
|
+
"type": "publicKey",
|
6365
|
+
"index": false
|
6366
|
+
},
|
6367
|
+
{
|
6368
|
+
"name": "oldOwner",
|
6369
|
+
"type": "publicKey",
|
6370
|
+
"index": false
|
6371
|
+
},
|
6372
|
+
{
|
6373
|
+
"name": "newOwner",
|
6374
|
+
"type": "publicKey",
|
6375
|
+
"index": false
|
6376
|
+
}
|
6377
|
+
]
|
6338
6378
|
}
|
6339
6379
|
],
|
6340
6380
|
"errors": [
|
@@ -0,0 +1,99 @@
|
|
1
|
+
import { BN, Program, Provider } from '@project-serum/anchor'
|
2
|
+
import {
|
3
|
+
Keypair,
|
4
|
+
PublicKey,
|
5
|
+
sendAndConfirmTransaction,
|
6
|
+
Signer,
|
7
|
+
SystemProgram,
|
8
|
+
SYSVAR_RENT_PUBKEY,
|
9
|
+
Transaction,
|
10
|
+
TransactionInstruction,
|
11
|
+
} from '@solana/web3.js'
|
12
|
+
import {
|
13
|
+
findAssociatedTokenAddress,
|
14
|
+
findVaultAddress,
|
15
|
+
getVaultTypeAccountPublicKey,
|
16
|
+
getUshMintPublicKey,
|
17
|
+
getVaultSystemStatePublicKey,
|
18
|
+
getPoolPublicKeyForMint,
|
19
|
+
getHedgeMintPublicKey,
|
20
|
+
} from '../Constants'
|
21
|
+
|
22
|
+
import { v4 as uuidv4 } from 'uuid'
|
23
|
+
import { parseAnchorErrors } from '../utils/Errors'
|
24
|
+
import sendAndConfirmWithDebug from '../utils/sendAndConfirmWithDebug'
|
25
|
+
import { Vault } from '../idl/vault'
|
26
|
+
|
27
|
+
export interface VaultTypeConfig {
|
28
|
+
maxDebtExtended?: BN
|
29
|
+
minDebtPerVault?: BN
|
30
|
+
loanInitFee?: BN
|
31
|
+
emergencyModeThreshold?: BN
|
32
|
+
|
33
|
+
oracleChainlink?: PublicKey
|
34
|
+
oraclePyth?: PublicKey
|
35
|
+
oracleSwitchboard?: PublicKey
|
36
|
+
|
37
|
+
priorityPyth?: number
|
38
|
+
priorityChainlink?: number
|
39
|
+
prioritySwitchboard?: number
|
40
|
+
|
41
|
+
deprecated?: boolean
|
42
|
+
}
|
43
|
+
|
44
|
+
export async function transferVault(
|
45
|
+
program: Program<Vault>,
|
46
|
+
provider: Provider,
|
47
|
+
payer: Signer,
|
48
|
+
vaultTypeAccount: PublicKey,
|
49
|
+
oracleInfoAccount: PublicKey,
|
50
|
+
config: VaultTypeConfig
|
51
|
+
): Promise<PublicKey> {
|
52
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
|
53
|
+
const transaction = new Transaction().add(
|
54
|
+
await transferVaultInstruction(
|
55
|
+
program,
|
56
|
+
vaultSystemStatePublicKey,
|
57
|
+
payer.publicKey,
|
58
|
+
vaultTypeAccount,
|
59
|
+
oracleInfoAccount,
|
60
|
+
config
|
61
|
+
)
|
62
|
+
)
|
63
|
+
|
64
|
+
await sendAndConfirmWithDebug(provider.connection, transaction, [payer])
|
65
|
+
return vaultSystemStatePublicKey
|
66
|
+
}
|
67
|
+
|
68
|
+
export async function transferVaultInstruction(
|
69
|
+
program: Program<Vault>,
|
70
|
+
vaultSystemStatePublicKey: PublicKey,
|
71
|
+
payerPublicKey: PublicKey,
|
72
|
+
vaultTypeAccount: PublicKey,
|
73
|
+
oracleInfoAccount: PublicKey,
|
74
|
+
vaultTypeConfig: VaultTypeConfig
|
75
|
+
): Promise<TransactionInstruction> {
|
76
|
+
const config = {
|
77
|
+
maxDebtExtended: vaultTypeConfig.maxDebtExtended ?? null,
|
78
|
+
minDebtPerVault: vaultTypeConfig.minDebtPerVault ?? null,
|
79
|
+
loanInitFee: vaultTypeConfig.loanInitFee ?? null,
|
80
|
+
emergencyModeThreshold: vaultTypeConfig.emergencyModeThreshold ?? null,
|
81
|
+
oracleChainlink: vaultTypeConfig.oracleChainlink ?? null,
|
82
|
+
oraclePyth: vaultTypeConfig.oraclePyth ?? null,
|
83
|
+
oracleSwitchboard: vaultTypeConfig.oracleSwitchboard ?? null,
|
84
|
+
deprecated: vaultTypeConfig.deprecated ?? null,
|
85
|
+
priorityPyth: vaultTypeConfig.priorityPyth ?? null,
|
86
|
+
priorityChainlink: vaultTypeConfig.priorityChainlink ?? null,
|
87
|
+
prioritySwitchboard: vaultTypeConfig.prioritySwitchboard ?? null,
|
88
|
+
}
|
89
|
+
|
90
|
+
return await program.methods
|
91
|
+
.updateVaultType(config)
|
92
|
+
.accounts({
|
93
|
+
payer: payerPublicKey,
|
94
|
+
vaultSystemState: vaultSystemStatePublicKey,
|
95
|
+
vaultType: vaultTypeAccount,
|
96
|
+
oracleInfoAccount: oracleInfoAccount,
|
97
|
+
})
|
98
|
+
.instruction()
|
99
|
+
}
|
@@ -16,7 +16,7 @@ export class VaultAccount {
|
|
16
16
|
/** The public key of the vault owner. */
|
17
17
|
vaultOwner: PublicKey
|
18
18
|
|
19
|
-
/** The public key of the vault owner. */
|
19
|
+
/** The public key of the vault owner. */ // dont think this is correct? thoughts?
|
20
20
|
pdaSalt: string
|
21
21
|
|
22
22
|
/** The deposited collateral of the vault (in SOL Lamports). */
|
@@ -50,7 +50,7 @@ export class VaultAccount {
|
|
50
50
|
this.publicKey = publicKey
|
51
51
|
this.vaultOwner = vault.vaultOwner
|
52
52
|
this.pdaSalt = vault.pdaSalt
|
53
|
-
|
53
|
+
|
54
54
|
this.deposited = vault.deposited
|
55
55
|
this.denormalizedDebt = vault.denormalizedDebt
|
56
56
|
|
@@ -177,16 +177,15 @@ export class VaultAccount {
|
|
177
177
|
public updateDebtAndCollateral(vaultTypeAccountData: VaultType) {
|
178
178
|
this.denormalizedDebt = new BN(
|
179
179
|
vaultTypeAccountData.debtRedistributionProduct
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
180
|
+
.div(this.debtProductSnapshotBytes)
|
181
|
+
.mul(new Decimal(this.denormalizedDebt.toString()))
|
182
|
+
.floor()
|
183
|
+
.toString()
|
184
184
|
)
|
185
185
|
|
186
|
-
const extraCollateralDeposited =
|
187
|
-
|
188
|
-
|
189
|
-
).floor()
|
186
|
+
const extraCollateralDeposited = new Decimal(this.denormalizedDebt.toString())
|
187
|
+
.mul(vaultTypeAccountData.collateralRedistributionAccumulator.sub(this.collateralAccumulatorSnapshotBytes).toNumber())
|
188
|
+
.floor()
|
190
189
|
this.deposited = this.deposited.add(new BN(extraCollateralDeposited.toString()))
|
191
190
|
|
192
191
|
this.collateralAccumulatorSnapshotBytes = vaultTypeAccountData.collateralRedistributionAccumulator
|