tronprotocol / tronweb

Javascript API Library for interacting with the TRON Network
MIT License
414 stars 271 forks source link

Detect crypto payment (USDT TRC20) #468

Closed 002love closed 7 months ago

002love commented 7 months ago

My problem is that I am not getting an event about my transaction when tracking a smart contract. In my code below is an example of my WalletService class in which I create an instance of new TronWeb(...) class. In this code we are interested in the updatePayment() function, in this function I connect to the Tether USDT smart contract (TRC20) through the tronweb library, put an event listener on the Transfer method.

When I compile and run the code, I can see in the console a lot of addresses of other people's wallets to which USDT tokens have been sent, but when I send a dollar to the wallet I'm tracking (check via if) I can't even see my wallet in the console....

Any help would be greatly appreciated

Сode:

import TronWeb from 'tronweb';
import crypto from 'crypto';
import base58 from 'bs58';

class WalletService {
    fullNode: string;
    solidityNode: string;
    eventServer: string;
    usdtContractAddress: string;
    privateKey: string;
    walletAddress: string;
    tronWeb: TronWeb;
    tronClass: TronWeb;
    headers: { 'TRON-PRO-API-KEY': string };

    constructor() {
        this.fullNode = 'https://api.trongrid.io';
        this.solidityNode = 'https://api.trongrid.io';
        this.eventServer = 'https://api.trongrid.io';
        this.usdtContractAddress = 'TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t';
        this.privateKey = 'EFF6787DC...B3BF742';
        this.walletAddress = 'TV1qVqWSJ...E68Qq9FL';
        this.headers = { 'TRON-PRO-API-KEY': '4f01...19c309e' };
        this.tronWeb = new TronWeb(
            {
                fullNode: this.fullNode,
                solidityNode: this.solidityNode,
                eventServer: this.eventServer,
                headers: { 'TRON-PRO-API-KEY': '4f01...19c309e' }
            },
            this.privateKey
        );
        this.tronWeb.setAddress(this.walletAddress);
    }

    async updatePayment(): Promise<void> {
        try {
            const contract = await this.tronWeb.contract().at(this.usdtContractAddress);

            contract.Transfer().watch((err: any, event: { result: { to: string; from: string; value: string } }) => {
                if (err) {
                    return console.error('Error with "Transfer" event:', err);
                }

                const addressTo = TronWeb.address.fromHex(event.result.to);
                console.log(addressTo);
                if (addressTo == this.walletAddress) {
                    console.log('Received USDT!', event.result.value);
                }
            });
        } catch (error) {
            console.error('Ошибка при ожидании платежа:', error);
        }
    }
}

new WalletService().updatePayment();

export default WalletService;

I tried with and without api keys, tried to track the wallet from which I send coins, the result is the same....

Important Mention!!! I have many wallets that need to be checked at once and their number is mastabitized

start940315 commented 7 months ago

Please refer to https://github.com/tronprotocol/tronweb/issues/51#issuecomment-785890109