fikatron

package module
v0.0.0-...-268b35e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 24, 2024 License: MIT Imports: 13 Imported by: 0

README

codecov Build Status go.dev Go Report Card Licenses

fikatron

Use GRPC to communicate with the TRON node to implement balance queries and other related operations.

Basic Usage

Installation

To get the package, execute:

go get github.com/gofika/fikatron
Example
package main

import (
	"encoding/hex"
	"fmt"

	"github.com/btcsuite/btcd/btcec/v2"
	"github.com/gofika/fikatron"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
)

func main() {
	const target = "grpc.trongrid.io:50051"
	client, err := fikatron.NewClient(target, grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		panic(err)
	}

	// calculate address
	privateKeyHex := "a3f4af5e7b28eb215f3887baa4f0e5b8d7e7c8d6e5f4c3b2a190807060504030"
	privateKeyBytes, err := hex.DecodeString(privateKeyHex)
	if err != nil {
		panic(err)
	}
	_, publicKey := btcec.PrivKeyFromBytes(privateKeyBytes)
	address := fikatron.AddressFromPublicKey(publicKey.ToECDSA())

	// get account
	account, err := client.Account(address)
	if err != nil {
		panic(err)
	}
	fmt.Printf("address: %s, balance: %d\n", address, account.Balance)

	// get TRC20-USDT balance
	const usdtContractAddress = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t"
	balance, err := client.TRC20Balance(usdtContractAddress, usdtContractAddress)
	if err != nil {
		panic(err)
	}
	fmt.Printf("address: %s, TRC20-USDT balance: %s\n", usdtContractAddress, balance.String())
}

Documentation

Index

Constants

View Source
const (
	TronBytePrefix = 0x41
	AddressLength  = 20
)

Variables

View Source
var (
	ErrInvalidAddress = errors.New("invalid address")
)

Functions

func AddressFromPublicKey

func AddressFromPublicKey(publicKey *ecdsa.PublicKey) string

AddressFromPublicKey convert public key to Tron address

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client client for the TRON RPC

func NewClient

func NewClient(target string, opts ...grpc.DialOption) (*Client, error)

NewClient creates a new client

func (*Client) Account

func (c *Client) Account(address string) (*core.Account, error)

Account gets the account information

func (*Client) Close

func (c *Client) Close() error

Close closes the client

func (*Client) TRC20Balance

func (c *Client) TRC20Balance(address, contractAddress string) (*big.Int, error)

TRC20Balance gets the balance of a TRC20 token

func (*Client) TRC20Call

func (c *Client) TRC20Call(from, contractAddress string, data []byte, constant bool, feeLimit uint64) (*api.TransactionExtention, error)

TRC20Call calls a TRC20 contract

func (*Client) Target

func (c *Client) Target() string

Target returns the target string of the ClientConn.

func (*Client) WalletClient

func (c *Client) WalletClient() api.WalletClient

WalletClient returns the wallet client If you need to call other RPCs, you can use this to get the client

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL