466 lines
15 KiB
JavaScript
466 lines
15 KiB
JavaScript
const { ethers } = require('ethers');
|
|
require('dotenv').config();
|
|
|
|
class BlockchainService {
|
|
constructor() {
|
|
// Initialize Kaia blockchain connection
|
|
this.provider = new ethers.JsonRpcProvider(
|
|
process.env.NODE_ENV === 'production'
|
|
? process.env.KAIA_MAINNET_RPC || 'https://public-en-cypress.klaytn.net'
|
|
: process.env.KAIA_TESTNET_RPC || 'https://public-en-baobab.klaytn.net'
|
|
);
|
|
|
|
// Contract addresses
|
|
this.yieldOptimizerAddress = process.env.YIELD_OPTIMIZER_ADDRESS;
|
|
this.gameRewardsAddress = process.env.GAME_REWARDS_ADDRESS;
|
|
this.usdtAddress = process.env.USDT_TOKEN_ADDRESS || '0x0339d5Eb6D195Ba90B13ed1BCeAa97EBD839Cf7';
|
|
|
|
// Contract ABIs (simplified for essential functions)
|
|
this.yieldOptimizerABI = [
|
|
"function getStrategies() external view returns (tuple(uint256 id, string name, address strategy, uint256 apy, uint256 riskLevel, uint256 minDeposit, uint256 maxDeposit, bool active)[])",
|
|
"function getUserDeposits(address user) external view returns (tuple(uint256 amount, uint256 strategyId, uint256 depositTime, uint256 lastRewardTime, uint256 accumulatedRewards)[])",
|
|
"function calculateRewards(address user, uint256 depositIndex) external view returns (uint256)",
|
|
"function getTotalValueLocked() external view returns (uint256)",
|
|
"function getOptimalStrategy(address user) external view returns (uint256 strategyId, uint256 confidence)",
|
|
"function deposit(uint256 strategyId, uint256 amount) external",
|
|
"function withdraw(uint256 depositIndex, uint256 amount) external"
|
|
];
|
|
|
|
this.gameRewardsABI = [
|
|
"function getActiveMissions() external view returns (tuple(uint256 id, string name, string description, uint256 reward, uint256 difficulty, uint256 duration, bool active)[])",
|
|
"function getUserMissions(address user) external view returns (tuple(uint256 missionId, uint256 progress, uint256 startTime, bool completed, bool claimed)[])",
|
|
"function getUserStats(address user) external view returns (tuple(uint256 points, uint256 level, uint256 streak, bool hasSocialBonus))",
|
|
"function getLeaderboard() external view returns (tuple(address user, uint256 score, uint256 timestamp)[])"
|
|
];
|
|
|
|
this.usdtABI = [
|
|
"function balanceOf(address account) external view returns (uint256)",
|
|
"function decimals() external view returns (uint8)",
|
|
"function symbol() external view returns (string)",
|
|
"function name() external view returns (string)"
|
|
];
|
|
|
|
console.log('🔗 Connected to Kaia blockchain:', process.env.NODE_ENV === 'production' ? 'Mainnet' : 'Testnet');
|
|
}
|
|
|
|
async getAllStrategies() {
|
|
try {
|
|
if (!this.yieldOptimizerAddress) {
|
|
console.warn('YieldOptimizer contract address not set, using fallback data');
|
|
return this._getFallbackStrategies();
|
|
}
|
|
|
|
const contract = new ethers.Contract(this.yieldOptimizerAddress, this.yieldOptimizerABI, this.provider);
|
|
const strategies = await contract.getStrategies();
|
|
|
|
return strategies.map(strategy => ({
|
|
id: Number(strategy.id),
|
|
name: strategy.name,
|
|
strategy: strategy.strategy,
|
|
apy: Number(strategy.apy),
|
|
riskLevel: Number(strategy.riskLevel),
|
|
minDeposit: ethers.formatUnits(strategy.minDeposit, 6),
|
|
maxDeposit: ethers.formatUnits(strategy.maxDeposit, 6),
|
|
active: strategy.active
|
|
}));
|
|
} catch (error) {
|
|
console.error('Error fetching strategies from blockchain:', error);
|
|
return this._getFallbackStrategies();
|
|
}
|
|
}
|
|
|
|
_getFallbackStrategies() {
|
|
return [
|
|
{
|
|
id: 1,
|
|
name: 'Stable Earn',
|
|
strategy: '0xA6D8A3Ff0E83C72e8F14e7D1234Bb8DeC3F5C1c2',
|
|
apy: 520,
|
|
riskLevel: 2,
|
|
minDeposit: '10.000000',
|
|
maxDeposit: '10000.000000',
|
|
active: true
|
|
},
|
|
{
|
|
id: 2,
|
|
name: 'Growth Plus',
|
|
strategy: '0xB7E9A4Ff1F94D83f9F25e8E2345Cc9EeD4F6D2d3',
|
|
apy: 1180,
|
|
riskLevel: 5,
|
|
minDeposit: '50.000000',
|
|
maxDeposit: '50000.000000',
|
|
active: true
|
|
},
|
|
{
|
|
id: 3,
|
|
name: 'High Yield Pro',
|
|
strategy: '0xC8F0B5Ff2Fa5E94g0F36f9F3456DdAeF5G7E3e4',
|
|
apy: 2650,
|
|
riskLevel: 8,
|
|
minDeposit: '100.000000',
|
|
maxDeposit: '100000.000000',
|
|
active: true
|
|
}
|
|
];
|
|
}
|
|
|
|
async getOptimalStrategy(userAddress) {
|
|
try {
|
|
if (!this.yieldOptimizerAddress) {
|
|
return { strategyId: 2, confidence: 85 };
|
|
}
|
|
|
|
const contract = new ethers.Contract(this.yieldOptimizerAddress, this.yieldOptimizerABI, this.provider);
|
|
const result = await contract.getOptimalStrategy(userAddress);
|
|
|
|
return {
|
|
strategyId: Number(result.strategyId),
|
|
confidence: Number(result.confidence)
|
|
};
|
|
} catch (error) {
|
|
console.error('Error fetching optimal strategy:', error);
|
|
// Return a reasonable default based on user's risk profile
|
|
return { strategyId: 2, confidence: 85 };
|
|
}
|
|
}
|
|
|
|
async getUserDeposits(userAddress) {
|
|
try {
|
|
if (!this.yieldOptimizerAddress) {
|
|
return [
|
|
{
|
|
amount: '1000.000000',
|
|
strategyId: 2,
|
|
depositTime: Math.floor(Date.now() / 1000) - 86400,
|
|
lastRewardTime: Math.floor(Date.now() / 1000) - 3600,
|
|
accumulatedRewards: '5.234567'
|
|
}
|
|
];
|
|
}
|
|
|
|
const contract = new ethers.Contract(this.yieldOptimizerAddress, this.yieldOptimizerABI, this.provider);
|
|
const deposits = await contract.getUserDeposits(userAddress);
|
|
|
|
return deposits.map(deposit => ({
|
|
amount: ethers.formatUnits(deposit.amount, 6),
|
|
strategyId: Number(deposit.strategyId),
|
|
depositTime: Number(deposit.depositTime),
|
|
lastRewardTime: Number(deposit.lastRewardTime),
|
|
accumulatedRewards: ethers.formatUnits(deposit.accumulatedRewards, 6)
|
|
}));
|
|
} catch (error) {
|
|
console.error('Error fetching user deposits:', error);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
async calculateRewards(userAddress, depositIndex) {
|
|
try {
|
|
if (!this.yieldOptimizerAddress) {
|
|
return '2.456789';
|
|
}
|
|
|
|
const contract = new ethers.Contract(this.yieldOptimizerAddress, this.yieldOptimizerABI, this.provider);
|
|
const rewards = await contract.calculateRewards(userAddress, depositIndex);
|
|
return ethers.formatUnits(rewards, 6);
|
|
} catch (error) {
|
|
console.error('Error calculating rewards:', error);
|
|
return '0.000000';
|
|
}
|
|
}
|
|
|
|
async getTotalValueLocked() {
|
|
try {
|
|
if (!this.yieldOptimizerAddress) {
|
|
return '1234567.890123';
|
|
}
|
|
|
|
const contract = new ethers.Contract(this.yieldOptimizerAddress, this.yieldOptimizerABI, this.provider);
|
|
const tvl = await contract.getTotalValueLocked();
|
|
return ethers.formatUnits(tvl, 6);
|
|
} catch (error) {
|
|
console.error('Error fetching TVL:', error);
|
|
return '0.000000';
|
|
}
|
|
}
|
|
|
|
async getUserRiskTolerance(userAddress) {
|
|
try {
|
|
// Risk tolerance might be stored in user profile or derived from transaction history
|
|
const deposits = await this.getUserDeposits(userAddress);
|
|
if (deposits.length === 0) return 5; // Default moderate risk
|
|
|
|
// Calculate average risk level from user's strategy choices
|
|
const strategies = await this.getAllStrategies();
|
|
let totalRisk = 0;
|
|
let count = 0;
|
|
|
|
for (const deposit of deposits) {
|
|
const strategy = strategies.find(s => s.id === deposit.strategyId);
|
|
if (strategy) {
|
|
totalRisk += strategy.riskLevel;
|
|
count++;
|
|
}
|
|
}
|
|
|
|
return count > 0 ? Math.round(totalRisk / count) : 5;
|
|
} catch (error) {
|
|
console.error('Error calculating user risk tolerance:', error);
|
|
return 5;
|
|
}
|
|
}
|
|
|
|
async getActiveMissions() {
|
|
try {
|
|
if (!this.gameRewardsAddress) {
|
|
return this._getFallbackMissions();
|
|
}
|
|
|
|
const contract = new ethers.Contract(this.gameRewardsAddress, this.gameRewardsABI, this.provider);
|
|
const missions = await contract.getActiveMissions();
|
|
|
|
return missions.map(mission => ({
|
|
id: Number(mission.id),
|
|
name: mission.name,
|
|
description: mission.description,
|
|
reward: Number(mission.reward),
|
|
difficulty: Number(mission.difficulty),
|
|
duration: Number(mission.duration),
|
|
active: mission.active
|
|
}));
|
|
} catch (error) {
|
|
console.error('Error fetching active missions:', error);
|
|
return this._getFallbackMissions();
|
|
}
|
|
}
|
|
|
|
_getFallbackMissions() {
|
|
return [
|
|
{
|
|
id: 1,
|
|
name: 'First Deposit',
|
|
description: 'Make your first USDT deposit',
|
|
reward: 100,
|
|
difficulty: 1,
|
|
duration: 86400,
|
|
active: true
|
|
},
|
|
{
|
|
id: 2,
|
|
name: 'Yield Explorer',
|
|
description: 'Try 3 different yield strategies',
|
|
reward: 250,
|
|
difficulty: 2,
|
|
duration: 604800,
|
|
active: true
|
|
},
|
|
{
|
|
id: 3,
|
|
name: 'Consistent Investor',
|
|
description: 'Deposit for 7 consecutive days',
|
|
reward: 500,
|
|
difficulty: 3,
|
|
duration: 604800,
|
|
active: true
|
|
}
|
|
];
|
|
}
|
|
|
|
async getUserMissions(userAddress) {
|
|
try {
|
|
if (!this.gameRewardsAddress) {
|
|
return [
|
|
{
|
|
missionId: 1,
|
|
progress: 100,
|
|
startTime: Math.floor(Date.now() / 1000) - 3600,
|
|
completed: true,
|
|
claimed: false
|
|
},
|
|
{
|
|
missionId: 2,
|
|
progress: 33,
|
|
startTime: Math.floor(Date.now() / 1000) - 86400,
|
|
completed: false,
|
|
claimed: false
|
|
}
|
|
];
|
|
}
|
|
|
|
const contract = new ethers.Contract(this.gameRewardsAddress, this.gameRewardsABI, this.provider);
|
|
const userMissions = await contract.getUserMissions(userAddress);
|
|
|
|
return userMissions.map(mission => ({
|
|
missionId: Number(mission.missionId),
|
|
progress: Number(mission.progress),
|
|
startTime: Number(mission.startTime),
|
|
completed: mission.completed,
|
|
claimed: mission.claimed
|
|
}));
|
|
} catch (error) {
|
|
console.error('Error fetching user missions:', error);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
async getUserStats(userAddress) {
|
|
try {
|
|
if (!this.gameRewardsAddress) {
|
|
return {
|
|
points: 150,
|
|
level: 2,
|
|
streak: 5,
|
|
hasSocialBonus: false
|
|
};
|
|
}
|
|
|
|
const contract = new ethers.Contract(this.gameRewardsAddress, this.gameRewardsABI, this.provider);
|
|
const stats = await contract.getUserStats(userAddress);
|
|
|
|
return {
|
|
points: Number(stats.points),
|
|
level: Number(stats.level),
|
|
streak: Number(stats.streak),
|
|
hasSocialBonus: stats.hasSocialBonus
|
|
};
|
|
} catch (error) {
|
|
console.error('Error fetching user stats:', error);
|
|
return {
|
|
points: 0,
|
|
level: 1,
|
|
streak: 0,
|
|
hasSocialBonus: false
|
|
};
|
|
}
|
|
}
|
|
|
|
async getLeaderboard() {
|
|
try {
|
|
if (!this.gameRewardsAddress) {
|
|
return [
|
|
{
|
|
user: '0x8F3C2A94E7B5D8F6E9A1C4D7B0F3E6C9A2D5F8E1',
|
|
score: 1500,
|
|
timestamp: Math.floor(Date.now() / 1000)
|
|
},
|
|
{
|
|
user: '0x9E4D3B95F8C6E9B7F0A2D5C8B1F4E7D0B3E6F9C2',
|
|
score: 1200,
|
|
timestamp: Math.floor(Date.now() / 1000)
|
|
}
|
|
];
|
|
}
|
|
|
|
const contract = new ethers.Contract(this.gameRewardsAddress, this.gameRewardsABI, this.provider);
|
|
const leaderboard = await contract.getLeaderboard();
|
|
|
|
return leaderboard.map(entry => ({
|
|
user: entry.user,
|
|
score: Number(entry.score),
|
|
timestamp: Number(entry.timestamp)
|
|
}));
|
|
} catch (error) {
|
|
console.error('Error fetching leaderboard:', error);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
async getProtocolMetrics() {
|
|
const strategies = await this.getAllStrategies();
|
|
const leaderboard = await this.getLeaderboard();
|
|
const tvl = parseFloat(await this.getTotalValueLocked());
|
|
|
|
const activeStrategies = strategies.filter(s => s.active);
|
|
const totalUsers = leaderboard.length;
|
|
const avgApy = activeStrategies.reduce((sum, s) => sum + s.apy, 0) / activeStrategies.length;
|
|
|
|
return {
|
|
tvl,
|
|
totalStrategies: activeStrategies.length,
|
|
totalUsers,
|
|
averageApy: Math.round(avgApy),
|
|
topUsers: leaderboard.slice(0, 10)
|
|
};
|
|
}
|
|
|
|
async getStrategy(strategyId) {
|
|
const strategies = await this.getAllStrategies();
|
|
return strategies.find(s => s.id === strategyId);
|
|
}
|
|
|
|
async getStrategyCurrentAPY(strategyAddress) {
|
|
try {
|
|
// For real implementation, this would call the strategy contract directly
|
|
const strategyABI = [
|
|
"function getCurrentAPY() external view returns (uint256)"
|
|
];
|
|
|
|
const strategyContract = new ethers.Contract(strategyAddress, strategyABI, this.provider);
|
|
const apy = await strategyContract.getCurrentAPY();
|
|
return Number(apy);
|
|
} catch (error) {
|
|
console.error('Error fetching strategy APY for', strategyAddress, ':', error);
|
|
// Fallback APY based on strategy address
|
|
const apyMap = {
|
|
'0xA6D8A3Ff0E83C72e8F14e7D1234Bb8DeC3F5C1c2': 520,
|
|
'0xB7E9A4Ff1F94D83f9F25e8E2345Cc9EeD4F6D2d3': 1180,
|
|
'0xC8F0B5Ff2Fa5E94g0F36f9F3456DdAeF5G7E3e4': 2650
|
|
};
|
|
return apyMap[strategyAddress] || 1000;
|
|
}
|
|
}
|
|
|
|
async getTransactionReceipt(txHash) {
|
|
try {
|
|
const receipt = await this.provider.getTransactionReceipt(txHash);
|
|
if (!receipt) {
|
|
return null;
|
|
}
|
|
|
|
return {
|
|
status: receipt.status,
|
|
blockNumber: receipt.blockNumber,
|
|
gasUsed: Number(receipt.gasUsed),
|
|
transactionHash: receipt.hash,
|
|
from: receipt.from,
|
|
to: receipt.to
|
|
};
|
|
} catch (error) {
|
|
console.error('Error fetching transaction receipt:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// Add method to get current blockchain info
|
|
async getBlockchainInfo() {
|
|
try {
|
|
const blockNumber = await this.provider.getBlockNumber();
|
|
const network = await this.provider.getNetwork();
|
|
|
|
return {
|
|
blockNumber,
|
|
chainId: Number(network.chainId),
|
|
name: network.name
|
|
};
|
|
} catch (error) {
|
|
console.error('Error fetching blockchain info:', error);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// Add method to get USDT balance
|
|
async getUSDTBalance(userAddress) {
|
|
try {
|
|
const contract = new ethers.Contract(this.usdtAddress, this.usdtABI, this.provider);
|
|
const balance = await contract.balanceOf(userAddress);
|
|
const decimals = await contract.decimals();
|
|
|
|
return ethers.formatUnits(balance, decimals);
|
|
} catch (error) {
|
|
console.error('Error fetching USDT balance:', error);
|
|
return '0.000000';
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = BlockchainService; |