Skip to content

Commit

Permalink
move some of the logic from the controller into the CosmosService mod…
Browse files Browse the repository at this point in the history
…el class and set up events for updating the UI.

fixing a messed up pull and rebase
  • Loading branch information
dekm committed Mar 12, 2024
1 parent 15ad48c commit a5c3436
Show file tree
Hide file tree
Showing 15 changed files with 421 additions and 391 deletions.
220 changes: 83 additions & 137 deletions fx/src/main/java/org/unigrid/janus/controller/CosmosController.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ of the License (see COPYING and COPYING.addendum).
import java.util.logging.Logger;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -94,20 +93,14 @@ of the License (see COPYING and COPYING.addendum).
import org.unigrid.janus.model.MnemonicModel;
import org.unigrid.janus.model.rest.entity.CollateralRequired;
import org.unigrid.janus.model.rest.entity.DelegationsRequest;
import org.unigrid.janus.model.rest.entity.GridnodeDelegationAmount;
import org.unigrid.janus.model.rest.entity.RedelegationsRequest;
import org.unigrid.janus.model.rest.entity.RewardsRequest;
import org.unigrid.janus.model.rest.entity.RewardsRequest.Balance;
import org.unigrid.janus.model.rest.entity.UnbondingDelegationsRequest;
import org.unigrid.janus.model.rest.entity.WithdrawAddressRequest;
import org.unigrid.janus.model.rpc.entity.TransactionResponse;
import org.unigrid.janus.model.rpc.entity.TransactionResponse.TxResponse;
import org.unigrid.janus.model.service.AccountsService;
import org.unigrid.janus.model.service.AddressCosmosService;
import org.unigrid.janus.model.service.CosmosRestClient;
import org.unigrid.janus.model.service.CosmosService;
import org.unigrid.janus.model.service.GrpcService;
import org.unigrid.janus.model.service.MnemonicService;
import org.unigrid.janus.model.service.RestCommand;
import org.unigrid.janus.model.service.RestService;
import org.unigrid.janus.model.signal.DisplaySwapPrompt;
import org.unigrid.janus.model.signal.MnemonicState;
Expand All @@ -117,7 +110,6 @@ of the License (see COPYING and COPYING.addendum).
import org.unigrid.janus.view.backing.CosmosTxList;
import java.math.BigInteger;
import org.bouncycastle.util.encoders.Hex;
import org.bouncycastle.util.encoders.Base64;
import cosmos.base.abci.v1beta1.Abci;
import cosmos.staking.v1beta1.QueryOuterClass.QueryDelegatorDelegationsRequest;
import cosmos.staking.v1beta1.QueryOuterClass.QueryDelegatorDelegationsResponse;
Expand All @@ -136,7 +128,6 @@ of the License (see COPYING and COPYING.addendum).
import javafx.scene.input.MouseEvent;
import javafx.scene.paint.Paint;
import org.apache.commons.lang3.SystemUtils;
import org.bitcoinj.crypto.TransactionSignature;
import org.controlsfx.control.Notifications;
import org.kordamp.ikonli.javafx.FontIcon;
import org.unigrid.janus.model.ValidatorInfo;
Expand All @@ -146,6 +137,15 @@ of the License (see COPYING and COPYING.addendum).
import org.unigrid.janus.model.gridnode.GridnodeData;
import org.unigrid.janus.model.gridnode.GridnodeModel;
import org.unigrid.janus.model.ApiConfig;
import org.unigrid.janus.model.rest.entity.RedelegationsRequest.RedelegationResponseEntry;
import org.unigrid.janus.model.rest.entity.UnbondingDelegationsRequest.UnbondingResponse;
import org.unigrid.janus.model.signal.CollateralUpdateEvent;
import org.unigrid.janus.model.signal.DelegationAmountEvent;
import org.unigrid.janus.model.signal.DelegationListEvent;
import org.unigrid.janus.model.signal.RedelegationsEvent;
import org.unigrid.janus.model.signal.RewardsEvent;
import org.unigrid.janus.model.signal.UnbondingDelegationsEvent;
import org.unigrid.janus.model.signal.WithdrawAddressEvent;

@ApplicationScoped
public class CosmosController implements Initializable {
Expand All @@ -161,7 +161,7 @@ public class CosmosController implements Initializable {
@Inject
private AccountsData accountsData;
@Inject
private CosmosRestClient cosmosClient;
private CosmosService cosmosClient;
@Inject
private MnemonicModel mnemonicModel;
@Inject
Expand All @@ -182,6 +182,8 @@ public class CosmosController implements Initializable {
private GrpcService grpcService;
@Inject
private GridnodeModel gridnodeModel;
@Inject
private CosmosService cosmosService;

private Account currentSelectedAccount;
@FXML
Expand Down Expand Up @@ -732,20 +734,6 @@ private void decryptPrivateKey(ActionEvent event) {
}
}

// @FXML
// private void decrypTest(ActionEvent event) {
// try {
//
// // Decrypt the mnemonic
// System.out.println("encryptedPrivateKey: " + keytoDecrypt.getText());
// String keys = cryptoUtils.decrypt(keytoDecrypt.getText(), "pickles");
// decryptField.setText(keys);
//
// } catch (Exception ex) {
// Logger.getLogger(CosmosController.class.getName()).log(Level.SEVERE, null,
// ex);
// }
// }
public void handleSaveAddress(AddressCosmos newAddress) {
try {
addressService.saveAddress(newAddress);
Expand Down Expand Up @@ -902,9 +890,22 @@ private void onErrorMessage(String message, Text sendWarnMsg) {
}

/* DELEGATION LIST VIEW */
public void setDelegations(List<DelegationsRequest.DelegationResponse> delegations) {
public void onDelegationListEvent(@Observes DelegationListEvent event) {
Platform.runLater(() -> {
delegationsListView.getItems().setAll(event.getDelegationList());
});
}

// signal event for staking rewards
public void onRewardsEvent(@Observes RewardsEvent event) {
Platform.runLater(() -> {
stakingRewardsValue(event.getRewardsResponse().getTotal());
});
}

public void onWithdrawAddressEvent(@Observes WithdrawAddressEvent event) {
Platform.runLater(() -> {
delegationsListView.getItems().setAll(delegations);
System.out.println("getWithdrawAddress: " + event.getWithdrawAddress());
});
}

Expand All @@ -931,6 +932,38 @@ protected void updateItem(Balance item, boolean empty) {
});
}

/* STAKING */
public void onRedelegationsEvent(@Observes RedelegationsEvent event) {
Platform.runLater(() -> {
// Update the view with redelegations data
updateRedelegationsView(event.getRedelegationsResponse().getRedelegationResponses());
});
}

// The method to update the view with redelegations data
private void updateRedelegationsView(List<RedelegationResponseEntry> redelegationResponses) {
// Update your UI components with redelegation data
// For example, if you have a ListView for redelegations:
// redelegationsListView.getItems().setAll(redelegationResponses);
System.out.println("redelegationResponses: " + redelegationResponses);
}

/* UNBONDING */
public void onUnbondingDelegationsEvent(@Observes UnbondingDelegationsEvent event) {
Platform.runLater(() -> {
// Call a method to update the view with the unbonding delegations data
updateUnbondingDelegationsView(event.getUnbondingDelegationsResponse().getUnbondingResponses());
});
}

// The method to update the view (the implementation depends on how you want to display the unbonding delegations)
private void updateUnbondingDelegationsView(List<UnbondingResponse> unbondingResponses) {
// Update your UI components with unbonding delegations data
// For example, if you have a ListView for unbonding delegations:
// unbondingDelegationsListView.getItems().setAll(unbondingResponses);
System.out.println("unbondingResponses: " + unbondingResponses);
}

private void createNewAccount() {
try {
showPane(cosmosWizardPane);
Expand Down Expand Up @@ -1004,29 +1037,6 @@ public static byte[] longToBytes(long value) {
return buffer.array();
}

private long getAccountNumber(String address) {
cosmos.auth.v1beta1.QueryGrpc.QueryBlockingStub authQueryClient = cosmos.auth.v1beta1.QueryGrpc
.newBlockingStub(grpcService.getChannel());
QueryAccountRequest accountRequest = QueryAccountRequest.newBuilder().setAddress(address).build();

try {
QueryAccountResponse response = authQueryClient.account(accountRequest);
Any accountAny = response.getAccount();
System.out.println("Type URL in getAccountNumber: " + accountAny.getTypeUrl());
BaseAccount baseAccount = accountAny.unpack(BaseAccount.class);
System.out.println("baseAccount.getPubKey(): " + baseAccount.getPubKey()
+ " \naddress :" + baseAccount.getAddress());

// Process baseAccount as needed
// we need the account number and not the sequence here
System.out.println("ACCOUNT NUMBER: " + baseAccount.getAccountNumber());
return baseAccount.getAccountNumber();
} catch (Exception e) {
e.printStackTrace();
return -1; // Handle this as per your application's requirement
}
}

@FXML
public void sendTokens() throws Exception {
byte[] privateKey = Hex.decode(getPrivateKeyHex());
Expand All @@ -1037,7 +1047,7 @@ public void sendTokens() throws Exception {

Account selectedAccount = accountsData.getSelectedAccount();
long sequence = getSequence(selectedAccount.getAddress());
long accountNumber = getAccountNumber(selectedAccount.getAddress());
long accountNumber = cosmosService.getAccountNumber(selectedAccount.getAddress());

SignUtil transactionService = new SignUtil(grpcService, sequence, accountNumber, "ugd", ApiConfig.getCHAIN_ID());

Expand Down Expand Up @@ -1079,9 +1089,9 @@ public void delegation(boolean delegate, String validatorAddress) throws Excepti

Account selectedAccount = accountsData.getSelectedAccount();
long sequence = getSequence(selectedAccount.getAddress());
long accountNumber = getAccountNumber(selectedAccount.getAddress());
long accountNumber = cosmosService.getAccountNumber(selectedAccount.getAddress());

SignUtil transactionService = new SignUtil(grpcService, sequence, accountNumber, "ugd", CHAIN_ID);
SignUtil transactionService = new SignUtil(grpcService, sequence, accountNumber, "ugd", ApiConfig.getCHAIN_ID());

long amount = 0;
if (validatorAddress == null) {
Expand Down Expand Up @@ -1267,30 +1277,12 @@ private void onMnemonicVerification(ActionEvent event) {
System.out.println("mnemonic does not match");
}
}

@FXML
private void onStartAllGridnodes(ActionEvent event) {
gridnodeModel.StartGridnode();
}

private static byte[] getBits(byte[] data, int fromBits, int toBits, boolean pad) {
final BitSet bits = BitSet.valueOf(data);
BitSet extractedBits = bits.get(fromBits, toBits);

int extractedBitLength = toBits - fromBits;
int remainder = extractedBitLength % 8;

if (pad && remainder != 0) {
int paddingLength = 8 - remainder;
// Increase the size of extractedBits to accommodate padding
extractedBits.set(extractedBitLength, extractedBitLength + paddingLength, false); // Set padding bits to 0
} else if (!pad && remainder != 0) {
// Throw an error if padding is not allowed but is required
throw new RuntimeException("ERR_BAD_FORMAT illegal zero padding");
}
return extractedBits.toByteArray();
}

/* MAIN VIEW */
public void loadAccounts() {
try {
Expand Down Expand Up @@ -1352,7 +1344,7 @@ public void loadAccounts() {
@Override
protected Void call() throws Exception {
Platform.runLater(() -> {
System.out.println("user can run: " + gridnodeNumberForUser() + " gridnode(s)!");
//System.out.println("user can run: " + gridnodeNumberForUser() + " gridnode(s)!");
getValidators();
// Update UI with the balance received from the response
balanceLabel.setText(getWalletBalance(selectedAccount.get().getAddress()) + " ugd");
Expand All @@ -1364,7 +1356,7 @@ protected Void call() throws Exception {
// Load transactions and other account data (assuming these methods are adapted
// for gRPC)
cosmosTxList.loadTransactions(10);
loadAccountData(accountsData.getSelectedAccount().getAddress());
cosmosService.loadAccountData(accountsData.getSelectedAccount().getAddress());

return null;
}
Expand All @@ -1386,50 +1378,6 @@ protected Void call() throws Exception {
initCopyButton();
}

private void loadAccountData(String account) throws IOException, InterruptedException {
RestService restService = new RestService();

// Delegations
DelegationsRequest delegationsRequest = new DelegationsRequest(account);
DelegationsRequest.Response delegationsResponse = new RestCommand<>(
delegationsRequest, restService).execute();
setDelegations(delegationsResponse.getDelegationResponses());
System.out.println(delegationsResponse);

// Rewards
RewardsRequest rewardsRequest = new RewardsRequest(account);
RewardsRequest.Response rewardsResponse = new RestCommand<>(rewardsRequest,
restService).execute();
stakingRewardsValue(rewardsResponse.getTotal());
System.out.println(rewardsResponse);

// Unbonding Delegations
UnbondingDelegationsRequest unbondingDelegationsRequest = new UnbondingDelegationsRequest(
account);
UnbondingDelegationsRequest.Response unbondingDelegationsResponse = new RestCommand<>(
unbondingDelegationsRequest, restService).execute();
System.out.println(unbondingDelegationsResponse);

// Redelegations
RedelegationsRequest redelegationsRequest = new RedelegationsRequest(account);
RedelegationsRequest.Response redelegationsResponse = new RestCommand<>(
redelegationsRequest, restService).execute();
System.out.println(redelegationsResponse);

// Withdraw Address
WithdrawAddressRequest withdrawAddressRequest = new WithdrawAddressRequest(
account);
WithdrawAddressRequest.Response withdrawAddressResponse = new RestCommand<>(
withdrawAddressRequest, restService).execute();
System.out.println(withdrawAddressResponse);

gridnodeDelegationService.fetchDelegationAmount(account);
setDelegationAmount();

updateCollateralDisplay();

}

private String getWalletBalance(String address) {
QueryBalanceRequest balanceRequest = QueryBalanceRequest.newBuilder()
.setAddress(address)
Expand Down Expand Up @@ -1513,23 +1461,22 @@ private long getUnboundingBalance(String address) {
return totalUnbondingAmount;
}

public void setDelegationAmount() {
GridnodeDelegationAmount.Response response = gridnodeDelegationService
.getCurrentResponse();
if (response != null) {
Platform.runLater(() -> {
BigDecimal amount = response.getAmount();
String text = amount.toPlainString() + " UGD";
delegationAmountLabel.setText(text);
});
}
public void onDelegationAmountEvent(@Observes DelegationAmountEvent event) {
Platform.runLater(() -> {
String text = event.getAmount().toPlainString() + " UGD";
delegationAmountLabel.setText(text);
System.out.println("Delegation Amount: " + text);
});
}

private void updateCollateralDisplay() {
if (hedgehog.fetchCollateralRequired()) {
System.out.println("Collateral Required: " + collateral.getAmount());
public void onCollateralUpdateEvent(@Observes CollateralUpdateEvent event) {
if (event.isSuccess()) {
int amount = event.getAmount();
System.out.println("Collateral Required: " + amount);
// Update the UI with the amount
} else {
System.out.println("Error fetching collateral");
// Update the UI to indicate an error
}
}

Expand All @@ -1545,17 +1492,16 @@ private long gridnodeNumberForUser() {

throw new IllegalStateException("Collateral amount was not fetched.");
}

public void fetchGridnodes() {



GridnodeData data = new GridnodeData();

data.setGridnodeId("testing testing");
data.setStatus("testing");

gridnodeData.add(data);

colStartGridnode.setCellValueFactory(cell -> {
Button button = new Button();
button.setText("Start Gridnode");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ public class GridnodeData {
private String gridnodeId;
@Getter @Setter
private String active;
@Getter @Setter
private String status;
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public TransactionResponse txResponse(String address)
TransactionResponse.class);
return txResponse;
} catch (URISyntaxException ex) {
Logger.getLogger(CosmosRestClient.class.getName()).log(Level.SEVERE, null, ex);
Logger.getLogger(CosmosService.class.getName()).log(Level.SEVERE, null, ex);
return null;
}
}
Expand Down
Loading

0 comments on commit a5c3436

Please sign in to comment.