Skip to content

Commit

Permalink
optional data on route methods - Router
Browse files Browse the repository at this point in the history
  • Loading branch information
sagars committed Nov 22, 2024
1 parent 68d6cbf commit a00b28c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private void swapDefault(Address fromToken, Address toToken) {
}
}

private void route(String from, Address startToken, List<RouteAction> _path, BigInteger _minReceive) {
private void route(String from, Address startToken, List<RouteAction> _path, BigInteger _minReceive, byte[] data) {
Address prevToken = null;
Address currentToken = startToken;
BigInteger fromAmount;
Expand Down Expand Up @@ -167,10 +167,6 @@ private void route(String from, Address startToken, List<RouteAction> _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);
Expand All @@ -179,7 +175,7 @@ private void route(String from, Address startToken, List<RouteAction> _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);

Expand All @@ -190,20 +186,20 @@ 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<RouteAction> actions = Route.fromBytes(_path).actions;
validateRoutePayload(actions.size(), _minReceive);
if (_receiver == null || _receiver.equals("")) {
_receiver = Context.getCaller().toString();
}

route(_receiver, null, actions, _minReceive);
route(_receiver, null, actions, _minReceive, _data);
}

private void validateRoutePayload(int _pathLength, BigInteger _minReceive) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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();
Expand All @@ -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);

Expand All @@ -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);
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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";
Expand All @@ -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;
Expand All @@ -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 "
Expand All @@ -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";
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

0 comments on commit a00b28c

Please sign in to comment.