|
15 | 15 | import network.icon.intent.utils.SwapOrderData;
|
16 | 16 | import score.Context;
|
17 | 17 |
|
| 18 | +import org.json.JSONObject; |
18 | 19 | import org.junit.jupiter.api.BeforeEach;
|
19 | 20 | import org.junit.jupiter.api.Test;
|
20 | 21 | import score.UserRevertedException;
|
21 | 22 |
|
22 | 23 | import java.math.BigInteger;
|
| 24 | +import java.nio.charset.StandardCharsets; |
23 | 25 |
|
24 | 26 | import static java.math.BigInteger.TEN;
|
25 | 27 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
@@ -630,4 +632,47 @@ void testSetProtocolFeeNonAdmin() {
|
630 | 632 | assertEquals("Reverted(0): Not Owner", exception.getMessage());
|
631 | 633 | }
|
632 | 634 |
|
| 635 | + @Test |
| 636 | + public void testTokenFallback() { |
| 637 | + when(token.tokenContract.mock.balanceOf(user1.getAddress())).thenReturn(totalSupply); |
| 638 | + |
| 639 | + SwapOrder swapOrder = new SwapOrder(BigInteger.valueOf(1), intent.getAddress().toString(), srcNid, |
| 640 | + destinationNetwork, user1.getAddress().toString(), user2.getAddress().toString(), |
| 641 | + token.tokenContract.getAddress().toString(), amount, toToken, toAmount, data); |
| 642 | + |
| 643 | + String depositor = user1.getAddress().toString(); |
| 644 | + byte[] swapOrderDataBytes = swapOrder.toBytes(); |
| 645 | + |
| 646 | + JSONObject jsonObject = new JSONObject(); |
| 647 | + jsonObject.put("depositor", depositor); |
| 648 | + jsonObject.put("token", token.tokenContract.getAddress().toString()); |
| 649 | + jsonObject.put("amount", amount); |
| 650 | + jsonObject.put("swapOrderDataBytes", bytesToHex(swapOrderDataBytes).toString()); |
| 651 | + |
| 652 | + byte[] finalData = jsonObjectToByteArray(jsonObject); |
| 653 | + |
| 654 | + intent.invoke(user1, "tokenFallback", user1.getAddress(), amount, finalData); |
| 655 | + |
| 656 | + BigInteger depositedAmount = (BigInteger) intent.call("getDepositAmount", |
| 657 | + user1.getAddress().toString(), |
| 658 | + token.tokenContract.getAddress().toString()); |
| 659 | + assertEquals(amount, depositedAmount); |
| 660 | + } |
| 661 | + |
| 662 | + public static String bytesToHex(byte[] bytes) { |
| 663 | + StringBuilder hexString = new StringBuilder(2 * bytes.length); |
| 664 | + for (byte b : bytes) { |
| 665 | + String hex = Integer.toHexString(0xff & b); |
| 666 | + if (hex.length() == 1) { |
| 667 | + hexString.append('0'); |
| 668 | + } |
| 669 | + hexString.append(hex); |
| 670 | + } |
| 671 | + return hexString.toString(); |
| 672 | + } |
| 673 | + |
| 674 | + public static byte[] jsonObjectToByteArray(JSONObject jsonObject) { |
| 675 | + String jsonString = jsonObject.toString(); |
| 676 | + return jsonString.getBytes(StandardCharsets.UTF_8); |
| 677 | + } |
633 | 678 | }
|
0 commit comments