Skip to content

Commit

Permalink
RouteData update to add data property, unit tests added for usdc staking
Browse files Browse the repository at this point in the history
  • Loading branch information
sagars committed Nov 26, 2024
1 parent 1f487ca commit db53648
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,11 @@ private void executeRoute(String _from, byte[] data) {
} else {
receiver = _from;
}

route(receiver, fromToken, routeData.actions, minimumReceive, EMPTY_DATA);
byte[] _data = EMPTY_DATA;
if(routeData.data!=null){
_data = routeData.data;
}
route(receiver, fromToken, routeData.actions, minimumReceive, _data);
}

private void jsonRoute(String _from, byte[] data) {
Expand Down Expand Up @@ -308,7 +311,11 @@ private void jsonRoute(String _from, byte[] data) {
}

Address fromToken = Context.getCaller();
route(receiver, fromToken, actions, minimumReceive, EMPTY_DATA);
byte[] _data = EMPTY_DATA;
if(params.get("data")!=null){
_data = params.get("data").asString().getBytes();
}
route(receiver, fromToken, actions, minimumReceive, _data);
}

@Payable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@
import network.balanced.score.lib.structs.RouteData;
import network.balanced.score.lib.test.mock.MockBalanced;
import network.balanced.score.lib.test.mock.MockContract;
import network.balanced.score.lib.tokens.HubTokenImpl;
import network.balanced.score.lib.tokens.IRC2Base;
import network.balanced.score.lib.tokens.IRC2Mintable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import score.Address;
import score.Context;
import scorex.util.ArrayList;

import java.math.BigInteger;
Expand All @@ -44,11 +40,8 @@

import static network.balanced.score.core.router.RouterImpl.*;
import static network.balanced.score.lib.test.UnitTest.*;
import static network.balanced.score.lib.utils.Constants.EXA;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -182,14 +175,7 @@ void tokenFallback() throws Exception {
when(balanced.baln.mock.balanceOf(routerScore.getAddress())).thenReturn(BigInteger.TEN);
when(balanced.sicx.mock.balanceOf(routerScore.getAddress())).thenReturn(BigInteger.TEN);

// byte[] invalidPathWithSicxTerminalToken = tokenData("_swap", Map.of("path",
// new Object[]{balanced.baln.getAddress().toString(), null}));
// Executable nonSicxIcxTrade = () -> routerScore.invoke(sicxScore.account, "tokenFallback", owner.getAddress(),
// BigInteger.TEN, invalidPathWithSicxTerminalToken);
// expectedErrorMessage = "Reverted(0): " + TAG + ": Native swaps not available to icon from " + balanced.baln.getAddress();
// expectErrorMessage(nonSicxIcxTrade, expectedErrorMessage);
//
// resetInRoute();

Account newReceiver = sm.createAccount();
byte[] pathWithSicxTerminalToken = tokenData("_swap", Map.of("path",
new Object[]{sicxScore.getAddress().toString(), null}, "receiver",
Expand Down Expand Up @@ -501,7 +487,7 @@ void tokenFallback_swapStable() throws Exception {
}

Account newReceiver = sm.createAccount();
byte[] data = new RouteData("_swap", newReceiver.getAddress().toString(), BigInteger.ZERO, actions).toBytes();
byte[] data = new RouteData("_swap", newReceiver.getAddress().toString(), BigInteger.ZERO, actions, null).toBytes();

// Act
routerScore.invoke(balanced.baln.account, "tokenFallback", owner.getAddress(), balnToSwap,
Expand Down Expand Up @@ -531,7 +517,7 @@ void tokenFallback_swapToICX() throws Exception {
routerScore.getAccount().addBalance("ICX", ICXResult);

Account newReceiver = sm.createAccount();
byte[] data = new RouteData("_swap", newReceiver.getAddress().toString(), BigInteger.ZERO, actions).toBytes();
byte[] data = new RouteData("_swap", newReceiver.getAddress().toString(), BigInteger.ZERO, actions, null).toBytes();

// Act
routerScore.invoke(balanced.baln.account, "tokenFallback", owner.getAddress(), USDToSwap,
Expand Down Expand Up @@ -586,4 +572,51 @@ private void resetInRoute() {
// in Production this happens between each tx
((RouterImpl)routerScore.getInstance()).inRoute = false;
}

@Test
void stakeToSavingsWithJSONRoute(){
// Arrange
//baln as usdc
when(balanced.baln.mock.balanceOf(routerScore.getAddress())).thenReturn(BigInteger.TEN);
when(balanced.bnUSD.mock.balanceOf(routerScore.getAddress())).thenReturn(BigInteger.TEN);

String data = new String(tokenData("_lock", Map.of()));
Account newReceiver = balanced.savings.account;
byte[] pathWithUSDCBnUSD = tokenData("_swap", Map.of("path",
new Object[]{balanced.bnUSD.getAddress().toString()}, "receiver",
newReceiver.getAddress().toString(), "data", data));

// Act
routerScore.invoke(balanced.baln.account, "tokenFallback", owner.getAddress(), BigInteger.TEN,
pathWithUSDCBnUSD);


// Verify
verify(balanced.bnUSD.mock).transfer(balanced.savings.getAddress(), BigInteger.TEN, data.getBytes());
}

@Test
void stakeToSavingsWithRLPData(){
// Arrange
//baln as usdc
BigInteger balnToSwap = BigInteger.TEN.multiply(ICX);
when(balanced.baln.mock.balanceOf(routerScore.getAddress())).thenReturn(balnToSwap);
when(balanced.bnUSD.mock.balanceOf(routerScore.getAddress())).thenReturn(balnToSwap);

List<RouteAction> actions = new ArrayList<>(1);
actions.add(new RouteAction(SWAP, balanced.bnUSD.getAddress()));

byte[] data = tokenData("_lock", Map.of());
Account newReceiver = balanced.savings.account;
byte[] routeData = new RouteData("_swap", newReceiver.getAddress().toString(), BigInteger.ZERO, actions, data).toBytes();

// Act
routerScore.invoke(balanced.baln.account, "tokenFallback", owner.getAddress(), balnToSwap,
routeData);


// Verify
verify(balanced.bnUSD.mock).transfer(balanced.savings.getAddress(), balnToSwap, data);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import static network.balanced.score.lib.utils.BalancedAddressManager.resetAddress;
import static network.balanced.score.lib.utils.Check.checkStatus;
import static network.balanced.score.lib.utils.Check.onlyGovernance;
import static network.balanced.score.lib.utils.Constants.EXA;

import java.math.BigInteger;
import java.util.Map;
Expand All @@ -45,12 +44,10 @@
import score.annotation.External;

public class SavingsImpl extends FloorLimited implements Savings {
public static final String LOCKED_SAVINGS = "Locked savings";
public static final String VERSION = "version";
private static final DictDB<Address, BigInteger> totalPayout = Context.newDictDB("TOTAL_PAYOUT", BigInteger.class);

private final VarDB<String> currentVersion = Context.newVarDB(VERSION, String.class);
public static final String TAG = Names.SAVINGS;

public SavingsImpl(Address _governance) {
BalancedAddressManager.setGovernance(_governance);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import network.balanced.score.lib.interfaces.addresses.AddressManager;
import network.balanced.score.lib.interfaces.base.Name;
import network.balanced.score.lib.interfaces.base.Version;
import network.balanced.score.lib.interfaces.tokens.XTokenReceiver;
import score.annotation.External;
import score.Address;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@ public class RouteData {
public String method;
public String receiver;
public BigInteger minimumReceive;
public byte[] data;
public List<RouteAction> actions;

public RouteData(){}

public RouteData(String method, String receiver, BigInteger minimumReceive, List<RouteAction> actions) {
public RouteData(String method, String receiver, BigInteger minimumReceive, List<RouteAction> actions, byte[] data) {
this.method = method;
this.receiver = receiver;
this.minimumReceive = minimumReceive;
this.data = data;
this.actions = actions;
}

Expand All @@ -34,6 +37,7 @@ public static RouteData readObject(ObjectReader reader) {
obj.method = reader.readString();
obj.receiver = reader.readNullable(String.class);
obj.minimumReceive = reader.readNullable((BigInteger.class));
obj.data = reader.readNullable(byte[].class);
while (reader.hasNext()) {
RouteAction data = reader.read(RouteAction.class);
actions.add(data);
Expand All @@ -48,6 +52,7 @@ public static void writeObject(ObjectWriter w, RouteData obj) {
w.write(obj.method);
w.writeNullable(obj.receiver);
w.writeNullable(obj.minimumReceive);
w.writeNullable(obj.data);
for (RouteAction action : obj.actions) {
w.write(action);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class Versions {
public final static String DEX = "v1.1.8";
public final static String GOVERNANCE = "v1.0.2";
public final static String REBALANCING = "v1.0.0";
public final static String ROUTER = "v1.1.8";
public final static String ROUTER = "v1.1.9";
public final static String STAKEDLP = "v1.0.8";
public final static String BOOSTED_BALN = "v1.1.0";
public final static String BRIBING = "v1.0.1";
Expand All @@ -42,7 +42,7 @@ public class Versions {
public final static String BALANCED_ASSET_MANAGER = "v1.0.6";
public final static String XCALL_MANAGER = "v1.0.2";
public final static String BURNER = "v1.0.0";
public final static String SAVINGS = "v1.0.0";
public final static String SAVINGS = "v1.0.1";
public final static String TRICKLER = "v1.0.0";

public final static String SPOKE_ASSET_MANAGER = "v1.0.2";
Expand Down

0 comments on commit db53648

Please sign in to comment.