Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Translation

Ursus supports bidirectional translation between smart contract languages, enabling migration of existing contracts and code reuse.

Overview

Translation allows you to:

  • Import existing Solidity/C++ contracts into Ursus
  • Verify imported contracts formally
  • Extract verified contracts back to target languages

Supported Translations

Solidity ↔ Ursus

Convert between Solidity and Ursus:

Solidity → Ursus: Import Solidity contracts

  • Parse Solidity source code
  • Generate Ursus definitions
  • Preserve semantics and structure

Ursus → Solidity: Extract to Solidity

  • Generate Solidity from verified Ursus
  • Maintain type safety
  • Optimize for gas efficiency

C++ → Ursus

Convert TON C++ contracts to Ursus: C++ Translation

  • Import TON C++ contracts
  • Map to Ursus semantics
  • Enable formal verification

Translation Workflow

┌─────────────────┐
│ Existing Code   │
│ (Solidity/C++)  │
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│   Translate     │
│   to Ursus      │
└────────┬────────┘
         │
         ▼
┌─────────────────┐
│ Ursus Contract  │
│ (Verifiable)    │
└────────┬────────┘
         │
         ├──────────┬──────────┐
         ▼          ▼          ▼
    Verify     Modify     Extract
    (Proofs)   (Enhance)  (Deploy)

Use Cases

1. Verify Existing Contracts

Import and verify deployed contracts:

# Import Solidity contract
ursus-translate --from solidity --to ursus ERC20.sol

# Verify properties
coqc ERC20.v
# Prove correctness

2. Migrate to Ursus

Gradually migrate codebase:

# Translate contracts one by one
ursus-translate Token.sol
ursus-translate TokenSale.sol
ursus-translate Vesting.sol

3. Cross-Platform Development

Develop once, deploy everywhere:

(* Write in Ursus *)
Contract MyContract ;

(* Extract to multiple targets *)
#[language = solidity]  (* For Ethereum *)
#[language = cpp]       (* For TON *)
#[language = rust]      (* For Solana *)

Translation Quality

Semantic Preservation

Ursus translation preserves:

  • Type safety - All types are preserved
  • Control flow - Logic remains identical
  • State transitions - Behavior is equivalent
  • Function signatures - Interfaces match

Limitations

Some features may require manual adjustment:

  • ⚠️ Inline assembly - Not directly translatable
  • ⚠️ Low-level calls - May need abstraction
  • ⚠️ Platform-specific features - Require adaptation

Translation Tools

Command-Line Tool

# Solidity to Ursus
ursus-translate --from solidity --to ursus Contract.sol

# C++ to Ursus
ursus-translate --from cpp --to ursus Contract.cpp

# Ursus to Solidity
ursus-translate --from ursus --to solidity Contract.v

Programmatic API

Require Import UrsusTranslation.

(* Import Solidity *)
Import Solidity "ERC20.sol" as ERC20.

(* Use imported contract *)
Contract MyToken ;
Inherits ERC20 ;

Best Practices

Before Translation

  1. Clean code - Remove unused code
  2. Document - Add comments for complex logic
  3. Test - Ensure original code works
  4. Backup - Keep original source

During Translation

  1. Review output - Check generated Ursus code
  2. Fix warnings - Address translation warnings
  3. Test equivalence - Verify behavior matches
  4. Add specifications - Write formal properties

After Translation

  1. Verify - Prove correctness properties
  2. Optimize - Improve generated code
  3. Document - Explain verification
  4. Test - Run comprehensive tests

Examples

Simple ERC20 Translation

Original Solidity:

contract ERC20 {
    mapping(address => uint256) public balances;

    function transfer(address to, uint256 amount) public returns (bool) {
        balances[msg.sender] -= amount;
        balances[to] += amount;
        return true;
    }
}

Generated Ursus:

Contract ERC20 ;
Sends To ;
Types ;
Constants ;

Record Contract := {
    balances: mapping address uint256
}.

Ursus Definition transfer (to: address) (amount: uint256):
  UExpression boolean false.
{
    ::// balances[[msg->sender]] := balances[[msg->sender]] - amount .
    ::// balances[[to]] := balances[[to]] + amount |.
}
return @true.
Defined.

Next Steps

See Also

  • Compilation - Compiling Ursus contracts
  • Ursus Language - Language reference
  • ursus-patterns repository - Translation examples