mirror of
https://github.com/mito-systems/ranger-app.git
synced 2026-05-28 23:34:59 +00:00
98 lines
3.0 KiB
TypeScript
98 lines
3.0 KiB
TypeScript
// src/services/blockchain/tokenRewardService.ts
|
|
import { awardTokensForWildlife } from './seiService';
|
|
import { isWalletConnected, getWalletAddress } from './seiService';
|
|
|
|
// Note: These reward amounts are for display purposes only
|
|
// The actual reward amounts are determined by the WILD token contract on Sei
|
|
export const TOKEN_REWARDS = {
|
|
WILDLIFE_SIGHTING: 10, // Base WILD token reward for any wildlife sighting
|
|
RARE_SPECIES: 25, // Additional WILD tokens for rare species
|
|
};
|
|
|
|
// List of species considered rare for bonus rewards
|
|
const RARE_SPECIES = [
|
|
'elephant',
|
|
'tiger',
|
|
'rhino',
|
|
'panda',
|
|
'gorilla',
|
|
'whale',
|
|
'dolphin',
|
|
'snow leopard',
|
|
'eagle',
|
|
// Add more rare species as needed
|
|
];
|
|
|
|
/**
|
|
* Determine if a species is considered rare
|
|
* @param species The detected animal species
|
|
* @returns True if the species is considered rare
|
|
*/
|
|
export const isRareSpecies = (species: string): boolean => {
|
|
if (!species) return false;
|
|
const normalizedSpecies = species.toLowerCase().trim();
|
|
return RARE_SPECIES.some(rareSpecies =>
|
|
normalizedSpecies.includes(rareSpecies)
|
|
);
|
|
};
|
|
|
|
/**
|
|
* Award tokens for a wildlife sighting if wallet is connected
|
|
* @param species The detected animal species
|
|
* @param points Optional number of points to convert to tokens (if not provided, calculates based on species)
|
|
* @returns Object containing success status and transaction info
|
|
*/
|
|
export const awardTokensForSighting = async (species: string, points?: number): Promise<{
|
|
success: boolean;
|
|
tokenAmount?: number;
|
|
txHash?: string;
|
|
error?: string;
|
|
walletConnected: boolean;
|
|
}> => {
|
|
try {
|
|
// Check if wallet is connected
|
|
if (!isWalletConnected()) {
|
|
return {
|
|
success: false,
|
|
error: 'No wallet connected',
|
|
walletConnected: false
|
|
};
|
|
}
|
|
|
|
// Calculate token reward amount - either from points or from species
|
|
let totalReward: number;
|
|
|
|
if (typeof points === 'number' && points > 0) {
|
|
// If points are provided, use them directly (1 point = 1 token)
|
|
totalReward = points;
|
|
} else {
|
|
// Otherwise calculate based on species
|
|
const baseReward = TOKEN_REWARDS.WILDLIFE_SIGHTING;
|
|
const isRare = isRareSpecies(species);
|
|
const rareBonus = isRare ? TOKEN_REWARDS.RARE_SPECIES : 0;
|
|
totalReward = baseReward + rareBonus;
|
|
}
|
|
|
|
// Award WILD tokens through Sei contract based on species and amount
|
|
const result = await awardTokensForWildlife(species, totalReward);
|
|
|
|
if (result.success) {
|
|
console.log(`Awarded ${totalReward} tokens for ${species} sighting to wallet ${getWalletAddress()}`);
|
|
return {
|
|
success: true,
|
|
tokenAmount: totalReward,
|
|
txHash: result.txHash,
|
|
walletConnected: true
|
|
};
|
|
} else {
|
|
throw new Error(result.error || 'Failed to award tokens');
|
|
}
|
|
} catch (error) {
|
|
console.error('Error in token reward service:', error);
|
|
return {
|
|
success: false,
|
|
error: error instanceof Error ? error.message : 'Unknown error processing token reward',
|
|
walletConnected: true
|
|
};
|
|
}
|
|
}; |