Merge pull request #273 from gradido/community_gdd_float_balance

give balance as float value in GDD (not longer GDD cent)
This commit is contained in:
Ulf Gebhardt 2021-05-06 15:51:49 +02:00 committed by GitHub
commit 832d380455
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 107 additions and 42 deletions

View File

@ -22,6 +22,7 @@ class AppRequestsController extends AppController
{
parent::initialize();
$this->loadComponent('JsonRequestClient');
$this->loadComponent('GradidoNumber');
//$this->loadComponent('JsonRpcRequestClient');
//$this->Auth->allow(['add', 'edit']);
$this->Auth->allow(['index', 'sendCoins', 'createCoins', 'getBalance', 'listTransactions']);
@ -115,9 +116,10 @@ class AppRequestsController extends AppController
return $required_fields;
}
if(intval($param['amount']) <= 0) {
if(floatval($param['amount']) <= 0.0) {
return ['state' => 'error', 'msg' => 'amount is invalid', 'details' => $param['amount']];
}
$param['amount'] = $this->GradidoNumber->parseInputNumberToCentNumber($param['amount']);
if(isset($data->memo)) {
$param['memo'] = $data->memo;
@ -268,7 +270,7 @@ class AppRequestsController extends AppController
public function getBalance($session_id = 0)
{
$this->viewBuilder()->setLayout('ajax');
$login_result = $this->requestLogin($session_id, false);
if($login_result !== true) {
return $this->returnJson($login_result);
@ -284,16 +286,18 @@ class AppRequestsController extends AppController
return $this->returnJson(['state' => 'success', 'balance' => 0]);
}
$now = new FrozenTime();
return $this->returnJson([
$body = [
'state' => 'success',
'balance' => $state_balance->amount,
'decay' => $state_balance->partDecay($now),
'decay_date' => $now
]);
];
$this->set('body', $body);
}
public function listTransactions($page = 1, $count = 25, $orderDirection = 'ASC', $session_id = 0)
{
$this->viewBuilder()->setLayout('ajax');
$startTime = microtime(true);
$login_result = $this->requestLogin($session_id, false);
if($login_result !== true) {
@ -337,14 +341,29 @@ class AppRequestsController extends AppController
$transactions = array_reverse($transactions);
}
}
return $this->returnJson([
'state' => 'success',
'transactions' => $transactions,
'transactionExecutingCount' => $session->read('Transactions.executing'),
'count' => count($transactions),
'gdtSum' => $gdtSum,
'timeUsed' => microtime(true) - $startTime
]);
$state_balance = $stateBalancesTable->find()->where(['state_user_id' => $user['id']])->first();
$body = [
'state' => 'success',
'transactions' => $transactions,
'transactionExecutingCount' => $session->read('Transactions.executing'),
'count' => count($transactions),
'gdtSum' => $gdtSum,
'timeUsed' => microtime(true) - $startTime
];
$now = new FrozenTime();
$body['decay_date'] = $now;
if(!$state_balance) {
$body['balance'] = 0.0;
$body['decay'] = 0.0;
} else {
$body['balance'] = $state_balance->amount;
$body['decay'] = $state_balance->partDecay($now);
}
$this->set('body', $body);
}
private function acquireAccessToken($session_id)

View File

@ -3,6 +3,7 @@ namespace App\Model\Entity;
use Cake\ORM\Entity;
use Cake\I18n\Time;
use Cake\I18n\Number;
/**
* StateBalance Entity
@ -34,7 +35,7 @@ class StateBalance extends Entity
'state_user' => true
];
protected $_virtual = ['decay'];
protected $_virtual = ['decay','amount_float'];
private function convertToTimestamp($dateOrTime)
{
@ -67,6 +68,7 @@ class StateBalance extends Entity
return intval($this->amount * pow(0.99999997802044727, $decay_duration));
}
public function partDecay($target_date)
{
$decay_duration = intval($this->convertToTimestamp($target_date) - $this->convertToTimestamp($this->record_date));

View File

@ -122,9 +122,9 @@ class StateUsersTable extends Table
// exchange back
$involvedUserIds = array_flip($involvedUser_temp);
$involvedUser = $this->find('all', [
'contain' => false,
'contain' => [],
'where' => ['id IN' => $involvedUserIds],
'fields' => ['id', 'first_name', 'last_name', 'email']
'fields' => ['id', 'first_name', 'last_name', 'email'],
]);
//var_dump($involvedUser->toArray());
$involvedUserIndices = [];

View File

@ -6,7 +6,7 @@ use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\ORM\TableRegistry;
use Cake\I18n\Number;
/**
* Transactions Model
*
@ -188,7 +188,7 @@ class TransactionsTable extends Table
//echo "prev balance: " . $prev->balance . ", diff_amount: $diff_amount, summe: " . (-intval($prev->balance - $diff_amount)) . "<br>";
$final_transactions[] = [
'type' => 'decay',
'balance' => -intval($prev->balance - $diff_amount),
'balance' => floatval(intval($prev->balance - $diff_amount)),
'decay_duration' => $interval->format('%a days, %H hours, %I minutes, %S seconds'),
'memo' => ''
];
@ -256,7 +256,7 @@ class TransactionsTable extends Table
$state_balance->record_date = $su_transaction->balance_date;
$final_transactions[] = [
'type' => 'decay',
'balance' => -intval($su_transaction->balance - $state_balance->decay),
'balance' => floatval(intval($su_transaction->balance - $state_balance->decay)),
'decay_duration' => $su_transaction->balance_date->timeAgoInWords(),
'memo' => ''
];

View File

@ -0,0 +1,11 @@
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
$body['balance'] = $this->element('centToFloat', ['cent' => $body['balance'], 'precision' => 4]);
$body['decay'] = $this->element('centToFloat', ['cent' => $body['decay'], 'precision' => 4]);
?><?= json_encode($body) ?>

View File

@ -0,0 +1,17 @@
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
$body['balance'] = $this->element('centToFloat', ['cent' => $body['balance'], 'precision' => 4]);
$body['decay'] = $this->element('centToFloat', ['cent' => $body['decay'], 'precision' => 4]);
$body['gdtSum'] = $this->element('centToFloat', ['cent' => $body['gdtSum'], 'precision' => 2]);
foreach($body['transactions'] as $i => $transaction) {
$body['transactions'][$i]['balance'] = $this->element('centToFloat', ['cent' => $transaction['balance'], 'precision' => 4]);
}
?><?= json_encode($body) ?>

View File

@ -0,0 +1,18 @@
<?php
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
$cut_places = $precision - 2;
$transformAmount = $cent;
if($cut_places > 0) {
$transformAmount = floor($cent / pow(10, $cut_places));
}
if($cut_places < 0) {
$cut_places = 0;
}
echo $transformAmount / pow(10, $precision - $cut_places);

View File

@ -23,13 +23,13 @@ Additional session can be provided as GET-Parameter
```json
{
"state":"success",
"balance":15906078,
"balance":1590.60,
"decay":15873851,
"decay_date":"2021-04-16T11:47:21+00:00"
}
```
- `balance` : balance describes gradido cents which are 4 digits behind the separator. A balance value of 174500 equals therefor 17,45 GDD
- `balance` : balance describes gradido
- `decay` : balance with decay on it at the time in decay_date, so it is the precise balance of user at time of calling this function
- `decay_date`: date and time for decay amount, should be the time and date of function call
@ -65,7 +65,7 @@ Assuming: session is valid
"type": "send",
"transaction_id": 2,
"date": "2021-02-19T13:25:36+00:00",
"balance": 1920000,
"balance": 192.0,
"memo": "a piece of cake :)",
"pubkey": "038a6f93270dc57b91d76bf110ad3863fcb7d1b08e7692e793fcdb4467e5b6a7"
}
@ -95,7 +95,7 @@ Transaction:
- `receiver`: user has received gradidos from another user
- `transaction_id`: id of transaction in db, in stage2 also the hedera sequence number of transaction
- `date`: date of ordering transaction (booking date)
- `balance`: Gradido Cent, 4 Nachkommastellen (2 Reserve), 1920000 = 192,00 GDD
- `balance`: Gradido
- `memo`: Details about transaction
## Creation transaction
@ -245,4 +245,3 @@ Without auto-sign the transaction is pending on the login-server and waits for t
// TODO Is this in line with our usability goals?
// TODO Should this not be handled client side?

View File

@ -176,7 +176,7 @@ Wenn alles okay:
"type": "creation|send|receive",
"transaction_id": <transaction_id>, // db id not id from blockchain
"date": "<date string>",
"balance": <GDD balance in GDD cent /10000>,
"balance": <GDD balance in GDD cent>,
"memo": "<Verwendungszweck>",
"pubkey": "<other_user.public_key in hex>"
@ -319,7 +319,7 @@ Wenn alles okay:
"type": "creation|send|receive",
"transaction_id": <transaction_id>, // db id not id from blockchain
"date": "<date string>",
"balance": <GDD balance in GDD cent /10000>,
"balance": <GDD balance in GDD cent>,
"memo": "<Verwendungszweck>",
"pubkey": "<other_user.public_key in hex>"

View File

@ -79,6 +79,7 @@ export default {
balance: 0,
GdtBalance: 0,
transactions: [],
bookedBalance: 0,
}
},
methods: {
@ -94,19 +95,13 @@ export default {
this.$store.dispatch('logout')
this.$router.push('/login')
},
async loadBalance() {
const result = await communityAPI.balance(this.$store.state.session_id)
if (result.success) {
this.balance = result.result.data.balance / 10000
} else {
// what to do when loading balance fails?
}
},
async updateTransactions() {
const result = await communityAPI.transactions(this.$store.state.session_id)
if (result.success) {
this.GdtBalance = result.result.data.gdtSum / 10000
this.GdtBalance = Number(result.result.data.gdtSum)
this.transactions = result.result.data.transactions
this.balance = Number(result.result.data.decay)
this.bookedBalance = Number(result.result.data.balance)
} else {
// what to do when loading balance fails?
}
@ -119,7 +114,6 @@ export default {
this.initScrollbar()
},
created() {
this.loadBalance()
this.updateTransactions()
},
}

View File

@ -14,6 +14,7 @@
<gdd-table
v-if="showTransactionList"
:transactions="transactions"
:max="5"
@update-transactions="updateTransactions"
/>
</b-container>

View File

@ -254,7 +254,7 @@ export default {
const result = await communityAPI.send(
this.$store.state.session_id,
this.ajaxCreateData.email,
this.ajaxCreateData.amount * 10000,
this.ajaxCreateData.amount,
this.ajaxCreateData.memo,
this.ajaxCreateData.target_date,
)

View File

@ -1,6 +1,6 @@
<template>
<div>
<b-row v-show="showTransactionList">
<b-row>
<b-col col="6">
<stats-card
type="gradient-red"

View File

@ -1,8 +1,8 @@
<template>
<div>
<b-list-group v-show="showTransactionList">
<b-list-group>
<b-list-group-item
v-for="item in transactions"
v-for="item in transactions.slice(0, max)"
:key="item.id"
style="background-color: #ebebeba3 !important"
>
@ -38,7 +38,7 @@
<h1 class="">
<span v-if="item.type === 'receive' || item.type === 'creation'">+</span>
<span v-else>-</span>
{{ $n(item.balance / 10000) }}
{{ $n(item.balance) }}
<small>GDD</small>
</h1>
<h2 class="text-muted">{{ item.name }}</h2>
@ -106,8 +106,8 @@
export default {
name: 'GddTable',
props: {
showTransactionList: { type: Boolean, default: true },
transactions: { default: [] },
max: { type: Number, default: 25 },
},
data() {
return {

View File

@ -121,8 +121,12 @@ Poco::JSON::Object* JsonCreateTransaction::transfer(Poco::Dynamic::Var params)
result = stateError("user not in group", "receiver user isn't in target group");
}
}
auto sender_user = mSession->getNewUser();
if (sender_user->getGradidoKeyPair()->isTheSame(*target_pubkey)) {
result = stateError("sender and receiver are the same");
}
if (!result) {
auto transaction = model::gradido::Transaction::createTransfer(mSession->getNewUser(), target_pubkey, mTargetGroup, amount, mMemo, mBlockchainType);
auto transaction = model::gradido::Transaction::createTransfer(sender_user, target_pubkey, mTargetGroup, amount, mMemo, mBlockchainType);
if (mAutoSign) {
Poco::JSON::Array errors;