Skip to content

Commit

Permalink
Merge pull request #128 from lidofinance/feature/si-1393-fix-chain-re…
Browse files Browse the repository at this point in the history
…wards-negative-balance

[fix] use correct address comparison in reward history
  • Loading branch information
Jeday authored May 2, 2024
2 parents a30880e + 55b8944 commit 35dee0f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
8 changes: 8 additions & 0 deletions packages/sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# 3.2.1

## SDK

### Fixed

- fixed edge-cases in `getRewardsFromChain` and `getRewardsFromSubgraph` sometimes causing transfer events to be wronged and leading to negative balance

# 3.2.0

## SDK
Expand Down
3 changes: 3 additions & 0 deletions packages/sdk/src/common/utils/address-equal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Address } from 'viem';

/**
* @deprecated use native viem isAddressEqual
*/
export const addressEqual = (a: Address | string, b: Address | string) =>
a.toLowerCase() === b.toLowerCase();
22 changes: 13 additions & 9 deletions packages/sdk/src/rewards/rewards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type PublicClient,
getContract,
zeroAddress,
isAddressEqual,
} from 'viem';
import { Logger, ErrorHandler, Cache } from '../common/decorators/index.js';
import { LidoSDKModule } from '../common/class-primitives/sdk-module.js';
Expand Down Expand Up @@ -31,7 +32,6 @@ import {
getTotalRewards,
getTransfers,
} from './subgraph/index.js';
import { addressEqual } from '../common/utils/address-equal.js';
import { getInitialData } from './subgraph/index.js';
import { calcShareRate, requestWithBlockStep, sharesToSteth } from './utils.js';
import {
Expand Down Expand Up @@ -193,12 +193,14 @@ export class LidoSDKRewards extends LidoSDKModule {
changeShares: Reward<RewardsChainEvents>['changeShares'],
balanceShares: Reward<RewardsChainEvents>['balanceShares'];

if (to === address) {
type = from === zeroAddress ? 'submit' : 'transfer_in';
if (isAddressEqual(to, address)) {
type = isAddressEqual(from, zeroAddress) ? 'submit' : 'transfer_in';
balanceShares = prevSharesBalance + sharesValue;
changeShares = sharesValue;
} else {
type = to === withdrawalQueueAddress ? 'withdrawal' : 'transfer_out';
type = isAddressEqual(to, withdrawalQueueAddress)
? 'withdrawal'
: 'transfer_out';
balanceShares = prevSharesBalance - sharesValue;
changeShares = -sharesValue;
}
Expand Down Expand Up @@ -338,10 +340,10 @@ export class LidoSDKRewards extends LidoSDKModule {
sharesAfterDecrease,
sharesAfterIncrease,
} = initialTransfer;
if (addressEqual(to, address)) {
if (isAddressEqual(to as Address, address)) {
prevBalanceShares = BigInt(sharesAfterIncrease);
prevBalance = BigInt(balanceAfterIncrease);
} else if (addressEqual(from, address)) {
} else if (isAddressEqual(from as Address, address)) {
prevBalanceShares = BigInt(sharesAfterDecrease);
prevBalance = BigInt(balanceAfterDecrease);
}
Expand Down Expand Up @@ -393,14 +395,16 @@ export class LidoSDKRewards extends LidoSDKModule {
change: Reward<RewardsSubgraphEvents>['change'],
balance: Reward<RewardsSubgraphEvents>['balance'];

if (addressEqual(to, address)) {
type = from === zeroAddress ? 'submit' : 'transfer_in';
if (isAddressEqual(to as Address, address)) {
type = isAddressEqual(from as Address, zeroAddress)
? 'submit'
: 'transfer_in';
changeShares = BigInt(shares);
balanceShares = BigInt(sharesAfterIncrease);
change = BigInt(value);
balance = BigInt(balanceAfterIncrease);
} else {
type = addressEqual(to, withdrawalQueueAddress)
type = isAddressEqual(to as Address, withdrawalQueueAddress)
? 'withdrawal'
: 'transfer_out';
balance = BigInt(balanceAfterDecrease);
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/stake/stake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
decodeEventLog,
getAbiItem,
getEventSelector,
isAddressEqual,
} from 'viem';

import type {
Expand Down Expand Up @@ -40,7 +41,6 @@ import type {
StakeResult,
} from './types.js';
import { LidoSDKModule } from '../common/class-primitives/sdk-module.js';
import { addressEqual } from '../common/utils/address-equal.js';

export class LidoSDKStake extends LidoSDKModule {
// Precomputed event signatures
Expand Down Expand Up @@ -188,12 +188,12 @@ export class LidoSDKStake extends LidoSDKModule {
});
if (
parsedLog.eventName === 'Transfer' &&
addressEqual(parsedLog.args.to, address)
isAddressEqual(parsedLog.args.to, address)
) {
stethReceived = parsedLog.args.value;
} else if (
parsedLog.eventName === 'TransferShares' &&
addressEqual(parsedLog.args.to, address)
isAddressEqual(parsedLog.args.to, address)
) {
sharesReceived = parsedLog.args.sharesValue;
}
Expand Down
10 changes: 5 additions & 5 deletions packages/sdk/src/wrap/wrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
decodeEventLog,
getAbiItem,
getEventSelector,
isAddressEqual,
} from 'viem';

import { LIDO_CONTRACT_NAMES, NOOP } from '../common/constants.js';
Expand Down Expand Up @@ -37,7 +38,6 @@ import {
} from './abi/steth-partial.js';
import { ERROR_CODE, invariant } from '../common/utils/sdk-error.js';
import { LidoSDKModule } from '../common/class-primitives/sdk-module.js';
import { addressEqual } from '../common/utils/address-equal.js';

export class LidoSDKWrap extends LidoSDKModule {
private static TRANSFER_SIGNATURE = getEventSelector(
Expand Down Expand Up @@ -397,9 +397,9 @@ export class LidoSDKWrap extends LidoSDKModule {
strict: true,
...log,
});
if (addressEqual(parsedLog.args.to, address)) {
if (isAddressEqual(parsedLog.args.to, address)) {
wstethReceived = parsedLog.args.value;
} else if (addressEqual(parsedLog.args.to, wstethAddress)) {
} else if (isAddressEqual(parsedLog.args.to, wstethAddress)) {
stethWrapped = parsedLog.args.value;
}
}
Expand Down Expand Up @@ -434,9 +434,9 @@ export class LidoSDKWrap extends LidoSDKModule {
strict: true,
...log,
});
if (addressEqual(parsedLog.args.from, address)) {
if (isAddressEqual(parsedLog.args.from, address)) {
wstethUnwrapped = parsedLog.args.value;
} else if (addressEqual(parsedLog.args.to, address)) {
} else if (isAddressEqual(parsedLog.args.to, address)) {
stethReceived = parsedLog.args.value;
}
}
Expand Down

0 comments on commit 35dee0f

Please sign in to comment.