0x1::YieldFarming
Farming
FarmingAsset
ParameterModifyCapability
Stake
Exp
exp
mul_u128
div_u128
truncate
initialize
initialize_asset
modify_parameter
stake
unstake
harvest
query_gov_token_amount
query_total_stake
query_stake
calculate_harvest_index_with_asset
calculate_harvest_index_weight_zero
calculate_harvest_index
calculate_withdraw_amount
exists_at
exists_asset_at
exists_stake_at_address
use 0x1::Errors;
use 0x1::Token;
Farming
The object of yield farming RewardTokenT meaning token of yield farming
struct Farming<PoolType, RewardTokenT> has store, key
treasury_token: Token::Token<RewardTokenT>
FarmingAsset
struct FarmingAsset<PoolType, AssetT> has store, key
asset_total_weight: u128
harvest_index: u128
last_update_timestamp: u64
release_per_second: u128
start_time: u64
ParameterModifyCapability
Capability to modify parameter such as period and release amount
struct ParameterModifyCapability<PoolType, AssetT> has store, key
dummy_field: bool
Stake
To store user’s asset token
struct Stake<PoolType, AssetT> has store, key
asset: AssetT
asset_weight: u128
last_harvest_index: u128
gain: u128
Exp
struct Exp has copy, drop, store
mantissa: u128
const EDEPRECATED_FUNCTION: u64 = 19;
const ERR_EXP_DIVIDE_BY_ZERO: u64 = 107;
const ERR_FARMING_BALANCE_EXCEEDED: u64 = 108;
const ERR_FARMING_HAVERST_NO_GAIN: u64 = 105;
const ERR_FARMING_INIT_REPEATE: u64 = 101;
const ERR_FARMING_NOT_ENOUGH_ASSET: u64 = 109;
const ERR_FARMING_NOT_STILL_FREEZE: u64 = 102;
const ERR_FARMING_STAKE_EXISTS: u64 = 103;
const ERR_FARMING_STAKE_NOT_EXISTS: u64 = 104;
const ERR_FARMING_TIMESTAMP_INVALID: u64 = 110;
const ERR_FARMING_TOTAL_WEIGHT_IS_ZERO: u64 = 106;
const EXP_SCALE: u128 = 1000000000000000000;
exp
fun exp(num: u128, denom: u128): YieldFarming::Exp
fun exp(num: u128, denom: u128): Exp {
// if overflow move will abort
let scaledNumerator = mul_u128(num, EXP_SCALE);
let rational = div_u128(scaledNumerator, denom);
Exp {
mantissa: rational
}
}
mul_u128
fun mul_u128(a: u128, b: u128): u128
fun mul_u128(a: u128, b: u128): u128 {
if (a == 0 || b == 0) {
return 0
};
a * b
}
div_u128
fun div_u128(a: u128, b: u128): u128
fun div_u128(a: u128, b: u128): u128 {
if ( b == 0) {
abort Errors::invalid_argument(ERR_EXP_DIVIDE_BY_ZERO)
};
if (a == 0) {
return 0
};
a / b
}
truncate
fun truncate(exp: YieldFarming::Exp): u128
initialize
Called by token issuer this will declare a yield farming pool
public fun initialize<PoolType: store, RewardTokenT: store>(_account: &signer, _treasury_token: Token::Token<RewardTokenT>)
public fun initialize<
PoolType: store,
RewardTokenT: store>(_account: &signer,
_treasury_token: Token::Token<RewardTokenT>) {
abort Errors::deprecated(EDEPRECATED_FUNCTION)
}
initialize_asset
public fun initialize_asset<PoolType: store, AssetT: store>(_account: &signer, _release_per_second: u128, _delay: u64): YieldFarming::ParameterModifyCapability<PoolType, AssetT>
public fun initialize_asset<PoolType: store, AssetT: store>(
_account: &signer,
_release_per_second: u128,
_delay: u64): ParameterModifyCapability<PoolType, AssetT> {
abort Errors::deprecated(EDEPRECATED_FUNCTION)
}
modify_parameter
public fun modify_parameter<PoolType: store, RewardTokenT: store, AssetT: store>(_cap: &YieldFarming::ParameterModifyCapability<PoolType, AssetT>, _broker: address, _release_per_second: u128)
public fun modify_parameter<PoolType: store, RewardTokenT: store, AssetT: store>(
_cap: &ParameterModifyCapability<PoolType, AssetT>,
_broker: address,
_release_per_second: u128) {
abort Errors::deprecated(EDEPRECATED_FUNCTION)
}
stake
Call by stake user, staking amount of asset in order to get yield farming token
public fun stake<PoolType: store, RewardTokenT: store, AssetT: store>(_account: &signer, _broker: address, _asset: AssetT, _asset_weight: u128)
public fun stake<PoolType: store, RewardTokenT: store, AssetT: store>(
_account: &signer,
_broker: address,
_asset: AssetT,
_asset_weight: u128) {
abort Errors::deprecated(EDEPRECATED_FUNCTION)
}
unstake
Unstake asset from farming pool
public fun unstake<PoolType: store, RewardTokenT: store, AssetT: store>(_account: &signer, _broker: address): (AssetT, Token::Token<RewardTokenT>)
public fun unstake<PoolType: store, RewardTokenT: store, AssetT: store>(_account: &signer, _broker: address)
: (AssetT, Token::Token<RewardTokenT>) {
abort Errors::deprecated(EDEPRECATED_FUNCTION)
}
harvest
Harvest yield farming token from stake
public fun harvest<PoolType: store, RewardTokenT: store, AssetT: store>(_account: &signer, _broker: address, _amount: u128): Token::Token<RewardTokenT>
public fun harvest<PoolType: store,
RewardTokenT: store,
AssetT: store>(
_account: &signer,
_broker: address,
_amount: u128): Token::Token<RewardTokenT> {
abort Errors::deprecated(EDEPRECATED_FUNCTION)
}
query_gov_token_amount
The user can quering all yield farming amount in any time and scene
public fun query_gov_token_amount<PoolType: store, RewardTokenT: store, AssetT: store>(_account: &signer, _broker: address): u128
public fun query_gov_token_amount<PoolType: store,
RewardTokenT: store,
AssetT: store>(_account: &signer, _broker: address): u128 {
0
}
query_total_stake
Query total stake count from yield farming resource
public fun query_total_stake<PoolType: store, AssetT: store>(_broker: address): u128
public fun query_total_stake<PoolType: store,
AssetT: store>(_broker: address): u128 {
0
}
query_stake
Query stake weight from user staking objects.
public fun query_stake<PoolType: store, AssetT: store>(_account: &signer): u128
public fun query_stake<PoolType: store,
AssetT: store>(_account: &signer): u128 {
0
}
calculate_harvest_index_with_asset
Update farming asset
fun calculate_harvest_index_with_asset<PoolType, AssetT>(_farming_asset: &YieldFarming::FarmingAsset<PoolType, AssetT>, _now_seconds: u64): u128
fun calculate_harvest_index_with_asset<PoolType, AssetT>(_farming_asset: &FarmingAsset<PoolType, AssetT>, _now_seconds: u64): u128 {
0
}
calculate_harvest_index_weight_zero
There is calculating from harvest index and global parameters without asset_total_weight
public fun calculate_harvest_index_weight_zero(_harvest_index: u128, _last_update_timestamp: u64, _now_seconds: u64, _release_per_second: u128): u128
public fun calculate_harvest_index_weight_zero(_harvest_index: u128,
_last_update_timestamp: u64,
_now_seconds: u64,
_release_per_second: u128): u128 {
0
}
calculate_harvest_index
There is calculating from harvest index and global parameters
public fun calculate_harvest_index(_harvest_index: u128, _asset_total_weight: u128, _last_update_timestamp: u64, _now_seconds: u64, _release_per_second: u128): u128
public fun calculate_harvest_index(_harvest_index: u128,
_asset_total_weight: u128,
_last_update_timestamp: u64,
_now_seconds: u64,
_release_per_second: u128): u128 {
0
}
calculate_withdraw_amount
This function will return a gain index
public fun calculate_withdraw_amount(_harvest_index: u128, _last_harvest_index: u128, _asset_weight: u128): u128
public fun calculate_withdraw_amount(_harvest_index: u128,
_last_harvest_index: u128,
_asset_weight: u128): u128 {
0
}
exists_at
Check the Farming of TokenT is exists.
public fun exists_at<PoolType: store, RewardTokenT: store>(broker: address): bool
public fun exists_at<PoolType: store, RewardTokenT: store>(broker: address): bool {
exists<Farming<PoolType, RewardTokenT>>(broker)
}
exists_asset_at
Check the Farming of AsssetT is exists.
public fun exists_asset_at<PoolType: store, AssetT: store>(broker: address): bool
public fun exists_asset_at<PoolType: store, AssetT: store>(broker: address): bool {
exists<FarmingAsset<PoolType, AssetT>>(broker)
}
exists_stake_at_address
Check stake at address exists.
public fun exists_stake_at_address<PoolType: store, AssetT: store>(account: address): bool
public fun exists_stake_at_address<PoolType: store, AssetT: store>(account: address): bool {
exists<Stake<PoolType, AssetT>>(account)
}
pragma verify = false;