Skip to content

Commit

Permalink
NetworkAddress implementation on boosted baln
Browse files Browse the repository at this point in the history
  • Loading branch information
sagars committed Dec 11, 2024
1 parent 094861e commit 826c64e
Show file tree
Hide file tree
Showing 13 changed files with 308 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public void xTokenFallback(String _from, BigInteger _value, byte[] _data) {
require(_value.compareTo(BigInteger.ZERO) > 0, TAG + ": Invalid token transfer value");

if (method.equals("_deposit")) {

JsonObject params = json.get("params").asObject();
String to = _from;
if (params.get("address") != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -619,11 +619,11 @@ public void updateBalanceAndSupplyBatch(String _name, BigInteger _totalSupply, R
}

@External
public void onKick(Address user) {
public void onKick(String user) {
checkStatus();
only(getBoostedBaln());
BigInteger boostedSupply = fetchBoostedSupply();
updateAllUserRewards(user.toString(), getAllSources(), BigInteger.ZERO, boostedSupply);
updateAllUserRewards(user, getAllSources(), BigInteger.ZERO, boostedSupply);
}

@External
Expand All @@ -635,11 +635,11 @@ public void kick(Address user, String[] sources) {
}

@External
public void onBalanceUpdate(Address user, BigInteger balance) {
public void onBalanceUpdate(String user, BigInteger balance) {
checkStatus();
only(getBoostedBaln());
BigInteger boostedSupply = fetchBoostedSupply();
updateAllUserRewards(user.toString(), getAllSources(), balance, boostedSupply);
updateAllUserRewards(user, getAllSources(), balance, boostedSupply);
}

@External
Expand Down Expand Up @@ -1034,7 +1034,7 @@ private BigInteger fetchBoostedBalance(String user) {

private BigInteger fetchBoostedBalance(Address user) {
try {
return (BigInteger) RewardsImpl.call(getBoostedBaln(), "balanceOf", user, BigInteger.ZERO);
return (BigInteger) RewardsImpl.call(getBoostedBaln(), "xBalanceOf", user, BigInteger.ZERO);
} catch (Exception e) {
return BigInteger.ZERO;
}
Expand Down
1 change: 1 addition & 0 deletions score-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ dependencies {
implementation Dependencies.javaeeScorex
implementation Dependencies.minimalJson
implementation 'xyz.venture23:xcall-lib:2.1.0'
implementation Dependencies.javaeeTokens

compileOnly Dependencies.javaeeScoreClient
annotationProcessor Dependencies.javaeeScoreClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,17 @@ public interface BoostedBaln extends AddressManager, TokenFallback, Version {
BigInteger getTotalLocked();

@External(readonly = true)
List<Address> getUsers(int start, int end);
List<String> getUsers(int start, int end);

@External(readonly = true)
boolean hasLocked(Address _owner);

@External(readonly = true)
BigInteger getLastUserSlope(Address address);

@External(readonly = true)
BigInteger getLastUserSlopeV2(String address);

@External(readonly = true)
BigInteger userPointHistoryTimestamp(Address address, BigInteger index);

Expand All @@ -80,6 +83,9 @@ public interface BoostedBaln extends AddressManager, TokenFallback, Version {
@External(readonly = true)
BigInteger balanceOf(Address _owner, @Optional BigInteger timestamp);

@External(readonly = true)
BigInteger xBalanceOf(String _owner, @Optional BigInteger timestamp);

@External(readonly = true)
BigInteger balanceOfAt(Address _owner, BigInteger block);

Expand All @@ -100,4 +106,7 @@ public interface BoostedBaln extends AddressManager, TokenFallback, Version {

@External(readonly = true)
BigInteger userPointEpoch(Address address);

@External(readonly = true)
BigInteger xUserPointEpoch(String address);
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ public interface Rewards extends
BigInteger getTimeOffset();

@External
void onKick(Address user);
void onKick(String user);

@External
void kick(Address user, String[] sources);

@External
void onBalanceUpdate(Address user, BigInteger balance);
void onBalanceUpdate(String user, BigInteger balance);

@External
void setBoostWeight(BigInteger weight);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package network.balanced.score.lib.utils;

import com.iconloop.score.util.EnumerableSet;

import foundation.icon.xcall.NetworkAddress;
import score.Address;

public class AddressEnumerableSet {

protected final EnumerableSet<Address> legacyEnumerableSet;
protected final EnumerableSet<String> enumerableSet;

public AddressEnumerableSet(String id){
legacyEnumerableSet = new EnumerableSet<>(id, Address.class);
enumerableSet = new EnumerableSet<>(id+"_migrated", String.class);
}

public int length() {
return legacyEnumerableSet.length()+enumerableSet.length();
}

public String at(int index) {
if(index< legacyEnumerableSet.length()){
return legacyEnumerableSet.at(index).toString();
}else{
index = index - enumerableSet.length();
return enumerableSet.at(index);
}
}

public boolean contains(String value) {
return enumerableSet.contains(value) || legacyEnumerableSet.contains(Address.fromString(NetworkAddress.valueOf(value).account()));
}

//remove if not needed
public Integer indexOf(String value) {
Integer index = enumerableSet.indexOf(value);
Integer legacyIndex = legacyEnumerableSet.indexOf(Address.fromString(value)); //todo: check if throws error
if(index!=null){
return index + legacyEnumerableSet.length();
}else return legacyIndex;
}

public void add(String value) {
if (!contains(value)) {
// add new value
enumerableSet.add(value);
}
}

public void remove(String value) {
Integer index = enumerableSet.indexOf(value);
if(index!=null){
enumerableSet.remove(value);
}else{
Integer legacyIndex = legacyEnumerableSet.indexOf(Address.fromString(NetworkAddress.valueOf(value).account())); //todo: check if throws error
if(legacyIndex!=null){
legacyEnumerableSet.remove(Address.fromString(value));
}
}
}

}
1 change: 1 addition & 0 deletions token-contracts/bBaln/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies {
implementation Dependencies.javaeeScorex
implementation Dependencies.minimalJson
implementation Dependencies.javaeeTokens
implementation 'xyz.venture23:xcall-lib:2.1.0'

testImplementation Dependencies.javaeeUnitTest
// Use JUnit Jupiter for testing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
*/

package network.balanced.score.tokens;

import com.iconloop.score.util.EnumerableSet;
import foundation.icon.xcall.NetworkAddress;
import network.balanced.score.lib.interfaces.BoostedBaln;
import network.balanced.score.lib.utils.Names;
import network.balanced.score.lib.utils.*;
import network.balanced.score.tokens.db.LockedBalance;
import network.balanced.score.tokens.db.Point;
import network.balanced.score.tokens.utils.UnsignedBigInteger;
Expand All @@ -44,7 +43,6 @@ public abstract class AbstractBoostedBaln implements BoostedBaln {
public static BigInteger ICX = pow(BigInteger.TEN, 18);
protected static final UnsignedBigInteger MULTIPLIER = pow10(18);

protected static final int DEPOSIT_FOR_TYPE = 0;
protected static final int CREATE_LOCK_TYPE = 1;
protected static final int INCREASE_LOCK_AMOUNT = 2;
protected static final int INCREASE_UNLOCK_TIME = 3;
Expand All @@ -56,26 +54,36 @@ public abstract class AbstractBoostedBaln implements BoostedBaln {

protected final VarDB<Address> penaltyAddress = Context.newVarDB("Boosted_baln_penalty_address", Address.class);

protected final DictDB<Address, LockedBalance> locked = Context.newDictDB("Boosted_Baln_locked",
LockedBalance.class);
// protected final DictDB<Address, LockedBalance> locked = Context.newDictDB("Boosted_Baln_locked",
// LockedBalance.class);

protected static final AddressDictDB<LockedBalance> locked = new AddressDictDB<>("Boosted_Baln_locked", LockedBalance.class);

protected final VarDB<BigInteger> epoch = Context.newVarDB("Boosted_Baln_epoch", BigInteger.class);
protected final DictDB<BigInteger, Point> pointHistory = Context.newDictDB("Boosted_baln_point_history",
Point.class);
protected final BranchDB<Address, DictDB<BigInteger, Point>> userPointHistory = Context.newBranchDB(
"Boosted_baln_user_point_history", Point.class);
protected final DictDB<Address, BigInteger> userPointEpoch = Context.newDictDB("Boosted_Baln_user_point_epoch",
// protected final BranchDB<Address, DictDB<BigInteger, Point>> userPointHistory = Context.newBranchDB(
// "Boosted_baln_user_point_history", Point.class);
protected final AddressBranchDictDB<BigInteger, Point> userPointHistory = new AddressBranchDictDB<>("Boosted_baln_user_point_history", Point.class);
// protected final DictDB<Address, BigInteger> userPointEpoch = Context.newDictDB("Boosted_Baln_user_point_epoch",
// BigInteger.class);

protected final AddressDictDB<BigInteger> userPointEpoch = new AddressDictDB<>("Boosted_Baln_user_point_epoch",
BigInteger.class);
protected final DictDB<BigInteger, BigInteger> slopeChanges = Context.newDictDB("Boosted_Baln_slope_changes",
BigInteger.class);

protected final EnumerableSet<Address> users = new EnumerableSet<>("users_list", Address.class);
protected final AddressEnumerableSet users = new AddressEnumerableSet("users_list");
protected final VarDB<BigInteger> minimumLockingAmount = Context.newVarDB("Boosted_baln_minimum_locking_amount",
BigInteger.class);

public final VarDB<String> currentVersion = Context.newVarDB("version", String.class);

public static String NATIVE_NID;

public AbstractBoostedBaln(Address _governance, String name, String symbol) {
onInstall(_governance, name, symbol);
NATIVE_NID = (String) Context.call(getXCall(), "getNetworkId");
}

private void onInstall(Address _governance, String name, String symbol) {
Expand Down Expand Up @@ -106,10 +114,18 @@ private void onInstall(Address _governance, String name, String symbol) {
public void Deposit(Address provider, BigInteger locktime, BigInteger value, int type, BigInteger timestamp) {
}

@EventLog(indexed = 2)
public void DepositV2(String provider, BigInteger locktime, BigInteger value, int type, BigInteger timestamp) {
}

@EventLog(indexed = 1)
public void Withdraw(Address provider, BigInteger value, BigInteger timestamp) {
}

@EventLog(indexed = 1)
public void WithdrawV2(String provider, BigInteger value, BigInteger timestamp) {
}

@EventLog
public void Supply(BigInteger prevSupply, BigInteger supply) {
}
Expand Down Expand Up @@ -156,7 +172,7 @@ protected BigInteger findBlockEpoch(BigInteger block, BigInteger maxEpoch) {
return min;
}

protected BigInteger findUserPointHistory(Address address, BigInteger block) {
protected BigInteger findUserPointHistory(String address, BigInteger block) {
BigInteger min = BigInteger.ZERO;
BigInteger max = this.userPointEpoch.getOrDefault(address, BigInteger.ZERO);

Expand Down Expand Up @@ -204,15 +220,20 @@ protected BigInteger supplyAt(Point point, BigInteger time) {
return lastPoint.bias;
}

protected LockedBalance getLockedBalance(Address user) {
protected LockedBalance getLockedBalance(String user) {
return locked.getOrDefault(user, new LockedBalance());
}

protected Point getUserPointHistory(Address user, BigInteger epoch) {
return this.userPointHistory.at(user).getOrDefault(epoch, new Point());
protected Point getUserPointHistory(String user, BigInteger epoch) {
user = user;
return this.userPointHistory.at(NetworkAddress.valueOf(user)).getOrDefault(epoch, new Point());
}

protected String getStringNetworkAddress(Address address){
return new NetworkAddress(NATIVE_NID, address).toString();
}

protected void checkpoint(Address address, LockedBalance oldLocked, LockedBalance newLocked) {
protected void checkpoint(String address, LockedBalance oldLocked, LockedBalance newLocked) {
Point uOld = new Point();
Point uNew = new Point();
BigInteger oldDSlope = BigInteger.ZERO;
Expand All @@ -222,7 +243,7 @@ protected void checkpoint(Address address, LockedBalance oldLocked, LockedBalanc
UnsignedBigInteger blockTimestamp = UnsignedBigInteger.valueOf(Context.getBlockTimestamp());
UnsignedBigInteger blockHeight = UnsignedBigInteger.valueOf(Context.getBlockHeight());

if (!address.equals(EOA_ZERO)) {
if (!address.equals(getStringNetworkAddress(EOA_ZERO))) {
// Calculate slopes and biases
// Kept at zero when they have to
if (oldLocked.end.compareTo(blockTimestamp) > 0 && oldLocked.amount.compareTo(BigInteger.ZERO) > 0) {
Expand Down Expand Up @@ -310,7 +331,7 @@ protected void checkpoint(Address address, LockedBalance oldLocked, LockedBalanc
}

this.epoch.set(epoch);
if (!address.equals(EOA_ZERO)) {
if (!address.equals(getStringNetworkAddress(EOA_ZERO))) {
lastPoint.slope = lastPoint.slope.add(uNew.slope.subtract(uOld.slope));
lastPoint.bias = lastPoint.bias.add(uNew.bias.subtract(uOld.bias));

Expand All @@ -324,7 +345,7 @@ protected void checkpoint(Address address, LockedBalance oldLocked, LockedBalanc

this.pointHistory.set(epoch, lastPoint);

if (!address.equals(EOA_ZERO)) {
if (!address.equals(getStringNetworkAddress(EOA_ZERO))) {
if (oldLocked.end.compareTo(blockTimestamp) > 0) {
oldDSlope = oldDSlope.add(uOld.slope);
if (newLocked.end.equals(oldLocked.end)) {
Expand All @@ -343,34 +364,34 @@ protected void checkpoint(Address address, LockedBalance oldLocked, LockedBalanc
this.userPointEpoch.set(address, userEpoch);
uNew.timestamp = blockTimestamp;
uNew.block = blockHeight;
this.userPointHistory.at(address).set(userEpoch, uNew);
this.userPointHistory.at(NetworkAddress.valueOf(address, NATIVE_NID)).set(userEpoch, uNew);
}
}

protected void depositFor(Address address, BigInteger value, BigInteger unlockTime, LockedBalance lockedBalance,
protected void depositFor(String address, BigInteger value, BigInteger unlockTime, LockedBalance lockedBalance,
int type) {
LockedBalance locked = lockedBalance.newLockedBalance();
LockedBalance balanceLocked = lockedBalance.newLockedBalance();
BigInteger supplyBefore = this.supply.get();
BigInteger blockTimestamp = BigInteger.valueOf(Context.getBlockTimestamp());

this.supply.set(supplyBefore.add(value));
LockedBalance oldLocked = locked.newLockedBalance();
LockedBalance oldLocked = balanceLocked.newLockedBalance();

locked.amount = locked.amount.add(value);
balanceLocked.amount = balanceLocked.amount.add(value);
if (!unlockTime.equals(BigInteger.ZERO)) {
locked.end = new UnsignedBigInteger(unlockTime);
balanceLocked.end = new UnsignedBigInteger(unlockTime);
}

this.locked.set(address, locked);
this.checkpoint(address, oldLocked, locked);
locked.set(address, balanceLocked);
this.checkpoint(address, oldLocked, balanceLocked);

Deposit(address, value, locked.getEnd(), type, blockTimestamp);
DepositV2(address, value, balanceLocked.getEnd(), type, blockTimestamp);
Supply(supplyBefore, supplyBefore.add(value));

onBalanceUpdate(address, balanceOf(address, blockTimestamp));
onBalanceUpdate(address, xBalanceOf(address, blockTimestamp));
}

protected void createLock(Address sender, BigInteger value, BigInteger unlockTime) {
protected void createLock(String sender, BigInteger value, BigInteger unlockTime) {
globalReentryLock();
BigInteger blockTimestamp = BigInteger.valueOf(Context.getBlockTimestamp());

Expand All @@ -388,7 +409,7 @@ protected void createLock(Address sender, BigInteger value, BigInteger unlockTim
this.depositFor(sender, value, unlockTime, locked, CREATE_LOCK_TYPE);
}

protected void increaseAmount(Address sender, BigInteger value, BigInteger unlockTime) {
protected void increaseAmount(String sender, BigInteger value, BigInteger unlockTime) {
globalReentryLock();
BigInteger blockTimestamp = BigInteger.valueOf(Context.getBlockTimestamp());
LockedBalance locked = getLockedBalance(sender);
Expand All @@ -408,7 +429,7 @@ protected void increaseAmount(Address sender, BigInteger value, BigInteger unloc
this.depositFor(sender, value, unlockTime, locked, INCREASE_LOCK_AMOUNT);
}

protected void onKick(Address user) {
protected void onKick(String user) {
try {
Context.call(getDividends(), "onKick", user);
} catch (Exception ignored) {
Expand All @@ -421,7 +442,7 @@ protected void onKick(Address user) {

}

protected void onBalanceUpdate(Address user, BigInteger newBalance) {
protected void onBalanceUpdate(String user, BigInteger newBalance) {
try {
Context.call(getRewards(), "onBalanceUpdate", user, newBalance);
} catch (Exception ignored) {
Expand Down
Loading

0 comments on commit 826c64e

Please sign in to comment.