2026-07-25 21:23:28 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace Contractless\Rpc\Transaction;
|
|
|
|
|
|
|
|
|
|
use Contractless\Rpc\Crypto\CryptoInterface;
|
|
|
|
|
use Contractless\Rpc\Protocol\Address;
|
|
|
|
|
use Contractless\Rpc\Protocol\Binary;
|
|
|
|
|
use Contractless\Rpc\Protocol\RequestBuilder;
|
2026-07-27 20:24:58 +00:00
|
|
|
use Contractless\Rpc\Signing\SignatureProviderInterface;
|
2026-07-25 21:23:28 +00:00
|
|
|
|
|
|
|
|
final class MiscellaneousBuilder
|
|
|
|
|
{
|
|
|
|
|
use SignsTransactions;
|
|
|
|
|
|
2026-07-27 20:24:58 +00:00
|
|
|
public function __construct(
|
|
|
|
|
CryptoInterface $crypto,
|
|
|
|
|
SignatureProviderInterface $signer,
|
|
|
|
|
)
|
2026-07-25 21:23:28 +00:00
|
|
|
{
|
|
|
|
|
$this->crypto = $crypto;
|
2026-07-27 20:24:58 +00:00
|
|
|
$this->signer = $signer;
|
2026-07-25 21:23:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function marketing(
|
|
|
|
|
int $campaign,
|
|
|
|
|
string $adType,
|
|
|
|
|
string $keyword,
|
|
|
|
|
string $displayed,
|
|
|
|
|
int $impressions,
|
|
|
|
|
int $clicks,
|
|
|
|
|
int $impressionValue,
|
|
|
|
|
int $clickValue,
|
|
|
|
|
string $advertiser,
|
|
|
|
|
int $fee,
|
|
|
|
|
?int $timestamp = null,
|
|
|
|
|
): SignedTransaction {
|
|
|
|
|
$timestamp ??= time();
|
|
|
|
|
$adType = Binary::fixedText($adType, 6, 'Ad type');
|
|
|
|
|
$keyword = Binary::fixedText($keyword, 40, 'Marketing keyword');
|
|
|
|
|
$displayed = Binary::fixedText($displayed, 100, 'Display location');
|
|
|
|
|
$unsigned = [
|
|
|
|
|
'txtype' => 5, 'time' => $timestamp, 'campaign' => $campaign,
|
|
|
|
|
'ad_type' => $adType, 'keyword' => $keyword, 'displayed' => $displayed,
|
|
|
|
|
'impression' => $impressions, 'click' => $clicks,
|
|
|
|
|
'impression_value' => $impressionValue, 'click_value' => $clickValue,
|
|
|
|
|
'advertiser' => $advertiser, 'txfee' => $fee,
|
|
|
|
|
];
|
|
|
|
|
$binary = Binary::u8(5) . Binary::u32($timestamp) . Binary::u64($campaign)
|
|
|
|
|
. $adType . $keyword . $displayed . Binary::u8($impressions)
|
|
|
|
|
. Binary::u8($clicks) . Binary::u16($impressionValue)
|
|
|
|
|
. Binary::u16($clickValue) . Address::toBytes($advertiser)
|
|
|
|
|
. Binary::u64($fee);
|
|
|
|
|
return $this->sign($unsigned, $binary);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function collateralClaim(
|
|
|
|
|
string $contractHash,
|
|
|
|
|
string $address,
|
|
|
|
|
int $fee,
|
|
|
|
|
?int $timestamp = null,
|
|
|
|
|
): SignedTransaction {
|
|
|
|
|
$timestamp ??= time();
|
|
|
|
|
$unsigned = [
|
|
|
|
|
'txtype' => 9, 'time' => $timestamp, 'contract_hash' => $contractHash,
|
|
|
|
|
'address' => $address, 'txfee' => $fee,
|
|
|
|
|
];
|
|
|
|
|
$binary = Binary::u8(9) . Binary::u32($timestamp)
|
|
|
|
|
. RequestBuilder::hash($contractHash, 'Loan contract hash')
|
|
|
|
|
. Address::toBytes($address) . Binary::u64($fee);
|
|
|
|
|
return $this->sign($unsigned, $binary);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function loanPayment(
|
|
|
|
|
int $amount,
|
|
|
|
|
string $contractHash,
|
|
|
|
|
string $address,
|
|
|
|
|
int $tip,
|
|
|
|
|
int $fee,
|
|
|
|
|
?int $timestamp = null,
|
|
|
|
|
): SignedTransaction {
|
|
|
|
|
$timestamp ??= time();
|
|
|
|
|
$unsigned = [
|
|
|
|
|
'txtype' => 8, 'timestamp' => $timestamp, 'payback_amount' => $amount,
|
|
|
|
|
'contract_hash' => $contractHash, 'address' => $address,
|
|
|
|
|
'tip' => $tip, 'txfee' => $fee,
|
|
|
|
|
];
|
|
|
|
|
$json = json_encode(
|
|
|
|
|
$unsigned,
|
|
|
|
|
JSON_THROW_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION,
|
|
|
|
|
);
|
|
|
|
|
$hash = $this->crypto->skein256($json);
|
2026-07-27 20:24:58 +00:00
|
|
|
$signature = $this->signer->sign($hash);
|
2026-07-25 21:23:28 +00:00
|
|
|
$binary = Binary::u8(8) . Binary::u32($timestamp) . Binary::u64($amount)
|
|
|
|
|
. RequestBuilder::hash($contractHash, 'Loan contract hash')
|
|
|
|
|
. Address::toBytes($address) . Binary::u64($tip) . Binary::u64($fee)
|
|
|
|
|
. $hash . $signature;
|
|
|
|
|
return new SignedTransaction($binary, $signature, $json);
|
|
|
|
|
}
|
|
|
|
|
}
|