qortal/src/qora/transaction/PaymentTransaction.java
catbref 9897981de1 Work on assets/payments
* Created PaymentData transfer objects for (recipient, assetId, amount) tuples
* Created corresponding Payment class for validating, processing and orphaning payment(s)

* Modified OrderData to support isClosed for when an Order is cancelled so no more trades can occur

* Migrated CancelOrderTransactions and MultiPaymentTransactions

* Converted MessageTransactions, PaymentTransactions and TransferAssetTransactions to use new Payment class

Can't use PaymentTransformer in PaymentTransformer or TransferAssetTransformer due to serialization differences.
2018-06-14 16:46:55 +01:00

65 lines
2.4 KiB
Java

package qora.transaction;
import java.util.Arrays;
import data.PaymentData;
import data.transaction.PaymentTransactionData;
import data.transaction.TransactionData;
import qora.account.Account;
import qora.account.PublicKeyAccount;
import qora.assets.Asset;
import qora.payment.Payment;
import repository.DataException;
import repository.Repository;
public class PaymentTransaction extends Transaction {
// Constructors
public PaymentTransaction(Repository repository, TransactionData transactionData) {
super(repository, transactionData);
}
// Processing
private PaymentData getPaymentData() {
PaymentTransactionData paymentTransactionData = (PaymentTransactionData) this.transactionData;
return new PaymentData(paymentTransactionData.getRecipient(), Asset.QORA, paymentTransactionData.getAmount());
}
public ValidationResult isValid() throws DataException {
PaymentTransactionData paymentTransactionData = (PaymentTransactionData) this.transactionData;
// Check reference is correct
Account sender = new PublicKeyAccount(repository, paymentTransactionData.getSenderPublicKey());
if (!Arrays.equals(sender.getLastReference(), paymentTransactionData.getReference()))
return ValidationResult.INVALID_REFERENCE;
// Wrap and delegate final payment checks to Payment class
return new Payment(this.repository).isValid(paymentTransactionData.getSenderPublicKey(), getPaymentData(), paymentTransactionData.getFee());
}
public void process() throws DataException {
PaymentTransactionData paymentTransactionData = (PaymentTransactionData) this.transactionData;
// Save this transaction itself
this.repository.getTransactionRepository().save(this.transactionData);
// Wrap and delegate payment processing to Payment class
new Payment(this.repository).process(paymentTransactionData.getSenderPublicKey(), getPaymentData(), paymentTransactionData.getFee(),
paymentTransactionData.getSignature());
}
public void orphan() throws DataException {
PaymentTransactionData paymentTransactionData = (PaymentTransactionData) this.transactionData;
// Delete this transaction
this.repository.getTransactionRepository().delete(this.transactionData);
// Wrap and delegate payment processing to Payment class
new Payment(this.repository).orphan(paymentTransactionData.getSenderPublicKey(), getPaymentData(), paymentTransactionData.getFee(),
paymentTransactionData.getSignature(), paymentTransactionData.getReference());
}
}