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: Add function to manually configure available amounts #11

Merged
merged 2 commits into from
May 3, 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
21 changes: 19 additions & 2 deletions network-owned-liquidity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ LiquidityOrder {

```java
/**
* Configures a new liquidity order
* Configures a liquidity order
*
* @param pid The poolId on the balanced dex
* @param limit The max ICX limit to purchase for each Order Period
Expand All @@ -49,14 +49,31 @@ public void configureOrder(BigInteger pid, BigInteger limit) {
OnlyICONGovernance()
order = LiquidityOrder {
limit = limit,
remaining = limit,
remaining = 0,
lastPurchaseBlock = Context.getBlockHeight()
}

orders.set(pid, order);
}
```

```java
/**
* Configures the remaining amount of a liquidity order.
*
* @param pid The poolId on the balanced dex
* @param amount The amount to make available for purchase, max limited by the order limit.
*/

@External
public void setAvailableAmount(BigInteger pid, BigInteger amount) {
OnlyICONGovernance();
order = orders.get(pid);
order.remaining = amount;
orders.set(pid, order);
}
```

```java
/**
* Removes a liquidity order
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ public class NetworkOwnedLiquidity implements INetworkOwnedLiquidity {
public static final BigInteger DEFAULT_SWAP_REWARDS = BigInteger.valueOf(100); // 1%
public static final BigInteger DEFAULT_LP_SLIPPAGE = BigInteger.valueOf(100); // 1%

public NetworkOwnedLiquidity(Address _balancedDex, Address _balancedOracle) {
balancedDex.set(_balancedDex);
balancedOracle.set(_balancedOracle);

orderPeriod.set(DEFAULT_ORDER_PERIOD);
swapReward.set(DEFAULT_SWAP_REWARDS);
lPSlippage.set(DEFAULT_LP_SLIPPAGE);
public NetworkOwnedLiquidity(@Optional Address _balancedDex, @Optional Address _balancedOracle) {
if (balancedDex.get() == null) {
balancedDex.set(_balancedDex);
balancedOracle.set(_balancedOracle);

orderPeriod.set(DEFAULT_ORDER_PERIOD);
swapReward.set(DEFAULT_SWAP_REWARDS);
lPSlippage.set(DEFAULT_LP_SLIPPAGE);
}
}

@EventLog(indexed = 1)
Expand Down Expand Up @@ -143,16 +145,22 @@ public BigInteger getInvestedEmissions() {
@External
public void configureOrder(BigInteger pid, BigInteger limit) {
onlyOwner();
LiquidityOrder order = new LiquidityOrder();
LiquidityOrder order = orders.getOrDefault(pid, new LiquidityOrder());
order.limit = limit;
order.lastPurchaseBlock = BigInteger.valueOf(Context.getBlockHeight());;
order.remaining = limit;
orders.set(pid, order);
if (!DBUtils.arrayDbContains(ordersList, pid)) {
ordersList.add(pid);
}
}

@External
public void setAvailableAmount(BigInteger pid, BigInteger amount) {
onlyOwner();
LiquidityOrder order = orders.get(pid);
order.remaining = amount;
orders.set(pid, order);
}

@External
public void removeOrder(BigInteger pid) {
onlyOwner();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ public void configureOrder_add() {
assertEquals(pid1, orders[0].pid);
assertEquals(limit1, orders[0].limit);
assertEquals(getCurrentBlock().subtract(BigInteger.ONE), orders[0].lastPurchaseBlock);
assertEquals(limit1, orders[0].remaining);
assertEquals(BigInteger.ZERO, orders[0].remaining);

assertEquals(pid2, orders[1].pid);
assertEquals(limit2, orders[1].limit);
assertEquals(getCurrentBlock(), orders[1].lastPurchaseBlock);
assertEquals(limit2, orders[1].remaining);
assertEquals(BigInteger.ZERO, orders[1].remaining);
}

@Test
Expand All @@ -98,8 +98,8 @@ public void configureOrder_reconfigure() {

assertEquals(pid1, orders[0].pid);
assertEquals(limit2, orders[0].limit);
assertEquals(getCurrentBlock(), orders[0].lastPurchaseBlock);
assertEquals(limit2, orders[0].remaining);
assertEquals(getCurrentBlock().subtract(BigInteger.ONE), orders[0].lastPurchaseBlock);
assertEquals(BigInteger.ZERO, orders[0].remaining);
}

@Test
Expand All @@ -123,7 +123,7 @@ public void configureOrder_remove() {
assertEquals(pid2, orders[0].pid);
assertEquals(limit2, orders[0].limit);
assertEquals(getCurrentBlock().subtract(BigInteger.ONE), orders[0].lastPurchaseBlock);
assertEquals(limit2, orders[0].remaining);
assertEquals(BigInteger.ZERO, orders[0].remaining);
}

@Test
Expand All @@ -134,6 +134,7 @@ public void swapLPTokens_simple() {
BigInteger amount = BigInteger.TEN.multiply(EXA);
BigInteger pid = BigInteger.ONE;
networkOwnedLiquidity.invoke(governance, "configureOrder", pid, BigInteger.valueOf(100000).multiply(EXA));
sm.getBlock().increase(NetworkOwnedLiquidity.DEFAULT_ORDER_PERIOD.longValue());

Address baseToken = sICX.getAddress();
Address quoteToken = bnUSD.getAddress();
Expand Down Expand Up @@ -198,6 +199,8 @@ public void swapLPTokens_multipleOrders() {
BigInteger pid2 = BigInteger.TWO;
networkOwnedLiquidity.invoke(governance, "configureOrder", pid1, EXA.multiply(EXA));
networkOwnedLiquidity.invoke(governance, "configureOrder", pid2, EXA.multiply(EXA));
networkOwnedLiquidity.invoke(governance, "setAvailableAmount", pid1, EXA.multiply(EXA));
networkOwnedLiquidity.invoke(governance, "setAvailableAmount", pid2, EXA.multiply(EXA));

Address baseToken1 = sICX.getAddress();
Address baseToken2 = sARCH.getAddress();
Expand Down Expand Up @@ -278,6 +281,7 @@ public void swapLPTokens_overSlippageLimit() {
BigInteger amount = BigInteger.TEN.multiply(EXA);
BigInteger pid = BigInteger.ONE;
networkOwnedLiquidity.invoke(governance, "configureOrder", pid, EXA.multiply(EXA));
networkOwnedLiquidity.invoke(governance, "setAvailableAmount", pid, EXA.multiply(EXA));

Address baseToken = sICX.getAddress();
Address quoteToken = bnUSD.getAddress();
Expand Down Expand Up @@ -356,6 +360,7 @@ public void swapLPTokens_aboveLimit() {
BigInteger expectedRewards = expectedUSDRewards.multiply(EXA).divide(ICXPriceInUSD);
networkOwnedLiquidity.getAccount().addBalance(expectedRewards.multiply(BigInteger.valueOf(3)));
networkOwnedLiquidity.invoke(governance, "configureOrder", pid, expectedRewards.multiply(BigInteger.TWO));
networkOwnedLiquidity.invoke(governance, "setAvailableAmount", pid, expectedRewards.multiply(BigInteger.TWO));

// Act
networkOwnedLiquidity.invoke(dex.account, "onIRC31Received", user.getAddress(), user.getAddress(), pid, amount,
Expand Down Expand Up @@ -389,6 +394,7 @@ public void swapLPTokens_non18Decimal() {
BigInteger amount = BigInteger.TEN.multiply(EXA);
BigInteger pid = BigInteger.ONE;
networkOwnedLiquidity.invoke(governance, "configureOrder", pid, EXA.multiply(EXA));
networkOwnedLiquidity.invoke(governance, "setAvailableAmount", pid, EXA.multiply(EXA));

Address baseToken = sICX.getAddress();
Address quoteToken = bnUSD.getAddress();
Expand Down Expand Up @@ -442,6 +448,7 @@ public void swapLPTokens_nonUSDQuote() {
BigInteger amount = BigInteger.TEN.multiply(EXA);
BigInteger pid = BigInteger.ONE;
networkOwnedLiquidity.invoke(governance, "configureOrder", pid, EXA.multiply(EXA));
networkOwnedLiquidity.invoke(governance, "setAvailableAmount", pid, EXA.multiply(EXA));

Address baseToken = sARCH.getAddress();
Address quoteToken = sICX.getAddress();
Expand Down Expand Up @@ -500,6 +507,7 @@ public void swapLPTokens_missPriced() {
BigInteger amount = BigInteger.TEN.multiply(EXA);
BigInteger pid = BigInteger.ONE;
networkOwnedLiquidity.invoke(governance, "configureOrder", pid, EXA.multiply(EXA));
networkOwnedLiquidity.invoke(governance, "setAvailableAmount", pid, EXA.multiply(EXA));

Address baseToken = sICX.getAddress();
Address quoteToken = bnUSD.getAddress();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,24 @@ public interface INetworkOwnedLiquidity {
BigInteger getInvestedEmissions();

/**
* Configures a new liquidity order
* Configures a liquidity order
*
* @param pid The poolId on the balanced dex
* @param limit The max USD limit to purchase for each Order Period
*/
@External
void configureOrder(BigInteger pid, BigInteger limit);


/**
* Configures the remaining amount of a liquidity order
*
* @param pid The poolId on the balanced dex
* @param amount The amount to make available for purchase, max limited by the order limit.
*/
@External
void setAvailableAmount(BigInteger pid, BigInteger amount);

/**
* Removes a liquidity order
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import score.ObjectReader;
import score.ObjectWriter;
import score.Context;

import java.math.BigInteger;

Expand All @@ -12,6 +13,11 @@ public class LiquidityOrder {
// return value only
public BigInteger pid;

public LiquidityOrder() {
lastPurchaseBlock = BigInteger.valueOf(Context.getBlockHeight());
remaining = BigInteger.ZERO;
}

public static void writeObject(ObjectWriter writer, LiquidityOrder liquidityOrder) {
liquidityOrder.writeObject(writer);
}
Expand Down
Loading