From 33513d21c5da6f05d56885e240b9b305b23160f1 Mon Sep 17 00:00:00 2001 From: sagar sapkota Date: Thu, 12 Dec 2024 22:31:35 +0545 Subject: [PATCH] x methods added --- .../score/lib/interfaces/BoostedBaln.java | 16 +++ .../score/lib/tokens/HubTokenImpl.java | 2 +- .../score/lib/utils/AddressBranchDictDB.java | 4 +- token-contracts/bBaln/build.gradle | 2 +- .../score/tokens/BoostedBalnImpl.java | 98 +++++++++++++------ 5 files changed, 88 insertions(+), 34 deletions(-) diff --git a/score-lib/src/main/java/network/balanced/score/lib/interfaces/BoostedBaln.java b/score-lib/src/main/java/network/balanced/score/lib/interfaces/BoostedBaln.java index 7c991717f..cae6be37d 100644 --- a/score-lib/src/main/java/network/balanced/score/lib/interfaces/BoostedBaln.java +++ b/score-lib/src/main/java/network/balanced/score/lib/interfaces/BoostedBaln.java @@ -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; @@ -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); @@ -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); diff --git a/score-lib/src/main/java/network/balanced/score/lib/tokens/HubTokenImpl.java b/score-lib/src/main/java/network/balanced/score/lib/tokens/HubTokenImpl.java index 56ce4d3a4..4591f5aea 100644 --- a/score-lib/src/main/java/network/balanced/score/lib/tokens/HubTokenImpl.java +++ b/score-lib/src/main/java/network/balanced/score/lib/tokens/HubTokenImpl.java @@ -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; @@ -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; diff --git a/score-lib/src/main/java/network/balanced/score/lib/utils/AddressBranchDictDB.java b/score-lib/src/main/java/network/balanced/score/lib/utils/AddressBranchDictDB.java index c2d097f23..dcb0828e7 100644 --- a/score-lib/src/main/java/network/balanced/score/lib/utils/AddressBranchDictDB.java +++ b/score-lib/src/main/java/network/balanced/score/lib/utils/AddressBranchDictDB.java @@ -9,8 +9,8 @@ public class AddressBranchDictDB { - private BranchDB> legacyAddressDictDB; - private BranchDB> addressDictDB; + private final BranchDB> legacyAddressDictDB; + private final BranchDB> addressDictDB; public AddressBranchDictDB(String id, Class valueClass) { this.legacyAddressDictDB = Context.newBranchDB(id, valueClass); diff --git a/token-contracts/bBaln/build.gradle b/token-contracts/bBaln/build.gradle index ffef403ad..4b6f8bd56 100644 --- a/token-contracts/bBaln/build.gradle +++ b/token-contracts/bBaln/build.gradle @@ -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 diff --git a/token-contracts/bBaln/src/main/java/network/balanced/score/tokens/BoostedBalnImpl.java b/token-contracts/bBaln/src/main/java/network/balanced/score/tokens/BoostedBalnImpl.java index 89ebfe0c4..894c999d1 100644 --- a/token-contracts/bBaln/src/main/java/network/balanced/score/tokens/BoostedBalnImpl.java +++ b/token-contracts/bBaln/src/main/java/network/balanced/score/tokens/BoostedBalnImpl.java @@ -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; @@ -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; @@ -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(); @@ -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); @@ -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 { @@ -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,