Add optional unsweep amount
This commit is contained in:
@@ -28,7 +28,7 @@ A PHP-based Telegram tip bot for the Salvium (Monero fork) cryptocurrency.
|
|||||||
## Commands
|
## Commands
|
||||||
- `/withdraw <address> <amount>` withdraws `SAL1`
|
- `/withdraw <address> <amount>` withdraws `SAL1`
|
||||||
- `/withdraw <asset> <address> <amount>` withdraws `SAL1` or a token asset, for example `CULT`/`salCULT`
|
- `/withdraw <asset> <address> <amount>` withdraws `SAL1` or a token asset, for example `CULT`/`salCULT`
|
||||||
- `/unsweep <asset> <outputs>` admin-only command that self-transfers unlocked wallet funds into multiple smaller outputs
|
- `/unsweep <asset> <outputs> [amount]` admin-only command that self-transfers unlocked wallet funds into multiple smaller outputs
|
||||||
|
|
||||||
## Withdrawal fees
|
## Withdrawal fees
|
||||||
- `WITHDRAWAL_FEE` is charged in `SAL1` for `SAL1` withdrawals.
|
- `WITHDRAWAL_FEE` is charged in `SAL1` for `SAL1` withdrawals.
|
||||||
|
|||||||
@@ -266,9 +266,9 @@ class SalviumTipBotCommands {
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
[$assetType, $outputCount] = $this->parseUnsweepArgs($args);
|
[$assetType, $outputCount, $requestedAmountAtomic] = $this->parseUnsweepArgs($args);
|
||||||
} catch (Throwable $e) {
|
} catch (Throwable $e) {
|
||||||
return "Usage: /unsweep <asset> <outputs>";
|
return "Usage: /unsweep <asset> <outputs> [amount]";
|
||||||
}
|
}
|
||||||
|
|
||||||
$maxOutputs = max(2, (int)($this->config['MAX_UNSWEEP_OUTPUTS'] ?? 100));
|
$maxOutputs = max(2, (int)($this->config['MAX_UNSWEEP_OUTPUTS'] ?? 100));
|
||||||
@@ -315,7 +315,11 @@ class SalviumTipBotCommands {
|
|||||||
$reserveAtomic = $this->humanToAtomic($this->config['UNSWEEP_SAL1_FEE_RESERVE'] ?? 0.5);
|
$reserveAtomic = $this->humanToAtomic($this->config['UNSWEEP_SAL1_FEE_RESERVE'] ?? 0.5);
|
||||||
}
|
}
|
||||||
|
|
||||||
$spendableAtomic = $unlockedAtomic - $reserveAtomic;
|
$spendableAtomic = $requestedAmountAtomic ?? ($unlockedAtomic - $reserveAtomic);
|
||||||
|
if ($requestedAmountAtomic !== null && $spendableAtomic > ($unlockedAtomic - $reserveAtomic)) {
|
||||||
|
return "Requested amount exceeds spendable unlocked {$assetType}. Max is " . $this->atomicToHuman(max(0, $unlockedAtomic - $reserveAtomic)) . " {$assetType}.";
|
||||||
|
}
|
||||||
|
|
||||||
if ($spendableAtomic <= 0) {
|
if ($spendableAtomic <= 0) {
|
||||||
return "No spendable {$assetType} after fee reserve of " . $this->atomicToHuman($reserveAtomic) . " {$assetType}.";
|
return "No spendable {$assetType} after fee reserve of " . $this->atomicToHuman($reserveAtomic) . " {$assetType}.";
|
||||||
}
|
}
|
||||||
@@ -346,8 +350,9 @@ class SalviumTipBotCommands {
|
|||||||
|
|
||||||
$totalAtomic = $amountPerOutputAtomic * $outputCount;
|
$totalAtomic = $amountPerOutputAtomic * $outputCount;
|
||||||
$reserveText = $reserveAtomic > 0 ? " Reserved " . $this->atomicToHuman($reserveAtomic) . " {$assetType} for fee/change." : "";
|
$reserveText = $reserveAtomic > 0 ? " Reserved " . $this->atomicToHuman($reserveAtomic) . " {$assetType} for fee/change." : "";
|
||||||
|
$sourceText = $requestedAmountAtomic !== null ? " Requested " . $this->atomicToHuman($requestedAmountAtomic) . " {$assetType}." : "";
|
||||||
return "Unswept " . $this->atomicToHuman($totalAtomic) . " {$assetType} into {$outputCount} outputs of "
|
return "Unswept " . $this->atomicToHuman($totalAtomic) . " {$assetType} into {$outputCount} outputs of "
|
||||||
. $this->atomicToHuman($amountPerOutputAtomic) . " {$assetType}.{$reserveText} "
|
. $this->atomicToHuman($amountPerOutputAtomic) . " {$assetType}.{$sourceText}{$reserveText} "
|
||||||
. $this->formatTxids($txids);
|
. $this->formatTxids($txids);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -492,7 +497,7 @@ class SalviumTipBotCommands {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private function parseUnsweepArgs(array $args): array {
|
private function parseUnsweepArgs(array $args): array {
|
||||||
if (count($args) !== 3 || !$this->looksLikeAssetType($args[1]) || !ctype_digit((string)$args[2])) {
|
if (!in_array(count($args), [3, 4], true) || !$this->looksLikeAssetType($args[1]) || !ctype_digit((string)$args[2])) {
|
||||||
throw new InvalidArgumentException("Invalid /unsweep arguments");
|
throw new InvalidArgumentException("Invalid /unsweep arguments");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -501,7 +506,18 @@ class SalviumTipBotCommands {
|
|||||||
throw new InvalidArgumentException("Invalid /unsweep output count");
|
throw new InvalidArgumentException("Invalid /unsweep output count");
|
||||||
}
|
}
|
||||||
|
|
||||||
return [$this->parseAssetType($args[1]), $outputCount];
|
$requestedAmountAtomic = null;
|
||||||
|
if (count($args) === 4) {
|
||||||
|
if (!is_numeric($args[3]) || (float)$args[3] <= 0) {
|
||||||
|
throw new InvalidArgumentException("Invalid /unsweep amount");
|
||||||
|
}
|
||||||
|
$requestedAmountAtomic = $this->humanToAtomic($args[3]);
|
||||||
|
if ($requestedAmountAtomic <= 0) {
|
||||||
|
throw new InvalidArgumentException("Invalid /unsweep amount");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [$this->parseAssetType($args[1]), $outputCount, $requestedAmountAtomic];
|
||||||
}
|
}
|
||||||
|
|
||||||
private function atomicSubtractNonNegative(string $left, string $right): string {
|
private function atomicSubtractNonNegative(string $left, string $right): string {
|
||||||
|
|||||||
Reference in New Issue
Block a user