|
| 1 | +import * as anchor from '@coral-xyz/anchor'; |
| 2 | +import { expect } from 'chai'; |
| 3 | +import { Program } from '@coral-xyz/anchor'; |
| 4 | +import { Keypair, LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js'; |
| 5 | +import { |
| 6 | + BN, |
| 7 | + TestClient, |
| 8 | + QUOTE_PRECISION, |
| 9 | + PRICE_PRECISION, |
| 10 | + PEG_PRECISION, |
| 11 | + OracleSource, |
| 12 | +} from '../sdk/src'; |
| 13 | +import { |
| 14 | + initializeQuoteSpotMarket, |
| 15 | + mockUSDCMint, |
| 16 | + mockUserUSDCAccount, |
| 17 | + mockOracleNoProgram, |
| 18 | + overWritePerpMarket, |
| 19 | +} from './testHelpers'; |
| 20 | +import { startAnchor } from 'solana-bankrun'; |
| 21 | +import { TestBulkAccountLoader } from '../sdk/src/accounts/testBulkAccountLoader'; |
| 22 | +import { BankrunContextWrapper } from '../sdk/src/bankrun/bankrunConnection'; |
| 23 | +import dotenv from 'dotenv'; |
| 24 | +import { |
| 25 | + CustomBorshAccountsCoder, |
| 26 | + CustomBorshCoder, |
| 27 | +} from '../sdk/src/decode/customCoder'; |
| 28 | +dotenv.config(); |
| 29 | + |
| 30 | +describe('Bankrun Overwrite Accounts', () => { |
| 31 | + const program = anchor.workspace.Drift as Program; |
| 32 | + // @ts-ignore |
| 33 | + program.coder.accounts = new CustomBorshAccountsCoder(program.idl); |
| 34 | + let bankrunContextWrapper: BankrunContextWrapper; |
| 35 | + let bulkAccountLoader: TestBulkAccountLoader; |
| 36 | + |
| 37 | + let adminClient: TestClient; |
| 38 | + let usdcMint: Keypair; |
| 39 | + let spotMarketOracle: PublicKey; |
| 40 | + |
| 41 | + const mantissaSqrtScale = new BN(Math.sqrt(PRICE_PRECISION.toNumber())); |
| 42 | + const ammInitialQuoteAssetReserve = new anchor.BN(10 * 10 ** 13).mul( |
| 43 | + mantissaSqrtScale |
| 44 | + ); |
| 45 | + const ammInitialBaseAssetReserve = new anchor.BN(10 * 10 ** 13).mul( |
| 46 | + mantissaSqrtScale |
| 47 | + ); |
| 48 | + |
| 49 | + let userUSDCAccount: Keypair; |
| 50 | + |
| 51 | + before(async () => { |
| 52 | + const context = await startAnchor( |
| 53 | + '', |
| 54 | + [ |
| 55 | + { |
| 56 | + name: 'serum_dex', |
| 57 | + programId: new PublicKey( |
| 58 | + 'srmqPvymJeFKQ4zGQed1GFppgkRHL9kaELCbyksJtPX' |
| 59 | + ), |
| 60 | + }, |
| 61 | + ], |
| 62 | + [] |
| 63 | + ); |
| 64 | + |
| 65 | + // @ts-ignore |
| 66 | + bankrunContextWrapper = new BankrunContextWrapper(context); |
| 67 | + |
| 68 | + bulkAccountLoader = new TestBulkAccountLoader( |
| 69 | + bankrunContextWrapper.connection, |
| 70 | + 'processed', |
| 71 | + 1 |
| 72 | + ); |
| 73 | + |
| 74 | + usdcMint = await mockUSDCMint(bankrunContextWrapper); |
| 75 | + spotMarketOracle = await mockOracleNoProgram(bankrunContextWrapper, 200.1); |
| 76 | + |
| 77 | + const keypair = new Keypair(); |
| 78 | + await bankrunContextWrapper.fundKeypair(keypair, 50 * LAMPORTS_PER_SOL); |
| 79 | + |
| 80 | + adminClient = new TestClient({ |
| 81 | + connection: bankrunContextWrapper.connection.toConnection(), |
| 82 | + wallet: new anchor.Wallet(keypair), |
| 83 | + programID: program.programId, |
| 84 | + opts: { |
| 85 | + commitment: 'confirmed', |
| 86 | + }, |
| 87 | + activeSubAccountId: 0, |
| 88 | + subAccountIds: [], |
| 89 | + perpMarketIndexes: [0, 1], |
| 90 | + spotMarketIndexes: [0, 1, 2], |
| 91 | + oracleInfos: [ |
| 92 | + { |
| 93 | + publicKey: spotMarketOracle, |
| 94 | + source: OracleSource.PYTH, |
| 95 | + }, |
| 96 | + ], |
| 97 | + accountSubscription: { |
| 98 | + type: 'polling', |
| 99 | + accountLoader: bulkAccountLoader, |
| 100 | + }, |
| 101 | + coder: new CustomBorshCoder(program.idl), |
| 102 | + }); |
| 103 | + await adminClient.initialize(usdcMint.publicKey, true); |
| 104 | + await adminClient.subscribe(); |
| 105 | + await initializeQuoteSpotMarket(adminClient, usdcMint.publicKey); |
| 106 | + |
| 107 | + userUSDCAccount = await mockUserUSDCAccount( |
| 108 | + usdcMint, |
| 109 | + new BN(10).mul(QUOTE_PRECISION), |
| 110 | + bankrunContextWrapper, |
| 111 | + keypair.publicKey |
| 112 | + ); |
| 113 | + |
| 114 | + await adminClient.initializeUserAccountAndDepositCollateral( |
| 115 | + new BN(10).mul(QUOTE_PRECISION), |
| 116 | + userUSDCAccount.publicKey |
| 117 | + ); |
| 118 | + |
| 119 | + const periodicity = new BN(0); |
| 120 | + |
| 121 | + await adminClient.initializePerpMarket( |
| 122 | + 0, |
| 123 | + spotMarketOracle, |
| 124 | + ammInitialBaseAssetReserve, |
| 125 | + ammInitialQuoteAssetReserve, |
| 126 | + periodicity, |
| 127 | + new BN(224 * PEG_PRECISION.toNumber()) |
| 128 | + ); |
| 129 | + }); |
| 130 | + |
| 131 | + after(async () => { |
| 132 | + await adminClient.unsubscribe(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should overwrite perp accounts', async () => { |
| 136 | + const perpMarket = adminClient.getPerpMarketAccount(0); |
| 137 | + |
| 138 | + const newBaseAssetAmount = new BN(123); |
| 139 | + perpMarket.amm.baseAssetAmountWithAmm = newBaseAssetAmount; |
| 140 | + |
| 141 | + await overWritePerpMarket( |
| 142 | + adminClient, |
| 143 | + bankrunContextWrapper, |
| 144 | + perpMarket.pubkey, |
| 145 | + perpMarket |
| 146 | + ); |
| 147 | + |
| 148 | + const updatedPerpMarket = adminClient.getPerpMarketAccount(0); |
| 149 | + expect(updatedPerpMarket.amm.baseAssetAmountWithAmm.eq(newBaseAssetAmount)) |
| 150 | + .to.be.true; |
| 151 | + }); |
| 152 | +}); |
0 commit comments