# tonkeeper/w5 architecture

> Architecture diagram of the GitHub repository tonkeeper/w5, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-19). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/tonkeeper/w5
- Repository: https://github.com/tonkeeper/w5

## Overview

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: [`contracts/wallet_v5.fc`](https://github.com/tonkeeper/w5/blob/main/contracts/wallet_v5.fc) (Wallet V5), [`wrappers/wallet-v5-test.ts`](https://github.com/tonkeeper/w5/blob/main/wrappers/wallet-v5-test.ts) (Test wrapper), [`tests/WalletW5.spec.ts`](https://github.com/tonkeeper/w5/blob/main/tests/WalletW5.spec.ts) (Wallet spec), [`wrappers/wallet-v5.ts`](https://github.com/tonkeeper/w5/blob/main/wrappers/wallet-v5.ts) (Wallet wrapper)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_docs["Spec & docs"]
  node_spec["Specification<br/>protocol spec<br/>[Specification.md]"]
  node_contest["Contest<br/>design notes<br/>[Contest.md]"]
  node_improvements["Improvements<br/>optimization log<br/>[Improvements.rst]"]
end

subgraph group_contracts["On-chain source"]
  node_wallet_contract["Wallet V5<br/>smart contract<br/>[wallet_v5.fc]"]
  node_library_contract["Library deployer<br/>smart contract"]
  node_stdlib["Stdlib<br/>shared contract lib<br/>[stdlib.fc]"]
end

subgraph group_artifacts["Compiled boundary"]
  node_wallet_artifact["Wallet bytecode<br/>compiled artifact"]
  node_library_artifact["Library bytecode<br/>compiled artifact"]
end

subgraph group_tooling["Wrappers & scripts"]
  node_wallet_compile["Wallet compile<br/>compiler wrapper"]
  node_library_compile["Library compile<br/>compiler wrapper"]
  node_wallet_wrapper["Wallet wrapper<br/>contract wrapper<br/>[wallet-v5.ts]"]
  node_library_wrapper["Library wrapper<br/>contract wrapper"]
  node_errors["Errors<br/>decoder<br/>[Errors.ts]"]
  node_wallet_test_wrapper["Test wrapper<br/>test adapter<br/>[wallet-v5-test.ts]"]
  node_deploy_wallet["Deploy wallet<br/>deployment script<br/>[deployWalletV5.ts]"]
  node_deploy_wallet_nolib["Deploy no-lib<br/>deployment script"]
  node_deploy_library["Deploy library<br/>deployment script<br/>[deployLibrary.ts]"]
  node_print_code["Print code<br/>inspection script<br/>[printWalletCode.ts]"]
end

subgraph group_tests["Verification"]
  node_wallet_spec["Wallet spec<br/>behavioral tests<br/>[WalletW5.spec.ts]"]
  node_external_tests["External msgs<br/>behavioral tests"]
  node_internal_tests["Internal msgs<br/>behavioral tests"]
  node_getter_tests["Getters<br/>behavioral tests"]
  node_extension_tests["Extensions<br/>behavioral tests"]
  node_test_utils["Test utils<br/>test support<br/>[utils.ts]"]
  node_gas_utils["Gas utils<br/>test support<br/>[gasUtils.ts]"]
  node_actions["Actions<br/>test support<br/>[actions.ts]"]
  node_test_only_actions["Test-only actions<br/>test support"]
  node_config["Config<br/>[config.ts]"]
end

subgraph group_infra["Project infra"]
  node_ci["CI<br/>automation<br/>[ci.yml]"]
  node_env_example["Env example<br/>env template<br/>[.env.example]"]
  node_package["Package<br/>project config<br/>[package.json]"]
end

node_spec -->|"defines"| node_wallet_contract
node_spec -->|"verified by"| node_wallet_spec
node_contest -.->|"informs"| node_wallet_contract
node_improvements -.->|"optimizes"| node_wallet_contract
node_wallet_contract -->|"imports"| node_stdlib
node_library_contract -->|"imports"| node_stdlib
node_wallet_contract -->|"compiles to"| node_wallet_artifact
node_library_contract -->|"compiles to"| node_library_artifact
node_wallet_compile -->|"builds"| node_wallet_contract
node_wallet_compile -->|"emits"| node_wallet_artifact
node_library_compile -->|"builds"| node_library_contract
node_library_compile -->|"emits"| node_library_artifact
node_wallet_wrapper -->|"loads"| node_wallet_artifact
node_library_wrapper -->|"loads"| node_library_artifact
node_wallet_test_wrapper -->|"extends"| node_wallet_wrapper
node_errors -->|"supports"| node_wallet_test_wrapper
node_deploy_wallet -->|"uses"| node_wallet_wrapper
node_deploy_wallet_nolib -->|"uses"| node_wallet_wrapper
node_deploy_library -->|"uses"| node_library_wrapper
node_print_code -->|"inspects"| node_wallet_artifact
node_wallet_spec -->|"drives"| node_wallet_test_wrapper
node_external_tests -->|"uses"| node_wallet_test_wrapper
node_internal_tests -->|"uses"| node_wallet_test_wrapper
node_getter_tests -->|"uses"| node_wallet_wrapper
node_extension_tests -->|"uses"| node_wallet_test_wrapper
node_test_utils -->|"shares"| node_actions
node_test_utils -->|"shares"| node_gas_utils
node_wallet_spec -->|"depends on"| node_test_utils
node_wallet_spec -->|"depends on"| node_config
node_ci -->|"runs"| node_package
node_ci -->|"executes"| node_wallet_spec
node_env_example -.->|"configures"| node_deploy_wallet

click node_spec "https://github.com/tonkeeper/w5/blob/main/Specification.md"
click node_contest "https://github.com/tonkeeper/w5/blob/main/Contest.md"
click node_improvements "https://github.com/tonkeeper/w5/blob/main/Improvements.rst"
click node_wallet_contract "https://github.com/tonkeeper/w5/blob/main/contracts/wallet_v5.fc"
click node_library_contract "https://github.com/tonkeeper/w5/blob/main/contracts/library-deployer.fc"
click node_stdlib "https://github.com/tonkeeper/w5/blob/main/contracts/imports/stdlib.fc"
click node_wallet_artifact "https://github.com/tonkeeper/w5/blob/main/build/wallet_v5.compiled.json"
click node_library_artifact "https://github.com/tonkeeper/w5/blob/main/build/library-deployer.compiled.json"
click node_wallet_compile "https://github.com/tonkeeper/w5/blob/main/wrappers/wallet_v5.compile.ts"
click node_library_compile "https://github.com/tonkeeper/w5/blob/main/wrappers/library-deployer.compile.ts"
click node_wallet_wrapper "https://github.com/tonkeeper/w5/blob/main/wrappers/wallet-v5.ts"
click node_library_wrapper "https://github.com/tonkeeper/w5/blob/main/wrappers/library-deployer.ts"
click node_errors "https://github.com/tonkeeper/w5/blob/main/wrappers/Errors.ts"
click node_wallet_test_wrapper "https://github.com/tonkeeper/w5/blob/main/wrappers/wallet-v5-test.ts"
click node_deploy_wallet "https://github.com/tonkeeper/w5/blob/main/scripts/deployWalletV5.ts"
click node_deploy_wallet_nolib "https://github.com/tonkeeper/w5/blob/main/scripts/deployWalletV5WithoutLibrary.ts"
click node_deploy_library "https://github.com/tonkeeper/w5/blob/main/scripts/deployLibrary.ts"
click node_print_code "https://github.com/tonkeeper/w5/blob/main/scripts/printWalletCode.ts"
click node_wallet_spec "https://github.com/tonkeeper/w5/blob/main/tests/WalletW5.spec.ts"
click node_external_tests "https://github.com/tonkeeper/w5/blob/main/tests/wallet-v5-external.spec.ts"
click node_internal_tests "https://github.com/tonkeeper/w5/blob/main/tests/wallet-v5-internal.spec.ts"
click node_getter_tests "https://github.com/tonkeeper/w5/blob/main/tests/wallet-v5-get.spec.ts"
click node_extension_tests "https://github.com/tonkeeper/w5/blob/main/tests/wallet-v5-extensions.spec.ts"
click node_test_utils "https://github.com/tonkeeper/w5/blob/main/tests/utils.ts"
click node_gas_utils "https://github.com/tonkeeper/w5/blob/main/tests/gasUtils.ts"
click node_actions "https://github.com/tonkeeper/w5/blob/main/tests/actions.ts"
click node_test_only_actions "https://github.com/tonkeeper/w5/blob/main/tests/test-only-actions.ts"
click node_config "https://github.com/tonkeeper/w5/blob/main/tests/config.ts"
click node_ci "https://github.com/tonkeeper/w5/blob/main/.github/workflows/ci.yml"
click node_env_example "https://github.com/tonkeeper/w5/blob/main/.env.example"
click node_package "https://github.com/tonkeeper/w5/blob/main/package.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_spec,node_contest,node_improvements toneBlue
class node_wallet_contract,node_library_contract,node_stdlib toneAmber
class node_wallet_artifact,node_library_artifact toneMint
class node_wallet_compile,node_library_compile,node_wallet_wrapper,node_library_wrapper,node_errors,node_wallet_test_wrapper,node_deploy_wallet,node_deploy_wallet_nolib,node_deploy_library,node_print_code toneRose
class node_wallet_spec,node_external_tests,node_internal_tests,node_getter_tests,node_extension_tests,node_test_utils,node_gas_utils,node_actions,node_test_only_actions,node_config toneIndigo
class node_ci,node_env_example,node_package toneTeal
```

## Components

### Spec & docs

Protocol definition and design history.

- [Specification](https://github.com/tonkeeper/w5/blob/main/Specification.md): protocol spec · Authoritative wallet behavior and message semantics. · `Specification.md`
- [Contest](https://github.com/tonkeeper/w5/blob/main/Contest.md): design notes · Discussion of design decisions and tradeoffs. · `Contest.md`
- [Improvements](https://github.com/tonkeeper/w5/blob/main/Improvements.rst): optimization log · Gas and implementation optimization history. · `Improvements.rst`

### On-chain source

Wallet contracts and shared contract code compiled for TON.

- [Wallet V5](https://github.com/tonkeeper/w5/blob/main/contracts/wallet_v5.fc): smart contract · Primary extensible wallet implementation on TON. · `contracts/wallet_v5.fc`
- [Library deployer](https://github.com/tonkeeper/w5/blob/main/contracts/library-deployer.fc): smart contract · Companion contract for deploying or managing wallet library code. · `contracts/library-deployer.fc`
- [Stdlib](https://github.com/tonkeeper/w5/blob/main/contracts/imports/stdlib.fc): shared contract lib · Shared standard library imported by the contracts. · `contracts/imports/stdlib.fc`

### Compiled boundary

Generated bytecode artifacts consumed by tooling.

- [Wallet bytecode](https://github.com/tonkeeper/w5/blob/main/build/wallet_v5.compiled.json): compiled artifact · Generated compiled wallet contract output. · `build/wallet_v5.compiled.json`
- [Library bytecode](https://github.com/tonkeeper/w5/blob/main/build/library-deployer.compiled.json): compiled artifact · Generated compiled library-deployer output. · `build/library-deployer.compiled.json`

### Wrappers & scripts

TypeScript tooling for compilation, deployment, and inspection.

- [Wallet compile](https://github.com/tonkeeper/w5/blob/main/wrappers/wallet_v5.compile.ts): compiler wrapper · Builds wallet artifacts from contract source. · `wrappers/wallet_v5.compile.ts`
- [Library compile](https://github.com/tonkeeper/w5/blob/main/wrappers/library-deployer.compile.ts): compiler wrapper · Builds library-deployer artifacts from contract source. · `wrappers/library-deployer.compile.ts`
- [Wallet wrapper](https://github.com/tonkeeper/w5/blob/main/wrappers/wallet-v5.ts): contract wrapper · Ton-core wrapper for the deployed Wallet V5 contract. · `wrappers/wallet-v5.ts`
- [Library wrapper](https://github.com/tonkeeper/w5/blob/main/wrappers/library-deployer.ts): contract wrapper · Ton-core wrapper for the deployed library-deployer contract. · `wrappers/library-deployer.ts`
- [Errors](https://github.com/tonkeeper/w5/blob/main/wrappers/Errors.ts): decoder · Error decoding and test-facing contract behavior helpers. · `wrappers/Errors.ts`
- [Test wrapper](https://github.com/tonkeeper/w5/blob/main/wrappers/wallet-v5-test.ts): test adapter · Wrapper utilities used by wallet tests. · `wrappers/wallet-v5-test.ts`
- [Deploy wallet](https://github.com/tonkeeper/w5/blob/main/scripts/deployWalletV5.ts): deployment script · Deploys the wallet using the standard path. · `scripts/deployWalletV5.ts`
- [Deploy no-lib](https://github.com/tonkeeper/w5/blob/main/scripts/deployWalletV5WithoutLibrary.ts): deployment script · Deploys the wallet when the library is not predeployed. · `scripts/deployWalletV5WithoutLibrary.ts`
- [Deploy library](https://github.com/tonkeeper/w5/blob/main/scripts/deployLibrary.ts): deployment script · Deploys the companion library contract. · `scripts/deployLibrary.ts`
- [Print code](https://github.com/tonkeeper/w5/blob/main/scripts/printWalletCode.ts): inspection script · Displays or extracts compiled wallet code for verification. · `scripts/printWalletCode.ts`

### Verification

Behavioral tests and reusable test utilities.

- [Wallet spec](https://github.com/tonkeeper/w5/blob/main/tests/WalletW5.spec.ts): behavioral tests · Main end-to-end wallet protocol suite. · `tests/WalletW5.spec.ts`
- [External msgs](https://github.com/tonkeeper/w5/blob/main/tests/wallet-v5-external.spec.ts): behavioral tests · External message handling coverage. · `tests/wallet-v5-external.spec.ts`
- [Internal msgs](https://github.com/tonkeeper/w5/blob/main/tests/wallet-v5-internal.spec.ts): behavioral tests · Internal message handling coverage. · `tests/wallet-v5-internal.spec.ts`
- [Getters](https://github.com/tonkeeper/w5/blob/main/tests/wallet-v5-get.spec.ts): behavioral tests · Getter and read-only interface coverage. · `tests/wallet-v5-get.spec.ts`
- [Extensions](https://github.com/tonkeeper/w5/blob/main/tests/wallet-v5-extensions.spec.ts): behavioral tests · Extension mechanism and edge-case coverage. · `tests/wallet-v5-extensions.spec.ts`
- [Test utils](https://github.com/tonkeeper/w5/blob/main/tests/utils.ts): test support · Reusable helpers for transactions, gas, and scenarios. · `tests/utils.ts`
- [Gas utils](https://github.com/tonkeeper/w5/blob/main/tests/gasUtils.ts): test support · Gas accounting helpers for protocol validation. · `tests/gasUtils.ts`
- [Actions](https://github.com/tonkeeper/w5/blob/main/tests/actions.ts): test support · Reusable action and transaction setup helpers. · `tests/actions.ts`
- [Test-only actions](https://github.com/tonkeeper/w5/blob/main/tests/test-only-actions.ts): test support · Scenario helpers only used in tests. · `tests/test-only-actions.ts`
- [Config](https://github.com/tonkeeper/w5/blob/main/tests/config.ts): test config · Test configuration and environment setup. · `tests/config.ts`

### Project infra

Repository-wide automation and environment configuration.

- [CI](https://github.com/tonkeeper/w5/blob/main/.github/workflows/ci.yml): automation · Automated validation workflow. · `.github/workflows/ci.yml`
- [Env example](https://github.com/tonkeeper/w5/blob/main/.env.example): env template · Example environment variables for deployment and tooling. · `.env.example`
- [Package](https://github.com/tonkeeper/w5/blob/main/package.json): project config · TypeScript project scripts and dependencies. · `package.json`

## Connections

- 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.
