Skip to content

gear-foundation/perp-sim

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

65 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Sim-Engine β€” Trading Simulation on Vara Network

On-chain trading simulation engine for VaraPerps perpetual DEX. Runs bots with various strategies that trade through a real smart contract deployed on Vara Network in real time.

Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Sim-Engine (Rust)                          β”‚
β”‚                                             β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚ Oracle   β”‚  β”‚ Exchange β”‚  β”‚ Keepers  β”‚  β”‚
β”‚  β”‚ (Pyth)   │──│ Agent    │──│          β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚                     β”‚                       β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”‚
β”‚  β”‚ Market   β”‚  β”‚VaraClientβ”‚  β”‚Liquidatorβ”‚  β”‚
β”‚  β”‚ Maker    │──│(gclient) │──│          β”‚  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β”‚
β”‚                     β”‚                       β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”‚
β”‚  β”‚ Smart Traders (Arb, Hodler, Fund..) β”‚    β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β”‚
β”‚                                             β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                 β”‚
β”‚  β”‚ HTTP API β”‚  β”‚ WebSocketβ”‚  ← Human trader β”‚
β”‚  β”‚ :8080    β”‚  β”‚ :8081    β”‚                  β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                β”‚ wss://
                β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  Vara Network (Testnet)               β”‚
β”‚  VaraPerps Smart Contract             β”‚
β”‚  Block time: ~3s                      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

All orders are sent on-chain. Every transaction goes through the real blockchain: SubmitOrder β†’ ExecuteOrder β†’ confirmation in the next block.

Requirements

  • Rust toolchain (stable)
  • Deployed VaraPerps contract on Vara testnet
  • Keystore with bot keys (gring format)
  • Passphrase file for the keystore

Setup

Environment Variables

# Required
export VARA_CONTRACT_ADDRESS="0x..."                   # Deployed VaraPerps contract address
export VARA_KEYSTORE_PATH="/path/to/keystore"          # Path to gring keystore
export VARA_PASSPHRASE_PATH="/path/to/.passphrase"     # Keystore passphrase file

# Optional
export VARA_WS_ENDPOINT="wss://testnet.vara.network"   # RPC endpoint (default: testnet)
export VARA_GAS_LIMIT="200000000000"                   # Base gas limit (default: 100B)
export VARA_BLOCK_TIME_MS="3000"                       # Block time in ms (default: 3000)
export VARA_HUMAN_ADDRESS="kG..."                      # SS58 address for HumanAgent

Gas Limits

Gas automatically scales from VARA_GAS_LIMIT (base):

Operation Multiplier At base=200B
Deposit/Withdraw 1x 200B
SubmitOrder 1x 200B
ExecuteOrder 1.5x 300B
CancelOrder 0.5x 100B

Keystore

Bot keys are generated via gring. Each agent_id maps to a keypair through the AddressBook:

keys/
β”œβ”€β”€ Library/Application Support/gring/   ← keystore
β”œβ”€β”€ .passphrase                          ← passphrase
└── funding_addresses.txt                ← generated automatically

Running

# Build
cargo build --release

# Run with test_strategies config
cargo run --release -- \
  --scenario test_strategies \
  --realtime \
  --skip-deposits \
  --tick-ms 3000 \
  --port 8080

CLI Arguments

Argument Description Default
--scenario NAME Config name (without .json) simple_demo
--realtime Realtime mode false
--tick-ms MS Tick interval (= block time) 3000
--port PORT HTTP API port 8080
--skip-deposits Skip initial deposits false

First Run vs Subsequent Runs

# First run β€” deposits to all accounts
cargo run --release -- --scenario test_strategies --realtime --tick-ms 3000 --port 8080

# Subsequent runs β€” balances already exist on-chain
cargo run --release -- --scenario test_strategies --realtime --skip-deposits --tick-ms 3000 --port 8080

Scenario Configuration

Config files are located at src/scenarios/*.json.

Timing and Block Time

Vara block time β‰ˆ 3 seconds. A transaction takes 2 steps (Submit + Execute) = minimum 2 blocks = 6 seconds.

Rules for wake_interval_ms:

  • Minimum = block_time * 2 = 6000ms
  • Sending orders faster than the block time is pointless β€” they will land in the same or next block anyway

Rules for start_delay_ms:

  • MarketMaker: 0 (starts first)
  • Everyone else: after MM positions are confirmed on-chain (~20s)

Agent Startup Order

t=0s      Oracle + Exchange + MarketMaker
          MM sends SEED orders (Long + Short)

t=6-12s   MM positions confirmed on-chain
          OI sync picks up non-zero positions

t=20-25s  Arbitrageurs + FundingHarvesters + LimitTraders
          See non-zero OI, start trading

t=30-45s  Hodlers + Institutional
          Market is already active

Config Example (Key Fields)

{
  "oracles": [{
    "cache_duration_ms": 3000,
    "wake_interval_ms": 3000
  }],
  "market_maker": {
    "wake_interval_ms": 6000,
    "order_size_tokens": 2.7,
    "leverage": 2
  },
  "keepers": [
    { "wake_interval_ms": 3000 }
  ],
  "smart_traders": [
    {
      "strategy": "arbitrageur",
      "wake_interval_ms": 6000,
      "start_delay_ms": 20000
    },
    {
      "strategy": "hodler",
      "wake_interval_ms": 9000,
      "start_delay_ms": 30000
    }
  ]
}

Bot Strategies

MarketMaker

Provides liquidity. Monitors OI balance between Long/Short, places SEED orders on the weaker side.

Arbitrageur

Catches divergence between the on-chain price and oracle (Pyth). Opens a position when deviation > threshold, closes when the price reverts.

FundingHarvester

Trades the funding rate. Opens a position on the side with positive funding, holds until exit deviation.

Hodler

Directional bet (Long/Short) with a fixed hold duration. TP/SL based on percentage thresholds.

Institutional

Large positions with long hold times and moderate leverage.

Limit Traders

  • MeanReversion β€” limit orders at current price Β± offset
  • Breakout β€” limit orders to catch level breakouts
  • Grid β€” grid of orders around the current price
  • Smart β€” technical analysis (SMA crossover + RSI + ATR)

Keepers

Execute pending limit/stop/TP orders when the price reaches the trigger level.

LiquidationAgent

Scans positions for liquidation, sends liquidation orders.

API

HTTP API (:8080)

# Open a position
curl -X POST http://localhost:8080/order -d '{
  "action": "open",
  "symbol": "ETH-USD",
  "side": "long",
  "qty": 1.0,
  "leverage": 5
}'

# Close a position
curl -X POST http://localhost:8080/order -d '{
  "action": "close",
  "symbol": "ETH-USD",
  "side": "long"
}'

WebSocket API (:8081)

const ws = new WebSocket('ws://localhost:8081');

ws.onopen = () => {
  ws.send(JSON.stringify({
    action: 'open',
    symbol: 'ETH-USD',
    side: 'long',
    qty: 5,
    leverage: 10
  }));
};

ws.onmessage = (event) => {
  const data = JSON.parse(event.data);
  // data.type: 'Event' | 'Response' | 'Error'
  // Event types: OracleTick, OrderExecuted, PositionLiquidated, PositionSnapshot
};

Logs

CSV logs are written to logs/:

File Contents
orders.csv All submitted orders
executions.csv Confirmed executions
oracle.csv Price ticks
positions.csv Position snapshots
markets.csv OI and liquidity

On-chain transaction results are also logged to vara_transactions.csv.

Project Structure

src/
β”œβ”€β”€ main.rs                 # CLI + VaraClient init
β”œβ”€β”€ kernel.rs               # Event loop + message queue
β”œβ”€β”€ sim_engine.rs           # SimEngine wrapper
β”œβ”€β”€ agents/
β”‚   β”œβ”€β”€ exchange_agent.rs   # Bridge: sim ↔ on-chain contract
β”‚   β”œβ”€β”€ market_maker_agent.rs
β”‚   β”œβ”€β”€ smart_trader_agent.rs
β”‚   β”œβ”€β”€ limit_trader_agent.rs
β”‚   β”œβ”€β”€ keeper_agent.rs
β”‚   β”œβ”€β”€ liquidation_agent.rs
β”‚   β”œβ”€β”€ human_agent.rs      # HTTP/WS β†’ sim messages
β”‚   └── oracle_agent.rs     # Pyth price feed
β”œβ”€β”€ vara/
β”‚   β”œβ”€β”€ client.rs           # VaraClient (gclient + sails)
β”‚   β”œβ”€β”€ keystore.rs         # Keypair management
β”‚   β”œβ”€β”€ types.rs            # Generated types re-export
β”‚   └── vara_perps.idl      # Contract IDL
β”œβ”€β”€ scenarios/
β”‚   β”œβ”€β”€ simple_demo.rs      # Scenario loader + runner
β”‚   β”œβ”€β”€ test_strategies.json
β”‚   └── *.json              # Other configs
β”œβ”€β”€ api/
β”‚   β”œβ”€β”€ server.rs           # HTTP API
β”‚   β”œβ”€β”€ ws.rs               # WebSocket API
β”‚   β”œβ”€β”€ pyth.rs             # Pyth price provider
β”‚   └── cache.rs            # Price cache
β”œβ”€β”€ messages.rs             # Message types + SimulatorApi
β”œβ”€β”€ events.rs               # EventBus + CSV logging
β”œβ”€β”€ logging.rs              # CSV loggers
β”œβ”€β”€ latency.rs              # Network latency model
β”œβ”€β”€ pending_orders.rs       # Pending order tracking
└── trigger_checker.rs      # Limit/Stop trigger logic

License

MIT

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Rust 87.6%
  • TypeScript 7.9%
  • CSS 3.4%
  • Other 1.1%