Compare commits
1 Commits
start
...
1-ecommerc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6ed7e5844d |
4
lib/addresses.ts
Normal file
4
lib/addresses.ts
Normal file
@ -0,0 +1,4 @@
|
||||
import { PublicKey } from "@solana/web3.js"
|
||||
|
||||
// Your shop wallet address
|
||||
export const shopAddress = new PublicKey('EXWr1Go8UyfA39U1dfRkcH8uUvqunbRhQEUQNm36UsyQ')
|
||||
1821
package-lock.json
generated
1821
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -6,6 +6,10 @@
|
||||
"start": "next start"
|
||||
},
|
||||
"dependencies": {
|
||||
"@solana/wallet-adapter-base": "^0.9.5",
|
||||
"@solana/wallet-adapter-react": "^0.15.4",
|
||||
"@solana/wallet-adapter-react-ui": "^0.9.6",
|
||||
"@solana/wallet-adapter-wallets": "^0.15.5",
|
||||
"@tailwindcss/forms": "^0.5.0",
|
||||
"bignumber.js": "^9.0.2",
|
||||
"next": "latest",
|
||||
|
||||
@ -2,16 +2,44 @@ import '../styles/globals.css'
|
||||
import type { AppProps } from 'next/app'
|
||||
import Layout from '../components/Layout'
|
||||
import Head from 'next/head'
|
||||
import { ConnectionProvider, WalletProvider } from '@solana/wallet-adapter-react'
|
||||
import { WalletModalProvider } from '@solana/wallet-adapter-react-ui'
|
||||
import { WalletAdapterNetwork } from '@solana/wallet-adapter-base'
|
||||
import { clusterApiUrl } from '@solana/web3.js'
|
||||
import { PhantomWalletAdapter, SolflareWalletAdapter } from '@solana/wallet-adapter-wallets'
|
||||
|
||||
// Default styles that can be overridden by your app
|
||||
require('@solana/wallet-adapter-react-ui/styles.css');
|
||||
|
||||
function MyApp({ Component, pageProps }: AppProps) {
|
||||
// The network can be set to 'devnet', 'testnet', or 'mainnet-beta'.
|
||||
const network = WalletAdapterNetwork.Devnet;
|
||||
|
||||
// You can also provide a custom RPC endpoint.
|
||||
const endpoint = clusterApiUrl(network);
|
||||
|
||||
// @solana/wallet-adapter-wallets includes all the adapters but supports tree shaking and lazy loading --
|
||||
// Only the wallets you configure here will be compiled into your application, and only the dependencies
|
||||
// of wallets that your users connect to will be loaded.
|
||||
const wallets = [
|
||||
new PhantomWalletAdapter(),
|
||||
new SolflareWalletAdapter({ network }),
|
||||
];
|
||||
|
||||
return (
|
||||
<Layout>
|
||||
<Head>
|
||||
<title>Cookies Inc</title>
|
||||
</Head>
|
||||
<Component {...pageProps} />
|
||||
</Layout>
|
||||
<ConnectionProvider endpoint={endpoint}>
|
||||
<WalletProvider wallets={wallets} autoConnect>
|
||||
<WalletModalProvider>
|
||||
<Layout>
|
||||
<Head>
|
||||
<title>Cookies Inc</title>
|
||||
</Head>
|
||||
<Component {...pageProps} />
|
||||
</Layout>
|
||||
</WalletModalProvider>
|
||||
</WalletProvider>
|
||||
</ConnectionProvider>
|
||||
)
|
||||
}
|
||||
|
||||
export default MyApp
|
||||
export default MyApp
|
||||
99
pages/api/makeTransaction.ts
Normal file
99
pages/api/makeTransaction.ts
Normal file
@ -0,0 +1,99 @@
|
||||
import { WalletAdapterNetwork } from "@solana/wallet-adapter-base"
|
||||
import { clusterApiUrl, Connection, PublicKey, Transaction, SystemProgram, LAMPORTS_PER_SOL } from "@solana/web3.js"
|
||||
import { NextApiRequest, NextApiResponse } from "next"
|
||||
import { shopAddress } from "../../lib/addresses"
|
||||
import calculatePrice from "../../lib/calculatePrice"
|
||||
|
||||
export type MakeTransactionInputData = {
|
||||
account: string,
|
||||
}
|
||||
|
||||
export type MakeTransactionOutputData = {
|
||||
transaction: string,
|
||||
message: string,
|
||||
}
|
||||
|
||||
type ErrorOutput = {
|
||||
error: string
|
||||
}
|
||||
|
||||
export default async function handler(
|
||||
req: NextApiRequest,
|
||||
res: NextApiResponse<MakeTransactionOutputData | ErrorOutput>
|
||||
) {
|
||||
try {
|
||||
// We pass the selected items in the query, calculate the expected cost
|
||||
const amount = calculatePrice(req.query)
|
||||
if (amount.toNumber() === 0) {
|
||||
res.status(400).json({ error: "Can't checkout with charge of 0" })
|
||||
return
|
||||
}
|
||||
|
||||
// We pass the reference to use in the query
|
||||
const { reference } = req.query
|
||||
if (!reference) {
|
||||
res.status(400).json({ error: "No reference provided" })
|
||||
return
|
||||
}
|
||||
|
||||
// We pass the buyer's public key in JSON body
|
||||
const { account } = req.body as MakeTransactionInputData
|
||||
if (!account) {
|
||||
res.status(40).json({ error: "No account provided" })
|
||||
return
|
||||
}
|
||||
const buyerPublicKey = new PublicKey(account)
|
||||
const shopPublicKey = shopAddress
|
||||
|
||||
const network = WalletAdapterNetwork.Devnet
|
||||
const endpoint = clusterApiUrl(network)
|
||||
const connection = new Connection(endpoint)
|
||||
|
||||
// Get a recent blockhash to include in the transaction
|
||||
const { blockhash } = await (connection.getLatestBlockhash('finalized'))
|
||||
|
||||
const transaction = new Transaction({
|
||||
recentBlockhash: blockhash,
|
||||
// The buyer pays the transaction fee
|
||||
feePayer: buyerPublicKey,
|
||||
})
|
||||
|
||||
// Create the instruction to send SOL from the buyer to the shop
|
||||
const transferInstruction = SystemProgram.transfer({
|
||||
fromPubkey: buyerPublicKey,
|
||||
lamports: amount.multipliedBy(LAMPORTS_PER_SOL).toNumber(),
|
||||
toPubkey: shopPublicKey,
|
||||
})
|
||||
|
||||
// Add the reference to the instruction as a key
|
||||
// This will mean this transaction is returned when we query for the reference
|
||||
transferInstruction.keys.push({
|
||||
pubkey: new PublicKey(reference),
|
||||
isSigner: false,
|
||||
isWritable: false,
|
||||
})
|
||||
|
||||
// Add the instruction to the transaction
|
||||
transaction.add(transferInstruction)
|
||||
|
||||
// Serialize the transaction and convert to base64 to return it
|
||||
const serializedTransaction = transaction.serialize({
|
||||
// We will need the buyer to sign this transaction after it's returned to them
|
||||
requireAllSignatures: false
|
||||
})
|
||||
const base64 = serializedTransaction.toString('base64')
|
||||
|
||||
// Insert into database: reference, amount
|
||||
|
||||
// Return the serialized transaction
|
||||
res.status(200).json({
|
||||
transaction: base64,
|
||||
message: "Thanks for your order! 🍪",
|
||||
})
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
|
||||
res.status(500).json({ error: 'error creating transaction', })
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -1,12 +1,24 @@
|
||||
import { useWallet } from '@solana/wallet-adapter-react'
|
||||
import { WalletMultiButton } from '@solana/wallet-adapter-react-ui'
|
||||
import Products from '../components/Products'
|
||||
import SiteHeading from '../components/SiteHeading'
|
||||
|
||||
export default function HomePage() {
|
||||
// We get the public key of the connected wallet, if there is one
|
||||
const { publicKey } = useWallet()
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-8 max-w-4xl items-stretch m-auto pt-24">
|
||||
<SiteHeading>Cookies Inc</SiteHeading>
|
||||
|
||||
<Products submitTarget='/checkout' enabled={true} />
|
||||
{/* We add the Solana wallet connect button */}
|
||||
<div className="basis-1/4">
|
||||
<WalletMultiButton className='!bg-gray-900 hover:scale-105' />
|
||||
</div>
|
||||
|
||||
{/* We disable checking out without a connected wallet */}
|
||||
{/* Also the submitTarget is /buy/transaction instead of /checkout */}
|
||||
<Products submitTarget='/checkout' enabled={publicKey !== null} />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user