diff --git a/docs/WavesKit.md b/docs/WavesKit.md index 9a89558..bd85b4c 100644 --- a/docs/WavesKit.md +++ b/docs/WavesKit.md @@ -72,6 +72,7 @@ |[txBroadcast](#waveskittxbroadcast)|Broadcasts a transaction| |[txBurn](#waveskittxburn)|Makes burn transaction as an array| |[txData](#waveskittxdata)|Makes data transaction as an array| +|[txDiffs](#waveskittxdiffs)|Calculates a transaction address/assets/amounts diffs as an array| |[txEvaluate](#waveskittxevaluate)|Evaluates a transaction| |[txInvokeScript](#waveskittxinvokescript)|Makes invoke script transaction as an array| |[txIssue](#waveskittxissue)|Makes issue transaction as an array| @@ -1824,7 +1825,7 @@ Makes asset script transaction as an array **Description** ```php -public txBody (string $tx) +public txBody (array $tx) ``` Gets transaction body @@ -1833,7 +1834,7 @@ Gets transaction body **Parameters** -* `(string) $tx` +* `(array) $tx` : Transaction as an array **Return Values** @@ -1933,6 +1934,33 @@ Makes data transaction as an array
+### WavesKit::txDiffs + +**Description** + +```php +public txDiffs (array $tx) +``` + +Calculates a transaction address/assets/amounts diffs as an array + + + +**Parameters** + +* `(array) $tx` +: Transaction as an array + +**Return Values** + +`array|false` + +> Calculated diffs as an array or FALSE on failure + + +
+ + ### WavesKit::txEvaluate **Description** diff --git a/src/WavesKit.php b/src/WavesKit.php index d7f3770..9dbf0c7 100644 --- a/src/WavesKit.php +++ b/src/WavesKit.php @@ -1180,6 +1180,51 @@ public function txEvaluate( $tx ) return $json; } + private function txDiffsAmount( &$diffs, $address, $asset, $amount ) + { + $diffs[$address][$asset] = $amount + ( isset( $diffs[$address][$asset] ) ? $diffs[$address][$asset] : 0 ); + } + + /** + * Calculates a transaction address/assets/amounts diffs as an array + * + * @param array $tx Transaction as an array + * + * @return array|false Calculated diffs as an array or FALSE on failure + */ + public function txDiffs( $tx, $caller = null, &$diffs = null ) + { + if( $diffs === null ) + $diffs = []; + + if( $caller === null ) + $caller = $tx['sender']; + + $dApp = $tx['dApp']; + $payments = $tx['payment']; + $stateChanges = $tx['stateChanges']; + + foreach( $stateChanges['invokes'] as $tx ) + $this->txDiffs( $tx, $dApp, $diffs ); + + foreach( $payments as $payment ) + { + $amount = $payment['amount']; + $asset = isset( $payment['assetId'] ) ? $payment['assetId'] : 'WAVES'; + $this->txDiffsAmount( $diffs, $caller, $asset, -$amount ); + $this->txDiffsAmount( $diffs, $dApp, $asset, +$amount ); + } + foreach( $stateChanges['transfers'] as $transfer ) + { + $amount = $transfer['amount']; + $asset = isset( $transfer['asset'] ) ? $transfer['asset'] : 'WAVES'; + $this->txDiffsAmount( $diffs, $dApp, $asset, -$amount ); + $this->txDiffsAmount( $diffs, $transfer['address'], $asset, +$amount ); + } + + return $diffs; + } + /** * Broadcasts a transaction * @@ -2218,7 +2263,7 @@ private function getScriptFee( $tx ) /** * Gets transaction body * - * @param string $tx Transaction as an array + * @param array $tx Transaction as an array * * @return string|false Body of the transaction or FALSE on failure */ diff --git a/test/selftest.php b/test/selftest.php index dfe0f87..bbe9c1e 100644 --- a/test/selftest.php +++ b/test/selftest.php @@ -659,6 +659,12 @@ function getData( $wk, $key ) $tx = $wk->txInvokeScript( '3MsABb4zvG6U2gczxt7y91XSzGGuZzmLxsi', 'retransmit', $args, $payments ); $tx['version'] = 2; $tx = $wk->txSign( $tx ); + { + $evtx = $wk->txEvaluate( $tx ); + $evtxDiffs = $wk->txDiffs( $evtx ); + $vtx = $wk->txValidate( $tx ); + $vtxDiffs = $wk->txDiffs( $vtx ); + } $tx = $wk->txBroadcast( $tx ); $result &= $tx !== false; @@ -666,4 +672,11 @@ function getData( $wk, $key ) $t->test( $result ); } +$t->pretest( 'txDiffs' ); +{ + $etx = $wk->ensure( $evtx ); + $etxDiffs = $wk->txDiffs( $etx ); + $t->test( -1 === $etxDiffs[$wk->getAddress()]['WAVES'] && $etxDiffs === $evtxDiffs && $etxDiffs === $vtxDiffs ); +} + $t->finish(); \ No newline at end of file