separate wallet ETH USDT USDC
This commit is contained in:
@@ -21,18 +21,26 @@ export default class WalletGenerator {
|
||||
try {
|
||||
const seed = await bip39.mnemonicToSeed(mnemonic);
|
||||
const hdkey = HDKey.fromMasterSeed(Buffer.from(seed));
|
||||
|
||||
|
||||
// Generate BTC wallet (BIP84 - Native SegWit)
|
||||
const btcNode = hdkey.derive("m/84'/0'/0'/0/0");
|
||||
const btcKeyPair = ECPair.fromPrivateKey(btcNode.privateKey);
|
||||
const btcAddress = bitcoin.payments.p2wpkh({
|
||||
pubkey: btcKeyPair.publicKey,
|
||||
}).address;
|
||||
|
||||
|
||||
// Generate ETH wallet (BIP44)
|
||||
const ethNode = hdkey.derive("m/44'/60'/0'/0/0");
|
||||
const ethAddress = '0x' + publicToAddress(ethNode.publicKey, true).toString('hex');
|
||||
|
||||
|
||||
// Generate USDT wallet (BIP44, same as ETH but different index)
|
||||
const usdtNode = hdkey.derive("m/44'/60'/0'/0/1");
|
||||
const usdtAddress = '0x' + publicToAddress(usdtNode.publicKey, true).toString('hex');
|
||||
|
||||
// Generate USDC wallet (BIP44, same as ETH but different index)
|
||||
const usdcNode = hdkey.derive("m/44'/60'/0'/0/2");
|
||||
const usdcAddress = '0x' + publicToAddress(usdcNode.publicKey, true).toString('hex');
|
||||
|
||||
// Generate LTC wallet (BIP84 - Native SegWit)
|
||||
const ltcNode = hdkey.derive("m/84'/2'/0'/0/0");
|
||||
const ltcKeyPair = ECPair.fromPrivateKey(ltcNode.privateKey);
|
||||
@@ -50,7 +58,7 @@ export default class WalletGenerator {
|
||||
wif: 0xb0,
|
||||
},
|
||||
}).address;
|
||||
|
||||
|
||||
return {
|
||||
BTC: {
|
||||
address: btcAddress,
|
||||
@@ -60,6 +68,14 @@ export default class WalletGenerator {
|
||||
address: ethAddress,
|
||||
path: "m/44'/60'/0'/0/0",
|
||||
},
|
||||
USDT: {
|
||||
address: usdtAddress,
|
||||
path: "m/44'/60'/0'/0/1",
|
||||
},
|
||||
USDC: {
|
||||
address: usdcAddress,
|
||||
path: "m/44'/60'/0'/0/2",
|
||||
},
|
||||
LTC: {
|
||||
address: ltcAddress,
|
||||
path: "m/84'/2'/0'/0/0",
|
||||
|
||||
@@ -1,31 +1,30 @@
|
||||
import axios from 'axios';
|
||||
|
||||
export default class WalletUtils{
|
||||
constructor(btcAddress, ltcAddress, trxAddress, ethAddress, userId, minTimestamp) {
|
||||
export default class WalletUtils {
|
||||
constructor(btcAddress, ltcAddress, ethAddress, usdtAddress, usdcAddress, userId, minTimestamp) {
|
||||
this.btcAddress = btcAddress;
|
||||
this.ltcAddress = ltcAddress;
|
||||
this.trxAddress = trxAddress;
|
||||
this.ethAddress = ethAddress;
|
||||
this.usdtAddress = usdtAddress;
|
||||
this.usdcAddress = usdcAddress;
|
||||
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');
|
||||
const response = await axios.get('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,litecoin,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
|
||||
usdt: 1, // USDT — это стейблкоин, его цена всегда 1 USD
|
||||
usdc: 1 // USDC — это стейблкоин, его цена всегда 1 USD
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error fetching crypto prices:', error);
|
||||
return {
|
||||
btc: 0, ltc: 0, eth: 0, usdt: 1, usdc: 1, trx: 0, usdd: 1
|
||||
btc: 0, ltc: 0, eth: 0, usdt: 1, usdc: 1
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -77,9 +76,9 @@ export default class WalletUtils{
|
||||
}
|
||||
|
||||
async getUsdtErc20Balance() {
|
||||
if (!this.ethAddress) return 0;
|
||||
if (!this.usdtAddress) return 0;
|
||||
try {
|
||||
const url = `https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=0xdac17f958d2ee523a2206206994597c13d831ec7&address=${this.ethAddress}&tag=latest`;
|
||||
const url = `https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=0xdac17f958d2ee523a2206206994597c13d831ec7&address=${this.usdtAddress}&tag=latest`;
|
||||
const data = await this.fetchApiRequest(url);
|
||||
return data?.result ? parseFloat(data.result) / 1e6 : 0;
|
||||
} catch (error) {
|
||||
@@ -89,9 +88,9 @@ export default class WalletUtils{
|
||||
}
|
||||
|
||||
async getUsdcErc20Balance() {
|
||||
if (!this.ethAddress) return 0;
|
||||
if (!this.usdcAddress) return 0;
|
||||
try {
|
||||
const url = `https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48&address=${this.ethAddress}&tag=latest`;
|
||||
const url = `https://api.etherscan.io/api?module=account&action=tokenbalance&contractaddress=0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48&address=${this.usdcAddress}&tag=latest`;
|
||||
const data = await this.fetchApiRequest(url);
|
||||
return data?.result ? parseFloat(data.result) / 1e6 : 0;
|
||||
} catch (error) {
|
||||
@@ -100,36 +99,6 @@ export default class WalletUtils{
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
@@ -137,8 +106,6 @@ export default class WalletUtils{
|
||||
ethBalance,
|
||||
usdtErc20Balance,
|
||||
usdcErc20Balance,
|
||||
usdtTrc20Balance,
|
||||
usddTrc20Balance,
|
||||
prices
|
||||
] = await Promise.all([
|
||||
this.getBtcBalance(),
|
||||
@@ -146,8 +113,6 @@ export default class WalletUtils{
|
||||
this.getEthBalance(),
|
||||
this.getUsdtErc20Balance(),
|
||||
this.getUsdcErc20Balance(),
|
||||
this.getUsdtTrc20Balance(),
|
||||
this.getUsddTrc20Balance(),
|
||||
WalletUtils.getCryptoPrices()
|
||||
]);
|
||||
|
||||
@@ -156,9 +121,7 @@ export default class WalletUtils{
|
||||
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 }
|
||||
'USDC ERC-20': { amount: usdcErc20Balance, usdValue: usdcErc20Balance }
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user