Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
441c2b8
chore: changelog
Unique-Divine Nov 4, 2025
bf1cd79
sai-trading: add first set of artifacts
Unique-Divine Nov 7, 2025
8bbf099
sai-trading: toml for build info
Unique-Divine Nov 7, 2025
0ac2a14
sai-trading: start a small program that uses a running nibid process …
Unique-Divine Nov 7, 2025
a989fee
chore: add out.txt to gitignore; add justfile comment
Unique-Divine Nov 8, 2025
1908fe2
Merge branch 'main' into ud/sai-trading-init
Unique-Divine Nov 8, 2025
9cee9d6
sai-trading: project scaffolding with script to deploy all Sai contracts
Unique-Divine Nov 8, 2025
f013071
sai-trading: get a simple test going that checks for the running netw…
Unique-Divine Nov 8, 2025
bc2f0ec
test(ci): need to install bun before running the script
Unique-Divine Nov 8, 2025
b132441
use which instead of command -v
Unique-Divine Nov 8, 2025
c1eefb7
fix array concatenation
Unique-Divine Nov 8, 2025
45b7814
make pr title lint more flexible to account for sai-trading and rever…
Unique-Divine Nov 8, 2025
ea462ec
test(.github): fix just install if statement
Unique-Divine Nov 8, 2025
ccec696
chore: linter
Unique-Divine Nov 8, 2025
2120654
fix(linter): revert the build tag issue and disable that small piece …
Unique-Divine Nov 8, 2025
c0d0cdf
Merge branch 'main' into ud/sai-trading-init
Unique-Divine Nov 10, 2025
d7ce146
Delete TODO_ANALYSIS.md
Unique-Divine Nov 10, 2025
5224eb6
evm trader
expertdicer Nov 12, 2025
aef4759
README
expertdicer Nov 12, 2025
c7646e4
merge
expertdicer Nov 12, 2025
6fa850f
adding open and close command, utilities command for querying markets…
expertdicer Nov 18, 2025
5b3ee3e
Merge branch 'main' into expertdicer/sai-trading
expertdicer Nov 20, 2025
1ab13a6
feat: add open-price flag for limit and stop orders in open command
expertdicer Nov 25, 2025
7abf185
feat: enhance trade parameters validation and open price handling
expertdicer Nov 25, 2025
1238389
feat: add TOML configuration for network settings and enhance trader …
expertdicer Dec 3, 2025
6d65dc5
feat: implement automated trading functionality with configuration su…
expertdicer Dec 5, 2025
c8cefdd
Merge branch 'main' into expertdicer/sai-trading
expertdicer Dec 5, 2025
814a043
Merge branch 'main' into expertdicer/sai-trading
expertdicer Dec 9, 2025
44f3c83
feat: add Slack notification configuration and error keyword filtering
expertdicer Dec 9, 2025
eb55d68
add .env.example file and update README for configuration instructions
expertdicer Dec 9, 2025
f662fa7
replace log calls with logError
expertdicer Dec 9, 2025
9ae53d5
Updated .env.example and README. Enhance Slack notification configura…
expertdicer Dec 11, 2025
4429b98
Merge branch 'main' into expertdicer/sai-trading
expertdicer Dec 11, 2025
a50f0e2
Merge branch 'main' into expertdicer/sai-trading
expertdicer Dec 17, 2025
cd41805
adding unit tests
expertdicer Dec 17, 2025
19179ef
update Dockerfile
expertdicer Dec 22, 2025
e2f19b7
feat: add SAI Trading E2E tests and update deployment processadding u…
expertdicer Dec 22, 2025
2ec35f2
minor fix
expertdicer Dec 22, 2025
4fdb05d
enhance trading functionality with account derivation and balance checks
expertdicer Jan 6, 2026
43e9b2f
refactor logging
expertdicer Jan 6, 2026
90fe63a
minor
expertdicer Jan 8, 2026
9aebf1e
Merge branch 'main' into expertdicer/sai-trading
expertdicer Jan 9, 2026
c1545a1
use mnemonic in e2e test
expertdicer Jan 13, 2026
525f0e2
update
expertdicer Jan 13, 2026
1132bd6
e2e
expertdicer Jan 13, 2026
a9bd156
helper method
expertdicer Jan 13, 2026
7ddf994
remove unused config and correct balance check
expertdicer Jan 13, 2026
b9679db
fix e2e
expertdicer Jan 14, 2026
00a75b4
enhance logging with token denom and balances
expertdicer Jan 14, 2026
244391f
index parsing helpers and enhance query logic
expertdicer Jan 15, 2026
d39e550
add balance logging on trade close and enhance index unmarshal logic
expertdicer Jan 15, 2026
2ac652f
gas estimation and balance checks for trading
expertdicer Jan 15, 2026
952f590
manage collateral and market token denoms
expertdicer Jan 15, 2026
f26c402
remove unused Cosmos address handling and trade size calculation logic
expertdicer Jan 20, 2026
e7f23e1
use arrays for market and collateral indices, add log before a trade
expertdicer Jan 22, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion .github/workflows/e2e-evm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,10 @@ jobs:
- if: steps.check_changes.outputs.check_changes == 'true'
working-directory: "sai-trading"
run: |
just test
just e2e-deploy

- if: steps.check_changes.outputs.check_changes == 'true'
name: "Run SAI Trading E2E tests"
working-directory: "sai-trading"
run: |
just e2e-test
12 changes: 12 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ test-e2e:
cd evm-e2e
just test

# Runs the SAI Trading E2E tests
test-e2e-sai-trading:
#!/usr/bin/env bash
source contrib/bashlib.sh
log_info "Make sure the localnet is running! (just localnet)"
log_info "Deploying SAI contracts..."

cd sai-trading
just e2e-deploy
log_info "Running SAI Trading E2E tests..."
just e2e-test


# Test: "localnet.sh" script
test-localnet:
Expand Down
4 changes: 4 additions & 0 deletions sai-trading/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
EVM_MNEMONIC=""

# Optional: Slack webhook for error notifications (leave empty to disable)
SLACK_WEBHOOK=""
9 changes: 9 additions & 0 deletions sai-trading/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@ artifacts/solidity/build-info
*.dbg.json

.env

# Compiled binaries
trader
build/
*.exe
*.exe~
*.dll
*.so
*.dylib
51 changes: 51 additions & 0 deletions sai-trading/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
FROM golang:1.24 AS builder

WORKDIR /nibiru

RUN apt-get update && \
apt-get install -y --no-install-recommends \
musl-dev build-essential libgflags-dev libsnappy-dev zlib1g-dev libbz2-dev liblz4-dev libzstd-dev ca-certificates wget && \
rm -rf /var/lib/apt/lists/*

ARG WASMVM_VERSION=1.5.9
ARG TARGETARCH

RUN mkdir -p /usr/lib && \
if [ "${TARGETARCH}" = "arm64" ]; then \
wget -q https://github.com/CosmWasm/wasmvm/releases/download/v${WASMVM_VERSION}/libwasmvm_muslc.aarch64.a -O /usr/lib/libwasmvm_muslc.a; \
else \
wget -q https://github.com/CosmWasm/wasmvm/releases/download/v${WASMVM_VERSION}/libwasmvm_muslc.x86_64.a -O /usr/lib/libwasmvm_muslc.a; \
fi

COPY go.mod go.sum /nibiru/
COPY internal/ /nibiru/internal/
RUN go mod download

COPY sai-trading/go.mod sai-trading/go.sum /nibiru/sai-trading/
RUN cd sai-trading && go mod download

COPY . .

WORKDIR /nibiru/sai-trading

RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg \
--mount=type=cache,target=/nibiru/sai-trading/temp \
CGO_ENABLED=1 \
go build -tags "netgo muslc" \
-ldflags '-linkmode=external -extldflags "-Wl,-z,muldefs -static -lstdc++ -lm -lz -lbz2 -lsnappy -llz4 -lzstd -lpthread"' \
-o ./build/trader ./cmd/trader

FROM alpine:3.20

WORKDIR /root

RUN apk --no-cache add ca-certificates

COPY --from=builder /nibiru/sai-trading/build/trader /usr/local/bin/trader

COPY --from=builder /nibiru/sai-trading/networks.toml /root/networks.toml
COPY --from=builder /nibiru/sai-trading/auto-trader.localnet.json /root/
COPY --from=builder /nibiru/sai-trading/auto-trader.testnet.json /root/

ENTRYPOINT ["trader"]
95 changes: 67 additions & 28 deletions sai-trading/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,51 +46,90 @@ https://github.com/mikefarah/yq?tab=readme-ov-file#github-action

## Running the EVM Trader

### Configuration via `.env` file
### Configuration

Create a `.env` file in the root directory to configure the trader:
Create a `.env` file in the root directory (see `.env.example`):

```bash
# Account credentials (use either private key OR mnemonic)
EVM_PRIVATE_KEY=0x1234567890abcdef... # Your private key in hex format
EVM_PRIVATE_KEY=0x1234567890abcdef...
# OR
EVM_MNEMONIC="word1 word2 word3 ..." # Your BIP39 mnemonic phrase
EVM_MNEMONIC="word1 word2 word3 ..."

SLACK_WEBHOOK=""
```

#### Slack Notification Configuration (Optional)

Slack notifications are optional. If not configured, errors will still be logged to stdout.

**Step 1: Set Slack Webhook**

Set your Slack webhook URL in `.env` file:

```bash
SLACK_WEBHOOK="https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
```

**Step 2: Configure Error Filters**

Configure which errors to send to Slack in `networks.toml`:

```toml
[notifications.filters]
include = ["insufficient funds", "gas error", "execution failed"]
exclude = ["expected error", "test"]
```

**Filter Logic:**
- **Exclude list**: If any keyword matches, skip the notification (highest priority)
- **Include list**: If not empty, only send if at least one keyword matches
- **Empty filters**: Send all errors to Slack

### Running the trader

**Dynamic trading** (uses config parameters):
**Auto trading** :
```bash
just run-trader
# or with custom parameters:
just run-trader --market-index 0 --leverage-min 5 --leverage-max 20
just run-trader auto --network testnet
```

**Static JSON file trading**:
With custom parameters:
```bash
just run-trader auto --market-index 0 --min-leverage 5 --max-leverage 20 --blocks-before-close 20 --network testnet
```

Or use a config file:
```bash
go run ./cmd/trader auto --config auto-trader.localnet.json
```

**Manual trading**:
```bash
# Open a single position
just run-trader open --trade-type trade --market-index 0 --long false --trade-size 1 --network testnet

# Using JSON file
just run-trader --trade-json sample_txs/open_trade.json
```

### Available flags

- `--network`: Network mode (`localnet`, `testnet`, `mainnet`)
**Root flags (shared across all commands):**
- `--network`: Network mode (`localnet`, `testnet`, `mainnet`) (default: `localnet`)
- `--evm-rpc`: EVM RPC URL (overrides network mode default)
- `--networks-toml`: Path to networks TOML configuration file (default: `networks.toml`)
- `--contracts-env`: Path to contracts env file (legacy, overrides networks.toml)
- `--private-key`: Private key in hex format (overrides `EVM_PRIVATE_KEY` env var)
- `--mnemonic`: BIP39 mnemonic phrase (overrides `EVM_MNEMONIC` env var)
- `--contracts-env`: Path to contracts env file (defaults to `.cache/localnet_contracts.env`)
- `--trade-json`: Path to JSON file with trade parameters (overrides dynamic trading)
- `--market-index`: Market index to trade (default: 0)
- `--collateral-index`: Collateral token index (default: 1)
- `--leverage-min`: Minimum leverage (default: 5)
- `--leverage-max`: Maximum leverage (default: 20)
- `--trade-size-min`: Minimum trade size in smallest units (default: 10000)
- `--trade-size-max`: Maximum trade size in smallest units (default: 50000)
- `--enable-limit-order`: Enable limit order trading (default: false)

### Example `.env` file

```bash
# Account
EVM_MNEMONIC="guard cream sadness conduct invite crumble clock pudding hole grit liar hotel maid produce squeeze return argue turtle know drive eight casino maze host"
```

**Note**: The `.env` file is automatically loaded if present. You can also pass values via command-line flags, which take precedence over environment variables.

**Auto command flags:**
- `--config`: Path to JSON config file (optional)
- `--market-index`: Market index to trade (default: 0)
- `--collateral-index`: Collateral token index (default: 0, uses market's quote token)
- `--min-trade-size`: Minimum trade size in smallest units (default: 1000000)
- `--max-trade-size`: Maximum trade size in smallest units (default: 5000000)
- `--min-leverage`: Minimum leverage (default: 1, e.g., 1 for 1x)
- `--max-leverage`: Maximum leverage (default: 10, e.g., 10 for 10x)
- `--blocks-before-close`: Number of blocks to wait before closing a position (default: 20)
- `--max-open-positions`: Maximum number of positions to keep open at once (default: 5)
- `--loop-delay`: Delay in seconds between each loop iteration (default: 5)
20 changes: 20 additions & 0 deletions sai-trading/auto-trader.localnet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"network": {
"mode": "localnet",
"evm_rpc_url": "http://localhost:8545",
"networks_toml": "networks.toml"
},
"trading": {
"market_indices": [0],
"collateral_indices": [1],
"min_trade_size": 10000,
"max_trade_size": 50000,
"min_leverage": 2,
"max_leverage": 8
},
"bot": {
"blocks_before_close": 20,
"max_open_positions": 3,
"loop_delay_seconds": 5
}
}
19 changes: 19 additions & 0 deletions sai-trading/auto-trader.testnet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"network": {
"mode": "testnet",
"networks_toml": "networks.toml"
},
"trading": {
"market_indices": [0, 1, 14, 18, 44],
"collateral_indices": [4],
"min_trade_size": 10000,
"max_trade_size": 100000,
"min_leverage": 1,
"max_leverage": 5
},
"bot": {
"blocks_before_close": 10,
"max_open_positions": 2,
"loop_delay_seconds": 10
}
}
Loading
Loading