Skip to content

Commit

Permalink
added txDiffs function
Browse files Browse the repository at this point in the history
  • Loading branch information
deemru committed Nov 21, 2024
1 parent 39a0fc9 commit 7c3e536
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 deletions.
32 changes: 30 additions & 2 deletions docs/WavesKit.md
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down Expand Up @@ -1824,7 +1825,7 @@ Makes asset script transaction as an array
**Description**

```php
public txBody (string $tx)
public txBody (array $tx)
```

Gets transaction body
Expand All @@ -1833,7 +1834,7 @@ Gets transaction body

**Parameters**

* `(string) $tx`
* `(array) $tx`
: Transaction as an array

**Return Values**
Expand Down Expand Up @@ -1933,6 +1934,33 @@ Makes data transaction as an array
<hr />


### 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

<hr />


### WavesKit::txEvaluate

**Description**
Expand Down
47 changes: 46 additions & 1 deletion src/WavesKit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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
*/
Expand Down
13 changes: 13 additions & 0 deletions test/selftest.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,11 +659,24 @@ 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;

$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();

0 comments on commit 7c3e536

Please sign in to comment.