Skip to content

Commit

Permalink
demo added
Browse files Browse the repository at this point in the history
  • Loading branch information
anoushk1234 committed Jun 9, 2024
1 parent 4814450 commit be245c9
Show file tree
Hide file tree
Showing 11 changed files with 9,187 additions and 9,567 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
# next.js
/.next/
/out/

.env
# production
/build

.yarn
# misc
.DS_Store
*.pem
Expand All @@ -37,4 +38,4 @@ yarn-error.log*
*.tsbuildinfo

# logs
*.log
*.log
11 changes: 10 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,33 @@
"version": "0.2.0",
"author": "Solana Maintainers <[email protected]>",
"license": "MIT",
"private": false,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"resolutions": {
"blake3-wasm": "^3.0.0",
"@c4312/blake3-internal": "^3.0.0"
},
"dependencies": {
"@heroicons/react": "^1.0.5",
"@noble/ed25519": "^1.7.1",
"@project-serum/anchor": "^0.26.0",
"@solana/wallet-adapter-base": "^0.9.22",
"@solana/wallet-adapter-react": "^0.15.32",
"@solana/wallet-adapter-react-ui": "^0.9.31",
"@solana/wallet-adapter-wallets": "^0.19.16",
"@solana/web3.js": "^1.73.0",
"@tailwindcss/typography": "^0.5.9",
"axios": "^1.7.2",
"blake3-wasm": "^3.0.0",
"borsh": "^2.0.0",
"bs58": "^5.0.0",
"daisyui": "^1.24.3",
"date-fns": "^2.29.3",
"hash-wasm": "^4.11.0",
"immer": "^9.0.12",
"next": "^13.1.5",
"next-compose-plugins": "^2.2.1",
Expand Down
70 changes: 53 additions & 17 deletions src/components/SendTransaction.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { useConnection, useWallet } from '@solana/wallet-adapter-react';
import { Keypair, SystemProgram, Transaction, TransactionMessage, TransactionSignature, VersionedTransaction } from '@solana/web3.js';
import { AccountInfo, Keypair, PublicKey, SystemProgram, SYSVAR_CLOCK_PUBKEY, Transaction, TransactionMessage, TransactionSignature, VersionedTransaction } from '@solana/web3.js';
import axios, { AxiosResponse } from 'axios';
import { SPVProxyResponse } from 'pages/api/proxy';
import { FC, useCallback } from 'react';
import { monitorAndVerifyUpdates } from 'spv/client';
import { COPY_PROGRAM_ID, getCopyProgram } from 'spv/program';
import { notify } from "../utils/notifications";

export const SendTransaction: FC = () => {
const { connection } = useConnection();
const { publicKey, sendTransaction } = useWallet();
const { publicKey, sendTransaction,signTransaction } = useWallet();

const onClick = useCallback(async () => {

if (!publicKey) {
notify({ type: 'error', message: `Wallet not connected!` });
console.log('error', `Send Transaction: Wallet not connected!`);
Expand All @@ -16,37 +21,68 @@ export const SendTransaction: FC = () => {

let signature: TransactionSignature = '';
try {
const copyProgram = getCopyProgram("http://" + (process.env.NEXT_PUBLIC_HOST || "127.0.0.1") + ":8899",Keypair.generate().secretKey,COPY_PROGRAM_ID);
// ;
let toAccount = new PublicKey("gvtVGeQAm5e1fprqZt3xmzTuTWKdTbbytr269wv4yS6");

let [account_for_proof,bump] = PublicKey.findProgramAddressSync([Buffer.from("copy_hash")],copyProgram.programId)
console.log("account_for_proof: ",account_for_proof);
let spv_ix = await copyProgram.methods.copyHash(bump).accounts({
copyAccount: account_for_proof,
sourceAccount: account_for_proof,
clock: SYSVAR_CLOCK_PUBKEY,
systemProgram: SystemProgram.programId,
creator: publicKey
}).instruction();
// console.log("txn_hash:",txn)

// Create instructions to send, in this case a simple transfer
const instructions = [
SystemProgram.transfer({
fromPubkey: publicKey,
toPubkey: Keypair.generate().publicKey,
lamports: 1_000_000,
toPubkey: toAccount,
lamports: 1_000,
}),
spv_ix
];

// Get the lates block hash to use on our transaction and confirmation
let latestBlockhash = await connection.getLatestBlockhash()

// let latestBlockhash = await connection.getLatestBlockhash()
let txn = new Transaction().add(instructions[0]).add(instructions[1]);
// Create a new TransactionMessage with version and compile it to legacy
const messageLegacy = new TransactionMessage({
payerKey: publicKey,
recentBlockhash: latestBlockhash.blockhash,
instructions,
}).compileToLegacyMessage();
// const messageLegacy = new TransactionMessage({
// payerKey: publicKey,
// recentBlockhash: latestBlockhash.blockhash,
// instructions,
// }).compileToLegacyMessage();

// Create a new VersionedTransacction which supports legacy and v0
const transation = new VersionedTransaction(messageLegacy)
// const transation = new VersionedTransaction(messageLegacy)
let blockhash = (await connection.getLatestBlockhash('finalized')).blockhash;
txn.recentBlockhash = blockhash;
txn.feePayer = publicKey;

txn = await signTransaction(txn);
// Send transaction and await for signature
signature = await sendTransaction(transation, connection);
// signature = await connection.sendRawTransaction(txn.serialize(),{
// skipPreflight: true,
//
// });
let account_state = await copyProgram.provider.connection.getAccountInfo(account_for_proof,"processed");
let spv_proxy_response: AxiosResponse<SPVProxyResponse> = await axios.post("/api/proxy",{
txn: txn.serialize().toString("base64")
})
console.log("done",spv_proxy_response.data.update)

// Send transaction and await for signature
await connection.confirmTransaction({ signature, ...latestBlockhash }, 'confirmed');
// await connection.confirmTransaction({ signature, ...latestBlockhash }, 'processed');
// console.log("not")

console.log(signature);
notify({ type: 'success', message: 'Transaction successful!', txid: signature });
// let spv_res = true
let spv_res = await monitorAndVerifyUpdates(account_for_proof,account_state as AccountInfo<Buffer>,copyProgram,bump,"145.40.125.153",spv_proxy_response.data.update);
console.log(spv_res);
spv_res && notify({ type: 'success', message: 'SPV & Txn successful!', txid: spv_proxy_response.data.signature});
!spv_res && notify({ type: 'error', message: 'Transaction successful but spv fails', txid: spv_proxy_response.data.signature });
} catch (error: any) {
notify({ type: 'error', message: `Transaction failed!`, description: error?.message, txid: signature });
console.log('error', `Transaction failed! ${error?.message}`, signature);
Expand All @@ -57,7 +93,7 @@ export const SendTransaction: FC = () => {
return (
<div className="flex flex-row justify-center">
<div className="relative group items-center">
<div className="m-1 absolute -inset-0.5 bg-gradient-to-r from-indigo-500 to-fuchsia-500
<div className="m-1 absolute -inset-0.5 bg-gradient-to-r from-indigo-500 to-fuchsia-500
rounded-lg blur opacity-20 group-hover:opacity-100 transition duration-1000 group-hover:duration-200 animate-tilt"></div>
<button
className="group w-60 m-2 btn animate-pulse bg-gradient-to-br from-indigo-500 to-fuchsia-500 hover:from-white hover:to-purple-300 text-black"
Expand Down
2 changes: 1 addition & 1 deletion src/contexts/ContextProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const WalletContextProvider: FC<{ children: ReactNode }> = ({ children }) => {

return (
// TODO: updates needed for updating and referencing endpoint: wallet adapter rework
<ConnectionProvider endpoint={endpoint}>
<ConnectionProvider endpoint={"http://" + (process.env.NEXT_PUBLIC_HOST || "127.0.0.1") +":8899"}>
<WalletProvider wallets={wallets} onError={onError} autoConnect={autoConnect}>
<ReactUIWalletModalProviderDynamic>
{children}
Expand Down
13 changes: 0 additions & 13 deletions src/pages/api/hello.ts

This file was deleted.

51 changes: 51 additions & 0 deletions src/pages/api/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next'
import { Update } from 'spv/utils'
import net from "net";
import { Connection } from '@solana/web3.js';
import * as borsh from "borsh"
import { UpdateSchema } from 'spv/client';
import bs58 from "bs58"
type SPVProxyRequestData = {
txn: string,
}
export type SPVProxyResponse = {
update: string,
signature: string
}
export default function handler(
req: NextApiRequest,
res: NextApiResponse<SPVProxyResponse>
) {
let data: SPVProxyRequestData = req.body;
let signature: string;
const client = net.connect(
{
port: 5000,
host: process.env.HOST || "127.0.0.1",
},
async function () {
console.log("LOG: Client connected to spv geyser");
const connection = new Connection("http://" + (process.env.HOST || "127.0.0.1") +":8899",{
commitment: "processed"
});
signature = await connection.sendEncodedTransaction(data.txn,{
skipPreflight: true
})
},
);

client.on("data", async function (update: Uint8Array) {
console.log("signature: ",signature);
console.dir(update)

// let received_update: Update = borsh.deserialize(UpdateSchema, update) as any;
// console.dir(received_update)

res.status(200).json({
signature,
update: bs58.encode(update)
})
})
// setTimeout(() => res.status(500).json({signature,update: new Uint8Array()}),1000 * 10);
}
Loading

0 comments on commit be245c9

Please sign in to comment.