Skip to content

Commit

Permalink
#10 Add CloseReminder to Logic Sig Transaction UI and change in service
Browse files Browse the repository at this point in the history
  • Loading branch information
satran004 committed Mar 18, 2021
1 parent 04f5760 commit 1f90956
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ public Result assetTransfer(Account signer, Address sender, String receiver, Acc
}
}

public Result logicSigAssetTransfer(Address sender, String receiver, AccountAsset asset, BigInteger amount,
public Result logicSigAssetTransfer(Address sender, String receiver, AccountAsset asset, BigInteger amount, Address closeReminderTo,
TxnDetailsParameters txnDetailsParameters, byte[] sourceBytes, RequestMode requestMode, TransactionSigner txnSigner) throws Exception {
if(sender == null) {
logListener.error("Sender cannot be null");
Expand Down Expand Up @@ -342,6 +342,9 @@ public Result logicSigAssetTransfer(Address sender, String receiver, AccountAsse
.assetReceiver(receiver)
.sender(sender);

if(closeReminderTo != null)
builder.assetCloseTo(closeReminderTo);

if (builder == null) {
logListener.error("Transaction could not be built");
return Result.error();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public LogicSigTransactionService(Project project, LogListener logListener) thro
this.assetTransactionService = new AssetTransactionService(project, logListener);
}

public Result logicSigTransaction(String lsigPath, Address sender, Address receiver, AccountAsset asset, BigInteger amount, TxnDetailsParameters txnDetailsParameters, RequestMode requestMode)
public Result logicSigTransaction(String lsigPath, Address sender, Address receiver, AccountAsset asset, BigInteger amount, Address closeReminderTo, TxnDetailsParameters txnDetailsParameters, RequestMode requestMode)
throws Exception {

byte[] logicSigBytes = FileUtil.loadFileBytes(new File(lsigPath));
Expand All @@ -48,12 +48,12 @@ public Result logicSigTransaction(String lsigPath, Address sender, Address recei

if(sender == null) {
if(asset == null) { //Algo
return contractAccountTransaction(logicSigBytes, receiver, amount, txnDetailsParameters, sourceBytes, requestMode);
return contractAccountTransaction(logicSigBytes, receiver, amount, closeReminderTo, txnDetailsParameters, sourceBytes, requestMode);
} else { //Asset transfer
LogicsigSignature logicsigSignature = Encoder.decodeFromMsgPack(logicSigBytes, LogicsigSignature.class);
Address fromAddress = logicsigSignature.toAddress();

return assetTransactionService.logicSigAssetTransfer(fromAddress, receiver.toString(), asset, amount, txnDetailsParameters, sourceBytes, requestMode, new TransactionSigner() {
return assetTransactionService.logicSigAssetTransfer(fromAddress, receiver.toString(), asset, amount, closeReminderTo, txnDetailsParameters, sourceBytes, requestMode, new TransactionSigner() {
@Override
public SignedTransaction signTransaction(Transaction transaction) throws Exception {
return Account.signLogicsigTransaction(logicsigSignature, transaction);
Expand All @@ -62,11 +62,11 @@ public SignedTransaction signTransaction(Transaction transaction) throws Excepti
}
} else {
if(asset == null) { //Algo
return accountDelegationTransaction(logicSigBytes, sender, receiver, amount, txnDetailsParameters, sourceBytes, requestMode);
return accountDelegationTransaction(logicSigBytes, sender, receiver, amount, closeReminderTo, txnDetailsParameters, sourceBytes, requestMode);
} else { // Asset transfer
LogicsigSignature logicsigSignature = Encoder.decodeFromMsgPack(logicSigBytes, LogicsigSignature.class);

return assetTransactionService.logicSigAssetTransfer(sender, receiver.toString(), asset, amount, txnDetailsParameters, sourceBytes, requestMode, new TransactionSigner() {
return assetTransactionService.logicSigAssetTransfer(sender, receiver.toString(), asset, amount, closeReminderTo, txnDetailsParameters, sourceBytes, requestMode, new TransactionSigner() {
@Override
public SignedTransaction signTransaction(Transaction transaction) throws Exception {
return Account.signLogicsigTransaction(logicsigSignature, transaction);
Expand Down Expand Up @@ -107,7 +107,7 @@ public SignedTransaction signTransaction(Transaction transaction) throws Excepti

}

public Result contractAccountTransaction(byte[] logicSigBytes, Address receiver, BigInteger amount,
public Result contractAccountTransaction(byte[] logicSigBytes, Address receiver, BigInteger amount, Address closeReminderTo,
TxnDetailsParameters txnDetailsParameters, byte[] sourceBytes, RequestMode requestMode) throws Exception {

if (receiver == null) {
Expand All @@ -128,7 +128,7 @@ public Result contractAccountTransaction(byte[] logicSigBytes, Address receiver,
logListener.info("From Contract Address : " + fromAddress.toString());

PaymentTransactionBuilder paymentTransactionBuilder = Transaction.PaymentTransactionBuilder();
Transaction txn = populatePaymentTransaction(paymentTransactionBuilder, fromAddress, receiver.toString(), amount.longValue(), null, txnDetailsParameters);
Transaction txn = populatePaymentTransaction(paymentTransactionBuilder, fromAddress, receiver.toString(), amount.longValue(), closeReminderTo, txnDetailsParameters);

if (txn == null) {
logListener.error("Transaction could not be built");
Expand Down Expand Up @@ -159,7 +159,7 @@ public SignedTransaction signTransaction(Transaction transaction) throws Excepti

}

public Result accountDelegationTransaction(byte[] logicSigBytes, Address sender, Address receiver, BigInteger amount,
public Result accountDelegationTransaction(byte[] logicSigBytes, Address sender, Address receiver, BigInteger amount, Address closeReminderTo,
TxnDetailsParameters txnDetailsParameters, byte[] sourceBytes, RequestMode requestMode) throws Exception {
if(sender == null) {
logListener.error("Sender address cannot be empty");
Expand All @@ -181,7 +181,7 @@ public Result accountDelegationTransaction(byte[] logicSigBytes, Address sender,
logListener.info("Starting Account Delegation transaction ...\n");

PaymentTransactionBuilder paymentTransactionBuilder = Transaction.PaymentTransactionBuilder();
Transaction txn = populatePaymentTransaction(paymentTransactionBuilder, sender, receiver.toString(), amount.longValue(), null, txnDetailsParameters);
Transaction txn = populatePaymentTransaction(paymentTransactionBuilder, sender, receiver.toString(), amount.longValue(), closeReminderTo, txnDetailsParameters);

if(txn == null) {
logListener.error("Transaction could not be built");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,11 @@ public void actionPerformed(@NotNull AnActionEvent e) {

Tuple<BigDecimal, BigInteger> amounts = dialog.getAmount(); //Algo, mAlgo

Address closeReminderTo = null;
try {
closeReminderTo = dialog.getCloseReminderTo();
} catch (Exception ex) {}

//txn detals params
TxnDetailsParameters txnDetailsParameters = null;
try {
Expand All @@ -161,6 +166,7 @@ public void actionPerformed(@NotNull AnActionEvent e) {
boolean isAlgoTransfer = dialog.isAlgoTransfer();

Address finalSenderAccount = senderAddress;
Address finalCloseReminderTo = closeReminderTo;
TxnDetailsParameters finalTxnDetailsParams = txnDetailsParameters;

RequestMode requestMode = dialog.getRequestMode();
Expand All @@ -177,9 +183,9 @@ public void run(@NotNull ProgressIndicator indicator) {
Result result = null;

if(isAlgoTransfer) {
result = transactionService.logicSigTransaction(lsigPath, finalSenderAccount, receiverAddress, null, amounts._2(), finalTxnDetailsParams, requestMode);
result = transactionService.logicSigTransaction(lsigPath, finalSenderAccount, receiverAddress, null, amounts._2(), finalCloseReminderTo, finalTxnDetailsParams, requestMode);
} else {
result = transactionService.logicSigTransaction(lsigPath, finalSenderAccount, receiverAddress, asset, amounts._2(), finalTxnDetailsParams, requestMode);
result = transactionService.logicSigTransaction(lsigPath, finalSenderAccount, receiverAddress, asset, amounts._2(), finalCloseReminderTo, finalTxnDetailsParams, requestMode);
}

processResult(project, module, result, requestMode, logListener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
<rowspec value="center:max(d;4px):noGrow"/>
<rowspec value="top:3dlu:noGrow"/>
<rowspec value="center:max(d;4px):noGrow"/>
<rowspec value="top:3dlu:noGrow"/>
<rowspec value="center:max(d;4px):noGrow"/>
<colspec value="fill:d:noGrow"/>
<colspec value="left:4dlu:noGrow"/>
<colspec value="fill:d:grow"/>
Expand Down Expand Up @@ -304,6 +306,33 @@
<text value="Multi-Sig"/>
</properties>
</component>
<component id="75c82" class="javax.swing.JLabel">
<constraints>
<grid row="22" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<forms/>
</constraints>
<properties>
<text value="Close Reminder To"/>
</properties>
</component>
<component id="c3c00" class="javax.swing.JTextField" binding="closeReminderTo">
<constraints>
<grid row="22" column="2" row-span="1" col-span="4" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
<forms defaultalign-horz="false"/>
</constraints>
<properties/>
</component>
<component id="4085e" class="javax.swing.JButton" binding="closeReminderAccountChooserBtn">
<constraints>
<grid row="22" column="8" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<forms/>
</constraints>
<properties>
<text value="..."/>
</properties>
</component>
</children>
</grid>
</children>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public class LogicSigSendTransactionDialog extends TxnDialogWrapper {
private JLabel unitLabel;
private JButton fetchAssetsBtn;
private JButton receiverMultiSigBtn;
private JTextField closeReminderTo;
private JButton closeReminderAccountChooserBtn;
private JTextField senderLogSigTf;

private ButtonGroup contractType;
Expand Down Expand Up @@ -184,6 +186,16 @@ private void initializeData(Project project, String lsigPath) throws DeploymentT
});

unitLabel.setText(ALGO);

closeReminderTo.setToolTipText("<html>When set, it indicates that the transaction is requesting that the Sender account<br/>" +
" should be closed, and all remaining funds,<br/>" +
" after the fee and amount are paid, be transferred to this address.</html>");
closeReminderAccountChooserBtn.addActionListener(e -> {
AlgoAccount algoAccount = AccountChooser.getSelectedAccount(project, true);
if(algoAccount != null) {
closeReminderTo.setText(algoAccount.getAddress());
}
});
}

private void loadLogicSigFile(String lsigPath) {
Expand Down Expand Up @@ -381,6 +393,15 @@ public Tuple<BigDecimal, BigInteger> getAmount() {
}
}

public Address getCloseReminderTo() throws Exception {
String closeReminderToAdd = StringUtil.trim(closeReminderTo.getText());
if(StringUtil.isEmpty(closeReminderToAdd))
return null;

Address address = new Address(closeReminderToAdd);
return address;
}

public TransactionDtlsEntryForm getTransactionDtlsEntryForm() {
return transactionDtlsEntryForm;
}
Expand All @@ -407,6 +428,12 @@ protected ValidationInfo doTransactionInputValidation() {
return new ValidationInfo("Please select a valid asset", assetsCB);
}

try {
getCloseReminderTo();
} catch (Exception e) {
return new ValidationInfo("Invalid Close Reminder To address", closeReminderTo);
}

return transactionDtlsEntryForm.doValidate();
}

Expand Down

0 comments on commit 1f90956

Please sign in to comment.