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.
βββββββββββββββββββββββββββββββββββββββββββββββ
β 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.
- Rust toolchain (stable)
- Deployed VaraPerps contract on Vara testnet
- Keystore with bot keys (gring format)
- Passphrase file for the keystore
# 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 HumanAgentGas 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 |
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
# Build
cargo build --release
# Run with test_strategies config
cargo run --release -- \
--scenario test_strategies \
--realtime \
--skip-deposits \
--tick-ms 3000 \
--port 8080| 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 β 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 8080Config files are located at src/scenarios/*.json.
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)
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
{
"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
}
]
}Provides liquidity. Monitors OI balance between Long/Short, places SEED orders on the weaker side.
Catches divergence between the on-chain price and oracle (Pyth). Opens a position when deviation > threshold, closes when the price reverts.
Trades the funding rate. Opens a position on the side with positive funding, holds until exit deviation.
Directional bet (Long/Short) with a fixed hold duration. TP/SL based on percentage thresholds.
Large positions with long hold times and moderate leverage.
- 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)
Execute pending limit/stop/TP orders when the price reaches the trigger level.
Scans positions for liquidation, sends liquidation orders.
# 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"
}'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
};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.
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
MIT