Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
a832ca7
cross chain lp functionalities and unit test
sagars Oct 2, 2024
3d20a79
Merge branch 'main' into feature/crosschain_lp
sagars Oct 3, 2024
24eed8e
integration test and fixes - crosschain lp feature
sagars Oct 8, 2024
39aaec5
integration test issues fixed
sagars Oct 23, 2024
ad6b8ac
Token transfer utility implementation, crossTransfer commented out
sagars Oct 26, 2024
1079d2f
Merge remote-tracking branch 'origin/feat/sicx-baln' into feat/crossc…
sagars Oct 27, 2024
558117e
test: all test pass including router update
sagars Oct 28, 2024
ea47433
Merge branch 'main' into feature/crosschain_lp
sagars Oct 29, 2024
eaa39ef
Fixes: review fixes
sagars Oct 29, 2024
dbcb461
Fix: review additional fixes, cleanups, formats
sagars Oct 30, 2024
1b20143
to param optional on xTokenFallback deposit, xWithdraw replaced with …
sagars Nov 6, 2024
fd54215
unit tests for optional to in xTokenFallback and xWithdraw on xAdd
sagars Nov 7, 2024
73adf70
fix: Address type changed to String type on xcall annotated methods
sagars Nov 15, 2024
3be7c9b
crosschain lp feature implementation guidance doc
sagars Nov 15, 2024
465d286
fallback method added to rewards and stakedLp, router tested
sagars Nov 18, 2024
102174f
fallback method removed from stakedLp contract
sagars Nov 19, 2024
14ad807
unused variables, imports and commented codes removed
sagars Nov 20, 2024
f792a5e
unused variables, imports and commented codes removed
sagars Nov 20, 2024
0973021
addressManagerMock static mock issue on TokenTransfer test is fixed
sagars Nov 21, 2024
e218850
test updates on xcall, transfer method merged in token class,exceptio…
sagars Nov 21, 2024
911856f
onIRC31Received ib lp transfer method, depositOfUser removed..
sagars Nov 22, 2024
68d6cbf
getDeposit method params position updated in dex inttest
sagars Nov 22, 2024
a00b28c
optional data on route methods - Router
sagars Nov 22, 2024
7a326cf
NetworkAddress Improvements
sagars Nov 22, 2024
1f487ca
NetworkAddress missed - fixed
sagars Nov 22, 2024
db53648
RouteData update to add data property, unit tests added for usdc staking
sagars Nov 26, 2024
53056e1
crosschain lp functionalities and unitest
sagars Nov 26, 2024
b7ea1fc
int test completed
sagars Nov 29, 2024
4c2f87a
Merge branch 'main' into feat/usdc_staking
sagars Nov 29, 2024
c970764
re-fix the previous update
sagars Nov 29, 2024
40114e5
Merge branch 'main' into feat/crosschain_savings
sagars Nov 29, 2024
6f6f668
crosschain savings documentation
sagars Dec 8, 2024
5bee6e8
Merge branch 'feat/usdc_staking' into feat/crosschain_savings
sagars Dec 8, 2024
77dd47e
minor fix
sagars Dec 10, 2024
251df28
RouteData version compatibility issue fixed
sagars Dec 10, 2024
4eb1254
unit test for old routerData struct, use of contains, sicx version fix
sagars Dec 18, 2024
20a0feb
Merge branch 'main' into feat/crosschain_savings
sagars Dec 18, 2024
3d864ec
data structure added on docs
sagars Dec 26, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,18 @@ public void xTokenFallback(String _from, BigInteger _value, byte[] _data) {
executeRoute(_from, _data);
}

private RouteData getRouteData(byte[] data){
RouteData routeData;
try {
routeData = RouteData.fromBytes(data);
}catch(IllegalStateException ignored ){
routeData = RouteData.fromBytesOld(data);
}
return routeData;
}

private void executeRoute(String _from, byte[] data) {
RouteData routeData = RouteData.fromBytes(data);
RouteData routeData = getRouteData(data);
Context.require(routeData.method.contains("_swap"), TAG + ": Fallback directly not allowed.");

Address fromToken = Context.getCaller();
Expand All @@ -261,8 +271,11 @@ private void executeRoute(String _from, byte[] data) {
} else {
receiver = _from;
}

route(receiver, fromToken, routeData.actions, minimumReceive, EMPTY_DATA);
byte[] _data = EMPTY_DATA;
if(routeData.data!=null){
_data = routeData.data;
}
route(receiver, fromToken, routeData.actions, minimumReceive, _data);
}

private void jsonRoute(String _from, byte[] data) {
Expand Down Expand Up @@ -308,7 +321,11 @@ private void jsonRoute(String _from, byte[] data) {
}

Address fromToken = Context.getCaller();
route(receiver, fromToken, actions, minimumReceive, EMPTY_DATA);
byte[] _data = EMPTY_DATA;
if(params.contains("data")){
_data = params.get("data").asString().getBytes();
}
route(receiver, fromToken, actions, minimumReceive, _data);
}

@Payable
Expand All @@ -318,4 +335,4 @@ public void fallback() {
@EventLog(indexed = 1)
public void Route(Address from, BigInteger fromAmount, Address to, BigInteger toAmount) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,10 @@
import network.balanced.score.lib.structs.RouteData;
import network.balanced.score.lib.test.mock.MockBalanced;
import network.balanced.score.lib.test.mock.MockContract;
import network.balanced.score.lib.tokens.HubTokenImpl;
import network.balanced.score.lib.tokens.IRC2Base;
import network.balanced.score.lib.tokens.IRC2Mintable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import score.Address;
import score.Context;
import scorex.util.ArrayList;

import java.math.BigInteger;
Expand All @@ -44,11 +40,8 @@

import static network.balanced.score.core.router.RouterImpl.*;
import static network.balanced.score.lib.test.UnitTest.*;
import static network.balanced.score.lib.utils.Constants.EXA;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -182,14 +175,7 @@ void tokenFallback() throws Exception {
when(balanced.baln.mock.balanceOf(routerScore.getAddress())).thenReturn(BigInteger.TEN);
when(balanced.sicx.mock.balanceOf(routerScore.getAddress())).thenReturn(BigInteger.TEN);

// byte[] invalidPathWithSicxTerminalToken = tokenData("_swap", Map.of("path",
// new Object[]{balanced.baln.getAddress().toString(), null}));
// Executable nonSicxIcxTrade = () -> routerScore.invoke(sicxScore.account, "tokenFallback", owner.getAddress(),
// BigInteger.TEN, invalidPathWithSicxTerminalToken);
// expectedErrorMessage = "Reverted(0): " + TAG + ": Native swaps not available to icon from " + balanced.baln.getAddress();
// expectErrorMessage(nonSicxIcxTrade, expectedErrorMessage);
//
// resetInRoute();

Account newReceiver = sm.createAccount();
byte[] pathWithSicxTerminalToken = tokenData("_swap", Map.of("path",
new Object[]{sicxScore.getAddress().toString(), null}, "receiver",
Expand Down Expand Up @@ -501,7 +487,42 @@ void tokenFallback_swapStable() throws Exception {
}

Account newReceiver = sm.createAccount();
byte[] data = new RouteData("_swap", newReceiver.getAddress().toString(), BigInteger.ZERO, actions).toBytes();
byte[] data = new RouteData("_swap", newReceiver.getAddress().toString(), BigInteger.ZERO, actions, null).toBytes();

// Act
routerScore.invoke(balanced.baln.account, "tokenFallback", owner.getAddress(), balnToSwap,
data);

// Assert
int i = 0;
for (MockContract<IRC2> token : tokens) {
if (i < tokens.size() - 1) {
byte[] d = tokens.get(i + 1).getAddress().toString().getBytes();
verify(token.mock).transfer(balanced.stability.getAddress(), balnToSwap, d);
}
i++;
}
}

@Test
void tokenFallback_swapStableOldRouteData() throws Exception {
// Arrange
BigInteger balnToSwap = BigInteger.TEN.multiply(ICX);
List<RouteAction> actions = new ArrayList<>(MAX_NUMBER_OF_ITERATIONS);
List<MockContract<IRC2>> tokens = new ArrayList<>(MAX_NUMBER_OF_ITERATIONS - 1);
for (int i = 0; i < MAX_NUMBER_OF_ITERATIONS; i++) {
if (i == 0) {
actions.add(new RouteAction(SWAP, balanced.sicx.getAddress()));
continue;
}
MockContract<IRC2> token = new MockContract<>(IRC2ScoreInterface.class, IRC2.class, sm, owner);
when(token.mock.balanceOf(routerScore.getAddress())).thenReturn(balnToSwap);
actions.add(new RouteAction(STABILITY_SWAP, token.getAddress()));
tokens.add(token);
}

Account newReceiver = sm.createAccount();
byte[] data = new RouteData("_swap", newReceiver.getAddress().toString(), BigInteger.ZERO, actions, null).toBytesOld();

// Act
routerScore.invoke(balanced.baln.account, "tokenFallback", owner.getAddress(), balnToSwap,
Expand Down Expand Up @@ -531,7 +552,30 @@ void tokenFallback_swapToICX() throws Exception {
routerScore.getAccount().addBalance("ICX", ICXResult);

Account newReceiver = sm.createAccount();
byte[] data = new RouteData("_swap", newReceiver.getAddress().toString(), BigInteger.ZERO, actions).toBytes();
byte[] data = new RouteData("_swap", newReceiver.getAddress().toString(), BigInteger.ZERO, actions, null).toBytes();

// Act
routerScore.invoke(balanced.baln.account, "tokenFallback", owner.getAddress(), USDToSwap,
data);

// Assert
assertEquals(ICXResult, newReceiver.getBalance());
}

@Test
void tokenFallback_swapToICXOldRouteData() throws Exception {
// Arrange
BigInteger USDToSwap = BigInteger.TEN.multiply(ICX);
BigInteger sICXResult = BigInteger.valueOf(100).multiply(ICX);
BigInteger ICXResult = BigInteger.valueOf(110).multiply(ICX);
List<RouteAction> actions = new ArrayList<>(2);
actions.add(new RouteAction(SWAP, balanced.sicx.getAddress()));
actions.add(new RouteAction(SWAP, null));
when(balanced.sicx.mock.balanceOf(routerScore.getAddress())).thenReturn(sICXResult);
routerScore.getAccount().addBalance("ICX", ICXResult);

Account newReceiver = sm.createAccount();
byte[] data = new RouteData("_swap", newReceiver.getAddress().toString(), BigInteger.ZERO, actions, null).toBytesOld();

// Act
routerScore.invoke(balanced.baln.account, "tokenFallback", owner.getAddress(), USDToSwap,
Expand Down Expand Up @@ -586,4 +630,51 @@ private void resetInRoute() {
// in Production this happens between each tx
((RouterImpl)routerScore.getInstance()).inRoute = false;
}

@Test
void stakeToSavingsWithJSONRoute(){
// Arrange
//baln as usdc
when(balanced.baln.mock.balanceOf(routerScore.getAddress())).thenReturn(BigInteger.TEN);
when(balanced.bnUSD.mock.balanceOf(routerScore.getAddress())).thenReturn(BigInteger.TEN);

String data = new String(tokenData("_lock", Map.of()));
Account newReceiver = balanced.savings.account;
byte[] pathWithUSDCBnUSD = tokenData("_swap", Map.of("path",
new Object[]{balanced.bnUSD.getAddress().toString()}, "receiver",
newReceiver.getAddress().toString(), "data", data));

// Act
routerScore.invoke(balanced.baln.account, "tokenFallback", owner.getAddress(), BigInteger.TEN,
pathWithUSDCBnUSD);


// Verify
verify(balanced.bnUSD.mock).transfer(balanced.savings.getAddress(), BigInteger.TEN, data.getBytes());
}

@Test
void stakeToSavingsWithRLPData(){
// Arrange
//baln as usdc
BigInteger balnToSwap = BigInteger.TEN.multiply(ICX);
when(balanced.baln.mock.balanceOf(routerScore.getAddress())).thenReturn(balnToSwap);
when(balanced.bnUSD.mock.balanceOf(routerScore.getAddress())).thenReturn(balnToSwap);

List<RouteAction> actions = new ArrayList<>(1);
actions.add(new RouteAction(SWAP, balanced.bnUSD.getAddress()));

byte[] data = tokenData("_lock", Map.of());
Account newReceiver = balanced.savings.account;
byte[] routeData = new RouteData("_swap", newReceiver.getAddress().toString(), BigInteger.ZERO, actions, data).toBytes();

// Act
routerScore.invoke(balanced.baln.account, "tokenFallback", owner.getAddress(), balnToSwap,
routeData);


// Verify
verify(balanced.bnUSD.mock).transfer(balanced.savings.getAddress(), balnToSwap, data);
}

}
1 change: 1 addition & 0 deletions core-contracts/Savings/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ dependencies {
implementation Dependencies.javaeeScorex
implementation Dependencies.minimalJson
implementation project(':score-lib')
implementation 'xyz.venture23:xcall-lib:2.1.0'

testImplementation Dependencies.javaeeUnitTest
testImplementation Dependencies.javaeeTokens
Expand Down
Loading
Loading