Skip to content

Commit

Permalink
Merge pull request #16 from icon-community/hotfix/treasury-swap-fix
Browse files Browse the repository at this point in the history
Hotfix/treasury swap fix
  • Loading branch information
sdpisreddevil authored Aug 10, 2023
2 parents 1108a02 + 9428707 commit b844827
Show file tree
Hide file tree
Showing 9 changed files with 227 additions and 84 deletions.
15 changes: 6 additions & 9 deletions CPFTreasury/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,19 @@ deployJar {
uri = 'http://localhost:9082/api/v3'
nid = 0x3
}
sejong {
uri = 'https://sejong.net.solidwallet.io/api/v3'
nid = 0x53

}
berlin {
uri = 'https://berlin.net.solidwallet.io/api/v3'
nid = 0x7
to = 'cx28ae7ed3b07ed5247a3d2f97680f8555ce7c0a92'
}
mainnet {
uri = 'https://ctz.solidwallet.io/api/v3'
nid = 0x1
to = 'cxdca1178010b5368aea929ad5c06abee64b91acc2'
}

}
keystore = rootProject.hasProperty('keystoreName') ? "$keystoreName" : ''
password = rootProject.hasProperty('keystorePass') ? "$keystorePass" : ''
parameters {
arg('score', 'cx0355e153f269c05f64100d34d3c48a002b404dc5')
}
}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
import community.icon.cps.score.lib.interfaces.CPFTreasuryInterface;
import score.*;
import score.annotation.EventLog;
import score.annotation.External;
Expand All @@ -16,7 +17,6 @@
import static community.icon.cps.score.cpftreasury.Constants.*;
import static community.icon.cps.score.cpftreasury.Validations.validateAdmins;
import static community.icon.cps.score.cpftreasury.Validations.validateCpsScore;
import community.icon.cps.score.lib.interfaces.CPFTreasuryInterface;

public class CPFTreasury extends SetterGetter implements CPFTreasuryInterface {
public static final VarDB<Address> cpsTreasuryScore = Context.newVarDB(CPS_TREASURY_SCORE, Address.class);
Expand All @@ -25,18 +25,28 @@ public class CPFTreasury extends SetterGetter implements CPFTreasuryInterface {
public static final VarDB<Address> dexScore = Context.newVarDB(DEX_SCORE, Address.class);
public static final VarDB<Address> sICXScore = Context.newVarDB(SICX_SCORE, Address.class);
public static final VarDB<Address> routerScore = Context.newVarDB(ROUTER_SCORE, Address.class);
public static final VarDB<Address> oracleAddress = Context.newVarDB(ORACLE_ADDRESS, Address.class);
private final ArrayDB<String> proposalsKeys = Context.newArrayDB(PROPOSALS_KEYS, String.class);
private final DictDB<String, BigInteger> proposalBudgets = Context.newDictDB(PROPOSAL_BUDGETS, BigInteger.class);
private final VarDB<BigInteger> treasuryFund = Context.newVarDB(TREASURY_FUND, BigInteger.class);
private final VarDB<BigInteger> treasuryFundbnUSD = Context.newVarDB(TREASURY_FUND_BNUSD, BigInteger.class);

private final VarDB<Integer> swapState = Context.newVarDB(SWAP_STATE, Integer.class);
private final VarDB<Integer> swapCount = Context.newVarDB(SWAP_COUNT, Integer.class);
private final VarDB<Integer> oraclePerDiff = Context.newVarDB(ORACLE_PERCENTAGE_DIFF, Integer.class);

private final VarDB<Boolean> swapFlag = Context.newVarDB(SWAP_FLAG, Boolean.class);

public CPFTreasury() {
swapFlag.set(true);
if (treasuryFund.get() == null) {
treasuryFund.set(BigInteger.valueOf(1000000).multiply(EXA));
swapCount.set(SwapReset);
swapState.set(SwapReset);
swapFlag.set(false);
}
oraclePerDiff.set(5);
oracleAddress.set(Address.fromString("cxe647e0af68a4661566f5e9861ad4ac854de808a2"));

}

private boolean proposalExists(String ipfsKey) {
Expand Down Expand Up @@ -217,6 +227,11 @@ private void burnExtraFund() {
}
}

@External(readonly = true)
public int getPerDiff() {
return oraclePerDiff.getOrDefault(0);
}

private void swapTokens(Address _from, Address _to, BigInteger _amount) {
JsonObject swapData = new JsonObject();
swapData.add("method", "_swap");
Expand All @@ -226,14 +241,42 @@ private void swapTokens(Address _from, Address _to, BigInteger _amount) {
Context.call(_from, "transfer", dexScore.get(), _amount, swapData.toString().getBytes());
}

@Override
@External
public void swapIcxBnusd(BigInteger amount) {
private void swapIcxBnusd(BigInteger amount, BigInteger _minReceive) {
if (getSwapFlag()) {
Address[] path = new Address[]{sICXScore.get(), balancedDollar.get()};
Object[] params = new Object[]{path};
Context.call(amount, routerScore.get(), "route", params);
BigInteger icxPrice = getOraclePrice();
int diffValue = 100 - oraclePerDiff.getOrDefault(2);
BigInteger minReceive = icxPrice.multiply(BigInteger.valueOf(diffValue)).divide(BigInteger.valueOf(100));
if (_minReceive.equals(BigInteger.ZERO)) {
_minReceive = minReceive;
} else if (_minReceive.compareTo(minReceive) < 0) {
_minReceive = minReceive;
}

Context.call(amount, routerScore.get(), "route", path, _minReceive);
}
}

@Override
@External
public void swapICXToBnUSD(BigInteger amount, @Optional BigInteger _minReceive) {
validateAdmins();
if (!getSwapFlag()) {
Context.revert(TAG + "SwapTurnedOff.");
}
if (_minReceive == null) {
_minReceive = BigInteger.ZERO;
}
swapIcxBnusd(amount, _minReceive);
}

@External(readonly = true)
public BigInteger getOraclePrice() {
String quote = "USD";

Map<String, BigInteger> priceData = (Map<String, BigInteger>) Context.call(oracleAddress.get(), "get_reference_data", ICX, quote);
return priceData.get("rate");

}

@Override
Expand Down Expand Up @@ -264,7 +307,7 @@ public void swap_tokens(int _count) {
}

if (remainingICXToSwap.compareTo(BigInteger.valueOf(5).multiply(EXA)) > 0) {
swapIcxBnusd(remainingICXToSwap);
swapIcxBnusd(remainingICXToSwap, BigInteger.ZERO);
}
}
}
Expand All @@ -289,6 +332,12 @@ public void reset_swap_state() {
swapCount.set(SwapReset);
}

@External
public void setOraclePercentageDifference(int _value) {
validateAdmins();
oraclePerDiff.set(_value);
}

@Override
@External(readonly = true)
public Map<String, Object> get_proposal_details(@Optional int _start_index, @Optional int _end_index) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ public class Constants {
public static final String DEX_SCORE = "dex_score";
public static final String SICX_SCORE = "sicx_score";
public static final String ROUTER_SCORE = "router_score";
public static final String ORACLE_ADDRESS = "oracle_address";

public static final String SWAP_STATE = "swap_state";
public static final String SWAP_COUNT = "swap_count";
public static final String ORACLE_PERCENTAGE_DIFF = "oracle_percentage_diff";
public static final String SWAP_FLAG = "swap_flag";
public static final Address SYSTEM_ADDRESS = Address.fromString("cx0000000000000000000000000000000000000000");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,21 @@ public void setRouterScore(Address _score) {
public Address getRouterScore() {
return CPFTreasury.routerScore.get();
}

@External
public void setOracleAddress(Address _score) {
Validations.validateAdminScore(_score);
CPFTreasury.oracleAddress.set(_score);
}

/**
* Returns the router score address
*
* @return router score address
*/
@External(readonly = true)
public Address getOracleAddress() {
return CPFTreasury.oracleAddress.get();
}

}
Loading

0 comments on commit b844827

Please sign in to comment.