Skip to content

Commit 5faf1cb

Browse files
committed
Add BC MPT issuer MPTokenIssuance - OutstandingAmount
1 parent d9e57c0 commit 5faf1cb

File tree

2 files changed

+92
-3
lines changed

2 files changed

+92
-3
lines changed

src/Utilities/BalanceChanges.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public function __construct(\stdClass $metadata, bool $calculateTradingFees = fa
4646
if($mptQuantity !== null)
4747
$quantities[$mptQuantity['account']][] = $mptQuantity;
4848
}
49+
50+
if($node->LedgerEntryType == 'MPTokenIssuance') {
51+
$mptOaQuantity = $this->getMPTOaQuantity($node);
52+
if($mptOaQuantity !== null)
53+
$quantities[$mptOaQuantity['account']][] = $mptOaQuantity;
54+
}
4955
}
5056
# Reorganize quantities array
5157
$final = [];
@@ -184,6 +190,66 @@ private function getMPTQuantity(\stdClass $node): ?array
184190
return $result;
185191
}
186192

193+
/**
194+
* Get OutstandingAmount balance change from MPTokenIssuance
195+
*/
196+
private function getMPTOaQuantity(\stdClass $node): ?array
197+
{
198+
$value = $this->computeMPTOutstandingAmountChange($node);
199+
200+
if($value === null)
201+
return null;
202+
$account = null;
203+
if($node->FinalFields && $node->FinalFields->Issuer)
204+
$account = $node->FinalFields->Issuer;
205+
elseif($node->NewFields && $node->NewFields->Issuer)
206+
$account = $node->NewFields->Issuer;
207+
if($account === null)
208+
return null;
209+
$fields = ($node->NewFields === null) ? $node->FinalFields : $node->NewFields;
210+
211+
$result = [
212+
'account' => (string)$account,
213+
'balance' => [
214+
'mpt_issuance_id' => Util::makeMptID($fields->Sequence,$fields->Issuer),
215+
'value' => (string)BigDecimal::of($value->toInt())->stripTrailingZeros(), //unscaled
216+
]
217+
];
218+
219+
return $result;
220+
}
221+
222+
private function computeMPTOutstandingAmountChange(\stdClass $node): ?BigDecimal
223+
{
224+
$value = null;
225+
if($node->NewFields !== null && isset($node->NewFields->OutstandingAmount)) {
226+
$value = $this->getValue($node->NewFields->OutstandingAmount);
227+
} elseif($node->PreviousFields !== null && isset($node->PreviousFields->OutstandingAmount) && $node->FinalFields !== null && isset($node->FinalFields->OutstandingAmount)) {
228+
$value = $this->getValue($node->FinalFields->OutstandingAmount)->minus($this->getValue($node->PreviousFields->OutstandingAmount));
229+
}
230+
231+
/*elseif($node->PreviousFields !== null && !isset($node->PreviousFields->OutstandingAmount) && $node->FinalFields !== null && isset($node->FinalFields->OutstandingAmount)) {
232+
$PreviousFieldsKeys = \array_keys((array)$node->PreviousFields);
233+
if(count($PreviousFieldsKeys)) {
234+
//there was some prev keys but MPTAmount was not set, something else than balance was changed
235+
$value = $this->getValue('0');
236+
} else {
237+
//there was no prev keys set
238+
//see 732EE5C1222385C34F965EF0FC7C2CD3E952AAA6A4CF2CA2F35D43C4CD40DCF6
239+
$value = $this->getValue('0');
240+
}
241+
}*/
242+
243+
if($value === null)
244+
return null;
245+
246+
if($value->isEqualTo(0))
247+
return null;
248+
return $value->negated();
249+
//return $value;
250+
251+
}
252+
187253
private function computeMPTAmountChange(\stdClass $node): ?BigDecimal
188254
{
189255
$value = null;

tests/Unit/BalanceChangesTest.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,13 +365,18 @@ public function test_mpt_payment1()
365365

366366
$parser = new BalanceChanges($tx->result->meta,false);
367367
$result = $parser->result();
368+
368369
$expected = [
369370
[
370371
'account' => 'rGepNyxjJbtN75Zb4fgkjQsnv3UUcbp45E',
371372
'balances' => [
372373
[
373374
'currency' => 'XRP',
374375
'value' => '-0.000001'
376+
],
377+
[
378+
'mpt_issuance_id' => '0042AB9FAB8A5036CE4DB80D47016F557F9BFC9523985BF1',
379+
'value' => '-589589'
375380
]
376381
],
377382
],
@@ -397,13 +402,18 @@ public function test_mpt_payment2()
397402

398403
$parser = new BalanceChanges($tx->result->meta,false);
399404
$result = $parser->result();
405+
400406
$expected = [
401407
[
402408
'account' => 'rGepNyxjJbtN75Zb4fgkjQsnv3UUcbp45E',
403409
'balances' => [
404410
[
405411
'currency' => 'XRP',
406412
'value' => '-0.000001'
413+
],
414+
[
415+
'mpt_issuance_id' => '0042AB9EAB8A5036CE4DB80D47016F557F9BFC9523985BF1',
416+
'value' => '-58900'
407417
]
408418
],
409419
],
@@ -431,7 +441,7 @@ public function test_mpt_payment3()
431441

432442
$parser = new BalanceChanges($tx->result->meta,false);
433443
$result = $parser->result();
434-
//dd($result);
444+
435445
$expected = [
436446
[
437447
'account' => 'rMdLLyrrh1UC7M5rA4UVvBDjsbzi4Go1yc',
@@ -446,6 +456,15 @@ public function test_mpt_payment3()
446456
]
447457
],
448458
],
459+
[
460+
'account' => 'rGepNyxjJbtN75Zb4fgkjQsnv3UUcbp45E',
461+
'balances' => [
462+
[
463+
'mpt_issuance_id' => '0042AB9EAB8A5036CE4DB80D47016F557F9BFC9523985BF1',
464+
'value' => '100000',
465+
]
466+
],
467+
],
449468
];
450469

451470
$this->assertEquals($expected,$result);
@@ -460,15 +479,19 @@ public function test_mpt_clawback1()
460479

461480
$parser = new BalanceChanges($tx->result->meta,false);
462481
$result = $parser->result();
463-
//dd($result);
482+
464483
$expected = [
465484
[
466485
'account' => 'rGepNyxjJbtN75Zb4fgkjQsnv3UUcbp45E',
467486
'balances' => [
468487
[
469488
'currency' => 'XRP',
470489
'value' => '-0.000001'
471-
]
490+
],
491+
[
492+
'mpt_issuance_id' => '0042AB9FAB8A5036CE4DB80D47016F557F9BFC9523985BF1',
493+
'value' => '10000',
494+
],
472495
],
473496
],
474497
[

0 commit comments

Comments
 (0)