Contractless-PHP-RPC/src/Transaction/MiscellaneousBuilder.php

103 lines
3.6 KiB
PHP
Raw Normal View History

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;
use Contractless\Rpc\Wallet\Credentials;
final class MiscellaneousBuilder
{
use SignsTransactions;
public function __construct(CryptoInterface $crypto, Credentials $credentials)
{
$this->crypto = $crypto;
$this->credentials = $credentials;
}
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);
$signature = $this->crypto->sign(
$hash,
$this->credentials->privateKey,
$this->credentials->publicKey,
);
$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);
}
}