Farm

A contract to distribute reward tokens to stakers.

All times are in seconds.

Structs

Account

  struct Account<phantom StakeCoin, phantom RewardCoin> has key, store {
    id: UID,
    farm_id: ID, 
    amount: u64,
    reward_debt: u256
  }
  • farm_id - The `sui::object::ID` of the farm to which this account belongs to.

  • amount - The amount of StakeCoin the user has in the Farm.

  • reward_debt - Amount of rewards the Farm has already paid the user.

Farm

struct Farm<phantom StakeCoin, phantom RewardCoin> has key, store {
    id: UID,  
    rewards_per_second: u64,
    start_timestamp: u64,
    last_reward_timestamp: u64, 
    accrued_rewards_per_share: u256,
    balance_stake_coin: Balance<StakeCoin>,
    balance_reward_coin: Balance<RewardCoin>,
    stake_coin_decimal_factor: u64,
    owned_by: ID
 }
  • rewards_per_second - Amount of RewardCoin to give to stakers per second.

  • start_timestamp - The timestamp in seconds that this farm will start distributing rewards.

  • last_reward_timestamp - Last timestamp that the farm was updated.

  • accrued_rewards_per_share - Total amount of rewards per share distributed by this farm.

  • balance_stake_coin - StakeCoin deposited in this farm.

  • balance_reward_coin - RewardCoin deposited in this farm.

  • stake_coin_decimal_factor - The decimal scalar of the StakeCoin.

  • owned_by - The `sui::object::ID` of the OwnerCap that "owns" this farm.

Interface

new_cap

It creates an OwnerCap. It is used to provide admin capabilities to the holder.

public fun new_cap(ctx: &mut TxContext): OwnerCap<FarmWitness>
  • @return OwnerCap.

new_farm

It creates an Farm<StakeCoin, RewardCoin>. The start_timestamp is in seconds.

public fun new_farm<StakeCoin, RewardCoin>(
    cap: &mut OwnerCap<FarmWitness>,
    stake_coin_metadata: &CoinMetadata<StakeCoin>,
    c: &Clock,
    rewards_per_second: u64,
    start_timestamp: u64,
    ctx: &mut TxContext
 ): Farm<StakeCoin, RewardCoin>
  • @param cap: An OwnerCap that will be assigned the admin rights of the newly created Farm.

  • @param stake_coin_metadata: The sui::coin::CoinMetadata of the StakeCoin.

  • @param c: The sui::clock::Clock shared object.

  • @param rewards_per_second: The amount of RewardCoin the farm can distribute to stakers.

  • @param start_timestamp: The timestamp in seconds that the farm is allowed to start distributing rewards.

  • @return Farm<StakeCoin, RewardCoin>.

new_account

It creates an Account<StakeCoin, RewardCoin>. It is used to keep track of the holder's deposit and rewards.

public fun new_account<StakeCoin, RewardCoin>(self: &Farm<StakeCoin, RewardCoin>, ctx: &mut TxContext): Account<StakeCoin, RewardCoin>
  • @param cap: self The Farm<StakeCoin, RewardCoin>.

  • @return Account<StakeCoin, RewardCoin>.

rewards_per_second

Returns the self rewards per second.

public fun rewards_per_second<StakeCoin, RewardCoin>(self: &Farm<StakeCoin, RewardCoin>): u64
  • @param cap: self The Farm<StakeCoin, RewardCoin>.

  • @return u64.

start_timestamp

Returns the self start timestamp.

public fun start_timestamp<StakeCoin, RewardCoin>(self: &Farm<StakeCoin, RewardCoin>): u64
  • @param cap: self The Farm<StakeCoin, RewardCoin>.

  • @return u64.

last_reward_timestamp

Returns the self last reward timestamp.

public fun last_reward_timestamp<StakeCoin, RewardCoin>(self: &Farm<StakeCoin, RewardCoin>): u64
  • @param cap: self The Farm<StakeCoin, RewardCoin>.

  • @return u64.

accrued_rewards_per_share

Returns the self accrued rewards per share.

public fun accrued_rewards_per_share<StakeCoin, RewardCoin>(self: &Farm<StakeCoin, RewardCoin>): u256
  • @param cap: self The Farm<StakeCoin, RewardCoin>.

  • @return u256.

balance_stake_coin

Returns the self stake coin balance.

public fun balance_stake_coin<StakeCoin, RewardCoin>(self: &Farm<StakeCoin, RewardCoin>): u64
  • @param cap: self The Farm<StakeCoin, RewardCoin>.

  • @return u64.

balance_reward_coin

Returns the self reward coin balance.

public fun balance_reward_coin<StakeCoin, RewardCoin>(self: &Farm<StakeCoin, RewardCoin>): u64
  • @param cap: self The Farm<StakeCoin, RewardCoin>.

  • @return u64.

stake_coin_decimal_factor

Returns the self reward coin decimal scalar.

public fun stake_coin_decimal_factor<StakeCoin, RewardCoin>(self: &Farm<StakeCoin, RewardCoin>): u64
  • @param cap: self The Farm<StakeCoin, RewardCoin>.

  • @return u64.

owned_by

Returns the self reward coin decimal scalar.

public fun owned_by<StakeCoin, RewardCoin>(self: &Farm<StakeCoin, RewardCoin>): ID
  • @param cap: self The Farm<StakeCoin, RewardCoin>.

  • @return ID.

amount

Returns the account staked amount.

public fun amount<StakeCoin, RewardCoin>(account: &Account<StakeCoin, RewardCoin>): u64
  • @param cap: self The Farm<StakeCoin, RewardCoin>.

  • @return u64.

reward_debt

Returns the account reward debt.

public fun reward_debt<StakeCoin, RewardCoin>(account: &Account<StakeCoin, RewardCoin>): u256
  • @param cap: self The Farm<StakeCoin, RewardCoin>.

  • @return u256.

pending_rewards

Returns the account's pending rewards. It does not update the state.

public fun pending_rewards<StakeCoin, RewardCoin>(
    farm: &Farm<StakeCoin, RewardCoin>, 
    account: &Account<StakeCoin, RewardCoin>,
    c: &Clock, 
 ): u64
  • @param farm: The Farm<StakeCoin, RewardCoin>.

  • @param account: The Account associated with the farm.

  • @param c: The sui::clock::Clock shared object.

  • @return u64.

add_rewards

It allows anyone to add rewards to the farm.

public fun add_rewards<StakeCoin, RewardCoin>(self: &mut Farm<StakeCoin, RewardCoin>, c: &Clock, reward: Coin<RewardCoin>)
  • @param self: The Farm<StakeCoin, RewardCoin>.

  • @param c: The sui::clock::Clock shared object.

  • @param reward: The RewardCoin to be added to the self.

stake

Allows a user to stake stake_coin in the farm. On the first deposits the returned Coin will have a value of zero. So make sure to destroy it.

public fun stake<StakeCoin, RewardCoin>(
    farm: &mut Farm<StakeCoin, RewardCoin>, 
    account: &mut Account<StakeCoin, RewardCoin>,
    stake_coin: Coin<StakeCoin>, 
    c: &Clock,
    ctx: &mut TxContext
): Coin<RewardCoin>
  • @param farm: The Farm<StakeCoin, RewardCoin>.

  • @param account: The Account associated with the farm.

  • @param stake_coin: The StakeCoin to stake in the farm.

  • @param c: The sui::clock::Clock shared object.

  • @return Coin. It gives any pending rewards to the user.

Aborts

  • If the account does not belong to the farm.

unstake

Allows a user to unstake his stake_coin in the farm.

public fun unstake<StakeCoin, RewardCoin>(
    farm: &mut Farm<StakeCoin, RewardCoin>, 
    account: &mut Account<StakeCoin, RewardCoin>,
    amount: u64,
    c: &Clock,
    ctx: &mut TxContext
 ): (Coin<StakeCoin>, Coin<RewardCoin>)
  • @param farm: The Farm<StakeCoin, RewardCoin>.

  • @param account: The Account associated with the farm.

  • @param amount: The amount of StakeCoin to remove from the farm.

  • @param c: The sui::clock::Clock shared object.

  • @return Coin. The staked Coin.

  • @return Coin. It gives any pending rewards to the user.

Aborts

  • amount is larger than the account.amount. If the user tries to unstake more than he has staked.

destroy_zero_account

Destroys the account.

public fun destroy_zero_account<StakeCoin, RewardCoin>(account: Account<StakeCoin, RewardCoin>)
  • @param account: The Account associated with the farm.

Aborts

  • account has an amount greater than zero.

update_rewards_per_second

Updates the rewards per second of the farm.

public fun update_rewards_per_second<StakeCoin, RewardCoin>(
    farm: &mut Farm<StakeCoin, RewardCoin>,     
    cap: &OwnerCap<FarmWitness>, 
    new_rewards_per_second: u64,
    c: &Clock
)
  • @param farm: The Farm<StakeCoin, RewardCoin>.

  • @param cap: The OwnerCap that "owns" the farm.

  • @param new_rewards_per_second: The new amount of RewardCoin the farm will give.

  • @param c: The sui::clock::Clock shared object.

Aborts

  • cap does not own the farm.

destroy_zero_farm

Destroys the farm.

public fun destroy_zero_farm<StakeCoin, RewardCoin>(farm: Farm<StakeCoin, RewardCoin>, cap: &OwnerCap<FarmWitness>)
  • @param farm: The Farm<StakeCoin, RewardCoin>.

  • @param cap: The OwnerCap that "owns" the farm.

Aborts

  • cap does not own the farm.

  • farm still has staked coins.

  • farm still has reward coins.

borrow_mut_uid

Returns a mutable reference of the farm's sui::object::UI to allow the cap owner to extend its functionalities.

public fun borrow_mut_uid<StakeCoin, RewardCoin>(farm: &mut Farm<StakeCoin, RewardCoin>, cap: &OwnerCap<FarmWitness>): &mut UID
  • @param farm: The Farm<StakeCoin, RewardCoin>.

  • @param cap: The OwnerCap that "owns" the farm.

  • @return &mut UID.

Aborts

  • cap does not own the farm.

Last updated