import { readFileSync } from "node:fs";
import {
AumCalculator,
ExponentVault,
resolvePriceIdFromMintToUnderlying,
vaultSettingsAction,
} from "@exponent-labs/exponent-sdk";
import {
createAssociatedTokenAccountIdempotentInstruction,
getAssociatedTokenAddressSync,
getMint,
TOKEN_PROGRAM_ID,
TOKEN_2022_PROGRAM_ID,
} from "@solana/spl-token";
import {
ComputeBudgetProgram,
Connection,
Keypair,
PublicKey,
Transaction,
sendAndConfirmTransaction,
} from "@solana/web3.js";
function requiredEnv(name: string): string {
const value = process.env[name];
if (!value) throw new Error(`Set ${name}`);
return value;
}
const connection = new Connection(requiredEnv("RPC_URL"), "confirmed");
const manager = Keypair.fromSecretKey(Uint8Array.from(
JSON.parse(readFileSync(requiredEnv("MANAGER_KEYPAIR_PATH"), "utf8")),
));
const vaultAddress = new PublicKey(requiredEnv("VAULT_ADDRESS"));
const assetMint = new PublicKey(requiredEnv("ASSET_MINT"));
const vault = await ExponentVault.load({ connection, address: vaultAddress });
if (!vault.state.roles.manager.some((key) => key.equals(manager.publicKey))) {
throw new Error("The signer must be a vault manager");
}
if (vault.state.tokenEntries.length || vault.state.strategyPositions.length) {
throw new Error("Use a new vault without token entries or strategy positions");
}
if (vault.getTotalAum() !== 0n || await vault.fetchLpTokenSupply() !== 0n) {
throw new Error("Use a vault with zero AUM and LP supply");
}
if (vault.isAumRefreshRequired()) {
throw new Error("Refresh AUM before changing vault settings");
}
const mintAccount = await connection.getAccountInfo(assetMint);
if (!mintAccount) throw new Error("The deposit mint does not exist");
const tokenProgram = mintAccount.owner;
if (!tokenProgram.equals(TOKEN_PROGRAM_ID) &&
!tokenProgram.equals(TOKEN_2022_PROGRAM_ID)) {
throw new Error("The mint must use Token Program or Token-2022");
}
await getMint(connection, assetMint, "confirmed", tokenProgram);
const prices = await vault.fetcher.fetchExponentPrices();
const assetPriceId = resolvePriceIdFromMintToUnderlying(
prices,
assetMint,
vault.state.underlyingMint,
);
if (!assetPriceId) throw new Error("No registered price path for this asset");
const tokenSquadsAccount = getAssociatedTokenAddressSync(
assetMint, vault.state.squadsVault, true, tokenProgram,
);
const tokenAccountVault = getAssociatedTokenAddressSync(
assetMint, vault.selfAddress, true, tokenProgram,
);
const addAsset = vault.ixManageVaultSettings({
manager: manager.publicKey,
actions: [vaultSettingsAction("AddTokenEntry", [{
mint: assetMint,
priceId: assetPriceId,
tokenSquadsAccount,
tokenAccountVault,
lastObservedAmount: 0n,
forceDeallocatePolicyIds: [],
}])],
remainingAccounts: [tokenSquadsAccount, tokenAccountVault].map((pubkey) => ({
pubkey,
isSigner: false,
isWritable: false,
})),
});
const transaction = new Transaction().add(
ComputeBudgetProgram.setComputeUnitLimit({ units: 400_000 }),
ComputeBudgetProgram.requestHeapFrame({ bytes: 256 * 1024 }),
createAssociatedTokenAccountIdempotentInstruction(
manager.publicKey, tokenSquadsAccount, vault.state.squadsVault,
assetMint, tokenProgram,
),
createAssociatedTokenAccountIdempotentInstruction(
manager.publicKey, tokenAccountVault, vault.selfAddress,
assetMint, tokenProgram,
),
addAsset,
);
const signature = await sendAndConfirmTransaction(
connection, transaction, [manager], { commitment: "confirmed" },
);
await vault.reload();
const entry = vault.state.tokenEntries.find((item) => item.mint.equals(assetMint));
if (!entry || !entry.tokenSquadsAccount.equals(tokenSquadsAccount) ||
!entry.tokenAccountVault.equals(tokenAccountVault)) {
throw new Error("Reloaded token entry does not match the submitted accounts");
}
const expectedPriceIds = assetPriceId.__kind === "Simple"
? [assetPriceId.priceId]
: assetPriceId.priceIds;
if (AumCalculator.getPriceIds(entry.priceId).join(",") !==
expectedPriceIds.join(",")) {
throw new Error("Reloaded price reference does not match the submitted chain");
}
console.log({
signature,
mint: entry.mint.toBase58(),
priceId: entry.priceId,
tokenSquadsAccount: entry.tokenSquadsAccount.toBase58(),
tokenAccountVault: entry.tokenAccountVault.toBase58(),
aumRefreshRequired: vault.isAumRefreshRequired(),
});