Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions ch1/1.5/solutions/paypal.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class Paypal extends Client {
// if send
if (tx.contents.type === 'send') {
// check that the transaction amount is positive and the sender has an account balance greater than or equal to the transaction amount
if (this.state[tx.contents.from].balance - tx.contents.amount < 0) {
if (this.state[tx.contents.from].balance - (tx.contents.amount + this.fee) < 0) {
// if a check fails, print an error to the console stating why and return false
console.log('Not enough money!');
return false;
Expand Down Expand Up @@ -217,7 +217,10 @@ class Paypal extends Client {

// Updates account balances according to the transaction, then adds the transaction to the history
applyTx(tx) {
// first decrease the balance of the transaction sender/signer

// first charge a fee to use the network
this.chargeFee(tx);
// then decrease the balance of the transaction sender/signer
this.state[tx.contents.from].balance -= tx.contents.amount;
// then increase the balance of the transaction receiver
this.state[tx.contents.to].balance += tx.contents.amount;
Expand Down Expand Up @@ -262,8 +265,7 @@ class Paypal extends Client {

// Checks if a transaction is valid, then processes it, then checks if there are any valid transactions in the pending transaction pool and processes those too
processTx(tx) {
// charge a fee to use the network
this.chargeFee(tx);

// check the transaction is valid
if (this.checkTx(tx)) {
// apply the transaction to Paypal's state
Expand Down