Skip to content

Commit

Permalink
EventFlow for returned funds start and end
Browse files Browse the repository at this point in the history
  • Loading branch information
zgrguric committed Sep 13, 2023
1 parent 665baf8 commit 3a89c3f
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 19 deletions.
69 changes: 53 additions & 16 deletions src/TxMutationParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,25 +305,62 @@ public function __construct(string $reference_account, \stdClass $tx)
* Return to issuer on Payment, via delivered_amount.
* Extract primary change from delivered_amount.
*/
if($this->tx->TransactionType == 'Payment' && $this->tx->Account == $this->account) {
if(
isset($this->tx->meta->delivered_amount->issuer) &&
isset($this->tx->Destination) &&
$this->tx->meta->delivered_amount->issuer == $this->tx->Destination
) {
if(count($eventList) == 0) {
if(is_string($this->tx->meta->delivered_amount)) {
//xrp
$eventList['primary']['currency'] = 'XRP';
$eventList['primary']['value'] = (string)BigDecimal::of($this->tx->meta->delivered_amount)->exactlyDividedBy(1000000)->stripTrailingZeros();
} else {
$eventList['primary']['currency'] = $this->tx->meta->delivered_amount->currency;
$eventList['primary']['counterparty'] = $this->tx->meta->delivered_amount->issuer;
$eventList['primary']['value'] = (string)BigDecimal::of($this->tx->meta->delivered_amount->value)->stripTrailingZeros();
if($this->tx->TransactionType == 'Payment') {

if($this->tx->Account == $this->account) {
//sender
if(
isset($this->tx->meta->delivered_amount->issuer) &&
isset($this->tx->Destination) &&
$this->tx->meta->delivered_amount->issuer == $this->tx->Destination
) {
if(count($eventList) == 0) {
if(is_string($this->tx->meta->delivered_amount)) {
//xrp
$eventList['primary']['currency'] = 'XRP';
$eventList['primary']['value'] = '-'.(string)BigDecimal::of($this->tx->meta->delivered_amount)->abs()->exactlyDividedBy(1000000)->stripTrailingZeros();
} else {
$eventList['primary']['currency'] = $this->tx->meta->delivered_amount->currency;
$eventList['primary']['counterparty'] = $this->tx->meta->delivered_amount->issuer;
$eventList['primary']['value'] = '-'.(string)BigDecimal::of($this->tx->meta->delivered_amount->value)->abs()->stripTrailingZeros();
}
$eventFlow['start']['account'] = $this->tx->Account;
$eventFlow['start']['mutation'] = $eventList['primary'];
$eventFlow['end']['account'] = $this->tx->Destination;
$eventFlow['end']['mutation'] = $eventList['primary'];
$eventFlow['end']['mutation']['value'] = \ltrim($eventFlow['end']['mutation']['value'],'-');
}
}

} else {
if(isset($this->tx->Destination) && $this->account == $this->tx->Destination) {
//reciever
if(
isset($this->tx->meta->delivered_amount->issuer) &&
$this->tx->meta->delivered_amount->issuer == $this->tx->Destination
) {
if(count($eventList) == 0) {

if(is_string($this->tx->meta->delivered_amount)) {
//xrp
$eventList['primary']['currency'] = 'XRP';
$eventList['primary']['value'] = (string)BigDecimal::of($this->tx->meta->delivered_amount)->abs()->exactlyDividedBy(1000000)->stripTrailingZeros();
} else {
$eventList['primary']['currency'] = $this->tx->meta->delivered_amount->currency;
$eventList['primary']['counterparty'] = $this->tx->meta->delivered_amount->issuer;
$eventList['primary']['value'] = (string)BigDecimal::of($this->tx->meta->delivered_amount->value)->abs()->stripTrailingZeros();
}
$eventFlow['start']['account'] = $this->tx->Account;
$eventFlow['start']['mutation'] = $eventList['primary'];
$eventFlow['start']['mutation']['value'] = '-'.$eventFlow['start']['mutation']['value'];
$eventFlow['end']['account'] = $this->tx->Destination;
$eventFlow['end']['mutation'] = $eventList['primary'];

}
}
}
//dd($eventList);
}

}


Expand Down
66 changes: 63 additions & 3 deletions tests/Tx20Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,37 @@ public function testPaymentSenderSentsBackFundsToIssuer()
$this->assertEquals([
'currency' => '53796D626F6C6F67790000000000000000000000',
'counterparty' => 'rMK4cYevA3vBMhiSDd76WrT8Rrhe6wqUB2',
'value' => '333'
'value' => '-333'
],$parsedTransaction['eventList']['primary']);

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

//does contain `start` entry
$this->assertArrayHasKey('start',$parsedTransaction['eventFlow']);
$this->assertEquals([
'account' => $account,
'mutation' => [
'currency' => '53796D626F6C6F67790000000000000000000000',
'counterparty' => 'rMK4cYevA3vBMhiSDd76WrT8Rrhe6wqUB2',
'value' => "-333",
]
],$parsedTransaction['eventFlow']['start']);

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

$this->assertEquals([
'account' => 'rMK4cYevA3vBMhiSDd76WrT8Rrhe6wqUB2',
'mutation' => [
'currency' => '53796D626F6C6F67790000000000000000000000',
'counterparty' => 'rMK4cYevA3vBMhiSDd76WrT8Rrhe6wqUB2',
'value' => "333",
]
],$parsedTransaction['eventFlow']['end']);



}

Expand All @@ -68,7 +96,39 @@ public function testPaymentIssuerRecievesReturnedFunds()

# Event list

//does NOT contain `primary` entry
$this->assertArrayNotHasKey('primary',$parsedTransaction['eventList']);
//does contain `primary` entry
$this->assertArrayHasKey('primary',$parsedTransaction['eventList']);
$this->assertEquals([
'currency' => '53796D626F6C6F67790000000000000000000000',
'counterparty' => 'rMK4cYevA3vBMhiSDd76WrT8Rrhe6wqUB2',
'value' => '333'
],$parsedTransaction['eventList']['primary']);

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

//does contain `start` entry
$this->assertArrayHasKey('start',$parsedTransaction['eventFlow']);
$this->assertEquals([
'account' => 'rGWv5YTG4ATZS6okStexXV5ZPRbGqb7E3k',
'mutation' => [
'currency' => '53796D626F6C6F67790000000000000000000000',
'counterparty' => 'rMK4cYevA3vBMhiSDd76WrT8Rrhe6wqUB2',
'value' => "-333",
]
],$parsedTransaction['eventFlow']['start']);

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

$this->assertEquals([
'account' => $account,
'mutation' => [
'currency' => '53796D626F6C6F67790000000000000000000000',
'counterparty' => 'rMK4cYevA3vBMhiSDd76WrT8Rrhe6wqUB2',
'value' => "333",
]
],$parsedTransaction['eventFlow']['end']);
}
}

0 comments on commit 3a89c3f

Please sign in to comment.