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.
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.
pubfnreceive<S:Storage, A:Api, Q:Querier>( deps:&Extern<S, A, Q>, env:Env, cw20_msg:Cw20ReceiveMsg,) ->StdResult<HandleResponse> {let sender = env.message.sender.clone();ifletSome(msg) = cw20_msg.msg {matchfrom_binary(&msg)? {Cw20HookMsg::Redeem {} => {// only asset contract can execute this messagelet config: config::Config= config::read(&deps.storage)?;if deps.api.canonical_address(&sender)?!= config.dp_token {returnErr(StdError::unauthorized()); }redeem(deps, env, cw20_msg.sender, cw20_msg.amount) } } } else {Err(StdError::generic_err("Invalid request: \"redeem\" message not included in request", )) }}
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.
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
calculate how much aUST should be redeemed: (total_aust_amount * exchange_rate) - (total_dp_balance)
apply a virtual exchange rate queryable from the PylonExchange 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