Beancount.io LogoBeancount.io

DeFi Accounting Made Simple: Tracking Yield Farming, Liquidity Pools, and Staking Rewards with Plain-Text Accounting

8 min readMike ThriftMike Thrift
DeFi Accounting Made Simple: Tracking Yield Farming, Liquidity Pools, and Staking Rewards with Plain-Text Accounting

DeFi Accounting Made Simple: Tracking Yield Farming, Liquidity Pools, and Staking Rewards with Plain-Text Accounting

Decentralized Finance (DeFi) has revolutionized how we interact with financial services, offering unprecedented opportunities for yield generation, liquidity provision, and decentralized trading. However, with these opportunities comes the challenge of accurately tracking complex transactions for tax compliance and portfolio management.

Traditional accounting methods struggle with DeFi's unique characteristics: automated market makers, liquidity mining, impermanent loss, and multi-token rewards. This comprehensive guide shows you how to master DeFi accounting using Beancount.io's powerful plain-text accounting system.

Understanding DeFi Accounting Challenges

The Complexity of DeFi Transactions

DeFi protocols create accounting challenges that don't exist in traditional finance:

  • Multi-token transactions: Single operations involving multiple cryptocurrencies
  • Automated compounding: Rewards automatically reinvested
  • Impermanent loss: Value changes due to price divergence in liquidity pools
  • Gas fee optimization: Complex fee structures across different networks
  • Protocol governance: Voting rights and governance token distributions
  • Cross-protocol interactions: Transactions spanning multiple DeFi platforms

Tax Implications of DeFi Activities

The IRS treats DeFi activities as taxable events:

  • Liquidity provision: May trigger taxable events when depositing assets
  • Yield farming rewards: Taxable as ordinary income at fair market value
  • Impermanent loss: Potential tax implications when withdrawing from pools
  • Governance tokens: Airdrops and rewards taxable as income
  • Staking rewards: Taxable as income when received

Setting Up DeFi Accounts in Beancount.io

Comprehensive Account Structure

Create a detailed account hierarchy that captures all DeFi activities:

; Wallet Accounts
1970-01-01 open Assets:Crypto:Wallet:MetaMask:ETH
1970-01-01 open Assets:Crypto:Wallet:MetaMask:USDC
1970-01-01 open Assets:Crypto:Wallet:MetaMask:DAI
1970-01-01 open Assets:Crypto:Wallet:MetaMask:WBTC
 
; Uniswap V3 Liquidity Pools
1970-01-01 open Assets:DeFi:Uniswap:ETH-USDC-LP
1970-01-01 open Assets:DeFi:Uniswap:DAI-USDC-LP
1970-01-01 open Assets:DeFi:Uniswap:WBTC-ETH-LP
 
; Compound Protocol
1970-01-01 open Assets:DeFi:Compound:cUSDC
1970-01-01 open Assets:DeFi:Compound:cETH
1970-01-01 open Assets:DeFi:Compound:cDAI
 
; Aave Protocol
1970-01-01 open Assets:DeFi:Aave:aUSDC
1970-01-01 open Assets:DeFi:Aave:aETH
1970-01-01 open Assets:DeFi:Aave:DebtETH
 
; Staking Protocols
1970-01-01 open Assets:Staking:Ethereum:ETH
1970-01-01 open Assets:Staking:Lido:stETH
1970-01-01 open Assets:Staking:RocketPool:rETH
 
; Income Accounts
1970-01-01 open Income:DeFi:Yield:Uniswap
1970-01-01 open Income:DeFi:Yield:Compound
1970-01-01 open Income:DeFi:Yield:Aave
1970-01-01 open Income:DeFi:Staking:Ethereum
1970-01-01 open Income:DeFi:Governance:Tokens
1970-01-01 open Income:DeFi:Airdrops
 
; Expense Accounts
1970-01-01 open Expenses:DeFi:Gas:Ethereum
1970-01-01 open Expenses:DeFi:Gas:Polygon
1970-01-01 open Expenses:DeFi:Gas:Arbitrum
1970-01-01 open Expenses:DeFi:ImpermanentLoss

DeFi-Specific Commodities

Define tokens and LP tokens with proper metadata:

1970-01-01 commodity UNI-V3-ETH-USDC
  name: "Uniswap V3 ETH-USDC LP Token"
  asset-class: "liquidity-pool"
  protocol: "uniswap-v3"
 
1970-01-01 commodity cUSDC
  name: "Compound USDC"
  asset-class: "lending-token"
  protocol: "compound"
 
1970-01-01 commodity stETH
  name: "Lido Staked Ethereum"
  asset-class: "staking-derivative"
  protocol: "lido"

Tracking Common DeFi Activities

1. Uniswap Liquidity Provision

Adding Liquidity to a Pool

2024-01-15 * "Add liquidity to ETH-USDC Uniswap V3 pool"
  Assets:Crypto:Wallet:MetaMask:ETH     -5.0 ETH {2500.00 USD}
  Assets:Crypto:Wallet:MetaMask:USDC  -12500 USDC
  Assets:DeFi:Uniswap:ETH-USDC-LP        100 UNI-V3-ETH-USDC {250.00 USD}
  Expenses:DeFi:Gas:Ethereum            0.01 ETH {2500.00 USD}
  Assets:Crypto:Wallet:MetaMask:ETH    -0.01 ETH {2500.00 USD}

Claiming LP Fees

2024-02-15 * "Claim Uniswap LP fees"
  Assets:Crypto:Wallet:MetaMask:ETH     0.2 ETH {2600.00 USD}
  Assets:Crypto:Wallet:MetaMask:USDC    300 USDC
  Income:DeFi:Yield:Uniswap            820.00 USD
  Expenses:DeFi:Gas:Ethereum           0.005 ETH {2600.00 USD}
  Assets:Crypto:Wallet:MetaMask:ETH   -0.005 ETH {2600.00 USD}

Removing Liquidity with Impermanent Loss

2024-03-15 * "Remove liquidity from ETH-USDC pool"
  Assets:DeFi:Uniswap:ETH-USDC-LP       -100 UNI-V3-ETH-USDC {250.00 USD}
  Assets:Crypto:Wallet:MetaMask:ETH      4.8 ETH {2800.00 USD}
  Assets:Crypto:Wallet:MetaMask:USDC   12800 USDC
  Expenses:DeFi:ImpermanentLoss         240.00 USD  ; IL calculation
  Expenses:DeFi:Gas:Ethereum           0.008 ETH {2800.00 USD}
  Assets:Crypto:Wallet:MetaMask:ETH   -0.008 ETH {2800.00 USD}

2. Compound Protocol Lending

Supplying Assets to Compound

2024-01-20 * "Supply USDC to Compound"
  Assets:Crypto:Wallet:MetaMask:USDC  -10000 USDC
  Assets:DeFi:Compound:cUSDC            500 cUSDC {20.00 USD}
  Expenses:DeFi:Gas:Ethereum           0.008 ETH {2500.00 USD}
  Assets:Crypto:Wallet:MetaMask:ETH   -0.008 ETH {2500.00 USD}

Earning Compound Interest

2024-02-20 * "Compound interest accrual"
  Assets:DeFi:Compound:cUSDC             5.2 cUSDC {20.50 USD}
  Income:DeFi:Yield:Compound           106.60 USD

Claiming COMP Rewards

2024-02-20 * "Claim COMP governance tokens"
  Assets:Crypto:Wallet:MetaMask:COMP     12 COMP {85.00 USD}
  Income:DeFi:Governance:Tokens        1020.00 USD
  Expenses:DeFi:Gas:Ethereum           0.006 ETH {2600.00 USD}
  Assets:Crypto:Wallet:MetaMask:ETH   -0.006 ETH {2600.00 USD}

3. Aave Protocol Operations

Depositing and Borrowing

; Deposit ETH as collateral
2024-01-25 * "Deposit ETH to Aave"
  Assets:Crypto:Wallet:MetaMask:ETH    -10 ETH {2500.00 USD}
  Assets:DeFi:Aave:aETH                 10 aETH {2500.00 USD}
  Expenses:DeFi:Gas:Ethereum          0.01 ETH {2500.00 USD}
  Assets:Crypto:Wallet:MetaMask:ETH  -0.01 ETH {2500.00 USD}
 
; Borrow USDC against ETH collateral
2024-01-25 * "Borrow USDC from Aave"
  Assets:Crypto:Wallet:MetaMask:USDC  15000 USDC
  Liabilities:DeFi:Aave:DebtUSDC     -15000 USDC
  Expenses:DeFi:Gas:Ethereum          0.008 ETH {2500.00 USD}
  Assets:Crypto:Wallet:MetaMask:ETH  -0.008 ETH {2500.00 USD}

Repaying Loans with Interest

2024-03-25 * "Repay USDC loan to Aave"
  Liabilities:DeFi:Aave:DebtUSDC      15000 USDC
  Expenses:DeFi:Interest:Aave           450 USDC  ; Interest accrued
  Assets:Crypto:Wallet:MetaMask:USDC -15450 USDC
  Expenses:DeFi:Gas:Ethereum          0.008 ETH {2700.00 USD}
  Assets:Crypto:Wallet:MetaMask:ETH  -0.008 ETH {2700.00 USD}

4. Ethereum Staking

Direct Ethereum Staking

2024-01-10 * "Stake ETH on Ethereum 2.0"
  Assets:Crypto:Wallet:MetaMask:ETH    -32 ETH {2500.00 USD}
  Assets:Staking:Ethereum:ETH           32 ETH {2500.00 USD}
  Expenses:DeFi:Gas:Ethereum          0.01 ETH {2500.00 USD}
  Assets:Crypto:Wallet:MetaMask:ETH  -0.01 ETH {2500.00 USD}

Staking Rewards

2024-02-10 * "ETH staking rewards"
  Assets:Staking:Ethereum:ETH          0.15 ETH {2600.00 USD}
  Income:DeFi:Staking:Ethereum        390.00 USD

Liquid Staking with Lido

2024-01-12 * "Stake ETH with Lido"
  Assets:Crypto:Wallet:MetaMask:ETH    -10 ETH {2500.00 USD}
  Assets:Staking:Lido:stETH             10 stETH {2500.00 USD}
  Expenses:DeFi:Gas:Ethereum          0.008 ETH {2500.00 USD}
  Assets:Crypto:Wallet:MetaMask:ETH  -0.008 ETH {2500.00 USD}

Advanced DeFi Scenarios

Yield Farming Strategies

Multi-Protocol Yield Farming

; Step 1: Deposit USDC to Compound
2024-01-30 * "Deposit USDC to Compound for yield farming"
  Assets:Crypto:Wallet:MetaMask:USDC  -20000 USDC
  Assets:DeFi:Compound:cUSDC            1000 cUSDC {20.00 USD}
 
; Step 2: Borrow DAI against cUSDC
2024-01-30 * "Borrow DAI from Compound"
  Assets:Crypto:Wallet:MetaMask:DAI    15000 DAI
  Liabilities:DeFi:Compound:DebtDAI   -15000 DAI
 
; Step 3: Provide DAI-USDC liquidity to Uniswap
2024-01-30 * "Add DAI-USDC liquidity for farming"
  Assets:Crypto:Wallet:MetaMask:DAI   -15000 DAI
  Assets:Crypto:Wallet:MetaMask:USDC   -5000 USDC  ; Additional USDC
  Assets:DeFi:Uniswap:DAI-USDC-LP        200 UNI-V3-DAI-USDC {100.00 USD}

Cross-Chain DeFi Operations

Bridging Assets

2024-02-05 * "Bridge ETH to Polygon"
  Assets:Crypto:Wallet:MetaMask:ETH    -5 ETH {2600.00 USD}
  Assets:Crypto:Wallet:Polygon:ETH      5 ETH {2600.00 USD}
  Expenses:DeFi:Bridge:Fees           0.01 ETH {2600.00 USD}
  Assets:Crypto:Wallet:MetaMask:ETH  -0.01 ETH {2600.00 USD}

Flash Loans and Arbitrage

2024-02-10 * "Flash loan arbitrage opportunity"
  ; Flash loan
  Assets:Crypto:Temp:FlashLoan       100000 USDC
  Liabilities:DeFi:Aave:FlashLoan   -100000 USDC
 
  ; Arbitrage trade
  Assets:Crypto:Temp:FlashLoan      -100000 USDC
  Assets:Crypto:Temp:Arbitrage       101500 USDC
 
  ; Repay flash loan
  Liabilities:DeFi:Aave:FlashLoan    100000 USDC
  Assets:Crypto:Temp:Arbitrage      -100090 USDC  ; Including fees
 
  ; Profit
  Assets:Crypto:Wallet:MetaMask:USDC   1410 USDC
  Income:DeFi:Arbitrage:Profit        1410.00 USD

DeFi Tax Reporting Considerations

Income Recognition

All DeFi rewards are taxable as ordinary income:

; Track all income sources separately
Income:DeFi:Yield:Uniswap           ; LP fees
Income:DeFi:Yield:Compound          ; Interest earned
Income:DeFi:Staking:Ethereum        ; Staking rewards
Income:DeFi:Governance:Tokens       ; Governance token airdrops
Income:DeFi:Airdrops               ; Protocol airdrops

Expense Deductions

Track deductible expenses:

Expenses:DeFi:Gas:Ethereum          ; Gas fees
Expenses:DeFi:Gas:Polygon           ; Layer 2 fees
Expenses:DeFi:Interest:Aave         ; Borrowing costs
Expenses:DeFi:ImpermanentLoss       ; IL from LP positions

Capital Gains Tracking

Use lot-based accounting for accurate capital gains:

; Different cost basis for same token
Assets:DeFi:Compound:cUSDC   100 cUSDC {20.00 USD}  ; Batch 1
Assets:DeFi:Compound:cUSDC   100 cUSDC {21.00 USD}  ; Batch 2

Best Practices for DeFi Accounting

1. Real-Time Transaction Recording

  • Record transactions immediately after execution
  • Use transaction hashes for verification
  • Monitor mempool for pending transactions

2. Gas Fee Optimization Tracking

; Track gas optimization strategies
Expenses:DeFi:Gas:Ethereum:Standard    ; Standard gas price
Expenses:DeFi:Gas:Ethereum:Fast        ; Fast gas price
Expenses:DeFi:Gas:Ethereum:Instant     ; Instant gas price

3. Protocol Risk Management

; Track protocol-specific risks
Expenses:DeFi:Risk:SmartContract      ; Smart contract failures
Expenses:DeFi:Risk:Liquidation        ; Liquidation events
Expenses:DeFi:Risk:Slippage          ; Slippage costs

4. Automated Reconciliation

  • Set up automated price feeds
  • Use protocol APIs for balance verification
  • Implement automated transaction imports

5. Documentation Standards

  • Maintain detailed transaction notes
  • Document strategy rationale
  • Keep protocol interaction records

Integration with DeFi Tools

Portfolio Tracking

  • DeBank: DeFi portfolio overview
  • Zapper: Multi-protocol dashboard
  • Zerion: DeFi wallet and tracker

Tax Reporting

  • Koinly: DeFi tax calculations
  • CoinTracker: Multi-protocol support
  • TokenTax: Specialized DeFi reporting

Analytics Platforms

  • DeFi Pulse: Protocol analytics
  • DeFiLlama: TVL and yield tracking
  • APY.vision: Impermanent loss tracking

Conclusion

DeFi accounting complexity shouldn't prevent you from participating in the decentralized finance revolution. With Beancount.io's powerful plain-text accounting system, you can:

  • Track Complex Transactions: Handle multi-protocol interactions seamlessly
  • Ensure Tax Compliance: Proper income recognition and expense tracking
  • Monitor Portfolio Performance: Real-time insights into DeFi positions
  • Manage Risk: Track impermanent loss and protocol risks
  • Scale Operations: From simple staking to complex yield farming strategies

The key to successful DeFi accounting is consistency, accuracy, and proper categorization. Start with basic protocols and gradually expand to more complex strategies as you become comfortable with the accounting patterns.

Ready to master DeFi accounting? Start your Beancount.io journey and take control of your decentralized finance portfolio today.

Explore a live cryptocurrency example ledger: