diff --git a/pi-nexus-smart-contracts/solana/scripts/execute.rs b/pi-nexus-smart-contracts/solana/scripts/execute.rs new file mode 100644 index 000000000..29bbc9066 --- /dev/null +++ b/pi-nexus-smart-contracts/solana/scripts/execute.rs @@ -0,0 +1,87 @@ +use solana_client::{ + rpc_client::RpcClient, + transaction::Transaction, +}; +use solana_program::{ + account_info::AccountInfo, + entrypoint, + entrypoint::ProgramResult, + msg, + program_error::ProgramError, + pubkey::Pubkey, +}; +use solana_sdk::{ + signature::{Keypair, Signer}, + transaction::TransactionError, +}; +use std::error::Error; + +// Define the contract program ID +const PROGRAM_ID: &str = "TokenizedAssets1111111111111111111111111111111111"; + +// Define the execute function +fn execute( + rpc_url: &str, + keypair_path: &str, + instruction: &str, + accounts: Vec, +) -> Result<(), Box> { + // Create a new RPC client + let rpc_client = RpcClient::new(rpc_url); + + // Load the keypair from the file + let keypair = Keypair::from_file(keypair_path)?; + + // Parse the instruction + let instruction_data = match instruction { + "initialize" => vec![0], + "mint" => vec![1], + "burn" => vec![2], + "transfer" => vec![3], + "approve" => vec![4], + _ => { + msg!("Error: Unknown instruction"); + return Err(Box::new(ProgramError::InvalidInstruction)); + } + }; + + // Get the account infos + let mut account_infos = Vec::new(); + for account in accounts { + let pubkey = Pubkey::new_from_string(account.as_str())?; + account_infos.push(AccountInfo::new_readonly(pubkey, false)); + } + + // Create a new transaction + let tx = Transaction::new_signed_with_payer( + &[instruction_data], + Some(&keypair.pubkey()), + &account_infos, + rpc_client.get_latest_blockhash()?, + ); + + // Sign the transaction + let signers = &[&keypair]; + tx.partial_sign(&signers, rpc_client.get_latest_blockhash()?)?; + + // Send and confirm the transaction + rpc_client.send_and_confirm_transaction(&tx)?; + + Ok(()) +} + +fn main() { + if let Err(err) = execute( + "https://api.devnet.solana.com", + "path/to/keypair.json", + "mint", + vec![ + "TokenizedAssets1111111111111111111111111111111111".to_string(), + "owner_account_pubkey".to_string(), + "tokenized_asset_account_pubkey".to_string(), + ], + ) { + msg!("Error: {}", err); + std::process::exit(1); + } +}