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

125 lines
4.3 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\Wallet\Credentials;
final class AssetBuilder
{
use SignsTransactions;
public function __construct(CryptoInterface $crypto, Credentials $credentials)
{
$this->crypto = $crypto;
$this->credentials = $credentials;
}
public function createToken(
string $creator,
string $ticker,
int $number,
bool $hardLimit,
int $fee,
?int $timestamp = null,
): SignedTransaction {
$timestamp ??= time();
$ticker = Binary::fixedText($ticker, 15, 'Token ticker');
$unsigned = [
'txtype' => 3, 'time' => $timestamp, 'creator' => $creator,
'ticker' => $ticker, 'number' => $number,
'hard_limit' => $hardLimit ? 1 : 0, 'txfee' => $fee,
];
$binary = Binary::u8(3) . Binary::u32($timestamp) . Address::toBytes($creator)
. $ticker . Binary::u64($number) . Binary::u8($hardLimit ? 1 : 0)
. Binary::u64($fee);
return $this->sign($unsigned, $binary);
}
public function issueToken(
string $creator,
string $ticker,
int $number,
int $fee,
?int $timestamp = null,
): SignedTransaction {
$timestamp ??= time();
$ticker = Binary::fixedText($ticker, 15, 'Token ticker');
$unsigned = [
'txtype' => 11, 'time' => $timestamp, 'creator' => $creator,
'ticker' => $ticker, 'number' => $number, 'txfee' => $fee,
];
$binary = Binary::u8(11) . Binary::u32($timestamp) . Address::toBytes($creator)
. $ticker . Binary::u64($number) . Binary::u64($fee);
return $this->sign($unsigned, $binary);
}
public function createNft(
string $creator,
bool $series,
bool $fractionalOwnership,
string $name,
string $ipfs,
int $count,
string $description,
int $fee,
?int $timestamp = null,
): SignedTransaction {
$timestamp ??= time();
$name = Binary::fixedText($name, 15, 'NFT name');
$ipfs = Binary::fixedText($ipfs, 100, 'NFT IPFS location');
$description = Binary::fixedText($description, 100, 'NFT description');
$unsigned = [
'txtype' => 4, 'time' => $timestamp, 'creator' => $creator,
'series' => $series ? 1 : 0,
'ownership_type' => $fractionalOwnership ? 1 : 0,
'nft_name' => $name, 'item_ipfs' => $ipfs, 'count' => $count,
'desc' => $description, 'txfee' => $fee,
];
$binary = Binary::u8(4) . Binary::u32($timestamp) . Address::toBytes($creator)
. Binary::u8($series ? 1 : 0) . Binary::u8($fractionalOwnership ? 1 : 0)
. $name . $ipfs . Binary::u32($count) . $description . Binary::u64($fee);
return $this->sign($unsigned, $binary);
}
public function burn(
string $address,
string $coin,
int $nftSeries,
int $value,
int $fee,
?int $timestamp = null,
): SignedTransaction {
$timestamp ??= time();
$coin = Binary::fixedText($coin, 15, 'Asset name');
$unsigned = [
'txtype' => 10, 'time' => $timestamp, 'address' => $address,
'coin' => $coin, 'nft_series' => $nftSeries, 'value' => $value,
'txfee' => $fee,
];
$binary = Binary::u8(10) . Binary::u32($timestamp) . Address::toBytes($address)
. $coin . Binary::u32($nftSeries) . Binary::u64($value) . Binary::u64($fee);
return $this->sign($unsigned, $binary);
}
public function vanity(
string $address,
string $vanityAddress,
int $fee,
?int $timestamp = null,
): SignedTransaction {
$timestamp ??= time();
$unsigned = [
'txtype' => 12, 'timestamp' => $timestamp, 'address' => $address,
'vanity_address' => $vanityAddress, 'txfee' => $fee,
];
$binary = Binary::u8(12) . Binary::u32($timestamp) . Address::toBytes($address)
. Address::vanityToBytes($vanityAddress) . Binary::u64($fee);
return $this->sign($unsigned, $binary);
}
}