diff --git a/core-contracts/Dex/src/main/java/network/balanced/score/core/dex/DexImpl.java b/core-contracts/Dex/src/main/java/network/balanced/score/core/dex/DexImpl.java index d2690e550..1b21ab10b 100644 --- a/core-contracts/Dex/src/main/java/network/balanced/score/core/dex/DexImpl.java +++ b/core-contracts/Dex/src/main/java/network/balanced/score/core/dex/DexImpl.java @@ -234,18 +234,6 @@ public void tokenFallback(Address _from, BigInteger _value, byte[] _data) { } } - @External - public void transfer(Address _to, BigInteger _value, BigInteger _id, @Optional byte[] _data) { - isDexOn(); - checkStatus(); - if (_data == null) { - _data = new byte[0]; - } - NetworkAddress from = new NetworkAddress(NATIVE_NID, Context.getCaller()); - NetworkAddress to = new NetworkAddress(NATIVE_NID, _to); - _transfer(from, to, _value, _id.intValue(), _data); - } - public void xWithdraw(String from, String _token, BigInteger _value) { NetworkAddress sender = NetworkAddress.valueOf(from); _withdraw(sender, Address.fromString(_token), _value); diff --git a/core-contracts/Dex/src/main/java/network/balanced/score/core/dex/IRC31StandardSpokeLpToken.java b/core-contracts/Dex/src/main/java/network/balanced/score/core/dex/IRC31StandardSpokeLpToken.java index 5e3740326..85833ea18 100644 --- a/core-contracts/Dex/src/main/java/network/balanced/score/core/dex/IRC31StandardSpokeLpToken.java +++ b/core-contracts/Dex/src/main/java/network/balanced/score/core/dex/IRC31StandardSpokeLpToken.java @@ -15,8 +15,10 @@ import static network.balanced.score.core.dex.DexDBVariables.*; import static network.balanced.score.core.dex.DexDBVariables.poolLpTotal; +import static network.balanced.score.core.dex.utils.Check.isDexOn; import static network.balanced.score.core.dex.utils.Const.SICXICX_POOL_ID; import static network.balanced.score.core.dex.utils.Const.TAG; +import static network.balanced.score.lib.utils.Check.checkStatus; public abstract class IRC31StandardSpokeLpToken extends FloorLimited implements Dex { @@ -40,16 +42,23 @@ public BigInteger xBalanceOf(String _owner, BigInteger _id) { @External public void transfer(Address _to, BigInteger _value, BigInteger _id, @Optional byte[] _data) { - _transfer( - new NetworkAddress(NATIVE_NID, Context.getCaller()), - new NetworkAddress(NATIVE_NID, _to), - _value, - _id.intValue(), - _data); + isDexOn(); + checkStatus(); + if (_data == null) { + _data = new byte[0]; + } + NetworkAddress from = new NetworkAddress(NATIVE_NID, Context.getCaller()); + NetworkAddress to = new NetworkAddress(NATIVE_NID, _to); + _transfer(from, to, _value, _id.intValue(), _data); } @External public void hubTransfer(String _to, BigInteger _value, BigInteger _id, @Optional byte[] _data) { + isDexOn(); + checkStatus(); + if (_data == null) { + _data = new byte[0]; + } _transfer( new NetworkAddress(NATIVE_NID, Context.getCaller()), NetworkAddress.valueOf(_to, NATIVE_NID), @@ -58,7 +67,6 @@ public void hubTransfer(String _to, BigInteger _value, BigInteger _id, @Optional _data); } - public void xHubTransfer(String from, String _to, BigInteger _value, BigInteger _id, byte[] _data) { _transfer( NetworkAddress.valueOf(from), @@ -115,10 +123,6 @@ void _transfer(NetworkAddress _from, NetworkAddress _to, BigInteger _value, Inte Context.call(contractAddress, "onXIRC31Received", _from.toString(), _from.toString(), _id, _value, dataBytes); } - protected boolean isNative(NetworkAddress address) { - return address.net().equals(NATIVE_NID); - } - boolean isLockingPool(Integer id) { return id.equals(SICXICX_POOL_ID); } diff --git a/core-contracts/Dex/src/test/java/network/balanced/score/core/dex/DexTestCore.java b/core-contracts/Dex/src/test/java/network/balanced/score/core/dex/DexTestCore.java index 8454cea6b..9f05318a3 100644 --- a/core-contracts/Dex/src/test/java/network/balanced/score/core/dex/DexTestCore.java +++ b/core-contracts/Dex/src/test/java/network/balanced/score/core/dex/DexTestCore.java @@ -35,7 +35,6 @@ import java.util.Map; import static network.balanced.score.core.dex.utils.Const.*; -import static network.balanced.score.lib.utils.BalancedAddressManager.getDaofund; import static network.balanced.score.lib.utils.Constants.EXA; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -156,15 +155,6 @@ void crossChainLP(){ } - public static byte[] hexStringToByteArray(String hex) { - int length = hex.length(); - byte[] data = new byte[length / 2]; - for (int i = 0; i < length; i += 2) { - data[i / 2] = (byte) ((Character.digit(hex.charAt(i), 16) << 4) - + Character.digit(hex.charAt(i+1), 16)); - } - return data; - } @Test void crossChainLPWithWithdraw(){ @@ -810,10 +800,21 @@ void transfer() { BigInteger initialValue = (BigInteger) dexScore.call("balanceOf", account.getAddress(), poolId); dexScore.invoke(account, "transfer", account1.getAddress(), transferValue, poolId, data.getBytes()); - BigInteger value = (BigInteger) dexScore.call("balanceOf", account.getAddress(), poolId); - assertEquals(value, initialValue.subtract(transferValue)); - value = (BigInteger) dexScore.call("balanceOf", account1.getAddress(), poolId); - assertEquals(value, transferValue); + BigInteger sendersBalanceAfterTransfer = (BigInteger) dexScore.call("balanceOf", account.getAddress(), poolId); + assertEquals(sendersBalanceAfterTransfer, initialValue.subtract(transferValue)); + BigInteger receiversBalance = (BigInteger) dexScore.call("balanceOf", account1.getAddress(), poolId); + assertEquals(receiversBalance, transferValue); + + + //test hub transfer + String toHubAddress = "0x1.ETH/0x123"; + dexScore.invoke(account, "hubTransfer", toHubAddress, transferValue, poolId, data.getBytes()); + + BigInteger sendersBalanceAfterHubTransfer = (BigInteger) dexScore.call("balanceOf", account.getAddress(), poolId); + assertEquals(sendersBalanceAfterHubTransfer, sendersBalanceAfterTransfer.subtract(transferValue)); + BigInteger hubReceiversBalance = (BigInteger) dexScore.call("xBalanceOf", toHubAddress, poolId); + assertEquals(hubReceiversBalance, transferValue); + } @Test diff --git a/score-lib/src/test/java/network/balanced/score/lib/utils/TokenTransferTest.java b/score-lib/src/test/java/network/balanced/score/lib/utils/TokenTransferTest.java index e8f625057..27fb6515c 100644 --- a/score-lib/src/test/java/network/balanced/score/lib/utils/TokenTransferTest.java +++ b/score-lib/src/test/java/network/balanced/score/lib/utils/TokenTransferTest.java @@ -15,8 +15,10 @@ import score.Context; import java.math.BigInteger; +import java.util.EmptyStackException; import static network.balanced.score.lib.utils.Constants.EXA; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -32,12 +34,15 @@ public class TokenTransferTest extends UnitTest { protected static MockedStatic contextMock; protected static MockedStatic addressManagerMock; + protected static MockedStatic tokenTransferMock; + @BeforeAll public static void setup() throws Exception{ dummyScore = sm.deploy(owner, DummyScore.class); MockBalanced mockBalanced = new MockBalanced(sm, owner); contextMock = Mockito.mockStatic(Context.class, Mockito.CALLS_REAL_METHODS); addressManagerMock = MockBalanced.addressManagerMock; + tokenTransferMock = Mockito.mockStatic(TokenTransfer.class, Mockito.CALLS_REAL_METHODS); } @Test @@ -55,9 +60,9 @@ public void crossTransfer(){ contextMock.when(() -> Context.call(any(Address.class), eq("claimXCallFee"), any(String.class), any(Boolean.class))).thenReturn(EXA); // Act - try { - TokenTransfer.transfer(dummyScore.getAddress(), new NetworkAddress("0x1.ETH", user.getAddress()).toString(), BigInteger.TWO); - }catch (Exception ignore){} + assertThrows( + EmptyStackException.class, + () -> TokenTransfer.transfer(dummyScore.getAddress(), new NetworkAddress("0x1.ETH", user.getAddress()).toString(), BigInteger.TWO) ); contextMock.when(() -> Context.call(any(BigInteger.class), any(Address.class), eq("crossTransfer"), any(String.class), any(BigInteger.class), any(byte[].class))).thenReturn(true); @@ -80,9 +85,9 @@ public void hubTransfer(){ contextMock.when(() -> Context.call(any(Address.class), eq("claimXCallFee"), any(String.class), any(Boolean.class))).thenReturn(EXA); // Act - try { - TokenTransfer.transfer(dummyScore.getAddress(), new NetworkAddress("0x1.ETH", user.getAddress()).toString(), BigInteger.TWO); - }catch (Exception ignore){} + assertThrows( + EmptyStackException.class, + () -> TokenTransfer.transfer(dummyScore.getAddress(), new NetworkAddress("0x1.ETH", user.getAddress()).toString(), BigInteger.TWO)); contextMock.when(() -> Context.call(any(Address.class), eq("hubTransfer"), any(String.class), any(BigInteger.class), any(byte[].class))).thenReturn(true); @@ -100,10 +105,9 @@ public void transfer(){ contextMock.when(() -> Context.call(any(Address.class), eq("getNetworkId"))).thenReturn("0x2.ICON"); - // Act - try { - TokenTransfer.transfer(dummyScore.getAddress(), new NetworkAddress("0x2.ICON", user.getAddress()).toString(), BigInteger.TWO); // native nid for transfer - }catch (Exception ignore){} + assertThrows( + EmptyStackException.class, + () -> TokenTransfer.transfer(dummyScore.getAddress(), new NetworkAddress("0x2.ICON", user.getAddress()).toString(), BigInteger.TWO)); contextMock.when(() -> Context.call(any(Address.class), eq("transfer"), any(Address.class), any(BigInteger.class), any(byte[].class))).thenReturn(true); @@ -131,6 +135,10 @@ public void withdrawTo(){ TokenTransfer.transfer(dummyScore.getAddress(), new NetworkAddress("0x1.ETH", user.getAddress()).toString(), BigInteger.TWO); }catch (Exception ignore){} + assertThrows( + EmptyStackException.class, + () -> TokenTransfer.transfer(dummyScore.getAddress(), new NetworkAddress("0x1.ETH", user.getAddress()).toString(), BigInteger.TWO)); + contextMock.when(() -> Context.call(any(BigInteger.class), any(Address.class), eq("withdrawTo"), any(Address.class), any(String.class), any(BigInteger.class))).thenReturn(true); // Verify