|
| 1 | +import * as anchor from "@coral-xyz/anchor"; |
| 2 | +import { Program } from "@coral-xyz/anchor"; |
| 3 | +import { PythLazerSolanaContract } from "../target/types/pyth_lazer_solana_contract"; |
| 4 | +import * as pythLazerSolanaContractIdl from "../target/idl/pyth_lazer_solana_contract.json"; |
| 5 | +import yargs from "yargs/yargs"; |
| 6 | +import { readFileSync } from "fs"; |
| 7 | +import NodeWallet from "@coral-xyz/anchor/dist/cjs/nodewallet"; |
| 8 | + |
| 9 | +// This script tops up the storage PDA and calls `migrateFrom010` on the contract. |
| 10 | +async function main() { |
| 11 | + let argv = await yargs(process.argv.slice(2)) |
| 12 | + .options({ |
| 13 | + url: { type: "string", demandOption: true }, |
| 14 | + "keypair-path": { type: "string", demandOption: true }, |
| 15 | + treasury: { type: "string", demandOption: true }, |
| 16 | + }) |
| 17 | + .parse(); |
| 18 | + |
| 19 | + const keypair = anchor.web3.Keypair.fromSecretKey( |
| 20 | + new Uint8Array(JSON.parse(readFileSync(argv.keypairPath, "ascii"))) |
| 21 | + ); |
| 22 | + const wallet = new NodeWallet(keypair); |
| 23 | + const connection = new anchor.web3.Connection(argv.url, { |
| 24 | + commitment: "confirmed", |
| 25 | + }); |
| 26 | + const provider = new anchor.AnchorProvider(connection, wallet); |
| 27 | + |
| 28 | + const program: Program<PythLazerSolanaContract> = new Program( |
| 29 | + pythLazerSolanaContractIdl as PythLazerSolanaContract, |
| 30 | + provider |
| 31 | + ); |
| 32 | + |
| 33 | + const storagePdaKey = new anchor.web3.PublicKey( |
| 34 | + "3rdJbqfnagQ4yx9HXJViD4zc4xpiSqmFsKpPuSCQVyQL" |
| 35 | + ); |
| 36 | + const storagePdaInfo = await provider.connection.getAccountInfo( |
| 37 | + storagePdaKey |
| 38 | + ); |
| 39 | + const newStorageSize = 381; |
| 40 | + if (storagePdaInfo.data.length == newStorageSize) { |
| 41 | + console.log("Already migrated"); |
| 42 | + const storage = await program.account.storage.all(); |
| 43 | + console.log("storage account: ", storage); |
| 44 | + return; |
| 45 | + } |
| 46 | + const minBalance = |
| 47 | + await provider.connection.getMinimumBalanceForRentExemption(newStorageSize); |
| 48 | + if (storagePdaInfo.lamports < minBalance) { |
| 49 | + console.log("storage PDA needs top-up"); |
| 50 | + const transaction = new anchor.web3.Transaction().add( |
| 51 | + anchor.web3.SystemProgram.transfer({ |
| 52 | + fromPubkey: keypair.publicKey, |
| 53 | + toPubkey: storagePdaKey, |
| 54 | + lamports: minBalance - storagePdaInfo.lamports, |
| 55 | + }) |
| 56 | + ); |
| 57 | + const signature = await anchor.web3.sendAndConfirmTransaction( |
| 58 | + provider.connection, |
| 59 | + transaction, |
| 60 | + [keypair] |
| 61 | + ); |
| 62 | + console.log("signature:", signature); |
| 63 | + } else { |
| 64 | + console.log("storage PDA doesn't need top-up"); |
| 65 | + } |
| 66 | + |
| 67 | + console.log("executing migration"); |
| 68 | + const signature2 = await program.methods |
| 69 | + .migrateFrom010(new anchor.web3.PublicKey(argv.treasury)) |
| 70 | + .accounts({}) |
| 71 | + .rpc(); |
| 72 | + console.log("signature:", signature2); |
| 73 | +} |
| 74 | + |
| 75 | +main(); |
0 commit comments