跳到主要内容

声誉集成指南

Karma 是 Status Network 的灵魂绑定声誉代币。每个地址通过真实的网络参与来赚取 Karma,例如质押 SNT、桥接资产、提供流动性、使用应用或支付高级 Gas 费用。Karma 不能被购买、出售或转让。

本指南介绍如何读取 Karma 数据以及构建响应用户声誉层级的应用。

链上读取 Karma

Karma 状态分布在两个合约中:

  • Karma — 灵魂绑定 ERC-20 代币。balanceOf 返回扣除任何削减后的净余额。转账和授权始终会回退。
  • KarmaTiers — 将余额映射到层级。调用 getTierIdByKarmaBalance(balance) 然后 getTierById(tierId) 来解析用户的层级及其 txPerEpoch 配额。
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

/// @notice Minimal interface for the Karma soulbound token.
interface IKarma {
/// @notice Net Karma balance of `account` after slashing.
function balanceOf(address account) external view returns (uint256);

/// @notice Total amount slashed from `account` across all distributors.
function slashedAmountOf(address account) external view returns (uint256);
}

/// @notice Minimal interface for the KarmaTiers registry.
interface IKarmaTiers {
struct Tier {
uint256 minKarma;
uint256 maxKarma;
string name;
uint32 txPerEpoch; // gasless transactions allowed per epoch
}

/// @notice Returns the highest tier ID the given karma balance qualifies for.
function getTierIdByKarmaBalance(uint256 karmaBalance) external view returns (uint8);

/// @notice Returns the full Tier struct for a given tier ID.
function getTierById(uint8 tierId) external view returns (Tier memory);

/// @notice Returns the total number of configured tiers.
function getTierCount() external view returns (uint256);
}

contract KarmaGated {
IKarma public karma;
IKarmaTiers public karmaTiers;

constructor(address _karmaContract, address _karmaTiersContract) {
karma = IKarma(_karmaContract);
karmaTiers = IKarmaTiers(_karmaTiersContract);
}

/// @notice Resolves the tier ID for `user` from their current Karma balance.
function tierIdOf(address user) public view returns (uint8) {
return karmaTiers.getTierIdByKarmaBalance(karma.balanceOf(user));
}

modifier onlyTier(uint8 minTier) {
require(tierIdOf(msg.sender) >= minTier, "Karma tier too low");
_;
}

function premiumAction() external onlyTier(3) {
// Only users at tier 3 or above can call this
}

function getDiscount(address user) external view returns (uint256) {
uint8 tierId = tierIdOf(user);
// Higher tiers get bigger discounts
return uint256(tierId) * 5; // 0%, 5%, 10%, ...
}
}
合约地址

Karma 和 KarmaTiers 的测试网合约地址发布在合约地址页面。

可能的集成模式

功能门控

基于 Karma 层级解锁功能。链上读取 Karma 部分中的 onlyTier 修饰器在合约层面强制执行访问控制:

function accessPremiumContent() external onlyTier(3) {
// Only users at tier 3 or above can call this
}

动态定价

为高 Karma 用户提供折扣或更优惠的费率:

function calculateFee(address user, uint256 baseAmount) public view returns (uint256) {
uint8 tierId = karmaTiers.getTierIdByKarmaBalance(karma.balanceOf(user));
// Each tier gives 5% discount
uint256 discount = uint256(tierId) * 5;
return baseAmount * (100 - discount) / 100;
}

声誉展示

通过直接从 KarmaTiers 合约读取名称,在 UI 中将 Karma 层级显示为信任信号:

// 从链上获取层级名称 — 无需硬编码数组
const balance = await karma.balanceOf(userAddress);
const tierId = await karmaTiers.getTierIdByKarmaBalance(balance);
const tier = await karmaTiers.getTierById(tierId);

function KarmaBadge({ tierName }) {
return <span className="karma-badge">{tierName}</span>;
}

// 使用
<KarmaBadge tierName={tier.name} />

加权治理

在应用的治理中使用 Karma 进行投票加权:

function vote(uint256 proposalId, bool support) external {
uint256 weight = karma.balanceOf(msg.sender);
proposals[proposalId].votes += support ? int256(weight) : -int256(weight);
}

下一步