Skip to content

Commit 3247fe1

Browse files
committed
Refactored Totals calc + Updated overall comments
1 parent b4c64a8 commit 3247fe1

7 files changed

Lines changed: 61 additions & 76 deletions

File tree

src/managers/approvals.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { setApprovalTx } from '../setters/approvals';
2222

2323

2424
/// @notice Manages approval events
25-
/// @param ev the approval event
25+
/// @param ev the parsed approval event
2626
/// @param token the approval token (gro, gvt or pwrd)
2727
/// @dev only handles approvals confirmed by users, but not approval
2828
/// updates (e.g.: during deposits and withdrawals)

src/mappings/gvault.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ export function handleStrategyTotalChanges(ev: LogStrategyTotalChanges): void {
138138
);
139139
}
140140

141-
export function handleOwnershipTransferred(ev: OwnershipTransferred): void {
141+
/// @notice Handles <OwnershipTransferred> events from GVault contract
142+
/// @dev No parameter is needed: this function is used to initialise Masterdata once
143+
export function handleOwnershipTransferred(_: OwnershipTransferred): void {
142144
initMasterDataOnce();
143-
}
145+
}

src/mappings/pwrd.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ export function handleTransfer(event: Transfer): void {
8686
}
8787
}
8888

89-
export function handleOwnershipTransferred(event: OwnershipTransferred) : void {
89+
/// @notice Handles <OwnershipTransferred> events from Pwrd contract
90+
/// @dev No parameter is needed: this function is used to initialise Masterdata once
91+
export function handleOwnershipTransferred(_: OwnershipTransferred) : void {
9092
initMasterDataOnce();
91-
}
93+
}

src/setters/coreData.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export function setTotalSupply(
7171
amount: BigInt,
7272
coin: string,
7373
): void {
74-
const decimals = (coin == Token.UNISWAP_GRO_USDC) ? 12 : DECIMALS;
74+
const decimals = (coin === Token.UNISWAP_GRO_USDC) ? 12 : DECIMALS;
7575
if (from == ADDR.ZERO) {
7676
updateTotalSupply(
7777
tokenToDecimal(amount, 18, decimals),
@@ -119,7 +119,10 @@ export const updateTotalSupply = (
119119
core.total_supply_balancer_gro_weth = core.total_supply_balancer_gro_weth
120120
.plus(amount);
121121
} else {
122-
showLog.error(`coreData.ts->updateTotalSupply(): can't update for coin {} side {}`, [coin]);
122+
showLog.error(
123+
`coreData.ts->updateTotalSupply(): can't update supply for coin {}`,
124+
[coin]
125+
);
123126
}
124127
core.save();
125-
}
128+
}

src/setters/totals.ts

Lines changed: 40 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -87,78 +87,51 @@ export const setTotals = (
8787
factor: BigDecimal,
8888
): void => {
8989
let total = initTotals(userAddress, false);
90+
91+
const isInbound = type === TxType.CORE_DEPOSIT || type === TxType.TRANSFER_IN;
92+
const coinAmountSigned = isInbound
93+
? coinAmount
94+
: coinAmount.times(NUM.MINUS_ONE);
95+
const usdAmountSigned = isInbound
96+
? usdAmount
97+
: usdAmount.times(NUM.MINUS_ONE);
98+
9099
if (coin === Token.GRO) {
91-
if (
92-
type === TxType.CORE_DEPOSIT
93-
|| type === TxType.TRANSFER_IN
94-
) {
95-
total.amount_added_gro = total.amount_added_gro
96-
.plus(coinAmount);
97-
total.amount_total_gro = total.amount_total_gro
98-
.plus(coinAmount);
100+
if (isInbound) {
101+
total.amount_added_gro = total.amount_added_gro.plus(coinAmount);
99102
} else {
100-
total.amount_removed_gro = total.amount_removed_gro
101-
.plus(coinAmount);
102-
total.amount_total_gro = total.amount_total_gro
103-
.minus(coinAmount);
103+
total.amount_removed_gro = total.amount_removed_gro.plus(coinAmount);
104104
}
105-
} else if (
106-
type === TxType.CORE_DEPOSIT
107-
|| type === TxType.TRANSFER_IN
108-
) {
109-
if (coin === Token.GVT) {
110-
total.amount_added_gvt = total.amount_added_gvt
111-
.plus(coinAmount);
112-
total.value_added_gvt = total.value_added_gvt
113-
.plus(usdAmount);
114-
total.net_amount_gvt = total.net_amount_gvt
115-
.plus(coinAmount);
116-
total.net_value_gvt = total.net_value_gvt
117-
.plus(usdAmount);
118-
} else if (coin === Token.PWRD) {
119-
const based_amount_pwrd = coinAmount.times(factor);
120-
total.amount_added_pwrd = total.amount_added_pwrd
121-
.plus(coinAmount);
122-
total.value_added_pwrd = total.value_added_pwrd
123-
.plus(usdAmount);
124-
total.net_based_amount_pwrd = total.net_based_amount_pwrd
125-
.plus(based_amount_pwrd);
126-
total.net_value_pwrd = total.net_value_pwrd
127-
.plus(usdAmount);
105+
total.amount_total_gro = total.amount_total_gro.plus(coinAmountSigned);
106+
} else if (coin === Token.GVT) {
107+
if (isInbound) {
108+
total.amount_added_gvt = total.amount_added_gvt.plus(coinAmount);
109+
total.value_added_gvt = total.value_added_gvt.plus(usdAmount);
110+
} else {
111+
total.amount_removed_gvt = total.amount_removed_gvt.plus(coinAmount);
112+
total.value_removed_gvt = total.value_removed_gvt.plus(usdAmount);
128113
}
129-
total.value_added_total = total.value_added_total
130-
.plus(usdAmount);
131-
total.net_value_total = total.net_value_total
132-
.plus(usdAmount);
133-
} else if (
134-
type === TxType.CORE_WITHDRAWAL
135-
|| type === TxType.TRANSFER_OUT
136-
) {
137-
if (coin === Token.GVT) {
138-
total.amount_removed_gvt = total.amount_removed_gvt
139-
.plus(coinAmount);
140-
total.value_removed_gvt = total.value_removed_gvt
141-
.plus(usdAmount);
142-
total.net_amount_gvt = total.net_amount_gvt
143-
.minus(coinAmount);
144-
total.net_value_gvt = total.net_value_gvt
145-
.minus(usdAmount);
146-
} else if (coin === Token.PWRD) {
147-
total.amount_removed_pwrd = total.amount_removed_pwrd
148-
.plus(coinAmount);
149-
total.value_removed_pwrd = total.value_removed_pwrd
150-
.plus(usdAmount);
151-
const based_amount_pwrd = coinAmount
152-
.times(factor);
153-
total.net_based_amount_pwrd = total.net_based_amount_pwrd
154-
.minus(based_amount_pwrd);
155-
total.net_value_pwrd = total.net_value_pwrd
156-
.minus(usdAmount);
114+
total.net_amount_gvt = total.net_amount_gvt.plus(coinAmountSigned);
115+
total.net_value_gvt = total.net_value_gvt.plus(usdAmountSigned);
116+
} else if (coin === Token.PWRD) {
117+
const basedAmountPwrdSigned = coinAmountSigned.times(factor);
118+
if (isInbound) {
119+
total.amount_added_pwrd = total.amount_added_pwrd.plus(coinAmount);
120+
total.value_added_pwrd = total.value_added_pwrd.plus(usdAmount);
121+
} else {
122+
total.amount_removed_pwrd = total.amount_removed_pwrd.plus(coinAmount);
123+
total.value_removed_pwrd = total.value_removed_pwrd.plus(usdAmount);
124+
}
125+
total.net_based_amount_pwrd = total.net_based_amount_pwrd.plus(basedAmountPwrdSigned);
126+
total.net_value_pwrd = total.net_value_pwrd.plus(usdAmountSigned);
127+
}
128+
if (coin === Token.GVT || coin === Token.PWRD) {
129+
total.net_value_total = total.net_value_total.plus(usdAmountSigned);
130+
if (isInbound) {
131+
total.value_added_total = total.value_added_total.plus(usdAmount);
132+
} else {
133+
total.value_removed_total = total.value_removed_total.plus(usdAmount);
157134
}
158-
total.value_removed_total = total.value_removed_total
159-
.plus(usdAmount);
160-
total.net_value_total = total.net_value_total
161-
.minus(usdAmount);
162135
}
163136
total.save();
164137
}

src/setters/transfers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
/// @notice Stores transfers in entity <TransferTx>
3131
/// @dev Staker transfers are excluded, as they are considered 'intra' operations
3232
/// within Gro Protocol and kept in users balance
33-
/// @param ev the transfer event
33+
/// @param ev the parsed transfer event
3434
/// @param userAddress the user address
3535
/// @param type the transfer type (core_deposit, core_withdrawal, transfer_in, transfer_out)
3636
/// @param token the transfer token (gvt, pwrd, gro)

src/utils/contracts.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { STAKER_ADDRESSES } from '../utils/constants';
1818

1919

2020
/// @notice Checks if Transfer is a deposit or withdrawal based on from/to addresses
21+
/// @param from the from address
22+
/// @param to the to address
2123
/// @return - True if deposit (from = 0x) or withdrawal (to = 0x)
2224
/// - False otherwise
2325
export const isDepositOrWithdrawal = (
@@ -30,6 +32,8 @@ export const isDepositOrWithdrawal = (
3032
}
3133

3234
/// @notice Checks if Transfer comes from Staker contract
35+
/// @param from the from address
36+
/// @param to the to address
3337
/// @return - True if from or to is a Staker address
3438
/// - False otherwise
3539
export const isStakerTransfer = (
@@ -42,6 +46,7 @@ export const isStakerTransfer = (
4246
}
4347

4448
/// @notice Checks if Transfer goes to the GRouter contract
49+
/// @param to the to address
4550
/// @return - True if from to is the GRouter address
4651
/// - False otherwise
4752
export const isTransferToGRouter = (to: Address): bool => {

0 commit comments

Comments
 (0)