Skip to content

Commit

Permalink
test updates on xcall, transfer method merged in token class,exceptio…
Browse files Browse the repository at this point in the history
…n verified on token transfer test
  • Loading branch information
sagars committed Nov 21, 2024
1 parent 0973021 commit e218850
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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];

Check warning on line 48 in core-contracts/Dex/src/main/java/network/balanced/score/core/dex/IRC31StandardSpokeLpToken.java

View check run for this annotation

Codecov / codecov/patch

core-contracts/Dex/src/main/java/network/balanced/score/core/dex/IRC31StandardSpokeLpToken.java#L48

Added line #L48 was not covered by tests
}
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];

Check warning on line 60 in core-contracts/Dex/src/main/java/network/balanced/score/core/dex/IRC31StandardSpokeLpToken.java

View check run for this annotation

Codecov / codecov/patch

core-contracts/Dex/src/main/java/network/balanced/score/core/dex/IRC31StandardSpokeLpToken.java#L60

Added line #L60 was not covered by tests
}
_transfer(
new NetworkAddress(NATIVE_NID, Context.getCaller()),
NetworkAddress.valueOf(_to, NATIVE_NID),
Expand All @@ -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),
Expand Down Expand Up @@ -115,10 +123,6 @@ void _transfer(NetworkAddress _from, NetworkAddress _to, BigInteger _value, Inte
Context.call(contractAddress, "onXIRC31Received", _from.toString(), _from.toString(), _id, _value, dataBytes);
}

Check warning on line 124 in core-contracts/Dex/src/main/java/network/balanced/score/core/dex/IRC31StandardSpokeLpToken.java

View check run for this annotation

Codecov / codecov/patch

core-contracts/Dex/src/main/java/network/balanced/score/core/dex/IRC31StandardSpokeLpToken.java#L123-L124

Added lines #L123 - L124 were not covered by tests

protected boolean isNative(NetworkAddress address) {
return address.net().equals(NATIVE_NID);
}

boolean isLockingPool(Integer id) {
return id.equals(SICXICX_POOL_ID);

Check warning on line 127 in core-contracts/Dex/src/main/java/network/balanced/score/core/dex/IRC31StandardSpokeLpToken.java

View check run for this annotation

Codecov / codecov/patch

core-contracts/Dex/src/main/java/network/balanced/score/core/dex/IRC31StandardSpokeLpToken.java#L127

Added line #L127 was not covered by tests
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(){
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -32,12 +34,15 @@ public class TokenTransferTest extends UnitTest {
protected static MockedStatic<Context> contextMock;
protected static MockedStatic<BalancedAddressManager> addressManagerMock;

protected static MockedStatic<TokenTransfer> 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
Expand All @@ -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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e218850

Please sign in to comment.