|
| 1 | +import { AnchorProvider, Wallet } from "@project-serum/anchor"; |
| 2 | +import { pythOracleProgram } from "@pythnetwork/client"; |
| 3 | +import { |
| 4 | + getPythClusterApiUrl, |
| 5 | + getPythProgramKeyForCluster, |
| 6 | + PythCluster, |
| 7 | +} from "@pythnetwork/client/lib/cluster"; |
| 8 | +import { Connection, Keypair, PublicKey } from "@solana/web3.js"; |
| 9 | +import { MultisigInstructionProgram, MultisigParser } from ".."; |
| 10 | +import { PythMultisigInstruction } from "../multisig_transaction/PythMultisigInstruction"; |
| 11 | + |
| 12 | +test("Pyth multisig instruction parse: create price account", (done) => { |
| 13 | + jest.setTimeout(60000); |
| 14 | + |
| 15 | + const cluster: PythCluster = "devnet"; |
| 16 | + const pythProgram = pythOracleProgram( |
| 17 | + getPythProgramKeyForCluster(cluster), |
| 18 | + new AnchorProvider( |
| 19 | + new Connection(getPythClusterApiUrl(cluster)), |
| 20 | + new Wallet(new Keypair()), |
| 21 | + AnchorProvider.defaultOptions() |
| 22 | + ) |
| 23 | + ); |
| 24 | + const parser = MultisigParser.fromCluster(cluster); |
| 25 | + |
| 26 | + pythProgram.methods |
| 27 | + .addPrice(-8, 1) |
| 28 | + .accounts({ |
| 29 | + fundingAccount: PublicKey.unique(), |
| 30 | + productAccount: PublicKey.unique(), |
| 31 | + priceAccount: PublicKey.unique(), |
| 32 | + }) |
| 33 | + .instruction() |
| 34 | + .then((instruction) => { |
| 35 | + const parsedInstruction = parser.parseInstruction(instruction); |
| 36 | + |
| 37 | + if (parsedInstruction instanceof PythMultisigInstruction) { |
| 38 | + expect(parsedInstruction.program).toBe( |
| 39 | + MultisigInstructionProgram.PythOracle |
| 40 | + ); |
| 41 | + expect(parsedInstruction.name).toBe("addPrice"); |
| 42 | + expect( |
| 43 | + parsedInstruction.accounts.named["fundingAccount"].pubkey.equals( |
| 44 | + instruction.keys[0].pubkey |
| 45 | + ) |
| 46 | + ).toBeTruthy(); |
| 47 | + expect( |
| 48 | + parsedInstruction.accounts.named["fundingAccount"].isSigner |
| 49 | + ).toBe(instruction.keys[0].isSigner); |
| 50 | + expect( |
| 51 | + parsedInstruction.accounts.named["fundingAccount"].isWritable |
| 52 | + ).toBe(instruction.keys[0].isWritable); |
| 53 | + console.log(parsedInstruction.accounts.named["productAccount"]); |
| 54 | + expect( |
| 55 | + parsedInstruction.accounts.named["productAccount"].pubkey.equals( |
| 56 | + instruction.keys[1].pubkey |
| 57 | + ) |
| 58 | + ).toBeTruthy(); |
| 59 | + expect( |
| 60 | + parsedInstruction.accounts.named["productAccount"].isSigner |
| 61 | + ).toBe(instruction.keys[1].isSigner); |
| 62 | + expect( |
| 63 | + parsedInstruction.accounts.named["productAccount"].isWritable |
| 64 | + ).toBe(instruction.keys[1].isWritable); |
| 65 | + expect( |
| 66 | + parsedInstruction.accounts.named["priceAccount"].pubkey.equals( |
| 67 | + instruction.keys[2].pubkey |
| 68 | + ) |
| 69 | + ).toBeTruthy(); |
| 70 | + expect(parsedInstruction.accounts.named["priceAccount"].isSigner).toBe( |
| 71 | + instruction.keys[2].isSigner |
| 72 | + ); |
| 73 | + expect( |
| 74 | + parsedInstruction.accounts.named["priceAccount"].isWritable |
| 75 | + ).toBe(instruction.keys[2].isWritable); |
| 76 | + expect( |
| 77 | + parsedInstruction.accounts.named["permissionsAccount"].pubkey.equals( |
| 78 | + instruction.keys[3].pubkey |
| 79 | + ) |
| 80 | + ).toBeTruthy(); |
| 81 | + expect( |
| 82 | + parsedInstruction.accounts.named["permissionsAccount"].isSigner |
| 83 | + ).toBe(instruction.keys[3].isSigner); |
| 84 | + expect( |
| 85 | + parsedInstruction.accounts.named["permissionsAccount"].isWritable |
| 86 | + ).toBe(instruction.keys[3].isWritable); |
| 87 | + expect(parsedInstruction.accounts.remaining.length).toBe(0); |
| 88 | + |
| 89 | + expect(parsedInstruction.args.expo).toBe(-8); |
| 90 | + expect(parsedInstruction.args.pType).toBe(1); |
| 91 | + done(); |
| 92 | + } else { |
| 93 | + done("Not instance of PythMultisigInstruction"); |
| 94 | + } |
| 95 | + }); |
| 96 | +}); |
| 97 | + |
| 98 | +test("Pyth multisig instruction parse: set minimum publishers", (done) => { |
| 99 | + jest.setTimeout(60000); |
| 100 | + |
| 101 | + const cluster: PythCluster = "devnet"; |
| 102 | + const pythProgram = pythOracleProgram( |
| 103 | + getPythProgramKeyForCluster(cluster), |
| 104 | + new AnchorProvider( |
| 105 | + new Connection(getPythClusterApiUrl(cluster)), |
| 106 | + new Wallet(new Keypair()), |
| 107 | + AnchorProvider.defaultOptions() |
| 108 | + ) |
| 109 | + ); |
| 110 | + const parser = MultisigParser.fromCluster(cluster); |
| 111 | + |
| 112 | + pythProgram.methods |
| 113 | + .setMinPub(25, [0, 0, 0]) |
| 114 | + .accounts({ |
| 115 | + fundingAccount: PublicKey.unique(), |
| 116 | + priceAccount: PublicKey.unique(), |
| 117 | + }) |
| 118 | + .instruction() |
| 119 | + .then((instruction) => { |
| 120 | + const parsedInstruction = parser.parseInstruction(instruction); |
| 121 | + |
| 122 | + if (parsedInstruction instanceof PythMultisigInstruction) { |
| 123 | + expect(parsedInstruction.program).toBe( |
| 124 | + MultisigInstructionProgram.PythOracle |
| 125 | + ); |
| 126 | + expect(parsedInstruction.name).toBe("setMinPub"); |
| 127 | + expect( |
| 128 | + parsedInstruction.accounts.named["fundingAccount"].pubkey.equals( |
| 129 | + instruction.keys[0].pubkey |
| 130 | + ) |
| 131 | + ).toBeTruthy(); |
| 132 | + expect( |
| 133 | + parsedInstruction.accounts.named["fundingAccount"].isSigner |
| 134 | + ).toBe(instruction.keys[0].isSigner); |
| 135 | + expect( |
| 136 | + parsedInstruction.accounts.named["fundingAccount"].isWritable |
| 137 | + ).toBe(instruction.keys[0].isWritable); |
| 138 | + expect( |
| 139 | + parsedInstruction.accounts.named["priceAccount"].pubkey.equals( |
| 140 | + instruction.keys[1].pubkey |
| 141 | + ) |
| 142 | + ).toBeTruthy(); |
| 143 | + expect(parsedInstruction.accounts.named["priceAccount"].isSigner).toBe( |
| 144 | + instruction.keys[1].isSigner |
| 145 | + ); |
| 146 | + expect( |
| 147 | + parsedInstruction.accounts.named["priceAccount"].isWritable |
| 148 | + ).toBe(instruction.keys[1].isWritable); |
| 149 | + expect( |
| 150 | + parsedInstruction.accounts.named["permissionsAccount"].pubkey.equals( |
| 151 | + instruction.keys[2].pubkey |
| 152 | + ) |
| 153 | + ).toBeTruthy(); |
| 154 | + expect( |
| 155 | + parsedInstruction.accounts.named["permissionsAccount"].isSigner |
| 156 | + ).toBe(instruction.keys[2].isSigner); |
| 157 | + expect( |
| 158 | + parsedInstruction.accounts.named["permissionsAccount"].isWritable |
| 159 | + ).toBe(instruction.keys[2].isWritable); |
| 160 | + expect(parsedInstruction.accounts.remaining.length).toBe(0); |
| 161 | + expect(parsedInstruction.args.minPub).toBe(25); |
| 162 | + done(); |
| 163 | + } else { |
| 164 | + done("Not instance of PythMultisigInstruction"); |
| 165 | + } |
| 166 | + }); |
| 167 | +}); |
0 commit comments