Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use persistent messages for withdraws from ICON #432

Merged
merged 3 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core-contracts/AssetManager/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repositories {
dependencies {
compileOnly Dependencies.javaeeApi
implementation project(':score-lib')
implementation 'xyz.venture23:xcall-lib:0.1.1'
implementation 'xyz.venture23:xcall-lib:2.1.0'

implementation Dependencies.javaeeScorex
implementation Dependencies.minimalJson
Expand Down Expand Up @@ -106,7 +106,7 @@ test {
jacocoTestReport {
dependsOn test
reports {
xml.required = true
xml.required = false
csv.required = false
html.outputLocation = layout.buildDirectory.dir('jacocoHtml')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void withdrawTo() {

// Act
byte[] deposit = AssetManagerMessages.deposit(balanced.ETH_TOKEN_ADDRESS, ethAccount.account(), iconAccount.toString(), amount, new byte[0]);
owner.xcall.sendCall(owner.assetManager._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_ASSET_MANAGER).toString(), deposit);
owner.xcall.recvCall(owner.assetManager._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_ASSET_MANAGER).toString(), deposit);
user.assetManager.withdrawTo(BigInteger.ONE, assetAddress, ethAccount.toString(), amount);

// Assert
Expand All @@ -107,14 +107,14 @@ void withdrawRollback() {

// Act
byte[] deposit = AssetManagerMessages.deposit(balanced.ETH_TOKEN_ADDRESS, ethAccount.account(), "", amount, new byte[0]);
owner.xcall.sendCall(owner.assetManager._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_ASSET_MANAGER).toString(), deposit);
owner.xcall.recvCall(owner.assetManager._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_ASSET_MANAGER).toString(), deposit);

balanced.addXCallFeePermission(balanced.assetManager._address(), balanced.ETH_NID, true);
byte[] withdraw = AssetManagerMessages.xWithdraw(assetAddress, amount);
owner.xcall.sendCall(owner.assetManager._address(), ethAccount.toString(), withdraw);
owner.xcall.recvCall(owner.assetManager._address(), ethAccount.toString(), withdraw);

byte[] withdrawRollback = AssetManagerMessages.withdrawRollback(nativeAssetAddress.toString(), ethAccount.toString(), amount);
owner.xcall.sendCall(owner.assetManager._address(), new NetworkAddress(balanced.ICON_NID, owner.xcall._address()).toString(), withdrawRollback);
owner.xcall.recvCall(owner.assetManager._address(), new NetworkAddress(balanced.ICON_NID, owner.xcall._address()).toString(), withdrawRollback);

// Assert
BigInteger assetDeposit = user.assetManager.getAssetDeposit(nativeAssetAddress.toString());
Expand Down Expand Up @@ -195,11 +195,11 @@ void xWithdraw() {

// Act
byte[] deposit = AssetManagerMessages.deposit(balanced.ETH_TOKEN_ADDRESS, ethAccount.account(), "", amount, new byte[0]);
owner.xcall.sendCall(owner.assetManager._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_ASSET_MANAGER).toString(), deposit);
owner.xcall.recvCall(owner.assetManager._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_ASSET_MANAGER).toString(), deposit);

balanced.addXCallFeePermission(balanced.assetManager._address(), balanced.ETH_NID, true);
byte[] withdraw = AssetManagerMessages.xWithdraw(assetAddress, amount);
owner.xcall.sendCall(owner.assetManager._address(), ethAccount.toString(), withdraw);
owner.xcall.recvCall(owner.assetManager._address(), ethAccount.toString(), withdraw);

// Assert
BigInteger assetDeposit = user.assetManager.getAssetDeposit(nativeAssetAddress.toString());
Expand All @@ -221,7 +221,7 @@ void deposit() {

// Act
byte[] deposit = AssetManagerMessages.deposit(balanced.ETH_TOKEN_ADDRESS, ethAccount.account(), "", amount, new byte[0]);
owner.xcall.sendCall(owner.assetManager._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_ASSET_MANAGER).toString(), deposit);
owner.xcall.recvCall(owner.assetManager._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_ASSET_MANAGER).toString(), deposit);

// Assert
BigInteger assetDeposit = user.assetManager.getAssetDeposit(nativeAssetAddress.toString());
Expand All @@ -243,13 +243,13 @@ void deposit_limitDeposit(){
// Act & Assert
//pass
byte[] deposit = AssetManagerMessages.deposit(balanced.ETH_TOKEN_ADDRESS, ethAccount.account(), "", amount, new byte[0]);
owner.xcall.sendCall(owner.assetManager._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_ASSET_MANAGER).toString(), deposit);
owner.xcall.recvCall(owner.assetManager._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_ASSET_MANAGER).toString(), deposit);

JsonArray params = new JsonArray().add(createParameter(nativeAssetAddress.toString())).add(createParameter(amount.subtract(BigInteger.ONE)));
owner.governance.execute(createSingleTransaction(balanced.assetManager._address(), "setAssetChainDepositLimit",
params).toString());
//fail
Executable ethChainDepositLimitExceeded = () -> owner.xcall.sendCall(owner.assetManager._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_ASSET_MANAGER).toString(), deposit);
Executable ethChainDepositLimitExceeded = () -> owner.xcall.recvCall(owner.assetManager._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_ASSET_MANAGER).toString(), deposit);
assertThrows(RevertedException.class, ethChainDepositLimitExceeded);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ public void deposit(String from, String tokenAddress, String fromAddress, String

public void xWithdraw(String from, Address tokenAddress, BigInteger amount) {
NetworkAddress _from = NetworkAddress.valueOf(from);
BigInteger xCallFee = Context.call(BigInteger.class, BalancedAddressManager.getDaofund(), "claimXCallFee", _from.net(), true);
BigInteger xCallFee = Context.call(BigInteger.class, BalancedAddressManager.getDaofund(), "claimXCallFee", _from.net(), false);
_withdrawTo(tokenAddress, from, from, amount, xCallFee);
}

Expand All @@ -310,7 +310,6 @@ private void _withdrawTo(Address asset, String from, String to, BigInteger amoun
NetworkAddress tokenAddress = new NetworkAddress(net, nativeTokenAddress);
NetworkAddress spoke = NetworkAddress.valueOf(spokes.get(tokenAddress.net()));
byte[] msg;
byte[] rollback = AssetManagerMessages.withdrawRollback(tokenAddress.toString(), to, amount);

BigInteger sendAmount = translateOutgoingDecimals(tokenAddress.toString(), amount);
Context.require(sendAmount.compareTo(BigInteger.ZERO) > 0, "Amount needs to be greater than 0 on the destination chain");
Expand All @@ -324,7 +323,7 @@ private void _withdrawTo(Address asset, String from, String to, BigInteger amoun
Context.require(remainingDeposit.signum() >= 0, "Remaining deposit can't be negative");
assetDeposits.set(tokenAddress.toString(), remainingDeposit);

XCallUtils.sendCall(fee, spoke, msg, rollback);
XCallUtils.sendPersistentCall(fee, spoke, msg);
}

private BigInteger translateOutgoingDecimals(String token, BigInteger amount) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import network.balanced.score.lib.interfaces.tokens.AssetTokenScoreInterface;
import network.balanced.score.lib.test.mock.MockBalanced;
import network.balanced.score.lib.test.mock.MockContract;
import network.balanced.score.lib.utils.XCallUtils;
import network.balanced.score.lib.structs.ProtocolConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
Expand Down Expand Up @@ -92,9 +94,9 @@ void setup() throws Exception {
when(mockBalanced.xCall.mock.getNetworkId()).thenReturn(NATIVE_NID);
assetManager = sm.deploy(owner, AssetManagerImpl.class, governance.getAddress(), tokeBytes);

when(mockBalanced.xCallManager.mock.getProtocols(ETH_NID)).thenReturn(Map.of("sources", defaultProtocols, "destinations", defaultDestinationsProtocols));
when(mockBalanced.xCallManager.mock.getProtocols(BSC_NID)).thenReturn(Map.of("sources", defaultProtocols, "destinations", defaultDestinationsProtocols));
when(mockBalanced.xCallManager.mock.getProtocols(INJ_NID)).thenReturn(Map.of("sources", defaultProtocols, "destinations", defaultDestinationsProtocols));
when(mockBalanced.xCallManager.mock.getProtocolsRaw(ETH_NID)).thenReturn(new ProtocolConfig(defaultProtocols, defaultDestinationsProtocols).toBytes());
when(mockBalanced.xCallManager.mock.getProtocolsRaw(BSC_NID)).thenReturn(new ProtocolConfig(defaultProtocols, defaultDestinationsProtocols).toBytes());
when(mockBalanced.xCallManager.mock.getProtocolsRaw(INJ_NID)).thenReturn(new ProtocolConfig(defaultProtocols, defaultDestinationsProtocols).toBytes());

assetManager.invoke(governance.account, "addSpokeManager", ethSpoke.toString());
assetManagerSpy = (AssetManagerImpl) spy(assetManager.getInstance());
Expand Down Expand Up @@ -161,9 +163,9 @@ void withdrawTo() {

// Assert
byte[] expectedMsg = SpokeAssetManagerMessages.WithdrawTo(tokenAddress.account(), ethAccount.account(), amount);
byte[] expectedRollback = AssetManagerMessages.withdrawRollback(tokenAddress.toString(), ethAccount.toString(), amount);
byte[] expectedCallData = XCallUtils.createPersistentMessage(expectedMsg, defaultProtocols, defaultDestinationsProtocols);

verify(mockBalanced.xCall.mock).sendCallMessage(ethSpoke.toString(), expectedMsg, expectedRollback, defaultProtocols, defaultDestinationsProtocols);
verify(mockBalanced.xCall.mock).sendCall(ethSpoke.toString(), expectedCallData);
verify(ethAsset1.mock).burnFrom(user.getAddress().toString(), amount);
}

Expand All @@ -181,9 +183,9 @@ void withdrawNativeTo() {

// Assert
byte[] expectedMsg = SpokeAssetManagerMessages.WithdrawNativeTo(tokenAddress.account(), ethAccount.account(), amount);
byte[] expectedRollback = AssetManagerMessages.withdrawRollback(tokenAddress.toString(), ethAccount.toString(), amount);
byte[] expectedCallData = XCallUtils.createPersistentMessage(expectedMsg, defaultProtocols, defaultDestinationsProtocols);

verify(mockBalanced.xCall.mock).sendCallMessage(ethSpoke.toString(), expectedMsg, expectedRollback, defaultProtocols, defaultDestinationsProtocols);
verify(mockBalanced.xCall.mock).sendCall(ethSpoke.toString(), expectedCallData);
verify(ethAsset1.mock).burnFrom(user.getAddress().toString(), amount);
}

Expand Down Expand Up @@ -232,11 +234,11 @@ void xCallWithdraw() {

// Assert
byte[] expectedMsg = SpokeAssetManagerMessages.WithdrawTo(tokenAddress.account(), ethAccount.account(), amount);
byte[] expectedRollback = AssetManagerMessages.withdrawRollback(tokenAddress.toString(), ethAccount.toString(), amount);
byte[] expectedCallData = XCallUtils.createPersistentMessage(expectedMsg, defaultProtocols, defaultDestinationsProtocols);

verify(mockBalanced.xCall.mock).sendCallMessage(ethSpoke.toString(), expectedMsg, expectedRollback, defaultProtocols, defaultDestinationsProtocols);
verify(mockBalanced.xCall.mock).sendCall(ethSpoke.toString(), expectedCallData);
verify(ethAsset1.mock).burnFrom(ethAccount.toString(), amount);
verify(mockBalanced.daofund.mock).claimXCallFee(ETH_NID, true);
verify(mockBalanced.daofund.mock).claimXCallFee(ETH_NID, false);
}

@Test
Expand Down Expand Up @@ -417,9 +419,9 @@ void linkToken_lowerDecimals_withdraw() {

// Assert
byte[] expectedMsg = SpokeAssetManagerMessages.WithdrawTo(tokenNetworkAddress.account(), injAccount.account(), withdrawAmount);
byte[] expectedRollback = AssetManagerMessages.withdrawRollback(tokenNetworkAddress.toString(), injAccount.toString(), burnAmount);
byte[] expectedCallData = XCallUtils.createPersistentMessage(expectedMsg, defaultProtocols, defaultDestinationsProtocols);

verify(mockBalanced.xCall.mock).sendCallMessage(injSpoke.toString(), expectedMsg, expectedRollback, defaultProtocols, defaultDestinationsProtocols);
verify(mockBalanced.xCall.mock).sendCall(injSpoke.toString(), expectedCallData);
verify(injLinkAsset.mock).burnFrom(user.getAddress().toString(), burnAmount);
BigInteger assetDeposit = (BigInteger) assetManager.call("getAssetDeposit", tokenNetworkAddress.toString());
assertEquals(assetDeposit, BigInteger.ZERO);
Expand Down Expand Up @@ -474,9 +476,9 @@ void linkToken_higherDecimals_withdraw() {

// Assert
byte[] expectedMsg = SpokeAssetManagerMessages.WithdrawTo(tokenNetworkAddress.account(), injAccount.account(), withdrawAmount);
byte[] expectedRollback = AssetManagerMessages.withdrawRollback(tokenNetworkAddress.toString(), injAccount.toString(), burnAmount);
byte[] expectedCallData = XCallUtils.createPersistentMessage(expectedMsg, defaultProtocols, defaultDestinationsProtocols);

verify(mockBalanced.xCall.mock).sendCallMessage(injSpoke.toString(), expectedMsg, expectedRollback, defaultProtocols, defaultDestinationsProtocols);
verify(mockBalanced.xCall.mock).sendCall(injSpoke.toString(), expectedCallData);
verify(injLinkAsset.mock).burnFrom(user.getAddress().toString(), burnAmount);
BigInteger assetDeposit = (BigInteger) assetManager.call("getAssetDeposit", tokenNetworkAddress.toString());
assertEquals(assetDeposit, BigInteger.ZERO);
Expand Down
4 changes: 2 additions & 2 deletions core-contracts/Loans/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ repositories {
dependencies {
compileOnly Dependencies.javaeeApi
implementation project(':score-lib')
implementation 'xyz.venture23:xcall-lib:0.1.1'
implementation 'xyz.venture23:xcall-lib:2.1.0'

implementation Dependencies.javaeeScorex
implementation Dependencies.minimalJson
Expand Down Expand Up @@ -98,7 +98,7 @@ test {
jacocoTestReport {
dependsOn test
reports {
xml.required = true
xml.required = false
csv.required = false
html.outputLocation = layout.buildDirectory.dir('jacocoHtml')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,24 +172,24 @@ void crossChainDepositAndBorrow() throws Exception {
JsonObject loanData = new JsonObject()
.add("_amount", loanAmount.toString());
byte[] depositAndBorrowETH = AssetManagerMessages.deposit(balanced.ETH_TOKEN_ADDRESS, ethUser.account().toString(), loansNetAddress, collateral, loanData.toString().getBytes());
owner.xcall.sendCall(balanced.assetManager._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_ASSET_MANAGER).toString(), depositAndBorrowETH);
owner.xcall.recvCall(balanced.assetManager._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_ASSET_MANAGER).toString(), depositAndBorrowETH);

// Deposit first then borrow
byte[] depositBSC = AssetManagerMessages.deposit(balanced.BSC_TOKEN_ADDRESS, bscUser.account().toString(), loansNetAddress, collateral, "{}".getBytes());
owner.xcall.sendCall(balanced.assetManager._address(), new NetworkAddress(balanced.BSC_NID, balanced.BSC_ASSET_MANAGER).toString(), depositBSC);
owner.xcall.recvCall(balanced.assetManager._address(), new NetworkAddress(balanced.BSC_NID, balanced.BSC_ASSET_MANAGER).toString(), depositBSC);
byte[] borrowBSC = LoansMessages.xBorrow(balanced.BSC_TOKEN_SYMBOL, loanAmount, "", new byte[0]);
owner.xcall.sendCall(balanced.loans._address(), bscUser.toString(), borrowBSC);
owner.xcall.recvCall(balanced.loans._address(), bscUser.toString(), borrowBSC);

// Bridge collateral to hub wallet first then borrow
byte[] transferBSC = AssetManagerMessages.deposit(balanced.BSC_TOKEN_ADDRESS, bscHubUser.account().toString(), bscHubUser.toString(), collateral, new byte[0]);
owner.xcall.sendCall(balanced.assetManager._address(), new NetworkAddress(balanced.BSC_NID, balanced.BSC_ASSET_MANAGER).toString(), transferBSC);
owner.xcall.recvCall(balanced.assetManager._address(), new NetworkAddress(balanced.BSC_NID, balanced.BSC_ASSET_MANAGER).toString(), transferBSC);

byte[] depositAndBorrowTransfer = SpokeTokenMessages.xHubTransfer(loansNetAddress, collateral, loanData.toString().getBytes());
owner.xcall.sendCall(balanced.bscBaseAsset, bscHubUser.toString(), depositAndBorrowTransfer);
owner.xcall.recvCall(balanced.bscBaseAsset, bscHubUser.toString(), depositAndBorrowTransfer);

// Bridge collateral to ICON wallet first then borrow
byte[] transferBSCToICON = AssetManagerMessages.deposit(balanced.BSC_TOKEN_ADDRESS, bscHubUser.account().toString(), new NetworkAddress(balanced.ICON_NID, nativeLoanTaker.getAddress()).toString(), collateral, new byte[0]);
owner.xcall.sendCall(balanced.assetManager._address(), new NetworkAddress(balanced.BSC_NID, balanced.BSC_ASSET_MANAGER).toString(), transferBSCToICON);
owner.xcall.recvCall(balanced.assetManager._address(), new NetworkAddress(balanced.BSC_NID, balanced.BSC_ASSET_MANAGER).toString(), transferBSCToICON);
nativeLoanTaker.spokeToken(balanced.bscBaseAsset).transfer(balanced.loans._address(), collateral, loanData.toString().getBytes());

// Assert
Expand Down Expand Up @@ -233,27 +233,27 @@ void crossChainRepayAndWithdraw() throws Exception {
.add("_collateral", balanced.ETH_TOKEN_SYMBOL)
.add("_withdrawAmount", amountToWithdraw.toString());
byte[] repayAndWithdraw = HubTokenMessages.xCrossTransfer(ethUser.toString(), loansNetAddress, amountToRepay, repayData.toString().getBytes());
owner.xcall.sendCall(balanced.bnusd._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_BNUSD_ADDRESS).toString(), repayAndWithdraw);
owner.xcall.recvCall(balanced.bnusd._address(), new NetworkAddress(balanced.ETH_NID, balanced.ETH_BNUSD_ADDRESS).toString(), repayAndWithdraw);

// Repay through transfer, then withdraw via xCall
repayData = new JsonObject()
.add("_collateral", balanced.BSC_TOKEN_SYMBOL)
.add("_withdrawAmount", "0");
byte[] repay = HubTokenMessages.xCrossTransfer(bscUser.toString(), loansNetAddress, amountToRepay, repayData.toString().getBytes());
owner.xcall.sendCall(balanced.bnusd._address(), new NetworkAddress(balanced.BSC_NID, balanced.BSC_BNUSD_ADDRESS).toString(), repay);
owner.xcall.recvCall(balanced.bnusd._address(), new NetworkAddress(balanced.BSC_NID, balanced.BSC_BNUSD_ADDRESS).toString(), repay);

byte[] withdraw = LoansMessages.xWithdraw(amountToWithdraw, balanced.BSC_TOKEN_SYMBOL);
owner.xcall.sendCall(balanced.loans._address(), bscUser.toString(), withdraw);
owner.xcall.recvCall(balanced.loans._address(), bscUser.toString(), withdraw);

// Repay and withdraw with bnUSD on the hub.
byte[] deposit = HubTokenMessages.xCrossTransfer(bscHubUser.toString(), bscHubUser.toString(), amountToRepay, new byte[0]);
owner.xcall.sendCall(balanced.bnusd._address(), new NetworkAddress(balanced.BSC_NID, balanced.BSC_BNUSD_ADDRESS).toString(), deposit);
owner.xcall.recvCall(balanced.bnusd._address(), new NetworkAddress(balanced.BSC_NID, balanced.BSC_BNUSD_ADDRESS).toString(), deposit);
repayData = new JsonObject()
.add("_collateral", balanced.BSC_TOKEN_SYMBOL)
.add("_withdrawAmount", amountToWithdraw.toString());

byte[] repayTransfer = HubTokenMessages.xHubTransfer(loansNetAddress, amountToRepay, repayData.toString().getBytes());
owner.xcall.sendCall(balanced.bnusd._address(), bscHubUser.toString(), repayTransfer);
owner.xcall.recvCall(balanced.bnusd._address(), bscHubUser.toString(), repayTransfer);

// Repay and withdraw with bnUSD on the ICON wallet.
nativeLoanTaker.loans.returnAsset("bnUSD", amountToRepay, balanced.BSC_TOKEN_SYMBOL, "");
Expand Down
Loading
Loading