挖矿-合约文档

3006字约10分钟

2024-05-22

这份合约是一个基于Solidity语言编写的智能合约,用于实现一个流动性挖矿平台,主要功能包括用户存入流动性、获取收益、以及管理产品等。以下是对合约的规范说明文档,旨在阐述其功能逻辑:

合约名称:LPFarm

合约功能:

  • 用户可以存入基金池质押 ETH 获得的 YT(ERC20 Token)来参与挖矿,并获得奖励。
  • 用户可以存入多个订单,每个质押订单单独计算。
  • 用户订单到期之后,收益继续产出,直到主动撤出质押资产。

主要变量:

  • TOKEN: 收益产出代币地址。
  • lpToken: LP Token的合约地址。
  • tokenPerBlock: 每块平均产出的代币数量。
  • proTotal: 产品总数。
  • startBlock: 开始挖矿的区块。
  • lastRewardBlock: 奖励最后更新的区块。
  • accTokenPerShare: 每股累计的代币奖励。
  • accDividendPerShare: 每股累计的分红奖励。
  • totalPower: 总算力。
  • totalDividend: 累计分红资金。

结构体:

  • Product: 定义产品周期时间和乘数。
  • Order: 定义订单的ID、参与金额、订单算力和订单时间。
  • UserInfo: 定义用户的算力、质押量、已获取奖励、待领取奖励、奖励债务等信息。

事件:

  • Deposit: 用户存入流动性时触发。
  • Withdraw: 用户提取收益时触发。

函数:

  1. 初始化函数 (initialize): 初始化合约的基本参数。
  2. 获取奖励代币地址 (getToken): 返回奖励代币的地址。
  3. 获取挖矿代币地址 (getLpToken): 返回挖矿代币的地址。
  4. 获取用户信息 (getUserInfo): 返回指定用户的详细信息。
  5. 获取加权奖励倍数 (getMultiplier): 根据区块范围计算奖励倍数。
  6. 查看pending的token奖励 (pendingToken): 查看用户待领取的奖励。
  7. 更新池子奖励变量 (updatePool): 更新总算力和每股累计奖励。
  8. 存入LP Token (deposit): 用户存入LP Token参与挖矿。
  9. 领取收益 (withdraw_withdraw): 用户领取收益。
  10. 领取本金 (harvestOrderPrincipal): 用户领取自己的本金。
  11. 安全转移TOKEN (safeTokenTransfer): 安全地将代币转移到用户地址。
  12. 设置出块奖励 (setTokenPerBlock): 管理员设置每块的奖励代币数量。
  13. 新增产品 (addPro): 管理员新增挖矿产品。
  14. 获取所有产品信息 (getAllProducts): 获取系统中所有产品的信息。
  15. 获取聚合信息 (argInfo): 获取用户的LP资产、授权额度、待领取收益等信息。
  16. 获取用户订单 (getUserOrders): 获取用户的当前订单和历史订单。
  17. 空投分红奖励 (intoFee): 管理员地址空投分红奖励。

安全和升级性:

  • 合约继承自 ManagerUpgradeablePausableUpgradeable,支持权限管理和升级功能。
  • 使用 SafeMath 库来防止整数溢出。
  • 使用 SafeERC20 库来安全地操作ERC20代币。

用户使用教程:LPFarm合约

简介

本教程旨在指导用户如何使用LPFarm智能合约进行流动性挖矿。LPFarm合约允许用户存入YT Token来获得奖励,并且提供了一系列的功能,包括存入、领取收益和本金等。

前提条件

  • 用户需要有一个支持以太坊的数字钱包,如MetaMask、TokenPoint。
  • 用户钱包中需要有足够的ETH和相应的YT Token。
  • 用户需要知道如何与智能合约交互。

步骤1:连接钱包

  1. 打开您选择的数字钱包应用。
  2. 确保您的钱包已连接到部署LPFarm合约的以太坊网络。

步骤2:授权LP Token

  1. 在LPFarm合约界面上找到“授权”按钮。
  2. 点击“授权”,并选择您想要授权的LP Token数量。
  3. 确认交易,等待网络确认。

步骤3:查看合约信息

  • 使用合约提供的getUserInfogetLpTokengetToken等函数来查看您的LP Token余额、奖励代币地址等信息。

步骤4:存入LP Token

  1. 在合约界面上找到“存入”功能。
  2. 输入您想要存入的LP Token数量。
  3. 选择一个产品ID(如果有多个产品)。
  4. 点击“存入”并确认交易。
  5. 等待网络确认,并查看您的存入状态。

步骤5:领取收益

  1. 在合约界面上找到“领取收益”功能。
  2. 点击“领取收益”,合约会自动处理并发送您应得的奖励到您的钱包。
  3. 确认交易,等待网络确认。

步骤6:领取本金

  1. 在合约界面上找到“领取本金”功能。
  2. 输入您想要领取的订单ID。
  3. 点击“领取本金”并确认交易。
  4. 等待网络确认,并查看您的本金领取状态。

步骤7:查看订单和收益

  • 使用getUserOrders函数来查看您的当前订单和历史订单。
  • 使用pendingToken函数来查看您的待领取奖励。

注意事项

  • 在进行任何交易之前,请确保您了解交易的详情和可能的费用。
  • 领取收益和本金之前,请确保合约有足够的奖励代币供应。
  • 如果合约被暂停,某些操作可能会受到限制。

常见问题

Q: 我如何知道我的授权额度? A: 您可以使用argInfo函数查看您的授权额度。

Q: 我可以同时存入多个产品吗? A: 是的,您可以存入多个产品。

Q: 我可以随时领取我的本金吗? A: 这取决于产品的具体规则,您可以在产品锁定期结束之后领取您的本金。

Q: 如果我有其他问题怎么办? A: 您可以联系合约的管理员或查看合约的官方文档获取更多帮助。

请在使用合约前仔细阅读本教程,并确保您理解了所有的步骤和注意事项。智能合约交互涉及到您的资产安全,请谨慎操作。

Interface

// 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;
}

ABI

{
  "_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": {}
}