Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix precision names. #113

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion contracts/partial/proxy/priceFeedMethods.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function receivePrice(
const pairName : string = param.0;
const oraclePrice = param.1.1;
const decimals : nat = getDecimal(pairName, s.tokensDecimals);
const price : nat = oraclePrice * precision / decimals;
const price : nat = oraclePrice * precision * precision / decimals;

const tokenId : nat = checkPairId(pairName, s.pairId);
var operations : list(operation) := list[
Expand Down
4 changes: 2 additions & 2 deletions contracts/partial/yToken/adminMethods.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function addMarket(
token.collateralFactorF := params.collateralFactorF;
token.reserveFactorF := params.reserveFactorF;
token.maxBorrowRate := params.maxBorrowRate;
token.threshold := params.threshold;
token.thresholdF := params.thresholdF;
token.liquidReserveRateF := params.liquidReserveRateF;

s.storage.assets[params.asset] := lastTokenId;
Expand Down Expand Up @@ -127,7 +127,7 @@ function setTokenFactors(
token.reserveFactorF := params.reserveFactorF;
token.interestRateModel := params.interestRateModel;
token.maxBorrowRate := params.maxBorrowRate;
token.threshold := params.threshold;
token.thresholdF := params.thresholdF;
token.liquidReserveRateF := params.liquidReserveRateF;
s.tokens[params.tokenId] := token;
}
Expand Down
26 changes: 13 additions & 13 deletions contracts/partial/yToken/fa2Methods.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ function getAccount(
case accounts[(user, tokenId)] of
None -> record [
allowances = (set [] : set(address));
borrow = 0n;
lastBorrowIndex = 0n;
borrowF = 0n;
lastBorrowIndexF = 0n;
]
| Some(v) -> v
end
Expand Down Expand Up @@ -37,16 +37,16 @@ function getToken(
totalLiquidF = 0n;
totalSupplyF = 0n;
totalReservesF = 0n;
borrowIndex = precision;
borrowIndexF = precision;
maxBorrowRate = 0n;
collateralFactorF = 0n;
reserveFactorF = 0n;
liquidReserveRateF = 0n;
lastPrice = 0n;
lastPriceFF = 0n;
borrowPause = False;
enterMintPause = False;
isInterestUpdating = False;
threshold = 0n;
thresholdF = 0n;
]
| Some(v) -> v
end
Expand Down Expand Up @@ -115,20 +115,20 @@ function iterateTransfer(
require(transferDst.token_id < s.lastTokenId, Errors.FA2.undefined);

(* Get source info *)
var srcBalance : nat := getBalanceByToken(params.from_, transferDst.token_id, s.ledger);
var srcBalanceF : nat := getBalanceByToken(params.from_, transferDst.token_id, s.ledger);
const transferAmountF : nat = transferDst.amount * precision;

(* Update source balance *)
srcBalance := get_nat_or_fail(srcBalance - transferAmountF, Errors.FA2.lowBalance);
srcBalanceF := get_nat_or_fail(srcBalanceF - transferAmountF, Errors.FA2.lowBalance);

s.ledger[(params.from_, transferDst.token_id)] := srcBalance;
s.ledger[(params.from_, transferDst.token_id)] := srcBalanceF;

(* Get receiver balance *)
var dstBalance : nat := getBalanceByToken(transferDst.to_, transferDst.token_id, s.ledger);
var dstBalanceF : nat := getBalanceByToken(transferDst.to_, transferDst.token_id, s.ledger);

(* Update destination balance *)
dstBalance := dstBalance + transferAmountF;
s.ledger[(transferDst.to_, transferDst.token_id)] := dstBalance;
dstBalanceF := dstBalanceF + transferAmountF;
s.ledger[(transferDst.to_, transferDst.token_id)] := dstBalanceF;
} with s
} with List.fold(makeTransfer, params.txs, s)

Expand Down Expand Up @@ -184,12 +184,12 @@ function getBalance(
block {
require(request.token_id < s.lastTokenId, Errors.FA2.undefined);
(* Retrieve the asked account from the storage *)
const userBalance : nat = getBalanceByToken(request.owner, request.token_id, s.ledger);
const userBalanceF : nat = getBalanceByToken(request.owner, request.token_id, s.ledger);

(* Form the response *)
const response : balance_of_response = record [
request = request;
balance = userBalance / precision;
balance = userBalanceF / precision;
];
} with response # l;

Expand Down
110 changes: 55 additions & 55 deletions contracts/partial/yToken/lendingMethods.ligo
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ function calcMaxCollateralInCU(
: nat is
block {
function oneToken(
var acc : nat;
var accCU : nat;
const tokenId : tokenId)
: nat is
block {
const userBalance : nat = getBalanceByToken(user, tokenId, ledger);
const userBalanceF : nat = getBalanceByToken(user, tokenId, ledger);
const token : tokenType = getToken(tokenId, tokens);
if token.totalSupplyF > 0n then {
const liquidityF : nat = getLiquidity(token);
Expand All @@ -65,27 +65,27 @@ function calcMaxCollateralInCU(
verifyInterestUpdated(token);

(* sum += collateralFactorF * exchangeRate * oraclePrice * balance *)
acc := acc + userBalance * token.lastPrice
accCU := accCU + userBalanceF * token.lastPriceFF
* token.collateralFactorF * liquidityF / token.totalSupplyF / precision;
}
else skip;

} with acc;
} with accCU;
} with Set.fold(oneToken, userMarkets, 0n)

function calcLiquidateCollateral(
function calcLiquidateCollateralInCU(
const userMarkets : set(tokenId);
const user : address;
const ledger : big_map((address * tokenId), nat);
const tokens : big_map(tokenId, tokenType))
: nat is
block {
function oneToken(
var acc : nat;
var accFF : nat;
const tokenId : tokenId)
: nat is
block {
const userBalance : nat = getBalanceByToken(user, tokenId, ledger);
const userBalanceF : nat = getBalanceByToken(user, tokenId, ledger);
const token : tokenType = getToken(tokenId, tokens);
if token.totalSupplyF > 0n then {
const liquidityF : nat = getLiquidity(token);
Expand All @@ -94,10 +94,10 @@ function calcLiquidateCollateral(
verifyInterestUpdated(token);

(* sum += balance * oraclePrice * exchangeRate *)
acc := acc + userBalance * token.lastPrice * liquidityF / token.totalSupplyF;
accCU := accCU + userBalanceF * token.lastPriceFF * liquidityF / token.totalSupplyF;
}
else skip;
} with acc * token.threshold / precision;
} with accCU * token.thresholdF / precision;
} with Set.fold(oneToken, userMarkets, 0n)

function applyInterestToBorrows(
Expand All @@ -117,11 +117,11 @@ function applyInterestToBorrows(

verifyInterestUpdated(token);

if userAccount.lastBorrowIndex =/= 0n
then userAccount.borrow := userAccount.borrow * token.borrowIndex / userAccount.lastBorrowIndex;
if userAccount.lastBorrowIndexF =/= 0n
then userAccount.borrowF := userAccount.borrowF * token.borrowIndexF / userAccount.lastBorrowIndexF;
else
skip;
userAccount.lastBorrowIndex := token.borrowIndex;
userAccount.lastBorrowIndexF := token.borrowIndexF;
} with Map.update((user, tokenId), Some(userAccount), userAccMap);
} with Set.fold(oneToken, borrowedTokens, accountsMap)

Expand All @@ -134,21 +134,21 @@ function calcOutstandingBorrowInCU(
: nat is
block {
function oneToken(
var acc : nat;
var accCU : nat;
var tokenId : tokenId)
: nat is
block {
const userAccount : account = getAccount(user, tokenId, accounts);
const userBalance : nat = getBalanceByToken(user, tokenId, ledger);
const userBalanceF : nat = getBalanceByToken(user, tokenId, ledger);
var token : tokenType := getToken(tokenId, tokens);

verifyPriceUpdated(token);

(* sum += oraclePrice * borrow *)
if userBalance > 0n or userAccount.borrow > 0n
then acc := acc + userAccount.borrow * token.lastPrice;
if userBalanceF > 0n or userAccount.borrowF > 0n
then accCU := accCU + userAccount.borrowF * token.lastPriceFF;
else skip;
} with acc;
} with accCU;
} with Set.fold(oneToken, userBorrow, 0n)

function updateInterest(
Expand Down Expand Up @@ -197,6 +197,7 @@ function mint(
require(params.tokenId < s.lastTokenId, Errors.YToken.undefined);

var mintTokensF : nat := params.amount * precision;
require(mintTokensF / precision >= params.minReceived, Errors.YToken.highReceived);
var token : tokenType := getToken(params.tokenId, s.tokens);
require(token.enterMintPause = False, Errors.YToken.enterMintPaused);

Expand All @@ -207,11 +208,10 @@ function mint(
mintTokensF := mintTokensF * token.totalSupplyF / liquidityF;
} else skip;

require(mintTokensF / precision >= params.minReceived, Errors.YToken.highReceived);

var userBalance : nat := getBalanceByToken(Tezos.sender, params.tokenId, s.ledger);
userBalance := userBalance + mintTokensF;
s.ledger[(Tezos.sender, params.tokenId)] := userBalance;
var userBalanceF : nat := getBalanceByToken(Tezos.sender, params.tokenId, s.ledger);
userBalanceF := userBalanceF + mintTokensF;
s.ledger[(Tezos.sender, params.tokenId)] := userBalanceF;
token.totalSupplyF := token.totalSupplyF + mintTokensF;
token.totalLiquidF := token.totalLiquidF + params.amount * precision;
s.tokens[params.tokenId] := token;
Expand All @@ -232,18 +232,18 @@ function redeem(
require(params.tokenId < s.lastTokenId, Errors.YToken.undefined);

var token : tokenType := getToken(params.tokenId, s.tokens);
var userBalance : nat := getBalanceByToken(Tezos.sender, params.tokenId, s.ledger);
var userBalanceF : nat := getBalanceByToken(Tezos.sender, params.tokenId, s.ledger);
const liquidityF : nat = getLiquidity(token);
const redeemAmount : nat = if params.amount = 0n
then userBalance * liquidityF / token.totalSupplyF / precision
else params.amount;
then userBalanceF * liquidityF / token.totalSupplyF / precision
else params.amount;
require(redeemAmount >= params.minReceived, Errors.YToken.highReceived);
var burnTokensF : nat := if params.amount = 0n
then userBalance
else ceil_div(redeemAmount * precision * token.totalSupplyF, liquidityF);
then userBalanceF
else ceil_div(redeemAmount * precision * token.totalSupplyF, liquidityF);

userBalance := get_nat_or_fail(userBalance - burnTokensF, Errors.YToken.lowBalance);
s.ledger[(Tezos.sender, params.tokenId)] := userBalance;
userBalanceF := get_nat_or_fail(userBalanceF - burnTokensF, Errors.YToken.lowBalance);
s.ledger[(Tezos.sender, params.tokenId)] := userBalanceF;

token.totalSupplyF := get_nat_or_fail(token.totalSupplyF - burnTokensF, Errors.YToken.lowSupply);
token.totalLiquidF := get_nat_or_fail(token.totalLiquidF - redeemAmount * precision, Errors.YToken.lowLiquidity);
Expand Down Expand Up @@ -304,7 +304,7 @@ function borrow(
var userAccount : account := getAccount(Tezos.sender, params.tokenId, s.accounts);
const borrowsF : nat = params.amount * precision;

userAccount.borrow := userAccount.borrow + borrowsF;
userAccount.borrowF := userAccount.borrowF + borrowsF;
s.accounts[(Tezos.sender, params.tokenId)] := userAccount;
s.borrows[Tezos.sender] := borrowTokens;

Expand Down Expand Up @@ -352,12 +352,12 @@ function repay(
var repayAmountF : nat := params.amount * precision;

if repayAmountF = 0n
then repayAmountF := userAccount.borrow;
then repayAmountF := userAccount.borrowF;
else skip;

userAccount.borrow := get_nat_or_fail(userAccount.borrow - repayAmountF, Errors.YToken.repayOverflow);
userAccount.borrowF := get_nat_or_fail(userAccount.borrowF - repayAmountF, Errors.YToken.repayOverflow);

if userAccount.borrow = 0n
if userAccount.borrowF = 0n
then borrowTokens := Set.remove(params.tokenId, borrowTokens);
else skip;

Expand Down Expand Up @@ -396,7 +396,7 @@ function liquidate(

require(Tezos.sender =/= params.borrower, Errors.YToken.borrowerNotLiquidator);

const liquidateCollateral : nat = calcLiquidateCollateral(
const liquidateCollateralInCU : nat = calcLiquidateCollateralInCU(
userCollateralTokens,
params.borrower,
s.ledger,
Expand All @@ -410,17 +410,17 @@ function liquidate(
s.tokens
);

require(liquidateCollateral < outstandingBorrowInCU, Errors.YToken.cantLiquidate);
require(liquidateCollateralInCU < outstandingBorrowInCU, Errors.YToken.cantLiquidate);

var liqAmountF : nat := params.amount * precision;
(* liquidate amount can't be more than allowed close factor *)
const maxClose : nat = borrowerAccount.borrow * s.closeFactorF / precision;
const maxCloseF : nat = borrowerAccount.borrowF * s.closeFactorF / precision;

require(maxClose >= liqAmountF, Errors.YToken.repayOverflow);
require(maxCloseF >= liqAmountF, Errors.YToken.repayOverflow);

borrowerAccount.borrow := get_nat_or_fail(borrowerAccount.borrow - liqAmountF, Errors.YToken.lowBorrowAmount);
borrowerAccount.borrowF := get_nat_or_fail(borrowerAccount.borrowF - liqAmountF, Errors.YToken.lowBorrowAmount);

if borrowerAccount.borrow = 0n
if borrowerAccount.borrowF = 0n
then userBorrowedTokens := Set.remove(params.borrowToken, userBorrowedTokens);
else skip;

Expand All @@ -436,33 +436,33 @@ function liquidate(
* priceBorrowed / priceCollateral
seizeTokens = seizeAmount / exchangeRate
*)
const seizeAmount : nat = liqAmountF * s.liqIncentiveF
* borrowToken.lastPrice * collateralToken.totalSupplyF;
const seizeAmountFFFFF : nat = liqAmountF * s.liqIncentiveF
* borrowToken.lastPriceFF * collateralToken.totalSupplyF;
const liquidityF : nat = getLiquidity(collateralToken);
const exchangeRateF : nat = liquidityF * precision * collateralToken.lastPrice;
const seizeTokensF : nat = seizeAmount / exchangeRateF;
const exchangeRateFFFF : nat = liquidityF * precision * collateralToken.lastPriceFF;
const seizeTokensF : nat = seizeAmountFFFFF / exchangeRateFFFF;
require(seizeTokensF / precision >= params.minSeized, Errors.YToken.highSeize);
var liquidatorAccount : account := getAccount(
Tezos.sender,
params.collateralToken,
s.accounts
);
var borrowerBalance : nat := getBalanceByToken(params.borrower, params.collateralToken, s.ledger);
var liquidatorBalance : nat := getBalanceByToken(Tezos.sender, params.collateralToken, s.ledger);
borrowerBalance := get_nat_or_fail(borrowerBalance - seizeTokensF, Errors.YToken.lowBorrowerBalanceS);
liquidatorBalance := liquidatorBalance + seizeTokensF;
var borrowerBalanceF : nat := getBalanceByToken(params.borrower, params.collateralToken, s.ledger);
var liquidatorBalanceF : nat := getBalanceByToken(Tezos.sender, params.collateralToken, s.ledger);
borrowerBalanceF := get_nat_or_fail(borrowerBalanceF - seizeTokensF, Errors.YToken.lowBorrowerBalanceS);
liquidatorBalanceF := liquidatorBalanceF + seizeTokensF;

(* collect reserves incentive from liquidation *)
const reserveAmountF : nat = liqAmountF * collateralToken.liquidReserveRateF
* borrowToken.lastPrice * collateralToken.totalSupplyF;
const reserveSharesF : nat = ceil_div(reserveAmountF, exchangeRateF);
const reserveTokensF : nat = liqAmountF * collateralToken.liquidReserveRateF * borrowToken.lastPrice / ( precision * collateralToken.lastPrice) ;
borrowerBalance := get_nat_or_fail(borrowerBalance - reserveSharesF, Errors.YToken.lowBorrowerBalanceR);
const reserveAmountFFFFFF : nat = liqAmountF * collateralToken.liquidReserveRateF
* borrowToken.lastPriceFF * collateralToken.totalSupplyF;
const reserveSharesF : nat = ceil_div(reserveAmountFFFFFF, (exchangeRateFFFF));
const reserveTokensF : nat = liqAmountF * collateralToken.liquidReserveRateF * borrowToken.lastPriceFF / ( precision * collateralToken.lastPriceFF) ;
borrowerBalanceF := get_nat_or_fail(borrowerBalanceF - reserveSharesF, Errors.YToken.lowBorrowerBalanceR);
collateralToken.totalReservesF := collateralToken.totalReservesF + reserveTokensF;
collateralToken.totalSupplyF := get_nat_or_fail(collateralToken.totalSupplyF - reserveSharesF, Errors.YToken.lowCollateralTotalSupply);

s.ledger[(params.borrower, params.collateralToken)] := borrowerBalance;
s.ledger[(Tezos.sender, params.collateralToken)] := liquidatorBalance;
s.ledger[(params.borrower, params.collateralToken)] := borrowerBalanceF;
s.ledger[(Tezos.sender, params.collateralToken)] := liquidatorBalanceF;
s.accounts[(params.borrower, params.borrowToken)] := borrowerAccount;
s.accounts[(Tezos.sender, params.collateralToken)] := liquidatorAccount;
s.tokens[params.collateralToken] := collateralToken;
Expand Down Expand Up @@ -540,7 +540,7 @@ function priceCallback(
params.tokenId,
s.storage.tokens
);
token.lastPrice := params.amount;
token.lastPriceFF := params.amount;
token.priceUpdateTime := Tezos.now;
s.storage.tokens[params.tokenId] := token;
} with (noOperations, s)
Expand Down Expand Up @@ -571,7 +571,7 @@ function accrueInterest(
token.totalReservesF := interestAccumulatedF * token.reserveFactorF /
precision + token.totalReservesF;
// one mult operation with F require precision division
token.borrowIndex := simpleInterestFactorF * token.borrowIndex / precision + token.borrowIndex;
token.borrowIndexF := simpleInterestFactorF * token.borrowIndexF / precision + token.borrowIndexF;
token.interestUpdateTime := Tezos.now;

s.storage.tokens[params.tokenId] := token;
Expand Down
Loading