Skip to content

Commit

Permalink
Merge pull request #45 from Soneso/p-22
Browse files Browse the repository at this point in the history
Support for Protocol 22
  • Loading branch information
christian-rogobete authored Oct 24, 2024
2 parents 162fa42 + a587214 commit 65fa4b8
Show file tree
Hide file tree
Showing 26 changed files with 544 additions and 209 deletions.
133 changes: 133 additions & 0 deletions Soneso/StellarSDK/CreateContractWithConstructorHostFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php declare(strict_types=1);

// Copyright 2024 The Stellar PHP SDK Authors. All rights reserved.
// Use of this source code is governed by a license that can be
// found in the LICENSE file.


namespace Soneso\StellarSDK;

use Exception;
use Soneso\StellarSDK\Soroban\Address;
use Soneso\StellarSDK\Xdr\XdrContractIDPreimageType;
use Soneso\StellarSDK\Xdr\XdrHostFunction;
use Soneso\StellarSDK\Xdr\XdrHostFunctionType;
use Soneso\StellarSDK\Xdr\XdrContractExecutableType;
use Soneso\StellarSDK\Xdr\XdrSCVal;

class CreateContractWithConstructorHostFunction extends HostFunction
{
public Address $address;
public string $wasmId;
public string $salt;
/**
* @var array<XdrSCVal>
*/
public array $constructorArgs;


/**
* @param Address $address
* @param string $wasmId
* @param string|null $salt
* @throws Exception
*/
public function __construct(Address $address, string $wasmId, array $constructorArgs, ?string $salt = null)
{
$this->address = $address;
$this->wasmId = $wasmId;
$this->constructorArgs = $constructorArgs;
$this->salt = $salt != null ? $salt : random_bytes(32);
parent::__construct();
}

public function toXdr() : XdrHostFunction {
return XdrHostFunction::forCreatingContractV2($this->address->toXdr(),
$this->wasmId, $this->salt, $this->constructorArgs);
}

/**
* @throws Exception
*/
public static function fromXdr(XdrHostFunction $xdr) : CreateContractWithConstructorHostFunction {
$type = $xdr->type;
if ($type->value !== XdrHostFunctionType::HOST_FUNCTION_TYPE_CREATE_CONTRACT_V2 || $xdr->createContractV2 === null
|| $xdr->createContractV2->contractIDPreimage->type->value !== XdrContractIDPreimageType::CONTRACT_ID_PREIMAGE_FROM_ADDRESS
|| $xdr->createContractV2->executable->type->value !== XdrContractExecutableType::CONTRACT_EXECUTABLE_WASM) {
throw new Exception("Invalid argument");
}
$wasmId = $xdr->createContractV2->executable->wasmIdHex;
$xdrAddress = $xdr->createContractV2->contractIDPreimage->address;

if ($wasmId === null || $xdrAddress === null) {
throw new Exception("invalid argument");
}
return new CreateContractWithConstructorHostFunction(Address::fromXdr($xdrAddress), $wasmId,
$xdr->createContractV2->constructorArgs, $xdr->createContract->contractIDPreimage->salt);
}

/**
* @return Address
*/
public function getAddress(): Address
{
return $this->address;
}

/**
* @param Address $address
*/
public function setAddress(Address $address): void
{
$this->address = $address;
}

/**
* @return string
*/
public function getWasmId(): string
{
return $this->wasmId;
}

/**
* @param string $wasmId
*/
public function setWasmId(string $wasmId): void
{
$this->wasmId = $wasmId;
}

/**
* @return string
*/
public function getSalt(): string
{
return $this->salt;
}

/**
* @param string $salt
*/
public function setSalt(string $salt): void
{
$this->salt = $salt;
}

/**
* @return array
*/
public function getConstructorArgs(): array
{
return $this->constructorArgs;
}

/**
* @param array $constructorArgs
*/
public function setConstructorArgs(array $constructorArgs): void
{
$this->constructorArgs = $constructorArgs;
}

}
22 changes: 16 additions & 6 deletions Soneso/StellarSDK/DeploySACWithAssetHostFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,25 @@ public function toXdr() : XdrHostFunction {
*/
public static function fromXdr(XdrHostFunction $xdr) : DeploySACWithAssetHostFunction {
$type = $xdr->type;
if ($type->value != XdrHostFunctionType::HOST_FUNCTION_TYPE_CREATE_CONTRACT || $xdr->createContract == null
|| $xdr->createContract->contractIDPreimage->type->value != XdrContractIDPreimageType::CONTRACT_ID_PREIMAGE_FROM_ASSET
|| $xdr->createContract->executable->type->value != XdrContractExecutableType::CONTRACT_EXECUTABLE_STELLAR_ASSET) {
if ($type->value !== XdrHostFunctionType::HOST_FUNCTION_TYPE_CREATE_CONTRACT &&
$type->value !== XdrHostFunctionType::HOST_FUNCTION_TYPE_CREATE_CONTRACT_V2) {
throw new Exception("Invalid argument");
}

$xdrAsset = $xdr->createContract->contractIDPreimage->asset;
if ($xdrAsset == null) {
throw new Exception("invalid argument");
$preimage = $xdr->createContract !== null ? $xdr->createContract->contractIDPreimage :
($xdr->createContractV2 !== null ? $xdr->createContractV2->contractIDPreimage : null);
$executableTypeValue = $xdr->createContract !== null ? $xdr->createContract->executable->type->value :
($xdr->createContractV2 !== null ? $xdr->createContractV2->executable->type->value : null);
$xdrAsset = $xdr->createContract !== null ? $xdr->createContract->contractIDPreimage->asset :
($xdr->createContractV2 !== null ? $xdr->createContractV2->contractIDPreimage->asset : null);

if($preimage === null || $executableTypeValue === null || $xdrAsset === null) {
throw new Exception("Invalid argument");
}

if ($preimage->type->value !== XdrContractIDPreimageType::CONTRACT_ID_PREIMAGE_FROM_ASSET ||
$executableTypeValue !== XdrContractExecutableType::CONTRACT_EXECUTABLE_STELLAR_ASSET) {
throw new Exception("Invalid argument");
}

return new DeploySACWithAssetHostFunction(Asset::fromXdr($xdrAsset));
Expand Down
23 changes: 16 additions & 7 deletions Soneso/StellarSDK/DeploySACWithSourceAccountHostFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,28 @@ public function toXdr() : XdrHostFunction {
*/
public static function fromXdr(XdrHostFunction $xdr) : DeploySACWithSourceAccountHostFunction {
$type = $xdr->type;
if ($type->value != XdrHostFunctionType::HOST_FUNCTION_TYPE_CREATE_CONTRACT || $xdr->createContract == null
|| $xdr->createContract->contractIDPreimage->type->value != XdrContractIDPreimageType::CONTRACT_ID_PREIMAGE_FROM_ADDRESS
|| $xdr->createContract->executable->type->value != XdrContractExecutableType::CONTRACT_EXECUTABLE_STELLAR_ASSET) {
if ($type->value !== XdrHostFunctionType::HOST_FUNCTION_TYPE_CREATE_CONTRACT &&
$type->value !== XdrHostFunctionType::HOST_FUNCTION_TYPE_CREATE_CONTRACT_V2) {
throw new Exception("Invalid argument");
}

$xdrAddress = $xdr->createContract->contractIDPreimage->address;
$preimage = $xdr->createContract !== null ? $xdr->createContract->contractIDPreimage :
($xdr->createContractV2 !== null ? $xdr->createContractV2->contractIDPreimage : null);
$executableTypeValue = $xdr->createContract != null ? $xdr->createContract->executable->type->value :
($xdr->createContractV2 !== null ? $xdr->createContractV2->executable->type->value : null);
$xdrAddress = $xdr->createContract !== null ? $xdr->createContract->contractIDPreimage->address :
($xdr->createContractV2 !== null ? $xdr->createContractV2->contractIDPreimage->address : null);

if ($xdrAddress == null) {
throw new Exception("invalid argument");
if($preimage === null || $executableTypeValue === null || $xdrAddress === null) {
throw new Exception("Invalid argument");
}

if ($preimage->type->value !== XdrContractIDPreimageType::CONTRACT_ID_PREIMAGE_FROM_ADDRESS ||
$executableTypeValue !== XdrContractExecutableType::CONTRACT_EXECUTABLE_STELLAR_ASSET) {
throw new Exception("Invalid argument");
}

return new DeploySACWithSourceAccountHostFunction(Address::fromXdr($xdrAddress), $xdr->createContract->contractIDPreimage->salt);
return new DeploySACWithSourceAccountHostFunction(Address::fromXdr($xdrAddress), $preimage->salt);
}

/**
Expand Down
17 changes: 17 additions & 0 deletions Soneso/StellarSDK/InvokeHostFunctionOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,23 @@ public static function fromXdrOperation(XdrInvokeHostFunctionOp $xdrOp): InvokeH
return new InvokeHostFunctionOperation(DeploySACWithAssetHostFunction::fromXdr($xdrFunction), $auth);
}
break;
case XdrHostFunctionType::HOST_FUNCTION_TYPE_CREATE_CONTRACT_V2:
$createContractV2 = $xdrFunction->createContractV2;
if ($createContractV2 == null) {
throw new Exception("invalid argument");
}
$contractIdPreimageTypeVal = $createContractV2->contractIDPreimage->type->value;
$executableTypeValue = $createContractV2->executable->type->value;
if ($contractIdPreimageTypeVal == XdrContractIDPreimageType::CONTRACT_ID_PREIMAGE_FROM_ADDRESS) {
if ($executableTypeValue == XdrContractExecutableType::CONTRACT_EXECUTABLE_WASM) {
return new InvokeHostFunctionOperation(CreateContractWithConstructorHostFunction::fromXdr($xdrFunction), $auth);
} else if ($executableTypeValue == XdrContractExecutableType::CONTRACT_EXECUTABLE_STELLAR_ASSET){
return new InvokeHostFunctionOperation(DeploySACWithSourceAccountHostFunction::fromXdr($xdrFunction), $auth);
}
} else if ($executableTypeValue == XdrContractIDPreimageType::CONTRACT_ID_PREIMAGE_FROM_ASSET) {
return new InvokeHostFunctionOperation(DeploySACWithAssetHostFunction::fromXdr($xdrFunction), $auth);
}
break;
}
throw new Exception("invalid argument");
}
Expand Down
20 changes: 0 additions & 20 deletions Soneso/StellarSDK/Responses/Asset/AssetResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ class AssetResponse extends Response
private string $pagingToken;
private AssetAccountsResponse $accounts;
private AssetBalancesResponse $balances;
private string $amount;
private string $claimableBalancesAmount;
private string $liquidityPoolsAmount;
private int $numAccounts;
private int $numClaimableBalances;
private int $numLiquidityPools;
private ?int $numContracts = null;
Expand Down Expand Up @@ -77,14 +75,6 @@ public function getBalances(): AssetBalancesResponse
return $this->balances;
}

/**
* @return string
*/
public function getAmount(): string
{
return $this->amount;
}

/**
* @return string
*/
Expand All @@ -101,14 +91,6 @@ public function getLiquidityPoolsAmount(): string
return $this->liquidityPoolsAmount;
}

/**
* @return int
*/
public function getNumAccounts(): int
{
return $this->numAccounts;
}

/**
* @return int
*/
Expand Down Expand Up @@ -201,12 +183,10 @@ protected function loadFromJson(array $json) : void {
if (isset($json['paging_token'])) $this->pagingToken = $json['paging_token'];
if (isset($json['accounts'])) $this->accounts = AssetAccountsResponse::fromJson($json['accounts']);
if (isset($json['balances'])) $this->balances = AssetBalancesResponse::fromJson($json['balances']);
if (isset($json['amount'])) $this->amount = $json['amount'];
if (isset($json['claimable_balances_amount'])) $this->claimableBalancesAmount = $json['claimable_balances_amount'];
if (isset($json['liquidity_pools_amount'])) $this->liquidityPoolsAmount = $json['liquidity_pools_amount'];
if (isset($json['contracts_amount'])) $this->contractsAmount = $json['contracts_amount'];
if (isset($json['archived_contracts_amount'])) $this->archivedContractsAmount = $json['archived_contracts_amount'];
if (isset($json['num_accounts'])) $this->numAccounts = $json['num_accounts'];
if (isset($json['num_claimable_balances'])) $this->numClaimableBalances = $json['num_claimable_balances'];
if (isset($json['num_liquidity_pools'])) $this->numLiquidityPools = $json['num_liquidity_pools'];
if (isset($json['num_contracts'])) $this->numContracts = $json['num_contracts'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ public static function fromJson(array $json, int $httpResponseStatusCode) : Subm
{
$txStatus = $json['tx_status'];
$hash = $json['hash'];
$errorResultXdrBase64 = isset($json['error_result_xdr_base64']) ? $json['error_result_xdr_base64'] : null;
$errorResultXdrBase64 = null;
if (isset($json['errorResultXdr'])) {
$errorResultXdrBase64 = $json['errorResultXdr']; // protocol < 22
} else if (isset($json['error_result_xdr'])) {
$errorResultXdrBase64 = $json['error_result_xdr']; // protocol 22
}
return new SubmitAsyncTransactionResponse(
txStatus: $txStatus,
hash: $hash,
Expand Down
12 changes: 1 addition & 11 deletions Soneso/StellarSDK/Responses/Transaction/TransactionResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ class TransactionResponse extends Response
private string $resultMetaXdrBase64;
private ?XdrTransactionMeta $resultMetaXdr = null;
private ?string $feeMetaXdrBase64 = null; // todo resolve
private ?array $feeMetaXdr = null; //
private ?string $validAfter = null; // [XdrLedgerEntryChange]
private ?array $feeMetaXdr = null;
private TransactionSignaturesResponse $signatures;
private ?FeeBumpTransactionResponse $feeBumpTransactionResponse = null;
private ?InnerTransactionResponse $innerTransactionResponse = null;
Expand Down Expand Up @@ -225,14 +224,6 @@ public function getFeeMetaXdr(): ?array
return $this->feeMetaXdr;
}

/**
* @return string|null
*/
public function getValidAfter(): ?string
{
return $this->validAfter;
}

/**
* @return TransactionSignaturesResponse
*/
Expand Down Expand Up @@ -338,7 +329,6 @@ protected function loadFromJson(array $json) : void {
array_push($this->feeMetaXdr, XdrLedgerEntryChange::decode($xdrBuffer));
}
}
if (isset($json['valid_after'])) $this->validAfter = $json['valid_after'];

if (isset($json['memo_type'])) {
$memoTypeStr = $json['memo_type'];
Expand Down
Loading

0 comments on commit 65fa4b8

Please sign in to comment.