Wepiggy: pToken redemption inputs compared
Updated
WePiggy is a non-custodial crypto lending protocol. Its published
master
implementation supports redemption by pToken count or by a specified amount of the market's underlying asset. The chosen input fixes one quantity, and the exchange rate determines the other. Redemption also depends on the account's pToken balance, collateral permission where applicable and the market's available cash.
These are internal contract paths in
PToken.sol
, with permission checks in
Comptroller.sol
. Their presence does not establish which choices a particular deployed interface exposes.
The quantity that each input fixes
A pToken represents a claim on a market's underlying asset. The published PToken redemption logic converts between those two quantities through the shared function
redeemFresh
.
- Input in pTokens
-
redeemInternal(redeemTokens)specifies the pToken quantity to deduct and passes zero for the underlying-amount input. The contract multiplies the specified pToken quantity by the exchange rate to calculate the underlying transfer amount. A different execution rate can change that amount while the pToken input stays fixed. - Input in the underlying asset
-
redeemUnderlyingInternal(redeemAmount)specifies the underlying transfer amount and passes zero for the pToken-count input. The contract divides the requested amount by the exchange rate to calculate the pToken deduction. This path expresses a withdrawal target in underlying units, while the required pToken quantity depends on the execution rate.
Each path accrues interest before
redeemFresh
reads the exchange rate. The conversion therefore uses the post-accrual state for that call. Both calculations use truncating integer arithmetic; token decimals must be accounted for when converting contract units to displayed balances. The shared function rejects a pair of nonzero redemption inputs.
Stages in the shared redemption path
| Contract stage | Condition and effect |
|---|---|
| Accrue interest and convert | The entry path accrues interest, then the shared function reads the exchange rate and derives the quantity that was not specified. |
| Check permission and freshness | The Comptroller must permit redemption. The market's interest-accrual block must also equal the current block. |
| Check balances and cash | The calculated pToken deduction must fit both the account balance and total supply. Available underlying cash in that market must cover the transfer amount. |
| Transfer and record |
A successful underlying transfer is followed by the reduced pToken balances being stored and the
Transfer
and
Redeem
events being emitted.
|
Account permission and available cash
The Comptroller redemption rules require the market to be listed. If the redeemer has entered that market, a hypothetical account-liquidity calculation tests the proposed pToken deduction. A resulting collateral shortfall or an error from that calculation prevents approval. An account that has not entered the market bypasses this collateral-liquidity check; the pToken balance and market-cash requirements still apply.
Collateral permission and available cash answer different questions. The first determines whether removing the claim would leave an account short of collateral. The second determines whether the selected market holds enough underlying asset to make the transfer. An adequate pToken balance alone does not satisfy both conditions.
Both internal entry paths set
msg.sender
as the redeemer. They deduct that account's pTokens and use the same account as the underlying transfer recipient, without taking a separate recipient argument.
Redemption results and returned errors
The
Redeem
event records the redeemer, underlying transfer amount, pToken deduction, new total pToken supply and new account pToken balance. These fields distinguish the amount transferred from the claim that was surrendered.
The internal redemption routines return zero for success and a nonzero error code for a reported failure. Some failures return an error without reverting. When the call does not revert, interest accrual performed before the failure can remain recorded even though redemption did not complete.