Vera3289/paystream-contracts
GitHub: Vera3289/paystream-contracts
基于 Stellar Soroban 的去中心化按秒薪水流式支付智能合约,实现雇主托管、实时提取和全链上审计。
Stars: 0 | Forks: 6
# PayStream 合约
[](https://github.com/Vera3289/paystream-contracts/actions/workflows/ci.yml)
[](LICENSE)
**PayStream** 的 Soroban 智能合约 —— 构建在 Stellar 区块链上的去中心化工资发放与薪水流式支付服务。
PayStream 允许雇主以实时、按秒的方式向员工流式支付薪水。员工无需等待每月的工资发放日,而是可以在工作期间持续赚取并随时提取工资 —— 全部在链上进行,无需信任且完全透明。
## 为什么选择 PayStream?
- **实时支付** —— 员工可以随时获取已赚取的工资,而不仅仅是在发薪日
- **无需信任的托管** —— 资金锁定在链上;雇主无法撤回已赚取的薪水
- **透明** —— 每一次流式支付、提取和取消都是不可篡改的链上事件
- **Stellar 原生** —— 构建在 Stellar 快速、低费用的基础设施之上,采用 Soroban 智能合约
- **灵活** —— 支持暂停、恢复、充值或取消流式支付;支持可选的硬性停止时间
- **多代币** —— 每个流式支付可以使用任何符合 [SEP-41](https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0041.md) 标准的代币;雇主和员工可以在不同的资产中运行并发的流式支付
## 项目结构
```
.
├── contracts
│ ├── stream # Core salary streaming and escrow contract
│ │ ├── src
│ │ │ ├── lib.rs # Stream entrypoints
│ │ │ ├── storage.rs # Persistence and claimable calculation
│ │ │ ├── events.rs # On-chain event publishing
│ │ │ ├── types.rs # Domain models and storage keys
│ │ │ └── test.rs # Contract tests
│ │ └── Cargo.toml
│ └── token # Fungible payment token contract
│ ├── src
│ │ ├── lib.rs
│ │ ├── storage.rs
│ │ ├── types.rs
│ │ └── test.rs
│ └── Cargo.toml
├── scripts
│ ├── build.sh
│ ├── deploy-local.sh
│ ├── deploy-testnet.sh
│ └── init-testnet.sh
├── Cargo.toml
├── Makefile
├── CONTRIBUTING.md
├── SECURITY.md
└── README.md
```
## 快速开始
### 前置条件
- [Rust](https://rustup.rs/)(最新稳定版)
- [Stellar CLI](https://developers.stellar.org/docs/tools/developer-tools/cli/stellar-cli)
```
git clone https://github.com/Vera3289/paystream-contracts.git
cd paystream-contracts
rustup target add wasm32-unknown-unknown
```
### 构建
```
make build
# 或: stellar contract build
```
### 测试
```
make test
# 或: cargo test
```
### 格式化与代码检查
```
make fmt-check
make lint
```
### Docker(无需本地安装 Rust/Stellar CLI)
完全在 Docker 内部进行构建和测试 —— 无需在本地安装 Rust 或 Stellar CLI。
**运行测试:**
```
docker compose run --rm test
```
**仅构建合约:**
```
docker compose run --rm build stellar contract build
```
`cargo-cache` 卷会在多次运行之间持久化 Cargo 注册表,从而使后续的构建速度更快。
## 流式支付合约参考
### 函数
| 函数 | 调用者 | 描述 |
|---|---|---|
| `initialize(admin)` | 管理员 | 设置合约管理员 |
| `create_stream(employer, employee, token, deposit, rate_per_second, stop_time)` | 雇主 | 创建流式支付,锁定存款 |
| `create_streams_batch(employer, params)` | 雇主 | 原子化地创建多个流式支付;全部成功或全部回滚 |
| `withdraw(employee, stream_id)` | 员工 | 提取所有可领取的收入 |
| `top_up(employer, stream_id, amount)` | 雇主 | 向活跃的流式支付中添加更多资金 |
| `pause_stream(employer, stream_id)` | 雇主 | 暂停计费 |
| `resume_stream(employer, stream_id)` | 雇主 | 恢复计费 |
| `cancel_stream(employer, stream_id)` | 雇主 | 支付员工应得份额,退还剩余资金 |
| `get_stream(stream_id)` | 任何人 | 读取流式支付状态 |
| `claimable(stream_id)` | 任何人 | 查询当前可提取的金额 |
| `stream_count()` | 任何人 | 已创建的流式支付总数 |
### 批量创建与单独创建流式支付 —— 费用对比
| 方式 | 交易数 | 大约费用 |
|---|---|---|
| N 次单独的 `create_stream` 调用 | N | N × 基础费用 |
| 一次 `create_streams_batch` 调用 | 1 | 1 × 基础费用 + 每个流式支付的资源开销 |
当 N ≥ 2 时,`create_streams_batch` 会更便宜,因为 Stellar 会对每笔交易收取一次基础费用。每个流式支付的资源开销呈线性增长,但远远小于所节省的按交易收取的基础费用。
### 流式支付状态生命周期
```
Active → Paused → Active
Active → Cancelled
Active → Exhausted (deposit fully streamed)
```
### 可领取金额计算
```
claimable = min(
(now - last_withdraw_time) * rate_per_second,
deposit - withdrawn
)
```
如果设置了 `stop_time`,时间将以此值为上限。暂停的时间将被排除在外。
## 部署
### 测试网
```
./scripts/build.sh
./scripts/deploy-testnet.sh
export STELLAR_ADMIN_ADDRESS=
export TOKEN_CONTRACT_ID=
export STREAM_CONTRACT_ID=
./scripts/init-testnet.sh
```
### 本地
```
make deploy-local
```
## 技术栈
| 层级 | 技术 |
|---|---|
| 区块链 | Stellar (Soroban) |
| 编程语言 | Rust |
| SDK | Soroban SDK v22.0.0 |
| CI/CD | GitHub Actions |
## 贡献
参见 [CONTRIBUTING.md](CONTRIBUTING.md)。
## 安全
参见 [SECURITY.md](SECURITY.md)。请将漏洞报告发送至 `security@paystream.example` —— 请勿通过公开的 issues 提交。
## 许可证
[Apache 2.0](LICENSE)
Built with ❤️ on Stellar
标签:Apache 2.0, DeFi, DNS解析, FinTech, Rust, SEP-41, Soroban, Web3, 代币经济学, 分布式账本, 加密货币薪酬, 区块链, 去中心化金融, 可视化界面, 实时支付, 开源项目, 恒星链, 无信任机制, 智能合约, 流支付, 网络流量审计, 薪资流, 请求拦截, 资金托管, 连续支付, 透明度, 通知系统, 金融科技, 链上审计