Monitor TRON Deposits in Go
Monitor incoming TRX and TRC20 token deposits using GoTRON SDK. This guide covers two approaches: balance watching and block scanning.
Balance Watching
The simplest approach — poll an account’s balance at regular intervals:
package main
import (
"fmt"
"log"
"time"
"github.com/fbsobreira/gotron-sdk/pkg/client"
)
func main() {
conn := client.NewGrpcClient("grpc.trongrid.io:50051")
if err := conn.Start(); err != nil {
log.Fatal(err)
}
defer conn.Stop()
address := "WATCH_ADDRESS"
var lastBalance int64
for {
acc, err := conn.GetAccount(address)
if err != nil {
log.Printf("Error: %v", err)
time.Sleep(3 * time.Second)
continue
}
if acc.Balance != lastBalance {
fmt.Printf("TRX balance changed: %d -> %d SUN\n",
lastBalance, acc.Balance)
lastBalance = acc.Balance
}
time.Sleep(3 * time.Second)
}
}
Watch TRC20 Token Deposits
Monitor USDT or other TRC20 token balances:
package main
import (
"fmt"
"log"
"math/big"
"time"
"github.com/fbsobreira/gotron-sdk/pkg/client"
)
func main() {
conn := client.NewGrpcClient("grpc.trongrid.io:50051")
if err := conn.Start(); err != nil {
log.Fatal(err)
}
defer conn.Stop()
address := "WATCH_ADDRESS"
usdtContract := "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"
lastBalance := big.NewInt(0)
for {
balance, err := conn.TRC20ContractBalance(address, usdtContract)
if err != nil {
log.Printf("Error: %v", err)
time.Sleep(3 * time.Second)
continue
}
if balance.Cmp(lastBalance) != 0 {
fmt.Printf("USDT balance changed: %s -> %s\n",
lastBalance.String(), balance.String())
lastBalance = new(big.Int).Set(balance)
}
time.Sleep(3 * time.Second)
}
}
Block Scanning
For more detailed deposit tracking, scan blocks for transactions targeting your address:
package main
import (
"fmt"
"log"
"time"
"github.com/fbsobreira/gotron-sdk/pkg/client"
)
func main() {
conn := client.NewGrpcClient("grpc.trongrid.io:50051")
if err := conn.Start(); err != nil {
log.Fatal(err)
}
defer conn.Stop()
// Start from the latest block
block, err := conn.GetNowBlock()
if err != nil {
log.Fatal(err)
}
lastBlock := block.BlockHeader.RawData.Number
for {
current, err := conn.GetNowBlock()
if err != nil {
log.Printf("Error: %v", err)
time.Sleep(3 * time.Second)
continue
}
currentNum := current.BlockHeader.RawData.Number
for num := lastBlock + 1; num <= currentNum; num++ {
b, err := conn.GetBlockByNum(num)
if err != nil {
log.Printf("Error fetching block %d: %v", num, err)
continue
}
for _, tx := range b.Transactions {
// Process each transaction
fmt.Printf("Block %d: tx %x\n", num, tx.Txid)
}
}
lastBlock = currentNum
time.Sleep(3 * time.Second)
}
}
Production Considerations
- Persistence: Store the last processed block number in a database to resume after restarts
- Rate Limiting: TronGrid has rate limits — use your own node for high-volume scanning
- Confirmations: Wait for 19+ confirmations before considering a deposit final
- Error Recovery: Implement retry logic with exponential backoff
Next Steps
- Send USDT on TRON with Go — Send tokens programmatically
- Interact with TRON Smart Contracts — Decode contract events
- GoTRON Code Examples — More real-world examples