Skip to content

Commit

Permalink
x methods added
Browse files Browse the repository at this point in the history
  • Loading branch information
sagars committed Dec 12, 2024
1 parent 826c64e commit 33513d2
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import foundation.icon.score.client.ScoreClient;
import foundation.icon.score.client.ScoreInterface;
import network.balanced.score.lib.annotations.XCall;
import network.balanced.score.lib.interfaces.addresses.AddressManager;
import network.balanced.score.lib.interfaces.base.TokenFallback;
import network.balanced.score.lib.interfaces.base.Version;
Expand Down Expand Up @@ -71,6 +72,9 @@ public interface BoostedBaln extends AddressManager, TokenFallback, Version {
@External
void increaseUnlockTime(BigInteger unlockTime);

@XCall
void xIncreaseUnlockTime(String from, BigInteger unlockTime);

@External
void kick(Address user);

Expand All @@ -80,6 +84,18 @@ public interface BoostedBaln extends AddressManager, TokenFallback, Version {
@External
void withdrawEarly();

@XCall
void xKick(String from);

@XCall
void xWithdrawEarly(String from);

@XCall
void xWithdraw(String from);

@XCall
void checkpoint(String from);

@External(readonly = true)
BigInteger balanceOf(Address _owner, @Optional BigInteger timestamp);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package network.balanced.score.lib.tokens;

import network.balanced.score.lib.interfaces.tokens.HubToken;
import network.balanced.score.lib.interfaces.tokens.HubTokenMessages;
import network.balanced.score.lib.interfaces.tokens.HubTokenXCall;
import network.balanced.score.lib.utils.BalancedAddressManager;
import network.balanced.score.lib.utils.XCallUtils;
Expand All @@ -28,7 +29,6 @@
import score.annotation.External;
import score.annotation.Optional;
import score.annotation.Payable;
import network.balanced.score.lib.interfaces.tokens.HubTokenMessages;
import foundation.icon.xcall.NetworkAddress;

import java.math.BigInteger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

public class AddressBranchDictDB<K, V> {

private BranchDB<Address, DictDB<K, V>> legacyAddressDictDB;
private BranchDB<String, DictDB<K, V>> addressDictDB;
private final BranchDB<Address, DictDB<K, V>> legacyAddressDictDB;
private final BranchDB<String, DictDB<K, V>> addressDictDB;

public AddressBranchDictDB(String id, Class<V> valueClass) {
this.legacyAddressDictDB = Context.newBranchDB(id, valueClass);
Expand Down
2 changes: 1 addition & 1 deletion token-contracts/bBaln/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ repositories {

dependencies {
compileOnly Dependencies.javaeeApi
implementation project(':score-lib')
implementation Dependencies.javaeeScorex
implementation Dependencies.minimalJson
implementation Dependencies.javaeeTokens
implementation project(':score-lib')
implementation 'xyz.venture23:xcall-lib:2.1.0'

testImplementation Dependencies.javaeeUnitTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
import foundation.icon.xcall.NetworkAddress;
import network.balanced.score.lib.interfaces.BoostedBalnXCall;
import network.balanced.score.lib.utils.Names;
import network.balanced.score.lib.utils.TokenTransfer;
import network.balanced.score.lib.utils.Versions;
import network.balanced.score.lib.utils.XCallUtils;
import network.balanced.score.tokens.db.LockedBalance;
import network.balanced.score.tokens.db.Point;
import network.balanced.score.tokens.utils.UnsignedBigInteger;
Expand All @@ -32,14 +34,13 @@
import score.annotation.Optional;
import scorex.util.ArrayList;

import java.lang.annotation.Native;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;

import static network.balanced.score.lib.utils.Check.onlyOwner;
import static network.balanced.score.lib.utils.Check.checkStatus;
import static network.balanced.score.lib.utils.BalancedAddressManager.getBaln;
import static network.balanced.score.lib.utils.BalancedAddressManager.getXCall;
import static network.balanced.score.lib.utils.Check.*;
import static network.balanced.score.lib.utils.Constants.EOA_ZERO;
import static network.balanced.score.lib.utils.Math.convertToNumber;
import static network.balanced.score.lib.utils.NonReentrant.globalReentryLock;
Expand Down Expand Up @@ -169,6 +170,11 @@ public void checkpoint() {
this.checkpoint(getStringNetworkAddress(EOA_ZERO), new LockedBalance(), new LockedBalance());
}

public void checkpoint(String from) {
checkStatus();
this.checkpoint(getStringNetworkAddress(EOA_ZERO), new LockedBalance(), new LockedBalance());
}

@External
public void xTokenFallback(String _from, BigInteger _value, byte[] _data) {
checkStatus();
Expand Down Expand Up @@ -229,15 +235,20 @@ public void tokenFallback(Address _from, BigInteger _value, byte[] _data) {
}
}

//todo: crosschain method also required
@External
public void increaseUnlockTime(BigInteger unlockTime) {
increaseUnlockTimeInternal(getStringNetworkAddress(Context.getCaller()), unlockTime);
}

public void xIncreaseUnlockTime(String from, BigInteger unlockTime){
increaseUnlockTimeInternal(from, unlockTime);
}

private void increaseUnlockTimeInternal(String stingSender, BigInteger unlockTime){
checkStatus();
globalReentryLock();
Address sender = Context.getCaller();
BigInteger blockTimestamp = BigInteger.valueOf(Context.getBlockTimestamp());

String stingSender = getStringNetworkAddress(sender);
LockedBalance locked = getLockedBalance(stingSender);
unlockTime = unlockTime.divide(WEEK_IN_MICRO_SECONDS).multiply(WEEK_IN_MICRO_SECONDS);

Expand All @@ -253,9 +264,16 @@ public void increaseUnlockTime(BigInteger unlockTime) {

@External
public void kick(Address user) {
kickInternal(getStringNetworkAddress(user));
}

public void xKick(String from){
kickInternal(from);
}

private void kickInternal(String stringUser){
checkStatus();
String stringUser = getStringNetworkAddress(user);
BigInteger bBalnBalance = balanceOf(user, BigInteger.ZERO);
BigInteger bBalnBalance = xBalanceOf(stringUser, BigInteger.ZERO);
if (bBalnBalance.equals(BigInteger.ZERO)) {
onKick(stringUser);
} else {
Expand All @@ -265,60 +283,80 @@ public void kick(Address user) {

@External
public void withdraw() {
withdrawInternal(getStringNetworkAddress(Context.getCaller()));
}


private void withdrawInternal(String senderAddress){
checkStatus();
globalReentryLock();
Address sender = Context.getCaller();
BigInteger blockTimestamp = BigInteger.valueOf(Context.getBlockTimestamp());
String senderAddress = getStringNetworkAddress(sender);
LockedBalance locked = getLockedBalance(senderAddress);
Context.require(blockTimestamp.compareTo(locked.getEnd()) >= 0, "Withdraw: The lock haven't expire");
BigInteger value = locked.amount;
LockedBalance balanceLocked = getLockedBalance(senderAddress);
Context.require(blockTimestamp.compareTo(balanceLocked.getEnd()) >= 0, "Withdraw: The lock haven't expire");
BigInteger value = balanceLocked.amount;

LockedBalance oldLocked = locked.newLockedBalance();
locked.end = UnsignedBigInteger.ZERO;
locked.amount = BigInteger.ZERO;
LockedBalance oldLocked = balanceLocked.newLockedBalance();
balanceLocked.end = UnsignedBigInteger.ZERO;
balanceLocked.amount = BigInteger.ZERO;

this.locked.set(senderAddress, locked);
locked.set(senderAddress, balanceLocked);
BigInteger supplyBefore = this.supply.get();
this.supply.set(supplyBefore.subtract(value));

this.checkpoint(senderAddress, oldLocked, locked);
this.checkpoint(senderAddress, oldLocked, balanceLocked);

//Context.call(getBaln(), "transfer", sender, value, "withdraw".getBytes());
TokenTransfer.transfer(getBaln(), senderAddress, value, "withdraw".getBytes());

users.remove(senderAddress);
Withdraw(sender, value, blockTimestamp);
WithdrawV2(senderAddress, value, blockTimestamp);
Supply(supplyBefore, supplyBefore.subtract(value));
onKick(senderAddress);
}

//todo: crosschain method also required
@External
public void handleCallMessage(String _from, byte[] _data, @Optional String[] _protocols) {
checkStatus();
only(getXCall());
XCallUtils.verifyXCallProtocols(_from, _protocols);
BoostedBalnXCall.process(this, _from, _data);
}

public void xWithdrawEarly(String _from) {
withdrawEarlyInternal(_from);
}

public void xWithdraw(String _from) {
xWithdraw(_from);
}

@External
public void withdrawEarly() {
withdrawEarlyInternal(getStringNetworkAddress(Context.getCaller()));
}

private void withdrawEarlyInternal(String senderAddress){
checkStatus();
globalReentryLock();
Address sender = Context.getCaller();
String senderAddress = getStringNetworkAddress(sender);
BigInteger blockTimestamp = BigInteger.valueOf(Context.getBlockTimestamp());

LockedBalance locked = getLockedBalance(senderAddress);
Context.require(blockTimestamp.compareTo(locked.getEnd()) < 0, "Withdraw: The lock has expired, use withdraw " +
LockedBalance lockedBalance = getLockedBalance(senderAddress);
Context.require(blockTimestamp.compareTo(lockedBalance.getEnd()) < 0, "Withdraw: The lock has expired, use withdraw " +
"method");
BigInteger value = locked.amount;
BigInteger value = lockedBalance.amount;
BigInteger maxPenalty = value.divide(BigInteger.TWO);
BigInteger variablePenalty = balanceOf(sender, null);
BigInteger penaltyAmount = variablePenalty.min(maxPenalty);
BigInteger returnAmount = value.subtract(penaltyAmount);

LockedBalance oldLocked = locked.newLockedBalance();
locked.end = UnsignedBigInteger.ZERO;
locked.amount = BigInteger.ZERO;
this.locked.set(senderAddress, locked);
LockedBalance oldLocked = lockedBalance.newLockedBalance();
lockedBalance.end = UnsignedBigInteger.ZERO;
lockedBalance.amount = BigInteger.ZERO;
locked.set(senderAddress, lockedBalance);
BigInteger supplyBefore = this.supply.get();
this.supply.set(supplyBefore.subtract(value));

this.checkpoint(senderAddress, oldLocked, locked);
this.checkpoint(senderAddress, oldLocked, lockedBalance);


Context.call(getBaln(), "transfer", this.penaltyAddress.get(), penaltyAmount,
Expand Down

0 comments on commit 33513d2

Please sign in to comment.