Documentation
¶
Overview ¶
Package txbuilder provides a fluent builder for native TRON transactions (transfers, staking, voting, etc.) with Build / Send / SendAndConfirm terminal operations.
Index ¶
- Variables
- type Builder
- func (b *Builder) DelegateResource(from, to string, resource core.ResourceCode, amount int64, opts ...Option) *DelegateTx
- func (b *Builder) FreezeV2(from string, amount int64, resource core.ResourceCode, opts ...Option) *Tx
- func (b *Builder) Transfer(from, to string, amount int64, opts ...Option) *Tx
- func (b *Builder) UnDelegateResource(from, to string, resource core.ResourceCode, amount int64, opts ...Option) *Tx
- func (b *Builder) UnfreezeV2(from string, amount int64, resource core.ResourceCode, opts ...Option) *Tx
- func (b *Builder) VoteWitness(from string, opts ...Option) *VoteTx
- func (b *Builder) WithdrawExpireUnfreeze(from string, timestamp int64, opts ...Option) *Tx
- type Client
- type DelegateTx
- type Option
- type Receipt
- type Tx
- func (t *Tx) Build(ctx context.Context) (*api.TransactionExtention, error)
- func (t *Tx) Decode(ctx context.Context) (*transaction.ContractData, error)
- func (t *Tx) Send(ctx context.Context, s signer.Signer) (*Receipt, error)
- func (t *Tx) SendAndConfirm(ctx context.Context, s signer.Signer) (*Receipt, error)
- func (t *Tx) Sign(ctx context.Context, s signer.Signer) (*core.Transaction, error)
- func (t *Tx) WithMemo(memo string) *Tx
- func (t *Tx) WithPermissionID(id int32) *Tx
- type VoteTx
Constants ¶
This section is empty.
Variables ¶
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 ¶
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) 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 ¶
VoteWitness creates a witness vote transaction. Add votes using the fluent .Vote() or .Votes() methods on the returned VoteTx.
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 WithPermissionID ¶
WithPermissionID sets the permission ID used for multi-signature transactions.
func WithPollInterval ¶ added in v0.26.0
WithPollInterval sets the interval between confirmation checks in SendAndConfirm. If not set, txcore.DefaultPollInterval is used.
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 ¶
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 ¶
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 ¶
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
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
WithMemo attaches a memo to this transaction. Returns itself for chaining.
func (*Tx) WithPermissionID ¶ added in v0.26.0
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 ¶
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 ¶
Votes sets all votes at once from a map. Useful for programmatic vote construction. Merges with any previously added votes.
func (*VoteTx) WithPermissionID ¶ added in v0.26.0
WithPermissionID sets the permission ID for this vote transaction.