Skip to content

Commit d157fa2

Browse files
committed
feat: tokenFallback test added
1 parent 092f8dc commit d157fa2

File tree

1 file changed

+45
-0
lines changed
  • contracts/javascore/Intent_Contracts/app/src/test/java/intent_contracts

1 file changed

+45
-0
lines changed

contracts/javascore/Intent_Contracts/app/src/test/java/intent_contracts/IntentTest.java

+45
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
import network.icon.intent.utils.SwapOrderData;
1616
import score.Context;
1717

18+
import org.json.JSONObject;
1819
import org.junit.jupiter.api.BeforeEach;
1920
import org.junit.jupiter.api.Test;
2021
import score.UserRevertedException;
2122

2223
import java.math.BigInteger;
24+
import java.nio.charset.StandardCharsets;
2325

2426
import static java.math.BigInteger.TEN;
2527
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -630,4 +632,47 @@ void testSetProtocolFeeNonAdmin() {
630632
assertEquals("Reverted(0): Not Owner", exception.getMessage());
631633
}
632634

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+
}
633678
}

0 commit comments

Comments
 (0)