forked from zulucrypto/stellar-api
-
Notifications
You must be signed in to change notification settings - Fork 4
/
transaction-result.php
72 lines (57 loc) · 2.12 KB
/
transaction-result.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
require '../vendor/autoload.php';
use ZuluCrypto\StellarSdk\Horizon\Exception\PostTransactionException;
use ZuluCrypto\StellarSdk\Keypair;
use ZuluCrypto\StellarSdk\Server;
$server = Server::testNet();
// GD4JRFLPF4AGYQTLCMZ7Q7DRLGQZQTGWOOKDUCNRVKG66G5ZVYYFT76M
$sourceKeypair = Keypair::newFromSeed('SAA2U5UFW65DW3MLVX734BUQIHAWANQNBLTFT47X2NVVBCN7X6QC5AOG');
$newAccount1 = Keypair::newFromRandom();
$newAccount2 = Keypair::newFromRandom();
// ---------------------------------------------------------
// create two new accounts
$response = $server->buildTransaction($sourceKeypair)
->addCreateAccountOp($newAccount1, 5)
->addCreateAccountOp($newAccount2, 5)
->submit($sourceKeypair);
/*
* Get information on the overall result of the transaction
*/
/** @var \ZuluCrypto\StellarSdk\XdrModel\TransactionResult $result */
$result = $response->getResult();
print "Fee charged: " . $result->getFeeCharged()->getScaledValue() . " XLM" . PHP_EOL;
/*
* Each operation within the transaction has its own result
*/
$operationResults = $result->getOperationResults();
/*
* Each result will be a child class of OperationResult depending on what the
* original operation was.
*
* See these classes for additional details that can be retrieved for each type
* of result
*/
foreach ($operationResults as $operationResult) {
print "Operation result is a: " . get_class($operationResult) . PHP_EOL;
}
/*
* Exception handling
*/
// This transaction will fail because there aren't enough lumens in the source
// account
try {
$response = $server->buildTransaction($sourceKeypair)
->addLumenPayment($newAccount1, 9999999)
->submit($sourceKeypair);
} catch (PostTransactionException $e) {
// Details operation information can be retrieved from this exception
$operationResults = $e->getResult()->getOperationResults();
foreach ($operationResults as $result) {
// Skip through the ones that worked
if ($result->succeeded()) {
continue;
}
// Print out the first failed one
print "Operation failed with code: " . $result->getErrorCode() . PHP_EOL;
}
}