hedge-web3 0.1.17 → 0.1.20

Sign up to get free protection for your applications and to get access to all the features.
@@ -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>;
@@ -13,6 +13,7 @@ export * from './instructions/redeemVault';
13
13
  export * from './instructions/liquidateVault';
14
14
  export * from './instructions/refreshOraclePrice';
15
15
  export * from './instructions/initHedgeFoundation';
16
+ export * from './instructions/initHedgeFoundationTokens';
16
17
  export * from './instructions/setHalted';
17
18
  export * from './HedgeDecimal';
18
19
  export * from './Constants';
@@ -1,4 +1,4 @@
1
1
  import { Program, Provider } from '@project-serum/anchor';
2
2
  import { PublicKey, Signer, TransactionInstruction } from '@solana/web3.js';
3
3
  export declare function closeLiquidationPoolPosition(program: Program, provider: Provider, poolPosition: PublicKey, payer: Signer, overrideStartTime?: number): Promise<PublicKey>;
4
- export declare function closeLiquidationPoolPositionInstruction(program: Program, vaultSystemState: PublicKey, poolState: PublicKey, poolEra: PublicKey, poolPosition: PublicKey, poolUsdhAccount: PublicKey, payerPublicKey: PublicKey, payerUsdhAccount: PublicKey, hedgeMint: PublicKey, payerAssociatedHedgeAccount: PublicKey, communityAssociatedHedgeTokenAccount: PublicKey, overrideStartTime?: number): Promise<TransactionInstruction>;
4
+ export declare function closeLiquidationPoolPositionInstruction(program: Program, poolEra: PublicKey, poolPosition: PublicKey, payerPublicKey: PublicKey, payerUsdhAccount: PublicKey, payerAssociatedHedgeAccount: PublicKey, communityAssociatedHedgeTokenAccount: PublicKey, overrideStartTime?: number): Promise<TransactionInstruction>;
@@ -0,0 +1,4 @@
1
+ import { Program, Provider } from '@project-serum/anchor';
2
+ import { PublicKey, Signer, TransactionInstruction } from '@solana/web3.js';
3
+ export declare function initHedgeFoundationTokens(program: Program, provider: Provider, payer: Signer): Promise<PublicKey>;
4
+ export declare function initHedgeFoundationTokensInstruction(program: Program, payerPublicKey: PublicKey): Promise<TransactionInstruction>;
package/lib/Constants.js CHANGED
@@ -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.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
15
  exports.HEDGE_PROGRAM_ID = 'h6GZ7ZMB6Jwmj3k4mQZY1bfXMGREEWH47uJHbKvE6nd';
@@ -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);
package/lib/index.js CHANGED
@@ -25,6 +25,7 @@ __exportStar(require("./instructions/redeemVault"), exports);
25
25
  __exportStar(require("./instructions/liquidateVault"), exports);
26
26
  __exportStar(require("./instructions/refreshOraclePrice"), exports);
27
27
  __exportStar(require("./instructions/initHedgeFoundation"), exports);
28
+ __exportStar(require("./instructions/initHedgeFoundationTokens"), exports);
28
29
  __exportStar(require("./instructions/setHalted"), exports);
29
30
  __exportStar(require("./HedgeDecimal"), exports);
30
31
  __exportStar(require("./Constants"), exports);
@@ -19,26 +19,28 @@ function closeLiquidationPoolPosition(program, provider, poolPosition, payer, ov
19
19
  return __awaiter(this, void 0, void 0, function* () {
20
20
  const usdhMintPublickey = yield (0, Constants_1.getUsdhMintPublicKey)();
21
21
  const payerUsdhAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, usdhMintPublickey, payer.publicKey);
22
- const poolState = yield (0, Constants_1.getLiquidationPoolStatePublicKey)();
22
+ const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
23
23
  const liquidationPositionAccount = yield program.account.liquidationPosition.fetch(poolPosition);
24
24
  const poolEra = liquidationPositionAccount.era;
25
- const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
26
- const poolUsdhAccount = yield (0, Constants_1.getLiquidationPoolUsdhAccountPublicKey)();
27
25
  const hedgeMint = yield (0, Constants_1.getHedgeMintPublicKey)();
28
26
  const payerAssociatedHedgeAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, hedgeMint, payer.publicKey);
29
27
  const communityAssociatedHedgeTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, hedgeMint, vaultSystemStatePublicKey, true);
30
- const transaction = new web3_js_1.Transaction().add(yield closeLiquidationPoolPositionInstruction(program, vaultSystemStatePublicKey, poolState, poolEra, poolPosition, poolUsdhAccount, payer.publicKey, payerUsdhAccount.address, hedgeMint, payerAssociatedHedgeAccount.address, communityAssociatedHedgeTokenAccount.address, overrideStartTime));
28
+ const transaction = new web3_js_1.Transaction().add(yield closeLiquidationPoolPositionInstruction(program, poolEra, poolPosition, payer.publicKey, payerUsdhAccount.address, payerAssociatedHedgeAccount.address, communityAssociatedHedgeTokenAccount.address, overrideStartTime));
31
29
  yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer], provider.opts).catch(Errors_1.parseAnchorErrors);
32
30
  return poolPosition;
33
31
  });
34
32
  }
35
33
  exports.closeLiquidationPoolPosition = closeLiquidationPoolPosition;
36
- function closeLiquidationPoolPositionInstruction(program, vaultSystemState, poolState, poolEra, poolPosition, poolUsdhAccount, payerPublicKey, payerUsdhAccount, hedgeMint, payerAssociatedHedgeAccount, communityAssociatedHedgeTokenAccount, overrideStartTime) {
34
+ function closeLiquidationPoolPositionInstruction(program, poolEra, poolPosition, payerPublicKey, payerUsdhAccount, payerAssociatedHedgeAccount, communityAssociatedHedgeTokenAccount, overrideStartTime) {
37
35
  return __awaiter(this, void 0, void 0, function* () {
36
+ const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
37
+ const hedgeMint = yield (0, Constants_1.getHedgeMintPublicKey)();
38
+ const poolUsdhAccount = yield (0, Constants_1.getLiquidationPoolUsdhAccountPublicKey)();
39
+ const poolState = yield (0, Constants_1.getLiquidationPoolStatePublicKey)();
38
40
  return program.instruction.closeLiquidationPoolPosition(new anchor_1.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), // override current time
39
41
  {
40
42
  accounts: {
41
- vaultSystemState: vaultSystemState,
43
+ vaultSystemState: vaultSystemStatePublicKey,
42
44
  poolState: poolState,
43
45
  poolEra: poolEra,
44
46
  poolPosition: poolPosition,
@@ -0,0 +1,48 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.initHedgeFoundationTokensInstruction = exports.initHedgeFoundationTokens = void 0;
13
+ const spl_token_1 = require("@solana/spl-token");
14
+ const web3_js_1 = require("@solana/web3.js");
15
+ const Errors_1 = require("../utils/Errors");
16
+ const Constants_1 = require("../Constants");
17
+ function initHedgeFoundationTokens(program, provider, payer) {
18
+ return __awaiter(this, void 0, void 0, function* () {
19
+ const poolEra = web3_js_1.Keypair.generate();
20
+ const transaction = new web3_js_1.Transaction().add(yield initHedgeFoundationTokensInstruction(program, payer.publicKey));
21
+ yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer], provider.opts).catch(Errors_1.parseAnchorErrors);
22
+ return payer.publicKey;
23
+ });
24
+ }
25
+ exports.initHedgeFoundationTokens = initHedgeFoundationTokens;
26
+ function initHedgeFoundationTokensInstruction(program, payerPublicKey) {
27
+ return __awaiter(this, void 0, void 0, function* () {
28
+ const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
29
+ const hedgeMintPublickey = yield (0, Constants_1.getHedgeMintPublicKey)();
30
+ const founderHedgeTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(payerPublicKey, hedgeMintPublickey);
31
+ const communityHedgeTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(vaultSystemStatePublicKey, hedgeMintPublickey);
32
+ return program.instruction.initHedgeFoundationTokens({
33
+ accounts: {
34
+ vaultSystemState: vaultSystemStatePublicKey,
35
+ founder: payerPublicKey,
36
+ hedgeMint: hedgeMintPublickey,
37
+ founderAssociatedHedgeTokenAccount: founderHedgeTokenAccount,
38
+ communityAssociatedHedgeTokenAccount: communityHedgeTokenAccount,
39
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY,
40
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
41
+ associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
42
+ systemProgram: web3_js_1.SystemProgram.programId
43
+ },
44
+ signers: []
45
+ });
46
+ });
47
+ }
48
+ exports.initHedgeFoundationTokensInstruction = initHedgeFoundationTokensInstruction;
@@ -20,7 +20,8 @@ class LiquidationPosition {
20
20
  this.sumSnapshotsEntry = poolPositionInfo.sumSnapshotsEntry.map((sum) => { return (0, HedgeDecimal_1.DecimalFromU128)(sum); });
21
21
  this.sumSnapshotsClosed = poolPositionInfo.sumSnapshotsClosed.map((sum) => { return (0, HedgeDecimal_1.DecimalFromU128)(sum); });
22
22
  this.hedgeRewardsSnapshot = (0, HedgeDecimal_1.DecimalFromU128)(poolPositionInfo.hedgeRewardsSnapshotAccum);
23
- this.open = poolPositionInfo.state.open !== undefined;
23
+ this.open = poolPositionInfo.state === 1;
24
+ console.log("poolPositionInfo.state, poolPositionInfo.state");
24
25
  }
25
26
  getUsdhAvailable(era) {
26
27
  return (era.product.div(this.productSnapshotEntry)).mul(new decimal_js_1.default(this.deposit));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hedge-web3",
3
- "version": "0.1.17",
3
+ "version": "0.1.20",
4
4
  "description": "Hedge Javascript Web3 API",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {
package/src/Constants.ts CHANGED
@@ -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
package/src/index.ts CHANGED
@@ -14,6 +14,7 @@ export * from './instructions/redeemVault'
14
14
  export * from './instructions/liquidateVault'
15
15
  export * from './instructions/refreshOraclePrice'
16
16
  export * from './instructions/initHedgeFoundation'
17
+ export * from './instructions/initHedgeFoundationTokens'
17
18
  export * from './instructions/setHalted'
18
19
 
19
20
  export * from './HedgeDecimal'
@@ -13,13 +13,10 @@ export async function closeLiquidationPoolPosition (
13
13
  ): Promise<PublicKey> {
14
14
  const usdhMintPublickey = await getUsdhMintPublicKey()
15
15
  const payerUsdhAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, usdhMintPublickey, payer.publicKey)
16
-
17
- const poolState = await getLiquidationPoolStatePublicKey()
16
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
18
17
  const liquidationPositionAccount = await program.account.liquidationPosition.fetch(poolPosition)
19
18
  const poolEra = liquidationPositionAccount.era
20
19
 
21
- const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
22
- const poolUsdhAccount = await getLiquidationPoolUsdhAccountPublicKey()
23
20
  const hedgeMint = await getHedgeMintPublicKey()
24
21
  const payerAssociatedHedgeAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, hedgeMint, payer.publicKey)
25
22
  const communityAssociatedHedgeTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, hedgeMint, vaultSystemStatePublicKey, true)
@@ -27,14 +24,10 @@ export async function closeLiquidationPoolPosition (
27
24
  const transaction = new Transaction().add(
28
25
  await closeLiquidationPoolPositionInstruction(
29
26
  program,
30
- vaultSystemStatePublicKey,
31
- poolState,
32
27
  poolEra,
33
28
  poolPosition,
34
- poolUsdhAccount,
35
29
  payer.publicKey,
36
30
  payerUsdhAccount.address,
37
- hedgeMint,
38
31
  payerAssociatedHedgeAccount.address,
39
32
  communityAssociatedHedgeTokenAccount.address,
40
33
  overrideStartTime
@@ -46,23 +39,25 @@ export async function closeLiquidationPoolPosition (
46
39
 
47
40
  export async function closeLiquidationPoolPositionInstruction (
48
41
  program: Program,
49
- vaultSystemState: PublicKey,
50
- poolState: PublicKey,
51
42
  poolEra: PublicKey,
52
43
  poolPosition: PublicKey,
53
- poolUsdhAccount: PublicKey,
54
44
  payerPublicKey: PublicKey,
55
45
  payerUsdhAccount: PublicKey,
56
- hedgeMint: PublicKey,
57
46
  payerAssociatedHedgeAccount: PublicKey,
58
47
  communityAssociatedHedgeTokenAccount: PublicKey,
59
48
  overrideStartTime?: number
60
49
  ): Promise<TransactionInstruction> {
50
+
51
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
52
+ const hedgeMint = await getHedgeMintPublicKey()
53
+ const poolUsdhAccount = await getLiquidationPoolUsdhAccountPublicKey()
54
+ const poolState = await getLiquidationPoolStatePublicKey()
55
+
61
56
  return program.instruction.closeLiquidationPoolPosition(
62
57
  new BN(overrideStartTime ?? Date.now() / 1000), // override current time
63
58
  {
64
59
  accounts: {
65
- vaultSystemState: vaultSystemState,
60
+ vaultSystemState: vaultSystemStatePublicKey,
66
61
  poolState: poolState,
67
62
  poolEra: poolEra,
68
63
  poolPosition: poolPosition,
@@ -0,0 +1,55 @@
1
+ import { Program, Provider } from '@project-serum/anchor'
2
+ import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
+ import { Keypair, PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
4
+ import { parseAnchorErrors } from '../utils/Errors'
5
+ import { findAssociatedTokenAddress, getHedgeMintPublicKey, getLiquidationPoolStatePublicKey, getLiquidationPoolUsdhAccountPublicKey, getUsdhMintPublicKey, getVaultSystemStatePublicKey } from '../Constants'
6
+
7
+ export async function initHedgeFoundationTokens(
8
+ program: Program,
9
+ provider: Provider,
10
+ payer: Signer
11
+ ): Promise<PublicKey> {
12
+
13
+ const poolEra = Keypair.generate()
14
+ const transaction = new Transaction().add(
15
+ await initHedgeFoundationTokensInstruction(
16
+ program,
17
+ payer.publicKey,
18
+ )
19
+ )
20
+
21
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors)
22
+ return payer.publicKey
23
+ }
24
+
25
+ export async function initHedgeFoundationTokensInstruction (
26
+ program: Program,
27
+ payerPublicKey: PublicKey,
28
+ ): Promise<TransactionInstruction> {
29
+ const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
30
+ const hedgeMintPublickey = await getHedgeMintPublicKey()
31
+ const founderHedgeTokenAccount = await findAssociatedTokenAddress(
32
+ payerPublicKey,
33
+ hedgeMintPublickey
34
+ )
35
+ const communityHedgeTokenAccount = await findAssociatedTokenAddress(
36
+ vaultSystemStatePublicKey,
37
+ hedgeMintPublickey
38
+ )
39
+
40
+ return program.instruction.initHedgeFoundationTokens(
41
+ {
42
+ accounts: {
43
+ vaultSystemState: vaultSystemStatePublicKey,
44
+ founder: payerPublicKey,
45
+ hedgeMint: hedgeMintPublickey,
46
+ founderAssociatedHedgeTokenAccount: founderHedgeTokenAccount,
47
+ communityAssociatedHedgeTokenAccount: communityHedgeTokenAccount,
48
+ rent: SYSVAR_RENT_PUBKEY,
49
+ tokenProgram: TOKEN_PROGRAM_ID,
50
+ associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
51
+ systemProgram: SystemProgram.programId
52
+ },
53
+ signers: []
54
+ })
55
+ }
@@ -36,7 +36,8 @@ export class LiquidationPosition {
36
36
  this.sumSnapshotsEntry = poolPositionInfo.sumSnapshotsEntry.map((sum: any) => { return DecimalFromU128(sum) })
37
37
  this.sumSnapshotsClosed = poolPositionInfo.sumSnapshotsClosed.map((sum: any) => { return DecimalFromU128(sum) })
38
38
  this.hedgeRewardsSnapshot = DecimalFromU128(poolPositionInfo.hedgeRewardsSnapshotAccum)
39
- this.open = poolPositionInfo.state.open !== undefined
39
+ this.open = poolPositionInfo.state === 1
40
+ console.log("poolPositionInfo.state, poolPositionInfo.state")
40
41
  }
41
42
 
42
43
  public getUsdhAvailable (era: LiquidationPoolEra): Decimal {