|
16 | 16 |
|
17 | 17 | package network.balanced.score.core.dex; |
18 | 18 |
|
| 19 | +import com.eclipsesource.json.JsonObject; |
19 | 20 | import com.eclipsesource.json.JsonArray; |
20 | 21 | import foundation.icon.icx.Wallet; |
21 | 22 | import foundation.icon.jsonrpc.Address; |
22 | 23 | import foundation.icon.score.client.DefaultScoreClient; |
| 24 | +import foundation.icon.xcall.NetworkAddress; |
23 | 25 | import network.balanced.score.lib.interfaces.*; |
24 | 26 | import network.balanced.score.lib.interfaces.dex.DexTestScoreClient; |
25 | 27 | import network.balanced.score.lib.test.integration.Balanced; |
| 28 | +import network.balanced.score.lib.test.integration.BalancedClient; |
26 | 29 | import network.balanced.score.lib.test.integration.Env; |
27 | 30 | import org.junit.jupiter.api.Order; |
28 | 31 | import org.junit.jupiter.api.Test; |
| 32 | +import score.ByteArrayObjectWriter; |
| 33 | +import score.Context; |
29 | 34 |
|
30 | 35 | import java.io.File; |
31 | 36 | import java.math.BigInteger; |
@@ -58,6 +63,7 @@ class DexIntegrationTest { |
58 | 63 | private static DefaultScoreClient tokenCClient; |
59 | 64 | private static DefaultScoreClient tokenDClient; |
60 | 65 | private static DefaultScoreClient daoFundScoreClient; |
| 66 | + private static BalancedClient owner; |
61 | 67 |
|
62 | 68 | private static final File jarfile = new File("src/intTest/java/network/balanced/score/core/dex/testtokens" + |
63 | 69 | "/DexIntTestToken.jar"); |
@@ -87,6 +93,7 @@ class DexIntegrationTest { |
87 | 93 | balnScoreClient = balanced.baln; |
88 | 94 | rewardsScoreClient = balanced.rewards; |
89 | 95 | daoFundScoreClient = balanced.daofund; |
| 96 | + owner = balanced.ownerClient; |
90 | 97 |
|
91 | 98 | Rewards rewards = new RewardsScoreClient(balanced.rewards); |
92 | 99 | Loans loans = new LoansScoreClient(balanced.loans); |
@@ -209,47 +216,18 @@ void testICXTransferSwapEarningAndCancelOrder() { |
209 | 216 | dexUserScoreClient.cancelSicxicxOrder(); |
210 | 217 | } |
211 | 218 |
|
212 | | - /*@Test |
213 | | - @Order(4) |
214 | | - void testBalnPoolTokenTransferableOnContinuousRewards(){ |
215 | | -
|
216 | | - if(dexUserScoreClient.getContinuousRewardsDay()==null) { |
217 | | - governanceDexScoreClient.setContinuousRewardsDay(dexUserScoreClient.getDay().add(BigInteger.ONE)); |
218 | | - } |
219 | | - waitForADay(); |
220 | | - balanced.syncDistributions(); |
221 | | - //continuous starts |
222 | | - byte[] tokenDeposit = "{\"method\":\"_deposit\",\"params\":{\"none\":\"none\"}}".getBytes(); |
223 | | - mintAndTransferTestTokens(tokenDeposit); |
224 | | - dexUserScoreClient.add(Address.fromString(dexTestBaseScoreAddress), Address.fromString |
225 | | - (dexTestFourthScoreClient._address().toString()), BigInteger.valueOf(50).multiply(EXA), BigInteger.valueOf |
226 | | - (50).multiply(EXA), false); |
227 | | - BigInteger poolId = dexUserScoreClient.getPoolId(Address.fromString(dexTestBaseScoreAddress), Address |
228 | | - .fromString(dexTestFourthScoreAddress)); |
229 | | - //assert pool id is less than 5 |
230 | | - assert poolId.compareTo(BigInteger.valueOf(6)) < 0; |
231 | | - BigInteger liquidity = (BigInteger.valueOf(50).multiply(EXA).multiply(BigInteger.valueOf(50).multiply(EXA))) |
232 | | - .sqrt(); |
233 | | - BigInteger balance = dexUserScoreClient.balanceOf(userAddress, poolId); |
234 | | - BigInteger tUsersPrevBalance = dexUserScoreClient.balanceOf(tUserAddress, poolId); |
235 | | -
|
236 | | - assertEquals(balance, liquidity); |
237 | | - dexUserScoreClient.transfer(tUserAddress, BigInteger.valueOf(5).multiply(EXA), poolId, new byte[0]); |
238 | | - BigInteger tUsersBalance = dexUserScoreClient.balanceOf(tUserAddress, poolId); |
239 | | - assertEquals(tUsersPrevBalance.add(BigInteger.valueOf(5).multiply(EXA)), tUsersBalance); |
240 | | - }*/ |
241 | 219 |
|
242 | 220 | @Test |
243 | 221 | @Order(6) |
244 | 222 | void testWithdraw() { |
245 | 223 | byte[] tokenDeposit = "{\"method\":\"_deposit\",\"params\":{\"none\":\"none\"}}".getBytes(); |
246 | 224 | this.mintAndTransferTestTokens(tokenDeposit); |
247 | 225 | BigInteger withdrawAMount = BigInteger.valueOf(50); |
248 | | - BigInteger balanceBeforeWithdraw = dexUserScoreClient.depositOfUser(userAddress, tokenAAddress); |
| 226 | + BigInteger balanceBeforeWithdraw = dexUserScoreClient.getDeposit(tokenAAddress, userAddress); |
249 | 227 | //withdraw test token |
250 | 228 | dexUserScoreClient.withdraw(tokenAAddress, withdrawAMount); |
251 | 229 |
|
252 | | - BigInteger balanceAfterWithdraw = dexUserScoreClient.depositOfUser(userAddress, tokenAAddress); |
| 230 | + BigInteger balanceAfterWithdraw = dexUserScoreClient.getDeposit(tokenAAddress, userAddress); |
253 | 231 |
|
254 | 232 | assert balanceBeforeWithdraw.equals(balanceAfterWithdraw.add(withdrawAMount)); |
255 | 233 | } |
@@ -317,12 +295,192 @@ void testNonContinuousAndContinuousReward() { |
317 | 295 | BigInteger nextUpdatedBalnHolding = userRewardScoreClient.getBalnHolding(tUserAddress.toString()); |
318 | 296 | assertEquals(beforeSleepDay, dexUserScoreClient.getDay()); |
319 | 297 |
|
320 | | - System.out.println("updated baln holding: " + updatedBalnHolding); |
321 | | - System.out.println("next updated baln holding: " + nextUpdatedBalnHolding); |
| 298 | + |
322 | 299 | assert updatedBalnHolding.compareTo(nextUpdatedBalnHolding) < 0; |
323 | 300 |
|
324 | 301 | } |
325 | 302 |
|
| 303 | + @Test |
| 304 | + @Order(9) |
| 305 | + void crossChainDepositViaAssetManagerDirect() { |
| 306 | + //Arrange |
| 307 | + NetworkAddress ethAccount = new NetworkAddress(balanced.ETH_NID, "0x123"); |
| 308 | + BigInteger amount = BigInteger.valueOf(100).multiply(EXA); |
| 309 | + NetworkAddress ethAssetAddress = new NetworkAddress(balanced.ETH_NID, balanced.ETH_TOKEN_ADDRESS); |
| 310 | + String toNetworkAddress = new NetworkAddress(balanced.ICON_NID, dexScoreClient._address()).toString(); |
| 311 | + score.Address assetAddress = owner.assetManager.getAssetAddress(ethAssetAddress.toString()); |
| 312 | + |
| 313 | + // Act |
| 314 | + byte[] depositData = tokenData("_deposit", |
| 315 | + new JsonObject().set( "address", ethAccount.toString())); |
| 316 | + byte[] deposit = AssetManagerMessages.deposit(balanced.ETH_TOKEN_ADDRESS, ethAccount.account(), toNetworkAddress, amount, depositData); |
| 317 | + owner.xcall.recvCall(owner.assetManager._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_ASSET_MANAGER).toString(), deposit); |
| 318 | + |
| 319 | + // Verify |
| 320 | + BigInteger retrievedValue = dexUserScoreClient.getDepositV2(assetAddress, ethAccount.toString()); |
| 321 | + assertEquals(amount, retrievedValue); |
| 322 | + } |
| 323 | + |
| 324 | + @Test |
| 325 | + @Order(10) |
| 326 | + void crossChainDeposit() { |
| 327 | + //Arrange |
| 328 | + NetworkAddress ethAccount = new NetworkAddress(balanced.ETH_NID, "0x123"); |
| 329 | + BigInteger amount = BigInteger.valueOf(100).multiply(EXA); |
| 330 | + NetworkAddress ethAssetAddress = new NetworkAddress(balanced.ETH_NID, balanced.ETH_TOKEN_ADDRESS); |
| 331 | + String toNetworkAddress = new NetworkAddress(balanced.ICON_NID, dexScoreClient._address()).toString(); |
| 332 | + score.Address assetAddress = owner.assetManager.getAssetAddress(ethAssetAddress.toString()); |
| 333 | + |
| 334 | + // Arrange - Initial deposit |
| 335 | + byte[] deposit = AssetManagerMessages.deposit(balanced.ETH_TOKEN_ADDRESS, ethAccount.account(), "", amount, new byte[0]); |
| 336 | + owner.xcall.recvCall(owner.assetManager._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_ASSET_MANAGER).toString(), deposit); |
| 337 | + |
| 338 | + // Act |
| 339 | + byte[] message = depositMsg(ethAccount.toString(), toNetworkAddress, amount, tokenData("_deposit", |
| 340 | + new JsonObject().set( "address", ethAccount.toString()))); |
| 341 | + owner.xcall.recvCall(assetAddress, ethAccount.toString(), message); |
| 342 | + |
| 343 | + // Verify |
| 344 | + BigInteger retrievedValue = dexUserScoreClient.getDepositV2(assetAddress, ethAccount.toString()); |
| 345 | + assertEquals(amount.add(amount), retrievedValue); |
| 346 | + } |
| 347 | + |
| 348 | + @Test |
| 349 | + @Order(11) |
| 350 | + void crossChainLP() { |
| 351 | + // Arrange |
| 352 | + NetworkAddress ethBaseAssetAddress = new NetworkAddress(balanced.ETH_NID, balanced.ETH_TOKEN_ADDRESS); |
| 353 | + NetworkAddress ethQuoteAssetAddress = new NetworkAddress(balanced.ETH_NID, "ox100"); |
| 354 | + NetworkAddress ethAccount = new NetworkAddress(balanced.ETH_NID, "0x123"); |
| 355 | + BigInteger amount = BigInteger.valueOf(100).multiply(EXA); |
| 356 | + String toNetworkAddress = new NetworkAddress(balanced.ICON_NID, dexScoreClient._address()).toString(); |
| 357 | + |
| 358 | + // Arrange - deploy a new token |
| 359 | + JsonArray addAssetParams = new JsonArray() |
| 360 | + .add(createParameter(ethQuoteAssetAddress.toString())) |
| 361 | + .add(createParameter("ETHZ")) |
| 362 | + .add(createParameter("ETHZ")) |
| 363 | + .add(createParameter(BigInteger.valueOf(18))); |
| 364 | + JsonObject addAsset = createTransaction(balanced.assetManager._address(), "deployAsset", addAssetParams); |
| 365 | + JsonArray transactions = new JsonArray() |
| 366 | + .add(addAsset); |
| 367 | + balanced.governanceClient.execute(transactions.toString()); |
| 368 | + |
| 369 | + score.Address baseAssetAddress = owner.assetManager.getAssetAddress(ethBaseAssetAddress.toString()); |
| 370 | + score.Address quoteAssetAddress = owner.assetManager.getAssetAddress(ethQuoteAssetAddress.toString()); |
| 371 | + |
| 372 | + // Arrange - deposits |
| 373 | + byte[] deposit1 = AssetManagerMessages.deposit(balanced.ETH_TOKEN_ADDRESS, ethAccount.account(), toNetworkAddress, amount, tokenData("_deposit", |
| 374 | + new JsonObject().set( "address", ethAccount.toString()))); |
| 375 | + owner.xcall.recvCall(owner.assetManager._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_ASSET_MANAGER).toString(), deposit1); |
| 376 | + |
| 377 | + byte[] deposit2 = AssetManagerMessages.deposit("ox100", ethAccount.account(), toNetworkAddress, amount, tokenData("_deposit", |
| 378 | + new JsonObject().set( "address", ethAccount.toString()))); |
| 379 | + owner.xcall.recvCall(owner.assetManager._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_ASSET_MANAGER).toString(), deposit2); |
| 380 | + |
| 381 | + // Arrange add quote token |
| 382 | + dexAddQuoteCoin((Address) quoteAssetAddress); |
| 383 | + |
| 384 | + // Act |
| 385 | + byte[] xaddMessage = getAddLPData(baseAssetAddress.toString(), quoteAssetAddress.toString(), amount, amount, false, BigInteger.valueOf(5) ); |
| 386 | + owner.xcall.recvCall(dexScoreClient._address(), ethAccount.toString(), xaddMessage); |
| 387 | + |
| 388 | + // Verify |
| 389 | + BigInteger poolId = dexUserScoreClient.getPoolId(baseAssetAddress, |
| 390 | + quoteAssetAddress); |
| 391 | + assert poolId.compareTo(BigInteger.valueOf(6)) < 0; |
| 392 | + BigInteger liquidity = |
| 393 | + (amount.multiply(amount)).sqrt(); |
| 394 | + BigInteger balance = dexUserScoreClient.xBalanceOf(ethAccount.toString(), poolId); |
| 395 | + |
| 396 | + assertEquals(balance, liquidity); |
| 397 | + } |
| 398 | + |
| 399 | + //depends on crossChainLP test |
| 400 | + @Test |
| 401 | + @Order(12) |
| 402 | + void xRemove(){ |
| 403 | + // Arrange |
| 404 | + NetworkAddress ethAccount = new NetworkAddress(balanced.ETH_NID, "0x123"); |
| 405 | + NetworkAddress ethBaseAssetAddress = new NetworkAddress(balanced.ETH_NID, balanced.ETH_TOKEN_ADDRESS); |
| 406 | + NetworkAddress ethQuoteAssetAddress = new NetworkAddress(balanced.ETH_NID, "ox100"); |
| 407 | + |
| 408 | + score.Address baseAssetAddress = owner.assetManager.getAssetAddress(ethBaseAssetAddress.toString()); |
| 409 | + score.Address quoteAssetAddress = owner.assetManager.getAssetAddress(ethQuoteAssetAddress.toString()); |
| 410 | + |
| 411 | + BigInteger poolId = dexUserScoreClient.getPoolId(baseAssetAddress, |
| 412 | + quoteAssetAddress); |
| 413 | + BigInteger balance = dexUserScoreClient.xBalanceOf(ethAccount.toString(), poolId); |
| 414 | + BigInteger withdrawAmount = balance.divide(BigInteger.TWO); |
| 415 | + JsonArray setXCallFeePermissionParameters = new JsonArray() |
| 416 | + .add(createParameter(balanced.dex._address())).add(createParameter(balanced.ETH_NID)).add(createParameter(true)); |
| 417 | + JsonArray actions = new JsonArray() |
| 418 | + .add(createTransaction(balanced.daofund._address(), "setXCallFeePermission", setXCallFeePermissionParameters)); |
| 419 | + owner.governance.execute(actions.toString()); |
| 420 | + |
| 421 | + // Act |
| 422 | + byte[] removeLPMsg = getXRemoveData(poolId, withdrawAmount, true); |
| 423 | + owner.xcall.recvCall(dexScoreClient._address(), ethAccount.toString(), removeLPMsg); |
| 424 | + |
| 425 | + // Verify |
| 426 | + BigInteger updatedBalance = dexUserScoreClient.xBalanceOf(ethAccount.toString(), poolId); |
| 427 | + assertEquals(withdrawAmount, updatedBalance); |
| 428 | + } |
| 429 | + |
| 430 | + public static byte[] tokenData(String method, JsonObject params) { |
| 431 | + JsonObject data = new JsonObject(); |
| 432 | + data.set("method", method); |
| 433 | + data.set("params", params); |
| 434 | + return data.toString().getBytes(); |
| 435 | + } |
| 436 | + |
| 437 | + static byte[] depositMsg(String from, String to, BigInteger amount, byte[] data) { |
| 438 | + ByteArrayObjectWriter writer = Context.newByteArrayObjectWriter("RLPn"); |
| 439 | + writer.beginList(4); |
| 440 | + writer.write("xhubtransfer"); |
| 441 | + writer.write(to); |
| 442 | + writer.write(amount); |
| 443 | + writer.write(data); |
| 444 | + writer.end(); |
| 445 | + return writer.toByteArray(); |
| 446 | + } |
| 447 | + static byte[] getXRemoveData(BigInteger poolId, BigInteger lpTokenBalance, Boolean withdraw) { |
| 448 | + ByteArrayObjectWriter writer = Context.newByteArrayObjectWriter("RLPn"); |
| 449 | + writer.beginList(4); |
| 450 | + writer.write("xremove"); |
| 451 | + writer.write(poolId); |
| 452 | + writer.write(lpTokenBalance); |
| 453 | + writer.write(withdraw); |
| 454 | + writer.end(); |
| 455 | + return writer.toByteArray(); |
| 456 | + } |
| 457 | + |
| 458 | + static byte[] getStakeData(BigInteger poolId, BigInteger amount, String to, byte[] data) { |
| 459 | + ByteArrayObjectWriter writer = Context.newByteArrayObjectWriter("RLPn"); |
| 460 | + writer.beginList(4); |
| 461 | + writer.write("xhubtransfer"); |
| 462 | + writer.write(to); |
| 463 | + writer.write(amount); |
| 464 | + writer.write(poolId); |
| 465 | + writer.write(data); |
| 466 | + writer.end(); |
| 467 | + return writer.toByteArray(); |
| 468 | + } |
| 469 | + |
| 470 | + static byte[] getAddLPData(String baseToken, String quoteToken, BigInteger baseValue, BigInteger quoteValue, Boolean withdraw_unused, BigInteger slippagePercentage) { |
| 471 | + ByteArrayObjectWriter writer = Context.newByteArrayObjectWriter("RLPn"); |
| 472 | + writer.beginList(7); |
| 473 | + writer.write("xadd"); |
| 474 | + writer.write(baseToken); |
| 475 | + writer.write(quoteToken); |
| 476 | + writer.write(baseValue); |
| 477 | + writer.write(quoteValue); |
| 478 | + writer.write(withdraw_unused); |
| 479 | + writer.write(slippagePercentage); |
| 480 | + writer.end(); |
| 481 | + return writer.toByteArray(); |
| 482 | + } |
| 483 | + |
326 | 484 | void transferSicxToken() { |
327 | 485 | byte[] data = "testData".getBytes(); |
328 | 486 | ((StakingScoreClient) userStakeScoreClient).stakeICX(BigInteger.valueOf(80).multiply(EXA), userAddress, data); |
|
0 commit comments