Pool

Deployed at: terra1z5j60wct88yz62ylqa4t8p8239cwx9kjlghkg2

config.rs

Stores all relevant contract parameters.

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
pub struct Config {
    pub this: CanonicalAddr,
    pub owner: CanonicalAddr,
    pub beneficiary: CanonicalAddr,
    pub moneymarket: CanonicalAddr,
    pub atoken: CanonicalAddr,
    pub stable_denom: String,
    pub dp_token: CanonicalAddr,
}

this: the address of this pool contract.

owner: the owner address of this pool contract.

beneficiary: the address of which all interest collected from principal deposits are sent to. could be a single address, or a contract address that oversees additional distribution logic.

moneymarket: the address of the Anchor money market contract to accept deposits.

atoken: the address of the aUST token contract.

stable_denom: base Terra stablecoin denomination of this pool to be deposited to Anchor, i.e. uusd.

dp_token: the address of the DP token contract minted by this pool.

contract.rs

Defines logic entry points for InitMsg, HandleMsg and QueryMsg calls.

Initializes a new pool contract. Updates data storage as defined with config.rs, and initializes a new DP token contract linked to this pool contract.

Defines HandleMsg endpoints. Calls the following functions per HandleMsg:

  • Receive(msg) : calls receive(deps, env, msg) defined under handler_exec.rs.

  • Deposit {} : calls deposit(deps, env) defined under handler_exec.rs.

  • ClaimReward {} : calls claim_reward(deps, env) defined under handler_exec.rs.

  • [INTERNAL] RegisterDPToken {} : calls register_dp_token(deps, env) defined under handler_exec.rs.

Defines QueryMsg endpoints. Calls the following functions per QueryMsg:

  • DepositAmountOf { owner } : calls deposit_amount(deps, owner) defined under handler_query.rs; returns the current DP token balance of the sender of this message.

  • TotalDepositAmount {} : calls total_deposit_amount(deps) defined under handler_query.rs; returns the total DP token balanced linked to this pool contract.

  • Config {} : calls config(deps) defined under handler/query.rs; returns the following:

    • beneficiary: returns the current beneficiary address set for this pool contract.

    • moneymarket: returns the Anchor money market contract set for this pool contract to call for deposit_stable and redeem_stable messages. For further information regarding logic of the Anchor money market, please refer to Anchor Protocol's official documentation.

    • stable_denom: returns the current Terra stablecoin denomination that the underlying Anchor money market contract accepts.

    • anchor_token: returns the aToken contract address that the underlying Anchor money market contract returns on deposits.

    • dp_token: returns the DP token contract address minted by this pool contract on new deposits.

  • GetClaimableReward {} : calls claimable_reward(deps) defined under handler_query.rs; returns all claimable UST rewards for all UST deposits held by this pool.

handler/core.rs

Defines core contract execution logic. Functions are as follows:

  • receive: defines a CW-20 token contract receive hook, which is executed when a Pylon DP token is sent to this Pylon pool.

  • deposit: deposits UST to this Pylon pool, deposits them to Anchor, and mints Pylon DP tokens back to the depositor.

  • redeem: consecutively called on a CW-20 receive call. Burns received DP tokens, retrieves equivalent amounts of UST from Anchor, and returns principal UST deposits excluding any interest generated.

  • claim_reward: claims any remaining interest remaining in the pool, and sends them over to the beneficiary address. Interest is calculated as (total_aust_amount * exchange_rate) - (total_dp_balance)

  • register_dp_token: registers a DP token contract controlled by this Pylon deposit pool for new DP token mints/burns on deposits/redemptions.

If a CW-20 transfer to this contract includes a Redeem {} Cw20HookMsg:

  • checks if the sender of this HookMsg is the registered DP token address (i.e. not any other token contract address being sent to this pool) - return unauthorized() error if not, else continue

  • if passed, call redeem(deps, _env, cw20_msg.sender, cw20_msg.amount)

If a Redeem {} message is not included with this function call, return an invalid_request error.

Sending native Terra stablecoins (UST) with this MsgExecuteContract call:

  • Check how much deposits have been made to this pool contract for stable_denom.

    • If there are no deposits, return a generic_err.

  • Issue a HandleResponse issuing the following messages:

    • call deposit_stable from the Anchor money market contract for deposit_amount units of stable_denom(e.g. 100 UST). aUST will be returned from the Anchor money market contract to this pool contract.

    • issue another MsgExecuteContract call that mints deposit_amount units of Pylon DP tokens for this particular pool to the caller.

When a Redeem {} Cw20HookMsg is called:

  • Query the current epoch_state from the Anchor money market.

  • Calculate how much aUST needs to be redeemed back to UST to maintain principle: dp_token_balance / epoch_state.exchange_rate

  • Calculate how much Terra tax is charged during redemptions. As tax is charged for every Terra stablecoin MsgSends on the Terra blockchain, deduct_tax should be called twice, as there are two cases when Terra tax is charged for UST transfers during redemptions:

    • when UST is transferred from the Anchor money market to this Pylon pool contract

    • when UST is transferred from this Pylon pool contract to the user's wallet

  • Issue a HandleResponse containing the following messages:

    • burn amount units of DP tokens for this Pylon pool.

    • redeem market_redeem_amount aUST (calculated as dp_token_balance / epoch_state.exchange_rate) for pool_redeem_amount UST.

    • MsgSends pool_redeem_amount back to the caller of this contract

On a ClaimReward {} HandleMsg call:

  • calculate how much aUST should be redeemed: (total_aust_amount * exchange_rate) - (total_dp_balance)

  • apply a virtual exchange rate queryable from the Pylon Exchange Rate contract. This effectively re-calculates how much UST should be claimed excluding MINE token buybacks, which is sent to the Collector contract.

  • calculates how much UST should be redeemed for:

    • market_redeem_amount : amount of aUST for redemptions before Terra blockchain tax for both beneficiary and collector. returns Uint256.

    • beneficiary_redeem_amount : amount of aUST for redemptions after Terra blockchain tax for beneficiary. returns a Coin object.

    • collector_redeem_amount : amount of aUST for redemptions after Terra blockchain tax for collector, sent to the Collector contract for buybacks. returns a Coin object.

  • Issue a HandleResponse that includes the following messages:

    • update virtual exchange rate

    • redeem market_redeem_amount aUST from Anchor

    • send beneficiary_redeem_amount to beneficiary account

    • send collector_redeem_amount to Collector contract

Last updated

Was this helpful?