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 a653f04c0..a321f6f53 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 @@ -131,7 +131,7 @@ private void swapDefault(Address fromToken, Address toToken) { } } - private void route(String from, Address startToken, List _path, BigInteger _minReceive) { + private void route(String from, Address startToken, List _path, BigInteger _minReceive, byte[] data) { Address prevToken = null; Address currentToken = startToken; BigInteger fromAmount; @@ -167,10 +167,6 @@ private void route(String from, Address startToken, List _path, Big BigInteger balance = (BigInteger) Context.call(currentToken, "balanceOf", Context.getAddress()); Context.require(balance.compareTo(_minReceive) >= 0, TAG + ": Below minimum receive amount of " + _minReceive); - byte[] data = new byte[0]; - if (networkAddress.net().equals(nativeNid)) { - data = EMPTY_DATA; - } TokenTransfer.transfer(currentToken, networkAddress.toString(), balance, data); Route(fromAddress, fromAmount, currentToken, balance); @@ -179,7 +175,7 @@ private void route(String from, Address startToken, List _path, Big @Payable @External - public void route(Address[] _path, @Optional BigInteger _minReceive, @Optional String _receiver) { + public void route(Address[] _path, @Optional BigInteger _minReceive, @Optional String _receiver, @Optional byte[] _data) { Context.require(!inRoute); validateRoutePayload(_path.length, _minReceive); @@ -190,12 +186,12 @@ public void route(Address[] _path, @Optional BigInteger _minReceive, @Optional S for (Address path : _path) { routeActions.add(new RouteAction(1, path)); } - route(_receiver, null, routeActions, _minReceive); + route(_receiver, null, routeActions, _minReceive, _data); } @Payable @External - public void routeV2(byte[] _path, @Optional BigInteger _minReceive, @Optional String _receiver) { + public void routeV2(byte[] _path, @Optional BigInteger _minReceive, @Optional String _receiver, @Optional byte[] _data) { Context.require(!inRoute); List actions = Route.fromBytes(_path).actions; validateRoutePayload(actions.size(), _minReceive); @@ -203,7 +199,7 @@ public void routeV2(byte[] _path, @Optional BigInteger _minReceive, @Optional St _receiver = Context.getCaller().toString(); } - route(_receiver, null, actions, _minReceive); + route(_receiver, null, actions, _minReceive, _data); } private void validateRoutePayload(int _pathLength, BigInteger _minReceive) { @@ -266,7 +262,7 @@ private void executeRoute(String _from, byte[] data) { receiver = _from; } - route(receiver, fromToken, routeData.actions, minimumReceive); + route(receiver, fromToken, routeData.actions, minimumReceive, EMPTY_DATA); } private void jsonRoute(String _from, byte[] data) { @@ -312,7 +308,7 @@ private void jsonRoute(String _from, byte[] data) { } Address fromToken = Context.getCaller(); - route(receiver, fromToken, actions, minimumReceive); + route(receiver, fromToken, actions, minimumReceive, EMPTY_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 dd172ab2e..dcd5823fd 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 @@ -80,7 +80,7 @@ void route() { Account balnToken = Account.newScoreAccount(scoreCount++); Address[] pathWithNonSicx = new Address[]{balnToken.getAddress()}; Executable nonSicxTrade = () -> sm.call(owner, icxToTrade, routerScore.getAddress(), "route", pathWithNonSicx, - BigInteger.ZERO, ""); + BigInteger.ZERO, "", "None".getBytes()); String expectedErrorMessage = "Reverted(0): " + TAG + ": ICX can only be traded for sICX"; expectErrorMessage(nonSicxTrade, expectedErrorMessage); @@ -90,7 +90,7 @@ void route() { } Executable maxTradeHops = () -> sm.call(owner, icxToTrade, routerScore.getAddress(), "route", - pathWithMoreHops, BigInteger.ZERO, ""); + pathWithMoreHops, BigInteger.ZERO, "", "None".getBytes()); expectedErrorMessage = "Reverted(0): " + TAG + ": Passed max swaps of " + MAX_NUMBER_OF_ITERATIONS; resetInRoute(); @@ -101,7 +101,7 @@ void route() { routerScore.getAccount().addBalance("ICX", icxToTrade); Executable lessThanMinimumReceivable = () -> sm.call(owner, icxToTrade, routerScore.getAddress(), "route", - path, icxToTrade.multiply(BigInteger.TWO), ""); + path, icxToTrade.multiply(BigInteger.TWO), "", "None".getBytes()); expectedErrorMessage = "Reverted(0): " + TAG + ": Below minimum receive amount of " + icxToTrade.multiply(BigInteger.TWO); @@ -111,11 +111,11 @@ void route() { routerScore.getAccount().addBalance("ICX", icxToTrade); resetInRoute(); sm.call(owner, icxToTrade, routerScore.getAddress(), "route", path, BigInteger.ZERO, - owner.getAddress().toString()); + owner.getAddress().toString(), "None".getBytes()); verify(sicxScore.mock).transfer(owner.getAddress(), icxToTrade, EMPTY_DATA); Executable negativeMinimumBalance = () -> sm.call(owner, icxToTrade, routerScore.getAddress(), - "route", path, icxToTrade.negate(), ""); + "route", path, icxToTrade.negate(), "", "None".getBytes()); expectedErrorMessage = "Reverted(0): " + TAG + ": Must specify a positive number for minimum to receive"; expectErrorMessage(negativeMinimumBalance, expectedErrorMessage); } @@ -243,7 +243,7 @@ void xTrade_WithdrawNotAllowed() throws Exception { routerScore.invoke(balanced.sicx.account, "tokenFallback", owner.getAddress(), amount, path); // Assert - verify(token.mock).hubTransfer(receiver.toString(), amount, new byte[0]); + verify(token.mock).hubTransfer(receiver.toString(), amount, "None".getBytes()); } @Test @@ -268,7 +268,7 @@ void xTrade_ToNetworkAddressWithDifferentNet() throws Exception { routerScore.invoke(balanced.sicx.account, "tokenFallback", owner.getAddress(), amount, path); // Assert - verify(token.mock).crossTransfer(receiver.toString(), amount, new byte[0]); + verify(token.mock).crossTransfer(receiver.toString(), amount, "None".getBytes()); } @Test @@ -289,7 +289,7 @@ void xTrade_ToBnUSD() throws Exception { routerScore.invoke(balanced.sicx.account, "tokenFallback", owner.getAddress(), amount, path); // Assert - verify(balanced.bnUSD.mock).crossTransfer(receiver.toString(), amount, new byte[0]); + verify(balanced.bnUSD.mock).crossTransfer(receiver.toString(), amount, "None".getBytes()); } @Test @@ -382,7 +382,7 @@ void routeV2_swap_icxSicx() { // Act Executable nonSicxTrade = () -> sm.call(owner, icxToTrade, routerScore.getAddress(), "routeV2", pathWithNonSicx, - BigInteger.ZERO, ""); + BigInteger.ZERO, "", "none".getBytes()); // Assert String expectedErrorMessage = "Reverted(0): " + TAG + ": ICX can only be traded for sICX"; @@ -402,7 +402,7 @@ void routeV2_swap_pathWithMoreHops() { // Act Executable maxTradeHops = () -> sm.call(owner, icxToTrade, routerScore.getAddress(), "routeV2", - pathWithMoreHops, BigInteger.ZERO, ""); + pathWithMoreHops, BigInteger.ZERO, "", "None".getBytes()); // Assert String expectedErrorMessage = "Reverted(0): " + TAG + ": Passed max swaps of " + MAX_NUMBER_OF_ITERATIONS; @@ -422,7 +422,7 @@ void routeV2_swap_belowMinimumReceive() { // Act Executable lessThanMinimumReceivable = () -> sm.call(owner, icxToTrade, routerScore.getAddress(), "routeV2", - path, icxToTrade.multiply(BigInteger.TWO), ""); + path, icxToTrade.multiply(BigInteger.TWO), "", "None".getBytes()); //Assert String expectedErrorMessage = "Reverted(0): " + TAG + ": Below minimum receive amount of " @@ -441,7 +441,7 @@ void routeV2_swap_positiveMinimumReceive() { // Act Executable negativeMinimumBalance = () -> sm.call(owner, icxToTrade, routerScore.getAddress(), - "routeV2", path, icxToTrade.negate(), ""); + "routeV2", path, icxToTrade.negate(), "", "None".getBytes()); // Assert String expectedErrorMessage = "Reverted(0): " + TAG + ": Must specify a positive number for minimum to receive"; @@ -470,7 +470,7 @@ void routeV2_swapStable() throws Exception { // Act sm.call(owner, icxToTrade, routerScore.getAddress(), "routeV2", - pathWithMoreHops, BigInteger.ZERO, ""); + pathWithMoreHops, BigInteger.ZERO, "", "None".getBytes()); // Assert int i = 0; diff --git a/score-lib/src/main/java/network/balanced/score/lib/interfaces/Router.java b/score-lib/src/main/java/network/balanced/score/lib/interfaces/Router.java index 654e43ccc..91b58b723 100644 --- a/score-lib/src/main/java/network/balanced/score/lib/interfaces/Router.java +++ b/score-lib/src/main/java/network/balanced/score/lib/interfaces/Router.java @@ -37,5 +37,9 @@ public interface Router extends Name, AddressManager, @Payable @External - void route(Address[] path, @Optional BigInteger _minReceive, @Optional String _receiver); + void route(Address[] path, @Optional BigInteger _minReceive, @Optional String _receiver, @Optional byte[] _data); + + @Payable + @External + void routeV2(byte[] _path, @Optional BigInteger _minReceive, @Optional String _receiver, @Optional byte[] _data); }