Wysdom

Solidity Examples

Examples of using the interpreter for solidity

1. Interactive Code Execution

Write and run Solidity code instantly, just like Python:

// Variables print their values automatically
uint256 balance = 1000
balance  // Outputs: 1000
 
// Simple function execution
function double(uint256 x) returns (uint256) {
    return x * 2
}
double(21)  // Outputs: 42

2. Live Contract Testing

Test contract behavior without deployment costs:

contract SimpleBank {
    mapping(address => uint256) private balances
 
    function deposit() payable {
        balances[msg.sender] += msg.value
    }
 
    function getBalance() returns (uint256) {
        return balances[msg.sender]
    }
}
// Create and interact with the contract instantly
bank = SimpleBank()
bank.deposit{value: 100}()
bank.getBalance()  // Outputs: 100

3. Multi-Contract Interactions

Easily test how contracts work together:

// Define a token contract
contract Token {
    mapping(address => uint256) public balances
    
    function mint(address to, uint256 amount) {
        balances[to] += amount
    }
}
// Define a vault contract that uses the token
contract Vault {
    Token public token
    
    constructor(address _token) {
        token = Token(_token)
    }
    
    function checkBalance(address user) returns (uint256) {
        return token.balances(user)
    }
}
// Test the interaction
token = Token()
vault = Vault(address(token))
 
token.mint(msg.sender, 500)
vault.checkBalance(msg.sender)  // Outputs: 500

Common Use Cases

1. Learning Solidity

Perfect for beginners to understand Solidity concepts:

// Experiment with data types
uint8 small = 255
small + 1  // See overflow behavior in action
// Understand mappings
mapping(address => uint256) scores
scores[msg.sender] = 100
scores[msg.sender]  // Outputs: 100

2. Rapid Prototyping

Quickly test your contract ideas:

contract NFTPrototype {
    mapping(uint256 => address) owners
    uint256 nextId = 0
 
    function mint() {
        owners[nextId] = msg.sender
        nextId += 1
    }
}
nft = NFTPrototype()
nft.mint()
nft.owners(0)  // Outputs: your address

3. Debugging and Testing

Find and fix issues quickly:

contract BugTesting {
    uint256 value
 
    function setValue(uint256 newValue) {
        value = newValue
    }
 
    function increment() {
        value += 1
    }
}
// Test the contract
bug = BugTesting()
bug.setValue(5)
bug.value  // Check initial state
bug.increment()
bug.value  // Verify the change

Best Practices

  1. Start Small: Test individual components before combining them
  2. Use Print Statements: Variables print automatically when referenced
  3. Experiment Freely: No gas costs means unlimited testing
  4. Save Your Work: Notebooks save your experiments for future reference

Tips for Success

  • Use descriptive variable names for better readability
  • Test edge cases without worrying about gas costs
  • Break down complex contracts into smaller testable pieces
  • Use comments to document your experiments

On this page