How tonkeeper/w5 works
1. Repository purpose
- This is a TON smart-contract repository for the Wallet V5 standard, centered on an extensible wallet implementation with lower fees, gasless transaction support, and an extension mechanism.
- The architecture is contract-first: the core product is the on-chain wallet code plus the supporting deployment, compilation, and test tooling around it.
2. Core on-chain subsystems
- contracts/wallet_v5.fc is the primary wallet contract implementation.
- contracts/library-deployer.fc is a companion contract used to deploy or manage the wallet library code.
- contracts/imports/stdlib.fc contains shared standard library code used by the contracts.
- These are the authoritative sources for on-chain logic; everything else either compiles, deploys, or verifies them.
3. Compiled artifacts as distribution boundary
- build/wallet_v5.compiled.json and build/library-deployer.compiled.json are generated compilation outputs used by deployment scripts, wrappers, and tests.
- This build/ layer is the boundary between source-level contract code and the deployable bytecode artifacts.
4. Wrapper layer
- wrappers/ contains TypeScript contract wrappers built on ton-core’s Contract abstraction.
- wallet-v5.ts and library-deployer.ts model the deployed contracts and provide serialization/deserialization and address/code handling.
- wallet-v5-test.ts and Errors.ts support test-facing contract behavior and error decoding.
- wallet_v5.compile.ts and library-deployer.compile.ts define the contract compilation pipeline from source to artifact.
5. Tests as behavioral specification
- tests/ is the main verification boundary for the wallet protocol.
- The suite is split by concern: external messages, internal messages, getters, and extensions.
- WalletW5.spec.ts appears to be the main end-to-end test entrypoint, while the other specs validate specific protocol surfaces and edge cases.
- Test utilities in actions.ts, gasUtils.ts, config.ts, utils.ts, and test-only-actions.ts provide reusable transaction setup, gas accounting, and scenario helpers.
6. Deployment and operational scripts
- scripts/deployLibrary.ts, scripts/deployWalletV5.ts, and scripts/deployWalletV5WithoutLibrary.ts form the deployment path for on-chain rollout.
- The split between “with library” and “without library” indicates the architecture supports multiple deployment modes depending on whether the library contract is predeployed.
- scripts/printWalletCode.ts is a utility for extracting or displaying compiled wallet code, likely used for verification and integration purposes.
7. Gas-optimization toolchain
- scripts/disasm.sh, scripts/print.sh, and scripts/scalpel.sh are supporting tools for code inspection and optimization.
- The fift/ directory provides standard Fift tooling and libraries used by these scripts for disassembly/printing and gas analysis.
- Improvements.rst records optimization work and gas reductions, showing gas efficiency is a first-class architectural concern.
8. Specification and documentation boundary
- Specification.md is the protocol-level contract for wallet behavior.
- Contest.md and Improvements.rst document design decisions and optimization history.
- The README points to the spec as the authoritative description of intended wallet behavior, while code and tests implement and enforce it.
9. Type and message schema layer
- types.tlb suggests typed message/layout definitions for TON serialization.
- This acts as a schema boundary between protocol semantics and binary message encoding used by the contracts and wrappers.
10. Data flow: source to deployed wallet
- The typical flow is: contract source in contracts/ → compiled artifacts in build/ → TypeScript wrappers/scripts consume those artifacts → deployment scripts publish the wallet and/or library to TON.
- Tests exercise the same compiled outputs through wrappers, so the same artifact set is shared by deployment and validation.
11. Data flow: external interaction
- The wallet is designed to process external and internal messages, which is reflected in the dedicated test coverage.
- Gasless transactions imply an off-chain relayer can submit transactions on behalf of users, so the architecture must accommodate third-party message senders while preserving wallet authorization rules.
12. Tooling and project governance
- The repo is TypeScript-based for wrappers, scripts, and tests, with formatting/linting configured via ESLint and Prettier.
- CI is defined in .github/workflows/ci.yml, indicating automated validation likely runs compilation and tests before changes land.
- .env.example suggests deployment scripts may consume environment-specific network or key configuration.
13. How to draw the architecture
- Draw four major layers: protocol spec/docs, smart contract source, generated artifacts + wrappers/scripts, and tests/deployment tooling.
- The central runtime boundary is the TON blockchain: contracts execute on-chain, while TypeScript tooling compiles, deploys, and validates them off-chain.
- The most important internal dependency is that the wallet contract depends on shared standard-library/import code and is validated through wrapper-driven tests against compiled artifacts.
Main entry points: wallet_v5.fc Wallet V5 · wallet-v5-test.ts Test wrapper · WalletW5.spec.ts Wallet spec · wallet-v5.ts Wallet wrapper
Components 31
Spec & docs
Protocol definition and design history.
- Specification · Authoritative wallet behavior and message semantics.
Specification.md - Contest · Discussion of design decisions and tradeoffs.
Contest.md - Improvements · Gas and implementation optimization history.
Improvements.rst
On-chain source
Wallet contracts and shared contract code compiled for TON.
- Wallet V5 · Primary extensible wallet implementation on TON.
contracts/wallet_v5.fc - Library deployer · Companion contract for deploying or managing wallet library code.
contracts/library-deployer.fc - Stdlib · Shared standard library imported by the contracts.
contracts/imports/stdlib.fc
Compiled boundary
Generated bytecode artifacts consumed by tooling.
- Wallet bytecode · Generated compiled wallet contract output.
build/wallet_v5.compiled.json - Library bytecode · Generated compiled library-deployer output.
build/library-deployer.compiled.json
Wrappers & scripts
TypeScript tooling for compilation, deployment, and inspection.
- Wallet compile · Builds wallet artifacts from contract source.
wrappers/wallet_v5.compile.ts - Library compile · Builds library-deployer artifacts from contract source.
wrappers/library-deployer.compile.ts - Wallet wrapper · Ton-core wrapper for the deployed Wallet V5 contract.
wrappers/wallet-v5.ts - Library wrapper · Ton-core wrapper for the deployed library-deployer contract.
wrappers/library-deployer.ts - Errors · Error decoding and test-facing contract behavior helpers.
wrappers/Errors.ts - Test wrapper · Wrapper utilities used by wallet tests.
wrappers/wallet-v5-test.ts - Deploy wallet · Deploys the wallet using the standard path.
scripts/deployWalletV5.ts - Deploy no-lib · Deploys the wallet when the library is not predeployed.
scripts/deployWalletV5WithoutLibrary.ts - Deploy library · Deploys the companion library contract.
scripts/deployLibrary.ts - Print code · Displays or extracts compiled wallet code for verification.
scripts/printWalletCode.ts
Verification
Behavioral tests and reusable test utilities.
- Wallet spec · Main end-to-end wallet protocol suite.
tests/WalletW5.spec.ts - External msgs · External message handling coverage.
tests/wallet-v5-external.spec.ts - Internal msgs · Internal message handling coverage.
tests/wallet-v5-internal.spec.ts - Getters · Getter and read-only interface coverage.
tests/wallet-v5-get.spec.ts - Extensions · Extension mechanism and edge-case coverage.
tests/wallet-v5-extensions.spec.ts - Test utils · Reusable helpers for transactions, gas, and scenarios.
tests/utils.ts - Gas utils · Gas accounting helpers for protocol validation.
tests/gasUtils.ts - Actions · Reusable action and transaction setup helpers.
tests/actions.ts - Test-only actions · Scenario helpers only used in tests.
tests/test-only-actions.ts - Config · Test configuration and environment setup.
tests/config.ts
Project infra
Repository-wide automation and environment configuration.
- CI · Automated validation workflow.
.github/workflows/ci.yml - Env example · Example environment variables for deployment and tooling.
.env.example - Package · TypeScript project scripts and dependencies.
package.json
Connections 32
- Specification → Wallet V5: defines · Protocol requirements implemented by the wallet contract.
- Specification → Wallet spec: verified by · Behavioral tests enforce the protocol specification.
- Contest → Wallet V5: informs · Design choices reflected in the wallet implementation.
- Improvements → Wallet V5: optimizes · Gas reduction and implementation tuning target the contract code.
- Wallet V5 → Stdlib: imports · Wallet logic depends on shared contract library code.
- Library deployer → Stdlib: imports · Library deployer also uses shared standard code.
- Wallet V5 → Wallet bytecode: compiles to · Source contract is built into deployable artifact.
- Library deployer → Library bytecode: compiles to · Source contract is built into deployable artifact.
- Wallet compile → Wallet V5: builds · Compilation wrapper turns source into compiled wallet output.
- Wallet compile → Wallet bytecode: emits · Produces the wallet compiled JSON artifact.
- Library compile → Library deployer: builds · Compilation wrapper turns source into compiled library output.
- Library compile → Library bytecode: emits · Produces the library compiled JSON artifact.
- Wallet wrapper → Wallet bytecode: loads · Wrapper consumes compiled wallet bytecode and metadata.
- Library wrapper → Library bytecode: loads · Wrapper consumes compiled library bytecode and metadata.
- Test wrapper → Wallet wrapper: extends · Test adapter builds on the main wallet wrapper.
- Errors → Test wrapper: supports · Error decoding and test assertions for wallet behavior.
- Deploy wallet → Wallet wrapper: uses · Deployment script uses wallet wrapper and artifact handling.
- Deploy no-lib → Wallet wrapper: uses · Alternative deployment path also relies on wallet wrapper.
- Deploy library → Library wrapper: uses · Deployment script uses library wrapper and artifact handling.
- Print code → Wallet bytecode: inspects · Utility reads compiled wallet code for output or verification.
- Wallet spec → Test wrapper: drives · Main test suite exercises the wallet through wrappers.
- External msgs → Test wrapper: uses · External message tests run through the wallet test adapter.
- Internal msgs → Test wrapper: uses · Internal message tests run through the wallet test adapter.
- Getters → Wallet wrapper: uses · Getter tests rely on the deployed-contract wrapper surface.
- Extensions → Test wrapper: uses · Extension tests exercise wallet extension flows.
- Test utils → Actions: shares · General scenario helpers compose lower-level actions.
- Test utils → Gas utils: shares · Shared helpers for gas accounting and validation.
- Wallet spec → Test utils: depends on · Main suite uses reusable helpers and scenario utilities.
- Wallet spec → Config: depends on · Test suite uses configuration and environment setup.
- CI → Package: runs · CI validates the project through package-defined scripts.
- CI → Wallet spec: executes · Automated workflow runs the wallet verification suite.
- Env example → Deploy wallet: configures · Deployment scripts consume environment-specific settings.