Skip to content

Commit 0bbc3b6

Browse files
authored
Merge pull request #173 from lidofinance/develop
4.0.1
2 parents 17805ad + 169fee9 commit 0bbc3b6

File tree

2 files changed

+163
-142
lines changed

2 files changed

+163
-142
lines changed

packages/sdk/CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
# 4.0.1
2+
3+
## SDK
4+
5+
### Fixed
6+
7+
- `LidoSDKRewards` now filter outs edgecases with self-transfers
8+
19
# 4.0.0
210

311
## Breaking change

packages/sdk/src/rewards/rewards.ts

+155-142
Original file line numberDiff line numberDiff line change
@@ -192,58 +192,65 @@ export class LidoSDKRewards extends LidoSDKModule {
192192
let totalRewards = 0n;
193193
let shareRate = baseShareRate;
194194
let prevSharesBalance = baseBalanceShares;
195-
let rewards: Reward<RewardsChainEvents>[] = events.map((event) => {
196-
if (event.eventName === 'TransferShares') {
197-
const { from, to, sharesValue } = event.args;
198-
let type: Reward<RewardsChainEvents>['type'],
199-
changeShares: Reward<RewardsChainEvents>['changeShares'],
200-
balanceShares: Reward<RewardsChainEvents>['balanceShares'];
201-
202-
if (isAddressEqual(to, address)) {
203-
type = isAddressEqual(from, zeroAddress) ? 'submit' : 'transfer_in';
204-
balanceShares = prevSharesBalance + sharesValue;
205-
changeShares = sharesValue;
206-
} else {
207-
type = isAddressEqual(to, withdrawalQueueAddress)
208-
? 'withdrawal'
209-
: 'transfer_out';
210-
balanceShares = prevSharesBalance - sharesValue;
211-
changeShares = -sharesValue;
195+
let rewards = events
196+
.map((event) => {
197+
if (event.eventName === 'TransferShares') {
198+
const { from, to, sharesValue } = event.args;
199+
200+
if (isAddressEqual(to, from)) {
201+
return null;
202+
}
203+
204+
let type: Reward<RewardsChainEvents>['type'],
205+
changeShares: Reward<RewardsChainEvents>['changeShares'],
206+
balanceShares: Reward<RewardsChainEvents>['balanceShares'];
207+
208+
if (isAddressEqual(to, address)) {
209+
type = isAddressEqual(from, zeroAddress) ? 'submit' : 'transfer_in';
210+
balanceShares = prevSharesBalance + sharesValue;
211+
changeShares = sharesValue;
212+
} else {
213+
type = isAddressEqual(to, withdrawalQueueAddress)
214+
? 'withdrawal'
215+
: 'transfer_out';
216+
balanceShares = prevSharesBalance - sharesValue;
217+
changeShares = -sharesValue;
218+
}
219+
220+
prevSharesBalance = balanceShares;
221+
return {
222+
type,
223+
balanceShares,
224+
changeShares,
225+
change: getCurrentStethFromShares(changeShares),
226+
balance: getCurrentStethFromShares(balanceShares),
227+
shareRate,
228+
originalEvent: event,
229+
};
212230
}
213-
214-
prevSharesBalance = balanceShares;
215-
return {
216-
type,
217-
balanceShares,
218-
changeShares,
219-
change: getCurrentStethFromShares(changeShares),
220-
balance: getCurrentStethFromShares(balanceShares),
221-
shareRate,
222-
originalEvent: event,
223-
};
224-
}
225-
if (event.eventName === 'TokenRebased') {
226-
const { postTotalEther, postTotalShares } = event.args;
227-
const oldBalance = getCurrentStethFromShares(prevSharesBalance);
228-
currentTotalEther = postTotalEther;
229-
currentTotalShares = postTotalShares;
230-
const newBalance = getCurrentStethFromShares(prevSharesBalance);
231-
shareRate = getCurrentShareRate();
232-
const change = newBalance - oldBalance;
233-
totalRewards += change;
234-
return {
235-
type: 'rebase',
236-
change,
237-
apr: LidoSDKApr.calculateAprFromRebaseEvent(event.args),
238-
changeShares: 0n,
239-
balance: newBalance,
240-
balanceShares: prevSharesBalance,
241-
shareRate,
242-
originalEvent: event,
243-
};
244-
}
245-
invariant(false, 'Impossible event');
246-
});
231+
if (event.eventName === 'TokenRebased') {
232+
const { postTotalEther, postTotalShares } = event.args;
233+
const oldBalance = getCurrentStethFromShares(prevSharesBalance);
234+
currentTotalEther = postTotalEther;
235+
currentTotalShares = postTotalShares;
236+
const newBalance = getCurrentStethFromShares(prevSharesBalance);
237+
shareRate = getCurrentShareRate();
238+
const change = newBalance - oldBalance;
239+
totalRewards += change;
240+
return {
241+
type: 'rebase',
242+
change,
243+
apr: LidoSDKApr.calculateAprFromRebaseEvent(event.args),
244+
changeShares: 0n,
245+
balance: newBalance,
246+
balanceShares: prevSharesBalance,
247+
shareRate,
248+
originalEvent: event,
249+
};
250+
}
251+
invariant(false, 'Impossible event');
252+
})
253+
.filter((event) => !!event) as Reward<RewardsChainEvents>[];
247254

248255
if (includeOnlyRebases) {
249256
rewards = rewards.filter((r) => r.type === 'rebase');
@@ -380,101 +387,107 @@ export class LidoSDKRewards extends LidoSDKModule {
380387
const baseBalanceShares = prevBalanceShares;
381388

382389
let totalRewards = 0n;
383-
let rewards: Reward<RewardsSubgraphEvents>[] = events.map((event) => {
384-
// it's a transfer
385-
if ('value' in event) {
386-
const {
387-
from,
388-
to,
389-
shares,
390-
sharesAfterIncrease,
391-
value,
392-
balanceAfterDecrease,
393-
balanceAfterIncrease,
394-
sharesAfterDecrease,
395-
totalPooledEther,
396-
totalShares,
397-
} = event;
398-
let type: Reward<RewardsSubgraphEvents>['type'],
399-
changeShares: Reward<RewardsSubgraphEvents>['changeShares'],
400-
balanceShares: Reward<RewardsSubgraphEvents>['balanceShares'],
401-
change: Reward<RewardsSubgraphEvents>['change'],
402-
balance: Reward<RewardsSubgraphEvents>['balance'];
403-
404-
if (isAddressEqual(to as Address, address)) {
405-
type = isAddressEqual(from as Address, zeroAddress)
406-
? 'submit'
407-
: 'transfer_in';
408-
changeShares = BigInt(shares);
409-
balanceShares = BigInt(sharesAfterIncrease);
410-
change = BigInt(value);
411-
balance = BigInt(balanceAfterIncrease);
412-
} else {
413-
type = isAddressEqual(to as Address, withdrawalQueueAddress)
414-
? 'withdrawal'
415-
: 'transfer_out';
416-
balance = BigInt(balanceAfterDecrease);
417-
change = -BigInt(value);
418-
changeShares = -BigInt(shares);
419-
balanceShares = BigInt(sharesAfterDecrease);
390+
let rewards = events
391+
.map((event) => {
392+
// it's a transfer
393+
if ('value' in event) {
394+
const {
395+
from,
396+
to,
397+
shares,
398+
sharesAfterIncrease,
399+
value,
400+
balanceAfterDecrease,
401+
balanceAfterIncrease,
402+
sharesAfterDecrease,
403+
totalPooledEther,
404+
totalShares,
405+
} = event;
406+
let type: Reward<RewardsSubgraphEvents>['type'],
407+
changeShares: Reward<RewardsSubgraphEvents>['changeShares'],
408+
balanceShares: Reward<RewardsSubgraphEvents>['balanceShares'],
409+
change: Reward<RewardsSubgraphEvents>['change'],
410+
balance: Reward<RewardsSubgraphEvents>['balance'];
411+
412+
if (isAddressEqual(to as Address, from as Address)) {
413+
return null;
414+
}
415+
416+
if (isAddressEqual(to as Address, address)) {
417+
type = isAddressEqual(from as Address, zeroAddress)
418+
? 'submit'
419+
: 'transfer_in';
420+
changeShares = BigInt(shares);
421+
balanceShares = BigInt(sharesAfterIncrease);
422+
change = BigInt(value);
423+
balance = BigInt(balanceAfterIncrease);
424+
} else {
425+
type = isAddressEqual(to as Address, withdrawalQueueAddress)
426+
? 'withdrawal'
427+
: 'transfer_out';
428+
balance = BigInt(balanceAfterDecrease);
429+
change = -BigInt(value);
430+
changeShares = -BigInt(shares);
431+
balanceShares = BigInt(sharesAfterDecrease);
432+
}
433+
434+
const shareRate = calcShareRate(
435+
BigInt(totalPooledEther),
436+
BigInt(totalShares),
437+
LidoSDKRewards.PRECISION,
438+
);
439+
prevBalance = balance;
440+
prevBalanceShares = balanceShares;
441+
442+
return {
443+
type,
444+
balanceShares,
445+
changeShares,
446+
change,
447+
balance,
448+
shareRate,
449+
originalEvent: event,
450+
};
420451
}
421-
422-
const shareRate = calcShareRate(
423-
BigInt(totalPooledEther),
424-
BigInt(totalShares),
425-
LidoSDKRewards.PRECISION,
426-
);
427-
prevBalance = balance;
428-
prevBalanceShares = balanceShares;
429-
430-
return {
431-
type,
432-
balanceShares,
433-
changeShares,
434-
change,
435-
balance,
436-
shareRate,
437-
originalEvent: event,
438-
};
439-
}
440-
// it's a rebase
441-
if ('apr' in event) {
442-
const {
443-
totalPooledEtherAfter,
444-
totalSharesAfter,
445-
apr: eventApr,
446-
} = event;
447-
448-
const totalEther = BigInt(totalPooledEtherAfter);
449-
const totalShares = BigInt(totalSharesAfter);
450-
451-
const newBalance = sharesToSteth(
452-
prevBalanceShares,
453-
totalEther,
454-
totalShares,
455-
LidoSDKRewards.PRECISION,
456-
);
457-
const change = newBalance - prevBalance;
458-
totalRewards += change;
459-
prevBalance = newBalance;
460-
461-
return {
462-
type: 'rebase',
463-
change,
464-
apr: Number(eventApr),
465-
changeShares: 0n,
466-
balance: newBalance,
467-
balanceShares: prevBalanceShares,
468-
shareRate: calcShareRate(
452+
// it's a rebase
453+
if ('apr' in event) {
454+
const {
455+
totalPooledEtherAfter,
456+
totalSharesAfter,
457+
apr: eventApr,
458+
} = event;
459+
460+
const totalEther = BigInt(totalPooledEtherAfter);
461+
const totalShares = BigInt(totalSharesAfter);
462+
463+
const newBalance = sharesToSteth(
464+
prevBalanceShares,
469465
totalEther,
470466
totalShares,
471467
LidoSDKRewards.PRECISION,
472-
),
473-
originalEvent: event,
474-
};
475-
}
476-
invariant(false, 'impossible event');
477-
});
468+
);
469+
const change = newBalance - prevBalance;
470+
totalRewards += change;
471+
prevBalance = newBalance;
472+
473+
return {
474+
type: 'rebase',
475+
change,
476+
apr: Number(eventApr),
477+
changeShares: 0n,
478+
balance: newBalance,
479+
balanceShares: prevBalanceShares,
480+
shareRate: calcShareRate(
481+
totalEther,
482+
totalShares,
483+
LidoSDKRewards.PRECISION,
484+
),
485+
originalEvent: event,
486+
};
487+
}
488+
invariant(false, 'impossible event');
489+
})
490+
.filter((events) => !!events) as Reward<RewardsSubgraphEvents>[];
478491

479492
if (includeOnlyRebases) {
480493
rewards = rewards.filter((r) => r.type === 'rebase');

0 commit comments

Comments
 (0)