Skip to content

Commit

Permalink
fix spl
Browse files Browse the repository at this point in the history
  • Loading branch information
timongll committed Aug 23, 2023
1 parent 2871290 commit 6ee6867
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
39 changes: 24 additions & 15 deletions shared/src/utils/vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
import { getAssetColor } from "./asset";
import { Connection, PublicKey } from "@solana/web3.js";
import { Vault, vaultTypes, vaultUtils } from "@zetamarkets/flex-sdk";
import * as spl from "@solana/spl-token";

export const isVaultFull = (
deposits: BigNumber,
Expand Down Expand Up @@ -89,29 +88,39 @@ export const getSOLPricePerShare = async (): Promise<number> => {
);
const vaultAddress = vaultUtils.getVaultAddress("rSOL-THETA")[0];
const vaultInfo = Vault.getVault(vaultAddress);
const vaultUnderlyingTokenAccount = await spl.AccountLayout(
connection,
vaultInfo.vaultUnderlyingTokenAccount

// Get the vault's underlying token account
const vaultUnderlyingTokenAccountInfo = await connection.getBalance(
new PublicKey(vaultInfo.vaultUnderlyingTokenAccount)
);

let totalBalance = Number(vaultUnderlyingTokenAccount.amount);
if (!vaultUnderlyingTokenAccountInfo) {
return 0;
}

let totalBalance = Number(vaultUnderlyingTokenAccountInfo); // Adjust according to your data layout if necessary

// If the option is initialized, we know that a portion of the vault balance is in the option collateral.
if (vaultInfo.option !== null) {
const optionVault = await spl.AccountLayout(
connection,
vaultInfo.option.vault
// If the option is initialized, get the option's vault balance
const optionVaultBalance = await connection.getBalance(
new PublicKey(vaultInfo.option.vault)
);
totalBalance += Number(optionVault.amount);
if (!optionVaultBalance) {
return 0;
}
totalBalance += Number(optionVaultBalance); // Adjust according to your data layout if necessary
}

const redeemableMint = await spl.MintLayout(
connection,
vaultInfo.redeemableMint
// Fetch mint information using Connection
const redeemableMintAccountInfo = await connection.getTokenSupply(
new PublicKey(vaultInfo.redeemableMint)
);
if (!redeemableMintAccountInfo) {
throw new Error("Failed to fetch the redeemable mint account info.");
}

// Price per share = total vault balance / redeemable mint supply
const pricePerShare = totalBalance / Number(redeemableMint.supply);
const redeemableMintSupply = redeemableMintAccountInfo.value.amount;
const pricePerShare = totalBalance / Number(redeemableMintSupply);

return pricePerShare;
};
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3670,7 +3670,7 @@
dependencies:
"@solana/wallet-adapter-base" "^0.9.23"

"@solana/wallet-adapter-react@0.15.34":
"@solana/wallet-adapter-react@0.15.34", "@solana/wallet-adapter-react@^0.13.1":
version "0.15.34"
resolved "https://registry.yarnpkg.com/@solana/wallet-adapter-react/-/wallet-adapter-react-0.15.34.tgz#40b8260f52f46063a1a12b4e2767e3ecc9ae3726"
integrity sha512-HuJyDQ7KxEM2D0Zx2kGTGZlPxVZztAX1N1Hd7WaBeRJGT0NUPHJJFo9q7Wg0rH1BRBtMUQfQyiVc2lF+om+Z9w==
Expand Down Expand Up @@ -3805,7 +3805,7 @@
"@jnwng/walletconnect-solana" "^0.2.0"
"@solana/wallet-adapter-base" "^0.9.23"

"@solana/wallet-adapter-wallets@0.19.20":
"@solana/wallet-adapter-wallets@0.19.20", "@solana/wallet-adapter-wallets@^0.11.3":
version "0.19.20"
resolved "https://registry.yarnpkg.com/@solana/wallet-adapter-wallets/-/wallet-adapter-wallets-0.19.20.tgz#25bbbbf2349f9c8ee169de1db34c74900f0e8a35"
integrity sha512-bE7tS6UykCSiWmudo8DYe/mZV0HoyqI+XifIjPaY/BNSgXQ3u1cwbqvqcLURuixBRnOka7FE6VuT90PhRsosqA==
Expand Down

0 comments on commit 6ee6867

Please sign in to comment.