Initial commit
This commit is contained in:
164
src/utils/walletService.js
Normal file
164
src/utils/walletService.js
Normal file
@@ -0,0 +1,164 @@
|
||||
import axios from 'axios';
|
||||
|
||||
export default class WalletService {
|
||||
constructor(btcAddress, ltcAddress, trxAddress, ethAddress, userId, minTimestamp) {
|
||||
this.btcAddress = btcAddress;
|
||||
this.ltcAddress = ltcAddress;
|
||||
this.trxAddress = trxAddress;
|
||||
this.ethAddress = ethAddress;
|
||||
this.userId = userId;
|
||||
this.minTimestamp = minTimestamp;
|
||||
}
|
||||
|
||||
static async getCryptoPrices() {
|
||||
try {
|
||||
const response = await axios.get('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,litecoin,tether,usd-coin,tron,ethereum&vs_currencies=usd');
|
||||
return {
|
||||
btc: response.data.bitcoin?.usd || 0,
|
||||
ltc: response.data.litecoin?.usd || 0,
|
||||
eth: response.data.ethereum?.usd || 0,
|
||||
usdt: 1, // Stablecoin
|
||||
usdc: 1, // Stablecoin
|
||||
trx: response.data.tron?.usd || 0,
|
||||
usdd: 1 // Stablecoin
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error fetching crypto prices:', error);
|
||||
return {
|
||||
btc: 0, ltc: 0, eth: 0, usdt: 1, usdc: 1, trx: 0, usdd: 1
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async fetchApiRequest(url) {
|
||||
try {
|
||||
const response = await axios.get(url);
|
||||
return response.data;
|
||||
} catch (error) {
|
||||
console.error(`Error fetching data from ${url}:`, error);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async getBtcBalance() {
|
||||
if (!this.btcAddress) return 0;
|
||||
try {
|
||||
const url = `https://blockchain.info/balance?active=${this.btcAddress}`;
|
||||
const data = await this.fetchApiRequest(url);
|
||||
return data?.[this.btcAddress]?.final_balance / 100000000 || 0;
|
||||
} catch (error) {
|
||||
console.error('Error getting BTC balance:', error);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
async getLtcBalance() {
|
||||
if (!this.ltcAddress) return 0;
|
||||
try {
|
||||
const url = `https://api.blockcypher.com/v1/ltc/main/addrs/${this.ltcAddress}/balance`;
|
||||
const data = await this.fetchApiRequest(url);
|
||||
return data?.balance / 100000000 || 0;
|
||||
} catch (error) {
|
||||
console.error('Error getting LTC balance:', error);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
async getEthBalance() {
|
||||
if (!this.ethAddress) return 0;
|
||||
try {
|
||||
const url = `https://api.etherscan.io/api?module=account&action=balance&address=${this.ethAddress}&tag=latest`;
|
||||
const data = await this.fetchApiRequest(url);
|
||||
return data?.result ? parseFloat(data.result) / 1e18 : 0;
|
||||
} catch (error) {
|
||||
console.error('Error getting ETH balance:', error);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
async getUsdtErc20Balance() {
|
||||
if (!this.ethAddress) return 0;
|
||||
try {
|
||||
const url = `https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=0xdac17f958d2ee523a2206206994597c13d831ec7&address=${this.ethAddress}&tag=latest`;
|
||||
const data = await this.fetchApiRequest(url);
|
||||
return data?.result ? parseFloat(data.result) / 1e6 : 0;
|
||||
} catch (error) {
|
||||
console.error('Error getting USDT ERC20 balance:', error);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
async getUsdcErc20Balance() {
|
||||
if (!this.ethAddress) return 0;
|
||||
try {
|
||||
const url = `https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48&address=${this.ethAddress}&tag=latest`;
|
||||
const data = await this.fetchApiRequest(url);
|
||||
return data?.result ? parseFloat(data.result) / 1e6 : 0;
|
||||
} catch (error) {
|
||||
console.error('Error getting USDC ERC20 balance:', error);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
async getUsdtTrc20Balance() {
|
||||
if (!this.trxAddress) return 0;
|
||||
try {
|
||||
const url = `https://apilist.tronscan.org/api/account?address=${this.trxAddress}`;
|
||||
const data = await this.fetchApiRequest(url);
|
||||
const usdtToken = data?.trc20token_balances?.find(token =>
|
||||
token.tokenId === 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t'
|
||||
);
|
||||
return usdtToken ? parseFloat(usdtToken.balance) / 1e6 : 0;
|
||||
} catch (error) {
|
||||
console.error('Error getting USDT TRC20 balance:', error);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
async getUsddTrc20Balance() {
|
||||
if (!this.trxAddress) return 0;
|
||||
try {
|
||||
const url = `https://apilist.tronscan.org/api/account?address=${this.trxAddress}`;
|
||||
const data = await this.fetchApiRequest(url);
|
||||
const usddToken = data?.trc20token_balances?.find(token =>
|
||||
token.tokenId === 'TPYmHEhy5n8TCEfYGqW2rPxsghSfzghPDn'
|
||||
);
|
||||
return usddToken ? parseFloat(usddToken.balance) / 1e18 : 0;
|
||||
} catch (error) {
|
||||
console.error('Error getting USDD TRC20 balance:', error);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
async getAllBalances() {
|
||||
const [
|
||||
btcBalance,
|
||||
ltcBalance,
|
||||
ethBalance,
|
||||
usdtErc20Balance,
|
||||
usdcErc20Balance,
|
||||
usdtTrc20Balance,
|
||||
usddTrc20Balance,
|
||||
prices
|
||||
] = await Promise.all([
|
||||
this.getBtcBalance(),
|
||||
this.getLtcBalance(),
|
||||
this.getEthBalance(),
|
||||
this.getUsdtErc20Balance(),
|
||||
this.getUsdcErc20Balance(),
|
||||
this.getUsdtTrc20Balance(),
|
||||
this.getUsddTrc20Balance(),
|
||||
WalletService.getCryptoPrices()
|
||||
]);
|
||||
|
||||
return {
|
||||
BTC: { amount: btcBalance, usdValue: btcBalance * prices.btc },
|
||||
LTC: { amount: ltcBalance, usdValue: ltcBalance * prices.ltc },
|
||||
ETH: { amount: ethBalance, usdValue: ethBalance * prices.eth },
|
||||
'USDT ERC-20': { amount: usdtErc20Balance, usdValue: usdtErc20Balance },
|
||||
'USDC ERC-20': { amount: usdcErc20Balance, usdValue: usdcErc20Balance },
|
||||
'USDT TRC-20': { amount: usdtTrc20Balance, usdValue: usdtTrc20Balance },
|
||||
'USDD TRC-20': { amount: usddTrc20Balance, usdValue: usddTrc20Balance }
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user