hedge-web3 0.1.19 → 0.1.23

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. package/declarations/Constants.d.ts +1 -1
  2. package/declarations/index.d.ts +1 -0
  3. package/declarations/instructions/claimStakingPoolPosition.d.ts +4 -0
  4. package/declarations/instructions/closeLiquidationPoolPosition.d.ts +1 -1
  5. package/declarations/state/StakingPool.d.ts +1 -1
  6. package/declarations/state/VaultHistoryEvent.d.ts +2 -2
  7. package/lib/Constants.js +1 -1
  8. package/lib/index.js +1 -0
  9. package/lib/instructions/claimStakingPoolPosition.js +55 -0
  10. package/lib/instructions/closeLiquidationPoolPosition.js +11 -7
  11. package/lib/instructions/createStakingPool.js +2 -1
  12. package/lib/instructions/createVault.js +1 -1
  13. package/lib/instructions/depositLiquidationPool.js +1 -1
  14. package/lib/instructions/depositStakingPool.js +1 -1
  15. package/lib/instructions/refreshOraclePrice.js +1 -1
  16. package/lib/instructions/withdrawStakingPool.js +1 -1
  17. package/lib/state/LiquidationPosition.js +2 -1
  18. package/lib/state/StakingPool.js +1 -1
  19. package/lib/state/VaultHistoryEvent.js +2 -2
  20. package/package.json +2 -1
  21. package/src/Constants.ts +1 -1
  22. package/src/index.ts +1 -0
  23. package/src/instructions/claimStakingPoolPosition.ts +76 -0
  24. package/src/instructions/closeLiquidationPoolPosition.ts +11 -14
  25. package/src/instructions/createStakingPool.ts +2 -1
  26. package/src/instructions/createVault.ts +2 -1
  27. package/src/instructions/depositLiquidationPool.ts +1 -1
  28. package/src/instructions/depositStakingPool.ts +1 -1
  29. package/src/instructions/refreshOraclePrice.ts +1 -1
  30. package/src/instructions/withdrawStakingPool.ts +1 -1
  31. package/src/state/LiquidationPosition.ts +2 -1
  32. package/src/state/StakingPool.ts +2 -2
  33. package/src/state/VaultHistoryEvent.ts +4 -4
@@ -1,5 +1,5 @@
1
1
  import { PublicKey } from '@solana/web3.js';
2
- export declare const HEDGE_PROGRAM_ID = "h6GZ7ZMB6Jwmj3k4mQZY1bfXMGREEWH47uJHbKvE6nd";
2
+ export declare const HEDGE_PROGRAM_ID = "h14ydGHkAt6vpBaxX461fgxGYsZjaf2K8Fqwm7Cn3ik";
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;
@@ -1,6 +1,7 @@
1
1
  export * from './instructions/createStakingPool';
2
2
  export * from './instructions/depositStakingPool';
3
3
  export * from './instructions/withdrawStakingPool';
4
+ export * from './instructions/claimStakingPoolPosition';
4
5
  export * from './instructions/depositLiquidationPool';
5
6
  export * from './instructions/closeLiquidationPoolPosition';
6
7
  export * from './instructions/claimLiquidationPoolPosition';
@@ -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 claimStakingPoolPosition(program: Program, provider: Provider, poolPosition: PublicKey, payer: Signer, collateralType: string): Promise<PublicKey>;
4
+ export declare function claimStakingPoolPositionInstruction(program: Program, feePool: PublicKey, feePoolAssociatedTokenAccount: PublicKey, stakedTokenMint: PublicKey, vaultTypeAccount: PublicKey, collateralMint: PublicKey, poolPosition: PublicKey, payer: PublicKey, payerAssociatedTokenAccount: PublicKey): Promise<TransactionInstruction>;
@@ -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>;
@@ -10,6 +10,6 @@ export declare class StakingPool {
10
10
  totalHedgeReward: number;
11
11
  hedgeRewardAccumulator: Decimal;
12
12
  usdhFeeAccumulator: Decimal;
13
- solFeeAccumulator: Decimal;
13
+ collateralFeeAccumulator: [Decimal];
14
14
  constructor(poolInfo: any, publicKey: PublicKey);
15
15
  }
@@ -7,8 +7,8 @@ export declare class VaultHistoryEvent {
7
7
  usdSolPrice: Decimal;
8
8
  usdhDebtBefore: number;
9
9
  usdhDebtAfter: number;
10
- solBalanceBefore: number;
11
- solBalanceAfter: number;
10
+ collateralBalanceBefore: number;
11
+ collateralBalanceAfter: number;
12
12
  minCollateralRatioBefore: Decimal;
13
13
  minCollateralRatioAfter: Decimal;
14
14
  vaultStateBefore: PublicKey;
package/lib/Constants.js CHANGED
@@ -12,7 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
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 = 'h6GZ7ZMB6Jwmj3k4mQZY1bfXMGREEWH47uJHbKvE6nd';
15
+ exports.HEDGE_PROGRAM_ID = 'h14ydGHkAt6vpBaxX461fgxGYsZjaf2K8Fqwm7Cn3ik';
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);
package/lib/index.js CHANGED
@@ -13,6 +13,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
13
13
  __exportStar(require("./instructions/createStakingPool"), exports);
14
14
  __exportStar(require("./instructions/depositStakingPool"), exports);
15
15
  __exportStar(require("./instructions/withdrawStakingPool"), exports);
16
+ __exportStar(require("./instructions/claimStakingPoolPosition"), exports);
16
17
  __exportStar(require("./instructions/depositLiquidationPool"), exports);
17
18
  __exportStar(require("./instructions/closeLiquidationPoolPosition"), exports);
18
19
  __exportStar(require("./instructions/claimLiquidationPoolPosition"), exports);
@@ -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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.claimStakingPoolPositionInstruction = exports.claimStakingPoolPosition = 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 claimStakingPoolPosition(program, provider, poolPosition, payer, collateralType) {
18
+ return __awaiter(this, void 0, void 0, function* () {
19
+ const vaultTypeAccount = yield (0, Constants_1.getVaultTypeAccountPublicKey)(collateralType);
20
+ const vaultTypeAccountInfo = yield program.account.vaultType.fetch(vaultTypeAccount);
21
+ const collateralMint = vaultTypeAccountInfo.collateralMint;
22
+ const stakedTokenMint = yield (0, Constants_1.getHedgeMintPublicKey)();
23
+ const [feePool] = yield (0, Constants_1.getPoolPublicKeyForMint)(stakedTokenMint);
24
+ const feePoolAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, collateralMint, feePool, true);
25
+ const payerAssociatedTokenAccount = yield (0, spl_token_1.getOrCreateAssociatedTokenAccount)(provider.connection, payer, collateralMint, payer.publicKey);
26
+ const transaction = new web3_js_1.Transaction().add(yield claimStakingPoolPositionInstruction(program, feePool, feePoolAssociatedTokenAccount.address, stakedTokenMint, vaultTypeAccount, collateralMint, poolPosition, payer.publicKey, payerAssociatedTokenAccount.address));
27
+ yield (0, web3_js_1.sendAndConfirmTransaction)(provider.connection, transaction, [payer], provider.opts).catch(Errors_1.parseAnchorErrors);
28
+ return payerAssociatedTokenAccount.address;
29
+ });
30
+ }
31
+ exports.claimStakingPoolPosition = claimStakingPoolPosition;
32
+ function claimStakingPoolPositionInstruction(program, feePool, feePoolAssociatedTokenAccount, stakedTokenMint, vaultTypeAccount, collateralMint, poolPosition, payer, payerAssociatedTokenAccount) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ const vaultSystemState = yield (0, Constants_1.getVaultSystemStatePublicKey)();
35
+ return program.instruction.claimStakingPoolPosition({
36
+ accounts: {
37
+ vaultSystemState: vaultSystemState,
38
+ feePool: feePool,
39
+ stakedTokenMint: stakedTokenMint,
40
+ feePoolAssociatedTokenAccount: feePoolAssociatedTokenAccount,
41
+ vaultTypeAccount: vaultTypeAccount,
42
+ collateralMint: collateralMint,
43
+ poolPosition: poolPosition,
44
+ payer: payer,
45
+ payerAssociatedTokenAccount: payerAssociatedTokenAccount,
46
+ associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
47
+ systemProgram: web3_js_1.SystemProgram.programId,
48
+ tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
49
+ rent: web3_js_1.SYSVAR_RENT_PUBKEY
50
+ },
51
+ signers: []
52
+ });
53
+ });
54
+ }
55
+ exports.claimStakingPoolPositionInstruction = claimStakingPoolPositionInstruction;
@@ -19,26 +19,29 @@ 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* () {
38
- return program.instruction.closeLiquidationPoolPosition(new anchor_1.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), // override current time
36
+ const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
37
+ const usdhMint = yield (0, Constants_1.getUsdhMintPublicKey)();
38
+ const hedgeMint = yield (0, Constants_1.getHedgeMintPublicKey)();
39
+ const poolUsdhAccount = yield (0, Constants_1.getLiquidationPoolUsdhAccountPublicKey)();
40
+ const poolState = yield (0, Constants_1.getLiquidationPoolStatePublicKey)();
41
+ return program.instruction.closeLiquidationPoolPosition(new anchor_1.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Math.floor(Date.now() / 1000)), // override current time
39
42
  {
40
43
  accounts: {
41
- vaultSystemState: vaultSystemState,
44
+ vaultSystemState: vaultSystemStatePublicKey,
42
45
  poolState: poolState,
43
46
  poolEra: poolEra,
44
47
  poolPosition: poolPosition,
@@ -46,6 +49,7 @@ function closeLiquidationPoolPositionInstruction(program, vaultSystemState, pool
46
49
  payer: payerPublicKey,
47
50
  ownerUsdhAccount: payerUsdhAccount,
48
51
  hedgeMint: hedgeMint,
52
+ usdhMint: usdhMint,
49
53
  payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
50
54
  communityAssociatedHedgeTokenAccount: communityAssociatedHedgeTokenAccount,
51
55
  associatedTokenProgram: spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID,
@@ -26,12 +26,13 @@ function createStakingPool(program, provider, payer, mintPublicKey, hedgeTokensT
26
26
  exports.createStakingPool = createStakingPool;
27
27
  function createStakingPoolInstruction(program, payerPublicKey, mintPublicKey, hedgeTokensToBeMinted, overrideStartTime) {
28
28
  return __awaiter(this, void 0, void 0, function* () {
29
+ console.log("createStakingPoolInstruction program ID", program.programId.toString());
29
30
  const vaultSystemStatePublicKey = yield (0, Constants_1.getVaultSystemStatePublicKey)();
30
31
  const usdhMintPublickey = yield (0, Constants_1.getUsdhMintPublicKey)();
31
32
  const [poolPublickey, poolBump, poolSeedPhrase] = yield (0, Constants_1.getPoolPublicKeyForMint)(mintPublicKey);
32
33
  const poolAssociatedStakedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(poolPublickey, mintPublicKey);
33
34
  const poolAssociatedUsdhTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(poolPublickey, usdhMintPublickey);
34
- return program.instruction.createStakingPool(poolBump, poolSeedPhrase, new anchor_1.BN(hedgeTokensToBeMinted), new anchor_1.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), {
35
+ return program.instruction.createStakingPool(poolBump, poolSeedPhrase, new anchor_1.BN(hedgeTokensToBeMinted), new anchor_1.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Math.floor(Date.now() / 1000)), {
35
36
  accounts: {
36
37
  signer: payerPublicKey,
37
38
  vaultSystemState: vaultSystemStatePublicKey,
@@ -114,8 +114,8 @@ function buildCreateVaultTransaction(program, collateralType, depositAmount, ove
114
114
  destination: payerPublicKey,
115
115
  owner: payerPublicKey,
116
116
  }));
117
+ transaction.partialSign(wrappedSolAccount);
117
118
  }
118
- transaction.partialSign(wrappedSolAccount);
119
119
  transaction.partialSign(history);
120
120
  console.log("transaction", transaction);
121
121
  return [transaction, signers, newVaultPublicKey];
@@ -33,7 +33,7 @@ function depositLiquidationPoolInstruction(program, payerPublicKey, payerUsdhAcc
33
33
  const poolUSDHAccount = yield (0, Constants_1.getLiquidationPoolUsdhAccountPublicKey)();
34
34
  const usdhMint = yield (0, Constants_1.getUsdhMintPublicKey)();
35
35
  const vaultSystemState = yield (0, Constants_1.getVaultSystemStatePublicKey)();
36
- return program.instruction.depositLiquidationPool(new anchor_1.BN(depositAmount), new anchor_1.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), // override current time
36
+ return program.instruction.depositLiquidationPool(new anchor_1.BN(depositAmount), new anchor_1.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Math.floor(Date.now() / 1000)), // override current time
37
37
  {
38
38
  accounts: {
39
39
  vaultSystemState: vaultSystemState,
@@ -30,7 +30,7 @@ function depositStakingPoolInstruction(program, payerPublicKey, poolPositionPubl
30
30
  const poolAssociatedStakedTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(poolPublickey, stakedTokenMintPublicKey);
31
31
  const payersArbitraryTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(payerPublicKey, stakedTokenMintPublicKey);
32
32
  const vaultSystemState = yield (0, Constants_1.getVaultSystemStatePublicKey)();
33
- return program.instruction.depositStakingPool(new anchor_1.BN(depositAmount), new anchor_1.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), // override current time
33
+ return program.instruction.depositStakingPool(new anchor_1.BN(depositAmount), new anchor_1.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Math.floor(Date.now() / 1000)), // override current time
34
34
  {
35
35
  accounts: {
36
36
  payer: payerPublicKey,
@@ -26,7 +26,7 @@ function refreshOraclePriceInstruction(program, collateralType, network, overrid
26
26
  const [oracleInfoAccount] = yield web3_js_1.PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('Oracle')], Constants_1.HEDGE_PROGRAM_PUBLICKEY);
27
27
  const [vaultTypeAccount] = yield web3_js_1.PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('State')], Constants_1.HEDGE_PROGRAM_PUBLICKEY);
28
28
  return program.instruction.refreshOraclePrice(new anchor_1.BN(overridePrice !== null && overridePrice !== void 0 ? overridePrice : web3_js_1.LAMPORTS_PER_SOL * 150), // override usd/sol price
29
- new anchor_1.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000)), // override override time
29
+ new anchor_1.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000) - 1), // override override time
30
30
  {
31
31
  accounts: {
32
32
  oracleInfoAccount: oracleInfoAccount,
@@ -36,7 +36,7 @@ function withdrawStakingPoolInstruction(program, payerPublicKey, poolPositionPub
36
36
  const payerAssociatedHedgeAccount = yield (0, Constants_1.findAssociatedTokenAddress)(payerPublicKey, hedgeMintPublickey);
37
37
  const payerAssociatedUsdhAccount = yield (0, Constants_1.findAssociatedTokenAddress)(payerPublicKey, usdhMintPublickey);
38
38
  const communityHedgeTokenAccount = yield (0, Constants_1.findAssociatedTokenAddress)(vaultSystemStatePublicKey, hedgeMintPublickey);
39
- return program.instruction.withdrawStakingPool(new anchor_1.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), // override current time
39
+ return program.instruction.withdrawStakingPool(new anchor_1.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Math.floor(Date.now() / 1000)), // override current time
40
40
  {
41
41
  accounts: {
42
42
  payer: payerPublicKey,
@@ -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));
@@ -15,7 +15,7 @@ class StakingPool {
15
15
  this.totalHedgeReward = poolInfo.totalHedgeReward.toNumber();
16
16
  this.hedgeRewardAccumulator = (0, HedgeDecimal_1.DecimalFromU128)(poolInfo.hedgeRewardAccumulator);
17
17
  this.usdhFeeAccumulator = (0, HedgeDecimal_1.DecimalFromU128)(poolInfo.usdhFeeAccumulator);
18
- this.solFeeAccumulator = (0, HedgeDecimal_1.DecimalFromU128)(poolInfo.solFeeAccumulator);
18
+ this.collateralFeeAccumulator = poolInfo.collateralFeeAccumulator.map((sum) => { return (0, HedgeDecimal_1.DecimalFromU128)(sum); });
19
19
  // this.currentRewardsPerDay = DecimalFromU128(poolInfo.currentRewardsPerDay)
20
20
  }
21
21
  }
@@ -10,8 +10,8 @@ class VaultHistoryEvent {
10
10
  this.usdSolPrice = (0, HedgeDecimal_1.DecimalFromU128)(account.usdSolPrice.toString());
11
11
  this.usdhDebtBefore = account.usdhDebtBefore.toNumber();
12
12
  this.usdhDebtAfter = account.usdhDebtAfter.toNumber();
13
- this.solBalanceBefore = account.solBalanceBefore.toNumber();
14
- this.solBalanceAfter = account.solBalanceAfter.toNumber();
13
+ this.collateralBalanceBefore = account.collateralBalanceBefore.toNumber();
14
+ this.collateralBalanceAfter = account.collateralBalanceAfter.toNumber();
15
15
  this.minCollateralRatioBefore = (0, HedgeDecimal_1.DecimalFromU128)(account.minCollateralRatioBefore);
16
16
  this.minCollateralRatioAfter = (0, HedgeDecimal_1.DecimalFromU128)(account.minCollateralRatioAfter);
17
17
  this.vaultStateBefore = account.vaultStateBefore;
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "hedge-web3",
3
- "version": "0.1.19",
3
+ "version": "0.1.23",
4
4
  "description": "Hedge Javascript Web3 API",
5
5
  "main": "lib/index.js",
6
+ "types": "declarations/index.d.ts",
6
7
  "scripts": {
7
8
  "build": "tsc",
8
9
  "dev": "tsc --watch",
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 = 'h6GZ7ZMB6Jwmj3k4mQZY1bfXMGREEWH47uJHbKvE6nd'
4
+ export const HEDGE_PROGRAM_ID = 'h14ydGHkAt6vpBaxX461fgxGYsZjaf2K8Fqwm7Cn3ik'
5
5
  export const HEDGE_PROGRAM_PUBLICKEY = new PublicKey(HEDGE_PROGRAM_ID)
6
6
 
7
7
  export const CHAINLINK_SOL_USD_ID = 'FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf'
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  export * from './instructions/createStakingPool'
3
3
  export * from './instructions/depositStakingPool'
4
4
  export * from './instructions/withdrawStakingPool'
5
+ export * from './instructions/claimStakingPoolPosition'
5
6
  export * from './instructions/depositLiquidationPool'
6
7
  export * from './instructions/closeLiquidationPoolPosition'
7
8
  export * from './instructions/claimLiquidationPoolPosition'
@@ -0,0 +1,76 @@
1
+ import { Program, Provider } from '@project-serum/anchor'
2
+ import { ASSOCIATED_TOKEN_PROGRAM_ID, getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from '@solana/spl-token'
3
+ import { PublicKey, sendAndConfirmTransaction, Signer, SystemProgram, SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction } from '@solana/web3.js'
4
+ import { parseAnchorErrors } from '../utils/Errors'
5
+ import { getHedgeMintPublicKey, findAssociatedTokenAddress, getVaultTypeAccountPublicKey, getPoolPublicKeyForMint, getVaultSystemStatePublicKey } from '../Constants'
6
+
7
+ export async function claimStakingPoolPosition(
8
+ program: Program,
9
+ provider: Provider,
10
+ poolPosition: PublicKey,
11
+ payer: Signer,
12
+ collateralType: string,
13
+ ): Promise<PublicKey> {
14
+ const vaultTypeAccount = await getVaultTypeAccountPublicKey(collateralType)
15
+ const vaultTypeAccountInfo = await program.account.vaultType.fetch(vaultTypeAccount)
16
+ const collateralMint = vaultTypeAccountInfo.collateralMint
17
+ const stakedTokenMint = await getHedgeMintPublicKey()
18
+ const [feePool] = await getPoolPublicKeyForMint(stakedTokenMint)
19
+ const feePoolAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(provider.connection, payer, collateralMint, feePool, true)
20
+ const payerAssociatedTokenAccount = await getOrCreateAssociatedTokenAccount(
21
+ provider.connection,
22
+ payer,
23
+ collateralMint,
24
+ payer.publicKey
25
+ )
26
+
27
+ const transaction = new Transaction().add(
28
+ await claimStakingPoolPositionInstruction(
29
+ program,
30
+ feePool,
31
+ feePoolAssociatedTokenAccount.address,
32
+ stakedTokenMint,
33
+ vaultTypeAccount,
34
+ collateralMint,
35
+ poolPosition,
36
+ payer.publicKey,
37
+ payerAssociatedTokenAccount.address
38
+ )
39
+ )
40
+ await sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors)
41
+ return payerAssociatedTokenAccount.address
42
+ }
43
+
44
+ export async function claimStakingPoolPositionInstruction(
45
+ program: Program,
46
+ feePool: PublicKey,
47
+ feePoolAssociatedTokenAccount: PublicKey,
48
+ stakedTokenMint: PublicKey,
49
+ vaultTypeAccount: PublicKey,
50
+ collateralMint: PublicKey,
51
+ poolPosition: PublicKey,
52
+ payer: PublicKey,
53
+ payerAssociatedTokenAccount: PublicKey
54
+ ): Promise<TransactionInstruction> {
55
+ const vaultSystemState = await getVaultSystemStatePublicKey()
56
+
57
+ return program.instruction.claimStakingPoolPosition(
58
+ {
59
+ accounts: {
60
+ vaultSystemState: vaultSystemState,
61
+ feePool: feePool,
62
+ stakedTokenMint: stakedTokenMint,
63
+ feePoolAssociatedTokenAccount: feePoolAssociatedTokenAccount,
64
+ vaultTypeAccount: vaultTypeAccount,
65
+ collateralMint: collateralMint,
66
+ poolPosition: poolPosition,
67
+ payer: payer,
68
+ payerAssociatedTokenAccount: payerAssociatedTokenAccount,
69
+ associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
70
+ systemProgram: SystemProgram.programId,
71
+ tokenProgram: TOKEN_PROGRAM_ID,
72
+ rent: SYSVAR_RENT_PUBKEY
73
+ },
74
+ signers: []
75
+ })
76
+ }
@@ -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,26 @@ 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 usdhMint = await getUsdhMintPublicKey()
53
+ const hedgeMint = await getHedgeMintPublicKey()
54
+ const poolUsdhAccount = await getLiquidationPoolUsdhAccountPublicKey()
55
+ const poolState = await getLiquidationPoolStatePublicKey()
56
+
61
57
  return program.instruction.closeLiquidationPoolPosition(
62
- new BN(overrideStartTime ?? Date.now() / 1000), // override current time
58
+ new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)), // override current time
63
59
  {
64
60
  accounts: {
65
- vaultSystemState: vaultSystemState,
61
+ vaultSystemState: vaultSystemStatePublicKey,
66
62
  poolState: poolState,
67
63
  poolEra: poolEra,
68
64
  poolPosition: poolPosition,
@@ -70,6 +66,7 @@ export async function closeLiquidationPoolPositionInstruction (
70
66
  payer: payerPublicKey,
71
67
  ownerUsdhAccount: payerUsdhAccount,
72
68
  hedgeMint: hedgeMint,
69
+ usdhMint: usdhMint,
73
70
  payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
74
71
  communityAssociatedHedgeTokenAccount: communityAssociatedHedgeTokenAccount,
75
72
  associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
@@ -33,6 +33,7 @@ export async function createStakingPoolInstruction (
33
33
  hedgeTokensToBeMinted: number,
34
34
  overrideStartTime?: number
35
35
  ): Promise<TransactionInstruction> {
36
+ console.log("createStakingPoolInstruction program ID", program.programId.toString())
36
37
  const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey()
37
38
  const usdhMintPublickey = await getUsdhMintPublicKey()
38
39
  const [poolPublickey, poolBump, poolSeedPhrase] = await getPoolPublicKeyForMint(mintPublicKey)
@@ -44,7 +45,7 @@ export async function createStakingPoolInstruction (
44
45
  poolBump,
45
46
  poolSeedPhrase,
46
47
  new BN(hedgeTokensToBeMinted),
47
- new BN(overrideStartTime ?? Date.now() / 1000),
48
+ new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)),
48
49
  {
49
50
  accounts: {
50
51
  signer: payerPublicKey,
@@ -241,8 +241,9 @@ export async function buildCreateVaultTransaction(
241
241
  owner: payerPublicKey,
242
242
  })
243
243
  );
244
+ transaction.partialSign(wrappedSolAccount)
244
245
  }
245
- transaction.partialSign(wrappedSolAccount)
246
+
246
247
  transaction.partialSign(history)
247
248
  console.log("transaction", transaction)
248
249
 
@@ -47,7 +47,7 @@ export async function depositLiquidationPoolInstruction (
47
47
 
48
48
  return program.instruction.depositLiquidationPool(
49
49
  new BN(depositAmount),
50
- new BN(overrideStartTime ?? Date.now() / 1000), // override current time
50
+ new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)), // override current time
51
51
  {
52
52
  accounts: {
53
53
  vaultSystemState: vaultSystemState,
@@ -42,7 +42,7 @@ export async function depositStakingPoolInstruction (
42
42
 
43
43
  return program.instruction.depositStakingPool(
44
44
  new BN(depositAmount),
45
- new BN(overrideStartTime ?? Date.now() / 1000), // override current time
45
+ new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)), // override current time
46
46
  {
47
47
  accounts: {
48
48
  payer: payerPublicKey,
@@ -36,7 +36,7 @@ export async function refreshOraclePriceInstruction (
36
36
 
37
37
  return program.instruction.refreshOraclePrice(
38
38
  new BN(overridePrice ?? LAMPORTS_PER_SOL * 150), // override usd/sol price
39
- new BN(overrideTime ?? Math.floor(Date.now() / 1000)), // override override time
39
+ new BN(overrideTime ?? Math.floor(Date.now() / 1000)-1), // override override time
40
40
  {
41
41
  accounts: {
42
42
  oracleInfoAccount: oracleInfoAccount,
@@ -45,7 +45,7 @@ export async function withdrawStakingPoolInstruction (
45
45
  const communityHedgeTokenAccount = await findAssociatedTokenAddress(vaultSystemStatePublicKey, hedgeMintPublickey)
46
46
 
47
47
  return program.instruction.withdrawStakingPool(
48
- new BN(overrideStartTime ?? Date.now() / 1000), // override current time
48
+ new BN(overrideStartTime ?? Math.floor(Date.now() / 1000)), // override current time
49
49
  {
50
50
  accounts: {
51
51
  payer: payerPublicKey,
@@ -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 {
@@ -13,7 +13,7 @@ export class StakingPool {
13
13
 
14
14
  hedgeRewardAccumulator: Decimal
15
15
  usdhFeeAccumulator: Decimal
16
- solFeeAccumulator: Decimal
16
+ collateralFeeAccumulator: [Decimal]
17
17
 
18
18
  constructor (public poolInfo: any, publicKey: PublicKey) {
19
19
  this.publicKey = publicKey
@@ -27,7 +27,7 @@ export class StakingPool {
27
27
 
28
28
  this.hedgeRewardAccumulator = DecimalFromU128(poolInfo.hedgeRewardAccumulator)
29
29
  this.usdhFeeAccumulator = DecimalFromU128(poolInfo.usdhFeeAccumulator)
30
- this.solFeeAccumulator = DecimalFromU128(poolInfo.solFeeAccumulator)
30
+ this.collateralFeeAccumulator = poolInfo.collateralFeeAccumulator.map((sum: any) => { return DecimalFromU128(sum) })
31
31
  // this.currentRewardsPerDay = DecimalFromU128(poolInfo.currentRewardsPerDay)
32
32
  }
33
33
 
@@ -9,8 +9,8 @@ export class VaultHistoryEvent {
9
9
  usdSolPrice: Decimal
10
10
  usdhDebtBefore: number
11
11
  usdhDebtAfter: number
12
- solBalanceBefore: number
13
- solBalanceAfter: number
12
+ collateralBalanceBefore: number
13
+ collateralBalanceAfter: number
14
14
  minCollateralRatioBefore: Decimal
15
15
  minCollateralRatioAfter: Decimal
16
16
  vaultStateBefore: PublicKey
@@ -26,8 +26,8 @@ export class VaultHistoryEvent {
26
26
 
27
27
  this.usdhDebtBefore = account.usdhDebtBefore.toNumber()
28
28
  this.usdhDebtAfter = account.usdhDebtAfter.toNumber()
29
- this.solBalanceBefore = account.solBalanceBefore.toNumber()
30
- this.solBalanceAfter = account.solBalanceAfter.toNumber()
29
+ this.collateralBalanceBefore = account.collateralBalanceBefore.toNumber()
30
+ this.collateralBalanceAfter = account.collateralBalanceAfter.toNumber()
31
31
  this.minCollateralRatioBefore = DecimalFromU128(account.minCollateralRatioBefore)
32
32
  this.minCollateralRatioAfter = DecimalFromU128(account.minCollateralRatioAfter)
33
33
  this.vaultStateBefore = account.vaultStateBefore