3006字约10分钟
2024-05-22
这份合约是一个基于Solidity语言编写的智能合约,用于实现一个流动性挖矿平台,主要功能包括用户存入流动性、获取收益、以及管理产品等。以下是对合约的规范说明文档,旨在阐述其功能逻辑:
TOKEN
: 收益产出代币地址。lpToken
: LP Token的合约地址。tokenPerBlock
: 每块平均产出的代币数量。proTotal
: 产品总数。startBlock
: 开始挖矿的区块。lastRewardBlock
: 奖励最后更新的区块。accTokenPerShare
: 每股累计的代币奖励。accDividendPerShare
: 每股累计的分红奖励。totalPower
: 总算力。totalDividend
: 累计分红资金。Product
: 定义产品周期时间和乘数。Order
: 定义订单的ID、参与金额、订单算力和订单时间。UserInfo
: 定义用户的算力、质押量、已获取奖励、待领取奖励、奖励债务等信息。Deposit
: 用户存入流动性时触发。Withdraw
: 用户提取收益时触发。initialize
): 初始化合约的基本参数。getToken
): 返回奖励代币的地址。getLpToken
): 返回挖矿代币的地址。getUserInfo
): 返回指定用户的详细信息。getMultiplier
): 根据区块范围计算奖励倍数。pendingToken
): 查看用户待领取的奖励。updatePool
): 更新总算力和每股累计奖励。deposit
): 用户存入LP Token参与挖矿。withdraw
和 _withdraw
): 用户领取收益。harvestOrderPrincipal
): 用户领取自己的本金。safeTokenTransfer
): 安全地将代币转移到用户地址。setTokenPerBlock
): 管理员设置每块的奖励代币数量。addPro
): 管理员新增挖矿产品。getAllProducts
): 获取系统中所有产品的信息。argInfo
): 获取用户的LP资产、授权额度、待领取收益等信息。getUserOrders
): 获取用户的当前订单和历史订单。intoFee
): 管理员地址空投分红奖励。ManagerUpgradeable
和 PausableUpgradeable
,支持权限管理和升级功能。SafeMath
库来防止整数溢出。SafeERC20
库来安全地操作ERC20代币。本教程旨在指导用户如何使用LPFarm智能合约进行流动性挖矿。LPFarm合约允许用户存入YT Token来获得奖励,并且提供了一系列的功能,包括存入、领取收益和本金等。
getUserInfo
、getLpToken
、getToken
等函数来查看您的LP Token余额、奖励代币地址等信息。getUserOrders
函数来查看您的当前订单和历史订单。pendingToken
函数来查看您的待领取奖励。Q: 我如何知道我的授权额度? A: 您可以使用argInfo
函数查看您的授权额度。
Q: 我可以同时存入多个产品吗? A: 是的,您可以存入多个产品。
Q: 我可以随时领取我的本金吗? A: 这取决于产品的具体规则,您可以在产品锁定期结束之后领取您的本金。
Q: 如果我有其他问题怎么办? A: 您可以联系合约的管理员或查看合约的官方文档获取更多帮助。
请在使用合约前仔细阅读本教程,并确保您理解了所有的步骤和注意事项。智能合约交互涉及到您的资产安全,请谨慎操作。
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.12;
pragma experimental ABIEncoderV2;
interface ILPFarm {
// 产品信息结构体
struct Product {
uint256 cycle; // 周期时间戳
uint256 multiplier; // 乘数 1=1/1000
}
// 订单信息结构体
struct Order {
uint256 id; // id
uint256 amount; // 参与金额
uint256 power; // 订单算力
uint256 orderTime; // 订单时间
}
// 用户信息结构体
struct UserInfo {
uint256 power; // 用户算力
uint256 amount; // 用户质押量
uint256 received; // 用户已经获取的奖励
uint256 earnings; // 用户待领取的奖励
uint256 rewardDebt; // 相当于之前用户累计提取奖励数
uint256 rewardDividendDebt; // 相当于之前用户累计提取奖励数
Order[] orders; // 用户当前订单列表
Order[] history; // 用户历史订单列表
}
// 存入事件
event Deposit(address indexed user, uint256 indexed pid, uint256 amount);
// 提现事件
event Withdraw(address indexed user, uint256 poolId, uint256 amount);
// 初始化函数
function initialize(
address _token,
address _lpToken,
uint256 _tokenPerBlock,
uint256 _startBlock
) external;
// 获取奖励代币地址
function getToken() external view returns (address);
// 获取挖矿代币地址
function getLpToken() external view returns (address);
// 获取奖励开始区块
function getStartBlock() external view returns (uint256);
// 奖励最后更新区块
function lastRewardBlock() external view returns (uint256);
// 每股累计算力
function accTokenPerShare() external view returns (uint256);
// 每股累计算力-分红
function accDividendPerShare() external view returns (uint256);
// 分红最后更新区块
function lastDividendRewardBlock() external view returns (uint256);
//总算力
function totalPower() external view returns (uint256);
//累计分红资金,只做计数,无用
function totalDividend() external view returns (uint256);
// 每个区块的产出的token数量
function getTokenPerBlock() external view returns (uint256);
// 获取用户信息
function getUserInfo(address _user) external view returns (UserInfo memory);
// 获取加权奖励倍数
function getMultiplier(
uint256 _from,
uint256 _to
) external pure returns (uint256);
// 查看pending的token奖励
// 0:矿池1 1:矿池2
function pendingToken(
address _user
) external view returns (uint256, uint256);
// 某个池子的奖励变量更新,更新算力
function updatePool() external;
/**
@dev 将lp令牌存入pool进行收益
@param _pid 产品id
@param _amount 参与 yt金额
*/
function deposit(uint256 _pid, uint256 _amount) external;
/**
@dev 用户领取自己的收益
@param _poolId 矿池=1 矿池2=2
*/
function withdraw(uint256 _poolId) external;
/**
@dev 用户领取自己的本金
@param _orderId 指定订单Id,需要注意,用户领取之后,订单会到历史订单
*/
function harvestOrderPrincipal(uint256 _orderId) external;
// 设置出快奖励
function setTokenPerBlock(uint256 _tokenPerBlock) external;
// 暂停合约
function pause() external;
// 恢复合约
function unpause() external;
// 只有管理员可以调用的新增产品
function addPro(uint256 _id, uint256 _cycle, uint256 _multiplier) external;
// 获取系统所有产品
function getAllProducts()
external
view
returns (Product[] memory _products);
/**
@dev 获取聚合信息
@param _user 用户地址
@return _balance 用户lp资产
@return _allowance 用户对farm合约的授权额度
@return _pending1 farm1收益
@return _penfing2 farm2收益
@return _userPower 用户算力
@return _userAmount 用户质押金额
@return _totalPower 矿池总算力
@return _totalAmount 矿池总质押量
*/
function argInfo(
address _user
)
external
view
returns (
uint256 _balance,
uint256 _allowance,
uint256 _pending1,
uint256 _penfing2,
uint256 _userPower,
uint256 _userAmount,
uint256 _totalPower,
uint256 _totalAmount
);
/**
@dev 获取用户的订单
@param _user 用户地址
@return _orders 当前订单
@return _history 历史订单
*/
function getUserOrders(
address _user
) external view returns (Order[] memory _orders, Order[] memory _history);
/**
@dev 管理员地址,空投分红奖励,外部注入奖励,直接分配
- 这个收益实时分配
*/
function intoFee(uint256 _amount) external;
}
{
"_format": "hh-sol-artifact-1",
"contractName": "ILPFarm",
"sourceName": "contracts/interface/ILPFarm.sol",
"abi": [
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "user",
"type": "address"
},
{
"indexed": true,
"internalType": "uint256",
"name": "pid",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "Deposit",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "user",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "poolId",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "amount",
"type": "uint256"
}
],
"name": "Withdraw",
"type": "event"
},
{
"inputs": [],
"name": "accDividendPerShare",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "accTokenPerShare",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_id",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_cycle",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_multiplier",
"type": "uint256"
}
],
"name": "addPro",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_user",
"type": "address"
}
],
"name": "argInfo",
"outputs": [
{
"internalType": "uint256",
"name": "_balance",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_allowance",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_pending1",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_penfing2",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_userPower",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_userAmount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_totalPower",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_totalAmount",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_pid",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_amount",
"type": "uint256"
}
],
"name": "deposit",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "getAllProducts",
"outputs": [
{
"components": [
{
"internalType": "uint256",
"name": "cycle",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "multiplier",
"type": "uint256"
}
],
"internalType": "struct ILPFarm.Product[]",
"name": "_products",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getLpToken",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_from",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_to",
"type": "uint256"
}
],
"name": "getMultiplier",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "pure",
"type": "function"
},
{
"inputs": [],
"name": "getStartBlock",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getToken",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getTokenPerBlock",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_user",
"type": "address"
}
],
"name": "getUserInfo",
"outputs": [
{
"components": [
{
"internalType": "uint256",
"name": "power",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "received",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "earnings",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "rewardDebt",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "rewardDividendDebt",
"type": "uint256"
},
{
"components": [
{
"internalType": "uint256",
"name": "id",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "power",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "orderTime",
"type": "uint256"
}
],
"internalType": "struct ILPFarm.Order[]",
"name": "orders",
"type": "tuple[]"
},
{
"components": [
{
"internalType": "uint256",
"name": "id",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "power",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "orderTime",
"type": "uint256"
}
],
"internalType": "struct ILPFarm.Order[]",
"name": "history",
"type": "tuple[]"
}
],
"internalType": "struct ILPFarm.UserInfo",
"name": "",
"type": "tuple"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_user",
"type": "address"
}
],
"name": "getUserOrders",
"outputs": [
{
"components": [
{
"internalType": "uint256",
"name": "id",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "power",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "orderTime",
"type": "uint256"
}
],
"internalType": "struct ILPFarm.Order[]",
"name": "_orders",
"type": "tuple[]"
},
{
"components": [
{
"internalType": "uint256",
"name": "id",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "power",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "orderTime",
"type": "uint256"
}
],
"internalType": "struct ILPFarm.Order[]",
"name": "_history",
"type": "tuple[]"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_orderId",
"type": "uint256"
}
],
"name": "harvestOrderPrincipal",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_token",
"type": "address"
},
{
"internalType": "address",
"name": "_lpToken",
"type": "address"
},
{
"internalType": "uint256",
"name": "_tokenPerBlock",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "_startBlock",
"type": "uint256"
}
],
"name": "initialize",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_amount",
"type": "uint256"
}
],
"name": "intoFee",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "lastDividendRewardBlock",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "lastRewardBlock",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "pause",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "_user",
"type": "address"
}
],
"name": "pendingToken",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_tokenPerBlock",
"type": "uint256"
}
],
"name": "setTokenPerBlock",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "totalDividend",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "totalPower",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "unpause",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "updatePool",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_poolId",
"type": "uint256"
}
],
"name": "withdraw",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
],
"bytecode": "0x",
"deployedBytecode": "0x",
"linkReferences": {},
"deployedLinkReferences": {}
}