Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core-contracts/Bribing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies {
implementation Dependencies.javaeeScorex
implementation Dependencies.minimalJson
implementation project(':score-lib')
implementation 'xyz.venture23:xcall-lib:2.1.0'

testImplementation Dependencies.javaeeUnitTest
testImplementation Dependencies.javaeeTokens
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package network.balanced.score.core.bribing;

import network.balanced.score.lib.utils.Versions;
import foundation.icon.xcall.NetworkAddress;
import network.balanced.score.lib.interfaces.BribingXCall;
import network.balanced.score.lib.interfaces.GovernanceXCall;
import network.balanced.score.lib.utils.*;
import score.*;
import score.annotation.External;

Expand All @@ -29,7 +32,9 @@
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import score.annotation.Optional;

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.MICRO_SECONDS_IN_A_DAY;
import static network.balanced.score.lib.utils.Constants.EXA;
Expand All @@ -51,7 +56,9 @@
//Source->bribeToken->period
public static final BranchDB<String, DictDB<Address, BigInteger>> activePeriod = Context.newBranchDB("activePeriod", BigInteger.class);
//userAddress->Source->bribeToken->timeOfLastClaim
public static final BranchDB<Address, BranchDB<String, DictDB<Address, BigInteger>>> lastUserClaim = Context.newBranchDB("lastUserClaim", BigInteger.class);
//public static final BranchDB<Address, BranchDB<String, DictDB<Address, BigInteger>>> lastUserClaim = Context.newBranchDB("lastUserClaim", BigInteger.class);
public static final NetworkAddressBranchedStringBranchDictDB<String, Address, BigInteger> lastUserClaim = new NetworkAddressBranchedStringBranchDictDB<>("lastUserClaim", BigInteger.class);


public static final BranchDB<String, ArrayDB<Address>> bribesPerSource = Context.newBranchDB("bribesPerSource", Address.class);
public static final BranchDB<Address, ArrayDB<String>> sourcesPerBribe = Context.newBranchDB("sourcesPerBribe", String.class);
Expand All @@ -61,7 +68,9 @@
private final VarDB<String> currentVersion = Context.newVarDB("version", String.class);
private final VarDB<BigInteger> migrationPeriod = Context.newVarDB("migration_period", BigInteger.class);

private class SourceStatus {
public static String NATIVE_NID;

private static class SourceStatus {
BigInteger period;
BigInteger bribesPerToken;
}
Expand All @@ -78,6 +87,7 @@
Context.revert("Can't Update same version of code");
}
this.currentVersion.set(Versions.BRIBING);
NATIVE_NID = Context.call(String.class, getXCall(), "getNetworkId");
}

@External(readonly=true)
Expand Down Expand Up @@ -148,7 +158,14 @@

@External(readonly=true)
public BigInteger claimable(Address user, String source, Address bribeToken) {
return getBribesAmount(user, source, bribeToken, true);
String networkAddress = new NetworkAddress(NATIVE_NID, user).toString();
return getBribesAmount(networkAddress, source, bribeToken, true);
}

@External(readonly=true)
public BigInteger xClaimable(String user, String source, Address bribeToken) {
String networkAddress = NetworkAddress.valueOf(user, NATIVE_NID).toString();
return getBribesAmount(networkAddress, source, bribeToken, true);

Check warning on line 168 in core-contracts/Bribing/src/main/java/network/balanced/score/core/bribing/BribingImpl.java

View check run for this annotation

Codecov / codecov/patch

core-contracts/Bribing/src/main/java/network/balanced/score/core/bribing/BribingImpl.java#L167-L168

Added lines #L167 - L168 were not covered by tests
}

@External
Expand All @@ -159,13 +176,30 @@
@External
public void claimBribe(String source, Address bribeToken) {
Address user = Context.getCaller();
BigInteger amount = getBribesAmount(user, source, bribeToken, false);
Context.require(amount.compareTo(BigInteger.ZERO) > 0, user.toString() + " has no bribe in " + bribeToken.toString() + " to claim for source: " + source);
String networkAddress = new NetworkAddress(NATIVE_NID, user).toString();
BigInteger amount = getBribesAmount(networkAddress, source, bribeToken, false);
Context.require(amount.compareTo(BigInteger.ZERO) > 0, user + " has no bribe in " + bribeToken.toString() + " to claim for source: " + source);
BigInteger prevClaims = claimsPerSource.at(source).getOrDefault(bribeToken, BigInteger.ZERO);
claimsPerSource.at(source).set(bribeToken, prevClaims.add(amount));
Context.call(bribeToken, "transfer", user, amount, new byte[0]);
}

@External
public void handleCallMessage(String _from, byte[] _data, @Optional String[] _protocols) {
Check.checkStatus();
only(getXCall());
XCallUtils.verifyXCallProtocols(_from, _protocols);
BribingXCall.process(this, _from, _data);
}

Check warning on line 193 in core-contracts/Bribing/src/main/java/network/balanced/score/core/bribing/BribingImpl.java

View check run for this annotation

Codecov / codecov/patch

core-contracts/Bribing/src/main/java/network/balanced/score/core/bribing/BribingImpl.java#L189-L193

Added lines #L189 - L193 were not covered by tests

public void xClaimTo(String from, String source, Address bribeToken) {
BigInteger amount = getBribesAmount(from, source, bribeToken, false);

Check warning on line 196 in core-contracts/Bribing/src/main/java/network/balanced/score/core/bribing/BribingImpl.java

View check run for this annotation

Codecov / codecov/patch

core-contracts/Bribing/src/main/java/network/balanced/score/core/bribing/BribingImpl.java#L196

Added line #L196 was not covered by tests
Context.require(amount.compareTo(BigInteger.ZERO) > 0, from + " has no bribe in " + bribeToken.toString() + " to claim for source: " + source);
BigInteger prevClaims = claimsPerSource.at(source).getOrDefault(bribeToken, BigInteger.ZERO);
claimsPerSource.at(source).set(bribeToken, prevClaims.add(amount));
TokenTransfer.transfer(bribeToken, from, amount);
}

Check warning on line 201 in core-contracts/Bribing/src/main/java/network/balanced/score/core/bribing/BribingImpl.java

View check run for this annotation

Codecov / codecov/patch

core-contracts/Bribing/src/main/java/network/balanced/score/core/bribing/BribingImpl.java#L198-L201

Added lines #L198 - L201 were not covered by tests

@External
public void tokenFallback(Address _from, BigInteger _value, byte[] _data) {
Address token = Context.getCaller();
Expand Down Expand Up @@ -219,27 +253,26 @@
add(source, bribeToken);
}

private BigInteger getBribesAmount(Address user, String source, Address bribeToken, boolean readonly) {
private BigInteger getBribesAmount(String user, String source, Address bribeToken, boolean readonly) {
SourceStatus status = updateSource(source, bribeToken, readonly);
DictDB<Address, BigInteger> lastUserClaim = BribingImpl.lastUserClaim.at(user).at(source);
if (lastUserClaim.getOrDefault(bribeToken, BigInteger.ZERO).compareTo(status.period) >= 0) {
NetworkAddress userNetworkAddress = NetworkAddress.valueOf(user, NATIVE_NID);
BigInteger lastUserClaimAmount = BribingImpl.lastUserClaim.getOrDefault(userNetworkAddress, source, bribeToken, BigInteger.ZERO);
if (lastUserClaimAmount.compareTo(status.period) >= 0) {
return BigInteger.ZERO;
}

if (!readonly) {
lastUserClaim.set(bribeToken, status.period);
lastUserClaim.set(userNetworkAddress, source, bribeToken, status.period);
}

BigInteger lastVote = Context.call(BigInteger.class, rewards.get(), "getLastUserVote", user, source);
BigInteger lastVote = Context.call(BigInteger.class, rewards.get(), "getLastUserVoteV2", user, source);

if (lastVote.compareTo(status.period) >= 0) {
return BigInteger.ZERO;
}

BigInteger bias = calculateUserBias(user, source);
BigInteger amount = bias.multiply(status.bribesPerToken).divide(EXA);

return amount;
return bias.multiply(status.bribesPerToken).divide(EXA);
}

private SourceStatus updateSource(String source, Address bribeToken, boolean readOnly) {
Expand Down Expand Up @@ -298,12 +331,12 @@
}


public Map<String, BigInteger> getUserSlope(Address user, String source) {
Map<String, BigInteger> userSlope = (Map<String, BigInteger>)Context.call(rewards.get(), "getUserSlope", user, source);
public Map<String, BigInteger> getUserSlope(String user, String source) {
Map<String, BigInteger> userSlope = (Map<String, BigInteger>)Context.call(rewards.get(), "getUserSlopeV2", user, source);

Check warning on line 335 in core-contracts/Bribing/src/main/java/network/balanced/score/core/bribing/BribingImpl.java

View check run for this annotation

Codecov / codecov/patch

core-contracts/Bribing/src/main/java/network/balanced/score/core/bribing/BribingImpl.java#L335

Added line #L335 was not covered by tests
return userSlope;
}

public BigInteger calculateUserBias(Address user, String source) {
public BigInteger calculateUserBias(String user, String source) {
BigInteger period = getCurrentPeriod();
Map<String, BigInteger> userSlope = getUserSlope(user, source);
BigInteger end = userSlope.get("end");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.math.BigInteger;
import java.util.Map;

import foundation.icon.xcall.NetworkAddress;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
Expand Down Expand Up @@ -55,9 +56,9 @@ public void addReward() {
// Act
addBribe(source, amount);

when(rewards.mock.getLastUserVote(user.getAddress(), source)).thenReturn(startPeriod.add(BigInteger.ONE));
when(rewards.mock.getLastUserVoteV2(new NetworkAddress(NATIVE_NID, user.getAddress()).toString(), source)).thenReturn(startPeriod.add(BigInteger.ONE));

doReturn(userWeight).when(bribingSpy).calculateUserBias(user.getAddress(), source);
doReturn(userWeight).when(bribingSpy).calculateUserBias(new NetworkAddress(NATIVE_NID, user.getAddress()).toString(), source);

assertEquals(BigInteger.ZERO, bribing.call("claimable", user.getAddress(), source, bribeToken.getAddress()));

Expand Down Expand Up @@ -103,8 +104,8 @@ public void claimBribe() {
addBribe(source, amount);

// Assert
when(rewards.mock.getLastUserVote(user.getAddress(), source)).thenReturn(startPeriod.subtract(BigInteger.ONE));
doReturn(userWeight).when(bribingSpy).calculateUserBias(user.getAddress(), source);
when(rewards.mock.getLastUserVoteV2(new NetworkAddress(NATIVE_NID, user.getAddress()).toString(), source)).thenReturn(startPeriod.subtract(BigInteger.ONE));
doReturn(userWeight).when(bribingSpy).calculateUserBias(new NetworkAddress(NATIVE_NID, user.getAddress()).toString(), source);

sm.getBlock().increase(WEEK);
doReturn(totalWeight).when(bribingSpy).getSourceBias(source, getPeriod());
Expand All @@ -131,8 +132,8 @@ public void claimBribe_carryUnclaimed() {
addBribe(source, amount);

// Assert
when(rewards.mock.getLastUserVote(user.getAddress(), source)).thenReturn(startPeriod.add(BigInteger.ONE));
doReturn(userWeight).when(bribingSpy).calculateUserBias(user.getAddress(), source);
when(rewards.mock.getLastUserVoteV2(new NetworkAddress(NATIVE_NID, user.getAddress()).toString(), source)).thenReturn(startPeriod.add(BigInteger.ONE));
doReturn(userWeight).when(bribingSpy).calculateUserBias(new NetworkAddress(NATIVE_NID, user.getAddress()).toString(), source);

sm.getBlock().increase(WEEK);
doReturn(totalWeight).when(bribingSpy).getSourceBias(source, getPeriod());
Expand Down Expand Up @@ -168,8 +169,8 @@ public void claimBribe_twice() {
// Act
addBribe(source, amount);

when(rewards.mock.getLastUserVote(user.getAddress(), source)).thenReturn(startPeriod.subtract(BigInteger.ONE));
doReturn(userWeight).when(bribingSpy).calculateUserBias(user.getAddress(), source);
when(rewards.mock.getLastUserVoteV2(new NetworkAddress(NATIVE_NID, user.getAddress()).toString(), source)).thenReturn(startPeriod.subtract(BigInteger.ONE));
doReturn(userWeight).when(bribingSpy).calculateUserBias(new NetworkAddress(NATIVE_NID, user.getAddress()).toString(), source);

sm.getBlock().increase(WEEK);
doReturn(totalWeight).when(bribingSpy).getSourceBias(source, getPeriod());;
Expand Down Expand Up @@ -210,10 +211,10 @@ public void scheduledBribes() {
scheduledBribes(source, total, amounts);

// Assert
when(rewards.mock.getLastUserVote(user1.getAddress(), source)).thenReturn(startPeriod.subtract(BigInteger.ONE));
doReturn(userWeight).when(bribingSpy).calculateUserBias(user1.getAddress(), source);
when(rewards.mock.getLastUserVote(user2.getAddress(), source)).thenReturn(startPeriod.subtract(BigInteger.ONE));
doReturn(userWeight).when(bribingSpy).calculateUserBias(user2.getAddress(), source);
when(rewards.mock.getLastUserVoteV2(new NetworkAddress(NATIVE_NID, user1.getAddress()).toString(), source)).thenReturn(startPeriod.subtract(BigInteger.ONE));
doReturn(userWeight).when(bribingSpy).calculateUserBias(new NetworkAddress(NATIVE_NID, user1.getAddress()).toString(), source);
when(rewards.mock.getLastUserVoteV2(new NetworkAddress(NATIVE_NID, user2.getAddress()).toString(), source)).thenReturn(startPeriod.subtract(BigInteger.ONE));
doReturn(userWeight).when(bribingSpy).calculateUserBias(new NetworkAddress(NATIVE_NID, user2.getAddress()).toString(), source);

for (int i = 0; i < 4; i++) {
sm.getBlock().increase(WEEK);
Expand Down Expand Up @@ -286,10 +287,10 @@ public void liveDataTest() {
"slope", new BigInteger("2c9bb4a", 16)
);

doReturn(user1Slope).when(bribingSpy).getUserSlope(user1.getAddress(), source);
doReturn(user2Slope).when(bribingSpy).getUserSlope(user2.getAddress(), source);
when(rewards.mock.getLastUserVote(user1.getAddress(), source)).thenReturn(startPeriod.subtract(BigInteger.ONE));
when(rewards.mock.getLastUserVote(user2.getAddress(), source)).thenReturn(startPeriod.subtract(BigInteger.ONE));
doReturn(user1Slope).when(bribingSpy).getUserSlope(new NetworkAddress(NATIVE_NID, user1.getAddress()).toString(), source);
doReturn(user2Slope).when(bribingSpy).getUserSlope(new NetworkAddress(NATIVE_NID, user2.getAddress()).toString(), source);
when(rewards.mock.getLastUserVoteV2(new NetworkAddress(NATIVE_NID, user1.getAddress()).toString(), source)).thenReturn(startPeriod.subtract(BigInteger.ONE));
when(rewards.mock.getLastUserVoteV2(new NetworkAddress(NATIVE_NID, user2.getAddress()).toString(), source)).thenReturn(startPeriod.subtract(BigInteger.ONE));

// Act
doReturn(period.subtract(WEEK_IN_MS)).when(bribingSpy).getBlockTime();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
import network.balanced.score.lib.interfaces.tokens.*;
import network.balanced.score.lib.structs.BalancedAddresses;
import network.balanced.score.lib.test.UnitTest;
import network.balanced.score.lib.test.integration.Balanced;
import network.balanced.score.lib.test.mock.MockBalanced;
import network.balanced.score.lib.test.mock.MockContract;
import network.balanced.score.lib.utils.BalancedAddressManager;
import network.balanced.score.lib.utils.Names;
import score.Context;

import java.math.BigInteger;
import java.util.Map;
Expand All @@ -51,13 +56,22 @@ class BribingImplTestBase extends UnitTest {

protected MockContract<Rewards> rewards;
protected MockContract<IRC2> bribeToken;
protected MockContract<XCall> xCall;
protected MockContract<Governance> governance;
private MockBalanced balanced;

protected Score bribing;
protected BribingImpl bribingSpy;

public final String NATIVE_NID = "0x1.ICON";

protected void setupBase() throws Exception {
rewards = new MockContract<>(RewardsScoreInterface.class, sm, owner);
balanced = new MockBalanced(sm, owner);
rewards = balanced.rewards;
governance = balanced.governance;
bribeToken = new MockContract<>(IRC2ScoreInterface.class, sm, owner);
xCall = balanced.xCall;
when(xCall.mock.getNetworkId()).thenReturn(NATIVE_NID);
bribing = sm.deploy(owner, BribingImpl.class, rewards.getAddress());

bribingSpy = (BribingImpl) spy(bribing.getInstance());
Expand Down
1 change: 0 additions & 1 deletion core-contracts/Dex/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ dependencies {
testImplementation Dependencies.mockitoCore
testImplementation Dependencies.mockitoInline


testImplementation project(':test-lib')
testImplementation Dependencies.junitJupiter
testRuntimeOnly Dependencies.junitJupiterEngine
Expand Down
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
1 change: 1 addition & 0 deletions core-contracts/Dividends/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies {
implementation Dependencies.javaeeScorex
implementation Dependencies.minimalJson
implementation project(':score-lib')
implementation 'xyz.venture23:xcall-lib:2.1.0'

testImplementation Dependencies.javaeeUnitTest
testImplementation Dependencies.javaeeTokens
Expand Down
Loading
Loading