|
28 | 28 | import network.balanced.score.lib.structs.RouteData; |
29 | 29 | import network.balanced.score.lib.test.mock.MockBalanced; |
30 | 30 | import network.balanced.score.lib.test.mock.MockContract; |
31 | | -import network.balanced.score.lib.tokens.HubTokenImpl; |
32 | | -import network.balanced.score.lib.tokens.IRC2Base; |
33 | | -import network.balanced.score.lib.tokens.IRC2Mintable; |
34 | 31 | import org.junit.jupiter.api.BeforeEach; |
35 | 32 | import org.junit.jupiter.api.Test; |
36 | 33 | import org.junit.jupiter.api.function.Executable; |
37 | 34 | import score.Address; |
38 | | -import score.Context; |
39 | 35 | import scorex.util.ArrayList; |
40 | 36 |
|
41 | 37 | import java.math.BigInteger; |
|
44 | 40 |
|
45 | 41 | import static network.balanced.score.core.router.RouterImpl.*; |
46 | 42 | import static network.balanced.score.lib.test.UnitTest.*; |
47 | | -import static network.balanced.score.lib.utils.Constants.EXA; |
48 | 43 | import static org.junit.jupiter.api.Assertions.assertEquals; |
49 | 44 | import static org.junit.jupiter.api.Assertions.assertThrows; |
50 | | -import static org.mockito.ArgumentMatchers.any; |
51 | | -import static org.mockito.ArgumentMatchers.eq; |
52 | 45 | import static org.mockito.Mockito.verify; |
53 | 46 | import static org.mockito.Mockito.when; |
54 | 47 |
|
@@ -182,14 +175,7 @@ void tokenFallback() throws Exception { |
182 | 175 | when(balanced.baln.mock.balanceOf(routerScore.getAddress())).thenReturn(BigInteger.TEN); |
183 | 176 | when(balanced.sicx.mock.balanceOf(routerScore.getAddress())).thenReturn(BigInteger.TEN); |
184 | 177 |
|
185 | | -// byte[] invalidPathWithSicxTerminalToken = tokenData("_swap", Map.of("path", |
186 | | -// new Object[]{balanced.baln.getAddress().toString(), null})); |
187 | | -// Executable nonSicxIcxTrade = () -> routerScore.invoke(sicxScore.account, "tokenFallback", owner.getAddress(), |
188 | | -// BigInteger.TEN, invalidPathWithSicxTerminalToken); |
189 | | -// expectedErrorMessage = "Reverted(0): " + TAG + ": Native swaps not available to icon from " + balanced.baln.getAddress(); |
190 | | -// expectErrorMessage(nonSicxIcxTrade, expectedErrorMessage); |
191 | | -// |
192 | | -// resetInRoute(); |
| 178 | + |
193 | 179 | Account newReceiver = sm.createAccount(); |
194 | 180 | byte[] pathWithSicxTerminalToken = tokenData("_swap", Map.of("path", |
195 | 181 | new Object[]{sicxScore.getAddress().toString(), null}, "receiver", |
@@ -501,7 +487,42 @@ void tokenFallback_swapStable() throws Exception { |
501 | 487 | } |
502 | 488 |
|
503 | 489 | Account newReceiver = sm.createAccount(); |
504 | | - byte[] data = new RouteData("_swap", newReceiver.getAddress().toString(), BigInteger.ZERO, actions).toBytes(); |
| 490 | + byte[] data = new RouteData("_swap", newReceiver.getAddress().toString(), BigInteger.ZERO, actions, null).toBytes(); |
| 491 | + |
| 492 | + // Act |
| 493 | + routerScore.invoke(balanced.baln.account, "tokenFallback", owner.getAddress(), balnToSwap, |
| 494 | + data); |
| 495 | + |
| 496 | + // Assert |
| 497 | + int i = 0; |
| 498 | + for (MockContract<IRC2> token : tokens) { |
| 499 | + if (i < tokens.size() - 1) { |
| 500 | + byte[] d = tokens.get(i + 1).getAddress().toString().getBytes(); |
| 501 | + verify(token.mock).transfer(balanced.stability.getAddress(), balnToSwap, d); |
| 502 | + } |
| 503 | + i++; |
| 504 | + } |
| 505 | + } |
| 506 | + |
| 507 | + @Test |
| 508 | + void tokenFallback_swapStableOldRouteData() throws Exception { |
| 509 | + // Arrange |
| 510 | + BigInteger balnToSwap = BigInteger.TEN.multiply(ICX); |
| 511 | + List<RouteAction> actions = new ArrayList<>(MAX_NUMBER_OF_ITERATIONS); |
| 512 | + List<MockContract<IRC2>> tokens = new ArrayList<>(MAX_NUMBER_OF_ITERATIONS - 1); |
| 513 | + for (int i = 0; i < MAX_NUMBER_OF_ITERATIONS; i++) { |
| 514 | + if (i == 0) { |
| 515 | + actions.add(new RouteAction(SWAP, balanced.sicx.getAddress())); |
| 516 | + continue; |
| 517 | + } |
| 518 | + MockContract<IRC2> token = new MockContract<>(IRC2ScoreInterface.class, IRC2.class, sm, owner); |
| 519 | + when(token.mock.balanceOf(routerScore.getAddress())).thenReturn(balnToSwap); |
| 520 | + actions.add(new RouteAction(STABILITY_SWAP, token.getAddress())); |
| 521 | + tokens.add(token); |
| 522 | + } |
| 523 | + |
| 524 | + Account newReceiver = sm.createAccount(); |
| 525 | + byte[] data = new RouteData("_swap", newReceiver.getAddress().toString(), BigInteger.ZERO, actions, null).toBytesOld(); |
505 | 526 |
|
506 | 527 | // Act |
507 | 528 | routerScore.invoke(balanced.baln.account, "tokenFallback", owner.getAddress(), balnToSwap, |
@@ -531,7 +552,30 @@ void tokenFallback_swapToICX() throws Exception { |
531 | 552 | routerScore.getAccount().addBalance("ICX", ICXResult); |
532 | 553 |
|
533 | 554 | Account newReceiver = sm.createAccount(); |
534 | | - byte[] data = new RouteData("_swap", newReceiver.getAddress().toString(), BigInteger.ZERO, actions).toBytes(); |
| 555 | + byte[] data = new RouteData("_swap", newReceiver.getAddress().toString(), BigInteger.ZERO, actions, null).toBytes(); |
| 556 | + |
| 557 | + // Act |
| 558 | + routerScore.invoke(balanced.baln.account, "tokenFallback", owner.getAddress(), USDToSwap, |
| 559 | + data); |
| 560 | + |
| 561 | + // Assert |
| 562 | + assertEquals(ICXResult, newReceiver.getBalance()); |
| 563 | + } |
| 564 | + |
| 565 | + @Test |
| 566 | + void tokenFallback_swapToICXOldRouteData() throws Exception { |
| 567 | + // Arrange |
| 568 | + BigInteger USDToSwap = BigInteger.TEN.multiply(ICX); |
| 569 | + BigInteger sICXResult = BigInteger.valueOf(100).multiply(ICX); |
| 570 | + BigInteger ICXResult = BigInteger.valueOf(110).multiply(ICX); |
| 571 | + List<RouteAction> actions = new ArrayList<>(2); |
| 572 | + actions.add(new RouteAction(SWAP, balanced.sicx.getAddress())); |
| 573 | + actions.add(new RouteAction(SWAP, null)); |
| 574 | + when(balanced.sicx.mock.balanceOf(routerScore.getAddress())).thenReturn(sICXResult); |
| 575 | + routerScore.getAccount().addBalance("ICX", ICXResult); |
| 576 | + |
| 577 | + Account newReceiver = sm.createAccount(); |
| 578 | + byte[] data = new RouteData("_swap", newReceiver.getAddress().toString(), BigInteger.ZERO, actions, null).toBytesOld(); |
535 | 579 |
|
536 | 580 | // Act |
537 | 581 | routerScore.invoke(balanced.baln.account, "tokenFallback", owner.getAddress(), USDToSwap, |
@@ -586,4 +630,51 @@ private void resetInRoute() { |
586 | 630 | // in Production this happens between each tx |
587 | 631 | ((RouterImpl)routerScore.getInstance()).inRoute = false; |
588 | 632 | } |
| 633 | + |
| 634 | + @Test |
| 635 | + void stakeToSavingsWithJSONRoute(){ |
| 636 | + // Arrange |
| 637 | + //baln as usdc |
| 638 | + when(balanced.baln.mock.balanceOf(routerScore.getAddress())).thenReturn(BigInteger.TEN); |
| 639 | + when(balanced.bnUSD.mock.balanceOf(routerScore.getAddress())).thenReturn(BigInteger.TEN); |
| 640 | + |
| 641 | + String data = new String(tokenData("_lock", Map.of())); |
| 642 | + Account newReceiver = balanced.savings.account; |
| 643 | + byte[] pathWithUSDCBnUSD = tokenData("_swap", Map.of("path", |
| 644 | + new Object[]{balanced.bnUSD.getAddress().toString()}, "receiver", |
| 645 | + newReceiver.getAddress().toString(), "data", data)); |
| 646 | + |
| 647 | + // Act |
| 648 | + routerScore.invoke(balanced.baln.account, "tokenFallback", owner.getAddress(), BigInteger.TEN, |
| 649 | + pathWithUSDCBnUSD); |
| 650 | + |
| 651 | + |
| 652 | + // Verify |
| 653 | + verify(balanced.bnUSD.mock).transfer(balanced.savings.getAddress(), BigInteger.TEN, data.getBytes()); |
| 654 | + } |
| 655 | + |
| 656 | + @Test |
| 657 | + void stakeToSavingsWithRLPData(){ |
| 658 | + // Arrange |
| 659 | + //baln as usdc |
| 660 | + BigInteger balnToSwap = BigInteger.TEN.multiply(ICX); |
| 661 | + when(balanced.baln.mock.balanceOf(routerScore.getAddress())).thenReturn(balnToSwap); |
| 662 | + when(balanced.bnUSD.mock.balanceOf(routerScore.getAddress())).thenReturn(balnToSwap); |
| 663 | + |
| 664 | + List<RouteAction> actions = new ArrayList<>(1); |
| 665 | + actions.add(new RouteAction(SWAP, balanced.bnUSD.getAddress())); |
| 666 | + |
| 667 | + byte[] data = tokenData("_lock", Map.of()); |
| 668 | + Account newReceiver = balanced.savings.account; |
| 669 | + byte[] routeData = new RouteData("_swap", newReceiver.getAddress().toString(), BigInteger.ZERO, actions, data).toBytes(); |
| 670 | + |
| 671 | + // Act |
| 672 | + routerScore.invoke(balanced.baln.account, "tokenFallback", owner.getAddress(), balnToSwap, |
| 673 | + routeData); |
| 674 | + |
| 675 | + |
| 676 | + // Verify |
| 677 | + verify(balanced.bnUSD.mock).transfer(balanced.savings.getAddress(), balnToSwap, data); |
| 678 | + } |
| 679 | + |
589 | 680 | } |
0 commit comments