|
1 | 1 | import { |
| 2 | + ComputeBudgetProgram, |
2 | 3 | Connection, |
| 4 | + Keypair, |
3 | 5 | PublicKey, |
| 6 | + TransactionMessage, |
| 7 | + VersionedTransaction, |
4 | 8 | } from "@solana/web3.js"; |
5 | 9 | import { parseObligation } from "../src"; |
| 10 | +import { PriceServiceConnection } from "@pythnetwork/price-service-client"; |
| 11 | +import { PythSolanaReceiver, pythSolanaReceiverIdl } from "@pythnetwork/pyth-solana-receiver"; |
| 12 | +import NodeWallet from "@coral-xyz/anchor/dist/cjs/nodewallet"; |
| 13 | +import { AnchorProvider, Program } from "@coral-xyz/anchor-30"; |
| 14 | +import { CrossbarClient, loadLookupTables, PullFeed, SB_ON_DEMAND_PID } from "@switchboard-xyz/on-demand"; |
6 | 15 |
|
7 | 16 | jest.setTimeout(50_000); |
8 | 17 |
|
9 | 18 | describe("check", function () { |
10 | 19 | it("parses obligation in both formats", async function () { |
11 | | - const zstdEncodedObligationData = Buffer.from( |
12 | | - "KLUv/QBYLQoAdBEBWZHIBwAAAAABM7MexO/4+iia6oyVTAFjLi12SQjOVE1oZb3vERv/YSsChcZ+Ktz1mRBrkyRk1BwBK+MRLDs2kN6sQJrkE/068ae2QKj1GoAIagEAUiLO4HOtZQD9iDA+OBRghg/ZNGo1t+NsujMBAQFsp+C1qN6toMtzc12/wVf8DODwcnh4K7b1j6DEElE6V8esAGvgw8KAVqMAbcvwdUngHTQhP3KK2EcTtbcXhy+sOBPKuWOvMSyN283/9ZTFWnxCDgDvyiK06ZHhKd04ANAh+wRuI+ENjqkOBTFIeS9ID9NTKr93sO+769EA4idmIqc8x1Enq7K/tz3DwaHWY5RMvzQOIJZ5G4lBjXXPHRJX7vzRwA8SAM1yFQDEt5APgChdqyVY2VtAAS80LATg+oCsBCUWCFQgLRhoqHwAcXWBMw==", |
13 | | - "base64" |
14 | | - ); |
15 | | - const base64EncodedObligationData = Buffer.from( |
16 | | - "AVmRyAcAAAAAATOzHsTv+PoomuqMlUwBYy4tdk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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==", |
17 | | - "base64" |
18 | | - ); |
19 | | - |
20 | | - const obligationPubkey = new PublicKey( |
21 | | - "FfRLBU1gHm3MyqJ3KX6dBnsxxJtwVGCwKFwJrfDnceWN" |
22 | | - ); |
23 | | - const connection = new Connection("https://api.mainnet-beta.solana.com", { |
24 | | - commitment: "finalized", |
25 | | - }); |
26 | | - |
27 | | - const zstdEncodedObligation = await connection.getAccountInfo( |
28 | | - obligationPubkey |
29 | | - ); |
30 | | - zstdEncodedObligation!.data = zstdEncodedObligationData; |
31 | | - const base64EncodedObligation = await connection.getAccountInfo( |
32 | | - obligationPubkey |
33 | | - ); |
34 | | - base64EncodedObligation!.data = base64EncodedObligationData; |
35 | | - |
36 | | - const parsedzstdEncodedObligation = parseObligation( |
37 | | - obligationPubkey, |
38 | | - zstdEncodedObligation!, |
39 | | - "base64+zstd" |
40 | | - ); |
41 | | - const parsedbase64EncodedObligation = parseObligation( |
42 | | - obligationPubkey, |
43 | | - base64EncodedObligation! |
44 | | - ); |
45 | | - |
46 | | - expect(parsedzstdEncodedObligation!).toMatchObject( |
47 | | - parsedbase64EncodedObligation! |
48 | | - ); |
| 20 | + const connection = new Connection("https://solendf-solendf-67c7.rpcpool.com/6096fc4b-78fc-4130-a42a-e6d4b9c37813"); |
| 21 | + // const priceServiceConnection = new PriceServiceConnection("https://hermes.pyth.network"); |
| 22 | + // const pythSolanaReceiver = new PythSolanaReceiver({ |
| 23 | + // connection: connection, |
| 24 | + // wallet: new NodeWallet(Keypair.fromSeed(new Uint8Array(32).fill(1))) |
| 25 | + // }); |
| 26 | + // const transactionBuilder = pythSolanaReceiver.newTransactionBuilder({ |
| 27 | + // closeUpdateAccounts: true, |
| 28 | + // }); |
| 29 | + |
| 30 | + const provider = new AnchorProvider(connection, new NodeWallet(Keypair.fromSecretKey(new Uint8Array( |
| 31 | + [103,129,37,223,47,99,63,95,41,95,190,254,216,11,20,217,101,217,65,173,210,207,137,44,176,136,93,84,15,87,212,78,254,157,169,160,244,214,188,162,227,202,121,33,107,191,169,212,16,93,176,86,248,238,250,64,147,23,50,212,180,223,6,63] |
| 32 | + ))), {}); |
| 33 | + const idl = (await Program.fetchIdl(SB_ON_DEMAND_PID, provider))!; |
| 34 | + const sbod = new Program(idl, provider); |
| 35 | + |
| 36 | + const sbPulledOracles = [ |
| 37 | + '2F9M59yYc28WMrAymNWceaBEk8ZmDAjUAKULp8seAJF3', |
| 38 | + 'AZcoqpWhMJUaKEDUfKsfzCr3Y96gSQwv43KSQ6KpeyQ1' |
| 39 | + ]; |
| 40 | + const feedAccounts = sbPulledOracles.map((oracleKey) => new PullFeed(sbod as any, oracleKey)); |
| 41 | + const crossbar = new CrossbarClient("https://crossbar.switchboard.xyz"); |
| 42 | + |
| 43 | + // Responses is Array<[pullIx, responses, success]> |
| 44 | + const responses = await Promise.all(feedAccounts.map((feedAccount) => feedAccount.fetchUpdateIx({ numSignatures: 1, crossbarClient: crossbar }))); |
| 45 | + const oracles = responses.flatMap((x) => x[1].map(y => y.oracle)); |
| 46 | + const lookupTables = await loadLookupTables([...oracles, ...feedAccounts]); |
| 47 | + |
| 48 | + console.log(responses); |
| 49 | + |
| 50 | + // Get the latest context |
| 51 | + const { |
| 52 | + value: { blockhash }, |
| 53 | + } = await connection.getLatestBlockhashAndContext(); |
| 54 | + |
| 55 | + // Get Transaction Message |
| 56 | + const message = new TransactionMessage({ |
| 57 | + payerKey: provider.publicKey, |
| 58 | + recentBlockhash: blockhash, |
| 59 | + instructions: [...responses.map(r => r[0]!)], |
| 60 | + }).compileToV0Message(lookupTables); |
| 61 | + |
| 62 | + // Get Versioned Transaction |
| 63 | + const vtx = new VersionedTransaction(message); |
| 64 | + provider.wallet.signAllTransactions([vtx]); |
| 65 | + const sig = await connection.sendRawTransaction(vtx.serialize(), {skipPreflight: true}); |
| 66 | + await connection.confirmTransaction(sig, 'confirmed'); |
| 67 | + // let priceFeedUpdateData; |
| 68 | + // priceFeedUpdateData = await priceServiceConnection.getLatestVaas( |
| 69 | + // [ |
| 70 | + // 'ef0d8b6fda2ceba41da15d4095d1da392a0d2f8ed0c6c7bc0f4cfac8c280b56d', |
| 71 | + // '67be9f519b95cf24338801051f9a808eff0a578ccb388db73b7f6fe1de019ffb', |
| 72 | + // 'c2289a6a43d2ce91c6f55caec370f4acc38a2ed477f58813334c6d03749ff2a4', |
| 73 | + // '89875379e70f8fbadc17aef315adf3a8d5d160b811435537e03c97e8aac97d9c', |
| 74 | + // '72b021217ca3fe68922a19aaf990109cb9d84e9ad004b4d2025ad6f529314419', |
| 75 | + // 'eff7446475e218517566ea99e72a4abec2e1bd8498b43b7d8331e29dcb059389' |
| 76 | + // ] |
| 77 | + // ); |
| 78 | + |
| 79 | + // await transactionBuilder.addUpdatePriceFeed( |
| 80 | + // priceFeedUpdateData, |
| 81 | + // 0 // shardId of 0 |
| 82 | + // ); |
| 83 | + // await transactionBuilder.addPostPriceUpdates(priceFeedUpdateData); |
| 84 | + |
| 85 | + // const transactionsWithSigners = await transactionBuilder.buildVersionedTransactions({ |
| 86 | + // tightComputeBudget: true, |
| 87 | + // }); |
| 88 | + |
| 89 | + // console.log(transactionsWithSigners); |
| 90 | + // console.log(transactionsWithSigners.map(t => t.tx.message.compiledInstructions)); |
| 91 | + |
| 92 | + |
49 | 93 | }); |
| 94 | + |
| 95 | + // it("parses obligation in both formats", async function () { |
| 96 | + // const connection = new Connection("https://solendf-solendf-67c7.rpcpool.com/6096fc4b-78fc-4130-a42a-e6d4b9c37813"); |
| 97 | + // const priceServiceConnection = new PriceServiceConnection("https://hermes.pyth.network"); |
| 98 | + // const pythSolanaReceiver = new PythSolanaReceiver({ |
| 99 | + // connection: connection, |
| 100 | + // wallet: new NodeWallet(Keypair.fromSecretKey(new Uint8Array( |
| 101 | + // [103,129,37,223,47,99,63,95,41,95,190,254,216,11,20,217,101,217,65,173,210,207,137,44,176,136,93,84,15,87,212,78,254,157,169,160,244,214,188,162,227,202,121,33,107,191,169,212,16,93,176,86,248,238,250,64,147,23,50,212,180,223,6,63] |
| 102 | + // ))) |
| 103 | + // }); |
| 104 | + // const transactionBuilder = pythSolanaReceiver.newTransactionBuilder({ |
| 105 | + // closeUpdateAccounts: true, |
| 106 | + // }); |
| 107 | + |
| 108 | + // const oracleAccount = await connection.getAccountInfo(new PublicKey('FFv5yoCGhEgWv6mXhwv4KX8A2dYcVAzi88a6Yu8Tf3iB')); |
| 109 | + |
| 110 | + // console.log(oracleAccount, pythSolanaReceiverIdl); |
| 111 | + // const priceUpdate = pythSolanaReceiver.receiver.account.priceUpdateV2.coder.accounts.decode( |
| 112 | + // 'priceUpdateV2', |
| 113 | + // oracleAccount!.data, |
| 114 | + // ); |
| 115 | + // const exponent = 10 ** priceUpdate.priceMessage.exponent; |
| 116 | + // const spotPrice = priceUpdate.priceMessage.price.toNumber() * exponent; |
| 117 | + // const emaPrice = priceUpdate.priceMessage.emaPrice.toNumber() * exponent; |
| 118 | + // console.log(spotPrice, emaPrice) |
| 119 | + // const priceFeedId = priceUpdate.priceMessage.feedId; |
| 120 | + |
| 121 | + // let priceFeedUpdateData; |
| 122 | + // priceFeedUpdateData = await priceServiceConnection.getLatestVaas( |
| 123 | + // [ |
| 124 | + // '0x93c3def9b169f49eed14c9d73ed0e942c666cf0e1290657ec82038ebb792c2a8', // BLZE |
| 125 | + // '0xf2fc1dfcf51867abfa70874c929e920edc649e4997cbac88f280094df8c72bcd', // EUROE |
| 126 | + // ] |
| 127 | + // ); |
| 128 | + |
| 129 | + // console.log(priceFeedUpdateData); |
| 130 | + // await transactionBuilder.addUpdatePriceFeed( |
| 131 | + // priceFeedUpdateData, |
| 132 | + // 0 // shardId of 0 |
| 133 | + // ); |
| 134 | + // // await transactionBuilder.addPostPriceUpdates(priceFeedUpdateData); |
| 135 | + |
| 136 | + // const transactionsWithSigners = await transactionBuilder.buildVersionedTransactions({ |
| 137 | + // tightComputeBudget: true, |
| 138 | + // }); |
| 139 | + |
| 140 | + // // transactionBuilder.addPriceConsumerInstructions |
| 141 | + |
| 142 | + |
| 143 | + // console.log(transactionsWithSigners); |
| 144 | + // const pullPriceTxns = [] as Array<VersionedTransaction>; |
| 145 | + // // TODO: Verify if there even is signers |
| 146 | + // for (const transaction of transactionsWithSigners) { |
| 147 | + // const signers = transaction.signers; |
| 148 | + // let tx = transaction.tx; |
| 149 | + |
| 150 | + // if (signers) { |
| 151 | + // tx.sign(signers); |
| 152 | + // pullPriceTxns.push(tx); |
| 153 | + // } |
| 154 | + // } |
| 155 | + |
| 156 | + // pythSolanaReceiver.wallet.signAllTransactions(pullPriceTxns) |
| 157 | + |
| 158 | + // for (const tx of pullPriceTxns) { |
| 159 | + // const serializedTransaction = tx.serialize(); |
| 160 | + // const sig = await connection.sendRawTransaction(serializedTransaction, {skipPreflight: true}); |
| 161 | + // await connection.confirmTransaction(sig, 'confirmed'); |
| 162 | + // } |
| 163 | + |
| 164 | + // }); |
50 | 165 | }); |
0 commit comments