Skip to content
Integra Protocol
API

@integraledger/lcp/tvm

TON: the pairing `x402/exact/tvm`. The ATR hash rides in the scheme's `extra.forwardPayload` as a TEP-74 text comment holding its LCP string.

For AI agents: this page as Markdown, and llms.txt, the index of every page.

TON: the pairing x402/exact/tvm. The ATR hash rides in the scheme's extra.forwardPayload as a TEP-74 text comment holding its LCP string.

The payer's W5 wallet signs a request whose one Jetton transfer carries that payload; settlement is read from the W5 wallet's transaction with that request and the Jetton wallets' transactions it led to, through a bounded reader.

Interfaces

TonAccountState

An account's status and data hash, as TON Center v3 states them before or after a transaction.

Properties

PropertyTypeDescription
dataHashstring | nullThe representation hash of the account's data, in the index's spelling; null where the account holds none.
statusTonAccountStatus-

TonCell

A TON cell, as @ton/core's Cell gives one: its data bits, its references and its representation hash. The entry point's public types name this shape rather than the optional peer's class, and a Cell is one.

Properties

PropertyModifierType
bitsreadonlyobject
bits.lengthreadonlynumber
refsreadonlyreadonly TonCell[]

Methods

hash()

hash(level?): Uint8Array

Parameters
ParameterType
level?number
Returns

Uint8Array


TonIndexed

The index's statement of the masterchain blocks it holds: every block from first through last, a finalized block.

Properties

PropertyTypeDescription
firstbigint-
lastbigint-
utimenumberlast's gen_utime.

A transaction of the wallet's list by the two members its successor links it by: its hash and its logical time.

Properties

PropertyModifierTypeDescription
hashreadonlystring-
ltreadonlystringIn decimal.

TonOutMsg

A transaction's out message: its hash, its body's opcode (null where it has none), and its body as a BoC.

Properties

PropertyTypeDescription
body?Uint8Array<ArrayBufferLike> | nullThe body as a bag of cells (message_content.body); absent or null where the index gives none.
hashstring-
opcodenumber | null-

TonTx

A transaction as a TON Center v3 endpoint reports it. inBody is the inbound message body as a BoC, or null when the transaction has no inbound message body (its in_msg, or that message's message_content.body, is null). The members after outMsgs are the ones the expiry read takes, from /api/v3/transactions: a transaction lacking one cannot be placed, linked or classed, and the read holds its search there.

Properties

PropertyTypeDescription
abortedboolean-
accountstring-
after?TonAccountStateThe account's state after it: its status TON Center v3's end_status, its data hash account_state_after's data_hash.
before?TonAccountStateThe account's state before it: its status TON Center v3's orig_status, its data hash account_state_before's data_hash. A transaction whose account did not exist before it, the account's deploy, carries it with the status nonexist, its orig_status, though account_state_before then states no account_status; so given, the expiry read does not hold its search there.
finality0 | 1 | 2TON Center v3's FinalityState: 0 pending, 1 confirmed, 2 finalized.
hashstring-
inBodyUint8Array<ArrayBufferLike> | null-
inSource?string | nullThe inbound message's source, raw (in_msg.source); null for an external message.
lt?stringIts logical time, in decimal (lt).
mcSeqno?bigint | nullThe masterchain block that holds it (mc_block_seqno); null while none does.
outMsgsreadonly TonOutMsg[]-
prev?{ hash: string; lt: string; } | nullIts predecessor on the account (prev_trans_hash, prev_trans_lt); null for the account's first.

TonWalletState

A wallet's state as the index holds it (walletStates): status, code hash, sequence number, last transaction.

Properties

PropertyTypeDescription
codeHash`0x${string}` | nullThe representation hash of the account's code, 0x and 64 hex digits; null where it holds none.
last{ hash: string; lt: string; } | nullThe account's last transaction; null where it has none.
seqnonumber | nullThe wallet's sequence number; null where the account holds no data.
statusTonAccountStatus-

TvmChoice

Properties

PropertyTypeDescription
acceptedPaymentRequirements-
attachNanotonsbigintThe value attached to the outgoing message, above forwardTonAmount.
jettonWalletstringThe payer's Jetton wallet for asset, raw (get_wallet_address on the master), on a workchain as wallet is.
nownumberSeconds since the epoch.
requiredPaymentRequired-
seqnonumber-
stateInit?string | TonCellThe wallet's state init, for a wallet not yet deployed: a cell, or a base64 BoC of one root cell. It must be exactly one StateInit, as TON reads a message's state init: no bit or reference after its fields, at most 1,024 cells read, and at most two Merkle proofs or updates nested in it. It must be one TON deploys a wallet from at the configuration of the option's network: no libraries dictionary (global version 15), a fixed prefix length of at most ConfigParam 43's max_acc_fixed_prefix_length, 8 (global version 10), and a representation hash that is wallet's 256-bit address, except the first fixed-prefix-length bits, which may differ, since TON deploys an account only from a state init whose hash is its address (check_in_msg_state_hash). Else x402/option-malformed. The settlement message carries it as its init exactly as given.
walletstringThe payer's W5 wallet, raw, on a workchain TON's configuration holds (-1 or 0; else x402/option-malformed).
walletIdnumberget_subwallet_id.

TvmReader

Bounded, read-only calls against one network's TON Center v3 endpoint. Every failure rejects, for example with ReaderError from @integraledger/lcp/evm: one of kind too-large reads as too-large, one of kind malformed as unparseable, and any other rejection as transport.

Properties

PropertyModifierType
networkreadonly`tvm:${number}`

Methods

byHash()

byHash(txHash): Promise<TonTx | null>

GET /api/v3/transactions?hash=…

Parameters
ParameterType
txHashstring
Returns

Promise<TonTx | null>

byInBody()

byInBody(bodyHash): Promise<readonly TonTx[]>

GET /api/v3/transactionsByMessage?body_hash=…&direction=in: the transactions that took a message with this body.

Parameters
ParameterType
bodyHash`0x${string}`
Returns

Promise<readonly TonTx[]>

byInMessage()

byInMessage(msgHash): Promise<readonly TonTx[]>

GET /api/v3/transactionsByMessage?msg_hash=…&direction=in

Parameters
ParameterType
msgHashstring
Returns

Promise<readonly TonTx[]>

headUtime()

headUtime(): Promise<number>

GET /api/v3/masterchainInfo: the latest masterchain block's gen_utime, the latest head the endpoint serves.

Returns

Promise<number>

indexed()

indexed(): Promise<TonIndexed | null>

The index's own statement of the masterchain blocks through which it holds every block, from the same index that answers the other calls, or null where it states none. masterchainInfo's last is no such statement: it can be ahead of an earlier block the index has not finished writing.

Returns

Promise<TonIndexed | null>

jettonMaster()

jettonMaster(jettonWallet): Promise<string>

runGetMethod get_wallet_data on a Jetton wallet: its master, raw.

Parameters
ParameterType
jettonWalletstring
Returns

Promise<string>

jettonWallet()

jettonWallet(master, owner): Promise<string>

runGetMethod get_wallet_address on a Jetton master for an owner: the owner's Jetton wallet, raw.

Parameters
ParameterType
masterstring
ownerstring
Returns

Promise<string>

transactions()

transactions(account, from, limit): Promise<readonly TonTx[]>

GET /api/v3/transactions?account=…&end_lt=…&sort=desc&limit=…: the account's transactions from from back, newest first, at most limit, each with the members the expiry read takes.

Parameters
ParameterType
accountstring
from{ hash: string; lt: string; }
from.hashstring
from.ltstring
limitnumber
Returns

Promise<readonly TonTx[]>

walletState()

walletState(account): Promise<TonWalletState>

GET /api/v3/walletStates?address=…: the wallet's state as the index holds it.

Parameters
ParameterType
accountstring
Returns

Promise<TonWalletState>


TvmRef

The read keys a payment's settlement is read by, the members of the payment presented, and the payee, the payTo of the option the payment answers, for both party tests and no search key. It names no asset.

Properties

PropertyTypeDescription
codeHash?`0x${string}`The representation hash of the code in the state init the payment carries; absent where it carries none.
h`0x${string}`H, which the request's Jetton transfer carries: the search for H looks for it in every run of the wallet.
jettonWalletstringThe payer's Jetton wallet, raw: the address TON sends the W5 request's one message to.
network`tvm:${number}`-
payeestring-
requestBodyHash`0x${string}`The representation hash of the signed W5 request: the whole body of the message to the payer's wallet, its signature and any bit or reference after has_other_actions 0, which W5 does not read, included.
seqnonumberThe sequence number the request was signed against: the single-use value.
signedHash`0x${string}`The representation hash of the W5 request with its signature removed, its last 512 bits: the hash the wallet's key signs. It holds the members of the payment presented, the request the payer signed, whole, its signature apart.
transferBodyHash`0x${string}`The representation hash of the Jetton transfer body the W5 request sends.
validUntilnumber-
walletstringThe payer's W5 wallet, raw: the address TON sends the settlement message to.

TvmSearch

The search position of the expiry read, from which the next read resumes: top, the newest transaction of the wallet the search had listed, and next, the transaction it reads next, at or below which nothing is read yet. The next read lists the wallet's transactions above top, which landed since, then reads on from next; the ones between were read.

Properties

PropertyModifierType
nextreadonlyTonLink
topreadonlyTonLink

TvmUnsigned

Properties

PropertyTypeDescription
requestobjectWhat the wallet key signs. signedBoc is the W5 request, its signature not in it, as a base64 bag of cells of one root; hash is that root cell's 32-byte representation hash. tvmRequest reads them.
request.hashUint8Array<ArrayBuffer>-
request.kind"ton-w5"-
request.signedBocstring-

Methods

complete()

complete(signature): Refusal | TvmPayment

Takes the 64-byte Ed25519 signature.

Parameters
ParameterType
signatureUint8Array
Returns

Refusal | TvmPayment

Type Aliases

TonAccountStatus

TonAccountStatus = "active" | "uninit" | "nonexist" | "frozen"

An account's status as TON Center v3 states it.


TvmMark

TvmMark = "confirmed" | "finalized"


TvmNetwork

TvmNetwork = `tvm:${number}`

tvm:<global_id>: tvm:-239 mainnet, tvm:-3 testnet.


TvmStatus

TvmStatus = StatusAnswer<bigint, TvmMark, TvmSearch>

The answer of tvmStatus, as the profile read-status gives it: each transfer of what moved named by the W5 transaction and, as its place, the hash of the Jetton transfer message that made it. Its marks are confirmed and finalized, TON Center v3's finality states 1 and 2, in that order; a position is a masterchain block's sequence number, and the search position a TvmSearch.

Variables

exactTvm

const exactTvm: Readonly<{ advertise: (doc, h, link, offer, agreementUrl?) => Refusal | PaymentRequired; bound: (presented) => Promise<Refusal | `0x${string}`>; broadcaster: (presented) => Promise<Refusal | Broadcaster>; build: (c, h) => Promise<Refusal | TvmUnsigned>; claims: boolean; data: PairingData; id: "x402/exact/tvm"; pattern: LcpPattern; read: (doc) => Refusal | X402Read; recover: (input, reader) => Promise<Refusal | `0x${string}`>; reference: (presented, option) => Promise<Refusal | TvmRef>; status: (ref, reader, query) => Promise<TvmStatus>; tie: (accepts, request) => ["x402", { accepts: readonly PaymentRequirements[]; request: RequestCommitment | ToolCallCommitment; }]; unplaced: (option) => Refusal | PaymentRequirements; }>

x402/exact/tvm. The payee broadcasts (broadcaster answers "payee"): the seller's facilitator sends the request the payer's W5 wallet signed; status reads the wallet's transaction and the Jetton transfers it led to.


MAX_TVM_ATTACHED_ABOVE_FORWARD

const MAX_TVM_ATTACHED_ABOVE_FORWARD: 30000000n = 30_000_000n

The most nanotons build attaches above the option's forward TON amount: x402's TON facilitator (@x402/tvm) refuses a request whose attached value is above forwardTonAmount plus this (@x402/tvm 2.30.0, src/exact/facilitator/scheme.ts:326-329, DEFAULT_JETTON_WALLET_MESSAGE_AMOUNT in src/constants.ts:14).


MAX_TVM_BOC_BYTES

const MAX_TVM_BOC_BYTES: 8192 = 8192

The largest bag of cells the pairing reads, in bytes: lcp's own resource bound, far above the settlement message of one W5 request with a deploying state init (a few hundred bytes).


MAX_TVM_CELLS

const MAX_TVM_CELLS: 512 = 512

The most distinct cells a bag of cells the pairing reads may hold: lcp's own resource bound, as above.


MAX_TVM_DEPTH

const MAX_TVM_DEPTH: 32 = 32

The deepest a bag of cells the pairing reads may be: lcp's own resource bound, as above.


MAX_TVM_LISTED

const MAX_TVM_LISTED: 256 = 256

The most transactions of the payer's wallet one read lists, back from its latest: lcp's own resource bound. Past it the search is held at the last masterchain block read.


OP

const OP: object

Type Declaration

NameTypeDefault value
internalSigned19362893960x73696e74
internalTransfer3951342330x178d4519
jettonTransfer2607346290x0f8a7ea5
sendMsg2477118530x0ec3c86d

TVM_EXPIRY_MARGIN

const TVM_EXPIRY_MARGIN: 60 = 60

The seconds a masterchain block's time must pass validUntil by before the read takes the request's validity as passed at that block: a W5 transaction runs at its shard block's time, and a masterchain block references that shard block only later, so the masterchain head can pass validUntil before an index holds a transaction that ran in time.


W5R1_CODE_HASH

const W5R1_CODE_HASH: "0x20834b7b72b112147e1b2fb457b84e74d1a30f04f737d4f62a668e9552d2b72f" = "0x20834b7b72b112147e1b2fb457b84e74d1a30f04f737d4f62a668e9552d2b72f"

W5R1's code hash, the one entry of the scheme's list of wallets (scheme_exact_ton.md:166 at x402-foundation/x402@88b66dd4).

Functions

lcpComment()

lcpComment(h): Refusal | TonCell

TEP-74's text comment: 32 zero bits, then the UTF-8 of the hash's LCP string. A value that is not a 32-byte hash is x402/payload-malformed.

Parameters

ParameterType
h`0x${string}`

Returns

Refusal | TonCell


tvmCarrier()

tvmCarrier(settlementBoc, network): Refusal | { codeHash?: `0x${string}`; h: `0x${string}`; jettonWallet: string; payload: TonCell; requestBodyHash: `0x${string}`; seqno: number; signedHash: `0x${string}`; transferBodyHash: `0x${string}`; validUntil: number; wallet: string; }

Reads the signed request in a settlement BoC: an internal message whose body is a W5 internal_signed request with exactly one action_send_msg behind an empty list, carrying a Jetton transfer whose forward payload is a reference to a text comment holding an LCP string. The request is read as W5's internal_signed path reads it (ton-blockchain/wallet-contract-v5 contracts/wallet_v5.fc, recv_internal, process_signed_request and process_actions): at least 640 bits, its signature the body's last 512 bits, and the signed part the rest with all its references, in which the wallet id, valid_until, seqno, the action list's Maybe ^Cell and has_other_actions are read; W5 reads nothing after has_other_actions 0, and neither does this, so bits and references there are carried, not read. A body W5 does not run as a signed request, or whose signed part ends before those fields, is tvm/not-w5-signed, and an empty action list or has_other_actions 1 is tvm/actions. The signature is not verified. The BoC's bytes are read as TON deserializes a bag of cells, and bytes TON refuses are tvm/boc-malformed. requestBodyHash and transferBodyHash are the representation hashes TON gives, for any cell TON reads, a pruned branch of any level mask included. Each message is read as TON's action phase sends it, at the configuration lcp holds for network, as bound and reference read the payment of an option on that network: a source of addr_none or an addr_std with no anycast (the outgoing message's the W5 wallet's own), a destination TON sends to, a value and an extra_flags of at most 3 each in its shortest form, its state init validated as TON validates it, a referenced body that is an ordinary cell with nothing after it, and at most two nested Merkle proofs or updates; the W5 action's send mode is one TON sends with. The network is tvm:-239 (mainnet) or tvm:-3 (testnet), and any other is tvm/network-malformed. The settlement message TON would not send is tvm/boc-malformed, and the outgoing one tvm/not-jetton-transfer; a send mode other than 1 or 3, the two the scheme admits, is tvm/actions. It also gives the request's sequence number, signedHash, the representation hash of the request less its signature, which the wallet's key signs, and, where the message carries a state init, the hash of its code. The settlement message's source names the account that relays it, which the BoC cannot show: one naming another account is read, and TON sends it when that account relays it. wallet and jettonWallet are the addresses TON sends to: an addr_var of 256 bits names its addr_std. The network, the base64, the 8 KiB bound, the bag-of-cells layout and its CRC32C need no peer: without @ton/core they give the codes they give with it, and a BoC that passes them is tvm/peer-missing.

Parameters

ParameterType
settlementBocstring
network`tvm:${number}`

Returns

Refusal | { codeHash?: `0x${string}`; h: `0x${string}`; jettonWallet: string; payload: TonCell; requestBodyHash: `0x${string}`; seqno: number; signedHash: `0x${string}`; transferBodyHash: `0x${string}`; validUntil: number; wallet: string; }


tvmRecover()

tvmRecover(input, reader): Promise<Refusal | `0x${string}`>

Recovers the hash from the payer Jetton wallet's transaction: the comment in its inbound Jetton transfer. A transaction with no inbound message body, or whose body is not a Jetton transfer, is tvm/not-jetton-transfer. A body that is not a bag of cells with one root is an answer out of the reader's documented form, tvm/unreadable. A ref that names no transaction string is tvm/wrong-reader, with nothing read. Without @ton/core the body's cells cannot be read: a transaction with an inbound message body is tvm/peer-missing, after the ref, the reader, the read and the body's size, bag-of-cells layout and CRC32C are checked, which need no peer, so an empty body is tvm/unreadable and one over 8 KiB tvm/boc-too-large either way. The input's tx is the transaction's hash, and its option the option the payment answers, an object, of which the read needs no member; an input whose tx is not a non-empty string, or whose option is not an object, is tvm/wrong-reader, with nothing read. One call.

Parameters

ParameterType
inputRecoverInput<`tvm:${number}`, PaymentRequirements | MppChallenge>
readerTvmReader

Returns

Promise<Refusal | `0x${string}`>


tvmRequest()

tvmRequest(signedBoc, network): Refusal | { amount: string; attachNanotons: bigint; customPayload?: string; extraCurrencies: object[]; forwardTonAmount: string; h: `0x${string}`; hash: Uint8Array<ArrayBuffer>; jettonWallet: string; mode: number; payTo: string; queryId: bigint; responseDestination: string | null; seqno: number; stateInit?: string; validUntil: number; walletId: number; }

Reads the W5 request a ton-w5 request carries, signedBoc, as tvmCarrier reads the signed part of a settlement message's body at the network's configuration, with its codes, and gives hash, the root cell's representation hash, which is the request's hash when the text is its signedBoc. The root must be an ordinary cell whose op is internal_signed, or it is tvm/not-w5-signed. It gives every member W5 reads: the wallet id, validUntil, the sequence number and the one action's send mode; and of the one message that action sends, its destination (jettonWallet), the nanotons it attaches, each extra currency it attaches by its id, and its state init, where it carries one, as a base64 bag of cells; and of the Jetton transfer it carries, the query id, amount, the destination (payTo), the response destination or null for addr_none, the custom payload where it carries one, as a base64 bag of cells, the forward TON amount and the hash the forward payload's comment holds. Addresses are raw. A request holds one action, sending one Jetton transfer, or it is refused, so nothing else runs under the signature; W5 reads nothing after has_other_actions, and neither does this. The request names no wallet, so the message's source is read as the settlement message's sender is, by its form alone.

Parameters

ParameterType
signedBocstring
network`tvm:${number}`

Returns

Refusal | { amount: string; attachNanotons: bigint; customPayload?: string; extraCurrencies: object[]; forwardTonAmount: string; h: `0x${string}`; hash: Uint8Array<ArrayBuffer>; jettonWallet: string; mode: number; payTo: string; queryId: bigint; responseDestination: string | null; seqno: number; stateInit?: string; validUntil: number; walletId: number; }


tvmStatus()

tvmStatus(ref, reader, query): Promise<TvmStatus>

Answers the read of the request as the profile read-status gives it, from the facts the profile x402-exact-tvm (rule 6) names, with presented on settled. The read takes, from one TON Center v3 index: the latest head's time; the index's statement of the masterchain blocks it holds; the W5 wallet's transactions with the request's body; the wallet's state; and, for a W5 wallet, its transactions from the latest back, each by its predecessor, to the first at or before the position given (the expiry read, which is the search for H). Every run of the wallet whose Jetton transfer carries H is followed to the payer's Jetton wallet and from its internal_transfer to the Jetton wallet that took it, and tested by the two party tests: the W5 wallet is not the option's payee, on either act; and, on the act payment, the delivering account is the payee's Jetton wallet of the payer's Jetton wallet's master, read by get_wallet_address for payTo through the reader. Positions are masterchain block sequence numbers. The single-use value is the signed sequence number: consumed where a listed run is at it, where the active wallet's number is above it, or where a listed transaction removed or deployed the account; lapsed where the latest head is more than 60 seconds past validUntil, and read at the final mark only where the stated finalized block is too. The search is read whole only through a reader that declares listsWhole. A wallet that is not W5 gets no expiry read: its value is not read, and the body-hash lookup is its search, read whole where the stated block is at or past the latest head. The read decodes the cells it reads itself, without @ton/core, and compares raw addresses itself, so every answer is the same with it and without it. A ref, a reader or a query that is not one, or a ref whose hashes are not 0x and 64 hex digits, is unreadable unparseable, with nothing read; a reader for another network than the ref's is chain-mismatch; a rejected read is transport, or too-large or unparseable for a ReaderError of that kind; an answer out of the reader's form is unparseable. A frozen account, its code removed, runs no request until a message restores the state its hash fixes, its number among it, so it reads as uninit and nonexist do: the number not passed.

Parameters

ParameterType
refTvmRef
readerTvmReader
queryStatusQuery<TvmRef, bigint, TvmMark, TvmSearch>

Returns

Promise<TvmStatus>

References

Broadcaster

Re-exports Broadcaster


TvmPayment

Re-exports TvmPayment


X402Payment

Re-exports X402Payment


X402Read

Re-exports X402Read

Last updated on

On this page