Skip to content

Commit

Permalink
Add preview logic for buy and sell methods
Browse files Browse the repository at this point in the history
  • Loading branch information
donhardman committed Jul 7, 2021
1 parent ef7e5f2 commit 1b6afe1
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/CreatorCoin.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class CreatorCoin {
const RESERVE_RATIO = 0.3333333;
const SLOPE = 0.003;
const NANOS_PER_UNIT = 1000000000;
const THRESHOLD = 10;

protected int $locked = 0;
protected int $supply = 0;
Expand Down Expand Up @@ -114,7 +115,7 @@ public function setIsCreator(bool $is_creator): static {
* Amount we buy in nanos of $clout
* @return static
*/
public function buy(int $amount): static {
public function buy(int $amount, bool $preview = false): static {
if ($this->reward === 10000 && !$this->is_creator && $this->strategy === 'reward') {
throw new InvalidArgumentException('Incorrect reward basis points');
}
Expand All @@ -135,9 +136,11 @@ public function buy(int $amount): static {
$minted = $this->calculateBancorMinting($buy_amount);
}

$this->locked += $buy_amount;
$this->supply += $minted;
$this->recalculateRate();
if (!$preview) {
$this->locked += $buy_amount;
$this->supply += $minted;
$this->recalculateRate();
}

$reward = ['amount' => $reward_amount, 'coin' => 0];
$watermark = max($this->supply, $this->watermark);
Expand All @@ -152,6 +155,7 @@ public function buy(int $amount): static {
$received = $this->is_creator ? $minted : intval($minted - $reward['coin']);

$this->buy = [
'preview' => $preview,
'amount' => $buy_amount,
'minted' => $minted,
'received' => $received,
Expand All @@ -168,15 +172,18 @@ public function buy(int $amount): static {
* Amount of creator coins in nanos
* @return static
*/
public function sell(int $amount): static {
public function sell(int $amount, bool $preview = false): static {
$returned = $this->calculateReturned($amount);

$this->locked -= $returned;
$this->supply -= $amount;
$this->recalculateRate();
if (!$preview) {
$this->locked -= $returned;
$this->supply -= $amount;
$this->recalculateRate();
}

$credited = $this->applyTradeFees($returned);
$this->sell = [
'preview' => $preview,
'amount' => $amount,
'returned' => $credited,
'rate' => intval(($credited / $amount) * static::NANOS_PER_UNIT),
Expand Down

0 comments on commit 1b6afe1

Please sign in to comment.