import * as anchor from "@project-serum/anchor"; import { Program, Idl, parseIdlErrors } from "@project-serum/anchor"; import { assert } from "chai"; import { parseTranactionError } from "../../deps/tokenized-euros/packages/tokenized-euros/src/utils"; import { EuroPrimitive } from "../../deps/tokenized-euros/target/types/euro_primitive"; const { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, Token, } = require("@solana/spl-token"); const { Connection, Keypair, Transaction, PublicKey, SystemProgram, SYSVAR_RENT_PUBKEY, } = require("@solana/web3.js"); import idl from "../../target/idl/psyfi_euros.json"; const { BN } = anchor; const textEncoder = new TextEncoder(); const psyfiIDL: Idl = JSON.parse(JSON.stringify(idl)); const idlErrors = parseIdlErrors(psyfiIDL); // Helper method for building an instruction into a transcaction, // signing and sending the transaction, followed by verifying that // the transaction has been confirmed. export const sendAndConfirmTransaction = async (provider, ix, signers) => { const recentBlockhash = await provider.connection.getRecentBlockhash( "processed" ); const tx = new anchor.web3.Transaction({ feePayer: provider.wallet.publicKey, recentBlockhash: recentBlockhash.blockhash, }); tx.add(ix); signers.forEach((element) => { tx.partialSign(element); }); provider.wallet.signTransaction(tx); const txId = await provider.connection.sendRawTransaction(tx.serialize()); return await provider.connection.confirmTransaction(txId); }; /** * From: tokenized-euros/tests/serum.ts * */ export const createFirstSetOfAccounts = async ({ connection, wallet, dexProgramId, }: { connection: Connection; wallet: typeof anchor.Wallet; dexProgramId: PublicKey; }) => { const eventQueue = new Keypair(); const bids = new Keypair(); const asks = new Keypair(); const tx2 = new Transaction(); tx2.add( SystemProgram.createAccount({ fromPubkey: wallet.publicKey, newAccountPubkey: eventQueue.publicKey, lamports: await connection.getMinimumBalanceForRentExemption(262144 + 12), space: 262144 + 12, programId: dexProgramId, }), SystemProgram.createAccount({ fromPubkey: wallet.publicKey, newAccountPubkey: bids.publicKey, lamports: await connection.getMinimumBalanceForRentExemption(65536 + 12), space: 65536 + 12, programId: dexProgramId, }), SystemProgram.createAccount({ fromPubkey: wallet.publicKey, newAccountPubkey: asks.publicKey, lamports: await connection.getMinimumBalanceForRentExemption(65536 + 12), space: 65536 + 12, programId: dexProgramId, }) ); const transactions = [ { transaction: tx2, signers: [eventQueue, bids, asks], }, ]; for (let tx of transactions) { await anchor.getProvider().send(tx.transaction, tx.signers); } return { eventQueue, bids, asks }; }; /** * From: tokenized-euros/packages/tokenized-euros/src/pdas.ts * Derive the EuroMeta account address from it's unique params. * * @param program * @param underlyingMint * @param stableMint * @param underlyingAmountPerContract * @param strikePrice */ export const deriveEuroMeta = ( program: Program, underlyingMint: anchor.web3.PublicKey, stableMint: anchor.web3.PublicKey, expiration: anchor.BN, underlyingAmountPerContract: anchor.BN, strikePrice: anchor.BN, priceDecimals: number ) => anchor.web3.PublicKey.findProgramAddress( [ underlyingMint.toBuffer(), stableMint.toBuffer(), expiration.toBuffer("le", 8), underlyingAmountPerContract.toBuffer("le", 8), strikePrice.toBuffer("le", 8), new anchor.BN(priceDecimals).toBuffer("le", 1), ], program.programId ); /** * From: tokenized-euros/packages/tokenized-euros/src/pdas.ts * @param program * @returns */ export const deriveUnderlyingPoolKey = ( program: Program, underlyingMint: anchor.web3.PublicKey ) => anchor.web3.PublicKey.findProgramAddress( [underlyingMint.toBuffer(), textEncoder.encode("underlyingPool")], program.programId ); export const deriveStablePoolKey = ( program: Program, stableMint: anchor.web3.PublicKey ) => anchor.web3.PublicKey.findProgramAddress( [stableMint.toBuffer(), textEncoder.encode("stablePool")], program.programId ); /** * From: tokenized-euros/packages/tokenized-euros/src/pdas.ts * Derive the ExpirationData for an underlying mint and oracles * * @param program * @param underlyingMint * @param expiration * @param pythOracleAddress * @param priceDecimals * @returns */ export const deriveExpirationData = ( program: Program, underlyingMint: anchor.web3.PublicKey, expiration: anchor.BN, pythOracleAddress: anchor.web3.PublicKey, priceDecimals: number ) => anchor.web3.PublicKey.findProgramAddress( [ underlyingMint.toBuffer(), expiration.toBuffer("le", 8), pythOracleAddress.toBuffer(), new anchor.BN(priceDecimals).toBuffer("le", 1), ], program.programId ); // From: tokenized-euros/packages/tokenized-euros/src/pdas.ts export const deriveCallOptionMint = ( program: Program, euroMetaKey: anchor.web3.PublicKey ) => anchor.web3.PublicKey.findProgramAddress( [euroMetaKey.toBuffer(), textEncoder.encode("callOptionMint")], program.programId ); // From: tokenized-euros/packages/tokenized-euros/src/pdas.ts export const deriveCallWriterMint = ( program: Program, euroMetaKey: anchor.web3.PublicKey ) => anchor.web3.PublicKey.findProgramAddress( [euroMetaKey.toBuffer(), textEncoder.encode("callWriterMint")], program.programId ); // From: tokenized-euros/packages/tokenized-euros/src/pdas.ts export const derivePutOptionMint = ( program: Program, euroMetaKey: anchor.web3.PublicKey ) => anchor.web3.PublicKey.findProgramAddress( [euroMetaKey.toBuffer(), textEncoder.encode("putOptionMint")], program.programId ); // From: tokenized-euros/packages/tokenized-euros/src/pdas.ts export const derivePutWriterMint = ( program: Program, euroMetaKey: anchor.web3.PublicKey ) => anchor.web3.PublicKey.findProgramAddress( [euroMetaKey.toBuffer(), textEncoder.encode("putWriterMint")], program.programId ); export const deriveCreateEuroMetaAccounts = async ( program: Program, underlyingMint: anchor.web3.PublicKey, expiration: anchor.BN, underlyingAmountPerContract: anchor.BN, strikePrice: anchor.BN, priceDecimals: number, pythOracleAddress: anchor.web3.PublicKey, stableMint: anchor.web3.PublicKey ) => { const [euroMetaKey, euroBump] = await deriveEuroMeta( program, underlyingMint, stableMint, expiration, underlyingAmountPerContract, strikePrice, priceDecimals ); const [poolAuthority] = await anchor.web3.PublicKey.findProgramAddress( [textEncoder.encode("poolAuthority")], program.programId ); const [underlyingPoolKey] = await deriveUnderlyingPoolKey( program, underlyingMint ); const [stablePoolKey] = await deriveStablePoolKey(program, stableMint); const [expirationDataKey, expirationDataBump] = await deriveExpirationData( program, underlyingMint, expiration, pythOracleAddress, priceDecimals ); const [callOptionMintKey] = await deriveCallOptionMint(program, euroMetaKey); const [callWriterMintKey] = await deriveCallWriterMint(program, euroMetaKey); const [putOptionMintKey] = await derivePutOptionMint(program, euroMetaKey); const [putWriterMintKey] = await derivePutWriterMint(program, euroMetaKey); return { euroMetaKey, euroBump, expirationDataKey, expirationDataBump, poolAuthority, underlyingMint, underlyingPool: underlyingPoolKey, stableMint, stablePool: stablePoolKey, pythOracle: pythOracleAddress, callOptionMint: callOptionMintKey, callWriterMint: callWriterMintKey, putOptionMint: putOptionMintKey, putWriterMint: putWriterMintKey, }; }; export const initializeEuroMeta = async ( program: Program, underlyingMint: anchor.web3.PublicKey, expiration: anchor.BN, underlyingAmountPerContract: anchor.BN, strikePrice: anchor.BN, priceDecimals: number, pythOracleAddress: anchor.web3.PublicKey, userWalletKeypair, stableMint: anchor.web3.PublicKey ) => { const euroMeta = await deriveCreateEuroMetaAccounts( program, underlyingMint, expiration, underlyingAmountPerContract, strikePrice, priceDecimals, pythOracleAddress, stableMint ); await program.rpc.initExpirationData(expiration, priceDecimals, { accounts: { payer: userWalletKeypair.publicKey, underlyingMint: euroMeta.underlyingMint, expirationData: euroMeta.expirationDataKey, pythOracle: euroMeta.pythOracle, tokenProgram: TOKEN_PROGRAM_ID, rent: SYSVAR_RENT_PUBKEY, systemProgram: SystemProgram.programId, }, signers: [userWalletKeypair], }); await program.rpc.initializeStablePool({ accounts: { payer: userWalletKeypair.publicKey, stableMint: euroMeta.stableMint, stablePool: euroMeta.stablePool, poolAuthority: euroMeta.poolAuthority, tokenProgram: TOKEN_PROGRAM_ID, rent: SYSVAR_RENT_PUBKEY, systemProgram: SystemProgram.programId, }, signers: [userWalletKeypair], }); await program.rpc.initializeUnderlyingPool({ accounts: { payer: userWalletKeypair.publicKey, underlyingMint: euroMeta.underlyingMint, underlyingPool: euroMeta.underlyingPool, poolAuthority: euroMeta.poolAuthority, tokenProgram: TOKEN_PROGRAM_ID, rent: SYSVAR_RENT_PUBKEY, systemProgram: SystemProgram.programId, }, signers: [userWalletKeypair], }); await program.rpc.createEuroMeta( underlyingAmountPerContract, expiration, strikePrice, priceDecimals, euroMeta.euroBump, euroMeta.expirationDataBump, { accounts: { payer: userWalletKeypair.publicKey, underlyingMint: euroMeta.underlyingMint, underlyingPool: euroMeta.underlyingPool, stableMint: euroMeta.stableMint, stablePool: euroMeta.stablePool, euroMeta: euroMeta.euroMetaKey, expirationData: euroMeta.expirationDataKey, callOptionMint: euroMeta.callOptionMint, callWriterMint: euroMeta.callWriterMint, putOptionMint: euroMeta.putOptionMint, putWriterMint: euroMeta.putWriterMint, pythOracle: euroMeta.pythOracle, tokenProgram: TOKEN_PROGRAM_ID, rent: SYSVAR_RENT_PUBKEY, systemProgram: SystemProgram.programId, }, signers: [userWalletKeypair], } ); return euroMeta; }; export const deriveDepositReceipt = async ( program: Program, userAuthority: anchor.web3.PublicKey, vaultAccount: String, epoch: String = "1" ) => { return await anchor.web3.PublicKey.findProgramAddress( [ userAuthority.toBuffer(), new PublicKey(vaultAccount).toBuffer(), epoch, "DepositReceipt", ], program.programId ); }; export const deriveWithdrawalReceipt = async ( program: Program, userAuthority: anchor.web3.PublicKey, vaultAccount: String, epoch: String = "1" ) => { return await anchor.web3.PublicKey.findProgramAddress( [ userAuthority.toBuffer(), new PublicKey(vaultAccount).toBuffer(), epoch, "WithdrawalReceipt", ], program.programId ); }; export const deriveOpenOrdersAddress = async ( program: Program, dexProgramId: PublicKey, serumMarket: PublicKey, authority: PublicKey ) => { return await anchor.web3.PublicKey.findProgramAddress( [ textEncoder.encode("open-orders"), dexProgramId.toBuffer(), serumMarket.toBuffer(), authority.toBuffer(), ], program.programId ); }; export const deriveVaultAuthority = async ( program, vaultAccount: PublicKey ) => { return await anchor.web3.PublicKey.findProgramAddress( [vaultAccount.toBuffer(), textEncoder.encode("VaultAuthority")], program.programId ); }; export const assertParsedError = (err: any, msg: String) => { const programError = parseTranactionError(err); if (programError != null) { assert.equal(programError.msg, msg); } }; export const createAssociatedTokenInstruction = async ( program: Program, mint: PublicKey, owner: PublicKey | undefined = undefined, allowOwnerOffCurve = false ) => { const associatedAddress = await Token.getAssociatedTokenAddress( ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, mint, owner || program.provider.wallet.publicKey, allowOwnerOffCurve ); const instruction = Token.createAssociatedTokenAccountInstruction( ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID, mint, associatedAddress, owner || program.provider.wallet.publicKey, program.provider.wallet.publicKey ); return { instruction, associatedAddress }; }; export const createAssociatedTokenAccount = async ( program: Program, mint: PublicKey, owner: PublicKey | undefined = undefined, allowOwnerOffCurve = false ) => { const { instruction, associatedAddress } = await createAssociatedTokenInstruction( program, mint, owner, allowOwnerOffCurve ); const tx = new Transaction(); tx.add(instruction); await program.provider.send(tx); return associatedAddress; };