Skip to content
Open

hi #10

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions EOS/eostoken.jsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,45 @@
import { getAssociatedTokenAddress, getAccount } from "@solana/spl-token";
import { PublicKey, Connection } from "@solana/web3.js";
import { PublicKey, Connection, clusterApiUrl } from "@solana/web3.js";

const connection = new Connection("https://api.devnet.solana.com");
const tokenMint = new PublicKey("FVWUJ8Ut6kT2fSM6bHkGGTJ32FmjQ2VGvyLwSzBAknA8");


const EOS_PRICE_USD = 0.05;

export const getTokenBalance = async (walletPublicKey) => {
export async function getTokenBalance(walletAddress, tokenMintAddress) {
try {
const tokenAccount = await getAssociatedTokenAddress(tokenMint, walletPublicKey);
const accountInfo = await getAccount(connection, tokenAccount);


const balanceBigInt = BigInt(accountInfo.amount.toString());
const balance = Number(balanceBigInt) / 10 ** 6;

const usdValue = balance * EOS_PRICE_USD;
return { balance, usdValue };
} catch (err) {
console.log("Error fetching token balance:", err);
return { balance: 0, usdValue: 0 };
const connection = new Connection(clusterApiUrl('devnet'), 'confirmed');
const walletPublicKey = new PublicKey(walletAddress);
const mintPublicKey = new PublicKey(tokenMintAddress);

// Get the token account for this wallet and mint
const tokenAccount = await getAssociatedTokenAddress(
mintPublicKey,
walletPublicKey
);

// Check if account exists first
const accountInfo = await connection.getAccountInfo(tokenAccount);
if (!accountInfo) {
console.log('Token account does not exist, returning 0 balance');
return 0;
}

// Get account info
const account = await getAccount(connection, tokenAccount);

return Number(account.amount) / Math.pow(10, 6); // 6 decimals
} catch (error) {
console.error('Error fetching token balance:', error);
// If it's a TokenAccountNotFoundError, return 0
if (error.name === 'TokenAccountNotFoundError' || error.message.includes('TokenAccountNotFoundError')) {
console.log('Token account not found, returning 0 balance');
return 0;
}
return 0;
}
};

const metadataUri = "https://raw.githubusercontent.com/naveenkumar29052006/eos/main/metadata.json";
}const metadataUri = "https://raw.githubusercontent.com/naveenkumar29052006/eos/main/metadata.json";
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

Fix syntax error after getTokenBalance definition

metadataUri is declared immediately after the closing brace without a separator (}const ...), which is a syntax error and will prevent this module from compiling. Insert a newline or semicolon before defining metadataUri.

-}const metadataUri = "https://raw.githubusercontent.com/naveenkumar29052006/eos/main/metadata.json";
+}
+
+const metadataUri = "https://raw.githubusercontent.com/naveenkumar29052006/eos/main/metadata.json";
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
}const metadataUri = "https://raw.githubusercontent.com/naveenkumar29052006/eos/main/metadata.json";
}
const metadataUri = "https://raw.githubusercontent.com/naveenkumar29052006/eos/main/metadata.json";
🤖 Prompt for AI Agents
In EOS/eostoken.jsx around line 42, there is a syntax error where the closing
brace of the previous function and the declaration of metadataUri are run
together ("}const metadataUri..."); separate them by inserting a newline or a
semicolon after the closing brace so metadataUri is declared on its own
statement (e.g., place a newline before const metadataUri or add a semicolon
after the brace).


export const fetchTokenMetadata = async () => {
try {
Expand Down
326 changes: 326 additions & 0 deletions SOLANA_REWARD_FLOW.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,326 @@
# Hackathon Reward Flow on Solana - Complete Guide

## 🏗️ System Architecture Overview

This system implements a trustless hackathon reward mechanism using Solana blockchain without requiring smart contracts. Organizations can fund issues, and contributors receive rewards automatically upon task completion.

## 📊 Flow Diagram

```
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ Organization │ │ Platform │ │ Contributor │
│ │ │ Treasury │ │ │
└─────────────────┘ └──────────────────┘ └─────────────────┘
│ │ │
│ 1. Create Issue │ │
│ ─────────────────────▶│ │
│ │ │
│ 2. Set Reward Amount │ │
│ ─────────────────────▶│ │
│ │ │
│ 3. Approve Transfer │ │
│ ─────────────────────▶│ │
│ │ │
│ 4. SOL/SPL Transfer │ │
│ ═════════════════════▶│ │
│ [Blockchain TX] │ │
│ │ │
│ │ 5. Issue Available │
│ │ ─────────────────────▶│
│ │ │
│ │ 6. Work on Issue │
│ │ ◀─────────────────────│
│ │ │
│ │ 7. Submit PR │
│ │ ◀─────────────────────│
│ │ │
│ │ 8. Verify Completion │
│ │ ─────────────────────▶│
│ │ │
│ │ 9. Send Reward │
│ │ ═════════════════════▶│
│ │ [Blockchain TX] │
│ │ │
```

## 🔄 Detailed Process Flow

### Phase 1: Issue Creation & Funding

1. **Organization Creates Issue**
- Organization logs into the platform
- Creates a new GitHub issue or imports existing one
- Specifies reward amount and token type (SOL/SPL)

2. **Wallet Connection & Approval**
- Organization connects Solana wallet (Phantom, Solflare, etc.)
- Platform generates approval transaction
- Organization signs transaction to transfer funds to treasury

3. **Treasury Storage**
- Funds are securely stored in platform's treasury wallet
- Transaction is verified on Solana blockchain
- Issue status updated to "FUNDED" in database

### Phase 2: Contributor Work

4. **Issue Discovery**
- Contributors browse available funded issues
- View reward amounts and requirements
- Select issues to work on

5. **Task Completion**
- Contributor works on the issue
- Submits pull request with solution
- GitHub webhook notifies platform of PR submission

### Phase 3: Verification & Reward

6. **Automated Verification**
- Platform verifies PR is linked to issue
- Checks if PR meets requirements
- Validates contributor's wallet address

7. **Security Checks**
- Rate limiting verification
- Fraud detection algorithms
- Treasury balance confirmation

8. **Reward Distribution**
- Platform sends reward from treasury to contributor
- Transaction signed by treasury wallet
- Contributor receives SOL/SPL tokens instantly

## 🛠️ Technical Implementation

### Key Components

#### 1. Solana Utilities (`/lib/solana-utils.js`)
```javascript
// Core functions for blockchain interactions
- getSolBalance(walletAddress)
- getTokenBalance(walletAddress, mintAddress)
- createSolTransferTransaction(from, to, amount)
- createTokenTransferTransaction(from, to, mint, amount)
- sendSolFromTreasury(recipient, amount)
- sendTokenFromTreasury(recipient, mint, amount)
- verifyTransaction(signature)
```

#### 2. Organization Approval Component (`/components/OrganizationApproval.jsx`)
```javascript
// React component for organization workflow
- Wallet connection interface
- Reward amount specification
- Transaction approval flow
- Real-time balance checking
```

#### 3. Security System (`/lib/security-utils.js`)
```javascript
// Comprehensive security measures
- Rate limiting (10 requests/hour)
- Amount validation (0.001-10 SOL)
- Transaction integrity verification
- Treasury fund monitoring
- Suspicious activity detection
```

#### 4. Reward Distribution (`/actions/rewardContributorForIssue.js`)
```javascript
// Automated reward processing
- GitHub webhook integration
- Contributor verification
- Secure treasury operations
- Database transaction logging
```

### Database Schema Extensions

The existing Prisma schema supports the flow with these key models:

```prisma
model Issue {
id Int @id @default(autoincrement())
title String
githubIssueId BigInt @unique
tokenReward Decimal @db.Decimal(18, 9) // Supports SOL precision
status IssueStatus @default(OPEN) // OPEN, CLOSED, REWARDED
}

model Contribution {
id Int @id @default(autoincrement())
transactionSignature String? // Solana TX signature
issueId Int @unique
contributorId String
}
```

## 🔒 Security Features

### 1. Rate Limiting
- Maximum 10 reward requests per hour per user
- Prevents spam and abuse

### 2. Amount Validation
- Minimum reward: 0.001 SOL
- Maximum reward: 10 SOL
- Configurable per token type

### 3. Transaction Verification
- Blockchain confirmation required
- Timing analysis to prevent pre-computed transactions
- Integrity checks against expected amounts

### 4. Treasury Monitoring
- Real-time balance verification
- Automatic alerts for low balances
- Multi-signature support capability

### 5. Fraud Detection
- Rapid successive claims detection
- New account pattern analysis
- Failed transaction monitoring

## 🚀 Setup Instructions

### 1. Environment Variables
```bash
# Solana Configuration
SOLANA_RPC_URL=https://api.devnet.solana.com
TREASURY_PRIVATE_KEY=[Base58_Encoded_Private_Key]
NEXT_PUBLIC_TREASURY_WALLET=[Treasury_Public_Key]

# Database
DATABASE_URL=postgresql://...

# GitHub Integration
GITHUB_WEBHOOK_SECRET=your_webhook_secret
```

### 2. Required Dependencies
```json
{
"@solana/web3.js": "^1.98.4",
"@solana/spl-token": "^0.4.14",
"@solana/wallet-adapter-react": "^0.15.39",
"@solana/wallet-adapter-react-ui": "^0.9.39",
"@solana/wallet-adapter-wallets": "^0.19.37"
}
```

### 3. Wallet Provider Setup
```jsx
// Wrap your app with Solana wallet providers
import { WalletProvider } from '@solana/wallet-adapter-react';
import { WalletModalProvider } from '@solana/wallet-adapter-react-ui';

// Configure supported wallets
const wallets = [
new PhantomWalletAdapter(),
new SolflareWalletAdapter(),
];
```

## 💡 Usage Examples

### For Organizations

```jsx
import OrganizationApproval from '@/components/OrganizationApproval';

function CreateReward() {
return (
<OrganizationApproval
issueId={123}
onApprovalComplete={(result) => {
console.log('Reward approved:', result);
}}
/>
);
}
```

### For Backend Integration

```javascript
import { rewardContributorForIssue } from '@/actions/rewardContributorForIssue';

// Called by GitHub webhook
const result = await rewardContributorForIssue(
githubIssueId,
githubUsername
);
```

## 📈 Monitoring & Analytics

### Transaction Tracking
- All transactions logged with signatures
- Real-time balance monitoring
- Performance metrics collection

### Security Monitoring
- Failed attempt tracking
- Suspicious pattern alerts
- Rate limit violation logs

## 🔧 Customization Options

### Token Support
- Easy addition of new SPL tokens
- Configurable reward limits per token
- Multi-token reward support

### Security Settings
- Adjustable rate limits
- Configurable amount thresholds
- Custom fraud detection rules

### Integration Options
- GitHub webhook customization
- Multiple platform support
- API-first architecture

## 🚨 Important Security Considerations

1. **Private Key Management**
- Store treasury private key securely
- Use environment variables
- Consider hardware security modules for production

2. **Network Configuration**
- Use mainnet for production
- Monitor devnet for testing
- Implement proper error handling

3. **Rate Limiting**
- Implement IP-based limits
- User-based restrictions
- Geographic considerations

4. **Monitoring**
- Set up alerting for unusual patterns
- Monitor treasury balance
- Track failed transactions

## 📞 Support & Troubleshooting

### Common Issues

1. **Transaction Failures**
- Check network status
- Verify wallet balances
- Confirm RPC endpoint

2. **Wallet Connection Issues**
- Ensure wallet extension installed
- Check network selection
- Verify permissions

3. **Rate Limiting**
- Monitor request frequency
- Implement proper error handling
- Display clear user messages

This system provides a secure, automated, and scalable solution for hackathon rewards using Solana blockchain technology without requiring smart contract development.
Loading