Skip to content

feat: add protocol fee mechanism#4

Open
devSoniia wants to merge 19 commits into
mainfrom
feat/protocol-fee-125
Open

feat: add protocol fee mechanism#4
devSoniia wants to merge 19 commits into
mainfrom
feat/protocol-fee-125

Conversation

@devSoniia
Copy link
Copy Markdown
Owner

Summary

Implements an optional protocol fee collected on withdrawals, as specified in Vera3289#125.

Changes

contracts/stream/src/types.rs

  • Added FeeBps and FeeRecipient variants to DataKey
  • Added PendingAdmin to DataKey (was missing; used by storage helpers)
  • Added ERR_FEE_TOO_HIGH error constant (E011)

contracts/stream/src/storage.rs

  • Added get_fee_bps / set_fee_bps — fee in basis points, stored in instance storage
  • Added get_fee_recipient / set_fee_recipient — fee destination address

contracts/stream/src/lib.rs

  • Added set_protocol_fee(admin, nonce, fee_bps, fee_recipient) — admin-only, nonce-protected
  • Updated withdraw to deduct fee before transferring to employee:
    fee = claimable * fee_bps / 10_000 (integer division, rounds down)
  • Fee is skipped entirely when fee_bps == 0 or no recipient is set

contracts/stream/src/test.rs

  • Fixed pre-existing broken use statement and missing imports
  • Added 5 protocol fee tests:
    • test_withdraw_no_fee_by_default — 0 fee → employee gets full amount
    • test_withdraw_with_fee_deducted — 1% fee → employee gets 99%
    • test_fee_disabled_when_zero — fee can be re-set to 0
    • test_set_protocol_fee_above_max_rejected — E011 on fee_bps > 100
    • test_set_protocol_fee_non_admin_rejected — non-admin rejected
    • test_fee_rounding — 0.5% fee rounds down correctly

Acceptance Criteria

  • Fee configurable by admin (0–1%, i.e. 0–100 bps)
  • Fee deducted from withdrawal amount
  • Fee recipient address configurable
  • Tests verify correct fee calculation
  • Fee can be set to 0 (disabled)

Closes Vera3289#125

Gloriachinedu and others added 19 commits April 25, 2026 04:36
…289#63)

- Add ERR_SAME_PARTY (E010) constant to types.rs for consistent error codes
- Use ERR_SAME_PARTY in validate_create_stream() instead of inline string
- Update test to match new error code prefix
- Pre-deployment checklist (tests, keys, audit, XLM balance)
- Step-by-step deploy commands for token and stream contracts
- Post-deploy verification steps
- Rollback procedure: pause+upgrade or revert to previous WASM
- Connect wallet via Freighter
- Create stream, withdraw, query state with TypeScript examples
- Error handling with PayStream error codes
- examples/javascript/stream.js — create stream, query state (Node.js)
- examples/python/stream.py — create stream, query state (stellar-sdk)
- examples/rust/stream.rs — create stream, query claimable (off-chain client)
- examples/README.md — setup instructions and links
- README.md — link examples/ and docs/integration/frontend.md
Closes Vera3289#101 — TypeScript SDK (sdk/ package)
Closes Vera3289#102 — Freighter wallet integration
Closes Vera3289#103 — Demo React app
Closes Vera3289#104 — pollClaimable utility

- sdk/: PayStreamClient wrapping all 10 contract functions with full
  TypeScript types; read-only calls via simulateTransaction, mutating
  calls return unsigned XDR for caller to sign and submit
- sdk/src/freighter.ts: connectFreighter, getFreighterPublicKey,
  freighterSignTransaction, isFreighterConnected helpers; throws
  FreighterNotInstalledError with install link when extension absent
- sdk/src/poll.ts: pollClaimable(client, streamId, intervalMs, cb)
  polls claimable() at a configurable interval, stops on unsubscribe,
  handles network errors gracefully
- demo/: Vite + React app connecting to testnet; connect wallet,
  create stream, load stream by ID, live claimable balance (5 s poll),
  withdraw button for employee
feat: TypeScript SDK, Freighter wallet, pollClaimable, and demo app
…ema-docs

Issue Vera3289#87: add event schema reference documentation
…-audit

Issue Vera3289#75: Fund-lock audit and recovery regression test
…ce-docs

Issue Vera3289#97: add performance benchmark documentation
feat: add SDK usage examples in JS, Python, and Rust (Vera3289#95)
…ntegration

docs: add frontend integration guide for JS/TS (Vera3289#91)
…-employee

fix: reject stream creation when employer == employee (Vera3289#63)
- Add FeeBps and FeeRecipient variants to DataKey enum
- Add PendingAdmin to DataKey (was missing, used by storage.rs)
- Add get/set_fee_bps and get/set_fee_recipient storage helpers
- Add set_protocol_fee admin function (0-100 bps, E011 if exceeded)
- Deduct fee from withdrawal amount; send to fee_recipient
- Fee of 0 disables the mechanism entirely
- Fix broken use statement in test.rs and add missing imports
- Add 5 tests: no fee default, 1% fee deduction, disable fee,
  above-max rejected (E011), non-admin rejected, 0.5% rounding

Closes Vera3289#125
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add protocol fee mechanism

5 participants