Skip to content

Commit d96c95e

Browse files
authored
Merge pull request #474 from balancednetwork/fix/rewards-optimization
fix: optimize rewards query
2 parents 6cc3af0 + eb3ca2a commit d96c95e

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

core-contracts/Rewards/src/main/java/network/balanced/score/core/rewards/DataSourceImpl.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -257,17 +257,7 @@ public void addRewardToken(Address token) {
257257

258258
@SuppressWarnings("unchecked")
259259
public Map<String, BigInteger> loadCurrentSupply(String owner) {
260-
// Bad handling that is only relevant during migration, otherwise it will always succeed on first scenario
261-
try {
262-
return (Map<String, BigInteger>) Context.call(getContractAddress(), "getBalanceAndSupply", getName(), owner);
263-
} catch (Exception e) {
264-
try {
265-
return (Map<String, BigInteger>) Context.call(getContractAddress(), "getBalanceAndSupply", getName(), Address.fromString(owner));
266-
} catch (Exception _e) {
267-
return Map.of("_totalSupply", BigInteger.ZERO,
268-
"_balance", BigInteger.ZERO);
269-
}
270-
}
260+
return (Map<String, BigInteger>) Context.call(getContractAddress(), "getBalanceAndSupply", getName(), owner);
271261
}
272262

273263
public Map<Address, BigInteger> updateSingleUserData(BigInteger currentTime, BalanceData balances, String user, boolean readOnlyContext) {

core-contracts/Rewards/src/main/java/network/balanced/score/core/rewards/RewardsImpl.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,20 @@ public BigInteger getBalnHolding(String _holder) {
194194
public Map<String, BigInteger> getRewards(String _holder) {
195195
Map<String, BigInteger> accruedRewards = getHoldings(_holder);
196196
int dataSourcesCount = DataSourceDB.size();
197+
BigInteger currentTime = getTime();
198+
197199
for (int i = 0; i < dataSourcesCount; i++) {
198200
String name = DataSourceDB.names.get(i);
199201
DataSourceImpl dataSource = DataSourceDB.get(name);
200-
BigInteger currentTime = getTime();
201202
BalanceData balances = new BalanceData();
203+
balances.prevWorkingBalance = dataSource.getWorkingBalance(_holder);
204+
if (balances.prevWorkingBalance.compareTo(BigInteger.ZERO) <= 0) {
205+
continue;
206+
}
207+
202208
balances.prevBalance = dataSource.getBalance(_holder);
203209
balances.prevSupply = dataSource.getTotalSupply();
204-
balances.prevWorkingBalance = dataSource.getWorkingBalance(_holder, true);
205-
balances.prevWorkingSupply = dataSource.getWorkingSupply(true);
210+
balances.prevWorkingSupply = dataSource.getWorkingSupply();
206211
Map<Address, BigInteger> sourceRewards = dataSource.updateSingleUserData(currentTime, balances, _holder, true);
207212
for (Map.Entry<Address, BigInteger> entry : sourceRewards.entrySet()) {
208213
accruedRewards.put(entry.getKey().toString(), accruedRewards.get(entry.getKey().toString()).add(entry.getValue()));

0 commit comments

Comments
 (0)