Skip to content

Commit 33513d2

Browse files
committed
x methods added
1 parent 826c64e commit 33513d2

File tree

5 files changed

+88
-34
lines changed

5 files changed

+88
-34
lines changed

score-lib/src/main/java/network/balanced/score/lib/interfaces/BoostedBaln.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import foundation.icon.score.client.ScoreClient;
2020
import foundation.icon.score.client.ScoreInterface;
21+
import network.balanced.score.lib.annotations.XCall;
2122
import network.balanced.score.lib.interfaces.addresses.AddressManager;
2223
import network.balanced.score.lib.interfaces.base.TokenFallback;
2324
import network.balanced.score.lib.interfaces.base.Version;
@@ -71,6 +72,9 @@ public interface BoostedBaln extends AddressManager, TokenFallback, Version {
7172
@External
7273
void increaseUnlockTime(BigInteger unlockTime);
7374

75+
@XCall
76+
void xIncreaseUnlockTime(String from, BigInteger unlockTime);
77+
7478
@External
7579
void kick(Address user);
7680

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

87+
@XCall
88+
void xKick(String from);
89+
90+
@XCall
91+
void xWithdrawEarly(String from);
92+
93+
@XCall
94+
void xWithdraw(String from);
95+
96+
@XCall
97+
void checkpoint(String from);
98+
8399
@External(readonly = true)
84100
BigInteger balanceOf(Address _owner, @Optional BigInteger timestamp);
85101

score-lib/src/main/java/network/balanced/score/lib/tokens/HubTokenImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package network.balanced.score.lib.tokens;
1818

1919
import network.balanced.score.lib.interfaces.tokens.HubToken;
20+
import network.balanced.score.lib.interfaces.tokens.HubTokenMessages;
2021
import network.balanced.score.lib.interfaces.tokens.HubTokenXCall;
2122
import network.balanced.score.lib.utils.BalancedAddressManager;
2223
import network.balanced.score.lib.utils.XCallUtils;
@@ -28,7 +29,6 @@
2829
import score.annotation.External;
2930
import score.annotation.Optional;
3031
import score.annotation.Payable;
31-
import network.balanced.score.lib.interfaces.tokens.HubTokenMessages;
3232
import foundation.icon.xcall.NetworkAddress;
3333

3434
import java.math.BigInteger;

score-lib/src/main/java/network/balanced/score/lib/utils/AddressBranchDictDB.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
public class AddressBranchDictDB<K, V> {
1111

12-
private BranchDB<Address, DictDB<K, V>> legacyAddressDictDB;
13-
private BranchDB<String, DictDB<K, V>> addressDictDB;
12+
private final BranchDB<Address, DictDB<K, V>> legacyAddressDictDB;
13+
private final BranchDB<String, DictDB<K, V>> addressDictDB;
1414

1515
public AddressBranchDictDB(String id, Class<V> valueClass) {
1616
this.legacyAddressDictDB = Context.newBranchDB(id, valueClass);

token-contracts/bBaln/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ repositories {
2727

2828
dependencies {
2929
compileOnly Dependencies.javaeeApi
30-
implementation project(':score-lib')
3130
implementation Dependencies.javaeeScorex
3231
implementation Dependencies.minimalJson
3332
implementation Dependencies.javaeeTokens
33+
implementation project(':score-lib')
3434
implementation 'xyz.venture23:xcall-lib:2.1.0'
3535

3636
testImplementation Dependencies.javaeeUnitTest

token-contracts/bBaln/src/main/java/network/balanced/score/tokens/BoostedBalnImpl.java

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
import com.eclipsesource.json.Json;
2020
import com.eclipsesource.json.JsonObject;
2121
import foundation.icon.xcall.NetworkAddress;
22+
import network.balanced.score.lib.interfaces.BoostedBalnXCall;
2223
import network.balanced.score.lib.utils.Names;
2324
import network.balanced.score.lib.utils.TokenTransfer;
2425
import network.balanced.score.lib.utils.Versions;
26+
import network.balanced.score.lib.utils.XCallUtils;
2527
import network.balanced.score.tokens.db.LockedBalance;
2628
import network.balanced.score.tokens.db.Point;
2729
import network.balanced.score.tokens.utils.UnsignedBigInteger;
@@ -32,14 +34,13 @@
3234
import score.annotation.Optional;
3335
import scorex.util.ArrayList;
3436

35-
import java.lang.annotation.Native;
3637
import java.math.BigInteger;
3738
import java.util.List;
3839
import java.util.Map;
3940

40-
import static network.balanced.score.lib.utils.Check.onlyOwner;
41-
import static network.balanced.score.lib.utils.Check.checkStatus;
4241
import 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.*;
4344
import static network.balanced.score.lib.utils.Constants.EOA_ZERO;
4445
import static network.balanced.score.lib.utils.Math.convertToNumber;
4546
import 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

Comments
 (0)