feat(sai-trading): Trades against testnet network#2484
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces a comprehensive CLI tool and an automated trading bot for the Sai trading platform. The changes are well-structured, with clear separation of concerns between the CLI, the core trader service, and configuration management. The addition of configuration files (networks.toml, auto-trader.json) and justfile recipes significantly improves usability.
My review focuses on the new Go code. I've identified a critical bug in how trade parameters are prepared, issues with the --long flag in the CLI, and incorrect logic for placing stop orders in the auto-trader. The feedback includes specific code suggestions to address these points. Overall, this is a great addition, and with these fixes, it will be a robust tool for interacting with the trading platform.
I am having trouble creating individual review comments. Click here to see my feedback.
sai-trading/services/evmtrader/trade_params.go (36-37)
The trade amount is incorrectly determined. It directly uses t.cfg.TradeSize, which is only set by the --trade-size flag and defaults to 0. This ignores the --trade-size-min and --trade-size-max flags, and also skips the crucial balance check.
The function determineTradeAmount, which contains the correct logic for calculating trade size and checking balance, is defined but never called within prepareTradeFromConfig. This is a critical bug that will cause trades from the open command to fail or use an incorrect size.
The TODO on line 36 indicates this was likely unfinished. You should replace the current implementation with a call to determineTradeAmount.
// Step 2: Determine trade amount (validates balance internally)
tradeAmt, err := t.determineTradeAmount(balance)
if err != nil {
return nil, err
}
if tradeAmt == nil {
// This means insufficient balance or no trade size configured.
// The log is already handled in determineTradeAmount.
return nil, nil
}
sai-trading/cmd/trader/main.go (114)
The default value for the --long flag is set to false, but the help text says (default: true). This is contradictory and confusing. The effective behavior when the flag is omitted is indeed long (true), due to how a nil pointer is handled by the evmtrader service. To make the flag's behavior explicit and align with the documentation, the default value should be set to true.
cmd.Flags().BoolVar(&long, "long", true, "Trade direction: true for long, false for short")sai-trading/services/evmtrader/auto_trader.go (197-217)
The logic for setting the trigger price for stop orders in the auto-trader appears to be incorrect for creating stop-entry orders.
- For a long stop order, the trigger price is set below the market price. A buy-stop order should be placed above the market price to enter a trade on an upward breakout.
- For a short stop order, the trigger price is set above the market price. A sell-stop order should be placed below the market price to enter a trade on a downward breakdown.
The current implementation effectively sets stop-loss orders for positions that don't exist yet, which will likely not execute as intended for opening new trades. The comments are also confusing as they refer to "stop loss".
if isLong {
// For long limit orders, set trigger price below market (buy cheaper)
if tradeType == TradeTypeLimit {
triggerPrice := marketPrice * (1.0 - adjustmentPercent)
openPrice = &triggerPrice
} else { // stop
// For long stop-entry orders, set trigger price above market (buy on breakout)
triggerPrice := marketPrice * (1.0 + adjustmentPercent)
openPrice = &triggerPrice
}
} else {
// For short limit orders, set trigger price above market (sell higher)
if tradeType == TradeTypeLimit {
triggerPrice := marketPrice * (1.0 + adjustmentPercent)
openPrice = &triggerPrice
} else { // stop
// For short stop-entry orders, set trigger price below market (sell on breakdown)
triggerPrice := marketPrice * (1.0 - adjustmentPercent)
openPrice = &triggerPrice
}
}sai-trading/cmd/trader/main.go (98-108)
The logic for handling the --long flag by checking cmd.Flags().Changed("long") is overly complex. Assuming the flag's default value is corrected to true (as suggested in a separate comment), you can simplify this RunE function by removing the conditional pointer logic for long and always passing its address to runOpen.
RunE: func(cmd *cobra.Command, args []string) error {
var openPricePtr *float64
if cmd.Flags().Changed("open-price") {
openPricePtr = &openPrice
}
return runOpen(tradeSize, leverage, &long, marketIndex, collateralIndex, tradeType, openPricePtr, tradeJSONFile)
},* chore(deps): bump github.com/CosmWasm/wasmvm from 1.5.8 to 1.5.9 Bumps [github.com/CosmWasm/wasmvm](https://github.com/CosmWasm/wasmvm) from 1.5.8 to 1.5.9. - [Release notes](https://github.com/CosmWasm/wasmvm/releases) - [Changelog](https://github.com/CosmWasm/wasmvm/blob/main/CHANGELOG.md) - [Commits](CosmWasm/wasmvm@v1.5.8...v1.5.9) --- updated-dependencies: - dependency-name: github.com/CosmWasm/wasmvm dependency-version: 1.5.9 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> * chore(deps): bump the dependency in wasmd too --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Unique Divine <realuniquedivine@gmail.com>
Bumps [golang.org/x/crypto](https://github.com/golang/crypto) from 0.37.0 to 0.45.0. - [Commits](golang/crypto@v0.37.0...v0.45.0) --- updated-dependencies: - dependency-name: golang.org/x/crypto dependency-version: 0.45.0 dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Unique Divine <51418232+Unique-Divine@users.noreply.github.com> Co-authored-by: Unique Divine <realuniquedivine@gmail.com>
…#2480) Bumps [github.com/status-im/keycard-go](https://github.com/status-im/keycard-go) from 0.2.0 to 0.3.3. - [Release notes](https://github.com/status-im/keycard-go/releases) - [Commits](keycard-tech/keycard-go@v0.2.0...v0.3.3) --- updated-dependencies: - dependency-name: github.com/status-im/keycard-go dependency-version: 0.3.3 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Unique Divine <realuniquedivine@gmail.com>
Bumps [github.com/spf13/pflag](https://github.com/spf13/pflag) from 1.0.5 to 1.0.10. - [Release notes](https://github.com/spf13/pflag/releases) - [Commits](spf13/pflag@v1.0.5...v1.0.10) --- updated-dependencies: - dependency-name: github.com/spf13/pflag dependency-version: 1.0.10 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Unique Divine <realuniquedivine@gmail.com>
Bumps [github.com/rs/zerolog](https://github.com/rs/zerolog) from 1.32.0 to 1.34.0. - [Commits](rs/zerolog@v1.32.0...v1.34.0) --- updated-dependencies: - dependency-name: github.com/rs/zerolog dependency-version: 1.34.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Unique Divine <realuniquedivine@gmail.com>
…2437) Bumps [github.com/consensys/gnark-crypto](https://github.com/consensys/gnark-crypto) from 0.12.1 to 0.18.1. - [Release notes](https://github.com/consensys/gnark-crypto/releases) - [Changelog](https://github.com/Consensys/gnark-crypto/blob/master/CHANGELOG.md) - [Commits](Consensys/gnark-crypto@v0.12.1...v0.18.1) --- updated-dependencies: - dependency-name: github.com/consensys/gnark-crypto dependency-version: 0.18.1 dependency-type: indirect ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Unique Divine <realuniquedivine@gmail.com>
…ps): upgrade gnark-crypto and go-kzg-4844 for compatibility (#2490) * refactor(collections): merge lib into repo * fi(deps): Use accurate reference to Nibiru code in `sai-trading` dir * fix: linter; Add workflow condition for go.mod and go.sum in `sai-trading` job * fix(deps): need compatible versions of deps for gnark pkg
Bumps [gotest.tools/v3](https://github.com/gotestyourself/gotest.tools) from 3.5.1 to 3.5.2. - [Release notes](https://github.com/gotestyourself/gotest.tools/releases) - [Commits](gotestyourself/gotest.tools@v3.5.1...v3.5.2) --- updated-dependencies: - dependency-name: gotest.tools/v3 dependency-version: 3.5.2 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Unique Divine <51418232+Unique-Divine@users.noreply.github.com>
…2488) * chore(deps): bump github.com/cosmos/gogoproto from 1.4.10 to 1.7.2 Bumps [github.com/cosmos/gogoproto](https://github.com/cosmos/gogoproto) from 1.4.10 to 1.7.2. - [Release notes](https://github.com/cosmos/gogoproto/releases) - [Changelog](https://github.com/cosmos/gogoproto/blob/main/CHANGELOG.md) - [Commits](cosmos/gogoproto@v1.4.10...v1.7.2) --- updated-dependencies: - dependency-name: github.com/cosmos/gogoproto dependency-version: 1.7.2 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * chore(deps): bump github.com/cosmos/gogoproto from 1.4.10 to 1.7.2 Bumps [github.com/cosmos/gogoproto](https://github.com/cosmos/gogoproto) from 1.4.10 to 1.7.2. - [Release notes](https://github.com/cosmos/gogoproto/releases) - [Changelog](https://github.com/cosmos/gogoproto/blob/main/CHANGELOG.md) - [Commits](cosmos/gogoproto@v1.4.10...v1.7.2) --- updated-dependencies: - dependency-name: github.com/cosmos/gogoproto dependency-version: 1.7.2 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Unique Divine <realuniquedivine@gmail.com>
* feat: add passkey signing * feat: fix the evm e2e test to have passkey signing * fix: make taht run on e2e * fix: fix lint * fix: address comments by gemini
Bumps [esbuild](https://github.com/evanw/esbuild) to 0.25.12 and updates ancestor dependency [vite](https://github.com/vitejs/vite/tree/HEAD/packages/vite). These dependencies need to be updated together. Updates `esbuild` from 0.21.5 to 0.25.12 - [Release notes](https://github.com/evanw/esbuild/releases) - [Changelog](https://github.com/evanw/esbuild/blob/main/CHANGELOG-2024.md) - [Commits](evanw/esbuild@v0.21.5...v0.25.12) Updates `vite` from 5.4.21 to 7.2.7 - [Release notes](https://github.com/vitejs/vite/releases) - [Changelog](https://github.com/vitejs/vite/blob/v7.2.7/packages/vite/CHANGELOG.md) - [Commits](https://github.com/vitejs/vite/commits/v7.2.7/packages/vite) --- updated-dependencies: - dependency-name: esbuild dependency-version: 0.25.12 dependency-type: indirect - dependency-name: vite dependency-version: 7.2.7 dependency-type: direct:development ... Signed-off-by: dependabot[bot] <support@github.com>
* feat: add bundler for passkey transactions * feat: continue bundler
Bumps [esbuild](https://github.com/evanw/esbuild) to 0.27.1 and updates ancestor dependency [tsup](https://github.com/egoist/tsup). These dependencies need to be updated together. Updates `esbuild` from 0.18.20 to 0.27.1 - [Release notes](https://github.com/evanw/esbuild/releases) - [Changelog](https://github.com/evanw/esbuild/blob/main/CHANGELOG-2023.md) - [Commits](evanw/esbuild@v0.18.20...v0.27.1) Updates `tsup` from 7.2.0 to 8.5.1 - [Release notes](https://github.com/egoist/tsup/releases) - [Commits](egoist/tsup@v7.2.0...v8.5.1) --- updated-dependencies: - dependency-name: esbuild dependency-version: 0.27.1 dependency-type: indirect - dependency-name: tsup dependency-version: 8.5.1 dependency-type: direct:development ... Signed-off-by: dependabot[bot] <support@github.com>
Bumps [cosmossdk.io/core](https://github.com/cosmos/cosmos-sdk) from 0.10.0 to 1.0.0. - [Release notes](https://github.com/cosmos/cosmos-sdk/releases) - [Changelog](https://github.com/cosmos/cosmos-sdk/blob/main/CHANGELOG.md) - [Commits](cosmos/cosmos-sdk@v0.10.0...log/v1.0.0) --- updated-dependencies: - dependency-name: cosmossdk.io/core dependency-version: 1.0.0 dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com>
…tisig self-managing (#2602) * feat(v2.14): Remove legacy SDK multisig as CW4 admin and make CW3 multisig self-managing * feat(v2.14): Return multisig upgrade errors for log/event emission without halting chain
* fix: Make gov module safer; accept only deposit denom * test(evm): Fix test assertion for FunToken token creation * test(bank): Use gov min deposit denom in send-enabled proposal test * refactor(gov): Avoid rebuilding MinDeposit coins in denom error
…ng UX to account for wallet client edge cases (#2596) * feat(evm): Enhance EVM zero gas detection to be automatic for eligible txs * test(evm): More test coverage for automatic zero gas functionality
…zero fee hints for EVM RPC calls used in wallet preflight (#2601)
* docs: markdown content * refactor: Helper fn DRY change * fix(localnet.sh): Show DEBUG logs, make configurable, and fix color terminal output bug * docs: Rm broken links and unused doc files * docs: Remove outdated stuff do broad cleanup * fix(localnet): use echo_error in which_ok before color init which_ok called undefined log_error when tput was missing, which could abort the script under set -euo pipefail instead of disabling colors. Define echo_info/echo_error/echo_success before which_ok so the missing binary path works during console_log_text_color startup. Co-authored-by: Unique Divine <Unique-Divine@users.noreply.github.com> * fix(justfile): just localnet script flag defaults * docs: Fix Cosmos SDK example snippets Co-authored-by: Cursor <cursoragent@cursor.com> * fix(lcoalnet.sh): Color terminal guards not working as expected * ci: Detect localnet startup failures in workflows --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: Unique Divine <Unique-Divine@users.noreply.github.com>
…rice formatting in logs
22fb261 to
ac05154
Compare
…g config structure in tests
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Drop resurrected internal/, evm-e2e/, and evm-forge/ trees kept by a bad merge, restore go.mod and remove stale proto/contrib files, and bump the sai-trading Dockerfile to Go 1.25. Co-authored-by: Cursor <cursoragent@cursor.com>
… and trade info querying
Abstract
Close: #2579
autocommand for running sai tradings,network.tomlfor the network configuration including testnet and mainnet.Dockerfile.