skynetcap/solanaj

Transaction not found

Opened this issue · 0 comments

I tried this code locally, but it does not change the sender nor the receiver account. I tested the transfer using js web3 , it works. do you have any idea why it is not working?

private static final RpcClient client = new RpcClient(LOCALNET);
public static void transfer() {
		try {
			// Load the sender and receiver accounts from JSON files
			Account sender = loadAccountFromFile("sender.json");
			// Account receiver = loadAccountFromFile("receiver.json");

			// Define transfer amount in lamports (1 SOL = 1_000_000_000 lamports)
			long transferAmountLamports = (long) (0.01 * 1_000_000_000); // 0.01 SOL in lamports

			PublicKey receiverPublicKey = new PublicKey("DYkcnWc1sefBhEe2T9NBQTKfaXf6rGE2RSEubRMLcUML");
			// Fetch pre-transaction balances
			long senderPreBalance = client.getApi().getBalance(sender.getPublicKey());
			long receiverPreBalance = client.getApi().getBalance(receiverPublicKey);

			System.out.println("Sender prebalance: " + senderPreBalance + " lamports");
			System.out.println("Receiver prebalance: " + receiverPreBalance + " lamports");

			// Create the transfer transaction
			Transaction transaction = new Transaction();
			transaction.addInstruction(
					SystemProgram.transfer(
							sender.getPublicKey(),
							receiverPublicKey,
							transferAmountLamports));

			// Send and confirm the transaction
			String signature = client.getApi().sendTransaction(transaction, sender);
			System.out.println("Transfer successful! Signature: " + signature);

			// Fetch post-transaction balances
			long senderPostBalance = client.getApi().getBalance(sender.getPublicKey());
			long receiverPostBalance = client.getApi().getBalance(receiverPublicKey);

			System.out.println("Sender postbalance: " + senderPostBalance + " lamports");
			System.out.println("Receiver postbalance: " + receiverPostBalance + " lamports");

		} catch (RpcException | IOException e) {
			e.printStackTrace();
		}
	}

	private static Account loadAccountFromFile(String filepath) throws IOException {
		// Read the JSON file and parse the keypair
		ObjectMapper mapper = new ObjectMapper();
		byte[] keypair = mapper.readValue(new File(filepath), byte[].class);
		return new Account(keypair);
	}```