背景
挖矿服务是SUN.io平台数字金融衍生品之一,为加密数字资产持有用户提供稳定、安全、可靠的理财产品申购和赎回服务。治理挖矿是SUN.io挖矿服务重要组成部分。如下图所示,用户奖励分成与用户持有的veSun Token,池子投票权重紧密相关,且用户可以随时赎回相关资产。
原理简介
用户在治理矿池质押后,会生成相应的虚拟质押金额:
对应合约代码
def _checkpoint(addr: address):
Controller(_controller).checkpoint_gauge(convert(self.controller_key, bytes32))
_working_balance: uint256 = self.working_balances[addr]
_working_supply: uint256 = self.working_supply
if block.timestamp > _period_time:
prev_week_time: uint256 = _period_time
week_time: uint256 = min((_period_time + WEEK) / WEEK * WEEK, block.timestamp)
for i in range(500):
dt: uint256 = week_time - prev_week_time
w: uint256 = Controller(_controller).gauge_relative_weight(convert(self.controller_key, bytes32), prev_week_time / WEEK * WEEK)
if _working_supply > 0:
if prev_future_epoch >= prev_week_time and prev_future_epoch < week_time:
_integrate_inv_supply += rate * w * (prev_future_epoch - prev_week_time) / _working_supply
rate = new_rate
_integrate_inv_supply += rate * w * (week_time - prev_future_epoch) / _working_supply
else:
_integrate_inv_supply += rate * w * dt / _working_supply
if week_time == block.timestamp:
break
prev_week_time = week_time
week_time = min(week_time + WEEK, block.timestamp)
_period += 1
self.period = _period
self.period_timestamp[_period] = block.timestamp
self.integrate_inv_supply[_period] = _integrate_inv_supply
self.integrate_fraction[addr] += _working_balance * (_integrate_inv_supply - self.integrate_inv_supply_of[addr]) / 10 ** 18
self.integrate_inv_supply_of[addr] = _integrate_inv_supply
self.integrate_checkpoint_of[addr] = block.timestamp
矿池
Old 3pool farm
主网合约地址:TCpu3GnK6PPZV9ama85mRP97YqRuVXdcSd
SUN-TRX farm
主网合约地址:TAkrcKsS5FW9f3ZfzvWy6Zvsz9uEjUxPoV
Old USDC farm
主网合约地址:TJ3Qnm9VhzJzhcxk49ZpHBRatD1bsD4xYq
USDT-SUN V2 farm
主网合约地址:TPx8BXTgiB1tiL9A9BZuz67bZBvFGYdJS8
BTT-USDT V2 farm
主网合约地址:TEULJy4MJeRUTMPGTTvsBkmDPM4PEsMBTw
USDT-TRX farm
主网合约地址:TSRhvs1R2hoNf5PuMG4nUrDa4XVBqBaKgF
USDJ-TRX farm
主网合约地址:TGJ8LcijTaLmzBhqtp2jWQ1DRCpCWPAmD1
TUSD-TRX farm
主网合约地址:TBPEX4Bu3rc8RAvv3qTBFVvSVAFNttinqe
USDC-TRX farm
主网合约地址:TDsuMrbQ8wHrM8cKpxFczBbaxhJwpmwRHr
WBTT-TRX farm
主网合约地址:TH1XkkkPa7aF1oi7xqFYuUdXR1UvTEd4Ld
BTT-TRX farm
主网合约地址:TVjuRFx5dUn4u386DJRJS8zxgWKKcPocXo
NFT-TRX farm
主网合约地址:TViH23SgGKWfX4EHB4Q7UMeo1wgvfSvyPS
WIN-TRX farm
主网合约地址:TBd8sxVK47adcaLZgbqvN5LTUQGEpawXnx
JST-TRX farm
主网合约地址:TFw58b9t55FsmFeZHjLooS3SkMYDKXZoW7
USDT-TRX V2 farm
主网合约地址:TUnkvMyZKt4HT2qGWaMwy35c9QTmjiMCWL
NFT-USDT V2 farm
主网合约地址:TSYJHJA5rDYhaiadNXDqA6Fk6sq1yqZKwT
WIN-USDT V2 farm
主网合约地址:TEHtYRLUTGLq4Z5Pyy5C7LYZBBVVcRuvUU
JST-USDT V2 farm
主网合约地址:TKAQajxhQscgakWaokd7uNeTxwBp97xhSD
USDD-USDT V2 farm
主网合约地址:TCkNadwxyik1D66qCGmavuneowDRXPgdkL
USDD-TRX farm
主网合约地址:TCpXumigVHd2iuSkotNgkSduKqksUfpcvc
3poolnew farm
主网合约地址:THZgwb6LTg9LydbLw6gT4YMdu9y4nA5dnp
Usdd2pool farm
主网合约地址:TBSRZYLZ2pguF3EqLz86Kt9hZ4eqKEQMSY
2pool farm
主网合约地址:TFpg63byqDwniXnyxVYpSzBfWGBwZExM9J
USDD_USDT LOCK V2 farm
主网合约地址:TY1mxnpL18oDP8hsSrrxNZgUMj71pcyFS8
2pool LOCK farm
主网合约地址:TJmn1bjmNfE2F1sw2x6P224i8sFQj5mnbg
与合约交互
我们利用TronWeb与合约交互, 初始化TronWeb实例后, 就能很方便的与线上合约交互。
const TronWeb = require('tronweb')
const privateKey = process.env.PRIVATE_KEY
const apiKey = process.env.API_KEY
var tronWeb = new TronWeb({
fullHost: "https://api.trongrid.io",
headers: { "TRON-PRO-API-KEY": apiKey },
privateKey: privateKey,
})
执行交易
查看治理奖励
函数名称:claimable_tokens(address)
>>> let contract = await tronWeb.getContract('TJmn1bjmNfE2F1sw2x6P224i8sFQj5mnbg')
>>> await contract.methods.claimable_tokens('TF5MekHgFz6neU7zTpX4h2tha5miPDUj3z').send()
存款
>>> let contract = await tronWeb.getContract('TJmn1bjmNfE2F1sw2x6P224i8sFQj5mnbg')
>>> await contract.methods.deposit(1000000000000000000).send()
取款
>>> let contract = await tronWeb.getContract('TJmn1bjmNfE2F1sw2x6P224i8sFQj5mnbg')
>>> await contract.methods.withdraw(1000000000000000000).send()