推荐与返佣 (Referral & Rebates)
ZTDX 提供多层级的推荐激励计划,用户可以通过邀请新用户交易来获得返佣收益。推荐系统同时支持链下数据库和链上智能合约两种模式,确保数据透明可查。
概述
推荐系统特性
- ✅ 5级返佣制度:Bronze (10%) → Diamond (30%)
- ✅ 自动等级升级:根据推荐人数自动提升返佣比例
- ✅ 链上透明:所有数据可在区块链上查询验证
- ✅ EIP-712 签名:创建和绑定推荐码需要钱包签名
- ✅ 实时结算:每笔交易后立即计算返佣
等级体系
| 等级 | 名称 | 推荐人数 | 返佣比例 | 下一级要求 |
|---|---|---|---|---|
| 0 | Bronze | 0-9 | 10% | 10 人 |
| 1 | Silver | 10-49 | 15% | 50 人 |
| 2 | Gold | 50-99 | 20% | 100 人 |
| 3 | Platinum | 100-499 | 25% | 500 人 |
| 4 | Diamond | 500+ | 30% | 已达最高 |
创建推荐码
每个用户可以创建一个唯一的推荐码用于邀请其他用户。推荐码基于用户地址自动生成。
接口信息
- Method:
POST - Path:
/api/v1/referral/codes - Authentication: 需要 JWT Token
- Content-Type:
application/json
请求参数
| 参数 | 类型 | 必须 | 描述 |
|---|---|---|---|
| signature | string | 是 | EIP-712 签名(0x 开头) |
| timestamp | number | 是 | Unix 时间戳(秒) |
EIP-712 签名
创建推荐码的 typed data 结构:
{
"types": {
"CreateReferralCode": [
{ "name": "wallet", "type": "address" },
{ "name": "timestamp", "type": "uint256" }
]
},
"message": {
"wallet": "0x742d35cc6634c0532925a3b844bc9e7595f0beb",
"timestamp": "1704067200"
}
}
前端签名示例
async function createReferralCode() {
const wallet = await getConnectedWallet();
const timestamp = Math.floor(Date.now() / 1000);
const domain = {
name: "ZTDX",
version: "1",
chainId: 421614,
verifyingContract: vaultAddress
};
const types = {
CreateReferralCode: [
{ name: "wallet", type: "address" },
{ name: "timestamp", type: "uint256" }
]
};
const message = {
wallet: wallet.address.toLowerCase(),
timestamp: timestamp.toString()
};
const provider = new BrowserProvider(window.ethereum);
const signer = await provider.getSigner();
const signature = await signer.signTypedData(domain, types, message);
const response = await fetch('/api/v1/referral/codes', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${jwtToken}`
},
body: JSON.stringify({ signature, timestamp })
});
return response.json();
}
请求示例
{
"signature": "0xabcdef123456...",
"timestamp": 1704067200
}
响应示例
{
"code": "A1B2C3D4",
"rebate_rate": "0.10",
"tier": 0,
"valid_until": 1735689600,
"created_at": 1704067200000
}
响应字段说明
| 字段 | 类型 | 描述 |
|---|---|---|
| code | string | 推荐码(8位大写字母数字,基于地址生成) |
| rebate_rate | string | 当前返佣比例(Decimal 字符串) |
| tier | number | 当前等级(0-4) |
| valid_until | number | 有效期(秒级时间戳) |
| created_at | number | 创建时间(毫秒级时间戳) |
推荐码生成算法
推荐码基于用户地址的 SHA256 哈希生成,确保唯一性和可重现性:
// 后端算法(Rust)
fn generate_referral_code(address: &str) -> String {
let hash = sha256(address.as_bytes());
let code = hex::encode(hash).chars().take(8).collect();
code.to_uppercase()
}
这意味着:
- ✅ 相同地址总是生成相同的推荐码
- ✅ 推荐码不会重复
- ✅ 可通过地址反推验证推荐码
错误响应
| HTTP 状态码 | 错误码 | 描述 |
|---|---|---|
| 400 | CODE_ALREADY_EXISTS | 用户已有推荐码 |
| 400 | TIMESTAMP_EXPIRED | 时间戳已过期 |
| 400 | INVALID_SIGNATURE_FORMAT | 签名格式无效 |
| 401 | SIGNATURE_INVALID | EIP-712 签名验证失败 |
绑定推荐码
新用户可以绑定一个推荐码,成为他人的推荐用户。每个地址只能绑定一次,绑定后不可更改。
接口信息
- Method:
POST - Path:
/api/v1/referral/bind - Authentication: 需要 JWT Token
- Content-Type:
application/json
请求参数
| 参数 | 类型 | 必须 | 描述 |
|---|---|---|---|
| code | string | 是 | 推荐码(8位大写) |
| signature | string | 是 | EIP-712 签名 |
| timestamp | number | 是 | Unix 时间戳(秒) |
EIP-712 签名
绑定推荐码的 typed data 结构:
{
"types": {
"BindReferralCode": [
{ "name": "wallet", "type": "address" },
{ "name": "code", "type": "string" },
{ "name": "timestamp", "type": "uint256" }
]
},
"message": {
"wallet": "0x123...",
"code": "A1B2C3D4",
"timestamp": "1704067200"
}
}
请求示例
{
"code": "A1B2C3D4",
"signature": "0xabcdef123456...",
"timestamp": 1704067200
}
响应示例
{
"bound": true,
"referrer_address": "0x742d35cc6634c0532925a3b844bc9e7595f0beb",
"referrer_code": "A1B2C3D4",
"bonus": "10.00"
}
错误响应
| HTTP 状态码 | 错误码 | 描述 |
|---|---|---|
| 404 | CODE_NOT_FOUND | 推荐码不存在 |
| 400 | ALREADY_BOUND | 已绑定过推荐码 |
| 400 | SELF_REFERRAL | 不能绑 定自己的推荐码 |
| 400 | TIMESTAMP_EXPIRED | 时间戳已过期 |
| 401 | SIGNATURE_INVALID | EIP-712 签名验证失败 |
重要提醒
- 每个地址只能绑定一次推荐码
- 绑定后不可更改或解绑
- 不能使用自己的推荐码
推荐面板
获取用户的完整推荐数据dashboard,包括收益、等级、活动记录等。
接口信息
- Method:
GET - Path:
/api/v1/referral/dashboard - Authentication: 需要 JWT Token
响应示例
{
"referral_code": "A1B2C3D4",
"referral_count": 52,
"tier": {
"level": 2,
"name": "Gold",
"rebate_rate": "0.20",
"next_level_requirement": 100
},
"total_earned": "5250.50",
"pending_earnings": "450.25",
"claimed_earnings": "4800.25",
"recent_referrals": [
{
"user_address": "0x123...",
"created_at": 1704067200000,
"total_trading_volume": "15000.00"
}
],
"activities": [
{
"type": "commission",
"amount": "15.50",
"timestamp": 1704067200000,
"details": "Trading fee rebate from 0x123..."
},
{
"type": "referral",
"amount": "0",
"timestamp": 1704063600000,
"details": "New referral: 0x456..."
}
]
}
响应字段说明
| 字段 | 类型 | 描述 |
|---|---|---|
| referral_code | string | 用户的推荐码 |
| referral_count | number | 成功邀请的总用户数 |
| tier | object | 当前等级信息 |
| tier.level | number | 等级编号(0-4) |
| tier.name | string | 等级名称 |
| tier.rebate_rate | string | 返佣比例(Decimal 字符串) |
| tier.next_level_requirement | number | null | 下一级所需推荐人数 |
| total_earned | string | 累计总收益(Decimal 字符串) |
| pending_earnings | string | 待领取收益 |
| claimed_earnings | string | 已领取收益 |
| recent_referrals | array | 最近的推荐用户列表 |
| activities | array | 活动记录(佣金、推荐、领取等) |