ton

package
v0.0.0-...-fb0f066 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package ton provider bindings for TON blockchain including Gateway contract wrapper.

Index

Constants

View Source
const (
	SendFlagSeparateFees = uint8(1)
	SendFlagIgnoreErrors = uint8(2)
)

See https://docs.ton.org/v3/documentation/smart-contracts/message-management/message-modes-cookbook

Variables

View Source
var (
	ErrParse     = errors.New("unable to parse tx")
	ErrUnknownOp = errors.New("unknown op")
	ErrCast      = errors.New("unable to cast tx content")
)

Functions

func Coins

func Coins(amount uint64) math.Uint

Coins takes amount in TON and returns it in nano tons. Example Coins(5) return math.Uint(5 * 10^9) nano tons.

func ErrCollect

func ErrCollect(errs ...error) error

func FilterInbounds

func FilterInbounds(tx *Transaction) bool

FilterInbounds filters transactions with deposit operations

func FormatCoins

func FormatCoins(v math.Uint) string

func GatewayCode

func GatewayCode() *boc.Cell

GatewayCode returns Gateway's code as cell

func GatewayStateInit

func GatewayStateInit(authority ton.AccountID, tss eth.Address, depositsEnabled bool) *boc.Cell

GatewayStateInit returns Gateway's stateInit as cell

func GramsToUint

func GramsToUint(g tlb.Grams) math.Uint

func MarshalSnakeCell

func MarshalSnakeCell(data []byte) (*boc.Cell, error)

MarshalSnakeCell encodes []byte to TLB using snake-cell encoding

func MarshalTLB

func MarshalTLB(v tlb.MarshalerTLB) (*boc.Cell, error)

MarshalTLB encodes entity to BOC

func UintToCoins

func UintToCoins(v math.Uint) tlb.Coins

func UnmarshalEVMAddress

func UnmarshalEVMAddress(cell *boc.Cell) (eth.Address, error)

UnmarshalEVMAddress decodes eth.Address from BOC

func UnmarshalSnakeCell

func UnmarshalSnakeCell(cell *boc.Cell) ([]byte, error)

UnmarshalSnakeCell decodes TLB cell to []byte using snake-cell encoding

func UnmarshalTLB

func UnmarshalTLB(t tlb.UnmarshalerTLB, cell *boc.Cell) error

UnmarshalTLB decodes entity from BOC

Types

type Call

type Call struct {
	Sender    ton.AccountID
	Recipient eth.Address
	CallData  []byte
}

Call represents a call operation

func (Call) AsBody

func (c Call) AsBody() (*boc.Cell, error)

type Client

type Client interface {
	SendMessage(ctx context.Context, payload []byte) (uint32, error)
}

Client represents a sender that allows sending an arbitrary external message to the network.

type Deposit

type Deposit struct {
	Sender    ton.AccountID
	Amount    math.Uint
	Recipient eth.Address
}

Deposit represents a deposit operation

func (Deposit) AsBody

func (d Deposit) AsBody() (*boc.Cell, error)

AsBody casts struct as internal message body.

type DepositAndCall

type DepositAndCall struct {
	Deposit
	CallData []byte
}

DepositAndCall represents a deposit and call operation

func (DepositAndCall) AsBody

func (d DepositAndCall) AsBody() (*boc.Cell, error)

AsBody casts struct to internal message body.

type Donation

type Donation struct {
	Sender ton.AccountID
	Amount math.Uint
}

Donation represents a donation operation

func (Donation) AsBody

func (d Donation) AsBody() (*boc.Cell, error)

AsBody casts struct as internal message body.

type ExitCode

type ExitCode uint32

ExitCode represents an error code. Might be TVM or custom. TVM: https://docs.ton.org/v3/documentation/tvm/tvm-exit-codes Zeta: https://github.com/zeta-chain/protocol-contracts-ton/blob/main/contracts/common/errors.fc

const (
	ExitCodeInvalidSeqno ExitCode = 109
)

type ExternalMsg

type ExternalMsg interface {
	Hash() ([32]byte, error)
	AsBody() (*boc.Cell, error)

	Signature() [65]byte
	SetSignature(sig [65]byte)
	// contains filtered or unexported methods
}

type Filter

type Filter func(*Transaction) bool

type Gateway

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

Gateway represents bindings for Zeta Gateway contract on TON

Gateway.ParseTransaction parses Gateway transaction. The parser reads tx body cell and decodes it based on Operation code (op)

  • inbound transactions: deposit, donate, depositAndCall
  • outbound transactions: not implemented yet
  • errors for all other transactions

`Send*` methods work the same way by constructing (& signing) tx body cell that is expected by the contract

@see https://github.com/zeta-chain/protocol-contracts-ton/blob/main/contracts/gateway.fc

func NewGateway

func NewGateway(accountID ton.AccountID) *Gateway

NewGateway Gateway constructor

func (*Gateway) AccountID

func (gw *Gateway) AccountID() ton.AccountID

AccountID returns gateway address

func (*Gateway) GetTxFee

func (gw *Gateway) GetTxFee(ctx context.Context, client MethodRunner, op Op) (math.Uint, error)

GetTxFee returns maximum transaction fee for the given operation. Real fee may be lower.

func (*Gateway) ParseAndFilter

func (gw *Gateway) ParseAndFilter(tx ton.Transaction, filter Filter) (*Transaction, bool, error)

ParseAndFilter parses transaction and applies filter to it. Returns (tx, skip?, error) If parse fails due to known error, skip is set to true

func (*Gateway) ParseAndFilterMany

func (gw *Gateway) ParseAndFilterMany(txs []ton.Transaction, filter Filter) []*Transaction

ParseAndFilterMany parses and filters many txs.

func (*Gateway) ParseTransaction

func (gw *Gateway) ParseTransaction(tx ton.Transaction) (*Transaction, error)

ParseTransaction parses transaction to Transaction

func (*Gateway) SendCall

func (gw *Gateway) SendCall(
	ctx context.Context,
	s Sender,
	amount math.Uint,
	zevmRecipient eth.Address,
	callData []byte,
	sendMode uint8,
) error

SendCall sends `call` operation to the gateway on behalf of the sender. The amount should be >= calculate_gas_fee(op::call)

func (*Gateway) SendDeposit

func (gw *Gateway) SendDeposit(
	ctx context.Context,
	s Sender,
	amount math.Uint,
	zevmRecipient eth.Address,
	sendMode uint8,
) error

SendDeposit sends a deposit operation to the gateway on behalf of the sender.

func (*Gateway) SendDepositAndCall

func (gw *Gateway) SendDepositAndCall(
	ctx context.Context,
	s Sender,
	amount math.Uint,
	zevmRecipient eth.Address,
	callData []byte,
	sendMode uint8,
) error

SendDepositAndCall sends a deposit operation to the gateway on behalf of the sender with a callData to the recipient.

func (*Gateway) SendExternalMessage

func (gw *Gateway) SendExternalMessage(ctx context.Context, s Client, msg ExternalMsg) (uint32, error)

SendExternalMessage sends an external message to the Gateway.

func (*Gateway) SendResetSeqno

func (gw *Gateway) SendResetSeqno(
	ctx context.Context,
	s Sender,
	amount math.Uint,
	newSeqno uint32,
	sendMode uint8,
) error

SendResetSeqno sends an admin operation to reset the gateway's seqno (nonce).

func (*Gateway) SendUpdateTSS

func (gw *Gateway) SendUpdateTSS(
	ctx context.Context,
	s Sender,
	amount math.Uint,
	newTSSAddress eth.Address,
	sendMode uint8,
) error

SendUpdateTSS sends an admin operation to update the TSS address on the gateway.

type IncreaseSeqno

type IncreaseSeqno struct {
	Seqno      uint32
	ReasonCode uint32
	Sig        [65]byte
}

IncreaseSeqno represents an external message (an alternative to Withdrawal) that only increases seqno (nonce) and might contain reason code. Used as a factual tx for "canceling" CCTX.

func (*IncreaseSeqno) AsBody

func (is *IncreaseSeqno) AsBody() (*boc.Cell, error)

func (*IncreaseSeqno) Hash

func (is *IncreaseSeqno) Hash() ([32]byte, error)

func (*IncreaseSeqno) SetSignature

func (is *IncreaseSeqno) SetSignature(sig [65]byte)

func (*IncreaseSeqno) Signature

func (is *IncreaseSeqno) Signature() [65]byte

func (*IncreaseSeqno) Signer

func (is *IncreaseSeqno) Signer() (eth.Address, error)

Signer returns EVM address of the signer (e.g. TSS)

type MethodRunner

type MethodRunner interface {
	RunSmcMethod(ctx context.Context, acc ton.AccountID, method string, params tlb.VmStack) (uint32, tlb.VmStack, error)
}

type Op

type Op uint32

Op operation code

const (
	OpDonate Op = 100 + iota
	OpDeposit
	OpDepositAndCall
	OpCall
)

github.com/zeta-chain/protocol-contracts-ton/blob/main/contracts/gateway.fc Inbound operations

const (
	OpWithdraw      Op = 200
	OpIncreaseSeqno Op = 205
	OpUpdateTSS     Op = 202
	OpResetSeqno    Op = 206
)

Outbound operations

type OutboundAuth

type OutboundAuth struct {
	Seqno  uint32
	Sig    [65]byte
	Signer eth.Address
}

OutboundAuth contains the outbound seqno, signature and signer.

type ResetSeqno

type ResetSeqno struct {
	NewSeqno uint32
}

ResetSeqno represents an admin operation to reset the gateway's seqno (nonce).

func (ResetSeqno) AsBody

func (r ResetSeqno) AsBody() (*boc.Cell, error)

AsBody casts struct as internal message body.

type Sender

type Sender interface {
	Send(ctx context.Context, messages ...wallet.Sendable) error
}

Sender TON tx sender. Usually an interface to a wallet.

type Transaction

type Transaction struct {
	ton.Transaction
	Operation Op
	ExitCode  int32
	// contains filtered or unexported fields
}

Transaction represents a Gateway transaction.

func (*Transaction) Call

func (tx *Transaction) Call() (Call, error)

Call casts the transaction content to a Call.

func (*Transaction) Deposit

func (tx *Transaction) Deposit() (Deposit, error)

Deposit casts the transaction content to a Deposit.

func (*Transaction) DepositAndCall

func (tx *Transaction) DepositAndCall() (DepositAndCall, error)

DepositAndCall casts the transaction content to a DepositAndCall.

func (*Transaction) Donation

func (tx *Transaction) Donation() (Donation, error)

Donation casts the transaction content to a Donation.

func (*Transaction) GasUsed

func (tx *Transaction) GasUsed() math.Uint

GasUsed returns the amount of gas used by the transaction.

func (*Transaction) IncreaseSeqno

func (tx *Transaction) IncreaseSeqno() (IncreaseSeqno, error)

func (*Transaction) IsInbound

func (tx *Transaction) IsInbound() bool

IsInbound returns true if the transaction is inbound.

func (*Transaction) IsOutbound

func (tx *Transaction) IsOutbound() bool

IsOutbound returns true if the transaction is outbound.

func (*Transaction) OutboundAuth

func (tx *Transaction) OutboundAuth() (OutboundAuth, error)

OutboundAuth returns the outbound seqno and signature

func (*Transaction) Withdrawal

func (tx *Transaction) Withdrawal() (Withdrawal, error)

Withdrawal casts the transaction content to a Withdrawal.

type UpdateTSS

type UpdateTSS struct {
	NewTSSAddress eth.Address
}

UpdateTSS represents an admin operation to update the TSS address on the gateway. This is an authority operation that must be sent by the gateway admin.

func (UpdateTSS) AsBody

func (u UpdateTSS) AsBody() (*boc.Cell, error)

AsBody casts struct as internal message body.

type Withdrawal

type Withdrawal struct {
	Recipient ton.AccountID
	Amount    math.Uint
	Seqno     uint32
	Sig       [65]byte
}

Withdrawal represents a withdrawal external message

func (*Withdrawal) AsBody

func (w *Withdrawal) AsBody() (*boc.Cell, error)

func (*Withdrawal) Hash

func (w *Withdrawal) Hash() ([32]byte, error)

Hash returns hash of the withdrawal message. (used for signing)

func (*Withdrawal) SetSignature

func (w *Withdrawal) SetSignature(sig [65]byte)

SetSignature sets signature to the withdrawal message. Note that signature has the following order: [R, S, V (recovery ID)]

func (*Withdrawal) Signature

func (w *Withdrawal) Signature() [65]byte

func (*Withdrawal) Signer

func (w *Withdrawal) Signer() (eth.Address, error)

Signer returns EVM address of the signer (e.g. TSS)

Jump to

Keyboard shortcuts

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