hedge-web3 0.1.13 → 0.1.14
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/lib/index.js +373 -147
- package/lib/index.js.map +1 -1
- package/lib/types/src/Constants.d.ts +7 -6
- package/lib/types/src/Constants.d.ts.map +1 -1
- package/lib/types/src/index.d.ts +3 -0
- package/lib/types/src/index.d.ts.map +1 -1
- package/lib/types/src/instructions/claimLiquidationPoolPosition.d.ts +5 -0
- package/lib/types/src/instructions/claimLiquidationPoolPosition.d.ts.map +1 -0
- package/lib/types/src/instructions/closeLiquidationPoolPosition.d.ts +5 -0
- package/lib/types/src/instructions/closeLiquidationPoolPosition.d.ts.map +1 -0
- package/lib/types/src/instructions/createVault.d.ts +2 -2
- package/lib/types/src/instructions/createVault.d.ts.map +1 -1
- package/lib/types/src/instructions/depositLiquidationPool.d.ts +5 -0
- package/lib/types/src/instructions/depositLiquidationPool.d.ts.map +1 -0
- package/lib/types/src/instructions/depositVault.d.ts +1 -1
- package/lib/types/src/instructions/depositVault.d.ts.map +1 -1
- package/lib/types/src/instructions/liquidateVault.d.ts +2 -2
- package/lib/types/src/instructions/liquidateVault.d.ts.map +1 -1
- package/lib/types/src/instructions/loanVault.d.ts +1 -1
- package/lib/types/src/instructions/loanVault.d.ts.map +1 -1
- package/lib/types/src/instructions/redeemVault.d.ts +2 -2
- package/lib/types/src/instructions/redeemVault.d.ts.map +1 -1
- package/lib/types/src/instructions/refreshOraclePrice.d.ts +2 -2
- package/lib/types/src/instructions/refreshOraclePrice.d.ts.map +1 -1
- package/lib/types/src/instructions/repayVault.d.ts +1 -1
- package/lib/types/src/instructions/repayVault.d.ts.map +1 -1
- package/lib/types/src/instructions/withdrawVault.d.ts +1 -1
- package/lib/types/src/instructions/withdrawVault.d.ts.map +1 -1
- package/lib/types/src/state/LiquidationPoolEra.d.ts +1 -1
- package/lib/types/src/state/LiquidationPoolEra.d.ts.map +1 -1
- package/lib/types/src/state/LiquidationPosition.d.ts +8 -9
- package/lib/types/src/state/LiquidationPosition.d.ts.map +1 -1
- package/lib/types/tsconfig.base.tsbuildinfo +1 -1
- package/package.json +6 -2
- package/rollup.config.js +3 -1
- package/src/Constants.ts +21 -17
- package/src/idl/idl.ts +13 -13
- package/src/index.ts +3 -0
- package/src/instructions/claimLiquidationPoolPosition.ts +68 -0
- package/src/instructions/closeLiquidationPoolPosition.ts +88 -0
- package/src/instructions/createStakingPool.ts +4 -4
- package/src/instructions/createVault.ts +75 -14
- package/src/instructions/depositLiquidationPool.ts +70 -0
- package/src/instructions/depositStakingPool.ts +2 -2
- package/src/instructions/depositVault.ts +68 -12
- package/src/instructions/liquidateVault.ts +90 -35
- package/src/instructions/loanVault.ts +19 -7
- package/src/instructions/redeemVault.ts +41 -15
- package/src/instructions/refreshOraclePrice.ts +5 -2
- package/src/instructions/repayVault.ts +20 -8
- package/src/instructions/withdrawStakingPool.ts +6 -6
- package/src/instructions/withdrawVault.ts +28 -5
- package/src/state/LiquidationPoolEra.ts +2 -8
- package/src/state/LiquidationPosition.ts +38 -39
package/lib/index.js
CHANGED
@@ -5,6 +5,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
5
|
var anchor = require('@project-serum/anchor');
|
6
6
|
var splToken = require('@solana/spl-token');
|
7
7
|
var web3_js = require('@solana/web3.js');
|
8
|
+
var serum = require('@project-serum/serum');
|
9
|
+
var uuid = require('uuid');
|
8
10
|
var Decimal = require('decimal.js');
|
9
11
|
|
10
12
|
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
@@ -79,12 +81,12 @@ const vaultIdl = {
|
|
79
81
|
isSigner: false
|
80
82
|
},
|
81
83
|
{
|
82
|
-
name: '
|
84
|
+
name: 'tokenProgram',
|
83
85
|
isMut: false,
|
84
86
|
isSigner: false
|
85
87
|
},
|
86
88
|
{
|
87
|
-
name: '
|
89
|
+
name: 'associatedTokenProgram',
|
88
90
|
isMut: false,
|
89
91
|
isSigner: false
|
90
92
|
},
|
@@ -149,12 +151,12 @@ const vaultIdl = {
|
|
149
151
|
isSigner: false
|
150
152
|
},
|
151
153
|
{
|
152
|
-
name: '
|
154
|
+
name: 'tokenProgram',
|
153
155
|
isMut: false,
|
154
156
|
isSigner: false
|
155
157
|
},
|
156
158
|
{
|
157
|
-
name: '
|
159
|
+
name: 'associatedTokenProgram',
|
158
160
|
isMut: false,
|
159
161
|
isSigner: false
|
160
162
|
},
|
@@ -212,7 +214,7 @@ const vaultIdl = {
|
|
212
214
|
isSigner: false
|
213
215
|
},
|
214
216
|
{
|
215
|
-
name: '
|
217
|
+
name: 'tokenProgram',
|
216
218
|
isMut: false,
|
217
219
|
isSigner: false
|
218
220
|
},
|
@@ -297,7 +299,7 @@ const vaultIdl = {
|
|
297
299
|
isSigner: false
|
298
300
|
},
|
299
301
|
{
|
300
|
-
name: '
|
302
|
+
name: 'tokenProgram',
|
301
303
|
isMut: false,
|
302
304
|
isSigner: false
|
303
305
|
},
|
@@ -484,7 +486,7 @@ const vaultIdl = {
|
|
484
486
|
isSigner: false
|
485
487
|
},
|
486
488
|
{
|
487
|
-
name: '
|
489
|
+
name: 'tokenProgram',
|
488
490
|
isMut: false,
|
489
491
|
isSigner: false
|
490
492
|
},
|
@@ -539,7 +541,7 @@ const vaultIdl = {
|
|
539
541
|
isSigner: false
|
540
542
|
},
|
541
543
|
{
|
542
|
-
name: '
|
544
|
+
name: 'tokenProgram',
|
543
545
|
isMut: false,
|
544
546
|
isSigner: false
|
545
547
|
},
|
@@ -605,7 +607,7 @@ const vaultIdl = {
|
|
605
607
|
isSigner: false
|
606
608
|
},
|
607
609
|
{
|
608
|
-
name: '
|
610
|
+
name: 'tokenProgram',
|
609
611
|
isMut: false,
|
610
612
|
isSigner: false
|
611
613
|
},
|
@@ -664,7 +666,7 @@ const vaultIdl = {
|
|
664
666
|
isSigner: false
|
665
667
|
},
|
666
668
|
{
|
667
|
-
name: '
|
669
|
+
name: 'tokenProgram',
|
668
670
|
isMut: false,
|
669
671
|
isSigner: false
|
670
672
|
},
|
@@ -744,7 +746,7 @@ const vaultIdl = {
|
|
744
746
|
isSigner: true
|
745
747
|
},
|
746
748
|
{
|
747
|
-
name: '
|
749
|
+
name: 'tokenProgram',
|
748
750
|
isMut: false,
|
749
751
|
isSigner: false
|
750
752
|
},
|
@@ -810,12 +812,12 @@ const vaultIdl = {
|
|
810
812
|
isSigner: false
|
811
813
|
},
|
812
814
|
{
|
813
|
-
name: '
|
815
|
+
name: 'tokenProgram',
|
814
816
|
isMut: false,
|
815
817
|
isSigner: false
|
816
818
|
},
|
817
819
|
{
|
818
|
-
name: '
|
820
|
+
name: 'associatedTokenProgram',
|
819
821
|
isMut: false,
|
820
822
|
isSigner: false
|
821
823
|
},
|
@@ -1499,34 +1501,38 @@ const CHAINLINK_SOL_USD_ID = 'FmAmfoyPXiA8Vhhe6MZTr3U6rZfEZ1ctEHay1ysqCqcf';
|
|
1499
1501
|
const CHAINLINK_SOL_USD_PUBLICKEY = new web3_js.PublicKey(CHAINLINK_SOL_USD_ID);
|
1500
1502
|
const enc = new TextEncoder();
|
1501
1503
|
async function getLiquidationPoolStatePublicKey() {
|
1502
|
-
const [poolPublicKey
|
1503
|
-
return
|
1504
|
+
const [poolPublicKey] = await web3_js.PublicKey.findProgramAddress([enc.encode('LiquidationPoolStateV1')], HEDGE_PROGRAM_PUBLICKEY);
|
1505
|
+
return poolPublicKey;
|
1504
1506
|
}
|
1505
1507
|
async function getLiquidationPoolUsdhAccountPublicKey() {
|
1506
|
-
const [poolPublicKey
|
1507
|
-
return
|
1508
|
+
const [poolPublicKey] = await web3_js.PublicKey.findProgramAddress([enc.encode('LiquidationPoolUSDHAccountV1')], HEDGE_PROGRAM_PUBLICKEY);
|
1509
|
+
return poolPublicKey;
|
1508
1510
|
}
|
1509
1511
|
async function getUsdhMintPublicKey() {
|
1510
|
-
const [findMintPublicKey
|
1511
|
-
return
|
1512
|
+
const [findMintPublicKey] = await web3_js.PublicKey.findProgramAddress([enc.encode('UsdhMintV1')], HEDGE_PROGRAM_PUBLICKEY);
|
1513
|
+
return findMintPublicKey;
|
1512
1514
|
}
|
1513
1515
|
async function getVaultSystemStatePublicKey() {
|
1514
|
-
const [publicKey
|
1515
|
-
return
|
1516
|
+
const [publicKey] = await web3_js.PublicKey.findProgramAddress([enc.encode('VaultSystemStateV1')], HEDGE_PROGRAM_PUBLICKEY);
|
1517
|
+
return publicKey;
|
1516
1518
|
}
|
1517
1519
|
async function getHedgeMintPublicKey() {
|
1518
|
-
const [publicKey
|
1519
|
-
return
|
1520
|
+
const [publicKey] = await web3_js.PublicKey.findProgramAddress([enc.encode('HEDGEMintV1')], HEDGE_PROGRAM_PUBLICKEY);
|
1521
|
+
return publicKey;
|
1520
1522
|
}
|
1521
1523
|
async function getPoolPublicKeyForMint(mintPublicKey) {
|
1522
1524
|
const strToEncode = (mintPublicKey.toString().substring(0, 12));
|
1523
1525
|
const [publicKey, bump] = await web3_js.PublicKey.findProgramAddress([enc.encode(strToEncode)], HEDGE_PROGRAM_PUBLICKEY);
|
1524
1526
|
return [publicKey, bump, strToEncode];
|
1525
1527
|
}
|
1526
|
-
async function
|
1527
|
-
const [collateralStateAccount] = await web3_js.PublicKey.findProgramAddress([enc.encode(
|
1528
|
+
async function getCollateralStateAccountPublicKey(collateralType) {
|
1529
|
+
const [collateralStateAccount] = await web3_js.PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('State')], HEDGE_PROGRAM_PUBLICKEY);
|
1528
1530
|
return collateralStateAccount;
|
1529
1531
|
}
|
1532
|
+
async function findVaultAddress(vaultSalt) {
|
1533
|
+
const [vaultAddress] = await web3_js.PublicKey.findProgramAddress([enc.encode('Vault'), enc.encode(vaultSalt)], HEDGE_PROGRAM_PUBLICKEY);
|
1534
|
+
return vaultAddress;
|
1535
|
+
}
|
1530
1536
|
async function findAssociatedTokenAddress(walletAddress, tokenMintAddress) {
|
1531
1537
|
return (await web3_js.PublicKey.findProgramAddress([
|
1532
1538
|
walletAddress.toBuffer(),
|
@@ -1542,8 +1548,8 @@ async function createStakingPool(program, provider, payer, mintPublicKey, hedgeT
|
|
1542
1548
|
return poolPublickey;
|
1543
1549
|
}
|
1544
1550
|
async function createStakingPoolInstruction(program, payerPublicKey, mintPublicKey, hedgeTokensToBeMinted, overrideStartTime) {
|
1545
|
-
const
|
1546
|
-
const
|
1551
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey();
|
1552
|
+
const usdhMintPublickey = await getUsdhMintPublicKey();
|
1547
1553
|
const [poolPublickey, poolBump, poolSeedPhrase] = await getPoolPublicKeyForMint(mintPublicKey);
|
1548
1554
|
const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, mintPublicKey);
|
1549
1555
|
const poolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(poolPublickey, usdhMintPublickey);
|
@@ -1557,8 +1563,8 @@ async function createStakingPoolInstruction(program, payerPublicKey, mintPublicK
|
|
1557
1563
|
poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
|
1558
1564
|
poolAssociatedUsdhTokenAccount: poolAssociatedUsdhTokenAccount,
|
1559
1565
|
rent: web3_js.SYSVAR_RENT_PUBKEY,
|
1560
|
-
|
1561
|
-
|
1566
|
+
tokenProgram: splToken.TOKEN_PROGRAM_ID,
|
1567
|
+
associatedTokenProgram: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
|
1562
1568
|
systemProgram: web3_js.SystemProgram.programId
|
1563
1569
|
},
|
1564
1570
|
signers: []
|
@@ -1585,8 +1591,8 @@ async function depositStakingPoolInstruction(program, payerPublicKey, poolPositi
|
|
1585
1591
|
poolAssociatedStakedTokenAccount: poolAssociatedStakedTokenAccount,
|
1586
1592
|
payerAssociatedStakedTokenAccount: payersArbitraryTokenAccount,
|
1587
1593
|
rent: web3_js.SYSVAR_RENT_PUBKEY,
|
1588
|
-
|
1589
|
-
|
1594
|
+
tokenProgram: splToken.TOKEN_PROGRAM_ID,
|
1595
|
+
associatedTokenProgram: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
|
1590
1596
|
systemProgram: web3_js.SystemProgram.programId
|
1591
1597
|
},
|
1592
1598
|
signers: []
|
@@ -1600,9 +1606,9 @@ async function withdrawStakingPool(program, provider, payer, poolPositionPublicK
|
|
1600
1606
|
return poolPosition.publicKey;
|
1601
1607
|
}
|
1602
1608
|
async function withdrawStakingPoolInstruction(program, payerPublicKey, poolPositionPublicKey, stakedTokenMintPublicKey, overrideStartTime) {
|
1603
|
-
const
|
1604
|
-
const
|
1605
|
-
const
|
1609
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey();
|
1610
|
+
const usdhMintPublickey = await getUsdhMintPublicKey();
|
1611
|
+
const hedgeMintPublickey = await getHedgeMintPublicKey();
|
1606
1612
|
const [poolPublickey] = await getPoolPublicKeyForMint(stakedTokenMintPublicKey);
|
1607
1613
|
const poolAssociatedStakedTokenAccount = await findAssociatedTokenAddress(poolPublickey, stakedTokenMintPublicKey);
|
1608
1614
|
const poolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(poolPublickey, usdhMintPublickey);
|
@@ -1627,88 +1633,269 @@ async function withdrawStakingPoolInstruction(program, payerPublicKey, poolPosit
|
|
1627
1633
|
stakedTokenMint: stakedTokenMintPublicKey,
|
1628
1634
|
usdhMint: usdhMintPublickey,
|
1629
1635
|
rent: web3_js.SYSVAR_RENT_PUBKEY,
|
1630
|
-
|
1631
|
-
|
1636
|
+
tokenProgram: splToken.TOKEN_PROGRAM_ID,
|
1637
|
+
associatedTokenProgram: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
|
1632
1638
|
systemProgram: web3_js.SystemProgram.programId
|
1633
1639
|
},
|
1634
1640
|
signers: []
|
1635
1641
|
});
|
1636
1642
|
}
|
1637
1643
|
|
1638
|
-
async function
|
1639
|
-
const
|
1644
|
+
async function depositLiquidationPool(program, provider, payer, depositAmount, overrideStartTime) {
|
1645
|
+
const usdhMintPublickey = await getUsdhMintPublicKey();
|
1646
|
+
const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
|
1647
|
+
const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
|
1648
|
+
const poolPosition = web3_js.Keypair.generate();
|
1649
|
+
const transaction = new web3_js.Transaction().add(await depositLiquidationPoolInstruction(program, payer.publicKey, payerUsdhAccount.address, poolPosition.publicKey, depositAmount, overrideStartTime));
|
1650
|
+
await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, poolPosition], provider.opts).catch(parseAnchorErrors);
|
1651
|
+
return poolPosition.publicKey;
|
1652
|
+
}
|
1653
|
+
async function depositLiquidationPoolInstruction(program, payerPublicKey, payerUsdhAccount, poolPositionPublicKey, depositAmount, overrideStartTime) {
|
1654
|
+
const liquidationPoolStatePublicKey = await getLiquidationPoolStatePublicKey();
|
1655
|
+
const liquidationPoolState = await program.account.liquidationPoolState.fetch(liquidationPoolStatePublicKey);
|
1656
|
+
const poolUSDHAccount = await getLiquidationPoolUsdhAccountPublicKey();
|
1657
|
+
const usdhMint = await getUsdhMintPublicKey();
|
1658
|
+
return program.instruction.depositLiquidationPool(new anchor.BN(depositAmount), new anchor.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), // override current time
|
1659
|
+
{
|
1660
|
+
accounts: {
|
1661
|
+
poolState: liquidationPoolStatePublicKey,
|
1662
|
+
poolEra: liquidationPoolState.currentEra,
|
1663
|
+
poolPosition: poolPositionPublicKey,
|
1664
|
+
poolUsdhAccount: poolUSDHAccount,
|
1665
|
+
usdhMint: usdhMint,
|
1666
|
+
payer: payerPublicKey,
|
1667
|
+
ownerUsdhAccount: payerUsdhAccount,
|
1668
|
+
tokenProgram: splToken.TOKEN_PROGRAM_ID,
|
1669
|
+
systemProgram: web3_js.SystemProgram.programId,
|
1670
|
+
rent: web3_js.SYSVAR_RENT_PUBKEY
|
1671
|
+
},
|
1672
|
+
signers: []
|
1673
|
+
});
|
1674
|
+
}
|
1675
|
+
|
1676
|
+
async function closeLiquidationPoolPosition(program, provider, poolPosition, payer, overrideStartTime) {
|
1677
|
+
const usdhMintPublickey = await getUsdhMintPublicKey();
|
1678
|
+
const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
|
1679
|
+
const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
|
1680
|
+
const poolState = await getLiquidationPoolStatePublicKey();
|
1681
|
+
const liquidationPositionAccount = await program.account.liquidationPosition.fetch(poolPosition);
|
1682
|
+
const poolEra = liquidationPositionAccount.era;
|
1683
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey();
|
1684
|
+
const poolUsdhAccount = await getLiquidationPoolUsdhAccountPublicKey();
|
1685
|
+
const hedgeMint = await getHedgeMintPublicKey();
|
1686
|
+
const payerAssociatedHedgeAccount = await findAssociatedTokenAddress(payer.publicKey, hedgeMint);
|
1687
|
+
const communityAssociatedHedgeTokenAccount = await findAssociatedTokenAddress(vaultSystemStatePublicKey, hedgeMint);
|
1688
|
+
const transaction = new web3_js.Transaction().add(await closeLiquidationPoolPositionInstruction(program, vaultSystemStatePublicKey, poolState, poolEra, poolPosition, poolUsdhAccount, payer.publicKey, payerUsdhAccount.address, hedgeMint, payerAssociatedHedgeAccount, communityAssociatedHedgeTokenAccount, overrideStartTime));
|
1689
|
+
await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors);
|
1690
|
+
return poolPosition;
|
1691
|
+
}
|
1692
|
+
async function closeLiquidationPoolPositionInstruction(program, vaultSystemState, poolState, poolEra, poolPosition, poolUsdhAccount, payerPublicKey, payerUsdhAccount, hedgeMint, payerAssociatedHedgeAccount, communityAssociatedHedgeTokenAccount, overrideStartTime) {
|
1693
|
+
return program.instruction.closeLiquidationPoolPosition(new anchor.BN(overrideStartTime !== null && overrideStartTime !== void 0 ? overrideStartTime : Date.now() / 1000), // override current time
|
1694
|
+
{
|
1695
|
+
accounts: {
|
1696
|
+
vaultSystemState: vaultSystemState,
|
1697
|
+
poolState: poolState,
|
1698
|
+
poolEra: poolEra,
|
1699
|
+
poolPosition: poolPosition,
|
1700
|
+
poolUsdhAccount: poolUsdhAccount,
|
1701
|
+
payer: payerPublicKey,
|
1702
|
+
ownerUsdhAccount: payerUsdhAccount,
|
1703
|
+
hedgeMint: hedgeMint,
|
1704
|
+
payerAssociatedHedgeAccount: payerAssociatedHedgeAccount,
|
1705
|
+
communityAssociatedHedgeTokenAccount: communityAssociatedHedgeTokenAccount,
|
1706
|
+
associatedTokenProgram: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
|
1707
|
+
tokenProgram: splToken.TOKEN_PROGRAM_ID,
|
1708
|
+
systemProgram: web3_js.SystemProgram.programId,
|
1709
|
+
rent: web3_js.SYSVAR_RENT_PUBKEY
|
1710
|
+
},
|
1711
|
+
signers: []
|
1712
|
+
});
|
1713
|
+
}
|
1714
|
+
|
1715
|
+
async function claimLiquidationPoolPosition(program, provider, poolPosition, payer, collateralType, overrideStartTime) {
|
1716
|
+
const collateralStateAccountPublicKey = await getCollateralStateAccountPublicKey(collateralType);
|
1717
|
+
const collateralStateAccountInfo = await program.account.collateralState.fetch(collateralStateAccountPublicKey);
|
1718
|
+
const collateralMint = collateralStateAccountInfo.collateralMint;
|
1719
|
+
const poolStatePublicKey = await getLiquidationPoolStatePublicKey();
|
1720
|
+
const poolAssociatedTokenAccount = await findAssociatedTokenAddress(poolStatePublicKey, collateralMint);
|
1721
|
+
const payerAssociatedTokenAccount = await findAssociatedTokenAddress(payer.publicKey, collateralMint);
|
1722
|
+
const transaction = new web3_js.Transaction().add(await claimLiquidationPoolPositionInstruction(program, poolStatePublicKey, poolAssociatedTokenAccount, collateralStateAccountPublicKey, collateralMint, poolPosition, payer.publicKey, payerAssociatedTokenAccount));
|
1723
|
+
await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts).catch(parseAnchorErrors);
|
1724
|
+
return payerAssociatedTokenAccount;
|
1725
|
+
}
|
1726
|
+
async function claimLiquidationPoolPositionInstruction(program, poolState, poolAssociatedTokenAccount, collateralStateAccount, collateralMint, poolPosition, payer, payerAssociatedTokenAccount, overrideStartTime) {
|
1727
|
+
return program.instruction.claimLiquidationPoolPosition({
|
1728
|
+
accounts: {
|
1729
|
+
poolState: poolState,
|
1730
|
+
poolAssociatedTokenAccount: poolAssociatedTokenAccount,
|
1731
|
+
collateralStateAccount: collateralStateAccount,
|
1732
|
+
collateralMint: collateralMint,
|
1733
|
+
poolPosition: poolPosition,
|
1734
|
+
payer: payer,
|
1735
|
+
payerAssociatedTokenAccount: payerAssociatedTokenAccount,
|
1736
|
+
associatedTokenProgram: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
|
1737
|
+
tokenProgram: splToken.TOKEN_PROGRAM_ID,
|
1738
|
+
systemProgram: web3_js.SystemProgram.programId,
|
1739
|
+
rent: web3_js.SYSVAR_RENT_PUBKEY
|
1740
|
+
},
|
1741
|
+
signers: []
|
1742
|
+
});
|
1743
|
+
}
|
1744
|
+
|
1745
|
+
async function createVault(program, provider, payer, collateralType, depositAmount, collateralRatio) {
|
1746
|
+
const usdhMintPublickey = await getUsdhMintPublicKey();
|
1640
1747
|
const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
|
1748
|
+
const salt = uuid.v4().substring(0, 8);
|
1749
|
+
const newVaultPublicKey = await findVaultAddress(salt);
|
1750
|
+
const history = web3_js.Keypair.generate();
|
1641
1751
|
// Prep the user to get USDH back out at some point
|
1642
1752
|
await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
|
1643
|
-
const
|
1644
|
-
const
|
1645
|
-
const
|
1646
|
-
const
|
1647
|
-
|
1648
|
-
|
1753
|
+
const collateralStateAccountPublicKey = await getCollateralStateAccountPublicKey(collateralType);
|
1754
|
+
const collateralStateAccountInfo = await program.account.collateralState.fetch(collateralStateAccountPublicKey);
|
1755
|
+
const payerTokenAccount = await findAssociatedTokenAddress(payer.publicKey, collateralStateAccountInfo.collateralMint);
|
1756
|
+
const vaultAssociatedCollateralAccountPublicKey = await findAssociatedTokenAddress(newVaultPublicKey, collateralStateAccountInfo.collateralMint);
|
1757
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey();
|
1758
|
+
const wrappedSolAccount = web3_js.Keypair.generate();
|
1759
|
+
const transaction = new web3_js.Transaction();
|
1760
|
+
const signers = [payer, history];
|
1761
|
+
if (collateralType === 'SOL') {
|
1762
|
+
transaction.add(web3_js.SystemProgram.createAccount({
|
1763
|
+
fromPubkey: payer.publicKey,
|
1764
|
+
lamports: depositAmount + 2.04e6,
|
1765
|
+
newAccountPubkey: wrappedSolAccount.publicKey,
|
1766
|
+
programId: splToken.TOKEN_PROGRAM_ID,
|
1767
|
+
space: 165
|
1768
|
+
}), serum.TokenInstructions.initializeAccount({
|
1769
|
+
account: wrappedSolAccount.publicKey,
|
1770
|
+
mint: serum.TokenInstructions.WRAPPED_SOL_MINT,
|
1771
|
+
owner: payer.publicKey
|
1772
|
+
}));
|
1773
|
+
signers.push(wrappedSolAccount);
|
1774
|
+
}
|
1775
|
+
transaction.add(await createVaultInstruction(program, salt, vaultSystemStatePublicKey, payer.publicKey, collateralType === 'SOL' ? wrappedSolAccount.publicKey : payerTokenAccount, newVaultPublicKey, vaultAssociatedCollateralAccountPublicKey, collateralStateAccountPublicKey, collateralStateAccountInfo.collateralMint, history.publicKey, depositAmount, collateralRatio));
|
1776
|
+
if (collateralType === 'SOL') {
|
1777
|
+
transaction.add(serum.TokenInstructions.closeAccount({
|
1778
|
+
source: wrappedSolAccount.publicKey,
|
1779
|
+
destination: payer.publicKey,
|
1780
|
+
owner: payer.publicKey
|
1781
|
+
}));
|
1782
|
+
}
|
1783
|
+
await web3_js.sendAndConfirmTransaction(provider.connection, transaction, signers, provider === null || provider === void 0 ? void 0 : provider.opts).catch(parseAnchorErrors);
|
1784
|
+
return newVaultPublicKey;
|
1649
1785
|
}
|
1650
|
-
async function createVaultInstruction(program, vaultSystemStatePublicKey, payerPublicKey, vaultPublicKey, historyPublicKey, depositAmount, collateralRatio) {
|
1651
|
-
const
|
1652
|
-
const ix = program.instruction.createVault(new anchor.BN(depositAmount), new anchor.BN(collateralRatio), {
|
1786
|
+
async function createVaultInstruction(program, salt, vaultSystemStatePublicKey, payerPublicKey, payerTokenAccountPublicKey, vaultPublicKey, vaultAssociatedCollateralPublicKey, collateralStateAccount, collateralMint, historyPublicKey, depositAmount, collateralRatio) {
|
1787
|
+
const ix = program.instruction.createVault(salt, new anchor.BN(depositAmount), new anchor.BN(collateralRatio), {
|
1653
1788
|
accounts: {
|
1654
1789
|
vaultSystemState: vaultSystemStatePublicKey,
|
1655
1790
|
collateralStateAccount: collateralStateAccount,
|
1656
1791
|
vault: vaultPublicKey,
|
1792
|
+
vaultAssociatedTokenAccount: vaultAssociatedCollateralPublicKey,
|
1657
1793
|
history: historyPublicKey,
|
1658
1794
|
payer: payerPublicKey,
|
1659
|
-
|
1795
|
+
payerTokenAccount: payerTokenAccountPublicKey,
|
1796
|
+
collateralMint: collateralMint,
|
1797
|
+
systemProgram: web3_js.SystemProgram.programId,
|
1798
|
+
tokenProgram: splToken.TOKEN_PROGRAM_ID,
|
1799
|
+
associatedTokenProgram: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
|
1800
|
+
rent: web3_js.SYSVAR_RENT_PUBKEY
|
1660
1801
|
},
|
1661
1802
|
signers: []
|
1662
1803
|
});
|
1663
1804
|
return ix;
|
1664
1805
|
}
|
1665
1806
|
|
1666
|
-
async function depositVault(program, provider, payer, vaultPublicKey,
|
1667
|
-
|
1807
|
+
async function depositVault(program, provider, payer, vaultPublicKey,
|
1808
|
+
// collateralType: string,
|
1809
|
+
depositAmount) {
|
1810
|
+
const usdhMintPublickey = await getUsdhMintPublicKey();
|
1668
1811
|
const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
|
1669
1812
|
// Prep the user to get USDH back out at some point
|
1670
1813
|
await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
|
1814
|
+
const vaultAccount = await program.account.vault.fetch(vaultPublicKey);
|
1815
|
+
const collateralStateAccountPublicKey = await getCollateralStateAccountPublicKey(vaultAccount.collateralType);
|
1816
|
+
const collateralStateAccountInfo = await program.account.collateralState.fetch(collateralStateAccountPublicKey);
|
1817
|
+
const collateralAssociatedTokenAccount = await findAssociatedTokenAddress(collateralStateAccountPublicKey, collateralStateAccountInfo.collateralMint);
|
1818
|
+
const payerTokenAccount = await findAssociatedTokenAddress(payer.publicKey, collateralStateAccountInfo.collateralMint);
|
1819
|
+
const vaultAssociatedCollateralAccountPublicKey = await findAssociatedTokenAddress(vaultPublicKey, collateralStateAccountInfo.collateralMint);
|
1671
1820
|
const history = web3_js.Keypair.generate();
|
1672
|
-
const
|
1673
|
-
const
|
1674
|
-
|
1821
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey();
|
1822
|
+
const wrappedSolAccount = web3_js.Keypair.generate();
|
1823
|
+
const transaction = new web3_js.Transaction();
|
1824
|
+
const signers = [payer, history];
|
1825
|
+
if (vaultAccount.collateralType === 'SOL') {
|
1826
|
+
transaction.add(web3_js.SystemProgram.createAccount({
|
1827
|
+
fromPubkey: payer.publicKey,
|
1828
|
+
lamports: depositAmount + 2.04e6,
|
1829
|
+
newAccountPubkey: wrappedSolAccount.publicKey,
|
1830
|
+
programId: splToken.TOKEN_PROGRAM_ID,
|
1831
|
+
space: 165
|
1832
|
+
}), serum.TokenInstructions.initializeAccount({
|
1833
|
+
account: wrappedSolAccount.publicKey,
|
1834
|
+
mint: serum.TokenInstructions.WRAPPED_SOL_MINT,
|
1835
|
+
owner: payer.publicKey
|
1836
|
+
}));
|
1837
|
+
signers.push(wrappedSolAccount);
|
1838
|
+
}
|
1839
|
+
transaction.add(await depositVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, vaultAccount.collateralType === 'SOL' ? wrappedSolAccount.publicKey : payerTokenAccount, vaultPublicKey, vaultAssociatedCollateralAccountPublicKey, history.publicKey, collateralStateAccountPublicKey, collateralAssociatedTokenAccount, collateralStateAccountInfo.collateralMint, depositAmount));
|
1840
|
+
if (vaultAccount.collateralType === 'SOL') {
|
1841
|
+
transaction.add(serum.TokenInstructions.closeAccount({
|
1842
|
+
source: wrappedSolAccount.publicKey,
|
1843
|
+
destination: payer.publicKey,
|
1844
|
+
owner: payer.publicKey
|
1845
|
+
}));
|
1846
|
+
}
|
1847
|
+
await web3_js.sendAndConfirmTransaction(provider.connection, transaction, signers, provider.opts);
|
1675
1848
|
return vaultPublicKey;
|
1676
1849
|
}
|
1677
|
-
async function depositVaultInstruction(program, vaultSystemStatePublicKey,
|
1678
|
-
const collateralStateAccount = await getSolCollateralStateAccountPublicKey();
|
1850
|
+
async function depositVaultInstruction(program, vaultSystemStatePublicKey, vaultOwner, vaultOwnerTokenAccount, vaultPublicKey, vaultAssociatedTokenAccount, historyPublicKey, collateralStateAccountPublicKey, collateralAssociatedTokenAccount, collateralMint, depositAmount) {
|
1679
1851
|
return program.instruction.depositVault(new anchor.BN(depositAmount), {
|
1680
1852
|
accounts: {
|
1681
1853
|
vaultSystemState: vaultSystemStatePublicKey,
|
1682
|
-
collateralStateAccount:
|
1683
|
-
|
1854
|
+
collateralStateAccount: collateralStateAccountPublicKey,
|
1855
|
+
collateralAssociatedTokenAccount: collateralAssociatedTokenAccount,
|
1856
|
+
collateralTokenMint: collateralMint,
|
1857
|
+
vault: vaultPublicKey,
|
1858
|
+
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
1684
1859
|
history: historyPublicKey,
|
1685
|
-
vaultOwner:
|
1686
|
-
|
1860
|
+
vaultOwner: vaultOwner,
|
1861
|
+
vaultOwnerTokenAccount: vaultOwnerTokenAccount,
|
1862
|
+
systemProgram: web3_js.SystemProgram.programId,
|
1863
|
+
tokenProgram: splToken.TOKEN_PROGRAM_ID
|
1687
1864
|
},
|
1688
1865
|
signers: []
|
1689
1866
|
});
|
1690
1867
|
}
|
1691
1868
|
|
1692
1869
|
async function withdrawVault(program, provider, payer, vaultPublicKey, withdrawAmount) {
|
1693
|
-
const
|
1870
|
+
const usdhMintPublickey = await getUsdhMintPublicKey();
|
1694
1871
|
const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
|
1695
1872
|
// Prep the user to get USDH back out at some point
|
1696
1873
|
await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
|
1697
1874
|
const history = web3_js.Keypair.generate();
|
1698
|
-
const
|
1699
|
-
const
|
1875
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey();
|
1876
|
+
const vaultAccount = await program.account.vault.fetch(vaultPublicKey);
|
1877
|
+
const collateralStateAccount = await getCollateralStateAccountPublicKey(vaultAccount.collateralType);
|
1878
|
+
const vaultAssociatedCollateralAccount = await findAssociatedTokenAddress(vaultPublicKey, serum.TokenInstructions.WRAPPED_SOL_MINT);
|
1879
|
+
const collateralStateAccountInfo = await program.account.collateralState.fetch(collateralStateAccount);
|
1880
|
+
const collateralAssociatedTokenAccount = await findAssociatedTokenAddress(collateralStateAccount, collateralStateAccountInfo.collateralMint);
|
1881
|
+
const token = new splToken.Token(provider.connection, collateralStateAccountInfo.collateralMint, splToken.TOKEN_PROGRAM_ID, payer);
|
1882
|
+
const destinationTokenAccount = await token.getOrCreateAssociatedAccountInfo(payer.publicKey);
|
1883
|
+
const transaction = new web3_js.Transaction().add(await withdrawVaultInstruction(program, vaultSystemStatePublicKey, payer.publicKey, destinationTokenAccount.address, vaultPublicKey, vaultAssociatedCollateralAccount, collateralStateAccount, collateralAssociatedTokenAccount, history.publicKey, withdrawAmount));
|
1700
1884
|
await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts);
|
1701
1885
|
return vaultPublicKey;
|
1702
1886
|
}
|
1703
|
-
async function withdrawVaultInstruction(program, vaultSystemStatePublicKey, payerPublicKey, vaultPublickey, historyPublicKey, withdrawAmount) {
|
1704
|
-
const collateralStateAccount = await getSolCollateralStateAccountPublicKey();
|
1887
|
+
async function withdrawVaultInstruction(program, vaultSystemStatePublicKey, payerPublicKey, destinationTokenAccount, vaultPublickey, vaultAssociatedCollateralPublicKey, collateralStateAccount, collateralAssociatedTokenAccount, historyPublicKey, withdrawAmount) {
|
1705
1888
|
return program.instruction.withdrawVault(new anchor.BN(withdrawAmount), {
|
1706
1889
|
accounts: {
|
1707
1890
|
vaultSystemState: vaultSystemStatePublicKey,
|
1708
1891
|
collateralStateAccount: collateralStateAccount,
|
1709
|
-
|
1892
|
+
collateralAssociatedTokenAccount: collateralAssociatedTokenAccount,
|
1893
|
+
vault: vaultPublickey,
|
1894
|
+
vaultAssociatedTokenAccount: vaultAssociatedCollateralPublicKey,
|
1710
1895
|
history: historyPublicKey,
|
1711
1896
|
vaultOwner: payerPublicKey,
|
1897
|
+
destinationTokenAccount: destinationTokenAccount,
|
1898
|
+
tokenProgram: splToken.TOKEN_PROGRAM_ID,
|
1712
1899
|
systemProgram: web3_js.SystemProgram.programId
|
1713
1900
|
},
|
1714
1901
|
signers: []
|
@@ -1716,34 +1903,40 @@ async function withdrawVaultInstruction(program, vaultSystemStatePublicKey, paye
|
|
1716
1903
|
}
|
1717
1904
|
|
1718
1905
|
async function loanVault(program, provider, payer, vaultPublicKey, loanAmount) {
|
1719
|
-
const
|
1906
|
+
const usdhMintPublickey = await getUsdhMintPublicKey();
|
1720
1907
|
const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
|
1721
1908
|
// Prep the user to get USDH back out at some point
|
1722
1909
|
const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
|
1910
|
+
const vaultAccount = await program.account.vault.fetch(vaultPublicKey);
|
1911
|
+
const collateralStateAccountPublicKey = await getCollateralStateAccountPublicKey(vaultAccount.collateralType);
|
1912
|
+
const collateralStateAccount = await program.account.collateralState.fetch(collateralStateAccountPublicKey);
|
1913
|
+
const collateralAssociatedTokenAccount = await findAssociatedTokenAddress(collateralStateAccountPublicKey, collateralStateAccount.collateralMint);
|
1914
|
+
const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, collateralStateAccount.collateralMint);
|
1723
1915
|
const history = web3_js.Keypair.generate();
|
1724
|
-
const transaction = new web3_js.Transaction().add(await loanVaultInstruction(program, payer.publicKey, payerUsdhAccount.address, vaultPublicKey, history.publicKey, loanAmount));
|
1916
|
+
const transaction = new web3_js.Transaction().add(await loanVaultInstruction(program, payer.publicKey, payerUsdhAccount.address, vaultPublicKey, vaultAssociatedTokenAccount, history.publicKey, collateralStateAccountPublicKey, collateralAssociatedTokenAccount, loanAmount));
|
1725
1917
|
await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts);
|
1726
1918
|
return vaultPublicKey;
|
1727
1919
|
}
|
1728
|
-
async function loanVaultInstruction(program, payerPublicKey, ownerUsdhAccount, vaultPublickey, historyPublicKey, loanAmount) {
|
1729
|
-
const
|
1730
|
-
const
|
1731
|
-
const
|
1920
|
+
async function loanVaultInstruction(program, payerPublicKey, ownerUsdhAccount, vaultPublickey, vaultAssociatedTokenAccount, historyPublicKey, collateralStateAccount, collateralAssociatedTokenAccount, loanAmount) {
|
1921
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey();
|
1922
|
+
const usdhMintPublickey = await getUsdhMintPublicKey();
|
1923
|
+
const hedgeMintPublickey = await getHedgeMintPublicKey();
|
1732
1924
|
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey);
|
1733
1925
|
const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(hedgeStakingPoolPublicKey, usdhMintPublickey);
|
1734
|
-
const collateralStateAccount = await getSolCollateralStateAccountPublicKey();
|
1735
1926
|
return program.instruction.loanVault(new anchor.BN(loanAmount), {
|
1736
1927
|
accounts: {
|
1737
1928
|
vaultSystemState: vaultSystemStatePublicKey,
|
1738
1929
|
collateralStateAccount: collateralStateAccount,
|
1930
|
+
collateralAssociatedTokenAccount: collateralAssociatedTokenAccount,
|
1739
1931
|
vaultAccount: vaultPublickey,
|
1932
|
+
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
1740
1933
|
history: historyPublicKey,
|
1741
1934
|
feePool: hedgeStakingPoolPublicKey,
|
1742
1935
|
feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
|
1743
1936
|
usdhMint: usdhMintPublickey,
|
1744
1937
|
vaultOwner: payerPublicKey,
|
1745
1938
|
ownerUsdhAccount: ownerUsdhAccount,
|
1746
|
-
|
1939
|
+
tokenProgram: splToken.TOKEN_PROGRAM_ID,
|
1747
1940
|
systemProgram: web3_js.SystemProgram.programId
|
1748
1941
|
},
|
1749
1942
|
signers: []
|
@@ -1751,120 +1944,158 @@ async function loanVaultInstruction(program, payerPublicKey, ownerUsdhAccount, v
|
|
1751
1944
|
}
|
1752
1945
|
|
1753
1946
|
async function repayVault(program, provider, payer, vaultPublicKey, repayAmount) {
|
1754
|
-
const
|
1947
|
+
const usdhMintPublickey = await getUsdhMintPublicKey();
|
1755
1948
|
const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
|
1756
1949
|
// Prep the user to get USDH back out at some point
|
1757
1950
|
const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
|
1951
|
+
const vaultAccount = await program.account.vault.fetch(vaultPublicKey);
|
1952
|
+
const collateralStateAccountPublicKey = await getCollateralStateAccountPublicKey(vaultAccount.collateralType);
|
1953
|
+
const collateralStateAccount = await program.account.collateralState.fetch(collateralStateAccountPublicKey);
|
1954
|
+
const collateralAssociatedTokenAccount = await findAssociatedTokenAddress(collateralStateAccountPublicKey, collateralStateAccount.collateralMint);
|
1955
|
+
const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, collateralStateAccount.collateralMint);
|
1758
1956
|
const history = web3_js.Keypair.generate();
|
1759
|
-
const transaction = new web3_js.Transaction().add(await repayVaultInstruction(program, payer.publicKey, payerUsdhAccount.address, vaultPublicKey, history.publicKey, repayAmount));
|
1957
|
+
const transaction = new web3_js.Transaction().add(await repayVaultInstruction(program, payer.publicKey, payerUsdhAccount.address, vaultPublicKey, vaultAssociatedTokenAccount, history.publicKey, collateralStateAccountPublicKey, collateralAssociatedTokenAccount, repayAmount));
|
1760
1958
|
await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts);
|
1761
1959
|
return vaultPublicKey;
|
1762
1960
|
}
|
1763
|
-
async function repayVaultInstruction(program, payerPublicKey, ownerUsdhAccount, vaultPublickey, historyPublicKey, repayAmount) {
|
1764
|
-
const
|
1765
|
-
const
|
1766
|
-
const
|
1961
|
+
async function repayVaultInstruction(program, payerPublicKey, ownerUsdhAccount, vaultPublickey, vaultAssociatedTokenAccount, historyPublicKey, collateralStateAccount, collateralAssociatedTokenAccount, repayAmount) {
|
1962
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey();
|
1963
|
+
const usdhMintPublickey = await getUsdhMintPublicKey();
|
1964
|
+
const hedgeMintPublickey = await getHedgeMintPublicKey();
|
1767
1965
|
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey);
|
1768
1966
|
const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(hedgeStakingPoolPublicKey, usdhMintPublickey);
|
1769
|
-
const collateralStateAccount = await getSolCollateralStateAccountPublicKey();
|
1770
1967
|
return program.instruction.repayVault(new anchor.BN(repayAmount), {
|
1771
1968
|
accounts: {
|
1772
1969
|
vaultSystemState: vaultSystemStatePublicKey,
|
1773
1970
|
collateralStateAccount: collateralStateAccount,
|
1971
|
+
collateralAssociatedTokenAccount: collateralAssociatedTokenAccount,
|
1774
1972
|
vaultAccount: vaultPublickey,
|
1973
|
+
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
1775
1974
|
history: historyPublicKey,
|
1776
1975
|
feePool: hedgeStakingPoolPublicKey,
|
1777
1976
|
feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
|
1778
1977
|
usdhMint: usdhMintPublickey,
|
1779
1978
|
vaultOwner: payerPublicKey,
|
1780
1979
|
ownerUsdhAccount: ownerUsdhAccount,
|
1781
|
-
|
1980
|
+
tokenProgram: splToken.TOKEN_PROGRAM_ID,
|
1782
1981
|
systemProgram: web3_js.SystemProgram.programId
|
1783
1982
|
},
|
1784
1983
|
signers: []
|
1785
1984
|
});
|
1786
1985
|
}
|
1787
1986
|
|
1788
|
-
async function redeemVault(program, provider, payer, vaultPublicKey,
|
1789
|
-
const
|
1987
|
+
async function redeemVault(program, provider, payer, vaultPublicKey, redeemAmount, transactionOverrideTime) {
|
1988
|
+
const usdhMintPublickey = await getUsdhMintPublicKey();
|
1790
1989
|
const USDH = new splToken.Token(provider.connection, usdhMintPublickey, splToken.TOKEN_PROGRAM_ID, payer);
|
1791
1990
|
// Prep the user to get USDH back out at some point
|
1792
1991
|
const payerUsdhAccount = await USDH.getOrCreateAssociatedAccountInfo(payer.publicKey);
|
1992
|
+
const vaultAccount = await program.account.vault.fetch(vaultPublicKey);
|
1993
|
+
const collateralStateAccountPublicKey = await getCollateralStateAccountPublicKey(vaultAccount.collateralType);
|
1994
|
+
const collateralStateAccountInfo = await program.account.collateralState.fetch(collateralStateAccountPublicKey);
|
1995
|
+
const collateralAssociatedTokenAccount = await findAssociatedTokenAddress(collateralStateAccountPublicKey, collateralStateAccountInfo.collateralMint);
|
1996
|
+
const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, collateralStateAccountInfo.collateralMint);
|
1997
|
+
const token = new splToken.Token(provider.connection, collateralStateAccountInfo.collateralMint, splToken.TOKEN_PROGRAM_ID, payer);
|
1998
|
+
const payerTokenAccount = await token.getOrCreateAssociatedAccountInfo(payer.publicKey);
|
1793
1999
|
const history = web3_js.Keypair.generate();
|
1794
|
-
const transaction = new web3_js.Transaction().add(await redeemVaultInstruction(program, payer.publicKey, payerUsdhAccount.address, vaultPublicKey, history.publicKey,
|
2000
|
+
const transaction = new web3_js.Transaction().add(await redeemVaultInstruction(program, payer.publicKey, payerUsdhAccount.address, payerTokenAccount.address, vaultPublicKey, vaultAssociatedTokenAccount, history.publicKey, collateralStateAccountPublicKey, collateralAssociatedTokenAccount, redeemAmount, transactionOverrideTime));
|
1795
2001
|
await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, history], provider.opts);
|
1796
2002
|
return vaultPublicKey;
|
1797
2003
|
}
|
1798
|
-
async function redeemVaultInstruction(program, payerPublicKey,
|
1799
|
-
const
|
1800
|
-
const
|
1801
|
-
const
|
2004
|
+
async function redeemVaultInstruction(program, payerPublicKey, payerUsdhAccount, destinationTokenAccount, vaultPublickey, vaultAssociatedTokenAccount, historyPublicKey, collateralStateAccount, collateralAssociatedTokenAccount, redeemAmount, transactionOverrideTime) {
|
2005
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey();
|
2006
|
+
const usdhMintPublickey = await getUsdhMintPublicKey();
|
2007
|
+
const hedgeMintPublickey = await getHedgeMintPublicKey();
|
1802
2008
|
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey);
|
1803
2009
|
const hedgeStakingPoolAssociatedUsdhTokenAccount = await findAssociatedTokenAddress(hedgeStakingPoolPublicKey, usdhMintPublickey);
|
1804
|
-
|
1805
|
-
return program.instruction.redeemVault(new anchor.BN(repayAmount), new anchor.BN(transactionOverrideTime !== null && transactionOverrideTime !== void 0 ? transactionOverrideTime : (Date.now() / 1000)), // override start time
|
2010
|
+
return program.instruction.redeemVault(new anchor.BN(redeemAmount), new anchor.BN(transactionOverrideTime !== null && transactionOverrideTime !== void 0 ? transactionOverrideTime : (Date.now() / 1000)), // override start time
|
1806
2011
|
{
|
1807
2012
|
accounts: {
|
1808
2013
|
vaultSystemState: vaultSystemStatePublicKey,
|
1809
2014
|
collateralStateAccount: collateralStateAccount,
|
1810
|
-
|
2015
|
+
collateralAssociatedTokenAccount: collateralAssociatedTokenAccount,
|
2016
|
+
vault: vaultPublickey,
|
2017
|
+
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
1811
2018
|
history: historyPublicKey,
|
1812
2019
|
feePool: hedgeStakingPoolPublicKey,
|
1813
2020
|
feePoolAssociatedUsdhTokenAccount: hedgeStakingPoolAssociatedUsdhTokenAccount,
|
1814
2021
|
usdhMint: usdhMintPublickey,
|
1815
2022
|
payer: payerPublicKey,
|
1816
|
-
payerUsdhAccount:
|
1817
|
-
|
2023
|
+
payerUsdhAccount: payerUsdhAccount,
|
2024
|
+
destinationTokenAccount: destinationTokenAccount,
|
2025
|
+
tokenProgram: splToken.TOKEN_PROGRAM_ID,
|
1818
2026
|
systemProgram: web3_js.SystemProgram.programId
|
1819
2027
|
},
|
1820
2028
|
signers: []
|
1821
2029
|
});
|
1822
2030
|
}
|
1823
2031
|
|
1824
|
-
async function liquidateVault(program, provider, payer, vaultPublicKey) {
|
2032
|
+
async function liquidateVault(program, provider, payer, vaultPublicKey, preload) {
|
2033
|
+
const vaultAccount = await program.account.vault.fetch(vaultPublicKey);
|
2034
|
+
const collateralStateAccountPublicKey = await getCollateralStateAccountPublicKey(vaultAccount.collateralType);
|
2035
|
+
const collateralStateAccountInfo = await program.account.collateralState.fetch(collateralStateAccountPublicKey);
|
2036
|
+
const collateralMint = collateralStateAccountInfo.collateralMint;
|
2037
|
+
const token = new splToken.Token(provider.connection, collateralMint, splToken.TOKEN_PROGRAM_ID, payer);
|
2038
|
+
const hedgeMintPublickey = await getHedgeMintPublicKey();
|
2039
|
+
const [hedgeStakingPoolPublicKey] = await getPoolPublicKeyForMint(hedgeMintPublickey);
|
2040
|
+
const liquidationPoolStatePublicKey = await getLiquidationPoolStatePublicKey();
|
2041
|
+
const poolStateInfo = await program.account.liquidationPoolState.fetch(liquidationPoolStatePublicKey);
|
2042
|
+
const payerAssociatedTokenAccount = await token.getOrCreateAssociatedAccountInfo(payer.publicKey);
|
2043
|
+
const feePoolAssociatedTokenAccount = await findAssociatedTokenAddress(hedgeStakingPoolPublicKey, collateralMint);
|
2044
|
+
const vaultAssociatedTokenAccount = await findAssociatedTokenAddress(vaultPublicKey, collateralMint);
|
2045
|
+
const poolAssociatedTokenAccount = await findAssociatedTokenAddress(liquidationPoolStatePublicKey, collateralMint);
|
2046
|
+
const collateralAssociatedTokenAccount = await findAssociatedTokenAddress(collateralStateAccountPublicKey, collateralMint);
|
1825
2047
|
const history = web3_js.Keypair.generate();
|
1826
2048
|
const newEra = web3_js.Keypair.generate();
|
1827
|
-
const transaction = new web3_js.Transaction()
|
2049
|
+
const transaction = new web3_js.Transaction();
|
2050
|
+
transaction.add(await liquidateVaultInstruction(program, payer.publicKey, payerAssociatedTokenAccount.address, vaultPublicKey, vaultAssociatedTokenAccount, liquidationPoolStatePublicKey, poolStateInfo.currentEra, poolAssociatedTokenAccount, history.publicKey, newEra.publicKey, hedgeStakingPoolPublicKey, feePoolAssociatedTokenAccount, collateralMint, collateralAssociatedTokenAccount, vaultAccount.collateralType, preload));
|
1828
2051
|
await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer, history, newEra], provider.opts);
|
1829
2052
|
return vaultPublicKey;
|
1830
2053
|
}
|
1831
|
-
async function liquidateVaultInstruction(program, payerPublicKey, vaultPublickey, historyPublicKey, newEraPublicKey) {
|
1832
|
-
const
|
1833
|
-
const
|
1834
|
-
const
|
1835
|
-
const
|
1836
|
-
const
|
1837
|
-
const [liquidationPoolUsdhAccountPublickey] = await getLiquidationPoolUsdhAccountPublicKey();
|
1838
|
-
const collateralStateAccount = await getSolCollateralStateAccountPublicKey();
|
1839
|
-
const poolStateInfo = await program.account.liquidationPoolState.fetch(liquidationPoolStatePublicKey);
|
1840
|
-
return program.instruction.liquidateVault({
|
2054
|
+
async function liquidateVaultInstruction(program, payerPublicKey, payerAssociatedTokenAccount, vaultPublickey, vaultAssociatedTokenAccount, poolState, poolEra, poolAssociatedTokenAccount, historyPublicKey, newEraPublicKey, feePool, feePoolAssociatedTokenAccount, collateralMint, collateralAssociatedTokenAccount, collateralType, preload) {
|
2055
|
+
const vaultSystemStatePublicKey = await getVaultSystemStatePublicKey();
|
2056
|
+
const usdhMintPublickey = await getUsdhMintPublicKey();
|
2057
|
+
const liquidationPoolUsdhAccountPublickey = await getLiquidationPoolUsdhAccountPublicKey();
|
2058
|
+
const collateralStateAccount = await getCollateralStateAccountPublicKey(collateralType);
|
2059
|
+
const payload = {
|
1841
2060
|
accounts: {
|
1842
2061
|
vaultSystemState: vaultSystemStatePublicKey,
|
1843
2062
|
collateralStateAccount: collateralStateAccount,
|
1844
|
-
|
2063
|
+
collateralAssociatedTokenAccount: collateralAssociatedTokenAccount,
|
2064
|
+
collateralMint: collateralMint,
|
2065
|
+
poolEra: poolEra,
|
1845
2066
|
vaultAccount: vaultPublickey,
|
1846
|
-
|
2067
|
+
vaultAssociatedTokenAccount: vaultAssociatedTokenAccount,
|
2068
|
+
poolState: poolState,
|
2069
|
+
poolAssociatedTokenAccount: poolAssociatedTokenAccount,
|
1847
2070
|
usdhMint: usdhMintPublickey,
|
1848
2071
|
history: historyPublicKey,
|
1849
2072
|
payer: payerPublicKey,
|
1850
|
-
|
1851
|
-
|
1852
|
-
|
2073
|
+
payerAssociatedTokenAccount: payerAssociatedTokenAccount,
|
2074
|
+
feePool: feePool,
|
2075
|
+
feePoolAssociatedTokenAccount: feePoolAssociatedTokenAccount,
|
1853
2076
|
liquidationPoolUsdhAccount: liquidationPoolUsdhAccountPublickey,
|
1854
|
-
newEra: newEraPublicKey
|
2077
|
+
newEra: newEraPublicKey,
|
2078
|
+
tokenProgram: splToken.TOKEN_PROGRAM_ID,
|
2079
|
+
systemProgram: web3_js.SystemProgram.programId,
|
2080
|
+
associatedTokenProgram: splToken.ASSOCIATED_TOKEN_PROGRAM_ID,
|
2081
|
+
rent: web3_js.SYSVAR_RENT_PUBKEY
|
1855
2082
|
},
|
1856
2083
|
signers: []
|
1857
|
-
}
|
2084
|
+
};
|
2085
|
+
if (preload) {
|
2086
|
+
return program.instruction.liquidateVaultPrepAccounts(payload);
|
2087
|
+
}
|
2088
|
+
return program.instruction.liquidateVault(payload);
|
1858
2089
|
}
|
1859
2090
|
|
1860
|
-
async function refreshOraclePrice(program, provider, payer, network, overridePrice, overrideTime) {
|
1861
|
-
const transaction = new web3_js.Transaction().add(await refreshOraclePriceInstruction(program, network, overridePrice, overrideTime));
|
2091
|
+
async function refreshOraclePrice(program, provider, payer, collateralType, network, overridePrice, overrideTime) {
|
2092
|
+
const transaction = new web3_js.Transaction().add(await refreshOraclePriceInstruction(program, collateralType, network, overridePrice, overrideTime));
|
1862
2093
|
return await web3_js.sendAndConfirmTransaction(provider.connection, transaction, [payer], provider.opts);
|
1863
2094
|
}
|
1864
|
-
async function refreshOraclePriceInstruction(program, network, overridePrice, overrideTime) {
|
2095
|
+
async function refreshOraclePriceInstruction(program, collateralType, network, overridePrice, overrideTime) {
|
1865
2096
|
const enc = new TextEncoder();
|
1866
|
-
const [oracleInfoAccount] = await web3_js.PublicKey.findProgramAddress([enc.encode(
|
1867
|
-
const [collateralStateAccount] = await web3_js.PublicKey.findProgramAddress([enc.encode(
|
2097
|
+
const [oracleInfoAccount] = await web3_js.PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('Oracle')], HEDGE_PROGRAM_PUBLICKEY);
|
2098
|
+
const [collateralStateAccount] = await web3_js.PublicKey.findProgramAddress([enc.encode(collateralType), enc.encode('State')], HEDGE_PROGRAM_PUBLICKEY);
|
1868
2099
|
return program.instruction.refreshOraclePrice(new anchor.BN(overridePrice !== null && overridePrice !== void 0 ? overridePrice : web3_js.LAMPORTS_PER_SOL * 150), // override usd/sol price
|
1869
2100
|
new anchor.BN(overrideTime !== null && overrideTime !== void 0 ? overrideTime : Math.floor(Date.now() / 1000)), // override override time
|
1870
2101
|
{
|
@@ -2054,7 +2285,7 @@ class LiquidationPoolEra {
|
|
2054
2285
|
this.liquidyPoolEra = liquidyPoolEra;
|
2055
2286
|
this.totalDeposits = liquidyPoolEra.totalDeposits.toNumber();
|
2056
2287
|
this.product = DecimalFromU128(liquidyPoolEra.productBytes);
|
2057
|
-
this.sum =
|
2288
|
+
this.sum = liquidyPoolEra.sumBytes.map((sumBytes) => { return DecimalFromU128(sumBytes); });
|
2058
2289
|
this.hedgeRewardsAccumulator = DecimalFromU128(liquidyPoolEra.hedgeRewardsAccumulatorBytes);
|
2059
2290
|
this.hedgeRewardsTimestamp = liquidyPoolEra.hedgeRewardsTimestamp.toNumber();
|
2060
2291
|
}
|
@@ -2070,47 +2301,35 @@ class LiquidationPoolState {
|
|
2070
2301
|
}
|
2071
2302
|
|
2072
2303
|
class LiquidationPosition {
|
2073
|
-
constructor(poolPositionInfo, key
|
2304
|
+
constructor(poolPositionInfo, key) {
|
2074
2305
|
this.publicKey = key;
|
2075
2306
|
this.eraPublicKey = poolPositionInfo.era;
|
2076
|
-
this.era = era;
|
2077
|
-
this.liquidationPoolState = liquidationPoolState;
|
2078
2307
|
this.ownerAccount = poolPositionInfo.ownerAccount;
|
2079
2308
|
this.deposit = poolPositionInfo.deposit.toNumber();
|
2080
2309
|
this.closedUsdh = poolPositionInfo.closedUsdh.toNumber();
|
2081
|
-
this.closedSol = poolPositionInfo.closedSol.toNumber();
|
2082
2310
|
this.timestampOpened = poolPositionInfo.timestampOpened.toNumber();
|
2083
2311
|
this.timestampClosed = poolPositionInfo.timestampClosed.toNumber();
|
2084
|
-
this.
|
2085
|
-
this.
|
2312
|
+
this.productSnapshotEntry = DecimalFromU128(poolPositionInfo.productSnapshotEntry);
|
2313
|
+
this.productSnapshotClosed = DecimalFromU128(poolPositionInfo.productSnapshotClosed);
|
2314
|
+
this.sumSnapshotsEntry = poolPositionInfo.sumSnapshotsEntry.map((sum) => { return DecimalFromU128(sum); });
|
2315
|
+
this.sumSnapshotsClosed = poolPositionInfo.sumSnapshotsClosed.map((sum) => { return DecimalFromU128(sum); });
|
2086
2316
|
this.hedgeRewardsSnapshot = DecimalFromU128(poolPositionInfo.hedgeRewardsSnapshotAccum);
|
2087
2317
|
this.open = poolPositionInfo.state.open !== undefined;
|
2088
2318
|
}
|
2089
|
-
getUsdhAvailable() {
|
2090
|
-
return (
|
2091
|
-
}
|
2092
|
-
getSolAvailable() {
|
2093
|
-
return this.era.sum.minus(this.sumSnapshot).div(this.productSnapshot).mul(new Decimal__default["default"](this.deposit)).floor();
|
2319
|
+
getUsdhAvailable(era) {
|
2320
|
+
return (era.product.div(this.productSnapshotEntry)).mul(new Decimal__default["default"](this.deposit));
|
2094
2321
|
}
|
2095
|
-
|
2096
|
-
|
2097
|
-
const hedgeRewardsSinceLastUpdate = hedgeRewardsDecay(LiquidationPoolTotalSupply, this.liquidationPoolState.hedgeInitRewardsTimestamp * 1000, this.era.hedgeRewardsTimestamp * 1000, Date.now(), 365 * 1000);
|
2098
|
-
if (this.era.totalDeposits === 0) {
|
2099
|
-
return new Decimal__default["default"](0);
|
2100
|
-
}
|
2101
|
-
const rewardsPerToken = this.era.product.mul(new Decimal__default["default"](hedgeRewardsSinceLastUpdate / this.era.totalDeposits));
|
2102
|
-
const newAccumulator = rewardsPerToken.add(new Decimal__default["default"](this.era.hedgeRewardsAccumulator));
|
2103
|
-
const hedgeAvailable = (newAccumulator.minus(this.hedgeRewardsSnapshot)).mul(new Decimal__default["default"](this.deposit)).div(new Decimal__default["default"](this.productSnapshot));
|
2104
|
-
return hedgeAvailable;
|
2322
|
+
getSolAvailable(era) {
|
2323
|
+
return era.sum[0].minus(this.sumSnapshotsEntry[0]).div(this.productSnapshotEntry).mul(new Decimal__default["default"](this.deposit)).floor();
|
2105
2324
|
}
|
2106
2325
|
}
|
2107
|
-
function hedgeRewardsDecay(supply, birthTime, timeIn, timeOut, halfLifeInDays) {
|
2108
|
-
|
2109
|
-
|
2110
|
-
|
2111
|
-
|
2112
|
-
|
2113
|
-
}
|
2326
|
+
// function hedgeRewardsDecay (supply: number, birthTime: number, timeIn: number, timeOut: number, halfLifeInDays: number): number {
|
2327
|
+
// const timeInOffsetStart = timeIn - birthTime
|
2328
|
+
// const timeOutOffsetStart = timeOut - birthTime
|
2329
|
+
// const halfLife = -1 * Math.log(2) / (halfLifeInDays * 60 * 60 * 24)
|
2330
|
+
// const awardedTokens = supply * (Math.pow(Math.E, halfLife * timeInOffsetStart) - Math.pow(Math.E, halfLife * timeOutOffsetStart))
|
2331
|
+
// return awardedTokens
|
2332
|
+
// }
|
2114
2333
|
|
2115
2334
|
exports.CHAINLINK_SOL_USD_ID = CHAINLINK_SOL_USD_ID;
|
2116
2335
|
exports.CHAINLINK_SOL_USD_PUBLICKEY = CHAINLINK_SOL_USD_PUBLICKEY;
|
@@ -2124,20 +2343,27 @@ exports.StakingPool = StakingPool;
|
|
2124
2343
|
exports.StakingPoolPosition = StakingPoolPosition;
|
2125
2344
|
exports.VaultAccount = VaultAccount;
|
2126
2345
|
exports.VaultHistoryEvent = VaultHistoryEvent;
|
2346
|
+
exports.claimLiquidationPoolPosition = claimLiquidationPoolPosition;
|
2347
|
+
exports.claimLiquidationPoolPositionInstruction = claimLiquidationPoolPositionInstruction;
|
2348
|
+
exports.closeLiquidationPoolPosition = closeLiquidationPoolPosition;
|
2349
|
+
exports.closeLiquidationPoolPositionInstruction = closeLiquidationPoolPositionInstruction;
|
2127
2350
|
exports.createStakingPool = createStakingPool;
|
2128
2351
|
exports.createStakingPoolInstruction = createStakingPoolInstruction;
|
2129
2352
|
exports.createVault = createVault;
|
2130
2353
|
exports.createVaultInstruction = createVaultInstruction;
|
2354
|
+
exports.depositLiquidationPool = depositLiquidationPool;
|
2355
|
+
exports.depositLiquidationPoolInstruction = depositLiquidationPoolInstruction;
|
2131
2356
|
exports.depositStakingPool = depositStakingPool;
|
2132
2357
|
exports.depositStakingPoolInstruction = depositStakingPoolInstruction;
|
2133
2358
|
exports.depositVault = depositVault;
|
2134
2359
|
exports.depositVaultInstruction = depositVaultInstruction;
|
2135
2360
|
exports.findAssociatedTokenAddress = findAssociatedTokenAddress;
|
2361
|
+
exports.findVaultAddress = findVaultAddress;
|
2362
|
+
exports.getCollateralStateAccountPublicKey = getCollateralStateAccountPublicKey;
|
2136
2363
|
exports.getHedgeMintPublicKey = getHedgeMintPublicKey;
|
2137
2364
|
exports.getLiquidationPoolStatePublicKey = getLiquidationPoolStatePublicKey;
|
2138
2365
|
exports.getLiquidationPoolUsdhAccountPublicKey = getLiquidationPoolUsdhAccountPublicKey;
|
2139
2366
|
exports.getPoolPublicKeyForMint = getPoolPublicKeyForMint;
|
2140
|
-
exports.getSolCollateralStateAccountPublicKey = getSolCollateralStateAccountPublicKey;
|
2141
2367
|
exports.getUsdhMintPublicKey = getUsdhMintPublicKey;
|
2142
2368
|
exports.getVaultSystemStatePublicKey = getVaultSystemStatePublicKey;
|
2143
2369
|
exports.liquidateVault = liquidateVault;
|