txbuilder

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: LGPL-3.0 Imports: 9 Imported by: 0

Documentation

Overview

Package txbuilder provides a fluent builder for native TRON transactions (transfers, staking, voting, etc.) with Build / Send / SendAndConfirm terminal operations.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrZeroAmount     = errors.New("amount must be greater than zero")
	ErrInvalidAddress = errors.New("invalid address")
	ErrMissingRawData = errors.New("invalid transaction: missing raw data")
	ErrAlreadyBuilt   = errors.New("txbuilder: Tx has already been built; create a new one")
)

Sentinel errors for transaction builder validation. Callers can test with errors.Is.

Functions

This section is empty.

Types

type Builder

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

Builder is the entry point for constructing native TRON transactions. Create one with New, then call transaction methods (Transfer, FreezeV2, etc.) to get a Tx with terminal operations.

func New

func New(client Client, opts ...Option) *Builder

New creates a Builder bound to the given client. Options set shared defaults that apply to every transaction produced by this builder.

func (*Builder) DelegateResource

func (b *Builder) DelegateResource(from, to string, resource core.ResourceCode, amount int64, opts ...Option) *DelegateTx

DelegateResource creates a resource delegation transaction. Use .Lock(period) on the returned DelegateTx to enable delegation locking.

func (*Builder) FreezeV2

func (b *Builder) FreezeV2(from string, amount int64, resource core.ResourceCode, opts ...Option) *Tx

FreezeV2 creates a Stake 2.0 freeze transaction.

func (*Builder) Transfer

func (b *Builder) Transfer(from, to string, amount int64, opts ...Option) *Tx

Transfer creates a TRX transfer transaction.

func (*Builder) UnDelegateResource

func (b *Builder) UnDelegateResource(from, to string, resource core.ResourceCode, amount int64, opts ...Option) *Tx

UnDelegateResource creates a resource un-delegation transaction.

func (*Builder) UnfreezeV2

func (b *Builder) UnfreezeV2(from string, amount int64, resource core.ResourceCode, opts ...Option) *Tx

UnfreezeV2 creates a Stake 2.0 unfreeze transaction.

func (*Builder) VoteWitness

func (b *Builder) VoteWitness(from string, opts ...Option) *VoteTx

VoteWitness creates a witness vote transaction. Add votes using the fluent .Vote() or .Votes() methods on the returned VoteTx.

func (*Builder) WithdrawExpireUnfreeze added in v0.26.0

func (b *Builder) WithdrawExpireUnfreeze(from string, timestamp int64, opts ...Option) *Tx

WithdrawExpireUnfreeze creates a transaction to withdraw expired unfrozen balances. The timestamp parameter specifies the expiration cutoff.

type Client

type Client interface {
	TransferCtx(ctx context.Context, from, toAddress string, amount int64) (*api.TransactionExtention, error)
	BroadcastCtx(ctx context.Context, tx *core.Transaction) (*api.Return, error)
	GetTransactionInfoByIDCtx(ctx context.Context, id string) (*core.TransactionInfo, error)
	FreezeBalanceV2Ctx(ctx context.Context, from string, resource core.ResourceCode, frozenBalance int64) (*api.TransactionExtention, error)
	UnfreezeBalanceV2Ctx(ctx context.Context, from string, resource core.ResourceCode, unfreezeBalance int64) (*api.TransactionExtention, error)
	DelegateResourceCtx(ctx context.Context, from, to string, resource core.ResourceCode, delegateBalance int64, lock bool, lockPeriod int64) (*api.TransactionExtention, error)
	UnDelegateResourceCtx(ctx context.Context, owner, receiver string, resource core.ResourceCode, delegateBalance int64) (*api.TransactionExtention, error)
	VoteWitnessAccountCtx(ctx context.Context, from string, witnessMap map[string]int64) (*api.TransactionExtention, error)
	WithdrawExpireUnfreezeCtx(ctx context.Context, from string, timestamp int64) (*api.TransactionExtention, error)
}

Client is the subset of GrpcClient that transaction builders need. Accepting an interface avoids import cycles and simplifies testing.

type DelegateTx

type DelegateTx struct {
	*Tx
	// contains filtered or unexported fields
}

DelegateTx is a delegation transaction builder with a fluent Lock method. It embeds *Tx so all terminal operations (Build, Send, SendAndConfirm) are available directly.

func (*DelegateTx) Lock

func (d *DelegateTx) Lock(period int64) *DelegateTx

Lock enables the delegation lock with the given period (in blocks). Returns itself for chaining.

func (*DelegateTx) WithMemo added in v0.26.0

func (d *DelegateTx) WithMemo(memo string) *DelegateTx

WithMemo attaches a memo to this delegation transaction.

func (*DelegateTx) WithPermissionID added in v0.26.0

func (d *DelegateTx) WithPermissionID(id int32) *DelegateTx

WithPermissionID sets the permission ID for this delegation transaction.

type Option

type Option func(*config)

Option configures a Tx.

func WithMemo

func WithMemo(memo string) Option

WithMemo attaches a memo (stored in RawData.Data) to the transaction.

func WithPermissionID

func WithPermissionID(id int32) Option

WithPermissionID sets the permission ID used for multi-signature transactions.

func WithPollInterval added in v0.26.0

func WithPollInterval(d time.Duration) Option

WithPollInterval sets the interval between confirmation checks in SendAndConfirm. If not set, txcore.DefaultPollInterval is used.

type Receipt

type Receipt = txcore.Receipt

Receipt is an alias for the shared receipt type.

type Tx

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

Tx represents a single prepared transaction with terminal operations.

A Tx is single-use: its Build, Send, or SendAndConfirm method may only be called once. Calling any terminal a second time returns an error. This prevents accidentally broadcasting the same transaction twice or getting unexpected results from a stale builder state.

To create multiple transactions of the same type, call the Builder method (e.g. Transfer, FreezeV2) again to obtain a fresh Tx.

func (*Tx) Build

func (t *Tx) Build(ctx context.Context) (*api.TransactionExtention, error)

Build creates the unsigned transaction, applying any configured options (permission ID, memo, etc.).

Build may only be called once per Tx. Subsequent calls return ErrAlreadyBuilt. Because Send and SendAndConfirm call Build internally, calling any terminal method consumes the Tx.

func (*Tx) Decode

func (t *Tx) Decode(ctx context.Context) (*transaction.ContractData, error)

Decode builds the transaction and decodes the first contract parameter into human-readable fields (base58 addresses, TRX-formatted amounts). Useful for inspecting or displaying what a transaction does before signing.

func (*Tx) Send

func (t *Tx) Send(ctx context.Context, s signer.Signer) (*Receipt, error)

Send builds, signs, and broadcasts the transaction. It returns a Receipt populated from the broadcast response. Like Build, it may only be called once per Tx.

func (*Tx) SendAndConfirm

func (t *Tx) SendAndConfirm(ctx context.Context, s signer.Signer) (*Receipt, error)

SendAndConfirm is like Send but additionally polls GetTransactionInfoByID until the transaction is confirmed or the context is cancelled. Like Build, it may only be called once per Tx.

func (*Tx) Sign added in v0.26.0

func (t *Tx) Sign(ctx context.Context, s signer.Signer) (*core.Transaction, error)

Sign builds and signs the transaction without broadcasting. Returns the signed transaction ready for deferred broadcast or inspection.

func (*Tx) WithMemo added in v0.26.0

func (t *Tx) WithMemo(memo string) *Tx

WithMemo attaches a memo to this transaction. Returns itself for chaining.

func (*Tx) WithPermissionID added in v0.26.0

func (t *Tx) WithPermissionID(id int32) *Tx

WithPermissionID sets the permission ID for multi-signature transactions. Returns itself for chaining.

type VoteTx

type VoteTx struct {
	*Tx
	// contains filtered or unexported fields
}

VoteTx is a vote transaction builder with fluent methods for adding votes. It embeds *Tx so all terminal operations (Build, Send, SendAndConfirm) are available directly.

func (*VoteTx) Vote

func (v *VoteTx) Vote(witnessAddress string, count int64) *VoteTx

Vote adds a vote for the given witness address. Can be called multiple times to build up the vote set. Returns itself for chaining.

func (*VoteTx) Votes

func (v *VoteTx) Votes(votes map[string]int64) *VoteTx

Votes sets all votes at once from a map. Useful for programmatic vote construction. Merges with any previously added votes.

func (*VoteTx) WithMemo added in v0.26.0

func (v *VoteTx) WithMemo(memo string) *VoteTx

WithMemo attaches a memo to this vote transaction.

func (*VoteTx) WithPermissionID added in v0.26.0

func (v *VoteTx) WithPermissionID(id int32) *VoteTx

WithPermissionID sets the permission ID for this vote transaction.

Jump to

Keyboard shortcuts

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