@@ -12,6 +12,7 @@ import (
1212 "github.com/fbsobreira/gotron-sdk/pkg/http/common"
1313 "github.com/fbsobreira/gotron-sdk/pkg/http/fullnode"
1414 "github.com/fbsobreira/gotron-sdk/pkg/http/soliditynode"
15+ "github.com/fbsobreira/gotron-sdk/pkg/proto/core"
1516 "github.com/google/uuid"
1617
1718 "github.com/smartcontractkit/chainlink-common/pkg/logger"
@@ -171,14 +172,37 @@ func (t *TronTxm) broadcastLoop() {
171172 for {
172173 select {
173174 case tx := <- t .BroadcastChan :
174- triggerResponse , err := t .TriggerSmartContract ( ctx , tx )
175+ feeLimit , err := t .calculateFeeLimit ( tx )
175176 if err != nil {
176- // TODO: is it ok to leave this transaction unmarked as fatal?
177- t .Logger .Errorw ("failed to trigger smart contract" , "error" , err , "tx" , tx , "txID" , tx .ID )
177+ t .Logger .Errorw ("failed to calculate fee limit" , "error" , err , "txID" , tx .ID )
178+ continue
179+ }
180+
181+ // Get the latest block info
182+ refBlockBytes , refBlockHash , err := t .computeRefBlockBytesAndHash ()
183+ if err != nil {
184+ t .Logger .Errorw ("failed to compute ref block bytes and hash" , "error" , err , "txID" , tx .ID )
185+ continue
186+ }
187+
188+ txSerializer := Serializer {
189+ TransactionType : core .Transaction_Contract_TriggerSmartContract ,
190+ FromAddress : tx .FromAddress ,
191+ ContractAddress : tx .ContractAddress ,
192+ Method : tx .Method ,
193+ Params : tx .Params ,
194+ CallValueSun : 0 ,
195+ FeeLimitSun : int64 (feeLimit ),
196+ RefBlockBytes : refBlockBytes ,
197+ RefBlockHash : refBlockHash ,
198+ }
199+
200+ coreTx , err := txSerializer .BuildTransaction ()
201+ if err != nil {
202+ t .Logger .Errorw ("failed to build transaction" , "error" , err , "txID" , tx .ID )
178203 continue
179204 }
180205
181- coreTx := triggerResponse .Transaction
182206 txHash := coreTx .TxID
183207
184208 // RefBlockNum is optional and does not seem in use anymore.
@@ -187,7 +211,7 @@ func (t *TronTxm) broadcastLoop() {
187211
188212 _ , err = t .SignAndBroadcast (ctx , tx .FromAddress , coreTx )
189213 if err != nil {
190- t .Logger .Errorw ("transaction failed to broadcast" , "txHash" , txHash , "error" , err , "tx" , tx , "triggerResponse " , triggerResponse , "txID" , tx .ID )
214+ t .Logger .Errorw ("transaction failed to broadcast" , "txHash" , txHash , "error" , err , "tx" , tx , "coreTx " , coreTx , "txID" , tx .ID )
191215 txStore .OnFatalError (tx .ID )
192216 continue
193217 }
@@ -202,6 +226,47 @@ func (t *TronTxm) broadcastLoop() {
202226 }
203227}
204228
229+ func (t * TronTxm ) computeRefBlockBytesAndHash () ([]byte , []byte , error ) {
230+ nowBlock , err := t .GetClient ().GetNowBlockFullNode ()
231+ if err != nil {
232+ return nil , nil , fmt .Errorf ("failed to get now block: %+w" , err )
233+ }
234+
235+ blockNumber := nowBlock .BlockHeader .RawData .Number
236+ blockId , err := hex .DecodeString (nowBlock .BlockID )
237+ if err != nil {
238+ return nil , nil , fmt .Errorf ("failed to decode block id: %+w" , err )
239+ }
240+
241+ refBlockBytes := []byte {byte ((blockNumber >> 8 ) & 0xFF ), byte (blockNumber & 0xFF )} // last 2 bytes
242+ refBlockHash := blockId [8 :16 ]
243+ return refBlockBytes , refBlockHash , nil
244+ }
245+
246+ func (t * TronTxm ) calculateFeeLimit (tx * TronTx ) (int32 , error ) {
247+ energyUsed , err := t .estimateEnergy (tx )
248+ if err != nil {
249+ return 0 , fmt .Errorf ("failed to estimate energy: %+w" , err )
250+ }
251+
252+ energyUnitPrice := DEFAULT_ENERGY_UNIT_PRICE
253+
254+ if energyPrices , err := t .GetClient ().GetEnergyPrices (); err == nil {
255+ if parsedPrice , err := ParseLatestEnergyPrice (energyPrices .Prices ); err == nil {
256+ energyUnitPrice = parsedPrice
257+ } else {
258+ t .Logger .Errorw ("error parsing energy unit price" , "error" , err , "txID" , tx .ID )
259+ }
260+ } else {
261+ t .Logger .Errorw ("failed to get energy unit price" , "error" , err , "txID" , tx .ID )
262+ }
263+
264+ feeLimit := energyUnitPrice * int32 (energyUsed )
265+ paddedFeeLimit := CalculatePaddedFeeLimit (feeLimit , tx .EnergyBumpTimes , t .Config .EnergyMultiplier )
266+
267+ return paddedFeeLimit , nil
268+ }
269+
205270func (t * TronTxm ) TriggerSmartContract (ctx context.Context , tx * TronTx ) (* fullnode.TriggerSmartContractResponse , error ) {
206271 energyUsed , err := t .estimateEnergy (tx )
207272 if err != nil {
0 commit comments