Skip to content

Commit

Permalink
Refactored Totals calc + Updated overall comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sjuanati committed Mar 20, 2023
1 parent b4c64a8 commit 3247fe1
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 76 deletions.
2 changes: 1 addition & 1 deletion src/managers/approvals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { setApprovalTx } from '../setters/approvals';


/// @notice Manages approval events
/// @param ev the approval event
/// @param ev the parsed approval event
/// @param token the approval token (gro, gvt or pwrd)
/// @dev only handles approvals confirmed by users, but not approval
/// updates (e.g.: during deposits and withdrawals)
Expand Down
6 changes: 4 additions & 2 deletions src/mappings/gvault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ export function handleStrategyTotalChanges(ev: LogStrategyTotalChanges): void {
);
}

export function handleOwnershipTransferred(ev: OwnershipTransferred): void {
/// @notice Handles <OwnershipTransferred> events from GVault contract
/// @dev No parameter is needed: this function is used to initialise Masterdata once
export function handleOwnershipTransferred(_: OwnershipTransferred): void {
initMasterDataOnce();
}
}
6 changes: 4 additions & 2 deletions src/mappings/pwrd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export function handleTransfer(event: Transfer): void {
}
}

export function handleOwnershipTransferred(event: OwnershipTransferred) : void {
/// @notice Handles <OwnershipTransferred> events from Pwrd contract
/// @dev No parameter is needed: this function is used to initialise Masterdata once
export function handleOwnershipTransferred(_: OwnershipTransferred) : void {
initMasterDataOnce();
}
}
9 changes: 6 additions & 3 deletions src/setters/coreData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function setTotalSupply(
amount: BigInt,
coin: string,
): void {
const decimals = (coin == Token.UNISWAP_GRO_USDC) ? 12 : DECIMALS;
const decimals = (coin === Token.UNISWAP_GRO_USDC) ? 12 : DECIMALS;
if (from == ADDR.ZERO) {
updateTotalSupply(
tokenToDecimal(amount, 18, decimals),
Expand Down Expand Up @@ -119,7 +119,10 @@ export const updateTotalSupply = (
core.total_supply_balancer_gro_weth = core.total_supply_balancer_gro_weth
.plus(amount);
} else {
showLog.error(`coreData.ts->updateTotalSupply(): can't update for coin {} side {}`, [coin]);
showLog.error(
`coreData.ts->updateTotalSupply(): can't update supply for coin {}`,
[coin]
);
}
core.save();
}
}
107 changes: 40 additions & 67 deletions src/setters/totals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,78 +87,51 @@ export const setTotals = (
factor: BigDecimal,
): void => {
let total = initTotals(userAddress, false);

const isInbound = type === TxType.CORE_DEPOSIT || type === TxType.TRANSFER_IN;
const coinAmountSigned = isInbound
? coinAmount
: coinAmount.times(NUM.MINUS_ONE);
const usdAmountSigned = isInbound
? usdAmount
: usdAmount.times(NUM.MINUS_ONE);

if (coin === Token.GRO) {
if (
type === TxType.CORE_DEPOSIT
|| type === TxType.TRANSFER_IN
) {
total.amount_added_gro = total.amount_added_gro
.plus(coinAmount);
total.amount_total_gro = total.amount_total_gro
.plus(coinAmount);
if (isInbound) {
total.amount_added_gro = total.amount_added_gro.plus(coinAmount);
} else {
total.amount_removed_gro = total.amount_removed_gro
.plus(coinAmount);
total.amount_total_gro = total.amount_total_gro
.minus(coinAmount);
total.amount_removed_gro = total.amount_removed_gro.plus(coinAmount);
}
} else if (
type === TxType.CORE_DEPOSIT
|| type === TxType.TRANSFER_IN
) {
if (coin === Token.GVT) {
total.amount_added_gvt = total.amount_added_gvt
.plus(coinAmount);
total.value_added_gvt = total.value_added_gvt
.plus(usdAmount);
total.net_amount_gvt = total.net_amount_gvt
.plus(coinAmount);
total.net_value_gvt = total.net_value_gvt
.plus(usdAmount);
} else if (coin === Token.PWRD) {
const based_amount_pwrd = coinAmount.times(factor);
total.amount_added_pwrd = total.amount_added_pwrd
.plus(coinAmount);
total.value_added_pwrd = total.value_added_pwrd
.plus(usdAmount);
total.net_based_amount_pwrd = total.net_based_amount_pwrd
.plus(based_amount_pwrd);
total.net_value_pwrd = total.net_value_pwrd
.plus(usdAmount);
total.amount_total_gro = total.amount_total_gro.plus(coinAmountSigned);
} else if (coin === Token.GVT) {
if (isInbound) {
total.amount_added_gvt = total.amount_added_gvt.plus(coinAmount);
total.value_added_gvt = total.value_added_gvt.plus(usdAmount);
} else {
total.amount_removed_gvt = total.amount_removed_gvt.plus(coinAmount);
total.value_removed_gvt = total.value_removed_gvt.plus(usdAmount);
}
total.value_added_total = total.value_added_total
.plus(usdAmount);
total.net_value_total = total.net_value_total
.plus(usdAmount);
} else if (
type === TxType.CORE_WITHDRAWAL
|| type === TxType.TRANSFER_OUT
) {
if (coin === Token.GVT) {
total.amount_removed_gvt = total.amount_removed_gvt
.plus(coinAmount);
total.value_removed_gvt = total.value_removed_gvt
.plus(usdAmount);
total.net_amount_gvt = total.net_amount_gvt
.minus(coinAmount);
total.net_value_gvt = total.net_value_gvt
.minus(usdAmount);
} else if (coin === Token.PWRD) {
total.amount_removed_pwrd = total.amount_removed_pwrd
.plus(coinAmount);
total.value_removed_pwrd = total.value_removed_pwrd
.plus(usdAmount);
const based_amount_pwrd = coinAmount
.times(factor);
total.net_based_amount_pwrd = total.net_based_amount_pwrd
.minus(based_amount_pwrd);
total.net_value_pwrd = total.net_value_pwrd
.minus(usdAmount);
total.net_amount_gvt = total.net_amount_gvt.plus(coinAmountSigned);
total.net_value_gvt = total.net_value_gvt.plus(usdAmountSigned);
} else if (coin === Token.PWRD) {
const basedAmountPwrdSigned = coinAmountSigned.times(factor);
if (isInbound) {
total.amount_added_pwrd = total.amount_added_pwrd.plus(coinAmount);
total.value_added_pwrd = total.value_added_pwrd.plus(usdAmount);
} else {
total.amount_removed_pwrd = total.amount_removed_pwrd.plus(coinAmount);
total.value_removed_pwrd = total.value_removed_pwrd.plus(usdAmount);
}
total.net_based_amount_pwrd = total.net_based_amount_pwrd.plus(basedAmountPwrdSigned);
total.net_value_pwrd = total.net_value_pwrd.plus(usdAmountSigned);
}
if (coin === Token.GVT || coin === Token.PWRD) {
total.net_value_total = total.net_value_total.plus(usdAmountSigned);
if (isInbound) {
total.value_added_total = total.value_added_total.plus(usdAmount);
} else {
total.value_removed_total = total.value_removed_total.plus(usdAmount);
}
total.value_removed_total = total.value_removed_total
.plus(usdAmount);
total.net_value_total = total.net_value_total
.minus(usdAmount);
}
total.save();
}
2 changes: 1 addition & 1 deletion src/setters/transfers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import {
/// @notice Stores transfers in entity <TransferTx>
/// @dev Staker transfers are excluded, as they are considered 'intra' operations
/// within Gro Protocol and kept in users balance
/// @param ev the transfer event
/// @param ev the parsed transfer event
/// @param userAddress the user address
/// @param type the transfer type (core_deposit, core_withdrawal, transfer_in, transfer_out)
/// @param token the transfer token (gvt, pwrd, gro)
Expand Down
5 changes: 5 additions & 0 deletions src/utils/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { STAKER_ADDRESSES } from '../utils/constants';


/// @notice Checks if Transfer is a deposit or withdrawal based on from/to addresses
/// @param from the from address
/// @param to the to address
/// @return - True if deposit (from = 0x) or withdrawal (to = 0x)
/// - False otherwise
export const isDepositOrWithdrawal = (
Expand All @@ -30,6 +32,8 @@ export const isDepositOrWithdrawal = (
}

/// @notice Checks if Transfer comes from Staker contract
/// @param from the from address
/// @param to the to address
/// @return - True if from or to is a Staker address
/// - False otherwise
export const isStakerTransfer = (
Expand All @@ -42,6 +46,7 @@ export const isStakerTransfer = (
}

/// @notice Checks if Transfer goes to the GRouter contract
/// @param to the to address
/// @return - True if from to is the GRouter address
/// - False otherwise
export const isTransferToGRouter = (to: Address): bool => {
Expand Down

0 comments on commit 3247fe1

Please sign in to comment.