From 3247fe1a4b976d1c3f15a101dd965a65aff2c52d Mon Sep 17 00:00:00 2001 From: Sergi Juanati Date: Mon, 20 Mar 2023 10:31:17 +0100 Subject: [PATCH] Refactored Totals calc + Updated overall comments --- src/managers/approvals.ts | 2 +- src/mappings/gvault.ts | 6 ++- src/mappings/pwrd.ts | 6 ++- src/setters/coreData.ts | 9 ++-- src/setters/totals.ts | 107 ++++++++++++++------------------------ src/setters/transfers.ts | 2 +- src/utils/contracts.ts | 5 ++ 7 files changed, 61 insertions(+), 76 deletions(-) diff --git a/src/managers/approvals.ts b/src/managers/approvals.ts index cd6c979..677c7c7 100644 --- a/src/managers/approvals.ts +++ b/src/managers/approvals.ts @@ -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) diff --git a/src/mappings/gvault.ts b/src/mappings/gvault.ts index 59a8baa..819a941 100644 --- a/src/mappings/gvault.ts +++ b/src/mappings/gvault.ts @@ -138,6 +138,8 @@ export function handleStrategyTotalChanges(ev: LogStrategyTotalChanges): void { ); } -export function handleOwnershipTransferred(ev: OwnershipTransferred): void { +/// @notice Handles events from GVault contract +/// @dev No parameter is needed: this function is used to initialise Masterdata once +export function handleOwnershipTransferred(_: OwnershipTransferred): void { initMasterDataOnce(); -} \ No newline at end of file +} diff --git a/src/mappings/pwrd.ts b/src/mappings/pwrd.ts index b4c716a..b47a3a7 100644 --- a/src/mappings/pwrd.ts +++ b/src/mappings/pwrd.ts @@ -86,6 +86,8 @@ export function handleTransfer(event: Transfer): void { } } -export function handleOwnershipTransferred(event: OwnershipTransferred) : void { +/// @notice Handles events from Pwrd contract +/// @dev No parameter is needed: this function is used to initialise Masterdata once +export function handleOwnershipTransferred(_: OwnershipTransferred) : void { initMasterDataOnce(); -} \ No newline at end of file +} diff --git a/src/setters/coreData.ts b/src/setters/coreData.ts index 9f17943..015e477 100644 --- a/src/setters/coreData.ts +++ b/src/setters/coreData.ts @@ -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), @@ -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(); -} \ No newline at end of file +} diff --git a/src/setters/totals.ts b/src/setters/totals.ts index 2e74970..f494e4f 100644 --- a/src/setters/totals.ts +++ b/src/setters/totals.ts @@ -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(); } diff --git a/src/setters/transfers.ts b/src/setters/transfers.ts index 4dee6de..05d5f9b 100644 --- a/src/setters/transfers.ts +++ b/src/setters/transfers.ts @@ -30,7 +30,7 @@ import { /// @notice Stores transfers in entity /// @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) diff --git a/src/utils/contracts.ts b/src/utils/contracts.ts index 02c578e..6edc946 100644 --- a/src/utils/contracts.ts +++ b/src/utils/contracts.ts @@ -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 = ( @@ -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 = ( @@ -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 => {