Commit befd7d42 authored by Ben's avatar Ben
Browse files

Added in offchain rate limiting

parent 1674767b
Loading
Loading
Loading
Loading
+31 −2
Original line number Original line Diff line number Diff line
@@ -98,6 +98,9 @@ class Transactions
            if ($balance->add($this->amount)->lt(0)) {
            if ($balance->add($this->amount)->lt(0)) {
                throw new \Exception('Not enough funds');
                throw new \Exception('Not enough funds');
            }
            }
            if ($this->countOffchainBoosts(time() - (60*60*24), time()) > 10) {
                throw new \Exception('Maximum of 10 offchain tokens per day exceeded.');
            }
    
    
            $transaction = new Transaction();
            $transaction = new Transaction();


@@ -200,7 +203,6 @@ class Transactions
            }
            }


            // Receiver Transaction
            // Receiver Transaction

            $receiverTxGuid = $this->guid->build();
            $receiverTxGuid = $this->guid->build();


            $receiverTx = new Transaction();
            $receiverTx = new Transaction();
@@ -274,4 +276,31 @@ class Transactions
        return (string) BigNumber::toPlain($value, 18);
        return (string) BigNumber::toPlain($value, 18);
    }
    }


    /**
     * Counts the amount of offchain boosts for the user.
     *
     * @param integer from - unix timestamp 'from' time.
     * @param integer to - unix timestamp 'to' time.
     * @return integer the amount of offchain boosts.
     */
    private function countOffchainBoosts($from, $to) {
        $repo = Di::_()->get('Blockchain\Transactions\Repository');
        $opts = [
            'timestamp' => [
                'gte' => $from,
                'lte' => $to,
            ],
            'user_guid' => $this->user->guid,
            'offset' => 0,
        ];
        $result = $repo->getList($opts);
        $result = array_filter($result['transactions'], function(Transaction $transaction) {
            return $transaction->getContract() === 'offchain:boost';
        });
        return array_reduce($result, function($acc, $transaction) {
            $acc -= $transaction->getAmount() / 1000000000000000000;
            return $acc;
        });
    }

}
}