Skip to content

Instantly share code, notes, and snippets.

@0xMukesh
Last active December 7, 2022 16:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0xMukesh/d421f25c0dd556af7b0dd468773565d3 to your computer and use it in GitHub Desktop.
Save 0xMukesh/d421f25c0dd556af7b0dd468773565d3 to your computer and use it in GitHub Desktop.
get status of a payment via the reference public key (solana pay)
import { test } from "vitest";
import {
Connection,
Keypair,
LAMPORTS_PER_SOL,
PublicKey,
sendAndConfirmTransaction,
SystemProgram,
Transaction,
} from "@solana/web3.js";
import { validateTransfer } from "@solana/pay";
import { BigNumber } from "bignumber.js";
import base58 from "bs58";
import dotenv from "dotenv";
dotenv.config();
test("verify transaction", async () => {
// hardcoded solana values for testing
const connection = new Connection("http://127.0.0.1:8899");
const reference = Keypair.generate();
const alice = Keypair.fromSecretKey(
base58.decode(process.env.FAUCET_SECRET_KEY!)
);
const bob = new PublicKey("2S9jKJEGKoVxR3xkEfFyGVrLwJj1H8xYjqtSP5LAX97x");
// just verifying stuff
console.log(`Alice - ${alice.publicKey.toString()}`);
console.log(`Bob - ${bob.toString()}`);
// airdrop sol
const airdropSignature = await connection.requestAirdrop(
alice.publicKey,
1 * LAMPORTS_PER_SOL
);
console.log(
`Airdropped 1 SOL to ${alice.publicKey.toString()} - ${airdropSignature}`
);
// creating a listener which finds a transaction having the `reference` public key
connection.onLogs(reference.publicKey, async (logs, _context) => {
console.log(`connection.onLogs fn - ${logs.signature}`);
try {
// validating the transfer
await validateTransfer(connection, logs.signature, {
recipient: bob,
amount: new BigNumber(0.0005),
reference: reference.publicKey,
});
console.log("Transfer validated");
} catch (err) {
console.log("Invalid transfer");
}
});
// just a simple SOL transfer instruction
const feeTransferInstruction = SystemProgram.transfer({
fromPubkey: alice.publicKey,
toPubkey: bob,
lamports: 0.0005 * LAMPORTS_PER_SOL,
});
// adding the `reference` public key into the instruction's keys
feeTransferInstruction.keys.push({
pubkey: reference.publicKey,
isSigner: false,
isWritable: false,
});
const transaction = new Transaction();
transaction.add(feeTransferInstruction);
const { blockhash } = await connection.getLatestBlockhash();
transaction.recentBlockhash = blockhash;
transaction.feePayer = alice.publicKey;
// signing a sending the transaction
const signature = await sendAndConfirmTransaction(connection, transaction, [
alice,
]);
// for verifying whether the signature from .onLogs fn is correct or not
console.log(`Signature - ${signature}`);
// added this 'cause sometimes the .onLogs fn doesn't catch the signature on the spot, so wait for 500ms before the test gets exited
const sleep = (ms: number) => {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
};
await sleep(500);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment