Skip to content

Commit

Permalink
Merge pull request #7 from icon-community/CPST_CPFT_merge_translation
Browse files Browse the repository at this point in the history
CPS Java Translation PR
  • Loading branch information
theconnectooor authored Nov 30, 2022
2 parents 5976ca5 + 2a3120e commit 1108a02
Show file tree
Hide file tree
Showing 78 changed files with 11,252 additions and 73 deletions.
32 changes: 16 additions & 16 deletions .github/workflows/pr-test.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
name: pr-test

on:
pull_request:
branches:
- '*'

jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Build optimized jar
run: ./gradlew clean build optimizedJar
#name: pr-test
#
#on:
# pull_request:
# branches:
# - '*'
#
#jobs:
# docker:
# runs-on: ubuntu-latest
# steps:
# - name: Checkout
# uses: actions/checkout@v2
#
# - name: Build optimized jar
# run: ./gradlew clean build optimizedJar
32 changes: 16 additions & 16 deletions .github/workflows/push-main.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
name: push-main

on:
push:
branches:
- main

jobs:
docker:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Build optimized jar
run: ./gradlew clean build optimizedJar
#name: push-main
#
#on:
# push:
# branches:
# - main
#
#jobs:
# docker:
# runs-on: ubuntu-latest
# steps:
# - name: Checkout
# uses: actions/checkout@v2
#
# - name: Build optimized jar
# run: ./gradlew clean build optimizedJar
46 changes: 37 additions & 9 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
keystore.json
keystore
gradle.properties
.gradle
# Compiled class file
*.class

# Log file
*.log

# BlueJ files
*.ctxt

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
.idea
keystore.zip
lib
CPSTreasury.iml
CPFTreasury/build/*
CPSTreasury/build/*

.gradle
**/build/
!gradle-wrapper.jar

gradle.properties
*.json

.deployment

.project
.classpath
.settings
**/bin/
4 changes: 4 additions & 0 deletions CPFTreasury/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ version = '0.9.1'

dependencies {
compileOnly 'foundation.icon:javaee-api:0.9.1'
implementation project(':score-lib')

implementation 'com.github.sink772:javaee-tokens:0.6.1'
implementation 'com.github.sink772:minimal-json:0.9.6'

Expand All @@ -26,6 +28,7 @@ deployJar {
lisbon {
uri = 'https://lisbon.net.solidwallet.io/api/v3'
nid = 0x2
to = 'cx3d4182c0e783b7ef97ba63409c4bdf808853bd9f'
}
local {
uri = 'http://localhost:9082/api/v3'
Expand All @@ -45,6 +48,7 @@ deployJar {
keystore = rootProject.hasProperty('keystoreName') ? "$keystoreName" : ''
password = rootProject.hasProperty('keystorePass') ? "$keystorePass" : ''
parameters {
arg('score', 'cx0355e153f269c05f64100d34d3c48a002b404dc5')
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,38 +16,41 @@
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 {

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);

public class CPFTreasury extends SetterGetter implements CPFTreasuryInterface {
public static final VarDB<Address> cpsTreasuryScore = Context.newVarDB(CPS_TREASURY_SCORE, Address.class);
public static final VarDB<Address> cpsScore = Context.newVarDB(CPS_SCORE, Address.class);
public static final VarDB<Address> balancedDollar = Context.newVarDB(BALANCED_DOLLAR, Address.class);
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);
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);

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

public CPFTreasury() {
swapFlag.set(true);
}

private boolean proposalExists(String ipfsKey) {
return proposalBudgets.get(ipfsKey) != null;
}

@Override
@External(readonly = true)
public String name() {
return TAG;
}


@Override
@External
public void setMaximumTreasuryFundIcx(BigInteger _value) {
validateAdmins();
Expand All @@ -59,12 +62,24 @@ public void setMaximumTreasuryFundIcx(BigInteger _value) {
*
* @param _value: value in loop
*/
@Override
@External
public void setMaximumTreasuryFundBnusd(BigInteger _value) {
validateAdmins();
treasuryFundbnUSD.set(_value);
}

@External
public void toggleSwapFlag() {
validateAdmins();
swapFlag.set(!swapFlag.getOrDefault(false));
}

@External(readonly = true)
public boolean getSwapFlag() {
return swapFlag.getOrDefault(false);
}


private void burn(BigInteger amount) {
Context.call(amount, SYSTEM_ADDRESS, "burn");
Expand All @@ -75,6 +90,7 @@ private void burn(BigInteger amount) {
*
* @return map of ICX and bnUSD amount
*/
@Override
@External(readonly = true)
public Map<String, BigInteger> get_total_funds() {
return Map.of(ICX, Context.getBalance(Context.getAddress()),
Expand All @@ -85,6 +101,7 @@ private BigInteger getTotalFundBNUSD() {
return (BigInteger) Context.call(balancedDollar.get(), "balanceOf", Context.getAddress());
}

@Override
@External(readonly = true)
public Map<String, BigInteger> get_remaining_swap_amount() {
BigInteger maxCap = treasuryFundbnUSD.get();
Expand All @@ -98,6 +115,7 @@ private void returnFundAmount(Address address, BigInteger value) {
FundReturned(address, "Sponsor Bond amount " + value + " " + bnUSD + " Returned to CPF Treasury.");
}

@Override
@External
public void transfer_proposal_fund_to_cps_treasury(String _ipfs_key, int _total_installment_count,
Address _sponsor_address, Address _contributor_address,
Expand Down Expand Up @@ -131,6 +149,7 @@ public void transfer_proposal_fund_to_cps_treasury(String _ipfs_key, int _total_
ProposalFundTransferred(_ipfs_key, "Successfully transferred " + totalTransfer + " " + token_flag + " to CPS Treasury " + cpsTreasuryScore.get());
}

@Override
@External
public void update_proposal_fund(String _ipfs_key, @Optional String _flag, @Optional BigInteger _added_budget,
@Optional int _total_installment_count) {
Expand Down Expand Up @@ -174,6 +193,7 @@ private void disqualifyProposalFund(String ipfsKey, BigInteger value) {
ProposalDisqualified(ipfsKey, "Proposal disqualified. " + value + " " + bnUSD + " is returned back to Treasury.");
}

@Override
@External
@Payable
public void add_fund() {
Expand Down Expand Up @@ -206,13 +226,17 @@ 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) {
Address[] path = new Address[]{sICXScore.get(), balancedDollar.get()};
Object[] params = new Object[]{path};
Context.call(amount, routerScore.get(), "route", params);
if (getSwapFlag()) {
Address[] path = new Address[]{sICXScore.get(), balancedDollar.get()};
Object[] params = new Object[]{path};
Context.call(amount, routerScore.get(), "route", params);
}
}

@Override
@External
public void swap_tokens(int _count) {
validateCpsScore();
Expand All @@ -234,35 +258,38 @@ public void swap_tokens(int _count) {
} else {
BigInteger remainingICXToSwap = bnUSDRemainingToSwap.multiply(EXA).divide(icxbnUSDPrice.multiply(BigInteger.valueOf(count)));
BigInteger icxBalance = Context.getBalance(Context.getAddress());
swapCount.set(swapCountValue + 1);
if (remainingICXToSwap.compareTo(icxBalance) > 0) {
remainingICXToSwap = icxBalance;
}

if (remainingICXToSwap.compareTo(BigInteger.valueOf(5).multiply(EXA)) > 0) {
swapIcxBnusd(remainingICXToSwap);
swapCount.set(swapCountValue + 1);
}
}
}
}
}

@Override
@External(readonly = true)
public Map<String, Integer> get_swap_state_status() {
return Map.of("state", swapState.getOrDefault(0), "count", swapCount.getOrDefault(0));
}

@Override
@External
public void reset_swap_state() {
Address cpsScoreAddress = cpsScore.get();
Address caller = Context.getCaller();

boolean checkCaller = caller.equals(cpsScoreAddress) || (Boolean) Context.call(cpsScoreAddress, "is_admin", caller);
boolean checkCaller = caller.equals(cpsScoreAddress) || (Boolean) Context.call(cpsScoreAddress, "isAdmin", caller);
Context.require(checkCaller, TAG + ": Only admin can call this method.");
swapState.set(SwapContinue);
swapCount.set(SwapReset);
}

@Override
@External(readonly = true)
public Map<String, Object> get_proposal_details(@Optional int _start_index, @Optional int _end_index) {
if (_end_index == 0) {
Expand Down Expand Up @@ -294,6 +321,7 @@ public Map<String, Object> get_proposal_details(@Optional int _start_index, @Opt
return Map.of("data", proposalsList, "count", count);
}

@Override
@External
public void tokenFallback(Address _from, BigInteger _value, byte[] _data) {
Address bnUSDScore = balancedDollar.get();
Expand Down Expand Up @@ -340,6 +368,7 @@ public void tokenFallback(Address _from, BigInteger _value, byte[] _data) {
}
}

@Override
@Payable
public void fallback() {
if (Context.getCaller().equals(dexScore.get())) {
Expand All @@ -351,18 +380,22 @@ public void fallback() {


//EventLogs
@Override
@EventLog(indexed = 1)
public void FundReturned(Address _sponsor_address, String note) {
}

@Override
@EventLog(indexed = 1)
public void ProposalFundTransferred(String _ipfs_key, String note) {
}

@Override
@EventLog(indexed = 1)
public void ProposalDisqualified(String _ipfs_key, String note) {
}

@Override
@EventLog(indexed = 1)
public void FundReceived(Address _sponsor_address, String note) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class Constants {

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

public static final int sICXICXPoolID = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class Validations {
public static void validateAdmins() {
Context.require((Boolean) Context.call(CPFTreasury.cpsScore.get(), "is_admin", Context.getCaller()),
Context.require((Boolean) Context.call(CPFTreasury.cpsScore.get(), "isAdmin", Context.getCaller()),
TAG + ": Only Admins can call this method");

}
Expand Down
Loading

0 comments on commit 1108a02

Please sign in to comment.