Skip to content

Commit

Permalink
Zero primary change if only fee pay
Browse files Browse the repository at this point in the history
  • Loading branch information
zgrguric committed Feb 28, 2023
1 parent 840258c commit 0a25679
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
10 changes: 5 additions & 5 deletions src/TxMutationParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ public function __construct(string $reference_account, \stdClass $tx)
}
}
unset($v);
//dd($balanceChangeExclFeeOnly,$ownBalanceChanges);

/**
* Get signer from tx public key
Expand Down Expand Up @@ -357,6 +356,9 @@ private function significantBalanceChange(array $balanceChanges, ?string $fee):
/**
* Fallback to default
* Possibly XRP sent, if so: exclude fee
* If fee equals to amount then fallback is zero,
* fee is only primary change and we do not record that,
* this will keep fee display consistant.
*/
$fallback = $balanceChanges[0];

Expand All @@ -366,12 +368,10 @@ private function significantBalanceChange(array $balanceChanges, ?string $fee):
\substr($fallback['value'],0,1) === '-' &&
$fee
) {

$fallback['value'] = BigDecimal::of($fallback['value'])->abs()->isEqualTo( BigDecimal::of($fee)->abs() )
? '0' //$fallback['value'] //fee equals to !value
? null //$fallback['value'] //fee equals to !value = zero change
: (string)BigDecimal::of($fallback['value'])->plus($fee)->stripTrailingZeros();
//dd($fallback,BigDecimal::of($fallback['value'])->abs()->isEqualTo( BigDecimal::of($fee)->abs() ));
if($fallback['value'] === '0')
if($fallback['value'] === null)
return [];
return $fallback;
}
Expand Down
13 changes: 11 additions & 2 deletions tests/ATx17Test.php → tests/Tx17Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* @see https://github.com/XRPL-Labs/TxMutationParser/blob/main/test/tx1.ts
* @see https://hash.xrp.fans/D36265AD359D82BDF056CAFE760F9DFF42BB21C308EC3F68C4DE0D707D2FB6B6/json
*/
final class ATx17Test extends TestCase
final class Tx17Test extends TestCase
{
public function testDepositPreauthFeePayer()
{
Expand Down Expand Up @@ -39,7 +39,16 @@ public function testDepositPreauthFeePayer()

//does not contain `secondary` entry
$this->assertArrayNotHasKey('secondary',$parsedTransaction['eventList']);


# Event flow
//does not contain `start` entry
$this->assertArrayNotHasKey('start',$parsedTransaction['eventFlow']);

//does not contain `intermediate` entry
$this->assertArrayNotHasKey('intermediate',$parsedTransaction['eventFlow']);

//does not contain `end` entry
$this->assertArrayNotHasKey('end',$parsedTransaction['eventFlow']);

}
}
26 changes: 5 additions & 21 deletions tests/Tx5Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function testTrustLineAddedByOwnAccount()
$account = "rwietsevLFg8XSmG3bEZzFein1g8RBqWDZ";
$TxMutationParser = new TxMutationParser($account, $transaction->result);
$parsedTransaction = $TxMutationParser->result();

//dd( $parsedTransaction);

//Self (own account) must be $account
$this->assertEquals($account,$parsedTransaction['self']['account']);
Expand All @@ -35,34 +35,18 @@ public function testTrustLineAddedByOwnAccount()

# Event list

//contains (correct) `primary` entry
$this->assertArrayHasKey('primary',$parsedTransaction['eventList']);
$this->assertEquals([
'currency' => 'XRP',
'value' => '-0.000012'
],$parsedTransaction['eventList']['primary']);

//does not contain `primary` entry
$this->assertArrayNotHasKey('primary',$parsedTransaction['eventList']);

//does not contain `secondary` entry
$this->assertArrayNotHasKey('secondary',$parsedTransaction['eventList']);


# Event flow

//does not contain `start` entry
$this->assertArrayNotHasKey('start',$parsedTransaction['eventFlow']);

//contains (correct) `intermediate` entry
$this->assertArrayHasKey('intermediate',$parsedTransaction['eventFlow']);
$this->assertEquals([
'account' => $account,
'mutations' => [
'out' => [
'currency' => "XRP",
'value' => "-0.000012",
]
]
],$parsedTransaction['eventFlow']['intermediate']);
//does not contain `intermediate` entry
$this->assertArrayNotHasKey('intermediate',$parsedTransaction['eventFlow']);

//does not contain `end` entry
$this->assertArrayNotHasKey('end',$parsedTransaction['eventFlow']);
Expand Down

0 comments on commit 0a25679

Please sign in to comment.