Liquidity Staking (Farming)

Contract Interface

1. Notations & Structs

1.1. Payment

EnterFarmResultType = EsdtTokenPayment

FARM token: a Meta-ESDT token representing the farming position.

1.2. FarmTokenAttributes

#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, PartialEq, TypeAbi, Clone, ManagedVecItem, Debug)]
pub struct FarmTokenAttributes<M: ManagedTypeApi> {
    pub reward_per_share: BigUint<M>,
    pub slope_used: BigUint<M>,
    pub booster: ManagedAddress<M>,
    pub initial_farm_amount: BigUint<M>,
    pub initial_farming_amount: BigUint<M>,
    pub reward_tokens: ManagedVec<M, RewardTokens<M>>,
}

fn get_farm_token_attributes<T: TopDecode>(&self, token_id: &TokenIdentifier, token_nonce: u64) -> T {
    let token_info = self.blockchain().get_esdt_token_data(&self.blockchain().get_sc_address(), token_id, token_nonce);
    token_info.decode_attributes()
}

Get attributes of Farm MetaESDT token.

1.3. InteractFarmEvent

2. Write functions

2.1. enterFarm

Start farming.

Attached tokens:

  • LP tokens from Ashswapโ€™s pools

  • old FARM tokens: if you attach multiple FARM tokens, they will be automatically merged with the new FARM token (like mergeFarmTokens).

Return: ManagedVec<EsdtTokenPayment>

  • A new Farm token

  • Reward tokens from old FARM tokens above.

2.2. exitFarm

Claim reward + withdraw LP token. You can only exit one farm using one FARM token per transaction.

Attached token: FARM token received from enterFarm function.

Return: ManagedVec<EsdtTokenPayment>

  • 1st EsdtTokenPayment: LP token

  • 2nd EsdtTokenPayment: Reward token

2.3. claimRewards

Claim rewards (ASH token) without withdrawal. You can only claim rewards from one FARM token in one transaction.

Return: ManagedVec<EsdtTokenPayment>

  • 1st EsdtTokenPayment: LP token

  • 2nd EsdtTokenPayment: Reward token

3. Read functions

3.1. getState

Get the current state of a farm.

3.2. getRewardPerShare

Return:

  • BigUint - the number of ASH tokens received each block per FARM token.

3.3. getFarmTokenSupply

Return:

  • BigUint - the total supply of the FARM token.

3.4 calculateRewardsForGivenPosition

Get your current reward from a farm.

Params:

  • amount: BigUint - your FARM tokenโ€™s balance

  • attributes: FarmTokenAttributes

    Note: The struct is decoded from FARM tokenโ€™s attributes.

4. Events

4.1. Interact farm event

Event for enterFarm, exitFarm, claimRewards

Last updated

Was this helpful?