|
| 1 | +import { AnchorProvider, Wallet } from '@coral-xyz/anchor' |
| 2 | +import { Connection, Keypair, PublicKey } from '@solana/web3.js' |
| 3 | +import { BN } from 'bn.js' |
| 4 | +import { getPythProgramKeyForCluster, pythOracleProgram, PythOracleCoder } from '../index' |
| 5 | + |
| 6 | +test('Anchor', (done) => { |
| 7 | + jest.setTimeout(60000) |
| 8 | + const provider = new AnchorProvider( |
| 9 | + new Connection('https://api.mainnet-beta.solana.com'), |
| 10 | + new Wallet(new Keypair()), |
| 11 | + AnchorProvider.defaultOptions(), |
| 12 | + ) |
| 13 | + const pythOracle = pythOracleProgram(getPythProgramKeyForCluster('mainnet-beta'), provider) |
| 14 | + pythOracle.methods |
| 15 | + .initMapping() |
| 16 | + .accounts({ fundingAccount: PublicKey.unique(), freshMappingAccount: PublicKey.unique() }) |
| 17 | + .instruction() |
| 18 | + .then((instruction) => { |
| 19 | + expect(instruction.data).toStrictEqual(Buffer.from([2, 0, 0, 0, 0, 0, 0, 0])) |
| 20 | + const decoded = (pythOracle.coder as PythOracleCoder).instruction.decode(instruction.data) |
| 21 | + expect(decoded?.name).toBe('initMapping') |
| 22 | + expect(decoded?.data).toStrictEqual({}) |
| 23 | + }) |
| 24 | + pythOracle.methods |
| 25 | + .addMapping() |
| 26 | + .accounts({ fundingAccount: PublicKey.unique(), curMapping: PublicKey.unique(), nextMapping: PublicKey.unique() }) |
| 27 | + .instruction() |
| 28 | + .then((instruction) => { |
| 29 | + expect(instruction.data).toStrictEqual(Buffer.from([2, 0, 0, 0, 1, 0, 0, 0])) |
| 30 | + const decoded = (pythOracle.coder as PythOracleCoder).instruction.decode(instruction.data) |
| 31 | + expect(decoded?.name).toBe('addMapping') |
| 32 | + expect(decoded?.data).toStrictEqual({}) |
| 33 | + }) |
| 34 | + pythOracle.methods |
| 35 | + .updProduct() |
| 36 | + .accounts({ fundingAccount: PublicKey.unique(), productAccount: PublicKey.unique() }) |
| 37 | + .instruction() |
| 38 | + .then((instruction) => { |
| 39 | + expect(instruction.data).toStrictEqual(Buffer.from([2, 0, 0, 0, 3, 0, 0, 0])) |
| 40 | + const decoded = (pythOracle.coder as PythOracleCoder).instruction.decode(instruction.data) |
| 41 | + expect(decoded?.name).toBe('updProduct') |
| 42 | + expect(decoded?.data).toStrictEqual({}) |
| 43 | + }) |
| 44 | + |
| 45 | + pythOracle.methods |
| 46 | + .addPrice(1, 1) |
| 47 | + .accounts({ fundingAccount: PublicKey.unique(), productAccount: PublicKey.unique() }) |
| 48 | + .instruction() |
| 49 | + .then((instruction) => { |
| 50 | + expect(instruction.data).toStrictEqual(Buffer.from([2, 0, 0, 0, 4, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0])) |
| 51 | + const decoded = (pythOracle.coder as PythOracleCoder).instruction.decode(instruction.data) |
| 52 | + expect(decoded?.name).toBe('addPrice') |
| 53 | + expect(decoded?.data).toStrictEqual({ expo: 1, pType: 1 }) |
| 54 | + }) |
| 55 | + |
| 56 | + pythOracle.methods |
| 57 | + .addPublisher(new PublicKey(5)) |
| 58 | + .accounts({ fundingAccount: PublicKey.unique(), priceAccount: PublicKey.unique() }) |
| 59 | + .instruction() |
| 60 | + .then((instruction) => { |
| 61 | + expect(instruction.data).toStrictEqual( |
| 62 | + Buffer.from([ |
| 63 | + 2, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 64 | + 0, 0, 5, |
| 65 | + ]), |
| 66 | + ) |
| 67 | + const decoded = (pythOracle.coder as PythOracleCoder).instruction.decode(instruction.data) |
| 68 | + expect(decoded?.name).toBe('addPublisher') |
| 69 | + expect(decoded?.data.pub.equals(new PublicKey(5))).toBeTruthy() |
| 70 | + }) |
| 71 | + |
| 72 | + pythOracle.methods |
| 73 | + .updPrice(1, 0, new BN(42), new BN(9), new BN(1)) |
| 74 | + .accounts({ fundingAccount: PublicKey.unique(), priceAccount: PublicKey.unique() }) |
| 75 | + .instruction() |
| 76 | + .then((instruction) => { |
| 77 | + expect(instruction.data).toStrictEqual( |
| 78 | + Buffer.from([ |
| 79 | + 2, 0, 0, 0, 7, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, |
| 80 | + 0, 0, 0, 0, |
| 81 | + ]), |
| 82 | + ) |
| 83 | + const decoded = (pythOracle.coder as PythOracleCoder).instruction.decode(instruction.data) |
| 84 | + expect(decoded?.name).toBe('updPrice') |
| 85 | + expect(decoded?.data.status === 1).toBeTruthy() |
| 86 | + expect(decoded?.data.price.eq(new BN(42))).toBeTruthy() |
| 87 | + expect(decoded?.data.conf.eq(new BN(9))).toBeTruthy() |
| 88 | + expect(decoded?.data.pubSlot.eq(new BN(1))).toBeTruthy() |
| 89 | + }) |
| 90 | + |
| 91 | + pythOracle.methods |
| 92 | + .updPriceNoFailOnError(1, 0, new BN(42), new BN(9), new BN(1)) |
| 93 | + .accounts({ fundingAccount: PublicKey.unique(), priceAccount: PublicKey.unique() }) |
| 94 | + .instruction() |
| 95 | + .then((instruction) => { |
| 96 | + expect(instruction.data).toStrictEqual( |
| 97 | + Buffer.from([ |
| 98 | + 2, 0, 0, 0, 13, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, |
| 99 | + 0, 0, 0, 0, |
| 100 | + ]), |
| 101 | + ) |
| 102 | + const decoded = (pythOracle.coder as PythOracleCoder).instruction.decode(instruction.data) |
| 103 | + expect(decoded?.name).toBe('updPriceNoFailOnError') |
| 104 | + expect(decoded?.data.status === 1).toBeTruthy() |
| 105 | + expect(decoded?.data.price.eq(new BN(42))).toBeTruthy() |
| 106 | + expect(decoded?.data.conf.eq(new BN(9))).toBeTruthy() |
| 107 | + expect(decoded?.data.pubSlot.eq(new BN(1))).toBeTruthy() |
| 108 | + }) |
| 109 | + |
| 110 | + pythOracle.methods |
| 111 | + .aggPrice(1, 0, new BN(42), new BN(9), new BN(1)) |
| 112 | + .accounts({ fundingAccount: PublicKey.unique(), priceAccount: PublicKey.unique() }) |
| 113 | + .instruction() |
| 114 | + .then((instruction) => { |
| 115 | + expect(instruction.data).toStrictEqual( |
| 116 | + Buffer.from([ |
| 117 | + 2, 0, 0, 0, 8, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 42, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, |
| 118 | + 0, 0, 0, 0, |
| 119 | + ]), |
| 120 | + ) |
| 121 | + const decoded = (pythOracle.coder as PythOracleCoder).instruction.decode(instruction.data) |
| 122 | + expect(decoded?.name).toBe('aggPrice') |
| 123 | + expect(decoded?.data.status === 1).toBeTruthy() |
| 124 | + expect(decoded?.data.price.eq(new BN(42))).toBeTruthy() |
| 125 | + expect(decoded?.data.conf.eq(new BN(9))).toBeTruthy() |
| 126 | + expect(decoded?.data.pubSlot.eq(new BN(1))).toBeTruthy() |
| 127 | + }) |
| 128 | + |
| 129 | + pythOracle.methods |
| 130 | + .setMinPub(5, [0, 0, 0]) |
| 131 | + .accounts({ fundingAccount: PublicKey.unique(), priceAccount: PublicKey.unique() }) |
| 132 | + .instruction() |
| 133 | + .then((instruction) => { |
| 134 | + expect(instruction.data).toStrictEqual(Buffer.from([2, 0, 0, 0, 12, 0, 0, 0, 5, 0, 0, 0])) |
| 135 | + const decoded = (pythOracle.coder as PythOracleCoder).instruction.decode(instruction.data) |
| 136 | + expect(decoded?.name).toBe('setMinPub') |
| 137 | + expect(decoded?.data.minPub === 5).toBeTruthy() |
| 138 | + }) |
| 139 | + |
| 140 | + pythOracle.methods |
| 141 | + .updPermissions(new PublicKey(6), new PublicKey(7), new PublicKey(8)) |
| 142 | + .accounts({ |
| 143 | + upgradeAuthority: PublicKey.unique(), |
| 144 | + programAccount: PublicKey.unique(), |
| 145 | + programDataAccount: PublicKey.unique(), |
| 146 | + }) |
| 147 | + .instruction() |
| 148 | + .then((instruction) => { |
| 149 | + const expectedPda = PublicKey.findProgramAddressSync([Buffer.from('permissions')], pythOracle.programId)[0] |
| 150 | + expect(expectedPda.equals(instruction.keys[3].pubkey)) |
| 151 | + expect(instruction.data).toStrictEqual( |
| 152 | + Buffer.from([ |
| 153 | + 2, 0, 0, 0, 17, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 154 | + 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, |
| 155 | + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, |
| 156 | + ]), |
| 157 | + ) |
| 158 | + const decoded = (pythOracle.coder as PythOracleCoder).instruction.decode(instruction.data) |
| 159 | + expect(decoded?.name).toBe('updPermissions') |
| 160 | + expect(decoded?.data.masterAuthority.equals(new PublicKey(6))).toBeTruthy() |
| 161 | + expect(decoded?.data.dataCurationAuthority.equals(new PublicKey(7))).toBeTruthy() |
| 162 | + expect(decoded?.data.securityAuthority.equals(new PublicKey(8))).toBeTruthy() |
| 163 | + }) |
| 164 | + |
| 165 | + done() |
| 166 | +}) |
0 commit comments