From db53648d7be00c357387e64383cb78f272e6d86c Mon Sep 17 00:00:00 2001 From: sagar sapkota Date: Tue, 26 Nov 2024 11:01:46 +0545 Subject: [PATCH] RouteData update to add data property, unit tests added for usdc staking --- .../score/core/router/RouterImpl.java | 13 +++- .../score/core/router/RouterTest.java | 67 ++++++++++++++----- .../score/core/savings/SavingsImpl.java | 3 - .../score/lib/interfaces/Savings.java | 1 - .../balanced/score/lib/structs/RouteData.java | 7 +- .../balanced/score/lib/utils/Versions.java | 4 +- 6 files changed, 68 insertions(+), 27 deletions(-) diff --git a/core-contracts/Router/src/main/java/network/balanced/score/core/router/RouterImpl.java b/core-contracts/Router/src/main/java/network/balanced/score/core/router/RouterImpl.java index a321f6f53..6f67d8f1b 100644 --- a/core-contracts/Router/src/main/java/network/balanced/score/core/router/RouterImpl.java +++ b/core-contracts/Router/src/main/java/network/balanced/score/core/router/RouterImpl.java @@ -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) { @@ -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 diff --git a/core-contracts/Router/src/test/java/network/balanced/score/core/router/RouterTest.java b/core-contracts/Router/src/test/java/network/balanced/score/core/router/RouterTest.java index dcd5823fd..dc21c299d 100644 --- a/core-contracts/Router/src/test/java/network/balanced/score/core/router/RouterTest.java +++ b/core-contracts/Router/src/test/java/network/balanced/score/core/router/RouterTest.java @@ -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; @@ -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; @@ -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", @@ -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, @@ -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, @@ -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 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); + } + } diff --git a/core-contracts/Savings/src/main/java/network/balanced/score/core/savings/SavingsImpl.java b/core-contracts/Savings/src/main/java/network/balanced/score/core/savings/SavingsImpl.java index 838ba26be..ef5d7149b 100644 --- a/core-contracts/Savings/src/main/java/network/balanced/score/core/savings/SavingsImpl.java +++ b/core-contracts/Savings/src/main/java/network/balanced/score/core/savings/SavingsImpl.java @@ -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; @@ -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 totalPayout = Context.newDictDB("TOTAL_PAYOUT", BigInteger.class); private final VarDB currentVersion = Context.newVarDB(VERSION, String.class); - public static final String TAG = Names.SAVINGS; public SavingsImpl(Address _governance) { BalancedAddressManager.setGovernance(_governance); diff --git a/score-lib/src/main/java/network/balanced/score/lib/interfaces/Savings.java b/score-lib/src/main/java/network/balanced/score/lib/interfaces/Savings.java index a92a41635..8fc7e6875 100644 --- a/score-lib/src/main/java/network/balanced/score/lib/interfaces/Savings.java +++ b/score-lib/src/main/java/network/balanced/score/lib/interfaces/Savings.java @@ -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; diff --git a/score-lib/src/main/java/network/balanced/score/lib/structs/RouteData.java b/score-lib/src/main/java/network/balanced/score/lib/structs/RouteData.java index 8b3194b2d..1c07e7c44 100644 --- a/score-lib/src/main/java/network/balanced/score/lib/structs/RouteData.java +++ b/score-lib/src/main/java/network/balanced/score/lib/structs/RouteData.java @@ -12,13 +12,16 @@ public class RouteData { public String method; public String receiver; public BigInteger minimumReceive; + public byte[] data; public List actions; + public RouteData(){} - public RouteData(String method, String receiver, BigInteger minimumReceive, List actions) { + public RouteData(String method, String receiver, BigInteger minimumReceive, List actions, byte[] data) { this.method = method; this.receiver = receiver; this.minimumReceive = minimumReceive; + this.data = data; this.actions = actions; } @@ -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); @@ -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); } diff --git a/score-lib/src/main/java/network/balanced/score/lib/utils/Versions.java b/score-lib/src/main/java/network/balanced/score/lib/utils/Versions.java index 6b40dc0c8..24bc3744f 100644 --- a/score-lib/src/main/java/network/balanced/score/lib/utils/Versions.java +++ b/score-lib/src/main/java/network/balanced/score/lib/utils/Versions.java @@ -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"; @@ -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";