diff --git a/stx-commodity-market/.gitattributes b/stx-commodity-market/.gitattributes new file mode 100644 index 0000000..da6a065 --- /dev/null +++ b/stx-commodity-market/.gitattributes @@ -0,0 +1,3 @@ +tests/** linguist-vendored +vitest.config.js linguist-vendored +* text=lf diff --git a/stx-commodity-market/.gitignore b/stx-commodity-market/.gitignore new file mode 100644 index 0000000..76c2842 --- /dev/null +++ b/stx-commodity-market/.gitignore @@ -0,0 +1,13 @@ + +**/settings/Mainnet.toml +**/settings/Testnet.toml +.cache/** +history.txt + +logs +*.log +npm-debug.log* +coverage +*.info +costs-reports.json +node_modules diff --git a/stx-commodity-market/.vscode/settings.json b/stx-commodity-market/.vscode/settings.json new file mode 100644 index 0000000..3062519 --- /dev/null +++ b/stx-commodity-market/.vscode/settings.json @@ -0,0 +1,4 @@ + +{ + "files.eol": "\n" +} diff --git a/stx-commodity-market/.vscode/tasks.json b/stx-commodity-market/.vscode/tasks.json new file mode 100644 index 0000000..4dec0ff --- /dev/null +++ b/stx-commodity-market/.vscode/tasks.json @@ -0,0 +1,19 @@ + +{ + "version": "2.0.0", + "tasks": [ + { + "label": "check contracts", + "group": "test", + "type": "shell", + "command": "clarinet check" + }, + { + "type": "npm", + "script": "test", + "group": "test", + "problemMatcher": [], + "label": "npm test" + } + ] +} diff --git a/stx-commodity-market/Clarinet.toml b/stx-commodity-market/Clarinet.toml new file mode 100644 index 0000000..3c61532 --- /dev/null +++ b/stx-commodity-market/Clarinet.toml @@ -0,0 +1,19 @@ +[project] +name = 'stx-commodity-market' +description = '' +authors = [] +telemetry = true +cache_dir = '.\.cache' +requirements = [] +[contracts.clarity-trade-engine] +path = 'contracts/clarity-trade-engine.clar' +clarity_version = 2 +epoch = 2.5 +[repl.analysis] +passes = ['check_checker'] + +[repl.analysis.check_checker] +strict = false +trusted_sender = false +trusted_caller = false +callee_filter = false diff --git a/stx-commodity-market/README.md b/stx-commodity-market/README.md new file mode 100644 index 0000000..e9160db --- /dev/null +++ b/stx-commodity-market/README.md @@ -0,0 +1,191 @@ +# Commodities Trading Smart Contract + +## About +A robust Clarity smart contract for managing commodities trading on the Stacks blockchain. This contract enables secure trading of commodities with features including escrow management, position tracking, and price oracle integration. + +## Features +- Secure escrow system for fund management +- Real-time price oracle integration +- Position tracking and management +- Multi-commodity support +- Role-based access control +- Efficient trade execution +- Automated settlement process + +## Contract Structure + +### Core Components + +1. **Administrator Functions** + - Contract initialization + - Price oracle management + - Market status control + - Commodity registration + +2. **Trading Functions** + - Execute trades + - Close positions + - View position details + +3. **Escrow Management** + - Deposit funds + - Withdraw funds + - Check balances + +### Data Maps + +1. **Available Commodities Inventory** + ```clarity + { + commodity-identifier: uint, + available-quantity: uint, + current-market-price: uint, + commodity-owner: principal + } + ``` + +2. **Active Trading Positions** + ```clarity + { + trader-address: principal, + trade-position-id: uint, + commodity-identifier: uint, + traded-quantity: uint, + position-entry-price: uint, + position-creation-timestamp: uint + } + ``` + +3. **Trader Escrow Accounts** + ```clarity + { + trader-address: principal, + escrow-balance: uint + } + ``` + +## Getting Started + +### Prerequisites +- Stacks blockchain environment +- Clarity CLI tools +- STX tokens for deployment and trading + +## Usage Guide + +### For Traders + +1. **Deposit Funds** + ```clarity + (deposit-funds-to-escrow deposit-amount) + ``` + +2. **Execute Trade** + ```clarity + (execute-trade commodity-identifier trade-quantity trade-position-id) + ``` + +3. **Close Position** + ```clarity + (close-trading-position trade-position-id) + ``` + +4. **Withdraw Funds** + ```clarity + (withdraw-funds-from-escrow withdrawal-amount) + ``` + +### For Administrators + +1. **Toggle Market Status** + ```clarity + (toggle-market-trading-status) + ``` + +2. **Update Oracle** + ```clarity + (update-price-oracle-address new-oracle-address) + ``` + +3. **Add New Commodity** + ```clarity + (register-new-commodity commodity-identifier quantity price) + ``` + +## Error Handling + +The contract includes comprehensive error codes: +- `ERROR-UNAUTHORIZED-ACCESS` (u100): Unauthorized operation attempt +- `ERROR-INVALID-COMMODITY-PRICE` (u101): Invalid price input +- `ERROR-INSUFFICIENT-ESCROW-BALANCE` (u102): Insufficient funds +- `ERROR-TRADING-DISABLED` (u103): Market is closed +- `ERROR-INVALID-TRADE-QUANTITY` (u104): Invalid trade amount +- `ERROR-ESCROW-TRANSACTION-FAILED` (u105): Escrow operation failed + +## Security Considerations + +1. **Access Control** + - Administrator-only functions are protected + - Escrow system prevents unauthorized withdrawals + +2. **Trade Validation** + - Minimum trade quantity enforcement + - Price validation + - Balance verification before execution + +3. **Fund Safety** + - Escrow-based trading + - Atomic settlements + - Balance checks before operations + +## Best Practices + +1. **For Traders** + - Always verify transaction parameters + - Maintain sufficient escrow balance + - Monitor position status regularly + - Verify market status before trading + +2. **For Administrators** + - Regular oracle updates + - Periodic market status review + - Monitor trading volumes + - Validate new commodity parameters + +## Limitations and Known Issues + +1. **Price Updates** + - Relies on external oracle + - Update frequency dependent on oracle + +2. **Trade Execution** + - Minimum trade quantity enforced + - Maximum position size not implemented + +3. **Market Hours** + - No automatic market hour controls + - Manual trading status toggle only + +## Future Enhancements + +1. **Planned Features** + - Multi-oracle support + - Advanced position types + - Automated market makers + - Position transfer capability + - Advanced reporting features + +2. **Under Consideration** + - Margin trading + - Stop-loss orders + - Automated liquidation + - Market maker incentives + +## Contributing + +We welcome contributions! Please follow these steps: +1. Fork the repository +2. Create a feature branch +3. Submit a pull request +4. Follow coding standards +5. Include tests for new features \ No newline at end of file diff --git a/stx-commodity-market/Testnet.toml b/stx-commodity-market/Testnet.toml new file mode 100644 index 0000000..b9cfb45 --- /dev/null +++ b/stx-commodity-market/Testnet.toml @@ -0,0 +1,7 @@ +[network] +name = "testnet" +stacks_node_rpc_address = "https://api.testnet.hiro.so" +deployment_fee_rate = 10 + +[accounts.deployer] +mnemonic = "" diff --git a/stx-commodity-market/contracts/clarity-trade-engine.clar b/stx-commodity-market/contracts/clarity-trade-engine.clar new file mode 100644 index 0000000..b43596f --- /dev/null +++ b/stx-commodity-market/contracts/clarity-trade-engine.clar @@ -0,0 +1,244 @@ +;; Commodities Trading Contract +;; Implements secure trading of commodities with price feeds, escrow, and trading functionality + +;; Error codes +(define-constant ERROR-UNAUTHORIZED-ACCESS (err u100)) +(define-constant ERROR-INVALID-COMMODITY-PRICE (err u101)) +(define-constant ERROR-INSUFFICIENT-ESCROW-BALANCE (err u102)) +(define-constant ERROR-TRADING-DISABLED (err u103)) +(define-constant ERROR-INVALID-TRADE-QUANTITY (err u104)) +(define-constant ERROR-ESCROW-TRANSACTION-FAILED (err u105)) +(define-constant ERROR-INVALID-INPUT (err u106)) + +;; Data Variables +(define-data-var contract-administrator principal tx-sender) +(define-data-var commodity-price-oracle principal tx-sender) +(define-data-var market-trading-status bool true) +(define-data-var minimum-trade-quantity uint u100) + +;; Data Maps +(define-map available-commodities-inventory + { commodity-identifier: uint } + { + available-quantity: uint, + current-market-price: uint, + commodity-owner: principal + } +) + +(define-map active-trading-positions + { trader-address: principal, trade-position-id: uint } + { + commodity-identifier: uint, + traded-quantity: uint, + position-entry-price: uint, + position-creation-timestamp: uint + } +) + +(define-map trader-escrow-accounts + { trader-address: principal } + { escrow-balance: uint } +) + +;; Read-only functions +(define-read-only (get-commodity-market-data (commodity-identifier uint)) + (match (map-get? available-commodities-inventory { commodity-identifier: commodity-identifier }) + commodity-market-data (ok commodity-market-data) + (err u404) + ) +) + +(define-read-only (get-trader-position-details (trader-address principal) (trade-position-id uint)) + (match (map-get? active-trading-positions { trader-address: trader-address, trade-position-id: trade-position-id }) + trader-position-data (ok trader-position-data) + (err u404) + ) +) + +(define-read-only (get-trader-escrow-balance (trader-address principal)) + (match (map-get? trader-escrow-accounts { trader-address: trader-address }) + escrow-account-data (ok (get escrow-balance escrow-account-data)) + (err u404) + ) +) + +;; Private functions +(define-private (verify-administrator-access (caller-address principal)) + (if (is-eq caller-address (var-get contract-administrator)) + (ok true) + ERROR-UNAUTHORIZED-ACCESS + ) +) + +(define-private (validate-trade-parameters (trade-quantity uint) (trade-price uint)) + (if (and + (>= trade-quantity (var-get minimum-trade-quantity)) + (> trade-price u0) + ) + (ok true) + ERROR-INVALID-TRADE-QUANTITY + ) +) + +(define-private (validate-uint (value uint)) + (> value u0) +) + +;; Public functions +(define-public (update-price-oracle-address (new-oracle-address principal)) + (begin + (try! (verify-administrator-access tx-sender)) + (asserts! (is-some (some new-oracle-address)) ERROR-INVALID-INPUT) + (ok (var-set commodity-price-oracle new-oracle-address)) + ) +) + +(define-public (toggle-market-trading-status) + (begin + (try! (verify-administrator-access tx-sender)) + (ok (var-set market-trading-status (not (var-get market-trading-status)))) + ) +) + +(define-public (register-new-commodity (commodity-identifier uint) (initial-available-quantity uint) (initial-market-price uint)) + (begin + (try! (verify-administrator-access tx-sender)) + (asserts! (validate-uint commodity-identifier) ERROR-INVALID-INPUT) + (asserts! (validate-uint initial-available-quantity) ERROR-INVALID-INPUT) + (asserts! (validate-uint initial-market-price) ERROR-INVALID-INPUT) + (try! (validate-trade-parameters initial-available-quantity initial-market-price)) + (map-set available-commodities-inventory + { commodity-identifier: commodity-identifier } + { + available-quantity: initial-available-quantity, + current-market-price: initial-market-price, + commodity-owner: tx-sender + } + ) + (ok true) + ) +) + +(define-public (deposit-funds-to-escrow (deposit-amount uint)) + (begin + (asserts! (validate-uint deposit-amount) ERROR-INVALID-INPUT) + (try! (stx-transfer? deposit-amount tx-sender (as-contract tx-sender))) + (match (map-get? trader-escrow-accounts { trader-address: tx-sender }) + existing-escrow-account + (map-set trader-escrow-accounts + { trader-address: tx-sender } + { escrow-balance: (+ deposit-amount (get escrow-balance existing-escrow-account)) } + ) + (map-set trader-escrow-accounts + { trader-address: tx-sender } + { escrow-balance: deposit-amount } + ) + ) + (ok true) + ) +) + +(define-public (withdraw-funds-from-escrow (withdrawal-amount uint)) + (begin + (asserts! (validate-uint withdrawal-amount) ERROR-INVALID-INPUT) + (match (map-get? trader-escrow-accounts { trader-address: tx-sender }) + escrow-account-data + (if (>= (get escrow-balance escrow-account-data) withdrawal-amount) + (begin + (try! (as-contract (stx-transfer? withdrawal-amount tx-sender tx-sender))) + (map-set trader-escrow-accounts + { trader-address: tx-sender } + { escrow-balance: (- (get escrow-balance escrow-account-data) withdrawal-amount) } + ) + (ok true) + ) + ERROR-INSUFFICIENT-ESCROW-BALANCE + ) + ERROR-INSUFFICIENT-ESCROW-BALANCE + ) + ) +) + +(define-public (execute-trade (commodity-identifier uint) (trade-quantity uint) (trade-position-id uint)) + (let ( + (commodity-data (unwrap! (get-commodity-market-data commodity-identifier) ERROR-INVALID-COMMODITY-PRICE)) + (current-market-price (get current-market-price commodity-data)) + (total-trade-cost (* trade-quantity current-market-price)) + ) + (begin + (asserts! (var-get market-trading-status) ERROR-TRADING-DISABLED) + (asserts! (validate-uint commodity-identifier) ERROR-INVALID-INPUT) + (asserts! (validate-uint trade-quantity) ERROR-INVALID-INPUT) + (asserts! (validate-uint trade-position-id) ERROR-INVALID-INPUT) + (try! (validate-trade-parameters trade-quantity current-market-price)) + + ;; Check escrow balance + (match (map-get? trader-escrow-accounts { trader-address: tx-sender }) + escrow-account-data + (if (>= (get escrow-balance escrow-account-data) total-trade-cost) + (begin + ;; Update escrow balance + (map-set trader-escrow-accounts + { trader-address: tx-sender } + { escrow-balance: (- (get escrow-balance escrow-account-data) total-trade-cost) } + ) + + ;; Record trading position + (map-set active-trading-positions + { trader-address: tx-sender, trade-position-id: trade-position-id } + { + commodity-identifier: commodity-identifier, + traded-quantity: trade-quantity, + position-entry-price: current-market-price, + position-creation-timestamp: block-height + } + ) + (ok true) + ) + ERROR-INSUFFICIENT-ESCROW-BALANCE + ) + ERROR-INSUFFICIENT-ESCROW-BALANCE + ) + ) + ) +) + +(define-public (close-trading-position (trade-position-id uint)) + (let ( + (position-data (unwrap! (get-trader-position-details tx-sender trade-position-id) ERROR-INVALID-TRADE-QUANTITY)) + (commodity-data (unwrap! (get-commodity-market-data (get commodity-identifier position-data)) ERROR-INVALID-COMMODITY-PRICE)) + (current-market-price (get current-market-price commodity-data)) + (position-settlement-amount (* (get traded-quantity position-data) current-market-price)) + ) + (begin + (asserts! (validate-uint trade-position-id) ERROR-INVALID-INPUT) + ;; Return funds to escrow account + (match (map-get? trader-escrow-accounts { trader-address: tx-sender }) + escrow-account-data + (map-set trader-escrow-accounts + { trader-address: tx-sender } + { escrow-balance: (+ (get escrow-balance escrow-account-data) position-settlement-amount) } + ) + (map-set trader-escrow-accounts + { trader-address: tx-sender } + { escrow-balance: position-settlement-amount } + ) + ) + + ;; Delete the position + (map-delete active-trading-positions { trader-address: tx-sender, trade-position-id: trade-position-id }) + (ok true) + ) + ) +) + +;; Contract initialization +(define-public (initialize-contract (administrator-address principal)) + (begin + (asserts! (is-some (some administrator-address)) ERROR-INVALID-INPUT) + (var-set contract-administrator administrator-address) + (var-set commodity-price-oracle administrator-address) + (ok true) + ) +) \ No newline at end of file diff --git a/stx-commodity-market/package.json b/stx-commodity-market/package.json new file mode 100644 index 0000000..d2cfd23 --- /dev/null +++ b/stx-commodity-market/package.json @@ -0,0 +1,24 @@ + +{ + "name": "stx-commodity-market-tests", + "version": "1.0.0", + "description": "Run unit tests on this project.", + "type": "module", + "private": true, + "scripts": { + "test": "vitest run", + "test:report": "vitest run -- --coverage --costs", + "test:watch": "chokidar \"tests/**/*.ts\" \"contracts/**/*.clar\" -c \"npm run test:report\"" + }, + "author": "", + "license": "ISC", + "dependencies": { + "@hirosystems/clarinet-sdk": "^2.3.2", + "@stacks/transactions": "^6.12.0", + "chokidar-cli": "^3.0.0", + "typescript": "^5.3.3", + "vite": "^5.1.4", + "vitest": "^1.3.1", + "vitest-environment-clarinet": "^2.0.0" + } +} diff --git a/stx-commodity-market/settings/Devnet.toml b/stx-commodity-market/settings/Devnet.toml new file mode 100644 index 0000000..7d865b9 --- /dev/null +++ b/stx-commodity-market/settings/Devnet.toml @@ -0,0 +1,151 @@ +[network] +name = "devnet" +deployment_fee_rate = 10 + +[accounts.deployer] +mnemonic = "twice kind fence tip hidden tilt action fragile skin nothing glory cousin green tomorrow spring wrist shed math olympic multiply hip blue scout claw" +balance = 100_000_000_000_000 +# secret_key: 753b7cc01a1a2e86221266a154af739463fce51219d97e4f856cd7200c3bd2a601 +# stx_address: ST1PQHQKV0RJXZFY1DGX8MNSNYVE3VGZJSRTPGZGM +# btc_address: mqVnk6NPRdhntvfm4hh9vvjiRkFDUuSYsH + +[accounts.wallet_1] +mnemonic = "sell invite acquire kitten bamboo drastic jelly vivid peace spawn twice guilt pave pen trash pretty park cube fragile unaware remain midnight betray rebuild" +balance = 100_000_000_000_000 +# secret_key: 7287ba251d44a4d3fd9276c88ce34c5c52a038955511cccaf77e61068649c17801 +# stx_address: ST1SJ3DTE5DN7X54YDH5D64R3BCB6A2AG2ZQ8YPD5 +# btc_address: mr1iPkD9N3RJZZxXRk7xF9d36gffa6exNC + +[accounts.wallet_2] +mnemonic = "hold excess usual excess ring elephant install account glad dry fragile donkey gaze humble truck breeze nation gasp vacuum limb head keep delay hospital" +balance = 100_000_000_000_000 +# secret_key: 530d9f61984c888536871c6573073bdfc0058896dc1adfe9a6a10dfacadc209101 +# stx_address: ST2CY5V39NHDPWSXMW9QDT3HC3GD6Q6XX4CFRK9AG +# btc_address: muYdXKmX9bByAueDe6KFfHd5Ff1gdN9ErG + +[accounts.wallet_3] +mnemonic = "cycle puppy glare enroll cost improve round trend wrist mushroom scorpion tower claim oppose clever elephant dinosaur eight problem before frozen dune wagon high" +balance = 100_000_000_000_000 +# secret_key: d655b2523bcd65e34889725c73064feb17ceb796831c0e111ba1a552b0f31b3901 +# stx_address: ST2JHG361ZXG51QTKY2NQCVBPPRRE2KZB1HR05NNC +# btc_address: mvZtbibDAAA3WLpY7zXXFqRa3T4XSknBX7 + +[accounts.wallet_4] +mnemonic = "board list obtain sugar hour worth raven scout denial thunder horse logic fury scorpion fold genuine phrase wealth news aim below celery when cabin" +balance = 100_000_000_000_000 +# secret_key: f9d7206a47f14d2870c163ebab4bf3e70d18f5d14ce1031f3902fbbc894fe4c701 +# stx_address: ST2NEB84ASENDXKYGJPQW86YXQCEFEX2ZQPG87ND +# btc_address: mg1C76bNTutiCDV3t9nWhZs3Dc8LzUufj8 + +[accounts.wallet_5] +mnemonic = "hurry aunt blame peanut heavy update captain human rice crime juice adult scale device promote vast project quiz unit note reform update climb purchase" +balance = 100_000_000_000_000 +# secret_key: 3eccc5dac8056590432db6a35d52b9896876a3d5cbdea53b72400bc9c2099fe801 +# stx_address: ST2REHHS5J3CERCRBEPMGH7921Q6PYKAADT7JP2VB +# btc_address: mweN5WVqadScHdA81aATSdcVr4B6dNokqx + +[accounts.wallet_6] +mnemonic = "area desk dutch sign gold cricket dawn toward giggle vibrant indoor bench warfare wagon number tiny universe sand talk dilemma pottery bone trap buddy" +balance = 100_000_000_000_000 +# secret_key: 7036b29cb5e235e5fd9b09ae3e8eec4404e44906814d5d01cbca968a60ed4bfb01 +# stx_address: ST3AM1A56AK2C1XAFJ4115ZSV26EB49BVQ10MGCS0 +# btc_address: mzxXgV6e4BZSsz8zVHm3TmqbECt7mbuErt + +[accounts.wallet_7] +mnemonic = "prevent gallery kind limb income control noise together echo rival record wedding sense uncover school version force bleak nuclear include danger skirt enact arrow" +balance = 100_000_000_000_000 +# secret_key: b463f0df6c05d2f156393eee73f8016c5372caa0e9e29a901bb7171d90dc4f1401 +# stx_address: ST3PF13W7Z0RRM42A8VZRVFQ75SV1K26RXEP8YGKJ +# btc_address: n37mwmru2oaVosgfuvzBwgV2ysCQRrLko7 + +[accounts.wallet_8] +mnemonic = "female adjust gallery certain visit token during great side clown fitness like hurt clip knife warm bench start reunion globe detail dream depend fortune" +balance = 100_000_000_000_000 +# secret_key: 6a1a754ba863d7bab14adbbc3f8ebb090af9e871ace621d3e5ab634e1422885e01 +# stx_address: ST3NBRSFKX28FQ2ZJ1MAKX58HKHSDGNV5N7R21XCP +# btc_address: n2v875jbJ4RjBnTjgbfikDfnwsDV5iUByw + +[accounts.faucet] +mnemonic = "shadow private easily thought say logic fault paddle word top book during ignore notable orange flight clock image wealth health outside kitten belt reform" +balance = 100_000_000_000_000 +# secret_key: de433bdfa14ec43aa1098d5be594c8ffb20a31485ff9de2923b2689471c401b801 +# stx_address: STNHKEPYEPJ8ET55ZZ0M5A34J0R3N5FM2CMMMAZ6 +# btc_address: mjSrB3wS4xab3kYqFktwBzfTdPg367ZJ2d + +[devnet] +disable_stacks_explorer = false +disable_stacks_api = false +# disable_subnet_api = false +# disable_bitcoin_explorer = true +# working_dir = "tmp/devnet" +# stacks_node_events_observers = ["host.docker.internal:8002"] +# miner_mnemonic = "fragile loan twenty basic net assault jazz absorb diet talk art shock innocent float punch travel gadget embrace caught blossom hockey surround initial reduce" +# miner_derivation_path = "m/44'/5757'/0'/0/0" +# faucet_mnemonic = "shadow private easily thought say logic fault paddle word top book during ignore notable orange flight clock image wealth health outside kitten belt reform" +# faucet_derivation_path = "m/44'/5757'/0'/0/0" +# orchestrator_port = 20445 +# bitcoin_node_p2p_port = 18444 +# bitcoin_node_rpc_port = 18443 +# bitcoin_node_username = "devnet" +# bitcoin_node_password = "devnet" +# bitcoin_controller_block_time = 30_000 +# stacks_node_rpc_port = 20443 +# stacks_node_p2p_port = 20444 +# stacks_api_port = 3999 +# stacks_api_events_port = 3700 +# bitcoin_explorer_port = 8001 +# stacks_explorer_port = 8000 +# postgres_port = 5432 +# postgres_username = "postgres" +# postgres_password = "postgres" +# postgres_database = "postgres" +# bitcoin_node_image_url = "quay.io/hirosystems/bitcoind:26.0" +# stacks_node_image_url = "quay.io/hirosystems/stacks-node:devnet-2.5" +# stacks_signer_image_url = "quay.io/hirosystems/stacks-signer:devnet-2.5" +# stacks_api_image_url = "hirosystems/stacks-blockchain-api:master" +# stacks_explorer_image_url = "hirosystems/explorer:latest" +# bitcoin_explorer_image_url = "quay.io/hirosystems/bitcoin-explorer:devnet" +# postgres_image_url = "postgres:alpine" +# enable_subnet_node = true +# subnet_node_image_url = "hirosystems/stacks-subnets:0.8.1" +# subnet_leader_mnemonic = "twice kind fence tip hidden tilt action fragile skin nothing glory cousin green tomorrow spring wrist shed math olympic multiply hip blue scout claw" +# subnet_leader_derivation_path = "m/44'/5757'/0'/0/0" +# subnet_contract_id = "ST173JK7NZBA4BS05ZRATQH1K89YJMTGEH1Z5J52E.subnet-v3-0-1" +# subnet_node_rpc_port = 30443 +# subnet_node_p2p_port = 30444 +# subnet_events_ingestion_port = 30445 +# subnet_node_events_observers = ["host.docker.internal:8002"] +# subnet_api_image_url = "hirosystems/stacks-blockchain-api:master" +# subnet_api_postgres_database = "subnet_api" + +# For testing in epoch 2.1 / using Clarity2 +# epoch_2_0 = 100 +# epoch_2_05 = 100 +# epoch_2_1 = 101 +# epoch_2_2 = 102 +# epoch_2_3 = 103 +# epoch_2_4 = 104 +# epoch_2_5 = 108 + + +# Send some stacking orders +[[devnet.pox_stacking_orders]] +start_at_cycle = 1 +duration = 12 +wallet = "wallet_1" +slots = 2 +btc_address = "mr1iPkD9N3RJZZxXRk7xF9d36gffa6exNC" + +[[devnet.pox_stacking_orders]] +start_at_cycle = 1 +duration = 12 +wallet = "wallet_2" +slots = 1 +btc_address = "muYdXKmX9bByAueDe6KFfHd5Ff1gdN9ErG" + +[[devnet.pox_stacking_orders]] +start_at_cycle = 1 +duration = 12 +wallet = "wallet_3" +slots = 1 +btc_address = "mvZtbibDAAA3WLpY7zXXFqRa3T4XSknBX7" diff --git a/stx-commodity-market/tests/clarity-trade-engine.test.ts b/stx-commodity-market/tests/clarity-trade-engine.test.ts new file mode 100644 index 0000000..f2d4bb8 --- /dev/null +++ b/stx-commodity-market/tests/clarity-trade-engine.test.ts @@ -0,0 +1,21 @@ + +import { describe, expect, it } from "vitest"; + +const accounts = simnet.getAccounts(); +const address1 = accounts.get("wallet_1")!; + +/* + The test below is an example. To learn more, read the testing documentation here: + https://docs.hiro.so/clarinet/feature-guides/test-contract-with-clarinet-sdk +*/ + +describe("example tests", () => { + it("ensures simnet is well initalised", () => { + expect(simnet.blockHeight).toBeDefined(); + }); + + // it("shows an example", () => { + // const { result } = simnet.callReadOnlyFn("counter", "get-counter", [], address1); + // expect(result).toBeUint(0); + // }); +}); diff --git a/stx-commodity-market/tsconfig.json b/stx-commodity-market/tsconfig.json new file mode 100644 index 0000000..1bdaf36 --- /dev/null +++ b/stx-commodity-market/tsconfig.json @@ -0,0 +1,26 @@ + +{ + "compilerOptions": { + "target": "ESNext", + "useDefineForClassFields": true, + "module": "ESNext", + "lib": ["ESNext"], + "skipLibCheck": true, + + "moduleResolution": "bundler", + "allowImportingTsExtensions": true, + "resolveJsonModule": true, + "isolatedModules": true, + "noEmit": true, + + "strict": true, + "noImplicitAny": true, + "noUnusedLocals": true, + "noUnusedParameters": true, + "noFallthroughCasesInSwitch": true + }, + "include": [ + "node_modules/@hirosystems/clarinet-sdk/vitest-helpers/src", + "tests" + ] +} diff --git a/stx-commodity-market/vitest.config.js b/stx-commodity-market/vitest.config.js new file mode 100644 index 0000000..c6a8506 --- /dev/null +++ b/stx-commodity-market/vitest.config.js @@ -0,0 +1,42 @@ + +/// + +import { defineConfig } from "vite"; +import { vitestSetupFilePath, getClarinetVitestsArgv } from "@hirosystems/clarinet-sdk/vitest"; + +/* + In this file, Vitest is configured so that it works seamlessly with Clarinet and the Simnet. + + The `vitest-environment-clarinet` will initialise the clarinet-sdk + and make the `simnet` object available globally in the test files. + + `vitestSetupFilePath` points to a file in the `@hirosystems/clarinet-sdk` package that does two things: + - run `before` hooks to initialize the simnet and `after` hooks to collect costs and coverage reports. + - load custom vitest matchers to work with Clarity values (such as `expect(...).toBeUint()`) + + The `getClarinetVitestsArgv()` will parse options passed to the command `vitest run --` + - vitest run -- --manifest ./Clarinet.toml # pass a custom path + - vitest run -- --coverage --costs # collect coverage and cost reports +*/ + +export default defineConfig({ + test: { + environment: "clarinet", // use vitest-environment-clarinet + pool: "forks", + poolOptions: { + threads: { singleThread: true }, + forks: { singleFork: true }, + }, + setupFiles: [ + vitestSetupFilePath, + // custom setup files can be added here + ], + environmentOptions: { + clarinet: { + ...getClarinetVitestsArgv(), + // add or override options + }, + }, + }, +}); +