Skip to content

Commit

Permalink
referrals
Browse files Browse the repository at this point in the history
  • Loading branch information
jongan69 committed Jan 2, 2025
1 parent 4e270bc commit 0c11737
Show file tree
Hide file tree
Showing 14 changed files with 4,002 additions and 820 deletions.
180 changes: 178 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions src/components/home/home-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { ItemList } from "@components/home/item-list"; // Import the ItemList co
import { toast } from "react-hot-toast"; // Import the toast notification library
import { Circles } from "react-loader-spinner"; // Import the Circles loader component
import { useTokenBalance } from "@utils/hooks/useTokenBalance"; // Import custom hook to get token balance
import { FEE_ADDRESS, REFERAL_WALLET } from "@utils/globals"; // Import FEE_ADDRESS constant
import { apiLimiter, fetchTokenAccounts, handleTokenData, TokenData } from "../../utils/tokenUtils"; // Import utility functions and types
import { REFERAL_TOKEN_ADDRESS, REFERAL_ADDRESS } from "@utils/globals"; // Import FEE_ADDRESS constant
import { fetchTokenAccounts, handleTokenData, TokenData } from "../../utils/tokenUtils"; // Import utility functions and types
import { saveWalletToDb } from "@utils/saveWallet";

export function HomeContent() {
Expand All @@ -15,11 +15,11 @@ export function HomeContent() {
const prevPublicKey = useRef<string>(publicKey?.toBase58() || ""); // Ref to store the previous public key
const [loading, setLoading] = useState<boolean>(false); // State for tracking the loading state
const [totalAccounts, setTotalAccounts] = useState<number>(0); // State for storing the total number of accounts
const { balance } = useTokenBalance(FEE_ADDRESS); // Get the balance using useTokenBalance hook
const { balance } = useTokenBalance(REFERAL_TOKEN_ADDRESS); // Get the balance using useTokenBalance hook
const [totalValue, setTotalValue] = useState<number>(0); // State for tracking the total value
const [swappableTokenCount, setSwappableTokenCount] = useState<number>(0);
const [rateLimitMessage, setRateLimitMessage] = useState<string | null>(null);
const [referrer, setReferrer] = useState<string>(REFERAL_WALLET); // State for storing the referrer
const [referrer, setReferrer] = useState<string>(REFERAL_ADDRESS); // State for storing the referrer
// Effect to reset sign state if the public key changes
useEffect(() => {
if (publicKey && publicKey.toBase58() !== prevPublicKey.current) {
Expand All @@ -45,7 +45,7 @@ export function HomeContent() {
const urlParams = new URLSearchParams(window.location.search);
const referredBy = urlParams.get('referredBy');
// Save the wallet and get the effective referral address
const effectiveReferral = await saveWalletToDb(publicKey.toBase58(), referredBy || REFERAL_WALLET);
const effectiveReferral = await saveWalletToDb(publicKey.toBase58(), referredBy || REFERAL_ADDRESS);
setReferrer(effectiveReferral);
try {
const tokenAccounts = await fetchTokenAccounts(publicKey);
Expand Down
5 changes: 3 additions & 2 deletions src/components/home/item-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { TokenItem, useCreateSwapInstructions } from "@utils/hooks/useCreateSwap
import { useCloseTokenAccount } from "@utils/hooks/useCloseTokenAccount"; // Import hook to close token accounts

// Import global constants
import { LOCKIN_MINT, REFERAL_WALLET } from "@utils/globals";
import { LOCKIN_MINT, REFERAL_ADDRESS } from "@utils/globals";
import { TokenData } from "@utils/tokenUtils"; // Import TokenData type

type Props = {
Expand Down Expand Up @@ -41,7 +41,8 @@ export const ItemList = ({ initialItems, totalValue, referrer }: Props) => {

const raydiumUrl = "https://raydium.io/swap/?inputMint=sol&outputMint=8Ki8DpuWNxu9VsS3kQbarsCWMcFGWkzzA8pUPto9zBd5&referrer=9yA9LPCRv8p8V8ZvJVYErrVGWbwqAirotDTQ8evRxE5N"; // URL for Raydium swap
const targetTokenMintAddress = LOCKIN_MINT; // Target token mint address
const referralAccountPubkey = new PublicKey(referrer) ?? new PublicKey(REFERAL_WALLET); // Referral account public key (if referrer is not set, use default referral wallet)
console.log(referrer);
const referralAccountPubkey = new PublicKey(referrer) ?? new PublicKey(REFERAL_ADDRESS); // Referral account public key (if referrer is not set, use default referral wallet)
const safeSignAllTransactions = signAllTransactions || (async (txs: VersionedTransaction[]) => txs);

const { handleClosePopup, sending } = useCreateSwapInstructions(
Expand Down
9 changes: 6 additions & 3 deletions src/components/layout/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react";
import { WalletMultiButton } from "@solana/wallet-adapter-react-ui";
import { useWallet } from "@solana/wallet-adapter-react";
import { useRouter } from "next/router";
import Link from "next/link";

export const Header: React.FC = () => {
const { publicKey } = useWallet();
Expand All @@ -16,9 +17,11 @@ export const Header: React.FC = () => {
return (
<header className="flex items-center justify-between py-6 mb-12">
<div className="flex items-center space-x-4">
<h1 className="text-4xl font-bold bg-gradient-to-r from-primary to-secondary bg-clip-text text-transparent">
Lock TF In
</h1>
<Link href="/">
<h1 className="text-4xl font-bold bg-gradient-to-r from-primary to-secondary bg-clip-text text-transparent cursor-pointer">
Lock TF In
</h1>
</Link>
</div>
<div className="flex items-center space-x-4">
<button
Expand Down
45 changes: 45 additions & 0 deletions src/pages/api/wallet/get-wallet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { MongoClient } from 'mongodb';
import type { NextApiRequest, NextApiResponse } from 'next';

const uri = process.env.MONGODB_URI;

if (!uri) {
throw new Error('Please add your Mongo URI to .env.local');
}

export default async function handler(
req: NextApiRequest,
res: NextApiResponse
) {
if (req.method !== 'GET') {
return res.status(405).json({ message: 'Method not allowed' });
}

const { address } = req.query;

if (!address) {
return res.status(400).json({ message: 'Wallet address is required' });
}

try {
const client = await MongoClient.connect(uri as string);
const db = client.db('referral');
const walletsCollection = db.collection('wallets');

const wallet = await walletsCollection.findOne({ address });

await client.close();

if (!wallet) {
return res.status(404).json({ message: 'Wallet not found' });
}

return res.status(200).json({
referredBy: wallet.referredBy,
referralAccountPubKey: wallet.referralAccountPubKey
});
} catch (error) {
console.error('Error fetching wallet:', error);
return res.status(500).json({ message: 'Error fetching wallet' });
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { REFERAL_WALLET } from '@utils/globals';
import { REFERAL_ADDRESS } from '@utils/globals';
import { MongoClient } from 'mongodb';
import type { NextApiRequest, NextApiResponse } from 'next';

Expand All @@ -16,14 +16,14 @@ export default async function handler(
return res.status(405).json({ message: 'Method not allowed' });
}

let { address, referredBy } = req.body;
let { address, referredBy, referralAccountPubKey } = req.body;

if (!address) {
return res.status(400).json({ message: 'Wallet address is required' });
}

if (!referredBy) {
referredBy = REFERAL_WALLET;
referredBy = REFERAL_ADDRESS;
}

try {
Expand Down
Loading

0 comments on commit 0c11737

Please sign in to comment.