writing TransactionTransfer Validation method, update tests

This commit is contained in:
Dario Rekowski on RockPI 2019-12-05 12:08:35 +00:00
parent 18848f65ae
commit 2bfee6b405
3 changed files with 54 additions and 8 deletions

View File

@ -41,7 +41,8 @@ class TransactionBase {
return NULL;
}
protected function updateStateBalance($stateUserId, $newAmountCent) {
protected function updateStateBalance($stateUserId, $addAmountCent) {
$finalBalance = 0;
$stateBalancesTable = TableRegistry::getTableLocator()->get('stateBalances');
$stateBalanceQuery = $stateBalancesTable
->find('all')
@ -52,18 +53,19 @@ class TransactionBase {
if($stateBalanceQuery->count() > 0) {
$stateBalanceEntry = $stateBalanceQuery->first();
$stateBalanceEntry->amount += $newAmountCent;
$stateBalanceEntry->amount += $addAmountCent;
} else {
$stateBalanceEntry = $stateBalancesTable->newEntity();
$stateBalanceEntry->state_user_id = $stateUserId;
$stateBalanceEntry->amount = $newAmountCent;
$stateBalanceEntry->amount = $addAmountCent;
}
$finalBalance = $stateBalanceEntry->amount;
//echo "\ntry to save: "; var_dump($stateBalanceEntry); echo "\n";
if(!$stateBalancesTable->save($stateBalanceEntry)) {
$errors = $stateBalanceEntry->getErrors();
$this->addError('TransactionBase::updateStateBalance', 'error saving state balance with: ' . json_encode($errors));
return false;
}
return true;
return $finalBalance;
}
}

View File

@ -145,7 +145,7 @@ class TransactionCreation extends TransactionBase {
}
// update state balance
if(!$this->updateStateBalance($receiverUser, $this->getAmount())) {
if(false === $this->updateStateBalance($receiverUser, $this->getAmount())) {
return false;
}

View File

@ -153,9 +153,53 @@ class TransactionTransfer extends TransactionBase {
}
public function save($transaction_id, $firstPublic) {
$this->addError('TransactionTransfer::save', 'not implemented yet');
static $functionName = 'TransactionCreation::save';
if(count($this->protoTransactionTransfer->getSenderAmounts()) !== 1) {
$this->addError($functionName, 'not more than one sender currently supported');
return false;
//return true;
}
$senderAmount = $this->protoTransactionTransfer->getSenderAmounts()[0];
if(count($this->protoTransactionTransfer->getReceiverAmounts()) !== 1) {
$this->addError($functionName, 'not more tahn one receiver currently supported');
return false;
}
$receiverAmount = $this->protoTransactionTransfer->getReceiverAmounts()[0];
$transactionTransferTable = TableRegistry::getTableLocator()->get('TransactionSendCoins');
$senderUserId = $this->getStateUserId($senderAmount->getEd25519SenderPubkey());
$receiverUserId = $this->getStateUserId($receiverAmount->getEd25519ReceiverPubkey());
if($senderUserId === NULL || $receiverUserId === NULL) {
return false;
}
$finalSenderBalance = $this->updateStateBalance($senderUserId, -$senderAmount->amount());
if(false === $finalSenderBalance) {
return false;
}
if(false === $this->updateStateBalance($receiverUserId, $receiverAmount->amount())) {
return false;
}
$transactionTransferEntity = $transactionTransferTable->newEntity();
$transactionTransferEntity->transaction_id = $transaction_id;
$transactionTransferEntity->state_user_id = $senderUserId;
$transactionTransferEntity->receiver_public_key = $receiverAmount->getEd25519ReceiverPubkey();
$transactionTransferEntity->receiver_user_id = $receiverUserId;
$transactionTransferEntity->amount = $senderAmount->amount();
$transactionTransferEntity->sender_final_balance = $finalSenderBalance;
if(!$transactionTransferTable->save($transactionTransferEntity)) {
$this->addError($functionName, 'error saving transactionSendCoins with errors: ' . json_encode($transactionTransferEntity->getErrors()));
return false;
}
//$this->addError('TransactionTransfer::save', 'not implemented yet');
//return false;
return true;
}
}