Skip to content

Commit

Permalink
Create execute.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 8, 2024
1 parent 42b35d8 commit e89ab2e
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions pi-nexus-smart-contracts/solana/scripts/execute.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
) -> Result<(), Box<dyn Error>> {
// 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);
}
}

0 comments on commit e89ab2e

Please sign in to comment.