starcoin-framework

Module 0x1::YieldFarming

use 0x1::Errors;
use 0x1::Token;

Resource Farming

The object of yield farming RewardTokenT meaning token of yield farming

struct Farming<PoolType, RewardTokenT> has store, key
Fields
treasury_token: Token::Token<RewardTokenT>

Resource FarmingAsset

struct FarmingAsset<PoolType, AssetT> has store, key
Fields
asset_total_weight: u128
harvest_index: u128
last_update_timestamp: u64
release_per_second: u128
start_time: u64

Resource ParameterModifyCapability

Capability to modify parameter such as period and release amount

struct ParameterModifyCapability<PoolType, AssetT> has store, key
Fields
dummy_field: bool

Resource Stake

To store user’s asset token

struct Stake<PoolType, AssetT> has store, key
Fields
asset: AssetT
asset_weight: u128
last_harvest_index: u128
gain: u128

Struct Exp

struct Exp has copy, drop, store
Fields
mantissa: u128

Constants

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;

Function exp

fun exp(num: u128, denom: u128): YieldFarming::Exp
Implementation
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
    }
}

Function mul_u128

fun mul_u128(a: u128, b: u128): u128
Implementation
fun mul_u128(a: u128, b: u128): u128 {
    if (a == 0 || b == 0) {
        return 0
    };

    a * b
}

Function div_u128

fun div_u128(a: u128, b: u128): u128
Implementation
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
}

Function truncate

fun truncate(exp: YieldFarming::Exp): u128
Implementation
fun truncate(exp: Exp): u128 {
    return exp.mantissa / EXP_SCALE
}

Function 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>)
Implementation
public fun initialize<
    PoolType: store,
    RewardTokenT: store>(_account: &signer,
                         _treasury_token: Token::Token<RewardTokenT>) {
    abort Errors::deprecated(EDEPRECATED_FUNCTION)
}

Function initialize_asset

public fun initialize_asset<PoolType: store, AssetT: store>(_account: &signer, _release_per_second: u128, _delay: u64): YieldFarming::ParameterModifyCapability<PoolType, AssetT>
Implementation
public fun initialize_asset<PoolType: store, AssetT: store>(
    _account: &signer,
    _release_per_second: u128,
    _delay: u64): ParameterModifyCapability<PoolType, AssetT> {
    abort Errors::deprecated(EDEPRECATED_FUNCTION)
}

Function modify_parameter

public fun modify_parameter<PoolType: store, RewardTokenT: store, AssetT: store>(_cap: &YieldFarming::ParameterModifyCapability<PoolType, AssetT>, _broker: address, _release_per_second: u128)
Implementation
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)
}

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)
Implementation
public fun stake<PoolType: store, RewardTokenT: store, AssetT: store>(
    _account: &signer,
    _broker: address,
    _asset: AssetT,
    _asset_weight: u128) {
    abort Errors::deprecated(EDEPRECATED_FUNCTION)
}

Function unstake

Unstake asset from farming pool

public fun unstake<PoolType: store, RewardTokenT: store, AssetT: store>(_account: &signer, _broker: address): (AssetT, Token::Token<RewardTokenT>)
Implementation
public fun unstake<PoolType: store, RewardTokenT: store, AssetT: store>(_account: &signer, _broker: address)
: (AssetT, Token::Token<RewardTokenT>) {
    abort Errors::deprecated(EDEPRECATED_FUNCTION)
}

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>
Implementation
public fun harvest<PoolType: store,
                   RewardTokenT: store,
                   AssetT: store>(
    _account: &signer,
    _broker: address,
    _amount: u128): Token::Token<RewardTokenT> {
    abort Errors::deprecated(EDEPRECATED_FUNCTION)
}

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
Implementation
public fun query_gov_token_amount<PoolType: store,
                                  RewardTokenT: store,
                                  AssetT: store>(_account: &signer, _broker: address): u128 {
    0
}

Function query_total_stake

Query total stake count from yield farming resource

public fun query_total_stake<PoolType: store, AssetT: store>(_broker: address): u128
Implementation
public fun query_total_stake<PoolType: store,
                             AssetT: store>(_broker: address): u128 {
    0
}

Function query_stake

Query stake weight from user staking objects.

public fun query_stake<PoolType: store, AssetT: store>(_account: &signer): u128
Implementation
public fun query_stake<PoolType: store,
                       AssetT: store>(_account: &signer): u128 {
    0
}

Function 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
Implementation
fun calculate_harvest_index_with_asset<PoolType, AssetT>(_farming_asset: &FarmingAsset<PoolType, AssetT>, _now_seconds: u64): u128 {
    0
}

Function 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
Implementation
public fun calculate_harvest_index_weight_zero(_harvest_index: u128,
                                               _last_update_timestamp: u64,
                                               _now_seconds: u64,
                                               _release_per_second: u128): u128 {
    0
}

Function 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
Implementation
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
}

Function 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
Implementation
public fun calculate_withdraw_amount(_harvest_index: u128,
                                     _last_harvest_index: u128,
                                     _asset_weight: u128): u128 {
    0
}

Function exists_at

Check the Farming of TokenT is exists.

public fun exists_at<PoolType: store, RewardTokenT: store>(broker: address): bool
Implementation
public fun exists_at<PoolType: store, RewardTokenT: store>(broker: address): bool {
    exists<Farming<PoolType, RewardTokenT>>(broker)
}

Function exists_asset_at

Check the Farming of AsssetT is exists.

public fun exists_asset_at<PoolType: store, AssetT: store>(broker: address): bool
Implementation
public fun exists_asset_at<PoolType: store, AssetT: store>(broker: address): bool {
    exists<FarmingAsset<PoolType, AssetT>>(broker)
}

Function exists_stake_at_address

Check stake at address exists.

public fun exists_stake_at_address<PoolType: store, AssetT: store>(account: address): bool
Implementation
public fun exists_stake_at_address<PoolType: store, AssetT: store>(account: address): bool {
    exists<Stake<PoolType, AssetT>>(account)
}

Module Specification

pragma verify = false;