1919import com .eclipsesource .json .Json ;
2020import com .eclipsesource .json .JsonObject ;
2121import foundation .icon .xcall .NetworkAddress ;
22+ import network .balanced .score .lib .interfaces .BoostedBalnXCall ;
2223import network .balanced .score .lib .utils .Names ;
2324import network .balanced .score .lib .utils .TokenTransfer ;
2425import network .balanced .score .lib .utils .Versions ;
26+ import network .balanced .score .lib .utils .XCallUtils ;
2527import network .balanced .score .tokens .db .LockedBalance ;
2628import network .balanced .score .tokens .db .Point ;
2729import network .balanced .score .tokens .utils .UnsignedBigInteger ;
3234import score .annotation .Optional ;
3335import scorex .util .ArrayList ;
3436
35- import java .lang .annotation .Native ;
3637import java .math .BigInteger ;
3738import java .util .List ;
3839import java .util .Map ;
3940
40- import static network .balanced .score .lib .utils .Check .onlyOwner ;
41- import static network .balanced .score .lib .utils .Check .checkStatus ;
4241import static network .balanced .score .lib .utils .BalancedAddressManager .getBaln ;
42+ import static network .balanced .score .lib .utils .BalancedAddressManager .getXCall ;
43+ import static network .balanced .score .lib .utils .Check .*;
4344import static network .balanced .score .lib .utils .Constants .EOA_ZERO ;
4445import static network .balanced .score .lib .utils .Math .convertToNumber ;
4546import static network .balanced .score .lib .utils .NonReentrant .globalReentryLock ;
@@ -169,6 +170,11 @@ public void checkpoint() {
169170 this .checkpoint (getStringNetworkAddress (EOA_ZERO ), new LockedBalance (), new LockedBalance ());
170171 }
171172
173+ public void checkpoint (String from ) {
174+ checkStatus ();
175+ this .checkpoint (getStringNetworkAddress (EOA_ZERO ), new LockedBalance (), new LockedBalance ());
176+ }
177+
172178 @ External
173179 public void xTokenFallback (String _from , BigInteger _value , byte [] _data ) {
174180 checkStatus ();
@@ -229,15 +235,20 @@ public void tokenFallback(Address _from, BigInteger _value, byte[] _data) {
229235 }
230236 }
231237
232- //todo: crosschain method also required
233238 @ External
234239 public void increaseUnlockTime (BigInteger unlockTime ) {
240+ increaseUnlockTimeInternal (getStringNetworkAddress (Context .getCaller ()), unlockTime );
241+ }
242+
243+ public void xIncreaseUnlockTime (String from , BigInteger unlockTime ){
244+ increaseUnlockTimeInternal (from , unlockTime );
245+ }
246+
247+ private void increaseUnlockTimeInternal (String stingSender , BigInteger unlockTime ){
235248 checkStatus ();
236249 globalReentryLock ();
237- Address sender = Context .getCaller ();
238250 BigInteger blockTimestamp = BigInteger .valueOf (Context .getBlockTimestamp ());
239251
240- String stingSender = getStringNetworkAddress (sender );
241252 LockedBalance locked = getLockedBalance (stingSender );
242253 unlockTime = unlockTime .divide (WEEK_IN_MICRO_SECONDS ).multiply (WEEK_IN_MICRO_SECONDS );
243254
@@ -253,9 +264,16 @@ public void increaseUnlockTime(BigInteger unlockTime) {
253264
254265 @ External
255266 public void kick (Address user ) {
267+ kickInternal (getStringNetworkAddress (user ));
268+ }
269+
270+ public void xKick (String from ){
271+ kickInternal (from );
272+ }
273+
274+ private void kickInternal (String stringUser ){
256275 checkStatus ();
257- String stringUser = getStringNetworkAddress (user );
258- BigInteger bBalnBalance = balanceOf (user , BigInteger .ZERO );
276+ BigInteger bBalnBalance = xBalanceOf (stringUser , BigInteger .ZERO );
259277 if (bBalnBalance .equals (BigInteger .ZERO )) {
260278 onKick (stringUser );
261279 } else {
@@ -265,60 +283,80 @@ public void kick(Address user) {
265283
266284 @ External
267285 public void withdraw () {
286+ withdrawInternal (getStringNetworkAddress (Context .getCaller ()));
287+ }
288+
289+
290+ private void withdrawInternal (String senderAddress ){
268291 checkStatus ();
269292 globalReentryLock ();
270- Address sender = Context .getCaller ();
271293 BigInteger blockTimestamp = BigInteger .valueOf (Context .getBlockTimestamp ());
272- String senderAddress = getStringNetworkAddress (sender );
273- LockedBalance locked = getLockedBalance (senderAddress );
274- Context .require (blockTimestamp .compareTo (locked .getEnd ()) >= 0 , "Withdraw: The lock haven't expire" );
275- BigInteger value = locked .amount ;
294+ LockedBalance balanceLocked = getLockedBalance (senderAddress );
295+ Context .require (blockTimestamp .compareTo (balanceLocked .getEnd ()) >= 0 , "Withdraw: The lock haven't expire" );
296+ BigInteger value = balanceLocked .amount ;
276297
277- LockedBalance oldLocked = locked .newLockedBalance ();
278- locked .end = UnsignedBigInteger .ZERO ;
279- locked .amount = BigInteger .ZERO ;
298+ LockedBalance oldLocked = balanceLocked .newLockedBalance ();
299+ balanceLocked .end = UnsignedBigInteger .ZERO ;
300+ balanceLocked .amount = BigInteger .ZERO ;
280301
281- this . locked .set (senderAddress , locked );
302+ locked .set (senderAddress , balanceLocked );
282303 BigInteger supplyBefore = this .supply .get ();
283304 this .supply .set (supplyBefore .subtract (value ));
284305
285- this .checkpoint (senderAddress , oldLocked , locked );
306+ this .checkpoint (senderAddress , oldLocked , balanceLocked );
286307
287- //Context.call(getBaln(), "transfer", sender, value, "withdraw".getBytes());
288308 TokenTransfer .transfer (getBaln (), senderAddress , value , "withdraw" .getBytes ());
289309
290310 users .remove (senderAddress );
291- Withdraw ( sender , value , blockTimestamp );
311+ WithdrawV2 ( senderAddress , value , blockTimestamp );
292312 Supply (supplyBefore , supplyBefore .subtract (value ));
293313 onKick (senderAddress );
294314 }
295315
296- //todo: crosschain method also required
316+ @ External
317+ public void handleCallMessage (String _from , byte [] _data , @ Optional String [] _protocols ) {
318+ checkStatus ();
319+ only (getXCall ());
320+ XCallUtils .verifyXCallProtocols (_from , _protocols );
321+ BoostedBalnXCall .process (this , _from , _data );
322+ }
323+
324+ public void xWithdrawEarly (String _from ) {
325+ withdrawEarlyInternal (_from );
326+ }
327+
328+ public void xWithdraw (String _from ) {
329+ xWithdraw (_from );
330+ }
331+
297332 @ External
298333 public void withdrawEarly () {
334+ withdrawEarlyInternal (getStringNetworkAddress (Context .getCaller ()));
335+ }
336+
337+ private void withdrawEarlyInternal (String senderAddress ){
299338 checkStatus ();
300339 globalReentryLock ();
301340 Address sender = Context .getCaller ();
302- String senderAddress = getStringNetworkAddress (sender );
303341 BigInteger blockTimestamp = BigInteger .valueOf (Context .getBlockTimestamp ());
304342
305- LockedBalance locked = getLockedBalance (senderAddress );
306- Context .require (blockTimestamp .compareTo (locked .getEnd ()) < 0 , "Withdraw: The lock has expired, use withdraw " +
343+ LockedBalance lockedBalance = getLockedBalance (senderAddress );
344+ Context .require (blockTimestamp .compareTo (lockedBalance .getEnd ()) < 0 , "Withdraw: The lock has expired, use withdraw " +
307345 "method" );
308- BigInteger value = locked .amount ;
346+ BigInteger value = lockedBalance .amount ;
309347 BigInteger maxPenalty = value .divide (BigInteger .TWO );
310348 BigInteger variablePenalty = balanceOf (sender , null );
311349 BigInteger penaltyAmount = variablePenalty .min (maxPenalty );
312350 BigInteger returnAmount = value .subtract (penaltyAmount );
313351
314- LockedBalance oldLocked = locked .newLockedBalance ();
315- locked .end = UnsignedBigInteger .ZERO ;
316- locked .amount = BigInteger .ZERO ;
317- this . locked .set (senderAddress , locked );
352+ LockedBalance oldLocked = lockedBalance .newLockedBalance ();
353+ lockedBalance .end = UnsignedBigInteger .ZERO ;
354+ lockedBalance .amount = BigInteger .ZERO ;
355+ locked .set (senderAddress , lockedBalance );
318356 BigInteger supplyBefore = this .supply .get ();
319357 this .supply .set (supplyBefore .subtract (value ));
320358
321- this .checkpoint (senderAddress , oldLocked , locked );
359+ this .checkpoint (senderAddress , oldLocked , lockedBalance );
322360
323361
324362 Context .call (getBaln (), "transfer" , this .penaltyAddress .get (), penaltyAmount ,
0 commit comments