88using Microsoft . Extensions . Options ;
99using Telegram . Bot ;
1010using Telegram . CoinConvertBot . BgServices . Base ;
11- using Telegram . CoinConvertBot . BgServices . BotHandler ;
1211using Telegram . CoinConvertBot . Domains . Tables ;
1312using Telegram . CoinConvertBot . Extensions ;
1413using Telegram . CoinConvertBot . Helper ;
1514using Telegram . CoinConvertBot . Models ;
15+ using Telegram . CoinConvertBot . Models . TronModel ;
1616
1717namespace Telegram . CoinConvertBot . BgServices
1818{
@@ -21,16 +21,19 @@ public class USDT_TRC20Service : BaseScheduledService
2121 private readonly ILogger < USDT_TRC20Service > _logger ;
2222 private readonly IConfiguration _configuration ;
2323 private readonly ITelegramBotClient _botClient ;
24+ private readonly IHostEnvironment _env ;
2425 private readonly IServiceProvider _serviceProvider ;
2526
2627 public USDT_TRC20Service ( ILogger < USDT_TRC20Service > logger ,
2728 IConfiguration configuration ,
2829 ITelegramBotClient botClient ,
29- IServiceProvider serviceProvider ) : base ( "USDT-TRC20记录检测" , TimeSpan . FromSeconds ( 30 ) , logger )
30+ IHostEnvironment env ,
31+ IServiceProvider serviceProvider ) : base ( "USDT-TRC20记录检测" , TimeSpan . FromSeconds ( 10 ) , logger )
3032 {
3133 _logger = logger ;
3234 _configuration = configuration ;
3335 _botClient = botClient ;
36+ _env = env ;
3437 _serviceProvider = serviceProvider ;
3538 }
3639
@@ -44,50 +47,61 @@ protected override async Task ExecuteAsync()
4447 var _bindRepository = provider . GetRequiredService < IBaseRepository < TokenBind > > ( ) ;
4548
4649 var payMinTime = DateTime . Now . AddSeconds ( - 60 * 5 ) ;
47- var payMaxTime = DateTime . Now ;
4850 var addressArray = _configuration . GetSection ( "Address:USDT-TRC20" ) . Get < string [ ] > ( ) ;
4951 if ( addressArray . Length == 0 )
5052 {
5153 _logger . LogWarning ( "未配置USDT收款地址!" ) ;
5254 return ;
5355 }
56+ var ContractAddress = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t" ;
57+ var BaseUrl = "https://api.trongrid.io" ;
58+ if ( ! _env . IsProduction ( ) )
59+ {
60+ ContractAddress = "TG3XXyExBkPp9nzdajDZsozEu4BkaSJozs" ;
61+ BaseUrl = "https://api.shasta.trongrid.io" ;
62+ }
5463 foreach ( var address in addressArray )
5564 {
5665
57- var data = new
58- {
59- start = 0 ,
60- limit = 200 ,
61- direction = "in" ,
62- tokens = _myTronConfig . Value . USDTContractAddress ,
63- relatedAddress = address ,
64- toAddress = address ,
65- start_timestamp = ( long ) payMinTime . ToUnixTimeStamp ( ) ,
66- end_timestamp = ( long ) payMaxTime . ToUnixTimeStamp ( )
67- } ;
68- var transfers = await _myTronConfig . Value . ApiHost
69- . AppendPathSegment ( "api/token_trc20/transfers" )
70- . SetQueryParams ( data )
71- . GetJsonAsync ( ) ;
72- if ( transfers . total > 0 )
66+ var query = new Dictionary < string , object > ( ) ;
67+ query . Add ( "only_confirmed" , true ) ;
68+ query . Add ( "only_to" , true ) ;
69+ query . Add ( " limit" , 50 ) ;
70+ query . Add ( "min_timestamp" , ( long ) payMinTime . ToUnixTimeStamp ( ) ) ;
71+ query . Add ( "contract_address" , ContractAddress ) ;
72+ var req = BaseUrl
73+ . AppendPathSegment ( $ "v1/accounts/ { address } /transactions/trc20" )
74+ . SetQueryParams ( query )
75+ . WithTimeout ( 15 ) ;
76+ if ( _env . IsProduction ( ) )
77+ req = req . WithHeader ( "TRON-PRO-API-KEY" , _configuration . GetValue ( "TRON-PRO-API-KEY" , "" ) ) ;
78+ var result = await req
79+ . GetJsonAsync < BaseResponse < Transactions > > ( ) ;
80+
81+ if ( result . Success && result . Data ? . Count > 0 )
7382 {
74- var list = ( IList < dynamic > ) transfers . token_transfers ;
75- foreach ( var item in list )
83+ foreach ( var item in result . Data )
7684 {
77- //收款地址相同,已确认的订单
78- if ( item . to_address != address || item . finalResult != "SUCCESS" ) continue ;
85+ //合约地址不匹配
86+ if ( item . TokenInfo ? . Address != ContractAddress )
87+ {
88+ continue ;
89+ }
90+ //收款地址相同
91+ if ( item . To != address ) continue ;
7992 //实际支付金额
80- var amount = Convert . ToDecimal ( item . quant ) / 1_000_000 ;
93+ var amount = item . Amount ;
8194 var record = new TokenRecord
8295 {
83- BlockTransactionId = item . transaction_id ,
84- FromAddress = item . from_address ,
85- ToAddress = item . to_address ,
96+ BlockTransactionId = item . TransactionId ,
97+ FromAddress = item . From ,
98+ ToAddress = item . To ,
99+ //ContractAddress = item.TokenInfo.Address,
86100 OriginalAmount = amount ,
87101 OriginalCurrency = Currency . USDT ,
88102 ConvertCurrency = Currency . TRX ,
89103 Status = Status . Pending ,
90- ReceiveTime = ( ( long ) item . block_ts ) . ToDateTime ( )
104+ ReceiveTime = item . BlockTimestamp . ToDateTime ( )
91105 } ;
92106 if ( ! await _repository . Where ( x => x . BlockTransactionId == record . BlockTransactionId ) . AnyAsync ( ) )
93107 {
@@ -96,7 +110,7 @@ protected override async Task ExecuteAsync()
96110 var AdminUserId = _configuration . GetValue < long > ( "BotConfig:AdminUserId" ) ;
97111 try
98112 {
99- var viewUrl = $ "https://nile .tronscan.org/#/transaction/{ record . BlockTransactionId } ";
113+ var viewUrl = $ "https://shasta .tronscan.org/#/transaction/{ record . BlockTransactionId } ";
100114 if ( hostEnvironment . IsProduction ( ) )
101115 {
102116 viewUrl = $ "https://tronscan.org/#/transaction/{ record . BlockTransactionId } ";
@@ -109,21 +123,19 @@ protected override async Task ExecuteAsync()
109123 Bot . Types . ReplyMarkups . InlineKeyboardButton . WithUrl ( "查看区块" , viewUrl ) ,
110124 } ,
111125 } ) ;
112- var _rateRepository = provider . GetRequiredService < IBaseRepository < TokenRate > > ( ) ;
113- var rate = await _rateRepository . Where ( x => x . Currency == Currency . USDT && x . ConvertCurrency == Currency . TRX ) . FirstAsync ( x => x . Rate ) ;
126+
114127 var binds = await _bindRepository . Where ( x => x . Currency == Currency . TRX && x . Address == record . FromAddress ) . ToListAsync ( ) ;
115128 if ( binds . Count > 0 )
116129 {
117130 foreach ( var bind in binds )
118131 {
119132 try
120133 {
121- await _botClient . SendTextMessageAsync ( bind . UserId , $@ "<b>我们已经收到您的USDT </b>
134+ await _botClient . SendTextMessageAsync ( bind . UserId , $@ "<b>我们已经收到您转出的USDT </b>
122135金额:<b>{ record . OriginalAmount : #.######} { record . OriginalCurrency } </b>
123136哈希:<code>{ record . BlockTransactionId } </code>
124137时间:<b>{ record . ReceiveTime : yyyy-MM-dd HH:mm:ss} </b>
125138地址:<code>{ record . FromAddress } </code>
126- 预估:<b>{ record . OriginalAmount . USDT_To_TRX ( rate , UpdateHandlers . FeeRate ) } TRX</b>
127139
128140您的兑换申请已进入队列,预计5分钟内转入您的账户!
129141" , Bot . Types . Enums . ParseMode . Html , replyMarkup : inlineKeyboard ) ;
@@ -136,13 +148,17 @@ await _botClient.SendTextMessageAsync(bind.UserId, $@"<b>我们已经收到您
136148 }
137149 if ( AdminUserId > 0 )
138150 {
151+ var _rateRepository = provider . GetRequiredService < IBaseRepository < TokenRate > > ( ) ;
152+ var rate = await _rateRepository . Where ( x => x . Currency == Currency . USDT && x . ConvertCurrency == Currency . TRX ) . FirstAsync ( x => x . Rate ) ;
139153 await _botClient . SendTextMessageAsync ( AdminUserId , $@ "<b>USDT入账通知!({ record . OriginalAmount : #.######} { record . OriginalCurrency } )</b>
140154
141155订单:<code>{ record . BlockTransactionId } </code>
156+ 原币:<b>{ record . OriginalCurrency } </b>
142157转入:<b>{ record . OriginalAmount : #.######} { record . OriginalCurrency } </b>
143158来源:<code>{ record . FromAddress } </code>
144159接收:<code>{ record . ToAddress } </code>
145- 预估:<b>{ record . OriginalAmount . USDT_To_TRX ( rate , UpdateHandlers . FeeRate ) } TRX</b>
160+ 转换:<b>{ record . ConvertCurrency } </b>
161+ 预估:<b>{ record . OriginalAmount . USDT_To_TRX ( rate ) } TRX</b>
146162时间:<b>{ record . ReceiveTime : yyyy-MM-dd HH:mm:ss} </b>
147163" , Bot . Types . Enums . ParseMode . Html , replyMarkup : inlineKeyboard ) ;
148164 }
0 commit comments