Skip to content

Commit

Permalink
+setMatcherSettings() +setMatcherFee()
Browse files Browse the repository at this point in the history
  • Loading branch information
deemru committed Feb 2, 2022
1 parent 1ef4f3b commit aba4779
Show file tree
Hide file tree
Showing 2 changed files with 217 additions and 4 deletions.
58 changes: 58 additions & 0 deletions docs/WavesKit.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
|[setCryptash](#waveskitsetcryptash)|Sets cryptash parameters|
|[setLastBitFlip](#waveskitsetlastbitflip)|Sets last bit flip option|
|[setLogFilter](#waveskitsetlogfilter)|Set filter specific messages|
|[setMatcherFee](#waveskitsetmatcherfee)|Sets an order fee based on matcher settings|
|[setMatcherSettings](#waveskitsetmatchersettings)|Sets matcher settings|
|[setNodeAddress](#waveskitsetnodeaddress)|Sets node address with cache lifetime and backup node addresses|
|[setPairsDatabase](#waveskitsetpairsdatabase)|Sets database pairs path|
|[setPrivateKey](#waveskitsetprivatekey)|Sets private key|
Expand Down Expand Up @@ -1353,6 +1355,62 @@ Set filter specific messages



<hr />


### WavesKit::setMatcherFee

**Description**

```php
public setMatcherFee (array $order, bool $discount)
```

Sets an order fee based on matcher settings



**Parameters**

* `(array) $order`
: Order as an array
* `(bool) $discount`
: Use dicount asset (default: true)

**Return Values**

`array|bool`

> Order as an array or FALSE on failure

<hr />


### WavesKit::setMatcherSettings

**Description**

```php
public setMatcherSettings (array|null $settings)
```

Sets matcher settings



**Parameters**

* `(array|null) $settings`
: Matcher settings or NULL to get them on-the-fly (default: null)

**Return Values**

`bool`

> TRUE on success or FALSE on failure

<hr />


Expand Down
163 changes: 159 additions & 4 deletions src/WavesKit.php
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,159 @@ private function setDefaultMatcher()
}
}

/**
* Sets matcher settings
*
* @param array|null $settings Matcher settings or NULL to get them on-the-fly (default: null)
*
* @return bool TRUE on success or FALSE on failure
*/
public function setMatcherSettings( $settings = null )
{
$this->setDefaultMatcher();

if( !isset( $settings ) )
{
$settings = $this->matcher->fetch( '/matcher/settings' );
if( $settings === false || false === ( $settings = $this->json_decode( $settings ) ) )
return false;
}

if( !isset( $settings['rates'] ) ||
!isset( $settings['orderFee']['composite']['default']['dynamic']['baseFee'] ) ||
!isset( $settings['orderFee']['composite']['custom'] ) ||
!isset( $settings['orderFee']['composite']['discount']['assetId'] ) ||
!isset( $settings['orderFee']['composite']['discount']['value'] ) )
return false;

$this->matcherBaseFee = $settings['orderFee']['composite']['default']['dynamic']['baseFee'];
$this->matcherDiscountAsset = $settings['orderFee']['composite']['discount']['assetId'];

$this->matcherRates = [];
foreach( $settings['rates'] as $asset => $rate )
{
$assetDecimals = $this->assetDecimals( $asset );
if( $assetDecimals === false )
return false;
$this->matcherRates[$asset] = $rate / $this->decimalize( 8 - $assetDecimals );
}

$this->matcherDiscountRate = $this->matcherRates[$this->matcherDiscountAsset] * ( ( 100 - $settings['orderFee']['composite']['discount']['value'] ) / 100 );

$this->matcherPairMinFees = [];
foreach( $settings['orderFee']['composite']['custom'] as $pair => $config )
{
if( !isset( $config['percent']['type'] ) ||
!isset( $config['percent']['minFee'] ) ||
!isset( $config['percent']['minFeeInWaves'] ) )
return false;

if( $config['percent']['type'] !== 'spending' ||
$config['percent']['minFeeInWaves'] !== $this->matcherBaseFee )
return false;

$this->matcherPairMinFees[$pair] = $config['percent']['minFee'] / 100;
}

return true;
}

private function assetDecimals( $asset )
{
if( $asset === 'WAVES' || $asset === null )
return 8;

static $db;
if( isset( $db[$asset] ) )
return $db[$asset];

$info = $this->fetch( '/assets/details/' . $asset );
if( $info === false || false === ( $info = $this->json_decode( $info ) ) || !isset( $info['decimals'] ) )
return false;

$db[$asset] = $info['decimals'];
return $info['decimals'];
}

private function assetId( $asset )
{
return isset( $asset ) ? $asset : 'WAVES';
}

private function decimalize( $n )
{
if( $n === 0 ) return 1;
if( $n === 1 ) return 10;
if( $n === 2 ) return 100;
if( $n === 3 ) return 1000;
if( $n === 4 ) return 10000;
if( $n === 5 ) return 100000;
if( $n === 6 ) return 1000000;
if( $n === 7 ) return 10000000;
return 100000000;
}

/**
* Sets an order fee based on matcher settings
*
* @param array $order Order as an array
* @param bool $discount Use dicount asset (default: true)
*
* @return array|bool Order as an array or FALSE on failure
*/
public function setMatcherFee( $order, $discount = true )
{
$amountAsset = $this->assetId( $order['assetPair']['amountAsset'] );
$priceAsset = $this->assetId( $order['assetPair']['priceAsset'] );
$mainAsset = $order['orderType'] === 'sell' ? $amountAsset : $priceAsset;
$pair = $amountAsset . '-' . $priceAsset;

if( !isset( $this->matcherBaseFee ) && !$this->setMatcherSettings() )
return false;

if( isset( $this->matcherPairMinFees[$pair] ) )
{
$rate = $this->matcherRates[$mainAsset];

$fee = $order['amount'] * $this->matcherPairMinFees[$pair];
$fee /= $rate;
if( $fee < $this->matcherBaseFee )
$fee = $this->matcherBaseFee;

if( $discount )
{
$asset = $this->matcherDiscountAsset;
$rate = $this->matcherDiscountRate;
}
else
{
$asset = $mainAsset;
//$rate = $rate;
}
}
else
{
$fee = $this->matcherBaseFee;

if( $discount )
{
$asset = $this->matcherDiscountAsset;
$rate = $this->matcherDiscountRate;
}
else
{
$asset = null;
$rate = 1;
}
}

$fee *= $rate;
$order['matcherFee'] = (int)ceil( $fee );
$order['matcherFeeAssetId'] = $asset;

return $order;
}

/**
* Fetches GET or POST response
*
Expand Down Expand Up @@ -1026,7 +1179,7 @@ public function height()
!isset( $json['height'] ) )
{
if( isset( $this->curlSetBestOnError ) && $this->curlSetBestOnError > 0 )
$this->curlSetBestOnError++;
$this->curlSetBestOnError++;

return false;
}
Expand Down Expand Up @@ -1200,7 +1353,7 @@ public function txOrderCancel( $tx )
}
else
return false;

if( false === ( $json = $this->matcher->fetch( '/matcher/orderbook/cancel', true, json_encode( $cancel ) ) ) )
return false;

Expand Down Expand Up @@ -1751,7 +1904,7 @@ public function txOrder( $amountAsset, $priceAsset, $isSell, $amount, $price, $e
$this->setDefaultMatcher();

$tx = [];
$tx['version'] = 2;
$tx['version'] = 3;
$tx['sender'] = isset( $options['sender'] ) ? $options['sender'] : $this->getAddress();
$tx['senderPublicKey'] = isset( $options['senderPublicKey'] ) ? $options['senderPublicKey'] : $this->getPublicKey();
$tx['matcherPublicKey'] = isset( $options['matcherPublicKey'] ) ? $options['matcherPublicKey'] : $this->matcherPublicKey;
Expand All @@ -1762,6 +1915,7 @@ public function txOrder( $amountAsset, $priceAsset, $isSell, $amount, $price, $e
$tx['amount'] = $amount;
$tx['price'] = $price;
$tx['matcherFee'] = isset( $options['matcherFee'] ) ? $options['matcherFee'] : 300000;
$tx['matcherFeeAssetId'] = isset( $options['matcherFeeAssetId'] ) ? $options['matcherFeeAssetId'] : null;
$tx['timestamp'] = isset( $options['timestamp'] ) ? $options['timestamp'] : $this->timestamp();
$tx['expiration'] = $tx['timestamp'] + $expiration;
return $tx;
Expand Down Expand Up @@ -2138,7 +2292,7 @@ public function txBody( $tx )

if( isset( $tx['orderType'] ) )
{
$body .= chr( 2 );
$body .= chr( 3 );
$body .= $this->base58Decode( $tx['senderPublicKey'] );
$body .= $this->base58Decode( $tx['matcherPublicKey'] );
$body .= isset( $tx['assetPair']['amountAsset'] ) ? chr( 1 ) . $this->base58Decode( $tx['assetPair']['amountAsset'] ) : chr( 0 );
Expand All @@ -2149,6 +2303,7 @@ public function txBody( $tx )
$body .= pack( 'J', $tx['timestamp'] );
$body .= pack( 'J', $tx['expiration'] );
$body .= pack( 'J', $tx['matcherFee'] );
$body .= isset( $tx['matcherFeeAssetId'] ) ? chr( 1 ) . $this->base58Decode( $tx['matcherFeeAssetId'] ) : chr( 0 );
return $body;
}

Expand Down

0 comments on commit aba4779

Please sign in to comment.