import * as anchor from '@project-serum/anchor' ; import { assert } from 'chai' ; import BN from 'bn.js' ; import { Program , Wallet } from '@project-serum/anchor' ; import { Keypair } from '@solana/web3.js' ; import { Admin , ClearingHouse , MAX_LEVERAGE , PositionDirection , } from '../sdk/src' ; import { Markets } from '../sdk/src/constants/markets' ; import { mockOracle , mockUSDCMint , mockUserUSDCAccount } from './testHelpers' ; import { FeeStructure } from '../sdk' ; const calculateTradeAmount = ( amountOfCollateral ) => { const ONE_MANTISSA = new BN ( 100000 ) ; const fee = ONE_MANTISSA . div ( new BN ( 1000 ) ) ; const tradeAmount = amountOfCollateral . mul ( MAX_LEVERAGE ) . mul ( ONE_MANTISSA . sub ( MAX_LEVERAGE . mul ( fee ) ) ) . div ( ONE_MANTISSA ) ; return tradeAmount ; } ; describe ( 'round in favor' , ( ) => { const provider = anchor . Provider . local ( ) ; const connection = provider . connection ; anchor . setProvider ( provider ) ; const chProgram = anchor . workspace . ClearingHouse as Program ; let usdcMint ; let primaryClearingHouse ; const ammInitialQuoteAssetReserve = new anchor . BN ( 17 * 10 ** 13 ) ; const ammInitialBaseAssetReserve = new anchor . BN ( 17 * 10 ** 13 ) ; const usdcAmount = new BN ( 9999 * 10 ** 3 ) ; before ( async ( ) => { usdcMint = await mockUSDCMint ( provider ) ; primaryClearingHouse = Admin . from ( connection , provider . wallet , chProgram . programId ) ; await primaryClearingHouse . initialize ( usdcMint . publicKey , true ) ; await primaryClearingHouse . subscribe ( ) ; const solUsd = await mockOracle ( 63000 ) ; const periodicity = new BN ( 60 * 60 ) ; await primaryClearingHouse . initializeMarket ( Markets [ 0 ] . marketIndex , solUsd , ammInitialBaseAssetReserve , ammInitialQuoteAssetReserve , periodicity , new BN ( 63000000 ) ) ; const newFeeStructure = { feeNumerator : new BN ( 0 ) , feeDenominator : new BN ( 1 ) , discountTokenTiers : { firstTier : { minimumBalance : new BN ( 1 ) , discountNumerator : new BN ( 1 ) , discountDenominator : new BN ( 1 ) , } , secondTier : { minimumBalance : new BN ( 1 ) , discountNumerator : new BN ( 1 ) , discountDenominator : new BN ( 1 ) , } , thirdTier : { minimumBalance : new BN ( 1 ) , discountNumerator : new BN ( 1 ) , discountDenominator : new BN ( 1 ) , } , fourthTier : { minimumBalance : new BN ( 1 ) , discountNumerator : new BN ( 1 ) , discountDenominator : new BN ( 1 ) , } , } , referralDiscount : { referrerRewardNumerator : new BN ( 1 ) , referrerRewardDenominator : new BN ( 1 ) , refereeDiscountNumerator : new BN ( 1 ) , refereeDiscountDenominator : new BN ( 1 ) , } , } ; await primaryClearingHouse . updateFee ( newFeeStructure ) ; } ) ; after ( async ( ) => { await primaryClearingHouse . unsubscribe ( ) ; } ) ; it ( 'short' , async ( ) => { const keypair = new Keypair ( ) ; await provider . connection . requestAirdrop ( keypair . publicKey , 10 ** 9 ) ; const wallet = new Wallet ( keypair ) ; const userUSDCAccount = await mockUserUSDCAccount ( usdcMint , usdcAmount , provider , keypair . publicKey ) ; const clearingHouse = ClearingHouse . from ( connection , wallet , chProgram . programId ) ; await clearingHouse . subscribe ( ) ; const [ , userAccountPublicKey ] = await clearingHouse . initializeUserAccountAndDepositCollateral ( usdcAmount , userUSDCAccount . publicKey ) ; const marketIndex = new BN ( 0 ) ; await clearingHouse . openPosition ( PositionDirection . SHORT , calculateTradeAmount ( usdcAmount ) , marketIndex , new BN ( 0 ) ) ; let user = await primaryClearingHouse . program . account . user . fetch ( userAccountPublicKey ) ; assert ( user . collateral . eq ( new BN ( 9999000 ) ) ) ; await clearingHouse . closePosition ( marketIndex ) ; user = await primaryClearingHouse . program . account . user . fetch ( userAccountPublicKey ) ; assert ( user . collateral . eq ( new BN ( 9999000 ) ) ) ; } ) ; it ( 'long' , async ( ) => { const keypair = new Keypair ( ) ; await provider . connection . requestAirdrop ( keypair . publicKey , 10 ** 9 ) ; const wallet = new Wallet ( keypair ) ; const userUSDCAccount = await mockUserUSDCAccount ( usdcMint , usdcAmount , provider , keypair . publicKey ) ; const clearingHouse = ClearingHouse . from ( connection , wallet , chProgram . programId ) ; await clearingHouse . subscribe ( ) ; const [ , userAccountPublicKey ] = await clearingHouse . initializeUserAccountAndDepositCollateral ( usdcAmount , userUSDCAccount . publicKey ) ; const marketIndex = new BN ( 0 ) ; await clearingHouse . openPosition ( PositionDirection . LONG , calculateTradeAmount ( usdcAmount ) , marketIndex , new BN ( 0 ) ) ; let user = await primaryClearingHouse . program . account . user . fetch ( userAccountPublicKey ) ; assert ( user . collateral . eq ( new BN ( 9999000 ) ) ) ; await clearingHouse . closePosition ( marketIndex ) ; user = await primaryClearingHouse . program . account . user . fetch ( userAccountPublicKey ) ; assert ( user . collateral . eq ( new BN ( 9998999 ) ) ) ; } ) ; } ) ;