文件 1 的 2:IReferral.sol
pragma solidity 0.8.7;
interface IReferral {
struct Tier {
uint256 feeDiscountPct;
uint256 refRebatePct;
}
event SetGov(address gov);
event SetHandler(address handler, bool isActive);
event SetTraderReferralCode(address account, bytes32 code);
event SetTier(uint256 tierId, uint256 feeDiscountPct, uint256 refRebatePct);
event SetReferrerTier(address referrer, uint256 tierId);
event SetReferrerDiscountShare(address referrer, uint256 discountShare);
event RegisterCode(address account, bytes32 code);
event SetCodeOwner(address oldOwner, address newOwner, bytes32 code);
event SetPendingCodeOwner(address account, address newAccount, bytes32 code);
event GovSetCodeOwner(bytes32 code, address newAccount);
function codeOwners(bytes32 _code) external view returns (address);
function traderReferralCodes(address _account) external view returns (bytes32);
function referrerTiers(address _account) external view returns (uint256);
function getTraderReferralInfo(address _account) external view returns (bytes32, address);
function setTraderReferralCode(address _account, bytes32 _code) external;
function setTier(uint256 _tierId, uint256 _totalRebate, uint256 _discountShare) external;
function setReferrerTier(address _referrer, uint256 _tierId) external;
function govSetCodeOwner(bytes32 _code, address _newAccount) external;
function traderReferralDiscount(address _account, uint _feeBips) external view returns (uint, address, uint);
}
文件 2 的 2:Referral.sol
pragma solidity 0.8.7;
import "./interfaces/IReferral.sol";
contract Referral is IReferral {
uint256 private constant _BASIS_POINTS = 10000;
uint private constant _DEFAULT_TIER_ID = 1;
address public gov;
address public requestedGov;
mapping(address => uint256) public override referrerTiers;
mapping(uint256 => Tier) public tiers;
mapping(address => bool) public isHandler;
mapping(bytes32 => address) public override codeOwners;
mapping(bytes32 => address) public pendingCodeOwners;
mapping(address => bytes32) public codes;
mapping(address => bytes32) public override traderReferralCodes;
modifier onlyHandler() {
require(isHandler[msg.sender], "ReferralStorage: forbidden");
_;
}
modifier onlyGov() {
require(msg.sender == gov, "Governable: forbidden");
_;
}
constructor() {
gov = msg.sender;
}
function requestGov(address _gov) external onlyGov {
require(_gov != address(0));
requestedGov = _gov;
}
function setGov(address _gov) external onlyGov {
require(_gov != address(0));
require(_gov == requestedGov);
gov = _gov;
emit SetGov(_gov);
}
function setHandler(address _handler, bool _isActive) external onlyGov {
isHandler[_handler] = _isActive;
emit SetHandler(_handler, _isActive);
}
function setTier(uint256 _tierId, uint256 _feeDiscountPct, uint256 _refRebatePct) external override onlyGov {
require(_feeDiscountPct <= _BASIS_POINTS, "ReferralStorage: invalid totalRebate");
require(_refRebatePct <= _BASIS_POINTS, "ReferralStorage: invalid discountShare");
Tier memory tier = tiers[_tierId];
tier.feeDiscountPct = _feeDiscountPct;
tier.refRebatePct = _refRebatePct;
tiers[_tierId] = tier;
emit SetTier(_tierId, _feeDiscountPct, _refRebatePct);
}
function setReferrerTier(address _referrer, uint256 _tierId) external override onlyGov {
referrerTiers[_referrer] = _tierId;
emit SetReferrerTier(_referrer, _tierId);
}
function setTraderReferralCode(address _account, bytes32 _code) external override onlyHandler {
_setTraderReferralCode(_account, _code);
}
function setTraderReferralCodeByUser(bytes32 _code) external {
_setTraderReferralCode(msg.sender, _code);
}
function registerCode(bytes32 _code) external {
require(_code != bytes32(0), "ReferralStorage: invalid _code");
require(codeOwners[_code] == address(0), "ReferralStorage: code already exists");
require(codes[msg.sender] == bytes32(0),"ReferralStorage: referrer already registered");
codeOwners[_code] = msg.sender;
codes[msg.sender] = _code;
referrerTiers[msg.sender] = _DEFAULT_TIER_ID;
emit RegisterCode(msg.sender, _code);
}
function setPendingCodeOwnershipTransfer(bytes32 _code, address _newAccount) external {
require(_code != bytes32(0), "ReferralStorage: invalid _code");
address account = codeOwners[_code];
require(msg.sender == account, "ReferralStorage: forbidden");
pendingCodeOwners[_code] = _newAccount;
emit SetPendingCodeOwner(msg.sender, _newAccount, _code);
}
function acceptCodeOwnership(bytes32 _code) external {
address oldOwner = codeOwners[_code];
require(pendingCodeOwners[_code] == msg.sender, "ReferralStorage: forbidden");
delete codeOwners[codes[msg.sender]];
delete pendingCodeOwners[codes[msg.sender]];
codeOwners[_code] = msg.sender;
codes[msg.sender] = _code;
referrerTiers[msg.sender] = referrerTiers[oldOwner];
emit SetCodeOwner(oldOwner, msg.sender, _code);
delete codes[oldOwner];
delete pendingCodeOwners[_code];
delete referrerTiers[oldOwner];
}
function govSetCodeOwner(bytes32 _code, address _newAccount) external override onlyGov {
require(_code != bytes32(0), "ReferralStorage: invalid _code");
address account = codeOwners[_code];
delete codes[account];
delete codeOwners[codes[_newAccount]];
codeOwners[_code] = _newAccount;
codes[_newAccount] = _code;
referrerTiers[_newAccount] = referrerTiers[account];
delete referrerTiers[account];
emit GovSetCodeOwner(_code, _newAccount);
}
function traderReferralDiscount(
address _account,
uint _fee
) external view override returns (uint traderFeesPostDiscount, address referrer, uint rebateShare) {
(, referrer) = getTraderReferralInfo(_account);
uint _tierId = referrerTiers[referrer];
traderFeesPostDiscount = _fee - (_fee * tiers[_tierId].feeDiscountPct) / _BASIS_POINTS;
rebateShare = (traderFeesPostDiscount * tiers[_tierId].refRebatePct) / _BASIS_POINTS;
}
function getTraderReferralInfo(address _account) public view override returns (bytes32, address) {
bytes32 code = traderReferralCodes[_account];
address referrer;
if (code != bytes32(0)) {
referrer = codeOwners[code];
}
return (code, referrer);
}
function _setTraderReferralCode(address _account, bytes32 _code) private {
traderReferralCodes[_account] = _code;
emit SetTraderReferralCode(_account, _code);
}
}
{
"compilationTarget": {
"src/Referral.sol": "Referral"
},
"evmVersion": "london",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": true,
"runs": 200
},
"remappings": [
":@chainlink/=lib/chainlink/",
":@openzeppelin/contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/contracts/",
":@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
":chainlink/=lib/chainlink/",
":ds-test/=lib/forge-std/lib/ds-test/src/",
":erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
":forge-std/=lib/forge-std/src/",
":openzeppelin-contracts-upgradeable/=lib/openzeppelin-contracts-upgradeable/",
":openzeppelin-contracts/=lib/openzeppelin-contracts/",
":openzeppelin/=lib/openzeppelin-contracts/contracts/",
":pyth-sdk-solidity/=lib/pyth-sdk-solidity/"
]
}
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes32","name":"code","type":"bytes32"},{"indexed":false,"internalType":"address","name":"newAccount","type":"address"}],"name":"GovSetCodeOwner","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"},{"indexed":false,"internalType":"bytes32","name":"code","type":"bytes32"}],"name":"RegisterCode","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"oldOwner","type":"address"},{"indexed":false,"internalType":"address","name":"newOwner","type":"address"},{"indexed":false,"internalType":"bytes32","name":"code","type":"bytes32"}],"name":"SetCodeOwner","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"gov","type":"address"}],"name":"SetGov","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"handler","type":"address"},{"indexed":false,"internalType":"bool","name":"isActive","type":"bool"}],"name":"SetHandler","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"},{"indexed":false,"internalType":"address","name":"newAccount","type":"address"},{"indexed":false,"internalType":"bytes32","name":"code","type":"bytes32"}],"name":"SetPendingCodeOwner","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"referrer","type":"address"},{"indexed":false,"internalType":"uint256","name":"discountShare","type":"uint256"}],"name":"SetReferrerDiscountShare","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"referrer","type":"address"},{"indexed":false,"internalType":"uint256","name":"tierId","type":"uint256"}],"name":"SetReferrerTier","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint256","name":"tierId","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"feeDiscountPct","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"refRebatePct","type":"uint256"}],"name":"SetTier","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"address","name":"account","type":"address"},{"indexed":false,"internalType":"bytes32","name":"code","type":"bytes32"}],"name":"SetTraderReferralCode","type":"event"},{"inputs":[{"internalType":"bytes32","name":"_code","type":"bytes32"}],"name":"acceptCodeOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"codeOwners","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"codes","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"}],"name":"getTraderReferralInfo","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"},{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"gov","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_code","type":"bytes32"},{"internalType":"address","name":"_newAccount","type":"address"}],"name":"govSetCodeOwner","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"isHandler","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"name":"pendingCodeOwners","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"referrerTiers","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_code","type":"bytes32"}],"name":"registerCode","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_gov","type":"address"}],"name":"requestGov","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"requestedGov","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_gov","type":"address"}],"name":"setGov","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_handler","type":"address"},{"internalType":"bool","name":"_isActive","type":"bool"}],"name":"setHandler","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_code","type":"bytes32"},{"internalType":"address","name":"_newAccount","type":"address"}],"name":"setPendingCodeOwnershipTransfer","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_referrer","type":"address"},{"internalType":"uint256","name":"_tierId","type":"uint256"}],"name":"setReferrerTier","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"_tierId","type":"uint256"},{"internalType":"uint256","name":"_feeDiscountPct","type":"uint256"},{"internalType":"uint256","name":"_refRebatePct","type":"uint256"}],"name":"setTier","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"},{"internalType":"bytes32","name":"_code","type":"bytes32"}],"name":"setTraderReferralCode","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes32","name":"_code","type":"bytes32"}],"name":"setTraderReferralCodeByUser","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"tiers","outputs":[{"internalType":"uint256","name":"feeDiscountPct","type":"uint256"},{"internalType":"uint256","name":"refRebatePct","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"traderReferralCodes","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_account","type":"address"},{"internalType":"uint256","name":"_fee","type":"uint256"}],"name":"traderReferralDiscount","outputs":[{"internalType":"uint256","name":"traderFeesPostDiscount","type":"uint256"},{"internalType":"address","name":"referrer","type":"address"},{"internalType":"uint256","name":"rebateShare","type":"uint256"}],"stateMutability":"view","type":"function"}]