diff --git a/README.md b/README.md index 28bb449..c660e37 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # [Stellar SDK for PHP](https://github.com/Soneso/stellar-php-sdk) -![v1.4.2](https://img.shields.io/badge/v1.4.2-green.svg) +![v1.5.0](https://img.shields.io/badge/v1.5.0-green.svg) The Soneso open source Stellar SDK for PHP provides APIs to build and sign transactions, connect and query [Horizon](https://github.com/stellar/horizon). diff --git a/Soneso/StellarSDK/Soroban/Responses/GetHealthResponse.php b/Soneso/StellarSDK/Soroban/Responses/GetHealthResponse.php index 2cda89e..33f359b 100644 --- a/Soneso/StellarSDK/Soroban/Responses/GetHealthResponse.php +++ b/Soneso/StellarSDK/Soroban/Responses/GetHealthResponse.php @@ -14,11 +14,17 @@ class GetHealthResponse extends SorobanRpcResponse const HEALTHY = "healthy"; public ?string $status = null; + public ?int $ledgerRetentionWindow = null; public static function fromJson(array $json) : GetHealthResponse { $result = new GetHealthResponse($json); if (isset($json['result'])) { - $result->status = $json['result']['status']; + if (isset($json['result']['status'])) { + $result->status = $json['result']['status']; + } + if (isset($json['result']['ledgerRetentionWindow'])) { + $result->ledgerRetentionWindow = $json['result']['ledgerRetentionWindow']; + } } else if (isset($json['error'])) { $result->error = SorobanRpcErrorResponse::fromJson($json); } diff --git a/Soneso/StellarSDK/Soroban/Responses/LedgerEntryChange.php b/Soneso/StellarSDK/Soroban/Responses/LedgerEntryChange.php new file mode 100644 index 0000000..2696503 --- /dev/null +++ b/Soneso/StellarSDK/Soroban/Responses/LedgerEntryChange.php @@ -0,0 +1,153 @@ +type = $type; + $this->key = $key; + $this->before = $before; + $this->after = $after; + } + + public static function fromJson(array $json) : LedgerEntryChange { + $before = null; + if(isset($json['before'])) { + $before = $json['before']; + } + + $after = null; + if(isset($json['after'])) { + $after = $json['after']; + } + return new LedgerEntryChange( + type: $json['type'], + key: $json['key'], + before: $before, + after: $after, + ); + } + + /** + * @return string + */ + public function getType(): string + { + return $this->type; + } + + /** + * @param string $type + */ + public function setType(string $type): void + { + $this->type = $type; + } + + /** + * @return string + */ + public function getKey(): string + { + return $this->key; + } + + /** + * @param string $key + */ + public function setKey(string $key): void + { + $this->key = $key; + } + + /** + * @return string|null + */ + public function getBefore(): ?string + { + return $this->before; + } + + /** + * @param string|null $before + */ + public function setBefore(?string $before): void + { + $this->before = $before; + } + + /** + * @return string|null + */ + public function getAfter(): ?string + { + return $this->after; + } + + /** + * @param string|null $after + */ + public function setAfter(?string $after): void + { + $this->after = $after; + } + + public function getKeyXdr() : XdrLedgerKey { + return XdrLedgerKey::fromBase64Xdr($this->key); + } + + public function getAfterXdr() : ?XdrLedgerEntry { + if($this->after !== null) { + return XdrLedgerEntry::fromBase64Xdr($this->after); + } + return null; + } + + public function getBeforeXdr() : ?XdrLedgerEntry { + if($this->before !== null) { + return XdrLedgerEntry::fromBase64Xdr($this->before); + } + return null; + } + +} \ No newline at end of file diff --git a/Soneso/StellarSDK/Soroban/Responses/SimulateTransactionResponse.php b/Soneso/StellarSDK/Soroban/Responses/SimulateTransactionResponse.php index ee8b8d9..e037cd2 100644 --- a/Soneso/StellarSDK/Soroban/Responses/SimulateTransactionResponse.php +++ b/Soneso/StellarSDK/Soroban/Responses/SimulateTransactionResponse.php @@ -41,7 +41,12 @@ class SimulateTransactionResponse extends SorobanRpcResponse /// the simulation detected expired ledger entries which requires restoring with the submission of a RestoreFootprint /// operation before submitting the InvokeHostFunction operation. The restorePreamble.minResourceFee and restorePreamble.transactionData fields should /// be used to construct the transaction containing the RestoreFootprint - public ?RestorePreamble $restorePreamble; + public ?RestorePreamble $restorePreamble = null; + + /** + * @var array|null $stateChanges If present, it indicates how the state (ledger entries) will change as a result of the transaction execution. + */ + public ?array $stateChanges = null; public static function fromJson(array $json) : SimulateTransactionResponse { $result = new SimulateTransactionResponse($json); @@ -78,6 +83,14 @@ public static function fromJson(array $json) : SimulateTransactionResponse { if (isset($json['result']['restorePreamble'])) { $result->restorePreamble = RestorePreamble::fromJson($json['result']['restorePreamble']); } + + if (isset($json['result']['stateChanges'])) { + $result->stateChanges = array(); + foreach ($json['result']['stateChanges'] as $jsonValue) { + $result->stateChanges[] = LedgerEntryChange::fromJson($jsonValue); + } + } + } else if (isset($json['error'])) { $result->error = SorobanRpcErrorResponse::fromJson($json); } diff --git a/Soneso/StellarSDK/StellarSDK.php b/Soneso/StellarSDK/StellarSDK.php index 9660714..bd11bcd 100644 --- a/Soneso/StellarSDK/StellarSDK.php +++ b/Soneso/StellarSDK/StellarSDK.php @@ -46,7 +46,7 @@ class StellarSDK { - public const VERSION_NR = "1.4.2"; + public const VERSION_NR = "1.5.0"; public static string $PUBLIC_NET_HORIZON_URL = "https://horizon.stellar.org"; public static string $TEST_NET_HORIZON_URL = "https://horizon-testnet.stellar.org"; public static string $FUTURE_NET_HORIZON_URL = "https://horizon-futurenet.stellar.org"; diff --git a/Soneso/StellarSDK/Xdr/XdrContractCodeCostInputs.php b/Soneso/StellarSDK/Xdr/XdrContractCodeCostInputs.php new file mode 100644 index 0000000..2398c65 --- /dev/null +++ b/Soneso/StellarSDK/Xdr/XdrContractCodeCostInputs.php @@ -0,0 +1,283 @@ +ext = $ext; + $this->nInstructions = $nInstructions; + $this->nFunctions = $nFunctions; + $this->nGlobals = $nGlobals; + $this->nTableEntries = $nTableEntries; + $this->nTypes = $nTypes; + $this->nDataSegments = $nDataSegments; + $this->nElemSegments = $nElemSegments; + $this->nImports = $nImports; + $this->nExports = $nExports; + $this->nDataSegmentBytes = $nDataSegmentBytes; + } + + public function encode(): string { + $bytes = $this->ext->encode(); + $bytes .= XdrEncoder::unsignedInteger32($this->nInstructions); + $bytes .= XdrEncoder::unsignedInteger32($this->nFunctions); + $bytes .= XdrEncoder::unsignedInteger32($this->nGlobals); + $bytes .= XdrEncoder::unsignedInteger32($this->nTableEntries); + $bytes .= XdrEncoder::unsignedInteger32($this->nTypes); + $bytes .= XdrEncoder::unsignedInteger32($this->nDataSegments); + $bytes .= XdrEncoder::unsignedInteger32($this->nElemSegments); + $bytes .= XdrEncoder::unsignedInteger32($this->nImports); + $bytes .= XdrEncoder::unsignedInteger32($this->nExports); + $bytes .= XdrEncoder::unsignedInteger32($this->nDataSegmentBytes); + return $bytes; + } + + public static function decode(XdrBuffer $xdr) : XdrContractCodeCostInputs { + $ext = XdrExtensionPoint::decode($xdr); + $nInstructions = $xdr->readUnsignedInteger32(); + $nFunctions = $xdr->readUnsignedInteger32(); + $nGlobals = $xdr->readUnsignedInteger32(); + $nTableEntries = $xdr->readUnsignedInteger32(); + $nTypes = $xdr->readUnsignedInteger32(); + $nDataSegments = $xdr->readUnsignedInteger32(); + $nElemSegments = $xdr->readUnsignedInteger32(); + $nImports = $xdr->readUnsignedInteger32(); + $nExports = $xdr->readUnsignedInteger32(); + $nDataSegmentBytes = $xdr->readUnsignedInteger32(); + + return new XdrContractCodeCostInputs( + $ext, + $nInstructions, + $nFunctions, + $nGlobals, + $nTableEntries, + $nTypes, + $nDataSegments, + $nElemSegments, + $nImports, + $nExports, + $nDataSegmentBytes, + ); + } + + /** + * @return XdrExtensionPoint + */ + public function getExt(): XdrExtensionPoint + { + return $this->ext; + } + + /** + * @param XdrExtensionPoint $ext + */ + public function setExt(XdrExtensionPoint $ext): void + { + $this->ext = $ext; + } + + /** + * @return int + */ + public function getNInstructions(): int + { + return $this->nInstructions; + } + + /** + * @param int $nInstructions + */ + public function setNInstructions(int $nInstructions): void + { + $this->nInstructions = $nInstructions; + } + + /** + * @return int + */ + public function getNFunctions(): int + { + return $this->nFunctions; + } + + /** + * @param int $nFunctions + */ + public function setNFunctions(int $nFunctions): void + { + $this->nFunctions = $nFunctions; + } + + /** + * @return int + */ + public function getNGlobals(): int + { + return $this->nGlobals; + } + + /** + * @param int $nGlobals + */ + public function setNGlobals(int $nGlobals): void + { + $this->nGlobals = $nGlobals; + } + + /** + * @return int + */ + public function getNTableEntries(): int + { + return $this->nTableEntries; + } + + /** + * @param int $nTableEntries + */ + public function setNTableEntries(int $nTableEntries): void + { + $this->nTableEntries = $nTableEntries; + } + + /** + * @return int + */ + public function getNTypes(): int + { + return $this->nTypes; + } + + /** + * @param int $nTypes + */ + public function setNTypes(int $nTypes): void + { + $this->nTypes = $nTypes; + } + + /** + * @return int + */ + public function getNDataSegments(): int + { + return $this->nDataSegments; + } + + /** + * @param int $nDataSegments + */ + public function setNDataSegments(int $nDataSegments): void + { + $this->nDataSegments = $nDataSegments; + } + + /** + * @return int + */ + public function getNElemSegments(): int + { + return $this->nElemSegments; + } + + /** + * @param int $nElemSegments + */ + public function setNElemSegments(int $nElemSegments): void + { + $this->nElemSegments = $nElemSegments; + } + + /** + * @return int + */ + public function getNImports(): int + { + return $this->nImports; + } + + /** + * @param int $nImports + */ + public function setNImports(int $nImports): void + { + $this->nImports = $nImports; + } + + /** + * @return int + */ + public function getNExports(): int + { + return $this->nExports; + } + + /** + * @param int $nExports + */ + public function setNExports(int $nExports): void + { + $this->nExports = $nExports; + } + + /** + * @return int + */ + public function getNDataSegmentBytes(): int + { + return $this->nDataSegmentBytes; + } + + /** + * @param int $nDataSegmentBytes + */ + public function setNDataSegmentBytes(int $nDataSegmentBytes): void + { + $this->nDataSegmentBytes = $nDataSegmentBytes; + } + +} \ No newline at end of file diff --git a/Soneso/StellarSDK/Xdr/XdrContractCodeEntry.php b/Soneso/StellarSDK/Xdr/XdrContractCodeEntry.php index b6bf74a..c03f4a8 100644 --- a/Soneso/StellarSDK/Xdr/XdrContractCodeEntry.php +++ b/Soneso/StellarSDK/Xdr/XdrContractCodeEntry.php @@ -8,16 +8,16 @@ class XdrContractCodeEntry { - public XdrExtensionPoint $ext; + public XdrContractCodeEntryExt $ext; public string $cHash; // hash public XdrDataValueMandatory $code; /** - * @param XdrExtensionPoint $ext + * @param XdrContractCodeEntryExt $ext * @param string $cHash * @param XdrDataValueMandatory $code */ - public function __construct(XdrExtensionPoint $ext, string $cHash, XdrDataValueMandatory $code) + public function __construct(XdrContractCodeEntryExt $ext, string $cHash, XdrDataValueMandatory $code) { $this->ext = $ext; $this->cHash = $cHash; @@ -33,7 +33,7 @@ public function encode(): string { } public static function decode(XdrBuffer $xdr) : XdrContractCodeEntry { - $ext = XdrExtensionPoint::decode($xdr); + $ext = XdrContractCodeEntryExt::decode($xdr); $cHash = $xdr->readOpaqueFixed(32); $code= XdrDataValueMandatory::decode($xdr); @@ -41,17 +41,17 @@ public static function decode(XdrBuffer $xdr) : XdrContractCodeEntry { } /** - * @return XdrExtensionPoint + * @return XdrContractCodeEntryExt */ - public function getExt(): XdrExtensionPoint + public function getExt(): XdrContractCodeEntryExt { return $this->ext; } /** - * @param XdrExtensionPoint $ext + * @param XdrContractCodeEntryExt $ext */ - public function setExt(XdrExtensionPoint $ext): void + public function setExt(XdrContractCodeEntryExt $ext): void { $this->ext = $ext; } diff --git a/Soneso/StellarSDK/Xdr/XdrContractCodeEntryExt.php b/Soneso/StellarSDK/Xdr/XdrContractCodeEntryExt.php new file mode 100644 index 0000000..98aee57 --- /dev/null +++ b/Soneso/StellarSDK/Xdr/XdrContractCodeEntryExt.php @@ -0,0 +1,82 @@ +discriminant = $discriminant; + $this->v1 = $v1; + } + + public function encode() : string { + $bytes = XdrEncoder::integer32($this->discriminant); + switch ($this->discriminant) { + case 0: + break; + case 1: + $bytes .= $this->v1->encode(); + break; + } + return $bytes; + } + + public static function decode(XdrBuffer $xdr) : XdrContractCodeEntryExt { + $v = $xdr->readInteger32(); + $v1 = null; + switch ($v) { + case 0: + break; + case 1: + $v1 = XdrContractCodeEntryExtV1::decode($xdr); + break; + + } + return new XdrContractCodeEntryExt($v,$v1); + } + + /** + * @return int + */ + public function getDiscriminant(): int + { + return $this->discriminant; + } + + /** + * @param int $discriminant + */ + public function setDiscriminant(int $discriminant): void + { + $this->discriminant = $discriminant; + } + + /** + * @return XdrContractCodeEntryExtV1|null + */ + public function getV1(): ?XdrContractCodeEntryExtV1 + { + return $this->v1; + } + + /** + * @param XdrContractCodeEntryExtV1|null $v1 + */ + public function setV1(?XdrContractCodeEntryExtV1 $v1): void + { + $this->v1 = $v1; + } + +} \ No newline at end of file diff --git a/Soneso/StellarSDK/Xdr/XdrContractCodeEntryExtV1.php b/Soneso/StellarSDK/Xdr/XdrContractCodeEntryExtV1.php new file mode 100644 index 0000000..3ad026f --- /dev/null +++ b/Soneso/StellarSDK/Xdr/XdrContractCodeEntryExtV1.php @@ -0,0 +1,72 @@ +ext = $ext; + $this->costInputs = $costInputs; + } + + + public function encode(): string { + $bytes = $this->ext->encode(); + $bytes .= $this->costInputs->encode(); + return $bytes; + } + + public static function decode(XdrBuffer $xdr) : XdrContractCodeEntryExtV1 { + $ext = XdrExtensionPoint::decode($xdr); + $costInputs = XdrContractCodeCostInputs::decode($xdr); + + return new XdrContractCodeEntryExtV1( + $ext, + $costInputs, + ); + } + + /** + * @return XdrExtensionPoint + */ + public function getExt(): XdrExtensionPoint + { + return $this->ext; + } + + /** + * @param XdrExtensionPoint $ext + */ + public function setExt(XdrExtensionPoint $ext): void + { + $this->ext = $ext; + } + + /** + * @return XdrContractCodeCostInputs + */ + public function getCostInputs(): XdrContractCodeCostInputs + { + return $this->costInputs; + } + + /** + * @param XdrContractCodeCostInputs $costInputs + */ + public function setCostInputs(XdrContractCodeCostInputs $costInputs): void + { + $this->costInputs = $costInputs; + } +} \ No newline at end of file diff --git a/Soneso/StellarSDK/Xdr/XdrContractCostType.php b/Soneso/StellarSDK/Xdr/XdrContractCostType.php index de132a5..916d53b 100644 --- a/Soneso/StellarSDK/Xdr/XdrContractCostType.php +++ b/Soneso/StellarSDK/Xdr/XdrContractCostType.php @@ -25,7 +25,7 @@ class XdrContractCostType const VmCachedInstantiation = 12; // Cost of instantiation a VM from a cached state. const InvokeVMFunction = 13; // Cost of invoking a function on the VM. If the function is a host function, additional cost will be covered by `DispatchHostFunction`. const ComputeKeccak256Hash = 14; // Cost of computing a keccak256 hash from bytes. - const ComputeEcdsaSecp256k1Sig = 15; // Cost of computing an ECDSA secp256k1 signature from bytes. + const DecodeEcdsaCurve256Sig = 15; // Cost of decoding an ECDSA signature computed from a 256-bit prime modulus curve (e.g. secp256k1 and secp256r1). const RecoverEcdsaSecp256k1Key = 16; // Cost of recovering an ECDSA secp256k1 key from a signature. const Int256AddSub = 17; // Cost of int256 addition (`+`) and subtraction (`-`) operations const Int256Mul = 18; // Cost of int256 multiplication (`*`) operation @@ -33,6 +33,28 @@ class XdrContractCostType const Int256Pow = 20; // Cost of int256 power (`exp`) operation const Int256Shift = 21; // Cost of int256 shift (`shl`, `shr`) operation const ChaCha20DrawBytes = 22; // Cost of drawing random bytes using a ChaCha20 PRNG + const ParseWasmInstructions = 23; // Cost of parsing wasm bytes that only encode instructions. + const ParseWasmFunctions = 24; // Cost of parsing a known number of wasm functions. + const ParseWasmGlobals = 25; // Cost of parsing a known number of wasm globals. + const ParseWasmTableEntries = 26; // Cost of parsing a known number of wasm table entries + const ParseWasmTypes = 27; // Cost of parsing a known number of wasm types. + const ParseWasmDataSegments = 28; // Cost of parsing a known number of wasm data segments. + const ParseWasmElemSegments = 29; // Cost of parsing a known number of wasm element segments. + const ParseWasmImports = 30; // Cost of parsing a known number of wasm imports. + const ParseWasmExports = 31; // Cost of parsing a known number of wasm exports. + const ParseWasmDataSegmentBytes = 32; // Cost of parsing a known number of data segment bytes. + const InstantiateWasmInstructions = 33; // Cost of instantiating wasm bytes that only encode instructions. + const InstantiateWasmFunctions = 34; // Cost of instantiating a known number of wasm functions. + const InstantiateWasmGlobals = 35; // Cost of instantiating a known number of wasm globals. + const InstantiateWasmTableEntries = 36; // Cost of instantiating a known number of wasm table entries. + const InstantiateWasmTypes = 37; // Cost of instantiating a known number of wasm types. + const InstantiateWasmDataSegments = 38; // Cost of instantiating a known number of wasm data segments. + const InstantiateWasmElemSegments = 39; // Cost of instantiating a known number of wasm element segments. + const InstantiateWasmImports = 40; // Cost of instantiating a known number of wasm imports. + const InstantiateWasmExports = 41; // Cost of instantiating a known number of wasm exports. + const InstantiateWasmDataSegmentBytes = 42; // Cost of instantiating a known number of data segment bytes. + const Sec1DecodePointUncompressed = 43; // Cost of decoding a bytes array representing an uncompressed SEC-1 encoded point on a 256-bit elliptic curve + const VerifyEcdsaSecp256r1Sig = 44; // Cost of verifying an ECDSA Secp256r1 signature public function __construct(int $value) { diff --git a/Soneso/StellarSDK/Xdr/XdrLedgerEntry.php b/Soneso/StellarSDK/Xdr/XdrLedgerEntry.php index 2202858..9233a36 100644 --- a/Soneso/StellarSDK/Xdr/XdrLedgerEntry.php +++ b/Soneso/StellarSDK/Xdr/XdrLedgerEntry.php @@ -39,6 +39,12 @@ public static function decode(XdrBuffer $xdr): XdrLedgerEntry { return new XdrLedgerEntry($lastModifiedLedgerSeq, $data, $ext); } + public static function fromBase64Xdr(String $base64Xdr) : XdrLedgerEntry { + $xdr = base64_decode($base64Xdr); + $xdrBuffer = new XdrBuffer($xdr); + return XdrLedgerEntry::decode($xdrBuffer); + } + /** * @return int */ diff --git a/Soneso/StellarSDK/Xdr/XdrSCAddress.php b/Soneso/StellarSDK/Xdr/XdrSCAddress.php index 76099ab..1ac95a6 100644 --- a/Soneso/StellarSDK/Xdr/XdrSCAddress.php +++ b/Soneso/StellarSDK/Xdr/XdrSCAddress.php @@ -6,6 +6,8 @@ namespace Soneso\StellarSDK\Xdr; +use Soneso\StellarSDK\Crypto\StrKey; + class XdrSCAddress { @@ -29,7 +31,11 @@ public function encode(): string { $bytes .= $this->accountId->encode(); break; case XdrSCAddressType::SC_ADDRESS_TYPE_CONTRACT: - $bytes .= XdrEncoder::opaqueFixed(hex2bin($this->contractId),32); + $contractIdHex = $this->contractId; + if (substr($contractIdHex, 0, 1 ) === 'C') { + $contractIdHex = StrKey::decodeContractIdHex($contractIdHex); + } + $bytes .= XdrEncoder::opaqueFixed(hex2bin($contractIdHex),32); break; } return $bytes; diff --git a/Soneso/StellarSDK/Xdr/XdrSorobanTransactionMeta.php b/Soneso/StellarSDK/Xdr/XdrSorobanTransactionMeta.php index 7f819b3..6b658e4 100644 --- a/Soneso/StellarSDK/Xdr/XdrSorobanTransactionMeta.php +++ b/Soneso/StellarSDK/Xdr/XdrSorobanTransactionMeta.php @@ -9,18 +9,32 @@ class XdrSorobanTransactionMeta { - public XdrExtensionPoint $ext; - public array $events; // [XdrContractEvent] + public XdrSorobanTransactionMetaExt $ext; + /** + * @var array $events + */ + public array $events; public XdrSCVal $returnValue; - public array $diagnosticEvents; // [XdrDiagnosticEvent] /** - * @param XdrExtensionPoint $ext - * @param array $events + * @var array $diagnosticEvents + */ + public array $diagnosticEvents; + + /** + * Constructor. + * + * @param XdrSorobanTransactionMetaExt $ext + * @param array $events * @param XdrSCVal $returnValue - * @param array $diagnosticEvents + * @param array $diagnosticEvents */ - public function __construct(XdrExtensionPoint $ext, array $events, XdrSCVal $returnValue, array $diagnosticEvents) + public function __construct( + XdrSorobanTransactionMetaExt $ext, + array $events, + XdrSCVal $returnValue, + array $diagnosticEvents, + ) { $this->ext = $ext; $this->events = $events; @@ -44,7 +58,7 @@ public function encode(): string { } public static function decode(XdrBuffer $xdr) : XdrSorobanTransactionMeta { - $ext = XdrExtensionPoint::decode($xdr); + $ext = XdrSorobanTransactionMetaExt::decode($xdr); $valCount = $xdr->readInteger32(); $eventsArr = array(); for ($i = 0; $i < $valCount; $i++) { @@ -60,23 +74,23 @@ public static function decode(XdrBuffer $xdr) : XdrSorobanTransactionMeta { } /** - * @return XdrExtensionPoint + * @return XdrSorobanTransactionMetaExt */ - public function getExt(): XdrExtensionPoint + public function getExt(): XdrSorobanTransactionMetaExt { return $this->ext; } /** - * @param XdrExtensionPoint $ext + * @param XdrSorobanTransactionMetaExt $ext */ - public function setExt(XdrExtensionPoint $ext): void + public function setExt(XdrSorobanTransactionMetaExt $ext): void { $this->ext = $ext; } /** - * @return array + * @return array */ public function getEvents(): array { @@ -84,7 +98,7 @@ public function getEvents(): array } /** - * @param array $events + * @param array $events */ public function setEvents(array $events): void { @@ -108,7 +122,7 @@ public function setReturnValue(XdrSCVal $returnValue): void } /** - * @return array + * @return array */ public function getDiagnosticEvents(): array { @@ -116,7 +130,7 @@ public function getDiagnosticEvents(): array } /** - * @param array $diagnosticEvents + * @param array $diagnosticEvents */ public function setDiagnosticEvents(array $diagnosticEvents): void { diff --git a/Soneso/StellarSDK/Xdr/XdrSorobanTransactionMetaExt.php b/Soneso/StellarSDK/Xdr/XdrSorobanTransactionMetaExt.php new file mode 100644 index 0000000..33172dd --- /dev/null +++ b/Soneso/StellarSDK/Xdr/XdrSorobanTransactionMetaExt.php @@ -0,0 +1,70 @@ +discriminant = $discriminant; + $this->v1 = $v1; + } + + public function encode() : string { + $bytes = XdrEncoder::integer32($this->discriminant); + switch ($this->discriminant) { + case 0: + break; + case 1: + $bytes .= $this->v1->encode(); + break; + } + return $bytes; + } + + public static function decode(XdrBuffer $xdr) : XdrSorobanTransactionMetaExt { + $v = $xdr->readInteger32(); + $v1 = null; + switch ($v) { + case 0: + break; + case 1: + $v1 = XdrSorobanTransactionMetaExtV1::decode($xdr); + break; + + } + return new XdrSorobanTransactionMetaExt($v,$v1); + } + + public function getDiscriminant(): int + { + return $this->discriminant; + } + + public function setDiscriminant(int $discriminant): void + { + $this->discriminant = $discriminant; + } + + public function getV1(): ?XdrSorobanTransactionMetaExtV1 + { + return $this->v1; + } + + public function setV1(?XdrSorobanTransactionMetaExtV1 $v1): void + { + $this->v1 = $v1; + } +} \ No newline at end of file diff --git a/Soneso/StellarSDK/Xdr/XdrSorobanTransactionMetaExtV1.php b/Soneso/StellarSDK/Xdr/XdrSorobanTransactionMetaExtV1.php new file mode 100644 index 0000000..4c45dbd --- /dev/null +++ b/Soneso/StellarSDK/Xdr/XdrSorobanTransactionMetaExtV1.php @@ -0,0 +1,124 @@ +ext = $ext; + $this->totalNonRefundableResourceFeeCharged = $totalNonRefundableResourceFeeCharged; + $this->totalRefundableResourceFeeCharged = $totalRefundableResourceFeeCharged; + $this->rentFeeCharged = $rentFeeCharged; + } + + public function encode(): string { + $bytes = $this->ext->encode(); + $bytes .= XdrEncoder::unsignedInteger64($this->totalNonRefundableResourceFeeCharged); + $bytes .= XdrEncoder::unsignedInteger64($this->totalRefundableResourceFeeCharged); + $bytes .= XdrEncoder::unsignedInteger64($this->rentFeeCharged); + return $bytes; + } + + public static function decode(XdrBuffer $xdr) : XdrSorobanTransactionMetaExtV1 { + $ext = XdrExtensionPoint::decode($xdr); + $totalNonRefundableResourceFeeCharged = $xdr->readUnsignedInteger64(); + $totalRefundableResourceFeeCharged = $xdr->readUnsignedInteger64(); + $rentFeeCharged = $xdr->readUnsignedInteger64(); + + return new XdrSorobanTransactionMetaExtV1( + $ext, + $totalNonRefundableResourceFeeCharged, + $totalRefundableResourceFeeCharged, + $rentFeeCharged, + ); + } + + public function getExt(): XdrExtensionPoint + { + return $this->ext; + } + + public function setExt(XdrExtensionPoint $ext): void + { + $this->ext = $ext; + } + + public function getTotalNonRefundableResourceFeeCharged(): int + { + return $this->totalNonRefundableResourceFeeCharged; + } + + public function setTotalNonRefundableResourceFeeCharged(int $totalNonRefundableResourceFeeCharged): void + { + $this->totalNonRefundableResourceFeeCharged = $totalNonRefundableResourceFeeCharged; + } + + public function getTotalRefundableResourceFeeCharged(): int + { + return $this->totalRefundableResourceFeeCharged; + } + + public function setTotalRefundableResourceFeeCharged(int $totalRefundableResourceFeeCharged): void + { + $this->totalRefundableResourceFeeCharged = $totalRefundableResourceFeeCharged; + } + + public function getRentFeeCharged(): int + { + return $this->rentFeeCharged; + } + + public function setRentFeeCharged(int $rentFeeCharged): void + { + $this->rentFeeCharged = $rentFeeCharged; + } +} \ No newline at end of file diff --git a/Soneso/StellarSDK/Xdr/XdrStateArchivalSettings.php b/Soneso/StellarSDK/Xdr/XdrStateArchivalSettings.php index f6f843c..f297b44 100644 --- a/Soneso/StellarSDK/Xdr/XdrStateArchivalSettings.php +++ b/Soneso/StellarSDK/Xdr/XdrStateArchivalSettings.php @@ -15,7 +15,8 @@ class XdrStateArchivalSettings public int $tempRentRateDenominator; // int64 public int $maxEntriesToArchive; // uint32 public int $bucketListSizeWindowSampleSize; // uint32 - public int $evictionScanSize; // uint64 + public int $bucketListWindowSamplePeriod; // uint32 + public int $evictionScanSize; // uint32 public int $startingEvictionScanLevel; // uint32 /** @@ -26,14 +27,22 @@ class XdrStateArchivalSettings * @param int $tempRentRateDenominator * @param int $maxEntriesToArchive * @param int $bucketListSizeWindowSampleSize + * @param int $bucketListWindowSamplePeriod * @param int $evictionScanSize * @param int $startingEvictionScanLevel */ - public function __construct(int $maxEntryTTL, int $minTemporaryTTL, - int $minPersistentTTL, - int $persistentRentRateDenominator, int $tempRentRateDenominator, - int $maxEntriesToArchive, int $bucketListSizeWindowSampleSize, - int $evictionScanSize, int $startingEvictionScanLevel) + public function __construct( + int $maxEntryTTL, + int $minTemporaryTTL, + int $minPersistentTTL, + int $persistentRentRateDenominator, + int $tempRentRateDenominator, + int $maxEntriesToArchive, + int $bucketListSizeWindowSampleSize, + int $bucketListWindowSamplePeriod, + int $evictionScanSize, + int $startingEvictionScanLevel, + ) { $this->maxEntryTTL = $maxEntryTTL; $this->minTemporaryTTL = $minTemporaryTTL; @@ -42,6 +51,7 @@ public function __construct(int $maxEntryTTL, int $minTemporaryTTL, $this->tempRentRateDenominator = $tempRentRateDenominator; $this->maxEntriesToArchive = $maxEntriesToArchive; $this->bucketListSizeWindowSampleSize = $bucketListSizeWindowSampleSize; + $this->bucketListWindowSamplePeriod = $bucketListWindowSamplePeriod; $this->evictionScanSize = $evictionScanSize; $this->startingEvictionScanLevel = $startingEvictionScanLevel; } @@ -55,7 +65,8 @@ public function encode(): string { $body .= XdrEncoder::integer64($this->tempRentRateDenominator); $body .= XdrEncoder::unsignedInteger32($this->maxEntriesToArchive); $body .= XdrEncoder::unsignedInteger32($this->bucketListSizeWindowSampleSize); - $body .= XdrEncoder::unsignedInteger64($this->evictionScanSize); + $body .= XdrEncoder::unsignedInteger32($this->bucketListWindowSamplePeriod); + $body .= XdrEncoder::unsignedInteger32($this->evictionScanSize); $body .= XdrEncoder::unsignedInteger32($this->startingEvictionScanLevel); return $body; } @@ -68,12 +79,21 @@ public static function decode(XdrBuffer $xdr) : XdrStateArchivalSettings { $tempRentRateDenominator = $xdr->readInteger64(); $maxEntriesToArchive = $xdr->readUnsignedInteger32(); $bucketListSizeWindowSampleSize = $xdr->readUnsignedInteger32(); - $evictionScanSize = $xdr->readUnsignedInteger64(); + $bucketListWindowSamplePeriod = $xdr->readUnsignedInteger32(); + $evictionScanSize = $xdr->readUnsignedInteger32(); $startingEvictionScanLevel = $xdr->readUnsignedInteger32(); - return new XdrStateArchivalSettings($maxEntryTTL, $minTemporaryTTL, - $minPersistentTTL, $persistentRentRateDenominator, - $tempRentRateDenominator, $maxEntriesToArchive,$bucketListSizeWindowSampleSize, - $evictionScanSize, $startingEvictionScanLevel); + return new XdrStateArchivalSettings( + $maxEntryTTL, + $minTemporaryTTL, + $minPersistentTTL, + $persistentRentRateDenominator, + $tempRentRateDenominator, + $maxEntriesToArchive, + $bucketListSizeWindowSampleSize, + $bucketListWindowSamplePeriod, + $evictionScanSize, + $startingEvictionScanLevel, + ); } /** @@ -204,6 +224,22 @@ public function setBucketListSizeWindowSampleSize(int $bucketListSizeWindowSampl $this->bucketListSizeWindowSampleSize = $bucketListSizeWindowSampleSize; } + /** + * @return int + */ + public function getBucketListWindowSamplePeriod(): int + { + return $this->bucketListWindowSamplePeriod; + } + + /** + * @param int $bucketListWindowSamplePeriod + */ + public function setBucketListWindowSamplePeriod(int $bucketListWindowSamplePeriod): void + { + $this->bucketListWindowSamplePeriod = $bucketListWindowSamplePeriod; + } + /** * @return int */ diff --git a/Soneso/StellarSDK/Xdr/XdrTransactionMetaV3.php b/Soneso/StellarSDK/Xdr/XdrTransactionMetaV3.php index ddf8582..95d1041 100644 --- a/Soneso/StellarSDK/Xdr/XdrTransactionMetaV3.php +++ b/Soneso/StellarSDK/Xdr/XdrTransactionMetaV3.php @@ -10,19 +10,36 @@ class XdrTransactionMetaV3 { public XdrExtensionPoint $ext; - public array $txChangesBefore; // [XdrLedgerEntryChange] - public array $operations; // [XdrOperationMeta] - public array $txChangesAfter; // [XdrLedgerEntryChange] + /** + * @var array $txChangesBefore + */ + public array $txChangesBefore; + + /** + * @var array $operations + */ + public array $operations; + + /** + * @var array $txChangesAfter + */ + public array $txChangesAfter; public ?XdrSorobanTransactionMeta $sorobanMeta = null; /** * @param XdrExtensionPoint $ext - * @param array $txChangesBefore - * @param array $operations - * @param array $txChangesAfter + * @param array $txChangesBefore + * @param array $operations + * @param array $txChangesAfter * @param XdrSorobanTransactionMeta|null $sorobanMeta */ - public function __construct(XdrExtensionPoint $ext, array $txChangesBefore, array $operations, array $txChangesAfter, ?XdrSorobanTransactionMeta $sorobanMeta) + public function __construct( + XdrExtensionPoint $ext, + array $txChangesBefore, + array $operations, + array $txChangesAfter, + ?XdrSorobanTransactionMeta $sorobanMeta, + ) { $this->ext = $ext; $this->txChangesBefore = $txChangesBefore; @@ -66,19 +83,29 @@ public function encode(): string { public static function decode(XdrBuffer $xdr): XdrTransactionMetaV3 { $ext = XdrExtensionPoint::decode($xdr); $valCount = $xdr->readInteger32(); + /** + * @var array $txChangesBefore + */ $txChangesBefore = array(); for ($i = 0; $i < $valCount; $i++) { - array_push($txChangesBefore, XdrLedgerEntryChange::decode($xdr)); + $txChangesBefore[] = XdrLedgerEntryChange::decode($xdr); } $valCount = $xdr->readInteger32(); + /** + * @var array $operations + */ $operations = array(); for ($i = 0; $i < $valCount; $i++) { - array_push($operations, XdrOperationMeta::decode($xdr)); + $operations[] = XdrOperationMeta::decode($xdr); } $valCount = $xdr->readInteger32(); + + /** + * @var array $txChangesAfter + */ $txChangesAfter = array(); for ($i = 0; $i < $valCount; $i++) { - array_push($txChangesAfter, XdrLedgerEntryChange::decode($xdr)); + $txChangesAfter[] = XdrLedgerEntryChange::decode($xdr); } $sorobanMeta = null; @@ -106,7 +133,7 @@ public function setExt(XdrExtensionPoint $ext): void } /** - * @return array + * @return array */ public function getTxChangesBefore(): array { @@ -114,7 +141,7 @@ public function getTxChangesBefore(): array } /** - * @param array $txChangesBefore + * @param array $txChangesBefore */ public function setTxChangesBefore(array $txChangesBefore): void { @@ -122,7 +149,7 @@ public function setTxChangesBefore(array $txChangesBefore): void } /** - * @return array + * @return array */ public function getOperations(): array { @@ -130,7 +157,7 @@ public function getOperations(): array } /** - * @param array $operations + * @param array $operations */ public function setOperations(array $operations): void { @@ -138,7 +165,7 @@ public function setOperations(array $operations): void } /** - * @return array + * @return array */ public function getTxChangesAfter(): array { @@ -146,7 +173,7 @@ public function getTxChangesAfter(): array } /** - * @param array $txChangesAfter + * @param array $txChangesAfter */ public function setTxChangesAfter(array $txChangesAfter): void { diff --git a/Soneso/StellarSDKTests/SorobanAtomicSwapTest.php b/Soneso/StellarSDKTests/SorobanAtomicSwapTest.php index 7c2a78d..03a54dc 100644 --- a/Soneso/StellarSDKTests/SorobanAtomicSwapTest.php +++ b/Soneso/StellarSDKTests/SorobanAtomicSwapTest.php @@ -26,6 +26,7 @@ use Soneso\StellarSDK\TransactionBuilder; use Soneso\StellarSDK\UploadContractWasmHostFunction; use Soneso\StellarSDK\Util\FriendBot; +use Soneso\StellarSDK\Util\FuturenetFriendBot; use Soneso\StellarSDK\Xdr\XdrExtensionPoint; use Soneso\StellarSDK\Xdr\XdrInt128Parts; use Soneso\StellarSDK\Xdr\XdrLedgerEntryType; @@ -45,12 +46,34 @@ class SorobanAtomicSwapTest extends TestCase const SWAP_CONTRACT_PATH = './wasm/soroban_atomic_swap_contract.wasm'; const TOKEN_CONTRACT_PATH = './wasm/soroban_token_contract.wasm'; - public function testAtomicSwap() { + const TESTNET_SERVER_URL = "https://soroban-testnet.stellar.org"; + const FUTURENET_SERVER_URL = "https://rpc-futurenet.stellar.org"; - $server = new SorobanServer("https://soroban-testnet.stellar.org"); - $server->enableLogging = true; + private string $testOn = 'testnet'; // 'futurenet' + private Network $network; + private SorobanServer $server; + private StellarSDK $sdk; + + public function setUp(): void + { + // Turn on error reporting + error_reporting(E_ALL); + if ($this->testOn === 'testnet') { + $this->network = Network::testnet(); + $this->server = new SorobanServer(self::TESTNET_SERVER_URL); + $this->server->enableLogging = true; + $this->sdk = StellarSDK::getTestNetInstance(); + } elseif ($this->testOn === 'futurenet') { + $this->network = Network::futurenet(); + $this->server = new SorobanServer(self::FUTURENET_SERVER_URL); + $this->server->enableLogging = true; + $this->sdk = StellarSDK::getFutureNetInstance(); + } + sleep(5); + } + + public function testAtomicSwap() { - $sdk = StellarSDK::getTestNetInstance(); $adminKeyPair = KeyPair::random(); $adminId = $adminKeyPair->getAccountId(); @@ -59,33 +82,40 @@ public function testAtomicSwap() { $bobKeyPair = KeyPair::random(); $bobId = $bobKeyPair->getAccountId(); - FriendBot::fundTestAccount($adminId); - FriendBot::fundTestAccount($aliceId); - FriendBot::fundTestAccount($bobId); + if ($this->testOn === 'testnet') { + FriendBot::fundTestAccount($adminId); + FriendBot::fundTestAccount($aliceId); + FriendBot::fundTestAccount($bobId); + } elseif ($this->testOn === 'futurenet') { + FuturenetFriendBot::fundTestAccount($adminId); + FuturenetFriendBot::fundTestAccount($aliceId); + FuturenetFriendBot::fundTestAccount($bobId); + } + sleep(5); print("admin: " . $adminKeyPair->getSecretSeed() . " : " . $adminKeyPair->getAccountId(). PHP_EOL); print("alice: " . $aliceKeyPair->getSecretSeed() . " : " . $aliceKeyPair->getAccountId(). PHP_EOL); print("bob: " . $bobKeyPair->getSecretSeed() . " : " . $bobKeyPair->getAccountId(). PHP_EOL); - $atomicSwapContractId = $this->deployContract($server,self::SWAP_CONTRACT_PATH, $adminKeyPair); + $atomicSwapContractId = $this->deployContract($this->server,self::SWAP_CONTRACT_PATH, $adminKeyPair); print("atomic swap cid: " . $atomicSwapContractId . PHP_EOL); - $tokenAContractId = $this->deployContract($server,self::TOKEN_CONTRACT_PATH, $adminKeyPair); + $tokenAContractId = $this->deployContract($this->server,self::TOKEN_CONTRACT_PATH, $adminKeyPair); print("token a cid: " . StrKey::encodeContractIdHex($tokenAContractId) . PHP_EOL); - $tokenBContractId = $this->deployContract($server,self::TOKEN_CONTRACT_PATH, $adminKeyPair); + $tokenBContractId = $this->deployContract($this->server,self::TOKEN_CONTRACT_PATH, $adminKeyPair); print("token b cid: " . StrKey::encodeContractIdHex($tokenBContractId) . PHP_EOL); - $this->createToken($server, $adminKeyPair, $tokenAContractId, "TokenA", "TokenA"); - $this->createToken($server, $adminKeyPair, $tokenBContractId, "TokenB", "TokenB"); + $this->createToken($this->server, $adminKeyPair, $tokenAContractId, "TokenA", "TokenA"); + $this->createToken($this->server, $adminKeyPair, $tokenBContractId, "TokenB", "TokenB"); - $this->mint($server, $adminKeyPair, $tokenAContractId, $aliceId, 10000000000000); - $this->mint($server, $adminKeyPair, $tokenBContractId, $bobId, 10000000000000); + $this->mint($this->server, $adminKeyPair, $tokenAContractId, $aliceId, 10000000000000); + $this->mint($this->server, $adminKeyPair, $tokenBContractId, $bobId, 10000000000000); - $aliceTokenABalance = $this->balance($server, $adminKeyPair, $tokenAContractId, $aliceId); + $aliceTokenABalance = $this->balance($this->server, $adminKeyPair, $tokenAContractId, $aliceId); $this->assertEquals(10000000000000, $aliceTokenABalance); - $bobTokenBBalance = $this->balance($server, $adminKeyPair, $tokenBContractId, $bobId); + $bobTokenBBalance = $this->balance($this->server, $adminKeyPair, $tokenBContractId, $bobId); $this->assertEquals(10000000000000, $bobTokenBBalance); @@ -115,11 +145,11 @@ public function testAtomicSwap() { $builder = new InvokeHostFunctionOperationBuilder($invokeContractHostFunction); $op = $builder->build(); - $source = $sdk->requestAccount($adminId); + $source = $this->sdk->requestAccount($adminId); $transaction = (new TransactionBuilder($source))->addOperation($op)->build(); $request = new SimulateTransactionRequest($transaction); - $simulateResponse = $server->simulateTransaction($request); + $simulateResponse = $this->server->simulateTransaction($request); $transactionData = $simulateResponse->getTransactionData(); @@ -130,7 +160,7 @@ public function testAtomicSwap() { $auth = $simulateResponse->getSorobanAuth(); $this->assertNotNull($auth); - $latestLedgerResponse = $server->getLatestLedger(); + $latestLedgerResponse = $this->server->getLatestLedger(); $this->assertNotNull($latestLedgerResponse->sequence); foreach ($auth as $a) { if ($a instanceof SorobanAuthorizationEntry) { @@ -139,10 +169,10 @@ public function testAtomicSwap() { $a->credentials->addressCredentials->signatureExpirationLedger = $latestLedgerResponse->sequence + 10; // sign if ($a->credentials->addressCredentials->address->accountId == $aliceId) { - $a->sign($aliceKeyPair, Network::testnet()); + $a->sign($aliceKeyPair, $this->network); } if ($a->credentials->addressCredentials->address->accountId == $bobId) { - $a->sign($bobKeyPair, Network::testnet()); + $a->sign($bobKeyPair, $this->network); } } else { self::fail("invalid auth"); @@ -151,19 +181,19 @@ public function testAtomicSwap() { $transaction->setSorobanAuth($auth); // sign transaction - $transaction->sign($adminKeyPair, Network::testnet()); + $transaction->sign($adminKeyPair, $this->network); // check transaction xdr encoding back and forth $transctionEnvelopeXdr = $transaction->toEnvelopeXdrBase64(); $this->assertEquals($transctionEnvelopeXdr, Transaction::fromEnvelopeBase64XdrString($transctionEnvelopeXdr)->toEnvelopeXdrBase64()); // send the transaction - $sendResponse = $server->sendTransaction($transaction); + $sendResponse = $this->server->sendTransaction($transaction); $this->assertNull($sendResponse->error); $this->assertNotEquals(SendTransactionResponse::STATUS_ERROR, $sendResponse->status); // poll until status is success or error - $statusResponse = $this->pollStatus($server, $sendResponse->hash); + $statusResponse = $this->pollStatus($this->server, $sendResponse->hash); $this->assertNotNull($statusResponse); $this->assertEquals(GetTransactionResponse::STATUS_SUCCESS, $statusResponse->status); $this->assertNotNull($statusResponse->getResultValue()); @@ -173,7 +203,6 @@ public function testAtomicSwap() { private function deployContract(SorobanServer $server, String $pathToCode, KeyPair $submitterKp) : String { sleep(5); - $sdk = StellarSDK::getTestNetInstance(); $this->restoreContractFootprint($server, $submitterKp, $pathToCode); @@ -186,7 +215,7 @@ private function deployContract(SorobanServer $server, String $pathToCode, KeyPa sleep(5); $submitterId = $submitterKp->getAccountId(); - $account = $sdk->requestAccount($submitterId); + $account = $this->sdk->requestAccount($submitterId); $transaction = (new TransactionBuilder($account))->addOperation($op)->build(); // simulate first to get the transaction data and resource fee @@ -197,7 +226,7 @@ private function deployContract(SorobanServer $server, String $pathToCode, KeyPa // set the transaction data + fee and sign $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->minResourceFee); - $transaction->sign($submitterKp, Network::testnet()); + $transaction->sign($submitterKp, $this->network); // send the transaction $sendResponse = $server->sendTransaction($transaction); @@ -217,7 +246,7 @@ private function deployContract(SorobanServer $server, String $pathToCode, KeyPa $op = $builder->build(); sleep(5); - $account = $sdk->requestAccount($submitterId); + $account = $this->sdk->requestAccount($submitterId); $transaction = (new TransactionBuilder($account)) ->addOperation($op)->build(); @@ -229,7 +258,7 @@ private function deployContract(SorobanServer $server, String $pathToCode, KeyPa $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->minResourceFee); $transaction->setSorobanAuth($simulateResponse->getSorobanAuth()); - $transaction->sign($submitterKp, Network::testnet()); + $transaction->sign($submitterKp, $this->network); // send the transaction $sendResponse = $server->sendTransaction($transaction); @@ -246,7 +275,6 @@ private function deployContract(SorobanServer $server, String $pathToCode, KeyPa private function createToken(SorobanServer $server, Keypair $submitterKp, String $contractId, String $name, String $symbol) : void { // see https://soroban.stellar.org/docs/reference/interfaces/token-interface - $sdk = StellarSDK::getTestNetInstance(); $submitterId = $submitterKp->getAccountId(); $adminAddress = Address::fromAccountId($submitterId)->toXdrSCVal(); @@ -263,7 +291,7 @@ private function createToken(SorobanServer $server, Keypair $submitterKp, String sleep(5); // reload account for sequence number - $account = $sdk->requestAccount($submitterId); + $account = $this->sdk->requestAccount($submitterId); $transaction = (new TransactionBuilder($account)) ->addOperation($op)->build(); @@ -274,7 +302,7 @@ private function createToken(SorobanServer $server, Keypair $submitterKp, String $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->minResourceFee); $transaction->setSorobanAuth($simulateResponse->getSorobanAuth()); - $transaction->sign($submitterKp, Network::testnet()); + $transaction->sign($submitterKp, $this->network); // send the transaction $sendResponse = $server->sendTransaction($transaction); @@ -290,7 +318,6 @@ private function mint(SorobanServer $server, Keypair $submitterKp, String $contr sleep(5); // see https://soroban.stellar.org/docs/reference/interfaces/token-interface - $sdk = StellarSDK::getTestNetInstance(); $submitterId = $submitterKp->getAccountId(); @@ -306,7 +333,7 @@ private function mint(SorobanServer $server, Keypair $submitterKp, String $contr $op = $builder->build(); // reload account for sequence number - $account = $sdk->requestAccount($submitterId); + $account = $this->sdk->requestAccount($submitterId); $transaction = (new TransactionBuilder($account))->addOperation($op)->build(); $request = new SimulateTransactionRequest($transaction); @@ -315,7 +342,7 @@ private function mint(SorobanServer $server, Keypair $submitterKp, String $contr $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->minResourceFee); $transaction->setSorobanAuth($simulateResponse->getSorobanAuth()); - $transaction->sign($submitterKp, Network::testnet()); + $transaction->sign($submitterKp, $this->network); // send the transaction @@ -332,7 +359,6 @@ private function balance(SorobanServer $server, Keypair $submitterKp, String $co sleep(5); // see https://soroban.stellar.org/docs/reference/interfaces/token-interface - $sdk = StellarSDK::getTestNetInstance(); $submitterId = $submitterKp->getAccountId(); $address = Address::fromAccountId($accountId)->toXdrSCVal(); @@ -344,7 +370,7 @@ private function balance(SorobanServer $server, Keypair $submitterKp, String $co $builder = new InvokeHostFunctionOperationBuilder($invokeContractHostFunction); $op = $builder->build(); - $account = $sdk->requestAccount($submitterId); + $account = $this->sdk->requestAccount($submitterId); $transaction = (new TransactionBuilder($account)) ->addOperation($op)->build(); @@ -353,7 +379,7 @@ private function balance(SorobanServer $server, Keypair $submitterKp, String $co // set the transaction data + fee and sign $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->minResourceFee); - $transaction->sign($submitterKp, Network::testnet()); + $transaction->sign($submitterKp, $this->network); // send the transaction $sendResponse = $server->sendTransaction($transaction); @@ -392,14 +418,13 @@ private function pollStatus(SorobanServer $server, string $transactionId) : ?Get private function restoreContractFootprint(SorobanServer $server, KeyPair $accountKeyPair, string $contractCodePath) : void { sleep(5); - $sdk = StellarSDK::getTestNetInstance(); $contractCode = file_get_contents($contractCodePath, false); $uploadContractHostFunction = new UploadContractWasmHostFunction($contractCode); $op = (new InvokeHostFunctionOperationBuilder($uploadContractHostFunction))->build(); $accountAId = $accountKeyPair->getAccountId(); - $getAccountResponse = $sdk->requestAccount($accountAId); + $getAccountResponse = $this->sdk->requestAccount($accountAId); $transaction = (new TransactionBuilder($getAccountResponse)) ->addOperation($op)->build(); @@ -414,7 +439,7 @@ private function restoreContractFootprint(SorobanServer $server, KeyPair $accoun $transactionData->resources->footprint->readWrite = $transactionData->resources->footprint->readWrite + $transactionData->resources->footprint->readOnly; $transactionData->resources->footprint->readOnly = array(); - $getAccountResponse = $sdk->requestAccount($accountAId); + $getAccountResponse = $this->sdk->requestAccount($accountAId); $restoreOp = (new RestoreFootprintOperationBuilder())->build(); $transaction = (new TransactionBuilder($getAccountResponse)) ->addOperation($restoreOp)->build(); @@ -431,7 +456,7 @@ private function restoreContractFootprint(SorobanServer $server, KeyPair $accoun // set the transaction data + fee and sign $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->getMinResourceFee()); - $transaction->sign($accountKeyPair, Network::testnet()); + $transaction->sign($accountKeyPair, $this->network); // check transaction xdr encoding back and forth $transctionEnvelopeXdr = $transaction->toEnvelopeXdrBase64(); @@ -453,13 +478,12 @@ private function restoreContractFootprint(SorobanServer $server, KeyPair $accoun private function bumpContractCodeFootprint(SorobanServer $server, KeyPair $accountKeyPair, string $wasmId, int $extendTo) : void { sleep(5); - $sdk = StellarSDK::getTestNetInstance(); $builder = new ExtendFootprintTTLOperationBuilder($extendTo); $bumpOp = $builder->build(); $accountAId = $accountKeyPair->getAccountId(); - $getAccountResponse = $sdk->requestAccount($accountAId); + $getAccountResponse = $this->sdk->requestAccount($accountAId); $transaction = (new TransactionBuilder($getAccountResponse)) ->addOperation($bumpOp)->build(); @@ -485,7 +509,7 @@ private function bumpContractCodeFootprint(SorobanServer $server, KeyPair $accou // set the transaction data + fee and sign $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->getMinResourceFee()); - $transaction->sign($accountKeyPair, Network::testnet()); + $transaction->sign($accountKeyPair, $this->network); // check transaction xdr encoding back and forth $transctionEnvelopeXdr = $transaction->toEnvelopeXdrBase64(); diff --git a/Soneso/StellarSDKTests/SorobanAuthTest.php b/Soneso/StellarSDKTests/SorobanAuthTest.php index 7cc1aff..a6755d3 100644 --- a/Soneso/StellarSDKTests/SorobanAuthTest.php +++ b/Soneso/StellarSDKTests/SorobanAuthTest.php @@ -28,6 +28,7 @@ use Soneso\StellarSDK\TransactionBuilder; use Soneso\StellarSDK\UploadContractWasmHostFunction; use Soneso\StellarSDK\Util\FriendBot; +use Soneso\StellarSDK\Util\FuturenetFriendBot; use Soneso\StellarSDK\Xdr\XdrExtensionPoint; use Soneso\StellarSDK\Xdr\XdrLedgerEntryType; use Soneso\StellarSDK\Xdr\XdrLedgerFootprint; @@ -44,27 +45,50 @@ class SorobanAuthTest extends TestCase { const AUTH_CONTRACT_PATH = './wasm/soroban_auth_contract.wasm'; - const SERVER_URL = "https://soroban-testnet.stellar.org"; + const TESTNET_SERVER_URL = "https://soroban-testnet.stellar.org"; + const FUTURENET_SERVER_URL = "https://rpc-futurenet.stellar.org"; + + private string $testOn = 'testnet'; // 'futurenet' + private Network $network; + private SorobanServer $server; + private StellarSDK $sdk; + + public function setUp(): void + { + // Turn on error reporting + error_reporting(E_ALL); + if ($this->testOn === 'testnet') { + $this->network = Network::testnet(); + $this->server = new SorobanServer(self::TESTNET_SERVER_URL); + $this->server->enableLogging = true; + $this->sdk = StellarSDK::getTestNetInstance(); + } elseif ($this->testOn === 'futurenet') { + $this->network = Network::futurenet(); + $this->server = new SorobanServer(self::FUTURENET_SERVER_URL); + $this->server->enableLogging = true; + $this->sdk = StellarSDK::getFutureNetInstance(); + } + } public function testAuthInvoker(): void { // submitter and invoker use are the same // no need to sign auth - $server = new SorobanServer(self::SERVER_URL); - $server->enableLogging = true; - $sdk = StellarSDK::getTestNetInstance(); - $invokerKeyPair = KeyPair::random(); $invokerId = $invokerKeyPair->getAccountId(); // get health - $getHealthResponse = $server->getHealth(); + $getHealthResponse = $this->server->getHealth(); $this->assertEquals(GetHealthResponse::HEALTHY, $getHealthResponse->status); - FriendBot::fundTestAccount($invokerId); + if ($this->testOn == 'testnet') { + FriendBot::fundTestAccount($invokerId); + } elseif($this->testOn == 'futurenet') { + FuturenetFriendBot::fundTestAccount($invokerId); + } - $contractId = $this->deployContract($server, self::AUTH_CONTRACT_PATH, $invokerKeyPair); + $contractId = $this->deployContract($this->server, self::AUTH_CONTRACT_PATH, $invokerKeyPair); // submitter and invoker use are the same // no need to sign auth @@ -80,12 +104,12 @@ public function testAuthInvoker(): void $op = $builder->build(); sleep(5); - $invokerAccount = $sdk->requestAccount($invokerId); + $invokerAccount = $this->sdk->requestAccount($invokerId); $transaction = (new TransactionBuilder($invokerAccount)) ->addOperation($op)->build(); $request = new SimulateTransactionRequest($transaction); - $simulateResponse = $server->simulateTransaction($request); + $simulateResponse = $this->server->simulateTransaction($request); $this->assertNull($simulateResponse->error); $this->assertNull($simulateResponse->resultError); @@ -101,19 +125,19 @@ public function testAuthInvoker(): void $transaction->addResourceFee($simulateResponse->minResourceFee); $transaction->setSorobanAuth($simulateResponse->getSorobanAuth()); // sign auth - $transaction->sign($invokerKeyPair, Network::testnet()); + $transaction->sign($invokerKeyPair, $this->network); // check transaction xdr encoding back and forth $transctionEnvelopeXdr = $transaction->toEnvelopeXdrBase64(); $this->assertEquals($transctionEnvelopeXdr, Transaction::fromEnvelopeBase64XdrString($transctionEnvelopeXdr)->toEnvelopeXdrBase64()); // send the transaction - $sendResponse = $server->sendTransaction($transaction); + $sendResponse = $this->server->sendTransaction($transaction); $this->assertNull($sendResponse->error); $this->assertNotEquals(SendTransactionResponse::STATUS_ERROR, $sendResponse->status); // poll until status is success or error - $statusResponse = $this->pollStatus($server, $sendResponse->hash); + $statusResponse = $this->pollStatus($this->server, $sendResponse->hash); $this->assertNotNull($statusResponse); $this->assertNotNull($statusResponse->getResultValue()); @@ -128,7 +152,7 @@ public function testAuthInvoker(): void sleep(3); // check horizon response decoding. - $transactionResponse = $sdk->requestTransaction($sendResponse->hash); + $transactionResponse = $this->sdk->requestTransaction($sendResponse->hash); $this->assertEquals(1, $transactionResponse->getOperationCount()); $this->assertEquals($transctionEnvelopeXdr, $transactionResponse->getEnvelopeXdr()->toBase64Xdr()); $meta = $transactionResponse->getResultMetaXdrBase64(); @@ -149,19 +173,21 @@ public function testAuthDifferentSubmitter(): void // submitter and invoker use are NOT the same // we need to sign auth - $server = new SorobanServer(self::SERVER_URL); - $server->enableLogging = true; - $sdk = StellarSDK::getTestNetInstance(); - $submitterKeyPair = KeyPair::random(); $submitterId = $submitterKeyPair->getAccountId(); $invokerKeyPair = KeyPair::random(); $invokerId = $invokerKeyPair->getAccountId(); - FriendBot::fundTestAccount($submitterId); - FriendBot::fundTestAccount($invokerId); + if ($this->testOn == 'testnet') { + FriendBot::fundTestAccount($submitterId); + FriendBot::fundTestAccount($invokerId); + } elseif($this->testOn == 'futurenet') { + FuturenetFriendBot::fundTestAccount($submitterId); + FuturenetFriendBot::fundTestAccount($invokerId); + } + sleep(5); - $contractId = $this->deployContract($server, self::AUTH_CONTRACT_PATH, $submitterKeyPair); + $contractId = $this->deployContract($this->server, self::AUTH_CONTRACT_PATH, $submitterKeyPair); // invoke contract // submitter and invoker use are NOT the same @@ -178,12 +204,12 @@ public function testAuthDifferentSubmitter(): void $builder = new InvokeHostFunctionOperationBuilder($invokeHostFunction); $op = $builder->build(); - $submitterAccount = $sdk->requestAccount($submitterId); + $submitterAccount = $this->sdk->requestAccount($submitterId); $transaction = (new TransactionBuilder($submitterAccount)) ->addOperation($op)->build(); $request = new SimulateTransactionRequest($transaction); - $simulateResponse = $server->simulateTransaction($request); + $simulateResponse = $this->server->simulateTransaction($request); $this->assertNull($simulateResponse->error); $this->assertNull($simulateResponse->resultError); @@ -203,7 +229,7 @@ public function testAuthDifferentSubmitter(): void $auth = $simulateResponse->getSorobanAuth(); $this->assertNotNull($auth); - $latestLedgerResponse = $server->getLatestLedger(); + $latestLedgerResponse = $this->server->getLatestLedger(); $this->assertNotNull($latestLedgerResponse->sequence); foreach ($auth as $a) { if ($a instanceof SorobanAuthorizationEntry) { @@ -211,25 +237,25 @@ public function testAuthDifferentSubmitter(): void // increase signature expiration ledger $a->credentials->addressCredentials->signatureExpirationLedger = $latestLedgerResponse->sequence + 10; // sign - $a->sign($invokerKeyPair, Network::testnet()); + $a->sign($invokerKeyPair, $this->network); } else { self::fail("invalid auth"); } } $transaction->setSorobanAuth($auth); - $transaction->sign($submitterKeyPair, Network::testnet()); + $transaction->sign($submitterKeyPair, $this->network); // check transaction xdr encoding back and forth $transctionEnvelopeXdr = $transaction->toEnvelopeXdrBase64(); $this->assertEquals($transctionEnvelopeXdr, Transaction::fromEnvelopeBase64XdrString($transctionEnvelopeXdr)->toEnvelopeXdrBase64()); // send the transaction - $sendResponse = $server->sendTransaction($transaction); + $sendResponse = $this->server->sendTransaction($transaction); $this->assertNull($sendResponse->error); $this->assertNotEquals(SendTransactionResponse::STATUS_ERROR, $sendResponse->status); // poll until status is success or error - $statusResponse = $this->pollStatus($server, $sendResponse->hash); + $statusResponse = $this->pollStatus($this->server, $sendResponse->hash); $this->assertNotNull($statusResponse); $this->assertNotNull($statusResponse->getResultValue()); @@ -244,7 +270,7 @@ public function testAuthDifferentSubmitter(): void sleep(5); // check horizon response decoding. - $transactionResponse = $sdk->requestTransaction($sendResponse->hash); + $transactionResponse = $this->sdk->requestTransaction($sendResponse->hash); $this->assertEquals(1, $transactionResponse->getOperationCount()); $this->assertEquals($transctionEnvelopeXdr, $transactionResponse->getEnvelopeXdr()->toBase64Xdr()); $meta = $transactionResponse->getResultMetaXdrBase64(); @@ -276,14 +302,13 @@ private function pollStatus(SorobanServer $server, string $transactionId) : ?Get private function restoreContractFootprint(SorobanServer $server, KeyPair $accountKeyPair, string $contractCodePath) : void { sleep(5); - $sdk = StellarSDK::getTestNetInstance(); $contractCode = file_get_contents($contractCodePath, false); $uploadContractHostFunction = new UploadContractWasmHostFunction($contractCode); $op = (new InvokeHostFunctionOperationBuilder($uploadContractHostFunction))->build(); $accountAId = $accountKeyPair->getAccountId(); - $getAccountResponse = $sdk->requestAccount($accountAId); + $getAccountResponse = $this->sdk->requestAccount($accountAId); $transaction = (new TransactionBuilder($getAccountResponse)) ->addOperation($op)->build(); @@ -298,7 +323,7 @@ private function restoreContractFootprint(SorobanServer $server, KeyPair $accoun $transactionData->resources->footprint->readWrite = $transactionData->resources->footprint->readWrite + $transactionData->resources->footprint->readOnly; $transactionData->resources->footprint->readOnly = array(); - $getAccountResponse = $sdk->requestAccount($accountAId); + $getAccountResponse = $this->sdk->requestAccount($accountAId); $restoreOp = (new RestoreFootprintOperationBuilder())->build(); $transaction = (new TransactionBuilder($getAccountResponse)) ->addOperation($restoreOp)->build(); @@ -315,7 +340,7 @@ private function restoreContractFootprint(SorobanServer $server, KeyPair $accoun // set the transaction data + fee and sign $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->getMinResourceFee()); - $transaction->sign($accountKeyPair, Network::testnet()); + $transaction->sign($accountKeyPair, $this->network); // check transaction xdr encoding back and forth $transctionEnvelopeXdr = $transaction->toEnvelopeXdrBase64(); @@ -337,13 +362,12 @@ private function restoreContractFootprint(SorobanServer $server, KeyPair $accoun private function bumpContractCodeFootprint(SorobanServer $server, KeyPair $accountKeyPair, string $wasmId, int $extendTo) : void { sleep(5); - $sdk = StellarSDK::getTestNetInstance(); $builder = new ExtendFootprintTTLOperationBuilder($extendTo); $bumpOp = $builder->build(); $accountAId = $accountKeyPair->getAccountId(); - $getAccountResponse = $sdk->requestAccount($accountAId); + $getAccountResponse = $this->sdk->requestAccount($accountAId); $transaction = (new TransactionBuilder($getAccountResponse)) ->addOperation($bumpOp)->build(); @@ -369,7 +393,7 @@ private function bumpContractCodeFootprint(SorobanServer $server, KeyPair $accou // set the transaction data + fee and sign $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->getMinResourceFee()); - $transaction->sign($accountKeyPair, Network::testnet()); + $transaction->sign($accountKeyPair, $this->network); // check transaction xdr encoding back and forth $transctionEnvelopeXdr = $transaction->toEnvelopeXdrBase64(); @@ -391,7 +415,6 @@ private function bumpContractCodeFootprint(SorobanServer $server, KeyPair $accou private function deployContract(SorobanServer $server, String $pathToCode, KeyPair $submitterKp) : String { sleep(5); - $sdk = StellarSDK::getTestNetInstance(); $this->restoreContractFootprint($server, $submitterKp, $pathToCode); @@ -404,7 +427,7 @@ private function deployContract(SorobanServer $server, String $pathToCode, KeyPa sleep(5); $submitterId = $submitterKp->getAccountId(); - $account = $sdk->requestAccount($submitterId); + $account = $this->sdk->requestAccount($submitterId); $transaction = (new TransactionBuilder($account))->addOperation($op)->build(); // simulate first to get the transaction data and resource fee @@ -415,7 +438,7 @@ private function deployContract(SorobanServer $server, String $pathToCode, KeyPa // set the transaction data + fee and sign $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->minResourceFee); - $transaction->sign($submitterKp, Network::testnet()); + $transaction->sign($submitterKp, $this->network); // send the transaction $sendResponse = $server->sendTransaction($transaction); @@ -435,7 +458,7 @@ private function deployContract(SorobanServer $server, String $pathToCode, KeyPa $op = $builder->build(); sleep(5); - $account = $sdk->requestAccount($submitterId); + $account = $this->sdk->requestAccount($submitterId); $transaction = (new TransactionBuilder($account)) ->addOperation($op)->build(); @@ -447,7 +470,7 @@ private function deployContract(SorobanServer $server, String $pathToCode, KeyPa $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->minResourceFee); $transaction->setSorobanAuth($simulateResponse->getSorobanAuth()); - $transaction->sign($submitterKp, Network::testnet()); + $transaction->sign($submitterKp, $this->network); // send the transaction $sendResponse = $server->sendTransaction($transaction); diff --git a/Soneso/StellarSDKTests/SorobanCustomAccountTest.php b/Soneso/StellarSDKTests/SorobanCustomAccountTest.php index d655960..33a389c 100644 --- a/Soneso/StellarSDKTests/SorobanCustomAccountTest.php +++ b/Soneso/StellarSDKTests/SorobanCustomAccountTest.php @@ -25,6 +25,7 @@ use Soneso\StellarSDK\TransactionBuilder; use Soneso\StellarSDK\UploadContractWasmHostFunction; use Soneso\StellarSDK\Util\FriendBot; +use Soneso\StellarSDK\Util\FuturenetFriendBot; use Soneso\StellarSDK\Xdr\XdrContractDataDurability; use Soneso\StellarSDK\Xdr\XdrExtensionPoint; use Soneso\StellarSDK\Xdr\XdrInt128Parts; @@ -43,16 +44,36 @@ class SorobanCustomAccountTest extends TestCase const CUSTOM_ACCOUNT_CONTRACT_PATH = './wasm/soroban_custom_account.wasm'; const TOKEN_CONTRACT_PATH = './wasm/soroban_token_contract.wasm'; - const SERVER_URL = "https://soroban-testnet.stellar.org"; + const TESTNET_SERVER_URL = "https://soroban-testnet.stellar.org"; + const FUTURENET_SERVER_URL = "https://rpc-futurenet.stellar.org"; + + private string $testOn = 'testnet'; // 'futurenet' + private Network $network; + private SorobanServer $server; + private StellarSDK $sdk; + + public function setUp(): void + { + // Turn on error reporting + error_reporting(E_ALL); + if ($this->testOn === 'testnet') { + $this->network = Network::testnet(); + $this->server = new SorobanServer(self::TESTNET_SERVER_URL); + $this->server->enableLogging = true; + $this->sdk = StellarSDK::getTestNetInstance(); + } elseif ($this->testOn === 'futurenet') { + $this->network = Network::futurenet(); + $this->server = new SorobanServer(self::FUTURENET_SERVER_URL); + $this->server->enableLogging = true; + $this->sdk = StellarSDK::getFutureNetInstance(); + } + } public function testCustomAccount() { // https://github.com/Soneso/as-soroban-examples/tree/main/custom_account // https://soroban.stellar.org/docs/advanced-tutorials/custom-account // https://soroban.stellar.org/docs/fundamentals-and-concepts/authorization - $server = new SorobanServer(self::SERVER_URL); - $server->enableLogging = true; - $adminKeyPair = KeyPair::random(); $adminId = $adminKeyPair->getAccountId(); //print("ADMIN: " . $adminKeyPair->getSecretSeed() . PHP_EOL); @@ -65,45 +86,51 @@ public function testCustomAccount() { $bobId = $bobKeyPair->getAccountId(); //print("BOB: " . $bobKeyPair->getSecretSeed() . PHP_EOL); - FriendBot::fundTestAccount($adminId); - FriendBot::fundTestAccount($aliceId); - FriendBot::fundTestAccount($bobId); + if ($this->testOn === 'testnet') { + FriendBot::fundTestAccount($adminId); + FriendBot::fundTestAccount($aliceId); + FriendBot::fundTestAccount($bobId); + } elseif ($this->testOn === 'futurenet') { + FuturenetFriendBot::fundTestAccount($adminId); + FuturenetFriendBot::fundTestAccount($aliceId); + FuturenetFriendBot::fundTestAccount($bobId); + } sleep(5); - $deployAccRes = $this->deployContract($server,self::CUSTOM_ACCOUNT_CONTRACT_PATH, $adminKeyPair); + $deployAccRes = $this->deployContract($this->server,self::CUSTOM_ACCOUNT_CONTRACT_PATH, $adminKeyPair); $accountContractWasmId = $deployAccRes[0]; $accountContractId = $deployAccRes[1]; //print("accountContractWasmId : " . $accountContractWasmId . PHP_EOL); //print("accountContractId : " . $accountContractId . PHP_EOL); - $deployTokRes = $this->deployContract($server,self::TOKEN_CONTRACT_PATH, $adminKeyPair); + $deployTokRes = $this->deployContract($this->server,self::TOKEN_CONTRACT_PATH, $adminKeyPair); $tokenAContractId = $deployTokRes[1]; //print("tokenAContractId: " . $tokenAContractId . PHP_EOL); - $this->createToken($server, $adminKeyPair, $tokenAContractId, "TokenA", "TokenA"); + $this->createToken($this->server, $adminKeyPair, $tokenAContractId, "TokenA", "TokenA"); $accountContractAddressXdrSCVal = Address::fromContractId($accountContractId)->toXdrSCVal(); - $this->mint($server, $adminKeyPair, $tokenAContractId, $accountContractAddressXdrSCVal, 10000000000000); + $this->mint($this->server, $adminKeyPair, $tokenAContractId, $accountContractAddressXdrSCVal, 10000000000000); - $accTokenABalance = $this->balance($server, $adminKeyPair, $tokenAContractId, $accountContractAddressXdrSCVal); + $accTokenABalance = $this->balance($this->server, $adminKeyPair, $tokenAContractId, $accountContractAddressXdrSCVal); $this->assertEquals(10000000000000, $accTokenABalance); $alicePk = XdrSCVal::forBytes($aliceKeyPair->getPublicKey()); $bobPk = XdrSCVal::forBytes($bobKeyPair->getPublicKey()); $signers = XdrSCVal::forVec([$bobPk, $alicePk]); - $this->initAccContract($server, $adminKeyPair, $accountContractId, $signers); + $this->initAccContract($this->server, $adminKeyPair, $accountContractId, $signers); - $this->addLimit($server, $adminKeyPair, $accountContractId, $tokenAContractId, 100, $aliceKeyPair, $bobKeyPair); + $this->addLimit($this->server, $adminKeyPair, $accountContractId, $tokenAContractId, 100, $aliceKeyPair, $bobKeyPair); $expectSuccess = true; - $this->transfer($expectSuccess, $server, $adminKeyPair, $accountContractId, $accountContractWasmId, $tokenAContractId, 10, $aliceId, signer1: $aliceKeyPair); + $this->transfer($expectSuccess, $this->server, $adminKeyPair, $accountContractId, $accountContractWasmId, $tokenAContractId, 10, $aliceId, signer1: $aliceKeyPair); $expectSuccess = false; - $this->transfer($expectSuccess, $server, $adminKeyPair, $accountContractId, $accountContractWasmId, $tokenAContractId, 101, $aliceId, signer1: $aliceKeyPair); + $this->transfer($expectSuccess, $this->server, $adminKeyPair, $accountContractId, $accountContractWasmId, $tokenAContractId, 101, $aliceId, signer1: $aliceKeyPair); $expectSuccess = true; - $this->transfer($expectSuccess, $server, $adminKeyPair, $accountContractId, $accountContractWasmId, $tokenAContractId, 101, $aliceId, signer1: $aliceKeyPair, signer2: $bobKeyPair); + $this->transfer($expectSuccess, $this->server, $adminKeyPair, $accountContractId, $accountContractWasmId, $tokenAContractId, 101, $aliceId, signer1: $aliceKeyPair, signer2: $bobKeyPair); } private function initAccContract(SorobanServer $server, Keypair $submitterKp, String $contractId, XdrSCVal $signers) : void @@ -111,9 +138,8 @@ private function initAccContract(SorobanServer $server, Keypair $submitterKp, St sleep(5); // https://soroban.stellar.org/docs/advanced-tutorials/custom-account - $sdk = StellarSDK::getTestNetInstance(); $submitterId = $submitterKp->getAccountId(); - $account = $sdk->requestAccount($submitterId); + $account = $this->sdk->requestAccount($submitterId); $functionName = "init"; @@ -129,7 +155,7 @@ private function initAccContract(SorobanServer $server, Keypair $submitterKp, St $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->minResourceFee); $transaction->setSorobanAuth($simulateResponse->getSorobanAuth()); - $transaction->sign($submitterKp, Network::testnet()); + $transaction->sign($submitterKp, $this->network); // send the transaction $sendResponse = $server->sendTransaction($transaction); @@ -143,9 +169,8 @@ private function initAccContract(SorobanServer $server, Keypair $submitterKp, St private function addLimit(SorobanServer $server, Keypair $submitterKp, String $contractId, String $tokenContractId, int $limit, KeyPair $signer1, KeyPair $signer2) : void { sleep(5); - $sdk = StellarSDK::getTestNetInstance(); $submitterId = $submitterKp->getAccountId(); - $account = $sdk->requestAccount($submitterId); + $account = $this->sdk->requestAccount($submitterId); $functionName = "add_limit"; $token = Address::fromContractId($tokenContractId)->toXdrSCVal(); @@ -186,15 +211,15 @@ private function addLimit(SorobanServer $server, Keypair $submitterKp, String $c // increase signature expiration ledger $a->credentials->addressCredentials->signatureExpirationLedger = $latestLedgerResponse->sequence + 10; // sign - $a->sign($signer1, Network::testnet()); - $a->sign($signer2, Network::testnet()); + $a->sign($signer1, $this->network); + $a->sign($signer2, $this->network); } else { self::fail("invalid auth"); } } $transaction->setSorobanAuth($auth); - $transaction->sign($submitterKp, Network::testnet()); + $transaction->sign($submitterKp, $this->network); // send the transaction $sendResponse = $server->sendTransaction($transaction); @@ -218,9 +243,8 @@ private function transfer(bool $expectSuccess, SorobanServer $server, Keypair $s { sleep(5); // see https://soroban.stellar.org/docs/reference/interfaces/token-interface - $sdk = StellarSDK::getTestNetInstance(); $submitterId = $submitterKp->getAccountId(); - $account = $sdk->requestAccount($submitterId); + $account = $this->sdk->requestAccount($submitterId); $functionName = "transfer"; @@ -285,10 +309,10 @@ private function transfer(bool $expectSuccess, SorobanServer $server, Keypair $s $a->credentials->addressCredentials->signatureExpirationLedger = $latestLedgerResponse->sequence + 10; // sign if ($signer1 != null) { - $a->sign($signer1, Network::testnet()); + $a->sign($signer1, $this->network); } if ($signer2 != null) { - $a->sign($signer2, Network::testnet()); + $a->sign($signer2, $this->network); } } else { self::fail("invalid auth"); @@ -296,7 +320,7 @@ private function transfer(bool $expectSuccess, SorobanServer $server, Keypair $s } $transaction->setSorobanAuth($auth); - $transaction->sign($submitterKp, Network::testnet()); + $transaction->sign($submitterKp, $this->network); // send the transaction $sendResponse = $server->sendTransaction($transaction); @@ -317,7 +341,6 @@ private function transfer(bool $expectSuccess, SorobanServer $server, Keypair $s private function deployContract(SorobanServer $server, String $pathToCode, KeyPair $submitterKp) : array { sleep(5); $result = array(); - $sdk = StellarSDK::getTestNetInstance(); //$this->restoreContractFootprint($server, $submitterKp, $pathToCode); @@ -330,7 +353,7 @@ private function deployContract(SorobanServer $server, String $pathToCode, KeyPa sleep(5); $submitterId = $submitterKp->getAccountId(); - $account = $sdk->requestAccount($submitterId); + $account = $this->sdk->requestAccount($submitterId); $transaction = (new TransactionBuilder($account))->addOperation($op)->build(); // simulate first to get the transaction data and resource fee @@ -341,7 +364,7 @@ private function deployContract(SorobanServer $server, String $pathToCode, KeyPa // set the transaction data + fee and sign $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->minResourceFee); - $transaction->sign($submitterKp, Network::testnet()); + $transaction->sign($submitterKp, $this->network); // send the transaction $sendResponse = $server->sendTransaction($transaction); @@ -362,7 +385,7 @@ private function deployContract(SorobanServer $server, String $pathToCode, KeyPa $op = $builder->build(); sleep(5); - $account = $sdk->requestAccount($submitterId); + $account = $this->sdk->requestAccount($submitterId); $transaction = (new TransactionBuilder($account)) ->addOperation($op)->build(); @@ -374,7 +397,7 @@ private function deployContract(SorobanServer $server, String $pathToCode, KeyPa $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->minResourceFee); $transaction->setSorobanAuth($simulateResponse->getSorobanAuth()); - $transaction->sign($submitterKp, Network::testnet()); + $transaction->sign($submitterKp, $this->network); // send the transaction $sendResponse = $server->sendTransaction($transaction); @@ -392,7 +415,6 @@ private function deployContract(SorobanServer $server, String $pathToCode, KeyPa private function createToken(SorobanServer $server, Keypair $submitterKp, String $contractId, String $name, String $symbol) : void { // see https://soroban.stellar.org/docs/reference/interfaces/token-interface - $sdk = StellarSDK::getTestNetInstance(); $submitterId = $submitterKp->getAccountId(); $adminAddress = Address::fromAccountId($submitterId)->toXdrSCVal(); @@ -409,7 +431,7 @@ private function createToken(SorobanServer $server, Keypair $submitterKp, String sleep(5); // reload account for sequence number - $account = $sdk->requestAccount($submitterId); + $account = $this->sdk->requestAccount($submitterId); $transaction = (new TransactionBuilder($account)) ->addOperation($op)->build(); @@ -420,7 +442,7 @@ private function createToken(SorobanServer $server, Keypair $submitterKp, String $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->minResourceFee); $transaction->setSorobanAuth($simulateResponse->getSorobanAuth()); - $transaction->sign($submitterKp, Network::testnet()); + $transaction->sign($submitterKp, $this->network); // send the transaction $sendResponse = $server->sendTransaction($transaction); @@ -436,7 +458,6 @@ private function mint(SorobanServer $server, Keypair $submitterKp, String $contr sleep(5); // see https://soroban.stellar.org/docs/reference/interfaces/token-interface - $sdk = StellarSDK::getTestNetInstance(); $submitterId = $submitterKp->getAccountId(); $amountValue = XdrSCVal::forI128(new XdrInt128Parts(0, $amount)); @@ -449,7 +470,7 @@ private function mint(SorobanServer $server, Keypair $submitterKp, String $contr $op = $builder->build(); // reload account for sequence number - $account = $sdk->requestAccount($submitterId); + $account = $this->sdk->requestAccount($submitterId); $transaction = (new TransactionBuilder($account))->addOperation($op)->build(); $request = new SimulateTransactionRequest($transaction); @@ -458,7 +479,7 @@ private function mint(SorobanServer $server, Keypair $submitterKp, String $contr $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->minResourceFee); $transaction->setSorobanAuth($simulateResponse->getSorobanAuth()); - $transaction->sign($submitterKp, Network::testnet()); + $transaction->sign($submitterKp, $this->network); // send the transaction @@ -475,7 +496,6 @@ private function balance(SorobanServer $server, Keypair $submitterKp, String $co sleep(5); // see https://soroban.stellar.org/docs/reference/interfaces/token-interface - $sdk = StellarSDK::getTestNetInstance(); $submitterId = $submitterKp->getAccountId(); $functionName = "balance"; @@ -486,7 +506,7 @@ private function balance(SorobanServer $server, Keypair $submitterKp, String $co $builder = new InvokeHostFunctionOperationBuilder($invokeContractHostFunction); $op = $builder->build(); - $account = $sdk->requestAccount($submitterId); + $account = $this->sdk->requestAccount($submitterId); $transaction = (new TransactionBuilder($account)) ->addOperation($op)->build(); @@ -495,7 +515,7 @@ private function balance(SorobanServer $server, Keypair $submitterKp, String $co // set the transaction data + fee and sign $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->minResourceFee); - $transaction->sign($submitterKp, Network::testnet()); + $transaction->sign($submitterKp, $this->network); // send the transaction $sendResponse = $server->sendTransaction($transaction); @@ -535,14 +555,13 @@ private function pollStatus(SorobanServer $server, string $transactionId) : ?Get private function restoreContractFootprint(SorobanServer $server, KeyPair $accountKeyPair, string $contractCodePath) : void { sleep(5); - $sdk = StellarSDK::getTestNetInstance(); $contractCode = file_get_contents($contractCodePath, false); $uploadContractHostFunction = new UploadContractWasmHostFunction($contractCode); $op = (new InvokeHostFunctionOperationBuilder($uploadContractHostFunction))->build(); $accountAId = $accountKeyPair->getAccountId(); - $getAccountResponse = $sdk->requestAccount($accountAId); + $getAccountResponse = $this->sdk->requestAccount($accountAId); $transaction = (new TransactionBuilder($getAccountResponse)) ->addOperation($op)->build(); @@ -557,7 +576,7 @@ private function restoreContractFootprint(SorobanServer $server, KeyPair $accoun $transactionData->resources->footprint->readWrite = $transactionData->resources->footprint->readWrite + $transactionData->resources->footprint->readOnly; $transactionData->resources->footprint->readOnly = array(); - $getAccountResponse = $sdk->requestAccount($accountAId); + $getAccountResponse = $this->sdk->requestAccount($accountAId); $restoreOp = (new RestoreFootprintOperationBuilder())->build(); $transaction = (new TransactionBuilder($getAccountResponse)) ->addOperation($restoreOp)->build(); @@ -574,7 +593,7 @@ private function restoreContractFootprint(SorobanServer $server, KeyPair $accoun // set the transaction data + fee and sign $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->getMinResourceFee()); - $transaction->sign($accountKeyPair, Network::testnet()); + $transaction->sign($accountKeyPair, $this->network); // check transaction xdr encoding back and forth $transctionEnvelopeXdr = $transaction->toEnvelopeXdrBase64(); @@ -596,13 +615,12 @@ private function restoreContractFootprint(SorobanServer $server, KeyPair $accoun private function bumpContractCodeFootprint(SorobanServer $server, KeyPair $accountKeyPair, string $wasmId, int $extendTo) : void { sleep(5); - $sdk = StellarSDK::getTestNetInstance(); $builder = new ExtendFootprintTTLOperationBuilder($extendTo); $bumpOp = $builder->build(); $accountAId = $accountKeyPair->getAccountId(); - $getAccountResponse = $sdk->requestAccount($accountAId); + $getAccountResponse = $this->sdk->requestAccount($accountAId); $transaction = (new TransactionBuilder($getAccountResponse)) ->addOperation($bumpOp)->build(); @@ -628,7 +646,7 @@ private function bumpContractCodeFootprint(SorobanServer $server, KeyPair $accou // set the transaction data + fee and sign $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->getMinResourceFee()); - $transaction->sign($accountKeyPair, Network::testnet()); + $transaction->sign($accountKeyPair, $this->network); // check transaction xdr encoding back and forth $transctionEnvelopeXdr = $transaction->toEnvelopeXdrBase64(); diff --git a/Soneso/StellarSDKTests/SorobanTest.php b/Soneso/StellarSDKTests/SorobanTest.php index 5feb28c..8dcaceb 100644 --- a/Soneso/StellarSDKTests/SorobanTest.php +++ b/Soneso/StellarSDKTests/SorobanTest.php @@ -24,7 +24,6 @@ use Soneso\StellarSDK\Responses\Operations\InvokeHostFunctionOperationResponse; use Soneso\StellarSDK\RestoreFootprintOperationBuilder; use Soneso\StellarSDK\Soroban\Address; -use Soneso\StellarSDK\Soroban\Requests\ResourceConfig; use Soneso\StellarSDK\Soroban\Requests\SimulateTransactionRequest; use Soneso\StellarSDK\Soroban\Requests\TopicFilter; use Soneso\StellarSDK\Soroban\Requests\TopicFilters; @@ -40,6 +39,7 @@ use Soneso\StellarSDK\TransactionBuilder; use Soneso\StellarSDK\UploadContractWasmHostFunction; use Soneso\StellarSDK\Util\FriendBot; +use Soneso\StellarSDK\Util\FuturenetFriendBot; use Soneso\StellarSDK\Xdr\XdrDiagnosticEvent; use Soneso\StellarSDK\Xdr\XdrExtensionPoint; use Soneso\StellarSDK\Xdr\XdrLedgerEntryType; @@ -50,6 +50,7 @@ use Soneso\StellarSDK\Xdr\XdrSorobanResources; use Soneso\StellarSDK\Xdr\XdrSorobanTransactionData; use Soneso\StellarSDK\Xdr\XdrTransactionMeta; +use function PHPUnit\Framework\assertNotNull; class SorobanTest extends TestCase { @@ -57,7 +58,36 @@ class SorobanTest extends TestCase const HELLO_CONTRACT_PATH = './wasm/soroban_hello_world_contract.wasm'; const EVENTS_CONTRACT_PATH = './wasm/soroban_events_contract.wasm'; - const SERVER_URL = "https://soroban-testnet.stellar.org"; + const TESTNET_SERVER_URL = "https://soroban-testnet.stellar.org"; + const FUTURENET_SERVER_URL = "https://rpc-futurenet.stellar.org"; + + private string $testOn = 'testnet'; // 'futurenet' + private Network $network; + private KeyPair $accountAKeyPair; + private string $accountAId; + private SorobanServer $server; + private StellarSDK $sdk; + public function setUp(): void + { + // Turn on error reporting + error_reporting(E_ALL); + $this->accountAKeyPair = KeyPair::random(); + $this->accountAId = $this->accountAKeyPair->getAccountId(); + if ($this->testOn === 'testnet') { + FriendBot::fundTestAccount($this->accountAId); + $this->network = Network::testnet(); + $this->server = new SorobanServer(self::TESTNET_SERVER_URL); + $this->server->enableLogging = true; + $this->sdk = StellarSDK::getTestNetInstance(); + } elseif ($this->testOn === 'futurenet') { + FuturenetFriendBot::fundTestAccount($this->accountAId); + $this->network = Network::futurenet(); + $this->server = new SorobanServer(self::FUTURENET_SERVER_URL); + $this->server->enableLogging = true; + $this->sdk = StellarSDK::getFutureNetInstance(); + } + sleep(5); + } /** * @throws GuzzleException @@ -65,30 +95,25 @@ class SorobanTest extends TestCase */ public function testSoroban(): void { - $server = new SorobanServer(self::SERVER_URL); - $server->enableLogging = true; - $sdk = StellarSDK::getTestNetInstance(); - - $accountAKeyPair = KeyPair::random(); - $accountAId = $accountAKeyPair->getAccountId(); - // get health - $getHealthResponse = $server->getHealth(); + $getHealthResponse = $this->server->getHealth(); $this->assertEquals(GetHealthResponse::HEALTHY, $getHealthResponse->status); - + if ($this->testOn === 'futurenet') { + $this->assertNotNull($getHealthResponse->ledgerRetentionWindow); + } // get network info - $getNetworkResponse = $server->getNetwork(); - $this->assertEquals("https://friendbot.stellar.org/", $getNetworkResponse->friendbotUrl); - $this->assertEquals("Test SDF Network ; September 2015", $getNetworkResponse->passphrase); - $this->assertNotNull($getNetworkResponse->protocolVersion); - - // fund account - if (!$sdk->accountExists($accountAId)) { - FriendBot::fundTestAccount($accountAId); - sleep(5); + $getNetworkResponse = $this->server->getNetwork(); + if ($this->testOn === 'testnet') { + $this->assertEquals("https://friendbot.stellar.org/", $getNetworkResponse->friendbotUrl); + $this->assertEquals("Test SDF Network ; September 2015", $getNetworkResponse->passphrase); + } elseif ($this->testOn === 'futurenet') { + $this->assertEquals("https://friendbot-futurenet.stellar.org/", $getNetworkResponse->friendbotUrl); + $this->assertEquals("Test SDF Future Network ; October 2022", $getNetworkResponse->passphrase); } - $this->restoreContractFootprint($server, $accountAKeyPair, self::HELLO_CONTRACT_PATH); + $this->assertNotNull($getNetworkResponse->protocolVersion); + + $this->restoreContractFootprint($this->server, $this->accountAKeyPair, self::HELLO_CONTRACT_PATH); // upload contract wasm $contractCode = file_get_contents(self::HELLO_CONTRACT_PATH, false); @@ -96,13 +121,13 @@ public function testSoroban(): void $op = (new InvokeHostFunctionOperationBuilder($uploadContractHostFunction))->build(); sleep(5); - $getAccountResponse = $sdk->requestAccount($accountAId); + $getAccountResponse = $this->sdk->requestAccount($this->accountAId); $transaction = (new TransactionBuilder($getAccountResponse)) ->addOperation($op)->build(); // simulate first to get the transaction data and fee $request = new SimulateTransactionRequest($transaction); - $simulateResponse = $server->simulateTransaction($request); + $simulateResponse = $this->server->simulateTransaction($request); $this->assertNull($simulateResponse->error); $this->assertNull($simulateResponse->resultError); @@ -121,7 +146,7 @@ public function testSoroban(): void // set the transaction data + fee and sign $transaction->setSorobanTransactionData($transactionData); $transaction->addResourceFee($minResourceFee); - $transaction->sign($accountAKeyPair, Network::testnet()); + $transaction->sign($this->accountAKeyPair, $this->network); // check transaction xdr encoding back and forth $transctionEnvelopeXdr = $transaction->toEnvelopeXdrBase64(); @@ -131,7 +156,7 @@ public function testSoroban(): void print($transaction->toEnvelopeXdrBase64() . PHP_EOL); // send the transaction - $sendResponse = $server->sendTransaction($transaction); + $sendResponse = $this->server->sendTransaction($transaction); $this->assertNull($sendResponse->error); $this->assertNotNull($sendResponse->hash); $this->assertNotNull($sendResponse->status); @@ -142,20 +167,20 @@ public function testSoroban(): void } // poll until status is success or error - $statusResponse = $this->pollStatus($server, $sendResponse->hash); + $statusResponse = $this->pollStatus($this->server, $sendResponse->hash); $this->assertNotNull($statusResponse); $helloContractWasmId = $statusResponse->getWasmId(); $this->assertNotNull($helloContractWasmId); //print("wasm id: " . $helloContractWasmId . PHP_EOL); - $contractCodeEntry = $server->loadContractCodeForWasmId($helloContractWasmId); + $contractCodeEntry = $this->server->loadContractCodeForWasmId($helloContractWasmId); $this->assertNotNull($contractCodeEntry); $loadedSourceCode = $contractCodeEntry->code->value; $this->assertEquals($contractCode, $loadedSourceCode); // check horizon response decoding. sleep(5); - $transactionResponse = $sdk->requestTransaction($sendResponse->hash); + $transactionResponse = $this->sdk->requestTransaction($sendResponse->hash); $this->assertEquals(1, $transactionResponse->getOperationCount()); $this->assertEquals($transctionEnvelopeXdr, $transactionResponse->getEnvelopeXdr()->toBase64Xdr()); $meta = $transactionResponse->getResultMetaXdrBase64(); @@ -165,7 +190,7 @@ public function testSoroban(): void //$this->assertEquals($meta, $metaXdr->toBase64Xdr()); // check horizon operation response - $operationsResponse = $sdk->operations()->forTransaction($sendResponse->hash)->limit(10)->order("desc")->execute(); + $operationsResponse = $this->sdk->operations()->forTransaction($sendResponse->hash)->limit(10)->order("desc")->execute(); $this->assertTrue($operationsResponse->getOperations()->count() == 1); $firstOp = $operationsResponse->getOperations()->toArray()[0]; if ($firstOp instanceof InvokeHostFunctionOperationResponse) { @@ -175,20 +200,20 @@ public function testSoroban(): void $this->fail(); } - $this->bumpContractCodeFootprint($server, $accountAKeyPair, $helloContractWasmId, 100000); + $this->bumpContractCodeFootprint($this->server, $this->accountAKeyPair, $helloContractWasmId, 100000); // create contract - $createContractHostFunction = new CreateContractHostFunction(Address::fromAccountId($accountAId), $helloContractWasmId); + $createContractHostFunction = new CreateContractHostFunction(Address::fromAccountId($this->accountAId), $helloContractWasmId); $builder = new InvokeHostFunctionOperationBuilder($createContractHostFunction); $op = $builder->build(); sleep(5); - $accountA = $sdk->requestAccount($accountAId); + $accountA = $this->sdk->requestAccount($this->accountAId); $transaction = (new TransactionBuilder($accountA)) ->addOperation($op)->build(); // simulate first to get the transaction data + fee $request = new SimulateTransactionRequest($transaction); - $simulateResponse = $server->simulateTransaction($request); + $simulateResponse = $this->server->simulateTransaction($request); $this->assertNull($simulateResponse->error); $this->assertNull($simulateResponse->resultError); @@ -206,7 +231,7 @@ public function testSoroban(): void $transaction->setSorobanTransactionData($simulateResponse->transactionData); $transaction->setSorobanAuth($simulateResponse->getSorobanAuth()); $transaction->addResourceFee($simulateResponse->minResourceFee); - $transaction->sign($accountAKeyPair, Network::testnet()); + $transaction->sign($this->accountAKeyPair, $this->network); // check transaction xdr encoding back and forth $transctionEnvelopeXdr = $transaction->toEnvelopeXdrBase64(); @@ -214,21 +239,21 @@ public function testSoroban(): void Transaction::fromEnvelopeBase64XdrString($transctionEnvelopeXdr)->toEnvelopeXdrBase64()); // send the transaction - $sendResponse = $server->sendTransaction($transaction); + $sendResponse = $this->server->sendTransaction($transaction); $this->assertNull($sendResponse->error); $this->assertNotNull($sendResponse->hash); $this->assertNotNull($sendResponse->status); $this->assertNotEquals(SendTransactionResponse::STATUS_ERROR, $sendResponse->status); // poll until status is success or error - $statusResponse = $this->pollStatus($server, $sendResponse->hash); + $statusResponse = $this->pollStatus($this->server, $sendResponse->hash); $this->assertNotNull($statusResponse); $helloContractId = $statusResponse->getCreatedContractId(); $this->assertNotNull($helloContractId); - //print("contract id: " . $helloContractId . PHP_EOL); + print("contract id: " . $helloContractId . PHP_EOL); - $contractCodeEntry = $server->loadContractCodeForContractId($helloContractId); + $contractCodeEntry = $this->server->loadContractCodeForContractId($helloContractId); $this->assertNotNull($contractCodeEntry); $loadedSourceCode = $contractCodeEntry->code->value; $this->assertEquals($contractCode, $loadedSourceCode); @@ -236,7 +261,7 @@ public function testSoroban(): void sleep(5); // check horizon response decoding. - $transactionResponse = $sdk->requestTransaction($sendResponse->hash); + $transactionResponse = $this->sdk->requestTransaction($sendResponse->hash); $this->assertEquals(1, $transactionResponse->getOperationCount()); $this->assertEquals($transctionEnvelopeXdr, $transactionResponse->getEnvelopeXdr()->toBase64Xdr()); $meta = $transactionResponse->getResultMetaXdrBase64(); @@ -245,7 +270,7 @@ public function testSoroban(): void $metaXdr = XdrTransactionMeta::fromBase64Xdr($meta); // check horizon operation response - $operationsResponse = $sdk->operations()->forTransaction($sendResponse->hash)->limit(10)->order("desc")->execute(); + $operationsResponse = $this->sdk->operations()->forTransaction($sendResponse->hash)->limit(10)->order("desc")->execute(); $this->assertTrue($operationsResponse->getOperations()->count() == 1); $firstOp = $operationsResponse->getOperations()->toArray()[0]; if ($firstOp instanceof InvokeHostFunctionOperationResponse) { @@ -262,7 +287,7 @@ public function testSoroban(): void $contractDataKey = $footprint->getContractDataLedgerKey(); $this->assertNotNull($contractDataKey); - $contractCodeEntryResponse = $server->getLedgerEntries([$contractCodeKey]); + $contractCodeEntryResponse = $this->server->getLedgerEntries([$contractCodeKey]); $this->assertNotNull($contractCodeEntryResponse->entries); $this->assertCount(1,$contractCodeEntryResponse->entries); $this->assertNotNull($contractCodeEntryResponse->latestLedger); @@ -270,7 +295,7 @@ public function testSoroban(): void $this->assertNotNull($contractCodeEntryResponse->entries[0]->lastModifiedLedgerSeq); $this->assertNotNull($contractCodeEntryResponse->entries[0]->getLedgerEntryDataXdr()); - $contractDataEntryResponse = $server->getLedgerEntries([$contractDataKey]); + $contractDataEntryResponse = $this->server->getLedgerEntries([$contractDataKey]); $this->assertNotNull($contractDataEntryResponse->entries); $this->assertCount(1,$contractDataEntryResponse->entries); $this->assertNotNull($contractDataEntryResponse->latestLedger); @@ -280,7 +305,8 @@ public function testSoroban(): void // invoke contract $argVal = XdrSCVal::forSymbol("friend"); - $accountA = $sdk->requestAccount($accountAId); + $accountA = $this->sdk->requestAccount($this->accountAId); + $invokeContractHostFunction = new InvokeContractHostFunction($helloContractId, "hello", [$argVal]); $builder = new InvokeHostFunctionOperationBuilder($invokeContractHostFunction); $op = $builder->build(); @@ -290,7 +316,7 @@ public function testSoroban(): void // simulate first to get the transaction data + fee $request = new SimulateTransactionRequest($transaction); - $simulateResponse = $server->simulateTransaction($request); + $simulateResponse = $this->server->simulateTransaction($request); $this->assertNull($simulateResponse->error); $this->assertNull($simulateResponse->resultError); @@ -306,7 +332,7 @@ public function testSoroban(): void // set the transaction data + fee and sign $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->minResourceFee); - $transaction->sign($accountAKeyPair, Network::testnet()); + $transaction->sign($this->accountAKeyPair, $this->network); // check transaction xdr encoding back and forth $transctionEnvelopeXdr = $transaction->toEnvelopeXdrBase64(); @@ -314,14 +340,14 @@ public function testSoroban(): void Transaction::fromEnvelopeBase64XdrString($transctionEnvelopeXdr)->toEnvelopeXdrBase64()); // send the transaction - $sendResponse = $server->sendTransaction($transaction); + $sendResponse = $this->server->sendTransaction($transaction); $this->assertNull($sendResponse->error); $this->assertNotNull($sendResponse->hash); $this->assertNotNull($sendResponse->status); $this->assertNotEquals(SendTransactionResponse::STATUS_ERROR, $sendResponse->status); // poll until status is success or error - $statusResponse = $this->pollStatus($server, $sendResponse->hash); + $statusResponse = $this->pollStatus($this->server, $sendResponse->hash); $this->assertNotNull($statusResponse); $resultValue = $statusResponse->getResultValue(); @@ -341,7 +367,7 @@ public function testSoroban(): void sleep(5); // check horizon response decoding. - $transactionResponse = $sdk->requestTransaction($sendResponse->hash); + $transactionResponse = $this->sdk->requestTransaction($sendResponse->hash); $this->assertEquals(1, $transactionResponse->getOperationCount()); $this->assertEquals($transctionEnvelopeXdr, $transactionResponse->getEnvelopeXdr()->toBase64Xdr()); $meta = $transactionResponse->getResultMetaXdrBase64(); @@ -351,7 +377,7 @@ public function testSoroban(): void //$this->assertEquals($meta, $metaXdr->toBase64Xdr()); // check horizon operation response - $operationsResponse = $sdk->operations()->forTransaction($sendResponse->hash)->limit(10)->order("desc")->execute(); + $operationsResponse = $this->sdk->operations()->forTransaction($sendResponse->hash)->limit(10)->order("desc")->execute(); $this->assertTrue($operationsResponse->getOperations()->count() == 1); $firstOp = $operationsResponse->getOperations()->toArray()[0]; if ($firstOp instanceof InvokeHostFunctionOperationResponse) { @@ -368,8 +394,8 @@ public function testSoroban(): void } // deploy create token contract with source account - $accountA = $sdk->requestAccount($accountAId); - $deploySACWithSourceAccountHostFunction = new DeploySACWithSourceAccountHostFunction(Address::fromAccountId($accountAId)); + $accountA = $this->sdk->requestAccount($this->accountAId); + $deploySACWithSourceAccountHostFunction = new DeploySACWithSourceAccountHostFunction(Address::fromAccountId($this->accountAId)); $builder = new InvokeHostFunctionOperationBuilder($deploySACWithSourceAccountHostFunction); $op = $builder->build(); @@ -378,7 +404,7 @@ public function testSoroban(): void // simulate first to get the transaction data + fee $request = new SimulateTransactionRequest($transaction); - $simulateResponse = $server->simulateTransaction($request); + $simulateResponse = $this->server->simulateTransaction($request); $this->assertNull($simulateResponse->error); $this->assertNull($simulateResponse->resultError); @@ -391,6 +417,14 @@ public function testSoroban(): void $this->assertGreaterThan(1, $simulateResponse->cost->cpuInsns); $this->assertGreaterThan(1, $simulateResponse->cost->memBytes); $this->assertGreaterThan(1, $simulateResponse->minResourceFee); + if ($this->testOn === 'futurenet') { + $this->assertNotNull($simulateResponse->stateChanges); + $stateChange = $simulateResponse->stateChanges[0]; + $stateChange->getKeyXdr(); + $after = $stateChange->getAfterXdr(); + assertNotNull($after); + } + /*print("Cost cpu: " . $simulateResponse->cost->cpuInsns . PHP_EOL); print("Cost mem: " . $simulateResponse->cost->memBytes . PHP_EOL); print("min res fee: " . $simulateResponse->minResourceFee . PHP_EOL);*/ @@ -399,7 +433,7 @@ public function testSoroban(): void $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->setSorobanAuth($simulateResponse->getSorobanAuth()); $transaction->addResourceFee($simulateResponse->minResourceFee); - $transaction->sign($accountAKeyPair, Network::testnet()); + $transaction->sign($this->accountAKeyPair, $this->network); // check transaction xdr encoding back and forth $transctionEnvelopeXdr = $transaction->toEnvelopeXdrBase64(); @@ -407,21 +441,21 @@ public function testSoroban(): void Transaction::fromEnvelopeBase64XdrString($transctionEnvelopeXdr)->toEnvelopeXdrBase64()); // send the transaction - $sendResponse = $server->sendTransaction($transaction); + $sendResponse = $this->server->sendTransaction($transaction); $this->assertNull($sendResponse->error); $this->assertNotNull($sendResponse->hash); $this->assertNotNull($sendResponse->status); $this->assertNotEquals(SendTransactionResponse::STATUS_ERROR, $sendResponse->status); // poll until status is success or error - $statusResponse = $this->pollStatus($server, $sendResponse->hash); + $statusResponse = $this->pollStatus($this->server, $sendResponse->hash); $this->assertNotNull($statusResponse); $ctcId = $statusResponse->getCreatedContractId(); $this->assertNotNull($ctcId); sleep(5); // check horizon response decoding. - $transactionResponse = $sdk->requestTransaction($sendResponse->hash); + $transactionResponse = $this->sdk->requestTransaction($sendResponse->hash); $this->assertEquals(1, $transactionResponse->getOperationCount()); $this->assertEquals($transctionEnvelopeXdr, $transactionResponse->getEnvelopeXdr()->toBase64Xdr()); $meta = $transactionResponse->getResultMetaXdrBase64(); @@ -431,7 +465,7 @@ public function testSoroban(): void //$this->assertEquals($meta, $metaXdr->toBase64Xdr()); // check horizon operation response - $operationsResponse = $sdk->operations()->forTransaction($sendResponse->hash)->limit(10)->order("desc")->execute(); + $operationsResponse = $this->sdk->operations()->forTransaction($sendResponse->hash)->limit(10)->order("desc")->execute(); $this->assertTrue($operationsResponse->getOperations()->count() == 1); $firstOp = $operationsResponse->getOperations()->toArray()[0]; if ($firstOp instanceof InvokeHostFunctionOperationResponse) { @@ -446,25 +480,31 @@ public function testSoroban(): void // prepare account and asset $accountBKeyPair = KeyPair::random(); $accountBId = $accountBKeyPair->getAccountId(); - FriendBot::fundTestAccount($accountBId); + // fund account + if ($this->testOn == 'testnet') { + FriendBot::fundTestAccount($accountBId); + } elseif ($this->testOn == 'futurenet') { + FuturenetFriendBot::fundTestAccount($accountBId); + } sleep(5); - $accountB = $sdk->requestAccount($accountBId); - $iomAsset = new AssetTypeCreditAlphanum4("IOM", $accountAId); + + $accountB = $this->sdk->requestAccount($accountBId); + $iomAsset = new AssetTypeCreditAlphanum4("IOM", $this->accountAId); $changeTrustBOperation = (new ChangeTrustOperationBuilder($iomAsset, "200999")) ->setSourceAccount($accountBId)->build(); - $paymentOperation = (new PaymentOperationBuilder($accountBId, $iomAsset, "100"))->setSourceAccount($accountAId)->build(); + $paymentOperation = (new PaymentOperationBuilder($accountBId, $iomAsset, "100"))->setSourceAccount($this->accountAId)->build(); $transaction = (new TransactionBuilder($accountB)) ->addOperation($changeTrustBOperation) ->addOperation($paymentOperation) ->build(); - $transaction->sign($accountAKeyPair, Network::testnet()); - $transaction->sign($accountBKeyPair, Network::testnet()); - $response = $sdk->submitTransaction($transaction); + $transaction->sign($this->accountAKeyPair, $this->network); + $transaction->sign($accountBKeyPair, $this->network); + $response = $this->sdk->submitTransaction($transaction); $this->assertTrue($response->isSuccessful()); // simulate sleep(5); - $accountB = $sdk->requestAccount($accountBId); + $accountB = $this->sdk->requestAccount($accountBId); $deploySACWithAssetHostFunction = new DeploySACWithAssetHostFunction($iomAsset); $builder = new InvokeHostFunctionOperationBuilder($deploySACWithAssetHostFunction); @@ -475,7 +515,7 @@ public function testSoroban(): void // simulate first to get the transaction data + fee $request = new SimulateTransactionRequest($transaction); - $simulateResponse = $server->simulateTransaction($request); + $simulateResponse = $this->server->simulateTransaction($request); $this->assertNull($simulateResponse->error); $this->assertNull($simulateResponse->resultError); @@ -493,7 +533,7 @@ public function testSoroban(): void // set the transaction data + fee and sign $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->minResourceFee); - $transaction->sign($accountBKeyPair, Network::testnet()); + $transaction->sign($accountBKeyPair, $this->network); // check transaction xdr encoding back and forth $transctionEnvelopeXdr = $transaction->toEnvelopeXdrBase64(); @@ -501,14 +541,14 @@ public function testSoroban(): void Transaction::fromEnvelopeBase64XdrString($transctionEnvelopeXdr)->toEnvelopeXdrBase64()); // send the transaction - $sendResponse = $server->sendTransaction($transaction); + $sendResponse = $this->server->sendTransaction($transaction); $this->assertNull($sendResponse->error); $this->assertNotNull($sendResponse->hash); $this->assertNotNull($sendResponse->status); $this->assertNotEquals(SendTransactionResponse::STATUS_ERROR, $sendResponse->status); // poll until status is success or error - $statusResponse = $this->pollStatus($server, $sendResponse->hash); + $statusResponse = $this->pollStatus($this->server, $sendResponse->hash); $this->assertNotNull($statusResponse); $ctcId = $statusResponse->getCreatedContractId(); $this->assertNotNull($ctcId); @@ -516,7 +556,7 @@ public function testSoroban(): void sleep(5); // check horizon response decoding. - $transactionResponse = $sdk->requestTransaction($sendResponse->hash); + $transactionResponse = $this->sdk->requestTransaction($sendResponse->hash); $this->assertEquals(1, $transactionResponse->getOperationCount()); $this->assertEquals($transctionEnvelopeXdr, $transactionResponse->getEnvelopeXdr()->toBase64Xdr()); $meta = $transactionResponse->getResultMetaXdrBase64(); @@ -526,7 +566,7 @@ public function testSoroban(): void //$this->assertEquals($meta, $metaXdr->toBase64Xdr()); // check horizon operation response - $operationsResponse = $sdk->operations()->forTransaction($sendResponse->hash)->limit(10)->order("desc")->execute(); + $operationsResponse = $this->sdk->operations()->forTransaction($sendResponse->hash)->limit(10)->order("desc")->execute(); $this->assertTrue($operationsResponse->getOperations()->count() == 1); $firstOp = $operationsResponse->getOperations()->toArray()[0]; if ($firstOp instanceof InvokeHostFunctionOperationResponse) { @@ -539,21 +579,14 @@ public function testSoroban(): void public function testSorobanEvents(): void { - $server = new SorobanServer(self::SERVER_URL); - $server->enableLogging = true; - $sdk = StellarSDK::getTestNetInstance(); - - $accountAKeyPair = KeyPair::random(); - $accountAId = $accountAKeyPair->getAccountId(); - FriendBot::fundTestAccount($accountAId); - $contractId = $this->deployContract($server, self::EVENTS_CONTRACT_PATH, $accountAKeyPair); + $contractId = $this->deployContract($this->server, self::EVENTS_CONTRACT_PATH, $this->accountAKeyPair); sleep(3); // invoke $fnName = "increment"; - $accountA = $sdk->requestAccount($accountAId); + $accountA = $this->sdk->requestAccount($this->accountAId); $invokeContractHostFunction = new InvokeContractHostFunction($contractId, $fnName); $builder = new InvokeHostFunctionOperationBuilder($invokeContractHostFunction); $op = $builder->build(); @@ -563,7 +596,7 @@ public function testSorobanEvents(): void // simulate first to get the transaction data and fee $request = new SimulateTransactionRequest($transaction); - $simulateResponse = $server->simulateTransaction($request); + $simulateResponse = $this->server->simulateTransaction($request); $this->assertNull($simulateResponse->error); $this->assertNull($simulateResponse->resultError); @@ -575,19 +608,19 @@ public function testSorobanEvents(): void // set the transaction data + fee and sign $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->minResourceFee); - $transaction->sign($accountAKeyPair, Network::testnet()); + $transaction->sign($this->accountAKeyPair, $this->network); // send the transaction - $sendResponse = $server->sendTransaction($transaction); + $sendResponse = $this->server->sendTransaction($transaction); $this->assertNull($sendResponse->error); $this->assertNotEquals(SendTransactionResponse::STATUS_ERROR, $sendResponse->status); // poll until status is success or error - $statusResponse = $this->pollStatus($server, $sendResponse->hash); + $statusResponse = $this->pollStatus($this->server, $sendResponse->hash); $this->assertNotNull($statusResponse); sleep(5); - $transactionResponse = $sdk->requestTransaction($sendResponse->hash); + $transactionResponse = $this->sdk->requestTransaction($sendResponse->hash); $this->assertEquals(1, $transactionResponse->getOperationCount()); // get events @@ -604,7 +637,7 @@ public function testSorobanEvents(): void $eventFilters->add($eventFilter); $request = new GetEventsRequest($startLedger, $eventFilters); - $response = $server->getEvents($request); + $response = $this->server->getEvents($request); $this->assertGreaterThan(0, count($response->events)); } @@ -629,7 +662,7 @@ private function pollStatus(SorobanServer $server, string $transactionId) : ?Get $status = GetTransactionResponse::STATUS_NOT_FOUND; while ($status == GetTransactionResponse::STATUS_NOT_FOUND) { sleep(3); - $statusResponse = $server->getTransaction($transactionId); + $statusResponse = $this->server->getTransaction($transactionId); $this->assertNull($statusResponse->error); $this->assertNotNull($statusResponse->status); $status = $statusResponse->status; @@ -645,9 +678,8 @@ private function pollStatus(SorobanServer $server, string $transactionId) : ?Get private function deployContract(SorobanServer $server, String $pathToCode, KeyPair $submitterKp) : String { sleep(5); - $sdk = StellarSDK::getTestNetInstance(); - $this->restoreContractFootprint($server, $submitterKp, $pathToCode); + $this->restoreContractFootprint($this->server, $submitterKp, $pathToCode); // upload contract wasm $contractCode = file_get_contents($pathToCode, false); @@ -658,30 +690,30 @@ private function deployContract(SorobanServer $server, String $pathToCode, KeyPa sleep(5); $submitterId = $submitterKp->getAccountId(); - $account = $sdk->requestAccount($submitterId); + $account = $this->sdk->requestAccount($submitterId); $transaction = (new TransactionBuilder($account))->addOperation($op)->build(); // simulate first to get the transaction data and resource fee $request = new SimulateTransactionRequest($transaction); - $simulateResponse = $server->simulateTransaction($request); + $simulateResponse = $this->server->simulateTransaction($request); // set the transaction data + fee and sign $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->minResourceFee); - $transaction->sign($submitterKp, Network::testnet()); + $transaction->sign($submitterKp, $this->network); // send the transaction - $sendResponse = $server->sendTransaction($transaction); + $sendResponse = $this->server->sendTransaction($transaction); $this->assertNotEquals(SendTransactionResponse::STATUS_ERROR, $sendResponse->status); // poll until status is success or error - $statusResponse = $this->pollStatus($server, $sendResponse->hash); + $statusResponse = $this->pollStatus($this->server, $sendResponse->hash); $this->assertNotNull($statusResponse); $wasmId = $statusResponse->getWasmId(); $this->assertNotNull($wasmId); - $this->bumpContractCodeFootprint($server, $submitterKp, $wasmId, 100000); + $this->bumpContractCodeFootprint($this->server, $submitterKp, $wasmId, 100000); // create contract $createContractHostFunction = new CreateContractHostFunction(Address::fromAccountId($submitterId), $wasmId); @@ -689,26 +721,26 @@ private function deployContract(SorobanServer $server, String $pathToCode, KeyPa $op = $builder->build(); sleep(5); - $account = $sdk->requestAccount($submitterId); + $account = $this->sdk->requestAccount($submitterId); $transaction = (new TransactionBuilder($account)) ->addOperation($op)->build(); // simulate first to get the transaction data and resource fee $request = new SimulateTransactionRequest($transaction); - $simulateResponse = $server->simulateTransaction($request); + $simulateResponse = $this->server->simulateTransaction($request); // set the transaction data + fee and sign $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->minResourceFee); $transaction->setSorobanAuth($simulateResponse->getSorobanAuth()); - $transaction->sign($submitterKp, Network::testnet()); + $transaction->sign($submitterKp, $this->network); // send the transaction - $sendResponse = $server->sendTransaction($transaction); + $sendResponse = $this->server->sendTransaction($transaction); $this->assertNotEquals(SendTransactionResponse::STATUS_ERROR, $sendResponse->status); // poll until status is success or error - $statusResponse = $this->pollStatus($server, $sendResponse->hash); + $statusResponse = $this->pollStatus($this->server, $sendResponse->hash); $this->assertNotNull($statusResponse); $contractId = $statusResponse->getCreatedContractId(); $this->assertNotNull($contractId); @@ -717,19 +749,18 @@ private function deployContract(SorobanServer $server, String $pathToCode, KeyPa private function restoreContractFootprint(SorobanServer $server, KeyPair $accountKeyPair, string $contractCodePath) : void { sleep(5); - $sdk = StellarSDK::getTestNetInstance(); $contractCode = file_get_contents($contractCodePath, false); $uploadContractHostFunction = new UploadContractWasmHostFunction($contractCode); $op = (new InvokeHostFunctionOperationBuilder($uploadContractHostFunction))->build(); - $accountAId = $accountKeyPair->getAccountId(); - $getAccountResponse = $sdk->requestAccount($accountAId); + $this->accountAId = $accountKeyPair->getAccountId(); + $getAccountResponse = $this->sdk->requestAccount($this->accountAId); $transaction = (new TransactionBuilder($getAccountResponse)) ->addOperation($op)->build(); $request = new SimulateTransactionRequest($transaction); - $simulateResponse = $server->simulateTransaction($request); + $simulateResponse = $this->server->simulateTransaction($request); $this->assertNull($simulateResponse->error); $this->assertNull($simulateResponse->resultError); @@ -739,14 +770,14 @@ private function restoreContractFootprint(SorobanServer $server, KeyPair $accoun $transactionData->resources->footprint->readWrite = $transactionData->resources->footprint->readWrite + $transactionData->resources->footprint->readOnly; $transactionData->resources->footprint->readOnly = array(); - $getAccountResponse = $sdk->requestAccount($accountAId); + $getAccountResponse = $this->sdk->requestAccount($this->accountAId); $restoreOp = (new RestoreFootprintOperationBuilder())->build(); $transaction = (new TransactionBuilder($getAccountResponse)) ->addOperation($restoreOp)->build(); $transaction->setSorobanTransactionData($transactionData) ; $request = new SimulateTransactionRequest($transaction); - $simulateResponse = $server->simulateTransaction($request); + $simulateResponse = $this->server->simulateTransaction($request); $this->assertNull($simulateResponse->error); $this->assertNull($simulateResponse->resultError); @@ -756,7 +787,7 @@ private function restoreContractFootprint(SorobanServer $server, KeyPair $accoun // set the transaction data + fee and sign $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->getMinResourceFee()); - $transaction->sign($accountKeyPair, Network::testnet()); + $transaction->sign($accountKeyPair, $this->network); // check transaction xdr encoding back and forth $transctionEnvelopeXdr = $transaction->toEnvelopeXdrBase64(); @@ -764,27 +795,26 @@ private function restoreContractFootprint(SorobanServer $server, KeyPair $accoun Transaction::fromEnvelopeBase64XdrString($transctionEnvelopeXdr)->toEnvelopeXdrBase64()); // send the transaction - $sendResponse = $server->sendTransaction($transaction); + $sendResponse = $this->server->sendTransaction($transaction); $this->assertNull($sendResponse->error); $this->assertNotNull($sendResponse->hash); $this->assertNotNull($sendResponse->status); $this->assertNotEquals(SendTransactionResponse::STATUS_ERROR, $sendResponse->status); // poll until status is success or error - $statusResponse = $this->pollStatus($server, $sendResponse->hash); + $statusResponse = $this->pollStatus($this->server, $sendResponse->hash); $this->assertNotNull($statusResponse); $this->assertEquals(GetTransactionResponse::STATUS_SUCCESS, $statusResponse->status); } private function bumpContractCodeFootprint(SorobanServer $server, KeyPair $accountKeyPair, string $wasmId, int $extendTo) : void { sleep(5); - $sdk = StellarSDK::getTestNetInstance(); $builder = new ExtendFootprintTTLOperationBuilder($extendTo); $bumpOp = $builder->build(); - $accountAId = $accountKeyPair->getAccountId(); - $getAccountResponse = $sdk->requestAccount($accountAId); + $this->accountAId = $accountKeyPair->getAccountId(); + $getAccountResponse = $this->sdk->requestAccount($this->accountAId); $transaction = (new TransactionBuilder($getAccountResponse)) ->addOperation($bumpOp)->build(); @@ -802,7 +832,7 @@ private function bumpContractCodeFootprint(SorobanServer $server, KeyPair $accou //$resourceConfig = new ResourceConfig(4000000); //$request = new SimulateTransactionRequest($transaction, $resourceConfig); $request = new SimulateTransactionRequest($transaction); - $simulateResponse = $server->simulateTransaction($request); + $simulateResponse = $this->server->simulateTransaction($request); $this->assertNull($simulateResponse->error); $this->assertNull($simulateResponse->resultError); @@ -812,7 +842,7 @@ private function bumpContractCodeFootprint(SorobanServer $server, KeyPair $accou // set the transaction data + fee and sign $transaction->setSorobanTransactionData($simulateResponse->getTransactionData()); $transaction->addResourceFee($simulateResponse->getMinResourceFee()); - $transaction->sign($accountKeyPair, Network::testnet()); + $transaction->sign($accountKeyPair, $this->network); // check transaction xdr encoding back and forth $transctionEnvelopeXdr = $transaction->toEnvelopeXdrBase64(); @@ -820,14 +850,14 @@ private function bumpContractCodeFootprint(SorobanServer $server, KeyPair $accou Transaction::fromEnvelopeBase64XdrString($transctionEnvelopeXdr)->toEnvelopeXdrBase64()); // send the transaction - $sendResponse = $server->sendTransaction($transaction); + $sendResponse = $this->server->sendTransaction($transaction); $this->assertNull($sendResponse->error); $this->assertNotNull($sendResponse->hash); $this->assertNotNull($sendResponse->status); $this->assertNotEquals(SendTransactionResponse::STATUS_ERROR, $sendResponse->status); // poll until status is success or error - $statusResponse = $this->pollStatus($server, $sendResponse->hash); + $statusResponse = $this->pollStatus($this->server, $sendResponse->hash); $this->assertNotNull($statusResponse); $this->assertEquals(GetTransactionResponse::STATUS_SUCCESS, $statusResponse->status); }